Merge branch 'work'
[linux-2.6] / drivers / video / acornfb.c
1 /*
2  *  linux/drivers/video/acornfb.c
3  *
4  *  Copyright (C) 1998-2001 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Frame buffer code for Acorn platforms
11  *
12  * NOTE: Most of the modes with X!=640 will disappear shortly.
13  * NOTE: Startup setting of HS & VS polarity not supported.
14  *       (do we need to support it if we're coming up in 640x480?)
15  *
16  * FIXME: (things broken by the "new improved" FBCON API)
17  *  - Blanking 8bpp displays with VIDC
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/fb.h>
29 #include <linux/platform_device.h>
30 #include <linux/dma-mapping.h>
31
32 #include <asm/hardware.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/mach-types.h>
36 #include <asm/pgtable.h>
37
38 #include "acornfb.h"
39
40 /*
41  * VIDC machines can't do 16 or 32BPP modes.
42  */
43 #ifdef HAS_VIDC
44 #undef FBCON_HAS_CFB16
45 #undef FBCON_HAS_CFB32
46 #endif
47
48 /*
49  * Default resolution.
50  * NOTE that it has to be supported in the table towards
51  * the end of this file.
52  */
53 #define DEFAULT_XRES    640
54 #define DEFAULT_YRES    480
55 #define DEFAULT_BPP     4
56
57 /*
58  * define this to debug the video mode selection
59  */
60 #undef DEBUG_MODE_SELECTION
61
62 /*
63  * Translation from RISC OS monitor types to actual
64  * HSYNC and VSYNC frequency ranges.  These are
65  * probably not right, but they're the best info I
66  * have.  Allow 1% either way on the nominal for TVs.
67  */
68 #define NR_MONTYPES     6
69 static struct fb_monspecs monspecs[NR_MONTYPES] __initdata = {
70         {       /* TV           */
71                 .hfmin  = 15469,
72                 .hfmax  = 15781,
73                 .vfmin  = 49,
74                 .vfmax  = 51,
75         }, {    /* Multi Freq   */
76                 .hfmin  = 0,
77                 .hfmax  = 99999,
78                 .vfmin  = 0,
79                 .vfmax  = 199,
80         }, {    /* Hi-res mono  */
81                 .hfmin  = 58608,
82                 .hfmax  = 58608,
83                 .vfmin  = 64,
84                 .vfmax  = 64,
85         }, {    /* VGA          */
86                 .hfmin  = 30000,
87                 .hfmax  = 70000,
88                 .vfmin  = 60,
89                 .vfmax  = 60,
90         }, {    /* SVGA         */
91                 .hfmin  = 30000,
92                 .hfmax  = 70000,
93                 .vfmin  = 56,
94                 .vfmax  = 75,
95         }, {
96                 .hfmin  = 30000,
97                 .hfmax  = 70000,
98                 .vfmin  = 60,
99                 .vfmax  = 60,
100         }
101 };
102
103 static struct fb_info fb_info;
104 static struct acornfb_par current_par;
105 static struct vidc_timing current_vidc;
106
107 extern unsigned int vram_size;  /* set by setup.c */
108
109 #ifdef HAS_VIDC
110
111 #define MAX_SIZE        480*1024
112
113 /* CTL     VIDC Actual
114  * 24.000  0     8.000
115  * 25.175  0     8.392
116  * 36.000  0    12.000
117  * 24.000  1    12.000
118  * 25.175  1    12.588
119  * 24.000  2    16.000
120  * 25.175  2    16.783
121  * 36.000  1    18.000
122  * 24.000  3    24.000
123  * 36.000  2    24.000
124  * 25.175  3    25.175
125  * 36.000  3    36.000
126  */
127 struct pixclock {
128         u_long  min_clock;
129         u_long  max_clock;
130         u_int   vidc_ctl;
131         u_int   vid_ctl;
132 };
133
134 static struct pixclock arc_clocks[] = {
135         /* we allow +/-1% on these */
136         { 123750, 126250, VIDC_CTRL_DIV3,   VID_CTL_24MHz },    /*  8.000MHz */
137         {  82500,  84167, VIDC_CTRL_DIV2,   VID_CTL_24MHz },    /* 12.000MHz */
138         {  61875,  63125, VIDC_CTRL_DIV1_5, VID_CTL_24MHz },    /* 16.000MHz */
139         {  41250,  42083, VIDC_CTRL_DIV1,   VID_CTL_24MHz },    /* 24.000MHz */
140 };
141
142 #ifdef CONFIG_ARCH_A5K
143 static struct pixclock a5k_clocks[] = {
144         { 117974, 120357, VIDC_CTRL_DIV3,   VID_CTL_25MHz },    /*  8.392MHz */
145         {  78649,  80238, VIDC_CTRL_DIV2,   VID_CTL_25MHz },    /* 12.588MHz */
146         {  58987,  60178, VIDC_CTRL_DIV1_5, VID_CTL_25MHz },    /* 16.588MHz */
147         {  55000,  56111, VIDC_CTRL_DIV2,   VID_CTL_36MHz },    /* 18.000MHz */
148         {  39325,  40119, VIDC_CTRL_DIV1,   VID_CTL_25MHz },    /* 25.175MHz */
149         {  27500,  28055, VIDC_CTRL_DIV1,   VID_CTL_36MHz },    /* 36.000MHz */
150 };
151 #endif
152
153 static struct pixclock *
154 acornfb_valid_pixrate(struct fb_var_screeninfo *var)
155 {
156         u_long pixclock = var->pixclock;
157         u_int i;
158
159         if (!var->pixclock)
160                 return NULL;
161
162         for (i = 0; i < ARRAY_SIZE(arc_clocks); i++)
163                 if (pixclock > arc_clocks[i].min_clock &&
164                     pixclock < arc_clocks[i].max_clock)
165                         return arc_clocks + i;
166
167 #ifdef CONFIG_ARCH_A5K
168         if (machine_is_a5k()) {
169                 for (i = 0; i < ARRAY_SIZE(a5k_clocks); i++)
170                         if (pixclock > a5k_clocks[i].min_clock &&
171                             pixclock < a5k_clocks[i].max_clock)
172                                 return a5k_clocks + i;
173         }
174 #endif
175
176         return NULL;
177 }
178
179 /* VIDC Rules:
180  * hcr  : must be even (interlace, hcr/2 must be even)
181  * hswr : must be even
182  * hdsr : must be odd
183  * hder : must be odd
184  *
185  * vcr  : must be odd
186  * vswr : >= 1
187  * vdsr : >= 1
188  * vder : >= vdsr
189  * if interlaced, then hcr/2 must be even
190  */
191 static void
192 acornfb_set_timing(struct fb_var_screeninfo *var)
193 {
194         struct pixclock *pclk;
195         struct vidc_timing vidc;
196         u_int horiz_correction;
197         u_int sync_len, display_start, display_end, cycle;
198         u_int is_interlaced;
199         u_int vid_ctl, vidc_ctl;
200         u_int bandwidth;
201
202         memset(&vidc, 0, sizeof(vidc));
203
204         pclk = acornfb_valid_pixrate(var);
205         vidc_ctl = pclk->vidc_ctl;
206         vid_ctl  = pclk->vid_ctl;
207
208         bandwidth = var->pixclock * 8 / var->bits_per_pixel;
209         /* 25.175, 4bpp = 79.444ns per byte, 317.776ns per word: fifo = 2,6 */
210         if (bandwidth > 143500)
211                 vidc_ctl |= VIDC_CTRL_FIFO_3_7;
212         else if (bandwidth > 71750)
213                 vidc_ctl |= VIDC_CTRL_FIFO_2_6;
214         else if (bandwidth > 35875)
215                 vidc_ctl |= VIDC_CTRL_FIFO_1_5;
216         else
217                 vidc_ctl |= VIDC_CTRL_FIFO_0_4;
218
219         switch (var->bits_per_pixel) {
220         case 1:
221                 horiz_correction = 19;
222                 vidc_ctl |= VIDC_CTRL_1BPP;
223                 break;
224
225         case 2:
226                 horiz_correction = 11;
227                 vidc_ctl |= VIDC_CTRL_2BPP;
228                 break;
229
230         case 4:
231                 horiz_correction = 7;
232                 vidc_ctl |= VIDC_CTRL_4BPP;
233                 break;
234
235         default:
236         case 8:
237                 horiz_correction = 5;
238                 vidc_ctl |= VIDC_CTRL_8BPP;
239                 break;
240         }
241
242         if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
243                 vidc_ctl |= VIDC_CTRL_CSYNC;
244         else {
245                 if (!(var->sync & FB_SYNC_HOR_HIGH_ACT))
246                         vid_ctl |= VID_CTL_HS_NHSYNC;
247
248                 if (!(var->sync & FB_SYNC_VERT_HIGH_ACT))
249                         vid_ctl |= VID_CTL_VS_NVSYNC;
250         }
251
252         sync_len        = var->hsync_len;
253         display_start   = sync_len + var->left_margin;
254         display_end     = display_start + var->xres;
255         cycle           = display_end + var->right_margin;
256
257         /* if interlaced, then hcr/2 must be even */
258         is_interlaced = (var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED;
259
260         if (is_interlaced) {
261                 vidc_ctl |= VIDC_CTRL_INTERLACE;
262                 if (cycle & 2) {
263                         cycle += 2;
264                         var->right_margin += 2;
265                 }
266         }
267
268         vidc.h_cycle            = (cycle - 2) / 2;
269         vidc.h_sync_width       = (sync_len - 2) / 2;
270         vidc.h_border_start     = (display_start - 1) / 2;
271         vidc.h_display_start    = (display_start - horiz_correction) / 2;
272         vidc.h_display_end      = (display_end - horiz_correction) / 2;
273         vidc.h_border_end       = (display_end - 1) / 2;
274         vidc.h_interlace        = (vidc.h_cycle + 1) / 2;
275
276         sync_len        = var->vsync_len;
277         display_start   = sync_len + var->upper_margin;
278         display_end     = display_start + var->yres;
279         cycle           = display_end + var->lower_margin;
280
281         if (is_interlaced)
282                 cycle = (cycle - 3) / 2;
283         else
284                 cycle = cycle - 1;
285
286         vidc.v_cycle            = cycle;
287         vidc.v_sync_width       = sync_len - 1;
288         vidc.v_border_start     = display_start - 1;
289         vidc.v_display_start    = vidc.v_border_start;
290         vidc.v_display_end      = display_end - 1;
291         vidc.v_border_end       = vidc.v_display_end;
292
293         if (machine_is_a5k())
294                 __raw_writeb(vid_ctl, IOEB_VID_CTL);
295
296         if (memcmp(&current_vidc, &vidc, sizeof(vidc))) {
297                 current_vidc = vidc;
298
299                 vidc_writel(0xe0000000 | vidc_ctl);
300                 vidc_writel(0x80000000 | (vidc.h_cycle << 14));
301                 vidc_writel(0x84000000 | (vidc.h_sync_width << 14));
302                 vidc_writel(0x88000000 | (vidc.h_border_start << 14));
303                 vidc_writel(0x8c000000 | (vidc.h_display_start << 14));
304                 vidc_writel(0x90000000 | (vidc.h_display_end << 14));
305                 vidc_writel(0x94000000 | (vidc.h_border_end << 14));
306                 vidc_writel(0x98000000);
307                 vidc_writel(0x9c000000 | (vidc.h_interlace << 14));
308                 vidc_writel(0xa0000000 | (vidc.v_cycle << 14));
309                 vidc_writel(0xa4000000 | (vidc.v_sync_width << 14));
310                 vidc_writel(0xa8000000 | (vidc.v_border_start << 14));
311                 vidc_writel(0xac000000 | (vidc.v_display_start << 14));
312                 vidc_writel(0xb0000000 | (vidc.v_display_end << 14));
313                 vidc_writel(0xb4000000 | (vidc.v_border_end << 14));
314                 vidc_writel(0xb8000000);
315                 vidc_writel(0xbc000000);
316         }
317 #ifdef DEBUG_MODE_SELECTION
318         printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
319                var->yres, var->bits_per_pixel);
320         printk(KERN_DEBUG " H-cycle          : %d\n", vidc.h_cycle);
321         printk(KERN_DEBUG " H-sync-width     : %d\n", vidc.h_sync_width);
322         printk(KERN_DEBUG " H-border-start   : %d\n", vidc.h_border_start);
323         printk(KERN_DEBUG " H-display-start  : %d\n", vidc.h_display_start);
324         printk(KERN_DEBUG " H-display-end    : %d\n", vidc.h_display_end);
325         printk(KERN_DEBUG " H-border-end     : %d\n", vidc.h_border_end);
326         printk(KERN_DEBUG " H-interlace      : %d\n", vidc.h_interlace);
327         printk(KERN_DEBUG " V-cycle          : %d\n", vidc.v_cycle);
328         printk(KERN_DEBUG " V-sync-width     : %d\n", vidc.v_sync_width);
329         printk(KERN_DEBUG " V-border-start   : %d\n", vidc.v_border_start);
330         printk(KERN_DEBUG " V-display-start  : %d\n", vidc.v_display_start);
331         printk(KERN_DEBUG " V-display-end    : %d\n", vidc.v_display_end);
332         printk(KERN_DEBUG " V-border-end     : %d\n", vidc.v_border_end);
333         printk(KERN_DEBUG " VIDC Ctrl (E)    : 0x%08X\n", vidc_ctl);
334         printk(KERN_DEBUG " IOEB Ctrl        : 0x%08X\n", vid_ctl);
335 #endif
336 }
337
338 static int
339 acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
340                   u_int trans, struct fb_info *info)
341 {
342         union palette pal;
343
344         if (regno >= current_par.palette_size)
345                 return 1;
346
347         pal.p = 0;
348         pal.vidc.reg   = regno;
349         pal.vidc.red   = red >> 12;
350         pal.vidc.green = green >> 12;
351         pal.vidc.blue  = blue >> 12;
352
353         current_par.palette[regno] = pal;
354
355         vidc_writel(pal.p);
356
357         return 0;
358 }
359 #endif
360
361 #ifdef HAS_VIDC20
362 #include <asm/arch/acornfb.h>
363
364 #define MAX_SIZE        2*1024*1024
365
366 /* VIDC20 has a different set of rules from the VIDC:
367  *  hcr  : must be multiple of 4
368  *  hswr : must be even
369  *  hdsr : must be even
370  *  hder : must be even
371  *  vcr  : >= 2, (interlace, must be odd)
372  *  vswr : >= 1
373  *  vdsr : >= 1
374  *  vder : >= vdsr
375  */
376 static void acornfb_set_timing(struct fb_info *info)
377 {
378         struct fb_var_screeninfo *var = &info->var;
379         struct vidc_timing vidc;
380         u_int vcr, fsize;
381         u_int ext_ctl, dat_ctl;
382         u_int words_per_line;
383
384         memset(&vidc, 0, sizeof(vidc));
385
386         vidc.h_sync_width       = var->hsync_len - 8;
387         vidc.h_border_start     = vidc.h_sync_width + var->left_margin + 8 - 12;
388         vidc.h_display_start    = vidc.h_border_start + 12 - 18;
389         vidc.h_display_end      = vidc.h_display_start + var->xres;
390         vidc.h_border_end       = vidc.h_display_end + 18 - 12;
391         vidc.h_cycle            = vidc.h_border_end + var->right_margin + 12 - 8;
392         vidc.h_interlace        = vidc.h_cycle / 2;
393         vidc.v_sync_width       = var->vsync_len - 1;
394         vidc.v_border_start     = vidc.v_sync_width + var->upper_margin;
395         vidc.v_display_start    = vidc.v_border_start;
396         vidc.v_display_end      = vidc.v_display_start + var->yres;
397         vidc.v_border_end       = vidc.v_display_end;
398         vidc.control            = acornfb_default_control();
399
400         vcr = var->vsync_len + var->upper_margin + var->yres +
401               var->lower_margin;
402
403         if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
404                 vidc.v_cycle = (vcr - 3) / 2;
405                 vidc.control |= VIDC20_CTRL_INT;
406         } else
407                 vidc.v_cycle = vcr - 2;
408
409         switch (var->bits_per_pixel) {
410         case  1: vidc.control |= VIDC20_CTRL_1BPP;      break;
411         case  2: vidc.control |= VIDC20_CTRL_2BPP;      break;
412         case  4: vidc.control |= VIDC20_CTRL_4BPP;      break;
413         default:
414         case  8: vidc.control |= VIDC20_CTRL_8BPP;      break;
415         case 16: vidc.control |= VIDC20_CTRL_16BPP;     break;
416         case 32: vidc.control |= VIDC20_CTRL_32BPP;     break;
417         }
418
419         acornfb_vidc20_find_rates(&vidc, var);
420         fsize = var->vsync_len + var->upper_margin + var->lower_margin - 1;
421
422         if (memcmp(&current_vidc, &vidc, sizeof(vidc))) {
423                 current_vidc = vidc;
424
425                 vidc_writel(VIDC20_CTRL| vidc.control);
426                 vidc_writel(0xd0000000 | vidc.pll_ctl);
427                 vidc_writel(0x80000000 | vidc.h_cycle);
428                 vidc_writel(0x81000000 | vidc.h_sync_width);
429                 vidc_writel(0x82000000 | vidc.h_border_start);
430                 vidc_writel(0x83000000 | vidc.h_display_start);
431                 vidc_writel(0x84000000 | vidc.h_display_end);
432                 vidc_writel(0x85000000 | vidc.h_border_end);
433                 vidc_writel(0x86000000);
434                 vidc_writel(0x87000000 | vidc.h_interlace);
435                 vidc_writel(0x90000000 | vidc.v_cycle);
436                 vidc_writel(0x91000000 | vidc.v_sync_width);
437                 vidc_writel(0x92000000 | vidc.v_border_start);
438                 vidc_writel(0x93000000 | vidc.v_display_start);
439                 vidc_writel(0x94000000 | vidc.v_display_end);
440                 vidc_writel(0x95000000 | vidc.v_border_end);
441                 vidc_writel(0x96000000);
442                 vidc_writel(0x97000000);
443         }
444
445         iomd_writel(fsize, IOMD_FSIZE);
446
447         ext_ctl = acornfb_default_econtrol();
448
449         if (var->sync & FB_SYNC_COMP_HIGH_ACT) /* should be FB_SYNC_COMP */
450                 ext_ctl |= VIDC20_ECTL_HS_NCSYNC | VIDC20_ECTL_VS_NCSYNC;
451         else {
452                 if (var->sync & FB_SYNC_HOR_HIGH_ACT)
453                         ext_ctl |= VIDC20_ECTL_HS_HSYNC;
454                 else
455                         ext_ctl |= VIDC20_ECTL_HS_NHSYNC;
456
457                 if (var->sync & FB_SYNC_VERT_HIGH_ACT)
458                         ext_ctl |= VIDC20_ECTL_VS_VSYNC;
459                 else
460                         ext_ctl |= VIDC20_ECTL_VS_NVSYNC;
461         }
462
463         vidc_writel(VIDC20_ECTL | ext_ctl);
464
465         words_per_line = var->xres * var->bits_per_pixel / 32;
466
467         if (current_par.using_vram && info->fix.smem_len == 2048*1024)
468                 words_per_line /= 2;
469
470         /* RiscPC doesn't use the VIDC's VRAM control. */
471         dat_ctl = VIDC20_DCTL_VRAM_DIS | VIDC20_DCTL_SNA | words_per_line;
472
473         /* The data bus width is dependent on both the type
474          * and amount of video memory.
475          *     DRAM     32bit low
476          * 1MB VRAM     32bit
477          * 2MB VRAM     64bit
478          */
479         if (current_par.using_vram && current_par.vram_half_sam == 2048)
480                 dat_ctl |= VIDC20_DCTL_BUS_D63_0;
481         else
482                 dat_ctl |= VIDC20_DCTL_BUS_D31_0;
483
484         vidc_writel(VIDC20_DCTL | dat_ctl);
485
486 #ifdef DEBUG_MODE_SELECTION
487         printk(KERN_DEBUG "VIDC registers for %dx%dx%d:\n", var->xres,
488                var->yres, var->bits_per_pixel);
489         printk(KERN_DEBUG " H-cycle          : %d\n", vidc.h_cycle);
490         printk(KERN_DEBUG " H-sync-width     : %d\n", vidc.h_sync_width);
491         printk(KERN_DEBUG " H-border-start   : %d\n", vidc.h_border_start);
492         printk(KERN_DEBUG " H-display-start  : %d\n", vidc.h_display_start);
493         printk(KERN_DEBUG " H-display-end    : %d\n", vidc.h_display_end);
494         printk(KERN_DEBUG " H-border-end     : %d\n", vidc.h_border_end);
495         printk(KERN_DEBUG " H-interlace      : %d\n", vidc.h_interlace);
496         printk(KERN_DEBUG " V-cycle          : %d\n", vidc.v_cycle);
497         printk(KERN_DEBUG " V-sync-width     : %d\n", vidc.v_sync_width);
498         printk(KERN_DEBUG " V-border-start   : %d\n", vidc.v_border_start);
499         printk(KERN_DEBUG " V-display-start  : %d\n", vidc.v_display_start);
500         printk(KERN_DEBUG " V-display-end    : %d\n", vidc.v_display_end);
501         printk(KERN_DEBUG " V-border-end     : %d\n", vidc.v_border_end);
502         printk(KERN_DEBUG " Ext Ctrl  (C)    : 0x%08X\n", ext_ctl);
503         printk(KERN_DEBUG " PLL Ctrl  (D)    : 0x%08X\n", vidc.pll_ctl);
504         printk(KERN_DEBUG " Ctrl      (E)    : 0x%08X\n", vidc.control);
505         printk(KERN_DEBUG " Data Ctrl (F)    : 0x%08X\n", dat_ctl);
506         printk(KERN_DEBUG " Fsize            : 0x%08X\n", fsize);
507 #endif
508 }
509
510 /*
511  * We have to take note of the VIDC20's 16-bit palette here.
512  * The VIDC20 looks up a 16 bit pixel as follows:
513  *
514  *   bits   111111
515  *          5432109876543210
516  *   red            ++++++++  (8 bits,  7 to 0)
517  *  green       ++++++++      (8 bits, 11 to 4)
518  *   blue   ++++++++          (8 bits, 15 to 8)
519  *
520  * We use a pixel which looks like:
521  *
522  *   bits   111111
523  *          5432109876543210
524  *   red               +++++  (5 bits,  4 to  0)
525  *  green         +++++       (5 bits,  9 to  5)
526  *   blue    +++++            (5 bits, 14 to 10)
527  */
528 static int
529 acornfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
530                   u_int trans, struct fb_info *info)
531 {
532         union palette pal;
533
534         if (regno >= current_par.palette_size)
535                 return 1;
536
537         if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
538                 u32 pseudo_val;
539
540                 pseudo_val  = regno << info->var.red.offset;
541                 pseudo_val |= regno << info->var.green.offset;
542                 pseudo_val |= regno << info->var.blue.offset;
543
544                 ((u32 *)info->pseudo_palette)[regno] = pseudo_val;
545         }
546
547         pal.p = 0;
548         pal.vidc20.red   = red >> 8;
549         pal.vidc20.green = green >> 8;
550         pal.vidc20.blue  = blue >> 8;
551
552         current_par.palette[regno] = pal;
553
554         if (info->var.bits_per_pixel == 16) {
555                 int i;
556
557                 pal.p = 0;
558                 vidc_writel(0x10000000);
559                 for (i = 0; i < 256; i += 1) {
560                         pal.vidc20.red   = current_par.palette[ i       & 31].vidc20.red;
561                         pal.vidc20.green = current_par.palette[(i >> 1) & 31].vidc20.green;
562                         pal.vidc20.blue  = current_par.palette[(i >> 2) & 31].vidc20.blue;
563                         vidc_writel(pal.p);
564                         /* Palette register pointer auto-increments */
565                 }
566         } else {
567                 vidc_writel(0x10000000 | regno);
568                 vidc_writel(pal.p);
569         }
570
571         return 0;
572 }
573 #endif
574
575 /*
576  * Before selecting the timing parameters, adjust
577  * the resolution to fit the rules.
578  */
579 static int
580 acornfb_adjust_timing(struct fb_info *info, struct fb_var_screeninfo *var, u_int fontht)
581 {
582         u_int font_line_len, sam_size, min_size, size, nr_y;
583
584         /* xres must be even */
585         var->xres = (var->xres + 1) & ~1;
586
587         /*
588          * We don't allow xres_virtual to differ from xres
589          */
590         var->xres_virtual = var->xres;
591         var->xoffset = 0;
592
593         if (current_par.using_vram)
594                 sam_size = current_par.vram_half_sam * 2;
595         else
596                 sam_size = 16;
597
598         /*
599          * Now, find a value for yres_virtual which allows
600          * us to do ywrap scrolling.  The value of
601          * yres_virtual must be such that the end of the
602          * displayable frame buffer must be aligned with
603          * the start of a font line.
604          */
605         font_line_len = var->xres * var->bits_per_pixel * fontht / 8;
606         min_size = var->xres * var->yres * var->bits_per_pixel / 8;
607
608         /*
609          * If minimum screen size is greater than that we have
610          * available, reject it.
611          */
612         if (min_size > info->fix.smem_len)
613                 return -EINVAL;
614
615         /* Find int 'y', such that y * fll == s * sam < maxsize
616          * y = s * sam / fll; s = maxsize / sam
617          */
618         for (size = info->fix.smem_len;
619              nr_y = size / font_line_len, min_size <= size;
620              size -= sam_size) {
621                 if (nr_y * font_line_len == size)
622                         break;
623         }
624         nr_y *= fontht;
625
626         if (var->accel_flags & FB_ACCELF_TEXT) {
627                 if (min_size > size) {
628                         /*
629                          * failed, use ypan
630                          */
631                         size = info->fix.smem_len;
632                         var->yres_virtual = size / (font_line_len / fontht);
633                 } else
634                         var->yres_virtual = nr_y;
635         } else if (var->yres_virtual > nr_y)
636                 var->yres_virtual = nr_y;
637
638         current_par.screen_end = info->fix.smem_start + size;
639
640         /*
641          * Fix yres & yoffset if needed.
642          */
643         if (var->yres > var->yres_virtual)
644                 var->yres = var->yres_virtual;
645
646         if (var->vmode & FB_VMODE_YWRAP) {
647                 if (var->yoffset > var->yres_virtual)
648                         var->yoffset = var->yres_virtual;
649         } else {
650                 if (var->yoffset + var->yres > var->yres_virtual)
651                         var->yoffset = var->yres_virtual - var->yres;
652         }
653
654         /* hsync_len must be even */
655         var->hsync_len = (var->hsync_len + 1) & ~1;
656
657 #ifdef HAS_VIDC
658         /* left_margin must be odd */
659         if ((var->left_margin & 1) == 0) {
660                 var->left_margin -= 1;
661                 var->right_margin += 1;
662         }
663
664         /* right_margin must be odd */
665         var->right_margin |= 1;
666 #elif defined(HAS_VIDC20)
667         /* left_margin must be even */
668         if (var->left_margin & 1) {
669                 var->left_margin += 1;
670                 var->right_margin -= 1;
671         }
672
673         /* right_margin must be even */
674         if (var->right_margin & 1)
675                 var->right_margin += 1;
676 #endif
677
678         if (var->vsync_len < 1)
679                 var->vsync_len = 1;
680
681         return 0;
682 }
683
684 static int
685 acornfb_validate_timing(struct fb_var_screeninfo *var,
686                         struct fb_monspecs *monspecs)
687 {
688         unsigned long hs, vs;
689
690         /*
691          * hs(Hz) = 10^12 / (pixclock * xtotal)
692          * vs(Hz) = hs(Hz) / ytotal
693          *
694          * No need to do long long divisions or anything
695          * like that if you factor it correctly
696          */
697         hs = 1953125000 / var->pixclock;
698         hs = hs * 512 /
699              (var->xres + var->left_margin + var->right_margin + var->hsync_len);
700         vs = hs /
701              (var->yres + var->upper_margin + var->lower_margin + var->vsync_len);
702
703         return (vs >= monspecs->vfmin && vs <= monspecs->vfmax &&
704                 hs >= monspecs->hfmin && hs <= monspecs->hfmax) ? 0 : -EINVAL;
705 }
706
707 static inline void
708 acornfb_update_dma(struct fb_info *info, struct fb_var_screeninfo *var)
709 {
710         u_int off = var->yoffset * info->fix.line_length;
711
712 #if defined(HAS_MEMC)
713         memc_write(VDMA_INIT, off >> 2);
714 #elif defined(HAS_IOMD)
715         iomd_writel(info->fix.smem_start + off, IOMD_VIDINIT);
716 #endif
717 }
718
719 static int
720 acornfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
721 {
722         u_int fontht;
723         int err;
724
725         /*
726          * FIXME: Find the font height
727          */
728         fontht = 8;
729
730         var->red.msb_right = 0;
731         var->green.msb_right = 0;
732         var->blue.msb_right = 0;
733         var->transp.msb_right = 0;
734
735         switch (var->bits_per_pixel) {
736         case 1: case 2: case 4: case 8:
737                 var->red.offset    = 0;
738                 var->red.length    = var->bits_per_pixel;
739                 var->green         = var->red;
740                 var->blue          = var->red;
741                 var->transp.offset = 0;
742                 var->transp.length = 0;
743                 break;
744
745 #ifdef HAS_VIDC20
746         case 16:
747                 var->red.offset    = 0;
748                 var->red.length    = 5;
749                 var->green.offset  = 5;
750                 var->green.length  = 5;
751                 var->blue.offset   = 10;
752                 var->blue.length   = 5;
753                 var->transp.offset = 15;
754                 var->transp.length = 1;
755                 break;
756
757         case 32:
758                 var->red.offset    = 0;
759                 var->red.length    = 8;
760                 var->green.offset  = 8;
761                 var->green.length  = 8;
762                 var->blue.offset   = 16;
763                 var->blue.length   = 8;
764                 var->transp.offset = 24;
765                 var->transp.length = 4;
766                 break;
767 #endif
768         default:
769                 return -EINVAL;
770         }
771
772         /*
773          * Check to see if the pixel rate is valid.
774          */
775         if (!acornfb_valid_pixrate(var))
776                 return -EINVAL;
777
778         /*
779          * Validate and adjust the resolution to
780          * match the video generator hardware.
781          */
782         err = acornfb_adjust_timing(info, var, fontht);
783         if (err)
784                 return err;
785
786         /*
787          * Validate the timing against the
788          * monitor hardware.
789          */
790         return acornfb_validate_timing(var, &info->monspecs);
791 }
792
793 static int acornfb_set_par(struct fb_info *info)
794 {
795         switch (info->var.bits_per_pixel) {
796         case 1:
797                 current_par.palette_size = 2;
798                 info->fix.visual = FB_VISUAL_MONO10;
799                 break;
800         case 2:
801                 current_par.palette_size = 4;
802                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
803                 break;
804         case 4:
805                 current_par.palette_size = 16;
806                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
807                 break;
808         case 8:
809                 current_par.palette_size = VIDC_PALETTE_SIZE;
810 #ifdef HAS_VIDC
811                 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
812 #else
813                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
814 #endif
815                 break;
816 #ifdef HAS_VIDC20
817         case 16:
818                 current_par.palette_size = 32;
819                 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
820                 break;
821         case 32:
822                 current_par.palette_size = VIDC_PALETTE_SIZE;
823                 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
824                 break;
825 #endif
826         default:
827                 BUG();
828         }
829
830         info->fix.line_length   = (info->var.xres * info->var.bits_per_pixel) / 8;
831
832 #if defined(HAS_MEMC)
833         {
834                 unsigned long size = info->fix.smem_len - VDMA_XFERSIZE;
835
836                 memc_write(VDMA_START, 0);
837                 memc_write(VDMA_END, size >> 2);
838         }
839 #elif defined(HAS_IOMD)
840         {
841                 unsigned long start, size;
842                 u_int control;
843
844                 start = info->fix.smem_start;
845                 size  = current_par.screen_end;
846
847                 if (current_par.using_vram) {
848                         size -= current_par.vram_half_sam;
849                         control = DMA_CR_E | (current_par.vram_half_sam / 256);
850                 } else {
851                         size -= 16;
852                         control = DMA_CR_E | DMA_CR_D | 16;
853                 }
854
855                 iomd_writel(start,   IOMD_VIDSTART);
856                 iomd_writel(size,    IOMD_VIDEND);
857                 iomd_writel(control, IOMD_VIDCR);
858         }
859 #endif
860
861         acornfb_update_dma(info, &info->var);
862         acornfb_set_timing(info);
863
864         return 0;
865 }
866
867 static int
868 acornfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
869 {
870         u_int y_bottom = var->yoffset;
871
872         if (!(var->vmode & FB_VMODE_YWRAP))
873                 y_bottom += var->yres;
874
875         BUG_ON(y_bottom > var->yres_virtual);
876
877         acornfb_update_dma(info, var);
878
879         return 0;
880 }
881
882 /*
883  * Note that we are entered with the kernel locked.
884  */
885 static int
886 acornfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
887 {
888         unsigned long off, start;
889         u32 len;
890
891         off = vma->vm_pgoff << PAGE_SHIFT;
892
893         start = info->fix.smem_start;
894         len = PAGE_ALIGN(start & ~PAGE_MASK) + info->fix.smem_len;
895         start &= PAGE_MASK;
896         if ((vma->vm_end - vma->vm_start + off) > len)
897                 return -EINVAL;
898         off += start;
899         vma->vm_pgoff = off >> PAGE_SHIFT;
900
901         /* This is an IO map - tell maydump to skip this VMA */
902         vma->vm_flags |= VM_IO;
903
904         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
905
906         /*
907          * Don't alter the page protection flags; we want to keep the area
908          * cached for better performance.  This does mean that we may miss
909          * some updates to the screen occasionally, but process switches
910          * should cause the caches and buffers to be flushed often enough.
911          */
912         if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
913                                 vma->vm_end - vma->vm_start,
914                                 vma->vm_page_prot))
915                 return -EAGAIN;
916         return 0;
917 }
918
919 static struct fb_ops acornfb_ops = {
920         .owner          = THIS_MODULE,
921         .fb_check_var   = acornfb_check_var,
922         .fb_set_par     = acornfb_set_par,
923         .fb_setcolreg   = acornfb_setcolreg,
924         .fb_pan_display = acornfb_pan_display,
925         .fb_fillrect    = cfb_fillrect,
926         .fb_copyarea    = cfb_copyarea,
927         .fb_imageblit   = cfb_imageblit,
928         .fb_mmap        = acornfb_mmap,
929 };
930
931 /*
932  * Everything after here is initialisation!!!
933  */
934 static struct fb_videomode modedb[] __initdata = {
935         {       /* 320x256 @ 50Hz */
936                 NULL, 50,  320,  256, 125000,  92,  62,  35, 19,  38, 2,
937                 FB_SYNC_COMP_HIGH_ACT,
938                 FB_VMODE_NONINTERLACED
939         }, {    /* 640x250 @ 50Hz, 15.6 kHz hsync */
940                 NULL, 50,  640,  250,  62500, 185, 123,  38, 21,  76, 3,
941                 0,
942                 FB_VMODE_NONINTERLACED
943         }, {    /* 640x256 @ 50Hz, 15.6 kHz hsync */
944                 NULL, 50,  640,  256,  62500, 185, 123,  35, 18,  76, 3,
945                 0,
946                 FB_VMODE_NONINTERLACED
947         }, {    /* 640x512 @ 50Hz, 26.8 kHz hsync */
948                 NULL, 50,  640,  512,  41667, 113,  87,  18,  1,  56, 3,
949                 0,
950                 FB_VMODE_NONINTERLACED
951         }, {    /* 640x250 @ 70Hz, 31.5 kHz hsync */
952                 NULL, 70,  640,  250,  39722,  48,  16, 109, 88,  96, 2,
953                 0,
954                 FB_VMODE_NONINTERLACED
955         }, {    /* 640x256 @ 70Hz, 31.5 kHz hsync */
956                 NULL, 70,  640,  256,  39722,  48,  16, 106, 85,  96, 2,
957                 0,
958                 FB_VMODE_NONINTERLACED
959         }, {    /* 640x352 @ 70Hz, 31.5 kHz hsync */
960                 NULL, 70,  640,  352,  39722,  48,  16,  58, 37,  96, 2,
961                 0,
962                 FB_VMODE_NONINTERLACED
963         }, {    /* 640x480 @ 60Hz, 31.5 kHz hsync */
964                 NULL, 60,  640,  480,  39722,  48,  16,  32, 11,  96, 2,
965                 0,
966                 FB_VMODE_NONINTERLACED
967         }, {    /* 800x600 @ 56Hz, 35.2 kHz hsync */
968                 NULL, 56,  800,  600,  27778, 101,  23,  22,  1, 100, 2,
969                 0,
970                 FB_VMODE_NONINTERLACED
971         }, {    /* 896x352 @ 60Hz, 21.8 kHz hsync */
972                 NULL, 60,  896,  352,  41667,  59,  27,   9,  0, 118, 3,
973                 0,
974                 FB_VMODE_NONINTERLACED
975         }, {    /* 1024x 768 @ 60Hz, 48.4 kHz hsync */
976                 NULL, 60, 1024,  768,  15385, 160,  24,  29,  3, 136, 6,
977                 0,
978                 FB_VMODE_NONINTERLACED
979         }, {    /* 1280x1024 @ 60Hz, 63.8 kHz hsync */
980                 NULL, 60, 1280, 1024,   9090, 186,  96,  38,  1, 160, 3,
981                 0,
982                 FB_VMODE_NONINTERLACED
983         }
984 };
985
986 static struct fb_videomode __initdata
987 acornfb_default_mode = {
988         .name =         NULL,
989         .refresh =      60,
990         .xres =         640,
991         .yres =         480,
992         .pixclock =     39722,
993         .left_margin =  56,
994         .right_margin = 16,
995         .upper_margin = 34,
996         .lower_margin = 9,
997         .hsync_len =    88,
998         .vsync_len =    2,
999         .sync =         0,
1000         .vmode =        FB_VMODE_NONINTERLACED
1001 };
1002
1003 static void __init acornfb_init_fbinfo(void)
1004 {
1005         static int first = 1;
1006
1007         if (!first)
1008                 return;
1009         first = 0;
1010
1011         fb_info.fbops           = &acornfb_ops;
1012         fb_info.flags           = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1013         fb_info.pseudo_palette  = current_par.pseudo_palette;
1014
1015         strcpy(fb_info.fix.id, "Acorn");
1016         fb_info.fix.type        = FB_TYPE_PACKED_PIXELS;
1017         fb_info.fix.type_aux    = 0;
1018         fb_info.fix.xpanstep    = 0;
1019         fb_info.fix.ypanstep    = 1;
1020         fb_info.fix.ywrapstep   = 1;
1021         fb_info.fix.line_length = 0;
1022         fb_info.fix.accel       = FB_ACCEL_NONE;
1023
1024         /*
1025          * setup initial parameters
1026          */
1027         memset(&fb_info.var, 0, sizeof(fb_info.var));
1028
1029 #if defined(HAS_VIDC20)
1030         fb_info.var.red.length     = 8;
1031         fb_info.var.transp.length  = 4;
1032 #elif defined(HAS_VIDC)
1033         fb_info.var.red.length     = 4;
1034         fb_info.var.transp.length  = 1;
1035 #endif
1036         fb_info.var.green          = fb_info.var.red;
1037         fb_info.var.blue           = fb_info.var.red;
1038         fb_info.var.nonstd         = 0;
1039         fb_info.var.activate       = FB_ACTIVATE_NOW;
1040         fb_info.var.height         = -1;
1041         fb_info.var.width          = -1;
1042         fb_info.var.vmode          = FB_VMODE_NONINTERLACED;
1043         fb_info.var.accel_flags    = FB_ACCELF_TEXT;
1044
1045         current_par.dram_size      = 0;
1046         current_par.montype        = -1;
1047         current_par.dpms           = 0;
1048 }
1049
1050 /*
1051  * setup acornfb options:
1052  *
1053  *  mon:hmin-hmax:vmin-vmax:dpms:width:height
1054  *      Set monitor parameters:
1055  *              hmin   = horizontal minimum frequency (Hz)
1056  *              hmax   = horizontal maximum frequency (Hz)      (optional)
1057  *              vmin   = vertical minimum frequency (Hz)
1058  *              vmax   = vertical maximum frequency (Hz)        (optional)
1059  *              dpms   = DPMS supported?                        (optional)
1060  *              width  = width of picture in mm.                (optional)
1061  *              height = height of picture in mm.               (optional)
1062  *
1063  * montype:type
1064  *      Set RISC-OS style monitor type:
1065  *              0 (or tv)       - TV frequency
1066  *              1 (or multi)    - Multi frequency
1067  *              2 (or hires)    - Hi-res monochrome
1068  *              3 (or vga)      - VGA
1069  *              4 (or svga)     - SVGA
1070  *              auto, or option missing
1071  *                              - try hardware detect
1072  *
1073  * dram:size
1074  *      Set the amount of DRAM to use for the frame buffer
1075  *      (even if you have VRAM).
1076  *      size can optionally be followed by 'M' or 'K' for
1077  *      MB or KB respectively.
1078  */
1079 static void __init
1080 acornfb_parse_mon(char *opt)
1081 {
1082         char *p = opt;
1083
1084         current_par.montype = -2;
1085
1086         fb_info.monspecs.hfmin = simple_strtoul(p, &p, 0);
1087         if (*p == '-')
1088                 fb_info.monspecs.hfmax = simple_strtoul(p + 1, &p, 0);
1089         else
1090                 fb_info.monspecs.hfmax = fb_info.monspecs.hfmin;
1091
1092         if (*p != ':')
1093                 goto bad;
1094
1095         fb_info.monspecs.vfmin = simple_strtoul(p + 1, &p, 0);
1096         if (*p == '-')
1097                 fb_info.monspecs.vfmax = simple_strtoul(p + 1, &p, 0);
1098         else
1099                 fb_info.monspecs.vfmax = fb_info.monspecs.vfmin;
1100
1101         if (*p != ':')
1102                 goto check_values;
1103
1104         fb_info.monspecs.dpms = simple_strtoul(p + 1, &p, 0);
1105
1106         if (*p != ':')
1107                 goto check_values;
1108
1109         fb_info.var.width = simple_strtoul(p + 1, &p, 0);
1110
1111         if (*p != ':')
1112                 goto check_values;
1113
1114         fb_info.var.height = simple_strtoul(p + 1, NULL, 0);
1115
1116 check_values:
1117         if (fb_info.monspecs.hfmax < fb_info.monspecs.hfmin ||
1118             fb_info.monspecs.vfmax < fb_info.monspecs.vfmin)
1119                 goto bad;
1120         return;
1121
1122 bad:
1123         printk(KERN_ERR "Acornfb: bad monitor settings: %s\n", opt);
1124         current_par.montype = -1;
1125 }
1126
1127 static void __init
1128 acornfb_parse_montype(char *opt)
1129 {
1130         current_par.montype = -2;
1131
1132         if (strncmp(opt, "tv", 2) == 0) {
1133                 opt += 2;
1134                 current_par.montype = 0;
1135         } else if (strncmp(opt, "multi", 5) == 0) {
1136                 opt += 5;
1137                 current_par.montype = 1;
1138         } else if (strncmp(opt, "hires", 5) == 0) {
1139                 opt += 5;
1140                 current_par.montype = 2;
1141         } else if (strncmp(opt, "vga", 3) == 0) {
1142                 opt += 3;
1143                 current_par.montype = 3;
1144         } else if (strncmp(opt, "svga", 4) == 0) {
1145                 opt += 4;
1146                 current_par.montype = 4;
1147         } else if (strncmp(opt, "auto", 4) == 0) {
1148                 opt += 4;
1149                 current_par.montype = -1;
1150         } else if (isdigit(*opt))
1151                 current_par.montype = simple_strtoul(opt, &opt, 0);
1152
1153         if (current_par.montype == -2 ||
1154             current_par.montype > NR_MONTYPES) {
1155                 printk(KERN_ERR "acornfb: unknown monitor type: %s\n",
1156                         opt);
1157                 current_par.montype = -1;
1158         } else
1159         if (opt && *opt) {
1160                 if (strcmp(opt, ",dpms") == 0)
1161                         current_par.dpms = 1;
1162                 else
1163                         printk(KERN_ERR
1164                                "acornfb: unknown monitor option: %s\n",
1165                                opt);
1166         }
1167 }
1168
1169 static void __init
1170 acornfb_parse_dram(char *opt)
1171 {
1172         unsigned int size;
1173
1174         size = simple_strtoul(opt, &opt, 0);
1175
1176         if (opt) {
1177                 switch (*opt) {
1178                 case 'M':
1179                 case 'm':
1180                         size *= 1024;
1181                 case 'K':
1182                 case 'k':
1183                         size *= 1024;
1184                 default:
1185                         break;
1186                 }
1187         }
1188
1189         current_par.dram_size = size;
1190 }
1191
1192 static struct options {
1193         char *name;
1194         void (*parse)(char *opt);
1195 } opt_table[] __initdata = {
1196         { "mon",     acornfb_parse_mon     },
1197         { "montype", acornfb_parse_montype },
1198         { "dram",    acornfb_parse_dram    },
1199         { NULL, NULL }
1200 };
1201
1202 int __init
1203 acornfb_setup(char *options)
1204 {
1205         struct options *optp;
1206         char *opt;
1207
1208         if (!options || !*options)
1209                 return 0;
1210
1211         acornfb_init_fbinfo();
1212
1213         while ((opt = strsep(&options, ",")) != NULL) {
1214                 if (!*opt)
1215                         continue;
1216
1217                 for (optp = opt_table; optp->name; optp++) {
1218                         int optlen;
1219
1220                         optlen = strlen(optp->name);
1221
1222                         if (strncmp(opt, optp->name, optlen) == 0 &&
1223                             opt[optlen] == ':') {
1224                                 optp->parse(opt + optlen + 1);
1225                                 break;
1226                         }
1227                 }
1228
1229                 if (!optp->name)
1230                         printk(KERN_ERR "acornfb: unknown parameter: %s\n",
1231                                opt);
1232         }
1233         return 0;
1234 }
1235
1236 /*
1237  * Detect type of monitor connected
1238  *  For now, we just assume SVGA
1239  */
1240 static int __init
1241 acornfb_detect_monitortype(void)
1242 {
1243         return 4;
1244 }
1245
1246 /*
1247  * This enables the unused memory to be freed on older Acorn machines.
1248  * We are freeing memory on behalf of the architecture initialisation
1249  * code here.
1250  */
1251 static inline void
1252 free_unused_pages(unsigned int virtual_start, unsigned int virtual_end)
1253 {
1254         int mb_freed = 0;
1255
1256         /*
1257          * Align addresses
1258          */
1259         virtual_start = PAGE_ALIGN(virtual_start);
1260         virtual_end = PAGE_ALIGN(virtual_end);
1261
1262         while (virtual_start < virtual_end) {
1263                 struct page *page;
1264
1265                 /*
1266                  * Clear page reserved bit,
1267                  * set count to 1, and free
1268                  * the page.
1269                  */
1270                 page = virt_to_page(virtual_start);
1271                 ClearPageReserved(page);
1272                 set_page_count(page, 1);
1273                 free_page(virtual_start);
1274
1275                 virtual_start += PAGE_SIZE;
1276                 mb_freed += PAGE_SIZE / 1024;
1277         }
1278
1279         printk("acornfb: freed %dK memory\n", mb_freed);
1280 }
1281
1282 static int __init acornfb_probe(struct platform_device *dev)
1283 {
1284         unsigned long size;
1285         u_int h_sync, v_sync;
1286         int rc, i;
1287         char *option = NULL;
1288
1289         if (fb_get_options("acornfb", &option))
1290                 return -ENODEV;
1291         acornfb_setup(option);
1292
1293         acornfb_init_fbinfo();
1294
1295         current_par.dev = &dev->dev;
1296
1297         if (current_par.montype == -1)
1298                 current_par.montype = acornfb_detect_monitortype();
1299
1300         if (current_par.montype == -1 || current_par.montype > NR_MONTYPES)
1301                 current_par.montype = 4;
1302
1303         if (current_par.montype >= 0) {
1304                 fb_info.monspecs = monspecs[current_par.montype];
1305                 fb_info.monspecs.dpms = current_par.dpms;
1306         }
1307
1308         /*
1309          * Try to select a suitable default mode
1310          */
1311         for (i = 0; i < sizeof(modedb) / sizeof(*modedb); i++) {
1312                 unsigned long hs;
1313
1314                 hs = modedb[i].refresh *
1315                      (modedb[i].yres + modedb[i].upper_margin +
1316                       modedb[i].lower_margin + modedb[i].vsync_len);
1317                 if (modedb[i].xres == DEFAULT_XRES &&
1318                     modedb[i].yres == DEFAULT_YRES &&
1319                     modedb[i].refresh >= fb_info.monspecs.vfmin &&
1320                     modedb[i].refresh <= fb_info.monspecs.vfmax &&
1321                     hs                >= fb_info.monspecs.hfmin &&
1322                     hs                <= fb_info.monspecs.hfmax) {
1323                         acornfb_default_mode = modedb[i];
1324                         break;
1325                 }
1326         }
1327
1328         fb_info.screen_base    = (char *)SCREEN_BASE;
1329         fb_info.fix.smem_start = SCREEN_START;
1330         current_par.using_vram = 0;
1331
1332         /*
1333          * If vram_size is set, we are using VRAM in
1334          * a Risc PC.  However, if the user has specified
1335          * an amount of DRAM then use that instead.
1336          */
1337         if (vram_size && !current_par.dram_size) {
1338                 size = vram_size;
1339                 current_par.vram_half_sam = vram_size / 1024;
1340                 current_par.using_vram = 1;
1341         } else if (current_par.dram_size)
1342                 size = current_par.dram_size;
1343         else
1344                 size = MAX_SIZE;
1345
1346         /*
1347          * Limit maximum screen size.
1348          */
1349         if (size > MAX_SIZE)
1350                 size = MAX_SIZE;
1351
1352         size = PAGE_ALIGN(size);
1353
1354 #if defined(HAS_VIDC20)
1355         if (!current_par.using_vram) {
1356                 dma_addr_t handle;
1357                 void *base;
1358
1359                 /*
1360                  * RiscPC needs to allocate the DRAM memory
1361                  * for the framebuffer if we are not using
1362                  * VRAM.
1363                  */
1364                 base = dma_alloc_writecombine(current_par.dev, size, &handle,
1365                                               GFP_KERNEL);
1366                 if (base == NULL) {
1367                         printk(KERN_ERR "acornfb: unable to allocate screen "
1368                                "memory\n");
1369                         return -ENOMEM;
1370                 }
1371
1372                 fb_info.screen_base = base;
1373                 fb_info.fix.smem_start = handle;
1374         }
1375 #endif
1376 #if defined(HAS_VIDC)
1377         /*
1378          * Archimedes/A5000 machines use a fixed address for their
1379          * framebuffers.  Free unused pages
1380          */
1381         free_unused_pages(PAGE_OFFSET + size, PAGE_OFFSET + MAX_SIZE);
1382 #endif
1383         
1384         fb_info.fix.smem_len = size;
1385         current_par.palette_size   = VIDC_PALETTE_SIZE;
1386
1387         /*
1388          * Lookup the timing for this resolution.  If we can't
1389          * find it, then we can't restore it if we change
1390          * the resolution, so we disable this feature.
1391          */
1392         do {
1393                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
1394                                  sizeof(modedb) / sizeof(*modedb),
1395                                  &acornfb_default_mode, DEFAULT_BPP);
1396                 /*
1397                  * If we found an exact match, all ok.
1398                  */
1399                 if (rc == 1)
1400                         break;
1401
1402                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
1403                                   &acornfb_default_mode, DEFAULT_BPP);
1404                 /*
1405                  * If we found an exact match, all ok.
1406                  */
1407                 if (rc == 1)
1408                         break;
1409
1410                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, modedb,
1411                                  sizeof(modedb) / sizeof(*modedb),
1412                                  &acornfb_default_mode, DEFAULT_BPP);
1413                 if (rc)
1414                         break;
1415
1416                 rc = fb_find_mode(&fb_info.var, &fb_info, NULL, NULL, 0,
1417                                   &acornfb_default_mode, DEFAULT_BPP);
1418         } while (0);
1419
1420         /*
1421          * If we didn't find an exact match, try the
1422          * generic database.
1423          */
1424         if (rc == 0) {
1425                 printk("Acornfb: no valid mode found\n");
1426                 return -EINVAL;
1427         }
1428
1429         h_sync = 1953125000 / fb_info.var.pixclock;
1430         h_sync = h_sync * 512 / (fb_info.var.xres + fb_info.var.left_margin +
1431                  fb_info.var.right_margin + fb_info.var.hsync_len);
1432         v_sync = h_sync / (fb_info.var.yres + fb_info.var.upper_margin +
1433                  fb_info.var.lower_margin + fb_info.var.vsync_len);
1434
1435         printk(KERN_INFO "Acornfb: %dkB %cRAM, %s, using %dx%d, "
1436                 "%d.%03dkHz, %dHz\n",
1437                 fb_info.fix.smem_len / 1024,
1438                 current_par.using_vram ? 'V' : 'D',
1439                 VIDC_NAME, fb_info.var.xres, fb_info.var.yres,
1440                 h_sync / 1000, h_sync % 1000, v_sync);
1441
1442         printk(KERN_INFO "Acornfb: Monitor: %d.%03d-%d.%03dkHz, %d-%dHz%s\n",
1443                 fb_info.monspecs.hfmin / 1000, fb_info.monspecs.hfmin % 1000,
1444                 fb_info.monspecs.hfmax / 1000, fb_info.monspecs.hfmax % 1000,
1445                 fb_info.monspecs.vfmin, fb_info.monspecs.vfmax,
1446                 fb_info.monspecs.dpms ? ", DPMS" : "");
1447
1448         if (fb_set_var(&fb_info, &fb_info.var))
1449                 printk(KERN_ERR "Acornfb: unable to set display parameters\n");
1450
1451         if (register_framebuffer(&fb_info) < 0)
1452                 return -EINVAL;
1453         return 0;
1454 }
1455
1456 static struct platform_driver acornfb_driver = {
1457         .probe  = acornfb_probe,
1458         .driver = {
1459                 .name   = "acornfb",
1460         },
1461 };
1462
1463 static int __init acornfb_init(void)
1464 {
1465         return platform_driver_register(&acornfb_driver);
1466 }
1467
1468 module_init(acornfb_init);
1469
1470 MODULE_AUTHOR("Russell King");
1471 MODULE_DESCRIPTION("VIDC 1/1a/20 framebuffer driver");
1472 MODULE_LICENSE("GPL");