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