xref: /openbmc/linux/drivers/video/fbdev/acornfb.c (revision 2aceb3a8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/video/acornfb.c
4  *
5  *  Copyright (C) 1998-2001 Russell King
6  *
7  * Frame buffer code for Acorn platforms
8  *
9  * NOTE: Most of the modes with X!=640 will disappear shortly.
10  * NOTE: Startup setting of HS & VS polarity not supported.
11  *       (do we need to support it if we're coming up in 640x480?)
12  *
13  * FIXME: (things broken by the "new improved" FBCON API)
14  *  - Blanking 8bpp displays with VIDC
15  */
16 
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/fb.h>
25 #include <linux/platform_device.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/io.h>
28 #include <linux/gfp.h>
29 
30 #include <mach/hardware.h>
31 #include <asm/irq.h>
32 #include <asm/mach-types.h>
33 
34 #include "acornfb.h"
35 
36 /*
37  * Default resolution.
38  * NOTE that it has to be supported in the table towards
39  * the end of this file.
40  */
41 #define DEFAULT_XRES	640
42 #define DEFAULT_YRES	480
43 #define DEFAULT_BPP	4
44 
45 /*
46  * define this to debug the video mode selection
47  */
48 #undef DEBUG_MODE_SELECTION
49 
50 /*
51  * Translation from RISC OS monitor types to actual
52  * HSYNC and VSYNC frequency ranges.  These are
53  * probably not right, but they're the best info I
54  * have.  Allow 1% either way on the nominal for TVs.
55  */
56 #define NR_MONTYPES	6
57 static struct fb_monspecs monspecs[NR_MONTYPES] = {
58 	{	/* TV		*/
59 		.hfmin	= 15469,
60 		.hfmax	= 15781,
61 		.vfmin	= 49,
62 		.vfmax	= 51,
63 	}, {	/* Multi Freq	*/
64 		.hfmin	= 0,
65 		.hfmax	= 99999,
66 		.vfmin	= 0,
67 		.vfmax	= 199,
68 	}, {	/* Hi-res mono	*/
69 		.hfmin	= 58608,
70 		.hfmax	= 58608,
71 		.vfmin	= 64,
72 		.vfmax	= 64,
73 	}, {	/* VGA		*/
74 		.hfmin	= 30000,
75 		.hfmax	= 70000,
76 		.vfmin	= 60,
77 		.vfmax	= 60,
78 	}, {	/* SVGA		*/
79 		.hfmin	= 30000,
80 		.hfmax	= 70000,
81 		.vfmin	= 56,
82 		.vfmax	= 75,
83 	}, {
84 		.hfmin	= 30000,
85 		.hfmax	= 70000,
86 		.vfmin	= 60,
87 		.vfmax	= 60,
88 	}
89 };
90 
91 static struct fb_info fb_info;
92 static struct acornfb_par current_par;
93 static struct vidc_timing current_vidc;
94 
95 extern unsigned int vram_size;	/* set by setup.c */
96 
97 #ifdef HAS_VIDC20
98 #include <mach/acornfb.h>
99 
100 #define MAX_SIZE	(2*1024*1024)
101 
102 /* VIDC20 has a different set of rules from the VIDC:
103  *  hcr  : must be multiple of 4
104  *  hswr : must be even
105  *  hdsr : must be even
106  *  hder : must be even
107  *  vcr  : >= 2, (interlace, must be odd)
108  *  vswr : >= 1
109  *  vdsr : >= 1
110  *  vder : >= vdsr
111  */
acornfb_set_timing(struct fb_info * info)112 static void acornfb_set_timing(struct fb_info *info)
113 {
114 	struct fb_var_screeninfo *var = &info->var;
115 	struct vidc_timing vidc;
116 	u_int vcr, fsize;
117 	u_int ext_ctl, dat_ctl;
118 	u_int words_per_line;
119 
120 	memset(&vidc, 0, sizeof(vidc));
121 
122 	vidc.h_sync_width	= var->hsync_len - 8;
123 	vidc.h_border_start	= vidc.h_sync_width + var->left_margin + 8 - 12;
124 	vidc.h_display_start	= vidc.h_border_start + 12 - 18;
125 	vidc.h_display_end	= vidc.h_display_start + var->xres;
126 	vidc.h_border_end	= vidc.h_display_end + 18 - 12;
127 	vidc.h_cycle		= vidc.h_border_end + var->right_margin + 12 - 8;
128 	vidc.h_interlace	= vidc.h_cycle / 2;
129 	vidc.v_sync_width	= var->vsync_len - 1;
130 	vidc.v_border_start	= vidc.v_sync_width + var->upper_margin;
131 	vidc.v_display_start	= vidc.v_border_start;
132 	vidc.v_display_end	= vidc.v_display_start + var->yres;
133 	vidc.v_border_end	= vidc.v_display_end;
134 	vidc.control		= acornfb_default_control();
135 
136 	vcr = var->vsync_len + var->upper_margin + var->yres +
137 	      var->lower_margin;
138 
139 	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
140 		vidc.v_cycle = (vcr - 3) / 2;
141 		vidc.control |= VIDC20_CTRL_INT;
142 	} else
143 		vidc.v_cycle = vcr - 2;
144 
145 	switch (var->bits_per_pixel) {
146 	case  1: vidc.control |= VIDC20_CTRL_1BPP;	break;
147 	case  2: vidc.control |= VIDC20_CTRL_2BPP;	break;
148 	case  4: vidc.control |= VIDC20_CTRL_4BPP;	break;
149 	default:
150 	case  8: vidc.control |= VIDC20_CTRL_8BPP;	break;
151 	case 16: vidc.control |= VIDC20_CTRL_16BPP;	break;
152 	case 32: vidc.control |= VIDC20_CTRL_32BPP;	break;
153 	}
154 
155 	acornfb_vidc20_find_rates(&vidc, var);
156 	fsize = var->vsync_len + var->upper_margin + var->lower_margin - 1;
157 
158 	if (memcmp(&current_vidc, &vidc, sizeof(vidc))) {
159 		current_vidc = vidc;
160 
161 		vidc_writel(VIDC20_CTRL | vidc.control);
162 		vidc_writel(0xd0000000 | vidc.pll_ctl);
163 		vidc_writel(0x80000000 | vidc.h_cycle);
164 		vidc_writel(0x81000000 | vidc.h_sync_width);
165 		vidc_writel(0x82000000 | vidc.h_border_start);
166 		vidc_writel(0x83000000 | vidc.h_display_start);
167 		vidc_writel(0x84000000 | vidc.h_display_end);
168 		vidc_writel(0x85000000 | vidc.h_border_end);
169 		vidc_writel(0x86000000);
170 		vidc_writel(0x87000000 | vidc.h_interlace);
171 		vidc_writel(0x90000000 | vidc.v_cycle);
172 		vidc_writel(0x91000000 | vidc.v_sync_width);
173 		vidc_writel(0x92000000 | vidc.v_border_start);
174 		vidc_writel(0x93000000 | vidc.v_display_start);
175 		vidc_writel(0x94000000 | vidc.v_display_end);
176 		vidc_writel(0x95000000 | vidc.v_border_end);
177 		vidc_writel(0x96000000);
178 		vidc_writel(0x97000000);
179 	}
180 
181 	iomd_writel(fsize, IOMD_FSIZE);
182 
183 	ext_ctl = acornfb_default_econtrol();
184 
185 	if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
186 		ext_ctl |= VIDC20_ECTL_HS_NCSYNC | VIDC20_ECTL_VS_NCSYNC;
187 	else {
188 		if (var->sync & FB_SYNC_HOR_HIGH_ACT)
189 			ext_ctl |= VIDC20_ECTL_HS_HSYNC;
190 		else
191 			ext_ctl |= VIDC20_ECTL_HS_NHSYNC;
192 
193 		if (var->sync & FB_SYNC_VERT_HIGH_ACT)
194 			ext_ctl |= VIDC20_ECTL_VS_VSYNC;
195 		else
196 			ext_ctl |= VIDC20_ECTL_VS_NVSYNC;
197 	}
198 
199 	vidc_writel(VIDC20_ECTL | ext_ctl);
200 
201 	words_per_line = var->xres * var->bits_per_pixel / 32;
202 
203 	if (current_par.using_vram && info->fix.smem_len == 2048*1024)
204 		words_per_line /= 2;
205 
206 	/* RiscPC doesn't use the VIDC's VRAM control. */
207 	dat_ctl = VIDC20_DCTL_VRAM_DIS | VIDC20_DCTL_SNA | words_per_line;
208 
209 	/* The data bus width is dependent on both the type
210 	 * and amount of video memory.
211 	 *     DRAM	32bit low
212 	 * 1MB VRAM	32bit
213 	 * 2MB VRAM	64bit
214 	 */
215 	if (current_par.using_vram && current_par.vram_half_sam == 2048)
216 		dat_ctl |= VIDC20_DCTL_BUS_D63_0;
217 	else
218 		dat_ctl |= VIDC20_DCTL_BUS_D31_0;
219 
220 	vidc_writel(VIDC20_DCTL | dat_ctl);
221 
222 #ifdef DEBUG_MODE_SELECTION
223 	printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
224 	       var->yres, var->bits_per_pixel);
225 	printk(KERN_DEBUG " H-cycle          : %d\n", vidc.h_cycle);
226 	printk(KERN_DEBUG " H-sync-width     : %d\n", vidc.h_sync_width);
227 	printk(KERN_DEBUG " H-border-start   : %d\n", vidc.h_border_start);
228 	printk(KERN_DEBUG " H-display-start  : %d\n", vidc.h_display_start);
229 	printk(KERN_DEBUG " H-display-end    : %d\n", vidc.h_display_end);
230 	printk(KERN_DEBUG " H-border-end     : %d\n", vidc.h_border_end);
231 	printk(KERN_DEBUG " H-interlace      : %d\n", vidc.h_interlace);
232 	printk(KERN_DEBUG " V-cycle          : %d\n", vidc.v_cycle);
233 	printk(KERN_DEBUG " V-sync-width     : %d\n", vidc.v_sync_width);
234 	printk(KERN_DEBUG " V-border-start   : %d\n", vidc.v_border_start);
235 	printk(KERN_DEBUG " V-display-start  : %d\n", vidc.v_display_start);
236 	printk(KERN_DEBUG " V-display-end    : %d\n", vidc.v_display_end);
237 	printk(KERN_DEBUG " V-border-end     : %d\n", vidc.v_border_end);
238 	printk(KERN_DEBUG " Ext Ctrl  (C)    : 0x%08X\n", ext_ctl);
239 	printk(KERN_DEBUG " PLL Ctrl  (D)    : 0x%08X\n", vidc.pll_ctl);
240 	printk(KERN_DEBUG " Ctrl      (E)    : 0x%08X\n", vidc.control);
241 	printk(KERN_DEBUG " Data Ctrl (F)    : 0x%08X\n", dat_ctl);
242 	printk(KERN_DEBUG " Fsize            : 0x%08X\n", fsize);
243 #endif
244 }
245 
246 /*
247  * We have to take note of the VIDC20's 16-bit palette here.
248  * The VIDC20 looks up a 16 bit pixel as follows:
249  *
250  *   bits   111111
251  *          5432109876543210
252  *   red            ++++++++  (8 bits,  7 to 0)
253  *  green       ++++++++      (8 bits, 11 to 4)
254  *   blue   ++++++++          (8 bits, 15 to 8)
255  *
256  * We use a pixel which looks like:
257  *
258  *   bits   111111
259  *          5432109876543210
260  *   red               +++++  (5 bits,  4 to  0)
261  *  green         +++++       (5 bits,  9 to  5)
262  *   blue    +++++            (5 bits, 14 to 10)
263  */
264 static int
acornfb_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int trans,struct fb_info * info)265 acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
266 		  u_int trans, struct fb_info *info)
267 {
268 	union palette pal;
269 
270 	if (regno >= current_par.palette_size)
271 		return 1;
272 
273 	if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
274 		u32 pseudo_val;
275 
276 		pseudo_val  = regno << info->var.red.offset;
277 		pseudo_val |= regno << info->var.green.offset;
278 		pseudo_val |= regno << info->var.blue.offset;
279 
280 		((u32 *)info->pseudo_palette)[regno] = pseudo_val;
281 	}
282 
283 	pal.p = 0;
284 	pal.vidc20.red   = red >> 8;
285 	pal.vidc20.green = green >> 8;
286 	pal.vidc20.blue  = blue >> 8;
287 
288 	current_par.palette[regno] = pal;
289 
290 	if (info->var.bits_per_pixel == 16) {
291 		int i;
292 
293 		pal.p = 0;
294 		vidc_writel(0x10000000);
295 		for (i = 0; i < 256; i += 1) {
296 			pal.vidc20.red   = current_par.palette[i       & 31].vidc20.red;
297 			pal.vidc20.green = current_par.palette[(i >> 1) & 31].vidc20.green;
298 			pal.vidc20.blue  = current_par.palette[(i >> 2) & 31].vidc20.blue;
299 			vidc_writel(pal.p);
300 			/* Palette register pointer auto-increments */
301 		}
302 	} else {
303 		vidc_writel(0x10000000 | regno);
304 		vidc_writel(pal.p);
305 	}
306 
307 	return 0;
308 }
309 #endif
310 
311 /*
312  * Before selecting the timing parameters, adjust
313  * the resolution to fit the rules.
314  */
315 static int
acornfb_adjust_timing(struct fb_info * info,struct fb_var_screeninfo * var,u_int fontht)316 acornfb_adjust_timing(struct fb_info *info, struct fb_var_screeninfo *var, u_int fontht)
317 {
318 	u_int font_line_len, sam_size, min_size, size, nr_y;
319 
320 	/* xres must be even */
321 	var->xres = (var->xres + 1) & ~1;
322 
323 	/*
324 	 * We don't allow xres_virtual to differ from xres
325 	 */
326 	var->xres_virtual = var->xres;
327 	var->xoffset = 0;
328 
329 	if (current_par.using_vram)
330 		sam_size = current_par.vram_half_sam * 2;
331 	else
332 		sam_size = 16;
333 
334 	/*
335 	 * Now, find a value for yres_virtual which allows
336 	 * us to do ywrap scrolling.  The value of
337 	 * yres_virtual must be such that the end of the
338 	 * displayable frame buffer must be aligned with
339 	 * the start of a font line.
340 	 */
341 	font_line_len = var->xres * var->bits_per_pixel * fontht / 8;
342 	min_size = var->xres * var->yres * var->bits_per_pixel / 8;
343 
344 	/*
345 	 * If minimum screen size is greater than that we have
346 	 * available, reject it.
347 	 */
348 	if (min_size > info->fix.smem_len)
349 		return -EINVAL;
350 
351 	/* Find int 'y', such that y * fll == s * sam < maxsize
352 	 * y = s * sam / fll; s = maxsize / sam
353 	 */
354 	for (size = info->fix.smem_len;
355 	     nr_y = size / font_line_len, min_size <= size;
356 	     size -= sam_size) {
357 		if (nr_y * font_line_len == size)
358 			break;
359 	}
360 	nr_y *= fontht;
361 
362 	if (var->accel_flags & FB_ACCELF_TEXT) {
363 		if (min_size > size) {
364 			/*
365 			 * failed, use ypan
366 			 */
367 			size = info->fix.smem_len;
368 			var->yres_virtual = size / (font_line_len / fontht);
369 		} else
370 			var->yres_virtual = nr_y;
371 	} else if (var->yres_virtual > nr_y)
372 		var->yres_virtual = nr_y;
373 
374 	current_par.screen_end = info->fix.smem_start + size;
375 
376 	/*
377 	 * Fix yres & yoffset if needed.
378 	 */
379 	if (var->yres > var->yres_virtual)
380 		var->yres = var->yres_virtual;
381 
382 	if (var->vmode & FB_VMODE_YWRAP) {
383 		if (var->yoffset > var->yres_virtual)
384 			var->yoffset = var->yres_virtual;
385 	} else {
386 		if (var->yoffset + var->yres > var->yres_virtual)
387 			var->yoffset = var->yres_virtual - var->yres;
388 	}
389 
390 	/* hsync_len must be even */
391 	var->hsync_len = (var->hsync_len + 1) & ~1;
392 
393 #if defined(HAS_VIDC20)
394 	/* left_margin must be even */
395 	if (var->left_margin & 1) {
396 		var->left_margin += 1;
397 		var->right_margin -= 1;
398 	}
399 
400 	/* right_margin must be even */
401 	if (var->right_margin & 1)
402 		var->right_margin += 1;
403 #endif
404 
405 	if (var->vsync_len < 1)
406 		var->vsync_len = 1;
407 
408 	return 0;
409 }
410 
411 static int
acornfb_validate_timing(struct fb_var_screeninfo * var,struct fb_monspecs * monspecs)412 acornfb_validate_timing(struct fb_var_screeninfo *var,
413 			struct fb_monspecs *monspecs)
414 {
415 	unsigned long hs, vs;
416 
417 	/*
418 	 * hs(Hz) = 10^12 / (pixclock * xtotal)
419 	 * vs(Hz) = hs(Hz) / ytotal
420 	 *
421 	 * No need to do long long divisions or anything
422 	 * like that if you factor it correctly
423 	 */
424 	hs = 1953125000 / var->pixclock;
425 	hs = hs * 512 /
426 	     (var->xres + var->left_margin + var->right_margin + var->hsync_len);
427 	vs = hs /
428 	     (var->yres + var->upper_margin + var->lower_margin + var->vsync_len);
429 
430 	return (vs >= monspecs->vfmin && vs <= monspecs->vfmax &&
431 		hs >= monspecs->hfmin && hs <= monspecs->hfmax) ? 0 : -EINVAL;
432 }
433 
434 static inline void
acornfb_update_dma(struct fb_info * info,struct fb_var_screeninfo * var)435 acornfb_update_dma(struct fb_info *info, struct fb_var_screeninfo *var)
436 {
437 	u_int off = var->yoffset * info->fix.line_length;
438 
439 #if defined(HAS_MEMC)
440 	memc_write(VDMA_INIT, off >> 2);
441 #elif defined(HAS_IOMD)
442 	iomd_writel(info->fix.smem_start + off, IOMD_VIDINIT);
443 #endif
444 }
445 
446 static int
acornfb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)447 acornfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
448 {
449 	u_int fontht;
450 	int err;
451 
452 	/*
453 	 * FIXME: Find the font height
454 	 */
455 	fontht = 8;
456 
457 	var->red.msb_right = 0;
458 	var->green.msb_right = 0;
459 	var->blue.msb_right = 0;
460 	var->transp.msb_right = 0;
461 
462 	switch (var->bits_per_pixel) {
463 	case 1:	case 2:	case 4:	case 8:
464 		var->red.offset    = 0;
465 		var->red.length    = var->bits_per_pixel;
466 		var->green         = var->red;
467 		var->blue          = var->red;
468 		var->transp.offset = 0;
469 		var->transp.length = 0;
470 		break;
471 
472 #ifdef HAS_VIDC20
473 	case 16:
474 		var->red.offset    = 0;
475 		var->red.length    = 5;
476 		var->green.offset  = 5;
477 		var->green.length  = 5;
478 		var->blue.offset   = 10;
479 		var->blue.length   = 5;
480 		var->transp.offset = 15;
481 		var->transp.length = 1;
482 		break;
483 
484 	case 32:
485 		var->red.offset    = 0;
486 		var->red.length    = 8;
487 		var->green.offset  = 8;
488 		var->green.length  = 8;
489 		var->blue.offset   = 16;
490 		var->blue.length   = 8;
491 		var->transp.offset = 24;
492 		var->transp.length = 4;
493 		break;
494 #endif
495 	default:
496 		return -EINVAL;
497 	}
498 
499 	/*
500 	 * Check to see if the pixel rate is valid.
501 	 */
502 	if (!acornfb_valid_pixrate(var))
503 		return -EINVAL;
504 
505 	/*
506 	 * Validate and adjust the resolution to
507 	 * match the video generator hardware.
508 	 */
509 	err = acornfb_adjust_timing(info, var, fontht);
510 	if (err)
511 		return err;
512 
513 	/*
514 	 * Validate the timing against the
515 	 * monitor hardware.
516 	 */
517 	return acornfb_validate_timing(var, &info->monspecs);
518 }
519 
acornfb_set_par(struct fb_info * info)520 static int acornfb_set_par(struct fb_info *info)
521 {
522 	switch (info->var.bits_per_pixel) {
523 	case 1:
524 		current_par.palette_size = 2;
525 		info->fix.visual = FB_VISUAL_MONO10;
526 		break;
527 	case 2:
528 		current_par.palette_size = 4;
529 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
530 		break;
531 	case 4:
532 		current_par.palette_size = 16;
533 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
534 		break;
535 	case 8:
536 		current_par.palette_size = VIDC_PALETTE_SIZE;
537 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
538 		break;
539 #ifdef HAS_VIDC20
540 	case 16:
541 		current_par.palette_size = 32;
542 		info->fix.visual = FB_VISUAL_DIRECTCOLOR;
543 		break;
544 	case 32:
545 		current_par.palette_size = VIDC_PALETTE_SIZE;
546 		info->fix.visual = FB_VISUAL_DIRECTCOLOR;
547 		break;
548 #endif
549 	default:
550 		BUG();
551 	}
552 
553 	info->fix.line_length	= (info->var.xres * info->var.bits_per_pixel) / 8;
554 
555 #if defined(HAS_MEMC)
556 	{
557 		unsigned long size = info->fix.smem_len - VDMA_XFERSIZE;
558 
559 		memc_write(VDMA_START, 0);
560 		memc_write(VDMA_END, size >> 2);
561 	}
562 #elif defined(HAS_IOMD)
563 	{
564 		unsigned long start, size;
565 		u_int control;
566 
567 		start = info->fix.smem_start;
568 		size  = current_par.screen_end;
569 
570 		if (current_par.using_vram) {
571 			size -= current_par.vram_half_sam;
572 			control = DMA_CR_E | (current_par.vram_half_sam / 256);
573 		} else {
574 			size -= 16;
575 			control = DMA_CR_E | DMA_CR_D | 16;
576 		}
577 
578 		iomd_writel(start,   IOMD_VIDSTART);
579 		iomd_writel(size,    IOMD_VIDEND);
580 		iomd_writel(control, IOMD_VIDCR);
581 	}
582 #endif
583 
584 	acornfb_update_dma(info, &info->var);
585 	acornfb_set_timing(info);
586 
587 	return 0;
588 }
589 
590 static int
acornfb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)591 acornfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
592 {
593 	u_int y_bottom = var->yoffset;
594 
595 	if (!(var->vmode & FB_VMODE_YWRAP))
596 		y_bottom += info->var.yres;
597 
598 	if (y_bottom > info->var.yres_virtual)
599 		return -EINVAL;
600 
601 	acornfb_update_dma(info, var);
602 
603 	return 0;
604 }
605 
606 static const struct fb_ops acornfb_ops = {
607 	.owner		= THIS_MODULE,
608 	FB_DEFAULT_IOMEM_OPS,
609 	.fb_check_var	= acornfb_check_var,
610 	.fb_set_par	= acornfb_set_par,
611 	.fb_setcolreg	= acornfb_setcolreg,
612 	.fb_pan_display	= acornfb_pan_display,
613 };
614 
615 /*
616  * Everything after here is initialisation!!!
617  */
618 static struct fb_videomode modedb[] = {
619 	{	/* 320x256 @ 50Hz */
620 		NULL, 50,  320,  256, 125000,  92,  62,  35, 19,  38, 2,
621 		FB_SYNC_COMP_HIGH_ACT,
622 		FB_VMODE_NONINTERLACED
623 	}, {	/* 640x250 @ 50Hz, 15.6 kHz hsync */
624 		NULL, 50,  640,  250,  62500, 185, 123,  38, 21,  76, 3,
625 		0,
626 		FB_VMODE_NONINTERLACED
627 	}, {	/* 640x256 @ 50Hz, 15.6 kHz hsync */
628 		NULL, 50,  640,  256,  62500, 185, 123,  35, 18,  76, 3,
629 		0,
630 		FB_VMODE_NONINTERLACED
631 	}, {	/* 640x512 @ 50Hz, 26.8 kHz hsync */
632 		NULL, 50,  640,  512,  41667, 113,  87,  18,  1,  56, 3,
633 		0,
634 		FB_VMODE_NONINTERLACED
635 	}, {	/* 640x250 @ 70Hz, 31.5 kHz hsync */
636 		NULL, 70,  640,  250,  39722,  48,  16, 109, 88,  96, 2,
637 		0,
638 		FB_VMODE_NONINTERLACED
639 	}, {	/* 640x256 @ 70Hz, 31.5 kHz hsync */
640 		NULL, 70,  640,  256,  39722,  48,  16, 106, 85,  96, 2,
641 		0,
642 		FB_VMODE_NONINTERLACED
643 	}, {	/* 640x352 @ 70Hz, 31.5 kHz hsync */
644 		NULL, 70,  640,  352,  39722,  48,  16,  58, 37,  96, 2,
645 		0,
646 		FB_VMODE_NONINTERLACED
647 	}, {	/* 640x480 @ 60Hz, 31.5 kHz hsync */
648 		NULL, 60,  640,  480,  39722,  48,  16,  32, 11,  96, 2,
649 		0,
650 		FB_VMODE_NONINTERLACED
651 	}, {	/* 800x600 @ 56Hz, 35.2 kHz hsync */
652 		NULL, 56,  800,  600,  27778, 101,  23,  22,  1, 100, 2,
653 		0,
654 		FB_VMODE_NONINTERLACED
655 	}, {	/* 896x352 @ 60Hz, 21.8 kHz hsync */
656 		NULL, 60,  896,  352,  41667,  59,  27,   9,  0, 118, 3,
657 		0,
658 		FB_VMODE_NONINTERLACED
659 	}, {	/* 1024x 768 @ 60Hz, 48.4 kHz hsync */
660 		NULL, 60, 1024,  768,  15385, 160,  24,  29,  3, 136, 6,
661 		0,
662 		FB_VMODE_NONINTERLACED
663 	}, {	/* 1280x1024 @ 60Hz, 63.8 kHz hsync */
664 		NULL, 60, 1280, 1024,   9090, 186,  96,  38,  1, 160, 3,
665 		0,
666 		FB_VMODE_NONINTERLACED
667 	}
668 };
669 
670 static struct fb_videomode acornfb_default_mode = {
671 	.name =		NULL,
672 	.refresh =	60,
673 	.xres =		640,
674 	.yres =		480,
675 	.pixclock =	39722,
676 	.left_margin =	56,
677 	.right_margin =	16,
678 	.upper_margin =	34,
679 	.lower_margin =	9,
680 	.hsync_len =	88,
681 	.vsync_len =	2,
682 	.sync =		0,
683 	.vmode =	FB_VMODE_NONINTERLACED
684 };
685 
acornfb_init_fbinfo(void)686 static void acornfb_init_fbinfo(void)
687 {
688 	static int first = 1;
689 
690 	if (!first)
691 		return;
692 	first = 0;
693 
694 	fb_info.fbops		= &acornfb_ops;
695 	fb_info.flags		= FBINFO_HWACCEL_YPAN;
696 	fb_info.pseudo_palette	= current_par.pseudo_palette;
697 
698 	strcpy(fb_info.fix.id, "Acorn");
699 	fb_info.fix.type	= FB_TYPE_PACKED_PIXELS;
700 	fb_info.fix.type_aux	= 0;
701 	fb_info.fix.xpanstep	= 0;
702 	fb_info.fix.ypanstep	= 1;
703 	fb_info.fix.ywrapstep	= 1;
704 	fb_info.fix.line_length	= 0;
705 	fb_info.fix.accel	= FB_ACCEL_NONE;
706 
707 	/*
708 	 * setup initial parameters
709 	 */
710 	memset(&fb_info.var, 0, sizeof(fb_info.var));
711 
712 #if defined(HAS_VIDC20)
713 	fb_info.var.red.length	   = 8;
714 	fb_info.var.transp.length  = 4;
715 #endif
716 	fb_info.var.green	   = fb_info.var.red;
717 	fb_info.var.blue	   = fb_info.var.red;
718 	fb_info.var.nonstd	   = 0;
719 	fb_info.var.activate	   = FB_ACTIVATE_NOW;
720 	fb_info.var.height	   = -1;
721 	fb_info.var.width	   = -1;
722 	fb_info.var.vmode	   = FB_VMODE_NONINTERLACED;
723 	fb_info.var.accel_flags	   = FB_ACCELF_TEXT;
724 
725 	current_par.dram_size	   = 0;
726 	current_par.montype	   = -1;
727 	current_par.dpms	   = 0;
728 }
729 
730 /*
731  * setup acornfb options:
732  *
733  *  mon:hmin-hmax:vmin-vmax:dpms:width:height
734  *	Set monitor parameters:
735  *		hmin   = horizontal minimum frequency (Hz)
736  *		hmax   = horizontal maximum frequency (Hz)	(optional)
737  *		vmin   = vertical minimum frequency (Hz)
738  *		vmax   = vertical maximum frequency (Hz)	(optional)
739  *		dpms   = DPMS supported?			(optional)
740  *		width  = width of picture in mm.		(optional)
741  *		height = height of picture in mm.		(optional)
742  *
743  * montype:type
744  *	Set RISC-OS style monitor type:
745  *		0 (or tv)	- TV frequency
746  *		1 (or multi)	- Multi frequency
747  *		2 (or hires)	- Hi-res monochrome
748  *		3 (or vga)	- VGA
749  *		4 (or svga)	- SVGA
750  *		auto, or option missing
751  *				- try hardware detect
752  *
753  * dram:size
754  *	Set the amount of DRAM to use for the frame buffer
755  *	(even if you have VRAM).
756  *	size can optionally be followed by 'M' or 'K' for
757  *	MB or KB respectively.
758  */
acornfb_parse_mon(char * opt)759 static void acornfb_parse_mon(char *opt)
760 {
761 	char *p = opt;
762 
763 	current_par.montype = -2;
764 
765 	fb_info.monspecs.hfmin = simple_strtoul(p, &p, 0);
766 	if (*p == '-')
767 		fb_info.monspecs.hfmax = simple_strtoul(p + 1, &p, 0);
768 	else
769 		fb_info.monspecs.hfmax = fb_info.monspecs.hfmin;
770 
771 	if (*p != ':')
772 		goto bad;
773 
774 	fb_info.monspecs.vfmin = simple_strtoul(p + 1, &p, 0);
775 	if (*p == '-')
776 		fb_info.monspecs.vfmax = simple_strtoul(p + 1, &p, 0);
777 	else
778 		fb_info.monspecs.vfmax = fb_info.monspecs.vfmin;
779 
780 	if (*p != ':')
781 		goto check_values;
782 
783 	fb_info.monspecs.dpms = simple_strtoul(p + 1, &p, 0);
784 
785 	if (*p != ':')
786 		goto check_values;
787 
788 	fb_info.var.width = simple_strtoul(p + 1, &p, 0);
789 
790 	if (*p != ':')
791 		goto check_values;
792 
793 	fb_info.var.height = simple_strtoul(p + 1, NULL, 0);
794 
795 check_values:
796 	if (fb_info.monspecs.hfmax < fb_info.monspecs.hfmin ||
797 	    fb_info.monspecs.vfmax < fb_info.monspecs.vfmin)
798 		goto bad;
799 	return;
800 
801 bad:
802 	printk(KERN_ERR "Acornfb: bad monitor settings: %s\n", opt);
803 	current_par.montype = -1;
804 }
805 
acornfb_parse_montype(char * opt)806 static void acornfb_parse_montype(char *opt)
807 {
808 	current_par.montype = -2;
809 
810 	if (strncmp(opt, "tv", 2) == 0) {
811 		opt += 2;
812 		current_par.montype = 0;
813 	} else if (strncmp(opt, "multi", 5) == 0) {
814 		opt += 5;
815 		current_par.montype = 1;
816 	} else if (strncmp(opt, "hires", 5) == 0) {
817 		opt += 5;
818 		current_par.montype = 2;
819 	} else if (strncmp(opt, "vga", 3) == 0) {
820 		opt += 3;
821 		current_par.montype = 3;
822 	} else if (strncmp(opt, "svga", 4) == 0) {
823 		opt += 4;
824 		current_par.montype = 4;
825 	} else if (strncmp(opt, "auto", 4) == 0) {
826 		opt += 4;
827 		current_par.montype = -1;
828 	} else if (isdigit(*opt))
829 		current_par.montype = simple_strtoul(opt, &opt, 0);
830 
831 	if (current_par.montype == -2 ||
832 	    current_par.montype > NR_MONTYPES) {
833 		printk(KERN_ERR "acornfb: unknown monitor type: %s\n",
834 			opt);
835 		current_par.montype = -1;
836 	} else
837 	if (opt && *opt) {
838 		if (strcmp(opt, ",dpms") == 0)
839 			current_par.dpms = 1;
840 		else
841 			printk(KERN_ERR
842 			       "acornfb: unknown monitor option: %s\n",
843 			       opt);
844 	}
845 }
846 
acornfb_parse_dram(char * opt)847 static void acornfb_parse_dram(char *opt)
848 {
849 	unsigned int size;
850 
851 	size = simple_strtoul(opt, &opt, 0);
852 
853 	if (opt) {
854 		switch (*opt) {
855 		case 'M':
856 		case 'm':
857 			size *= 1024;
858 			fallthrough;
859 		case 'K':
860 		case 'k':
861 			size *= 1024;
862 		default:
863 			break;
864 		}
865 	}
866 
867 	current_par.dram_size = size;
868 }
869 
870 static struct options {
871 	char *name;
872 	void (*parse)(char *opt);
873 } opt_table[] = {
874 	{ "mon",     acornfb_parse_mon     },
875 	{ "montype", acornfb_parse_montype },
876 	{ "dram",    acornfb_parse_dram    },
877 	{ NULL, NULL }
878 };
879 
acornfb_setup(char * options)880 static int acornfb_setup(char *options)
881 {
882 	struct options *optp;
883 	char *opt;
884 
885 	if (!options || !*options)
886 		return 0;
887 
888 	acornfb_init_fbinfo();
889 
890 	while ((opt = strsep(&options, ",")) != NULL) {
891 		if (!*opt)
892 			continue;
893 
894 		for (optp = opt_table; optp->name; optp++) {
895 			int optlen;
896 
897 			optlen = strlen(optp->name);
898 
899 			if (strncmp(opt, optp->name, optlen) == 0 &&
900 			    opt[optlen] == ':') {
901 				optp->parse(opt + optlen + 1);
902 				break;
903 			}
904 		}
905 
906 		if (!optp->name)
907 			printk(KERN_ERR "acornfb: unknown parameter: %s\n",
908 			       opt);
909 	}
910 	return 0;
911 }
912 
913 /*
914  * Detect type of monitor connected
915  *  For now, we just assume SVGA
916  */
acornfb_detect_monitortype(void)917 static int acornfb_detect_monitortype(void)
918 {
919 	return 4;
920 }
921 
acornfb_probe(struct platform_device * dev)922 static int acornfb_probe(struct platform_device *dev)
923 {
924 	unsigned long size;
925 	u_int h_sync, v_sync;
926 	int rc, i;
927 	char *option = NULL;
928 
929 	if (fb_get_options("acornfb", &option))
930 		return -ENODEV;
931 	acornfb_setup(option);
932 
933 	acornfb_init_fbinfo();
934 
935 	current_par.dev = &dev->dev;
936 
937 	if (current_par.montype == -1)
938 		current_par.montype = acornfb_detect_monitortype();
939 
940 	if (current_par.montype == -1 || current_par.montype > NR_MONTYPES)
941 		current_par.montype = 4;
942 
943 	if (current_par.montype >= 0) {
944 		fb_info.monspecs = monspecs[current_par.montype];
945 		fb_info.monspecs.dpms = current_par.dpms;
946 	}
947 
948 	/*
949 	 * Try to select a suitable default mode
950 	 */
951 	for (i = 0; i < ARRAY_SIZE(modedb); i++) {
952 		unsigned long hs;
953 
954 		hs = modedb[i].refresh *
955 		     (modedb[i].yres + modedb[i].upper_margin +
956 		      modedb[i].lower_margin + modedb[i].vsync_len);
957 		if (modedb[i].xres == DEFAULT_XRES &&
958 		    modedb[i].yres == DEFAULT_YRES &&
959 		    modedb[i].refresh >= fb_info.monspecs.vfmin &&
960 		    modedb[i].refresh <= fb_info.monspecs.vfmax &&
961 		    hs                >= fb_info.monspecs.hfmin &&
962 		    hs                <= fb_info.monspecs.hfmax) {
963 			acornfb_default_mode = modedb[i];
964 			break;
965 		}
966 	}
967 
968 	fb_info.screen_base    = (char *)SCREEN_BASE;
969 	fb_info.fix.smem_start = SCREEN_START;
970 	current_par.using_vram = 0;
971 
972 	/*
973 	 * If vram_size is set, we are using VRAM in
974 	 * a Risc PC.  However, if the user has specified
975 	 * an amount of DRAM then use that instead.
976 	 */
977 	if (vram_size && !current_par.dram_size) {
978 		size = vram_size;
979 		current_par.vram_half_sam = vram_size / 1024;
980 		current_par.using_vram = 1;
981 	} else if (current_par.dram_size)
982 		size = current_par.dram_size;
983 	else
984 		size = MAX_SIZE;
985 
986 	/*
987 	 * Limit maximum screen size.
988 	 */
989 	if (size > MAX_SIZE)
990 		size = MAX_SIZE;
991 
992 	size = PAGE_ALIGN(size);
993 
994 #if defined(HAS_VIDC20)
995 	if (!current_par.using_vram) {
996 		dma_addr_t handle;
997 		void *base;
998 
999 		/*
1000 		 * RiscPC needs to allocate the DRAM memory
1001 		 * for the framebuffer if we are not using
1002 		 * VRAM.
1003 		 */
1004 		base = dma_alloc_wc(current_par.dev, size, &handle,
1005 				    GFP_KERNEL);
1006 		if (base == NULL) {
1007 			printk(KERN_ERR "acornfb: unable to allocate screen memory\n");
1008 			return -ENOMEM;
1009 		}
1010 
1011 		fb_info.screen_base = base;
1012 		fb_info.fix.smem_start = handle;
1013 	}
1014 #endif
1015 	fb_info.fix.smem_len = size;
1016 	current_par.palette_size   = VIDC_PALETTE_SIZE;
1017 
1018 	/*
1019 	 * Lookup the timing for this resolution.  If we can't
1020 	 * find it, then we can't restore it if we change
1021 	 * the resolution, so we disable this feature.
1022 	 */
1023 	do {
1024 		rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
1025 				 ARRAY_SIZE(modedb),
1026 				 &acornfb_default_mode, DEFAULT_BPP);
1027 		/*
1028 		 * If we found an exact match, all ok.
1029 		 */
1030 		if (rc == 1)
1031 			break;
1032 
1033 		rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
1034 				  &acornfb_default_mode, DEFAULT_BPP);
1035 		/*
1036 		 * If we found an exact match, all ok.
1037 		 */
1038 		if (rc == 1)
1039 			break;
1040 
1041 		rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
1042 				 ARRAY_SIZE(modedb),
1043 				 &acornfb_default_mode, DEFAULT_BPP);
1044 		if (rc)
1045 			break;
1046 
1047 		rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
1048 				  &acornfb_default_mode, DEFAULT_BPP);
1049 	} while (0);
1050 
1051 	/*
1052 	 * If we didn't find an exact match, try the
1053 	 * generic database.
1054 	 */
1055 	if (rc == 0) {
1056 		printk("Acornfb: no valid mode found\n");
1057 		return -EINVAL;
1058 	}
1059 
1060 	h_sync = 1953125000 / fb_info.var.pixclock;
1061 	h_sync = h_sync * 512 / (fb_info.var.xres + fb_info.var.left_margin +
1062 		 fb_info.var.right_margin + fb_info.var.hsync_len);
1063 	v_sync = h_sync / (fb_info.var.yres + fb_info.var.upper_margin +
1064 		 fb_info.var.lower_margin + fb_info.var.vsync_len);
1065 
1066 	printk(KERN_INFO "Acornfb: %dkB %cRAM, %s, using %dx%d, %d.%03dkHz, %dHz\n",
1067 		fb_info.fix.smem_len / 1024,
1068 		current_par.using_vram ? 'V' : 'D',
1069 		VIDC_NAME, fb_info.var.xres, fb_info.var.yres,
1070 		h_sync / 1000, h_sync % 1000, v_sync);
1071 
1072 	printk(KERN_INFO "Acornfb: Monitor: %d.%03d-%d.%03dkHz, %d-%dHz%s\n",
1073 		fb_info.monspecs.hfmin / 1000, fb_info.monspecs.hfmin % 1000,
1074 		fb_info.monspecs.hfmax / 1000, fb_info.monspecs.hfmax % 1000,
1075 		fb_info.monspecs.vfmin, fb_info.monspecs.vfmax,
1076 		fb_info.monspecs.dpms ? ", DPMS" : "");
1077 
1078 	if (fb_set_var(&fb_info, &fb_info.var))
1079 		printk(KERN_ERR "Acornfb: unable to set display parameters\n");
1080 
1081 	if (register_framebuffer(&fb_info) < 0)
1082 		return -EINVAL;
1083 	return 0;
1084 }
1085 
1086 static struct platform_driver acornfb_driver = {
1087 	.probe	= acornfb_probe,
1088 	.driver	= {
1089 		.name	= "acornfb",
1090 	},
1091 };
1092 
acornfb_init(void)1093 static int __init acornfb_init(void)
1094 {
1095 	return platform_driver_register(&acornfb_driver);
1096 }
1097 
1098 module_init(acornfb_init);
1099 
1100 MODULE_AUTHOR("Russell King");
1101 MODULE_DESCRIPTION("VIDC 1/1a/20 framebuffer driver");
1102 MODULE_LICENSE("GPL");
1103