v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / video / controlfb.c
1 /*
2  *  controlfb.c -- frame buffer device for the PowerMac 'control' display
3  *
4  *  Created 12 July 1998 by Dan Jacobowitz <dan@debian.org>
5  *  Copyright (C) 1998 Dan Jacobowitz
6  *  Copyright (C) 2001 Takashi Oe
7  *
8  *  Mmap code by Michel Lanners <mlan@cpu.lu>
9  *
10  *  Frame buffer structure from:
11  *    drivers/video/chipsfb.c -- frame buffer device for
12  *    Chips & Technologies 65550 chip.
13  *
14  *    Copyright (C) 1998 Paul Mackerras
15  *
16  *    This file is derived from the Powermac "chips" driver:
17  *    Copyright (C) 1997 Fabio Riccardi.
18  *    And from the frame buffer device for Open Firmware-initialized devices:
19  *    Copyright (C) 1997 Geert Uytterhoeven.
20  *
21  *  Hardware information from:
22  *    control.c: Console support for PowerMac "control" display adaptor.
23  *    Copyright (C) 1996 Paul Mackerras
24  *
25  *  This file is subject to the terms and conditions of the GNU General Public
26  *  License. See the file COPYING in the main directory of this archive for
27  *  more details.
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
35 #include <linux/mm.h>
36 #include <linux/tty.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
41 #include <linux/fb.h>
42 #include <linux/selection.h>
43 #include <linux/init.h>
44 #include <linux/pci.h>
45 #include <linux/nvram.h>
46 #ifdef CONFIG_FB_COMPAT_XPMAC
47 #include <asm/vc_ioctl.h>
48 #endif
49 #include <linux/adb.h>
50 #include <linux/cuda.h>
51 #include <asm/io.h>
52 #include <asm/prom.h>
53 #include <asm/pgtable.h>
54
55 #include <video/fbcon.h>
56 #include <video/fbcon-cfb8.h>
57 #include <video/fbcon-cfb16.h>
58 #include <video/fbcon-cfb32.h>
59 #include <video/macmodes.h>
60
61 #include "controlfb.h"
62
63 struct fb_par_control {
64         int     vmode, cmode;
65         int     xres, yres;
66         int     vxres, vyres;
67         int     xoffset, yoffset;
68         int     pitch;
69         struct control_regvals  regvals;
70         unsigned long sync;
71         unsigned char ctrl;
72 };
73
74 #define DIRTY(z) ((x)->z != (y)->z)
75 #define DIRTY_CMAP(z) (memcmp(&((x)->z), &((y)->z), sizeof((y)->z)))
76 static inline int PAR_EQUAL(struct fb_par_control *x, struct fb_par_control *y)
77 {
78         int i, results;
79
80         results = 1;
81         for (i = 0; i < 3; i++)
82                 results &= !DIRTY(regvals.clock_params[i]);
83         if (!results)
84                 return 0;
85         for (i = 0; i < 16; i++)
86                 results &= !DIRTY(regvals.regs[i]);
87         if (!results)
88                 return 0;
89         return (!DIRTY(cmode) && !DIRTY(xres) && !DIRTY(yres)
90                 && !DIRTY(vxres) && !DIRTY(vyres));
91 }
92 static inline int VAR_MATCH(struct fb_var_screeninfo *x, struct fb_var_screeninfo *y)
93 {
94         return (!DIRTY(bits_per_pixel) && !DIRTY(xres)
95                 && !DIRTY(yres) && !DIRTY(xres_virtual)
96                 && !DIRTY(yres_virtual)
97                 && !DIRTY_CMAP(red) && !DIRTY_CMAP(green) && !DIRTY_CMAP(blue));
98 }
99
100 struct fb_info_control {
101         struct fb_info                  info;
102         struct display                  display;
103         struct fb_par_control           par;
104         struct {
105                 __u8 red, green, blue;
106         }                       palette[256];
107         
108         struct cmap_regs        *cmap_regs;
109         unsigned long           cmap_regs_phys;
110         
111         struct control_regs     *control_regs;
112         unsigned long           control_regs_phys;
113         unsigned long           control_regs_size;
114         
115         __u8                    *frame_buffer;
116         unsigned long           frame_buffer_phys;
117         unsigned long           fb_orig_base;
118         unsigned long           fb_orig_size;
119
120         int                     control_use_bank2;
121         unsigned long           total_vram;
122         unsigned char           vram_attr;
123         union {
124 #ifdef FBCON_HAS_CFB16
125                 u16 cfb16[16];
126 #endif
127 #ifdef FBCON_HAS_CFB32
128                 u32 cfb32[16];
129 #endif
130         } fbcon_cmap;
131 };
132
133 /* control register access macro */
134 #define CNTRL_REG(INFO,REG) (&(((INFO)->control_regs-> ## REG).r))
135
136
137 /******************** Prototypes for exported functions ********************/
138 /*
139  * struct fb_ops
140  */
141 static int control_get_fix(struct fb_fix_screeninfo *fix, int con,
142         struct fb_info *info);
143 static int control_get_var(struct fb_var_screeninfo *var, int con,
144         struct fb_info *info);
145 static int control_set_var(struct fb_var_screeninfo *var, int con,
146         struct fb_info *info);
147 static int control_pan_display(struct fb_var_screeninfo *var, int con,
148         struct fb_info *info);
149 static int control_get_cmap(struct fb_cmap *cmap, int kspc, int con,
150         struct fb_info *info);
151 static int control_set_cmap(struct fb_cmap *cmap, int kspc, int con,
152         struct fb_info *info);
153 static int control_mmap(struct fb_info *info, struct file *file,
154         struct vm_area_struct *vma);
155
156 /*
157  * low level fbcon ops
158  */
159 static int controlfb_switch(int con, struct fb_info *info);
160 static int controlfb_updatevar(int con, struct fb_info *info);
161 static void controlfb_blank(int blank_mode, struct fb_info *info);
162
163 /*
164  * low level cmap set/get ops
165  */
166 static int controlfb_getcolreg(u_int regno, u_int *red, u_int *green,
167         u_int *blue, u_int *transp, struct fb_info *info);
168 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
169         u_int transp, struct fb_info *info);
170
171 /*
172  * inititialization
173  */
174 int control_init(void);
175 void control_setup(char *);
176
177 /*
178  * low level fbcon revc ops
179  */
180 static void control_cfb16_revc(struct display *p, int xx, int yy);
181 static void control_cfb32_revc(struct display *p, int xx, int yy);
182
183
184 /******************** Prototypes for internal functions **********************/
185
186 static void do_install_cmap(struct display *disp, struct fb_info *info);
187 static void set_control_clock(unsigned char *params);
188 static int init_control(struct fb_info_control *p);
189 static void control_set_hardware(struct fb_info_control *p,
190         struct fb_par_control *par);
191 static int control_of_init(struct device_node *dp);
192 static void control_par_to_fix(struct fb_par_control *par,
193         struct fb_fix_screeninfo *fix, struct fb_info_control *p);
194 static void control_set_dispsw(struct display *disp, int cmode,
195         struct fb_info_control *p);
196 static void find_vram_size(struct fb_info_control *p);
197 static int read_control_sense(struct fb_info_control *p);
198 static int calc_clock_params(unsigned long clk, unsigned char *param);
199 static int control_var_to_par(struct fb_var_screeninfo *var,
200         struct fb_par_control *par, const struct fb_info *fb_info);
201 static inline void control_par_to_var(struct fb_par_control *par,
202         struct fb_var_screeninfo *var);
203 static void control_par_to_fix(struct fb_par_control *par,
204         struct fb_fix_screeninfo *fix, struct fb_info_control *p);
205 static void control_par_to_display(struct fb_par_control *par,
206         struct display *disp, struct fb_fix_screeninfo *fix,
207         struct fb_info_control *p);
208 static void control_set_dispsw(struct display *disp, int cmode,
209         struct fb_info_control *p);
210 static void control_init_info(struct fb_info *info, struct fb_info_control *p);
211 static void control_cleanup(void);
212
213
214 /************************** Internal variables *******************************/
215
216 static int currcon;
217 static struct fb_info_control *control_fb;
218
219 static char fontname[40] __initdata = { 0 };
220 static int default_vmode __initdata = VMODE_NVRAM;
221 static int default_cmode __initdata = CMODE_NVRAM;
222
223
224 static struct fb_ops controlfb_ops = {
225         owner:          THIS_MODULE,
226         fb_get_fix:     control_get_fix,
227         fb_get_var:     control_get_var,
228         fb_set_var:     control_set_var,
229         fb_get_cmap:    control_get_cmap,
230         fb_set_cmap:    control_set_cmap,
231         fb_pan_display: control_pan_display,
232         fb_mmap:        control_mmap,
233 };
234
235
236 /********************  The functions for controlfb_ops ********************/
237
238 #ifdef MODULE
239 int init_module(void)
240 {
241         struct device_node *dp;
242
243         dp = find_devices("control");
244         if (dp != 0 && !control_of_init(dp))
245                 return 0;
246
247         return -ENXIO;
248 }
249
250 void cleanup_module(void)
251 {
252         control_cleanup();
253 }
254 #endif
255
256 /*********** Providing our information to the user ************/
257
258 static int control_get_fix(struct fb_fix_screeninfo *fix, int con,
259                          struct fb_info *info)
260 {
261         struct fb_info_control *p = (struct fb_info_control *) info;
262
263         if(con == -1) {
264                 control_par_to_fix(&p->par, fix, p);
265         } else {
266                 struct fb_par_control par;
267                 
268                 control_var_to_par(&fb_display[con].var, &par, info);
269                 control_par_to_fix(&par, fix, p);
270         }
271         return 0;
272 }
273
274 static int control_get_var(struct fb_var_screeninfo *var, int con,
275                          struct fb_info *info)
276 {
277         struct fb_info_control *p = (struct fb_info_control *) info;
278
279         if(con == -1) {
280                 control_par_to_var(&p->par, var);
281         } else {
282                 *var = fb_display[con].var;
283         }
284         return 0;
285 }
286
287
288 /*
289  * Sets everything according to var
290  */
291 static int control_set_var(struct fb_var_screeninfo *var, int con,
292                          struct fb_info *info)
293 {
294         struct fb_info_control *p = (struct fb_info_control *) info;
295         struct display *disp;
296         struct fb_par_control par;
297         int depthchange, err;
298         int activate = var->activate;
299
300         if((err = control_var_to_par(var, &par, info))) {
301                 if (con < 0)
302                         printk (KERN_ERR "control_set_var: error calling"
303                                          " control_var_to_par: %d.\n", err);
304                 return err;
305         }
306         
307         control_par_to_var(&par, var);
308         
309         if ((activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW)
310                 return 0;
311
312         disp = (con >= 0) ? &fb_display[con] : info->disp;
313
314         depthchange = (disp->var.bits_per_pixel != var->bits_per_pixel);
315         if(!VAR_MATCH(&disp->var, var)) {
316                 struct fb_fix_screeninfo        fix;
317                 control_par_to_fix(&par, &fix, p);
318                 control_par_to_display(&par, disp, &fix, p);
319                 if(info->changevar)
320                         (*info->changevar)(con);
321         } else
322                 disp->var = *var;
323
324         if(con == currcon) {
325                 control_set_hardware(p, &par);
326                 if(depthchange) {
327                         if((err = fb_alloc_cmap(&disp->cmap, 0, 0)))
328                                 return err;
329                         do_install_cmap(disp, info);
330                 }
331         }
332
333         return 0;
334 }
335
336
337 /*
338  * Set screen start address according to var offset values
339  */
340 static inline void set_screen_start(int xoffset, int yoffset,
341         struct fb_info_control *p)
342 {
343         struct fb_par_control *par = &p->par;
344
345         par->xoffset = xoffset;
346         par->yoffset = yoffset;
347         out_le32(CNTRL_REG(p,start_addr),
348                  par->yoffset * par->pitch + (par->xoffset << par->cmode));
349 }
350
351
352 static int control_pan_display(struct fb_var_screeninfo *var, int con,
353                              struct fb_info *info)
354 {
355         unsigned int xoffset, hstep;
356         struct fb_info_control *p = (struct fb_info_control *)info;
357         struct fb_par_control *par = &p->par;
358
359         /*
360          * make sure start addr will be 32-byte aligned
361          */
362         hstep = 0x1f >> par->cmode;
363         xoffset = (var->xoffset + hstep) & ~hstep;
364
365         if (xoffset+par->xres > par->vxres ||
366             var->yoffset+par->yres > par->vyres)
367                 return -EINVAL;
368
369         set_screen_start(xoffset, var->yoffset, p);
370
371         return 0;
372 }
373
374 static int control_get_cmap(struct fb_cmap *cmap, int kspc, int con,
375                           struct fb_info *info)
376 {
377         if (con == currcon)             /* current console? */
378                 return fb_get_cmap(cmap, kspc, controlfb_getcolreg, info);
379         if (fb_display[con].cmap.len)   /* non default colormap? */
380                 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0: 2);
381         else {
382                 int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
383                 fb_copy_cmap(fb_default_cmap(size), cmap, kspc ? 0 : 2);
384         }
385         return 0;
386 }
387
388 static int control_set_cmap(struct fb_cmap *cmap, int kspc, int con,
389                          struct fb_info *info)
390 {
391         struct display *disp = (con < 0)? info->disp: &fb_display[con];
392         int err, size = disp->var.bits_per_pixel == 16 ? 32 : 256;
393
394         if (disp->cmap.len != size) {
395                 err = fb_alloc_cmap(&disp->cmap, size, 0);
396                 if (err)
397                         return err;
398         }
399         if (con == currcon)
400                 return fb_set_cmap(cmap, kspc, controlfb_setcolreg, info);
401         fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
402         return 0;
403 }
404
405
406 /*
407  * Private mmap since we want to have a different caching on the framebuffer
408  * for controlfb.
409  * Note there's no locking in here; it's done in fb_mmap() in fbmem.c.
410  */
411 static int control_mmap(struct fb_info *info, struct file *file,
412                        struct vm_area_struct *vma)
413 {
414        struct fb_ops *fb = info->fbops;
415        struct fb_fix_screeninfo fix;
416        struct fb_var_screeninfo var;
417        unsigned long off, start;
418        u32 len;
419
420        fb->fb_get_fix(&fix, PROC_CONSOLE(info), info);
421        off = vma->vm_pgoff << PAGE_SHIFT;
422
423        /* frame buffer memory */
424        start = fix.smem_start;
425        len = PAGE_ALIGN((start & ~PAGE_MASK)+fix.smem_len);
426        if (off >= len) {
427                /* memory mapped io */
428                off -= len;
429                fb->fb_get_var(&var, PROC_CONSOLE(info), info);
430                if (var.accel_flags)
431                        return -EINVAL;
432                start = fix.mmio_start;
433                len = PAGE_ALIGN((start & ~PAGE_MASK)+fix.mmio_len);
434                pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
435        } else {
436                /* framebuffer */
437                pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
438        }
439        start &= PAGE_MASK;
440        if ((vma->vm_end - vma->vm_start + off) > len)
441                 return -EINVAL;
442        off += start;
443        vma->vm_pgoff = off >> PAGE_SHIFT;
444        if (io_remap_page_range(vma->vm_start, off,
445            vma->vm_end - vma->vm_start, vma->vm_page_prot))
446                return -EAGAIN;
447
448        return 0;
449 }
450
451
452 /********************  End of controlfb_ops implementation  ******************/
453
454 /*
455  * low level fbcon ops
456  */
457
458 static int controlfb_switch(int con, struct fb_info *info)
459 {
460         struct fb_info_control  *p = (struct fb_info_control *)info;
461         struct fb_par_control   par;
462
463         if (currcon >= 0 && fb_display[currcon].cmap.len)
464                 fb_get_cmap(&fb_display[currcon].cmap, 1, controlfb_getcolreg,
465                             info);
466         currcon = con;
467
468         fb_display[con].var.activate = FB_ACTIVATE_NOW;
469         control_var_to_par(&fb_display[con].var, &par, info);
470         control_set_hardware(p, &par);
471         control_set_dispsw(&fb_display[con], par.cmode, p);
472         do_install_cmap(&fb_display[con], info);
473
474         return 1;
475 }
476
477
478 static int controlfb_updatevar(int con, struct fb_info *info)
479 {
480         struct fb_var_screeninfo *var = &fb_display[con].var;
481         struct fb_info_control *p = (struct fb_info_control *) info;
482
483         set_screen_start(var->xoffset, var->yoffset, p);
484
485         return 0;
486 }
487
488
489 static void controlfb_blank(int blank_mode, struct fb_info *info)
490 {
491         struct fb_info_control *p = (struct fb_info_control *) info;
492         unsigned ctrl;
493
494         ctrl = ld_le32(CNTRL_REG(p,ctrl));
495         if (blank_mode > 0)
496                 switch (blank_mode - 1) {
497                 case VESA_VSYNC_SUSPEND:
498                         ctrl &= ~3;
499                         break;
500                 case VESA_HSYNC_SUSPEND:
501                         ctrl &= ~0x30;
502                         break;
503                 case VESA_POWERDOWN:
504                         ctrl &= ~0x33;
505                         /* fall through */
506                 case VESA_NO_BLANKING:
507                         ctrl |= 0x400;
508                         break;
509                 default:
510                         break;
511                 }
512         else {
513                 ctrl &= ~0x400;
514                 ctrl |= 0x33;
515         }
516         out_le32(CNTRL_REG(p,ctrl), ctrl);
517
518         return;
519 }
520
521
522 /*
523  * low level cmap set/get ops
524  */
525
526 static int controlfb_getcolreg(u_int regno, u_int *red, u_int *green,
527                              u_int *blue, u_int *transp, struct fb_info *info)
528 {
529         struct fb_info_control *p = (struct fb_info_control *) info;
530
531         if (regno > 255)
532                 return 1;
533         *red = (p->palette[regno].red<<8) | p->palette[regno].red;
534         *green = (p->palette[regno].green<<8) | p->palette[regno].green;
535         *blue = (p->palette[regno].blue<<8) | p->palette[regno].blue;
536         *transp = 0;
537         return 0;
538 }
539
540 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
541                              u_int transp, struct fb_info *info)
542 {
543         struct fb_info_control *p = (struct fb_info_control *) info;
544         u_int i;
545         __u8 r, g, b;
546
547         if (regno > 255)
548                 return 1;
549
550         r = red >> 8;
551         g = green >> 8;
552         b = blue >> 8;
553
554         p->palette[regno].red = r;
555         p->palette[regno].green = g;
556         p->palette[regno].blue = b;
557
558         out_8(&p->cmap_regs->addr, regno);      /* tell clut what addr to fill  */
559         out_8(&p->cmap_regs->lut, r);           /* send one color channel at    */
560         out_8(&p->cmap_regs->lut, g);           /* a time...                    */
561         out_8(&p->cmap_regs->lut, b);
562
563         if (regno < 16)
564                 switch (p->par.cmode) {
565 #ifdef FBCON_HAS_CFB16
566                         case CMODE_16:
567                                 p->fbcon_cmap.cfb16[regno] = (regno << 10) | (regno << 5) | regno;
568                                 break;
569 #endif
570 #ifdef FBCON_HAS_CFB32
571                         case CMODE_32:
572                                 i = (regno << 8) | regno;
573                                 p->fbcon_cmap.cfb32[regno] = (i << 16) | i;
574                                 break;
575 #endif
576                 }
577         return 0;
578 }
579
580 static void do_install_cmap(struct display *disp, struct fb_info *info)
581 {
582         if (disp->cmap.len)
583                 fb_set_cmap(&disp->cmap, 1, controlfb_setcolreg,
584                             info);
585         else {
586                 int size = disp->var.bits_per_pixel == 16 ? 32 : 256;
587                 fb_set_cmap(fb_default_cmap(size), 1, controlfb_setcolreg,
588                             info);
589         }
590 }
591
592
593 static void set_control_clock(unsigned char *params)
594 {
595 #ifdef CONFIG_ADB_CUDA
596         struct adb_request req;
597         int i;
598
599         for (i = 0; i < 3; ++i) {
600                 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
601                              0x50, i + 1, params[i]);
602                 while (!req.complete)
603                         cuda_poll();
604         }
605 #endif  
606 }
607
608
609 /*
610  * finish off the driver initialization and register
611  */
612 static int __init init_control(struct fb_info_control *p)
613 {
614         int full, sense, vmode, cmode, vyres;
615         struct fb_var_screeninfo var;
616
617         printk(KERN_INFO "controlfb: ");
618
619         full = p->total_vram == 0x400000;
620
621         /* Try to pick a video mode out of NVRAM if we have one. */
622         if (default_cmode == CMODE_NVRAM){
623                 cmode = nvram_read_byte(NV_CMODE);
624                 if(cmode < CMODE_8 || cmode > CMODE_32)
625                         cmode = CMODE_8;
626         } else
627                 cmode=default_cmode;
628
629         if (default_vmode == VMODE_NVRAM) {
630                 vmode = nvram_read_byte(NV_VMODE);
631                 if (vmode < 1 || vmode > VMODE_MAX ||
632                     control_mac_modes[vmode - 1].m[full] < cmode) {
633                         sense = read_control_sense(p);
634                         printk("Monitor sense value = 0x%x, ", sense);
635                         vmode = mac_map_monitor_sense(sense);
636                         if (control_mac_modes[vmode - 1].m[full] < cmode)
637                                 vmode = VMODE_640_480_60;
638                 }
639         } else {
640                 vmode=default_vmode;
641                 if (control_mac_modes[vmode - 1].m[full] < cmode) {
642                         if (cmode > CMODE_8)
643                                 cmode--;
644                         else
645                                 vmode = VMODE_640_480_60;
646                 }
647         }
648
649         if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
650                 /* This shouldn't happen! */
651                 printk("mac_vmode_to_var(%d, %d,) failed\n", vmode, cmode);
652 try_again:
653                 vmode = VMODE_640_480_60;
654                 cmode = CMODE_8;
655                 if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
656                         printk(KERN_ERR "controlfb: mac_vmode_to_var() failed\n");
657                         return -ENXIO;
658                 }
659                 printk(KERN_INFO "controlfb: ");
660         }
661         printk("using video mode %d and color mode %d.\n", vmode, cmode);
662
663         vyres = (p->total_vram - CTRLFB_OFF) / (var.xres << cmode);
664         if (vyres > var.yres)
665                 var.yres_virtual = vyres;
666
667         control_init_info(&p->info, p);
668         currcon = -1;
669         var.activate = FB_ACTIVATE_NOW;
670
671         if (control_set_var(&var, -1, &p->info) < 0) {
672                 if (vmode != VMODE_640_480_60 || cmode != CMODE_8)
673                         goto try_again;
674                 printk(KERN_ERR "controlfb: initilization failed\n");
675                 return -ENXIO;
676         }
677
678         p->info.flags = FBINFO_FLAG_DEFAULT;
679         if (register_framebuffer(&p->info) < 0)
680                 return -ENXIO;
681         
682         printk(KERN_INFO "fb%d: control display adapter\n", GET_FB_IDX(p->info.node));  
683
684         return 0;
685 }
686
687 #define RADACAL_WRITE(a,d) \
688         out_8(&p->cmap_regs->addr, (a)); \
689         out_8(&p->cmap_regs->dat,   (d))
690
691 /* Now how about actually saying, Make it so! */
692 /* Some things in here probably don't need to be done each time. */
693 static void control_set_hardware(struct fb_info_control *p, struct fb_par_control *par)
694 {
695         struct control_regvals  *r;
696         volatile struct preg    *rp;
697         int                     i, cmode;
698
699         if (PAR_EQUAL(&p->par, par)) {
700                 /*
701                  * check if only xoffset or yoffset differs.
702                  * this prevents flickers in typical VT switch case.
703                  */
704                 if (p->par.xoffset != par->xoffset ||
705                     p->par.yoffset != par->yoffset)
706                         set_screen_start(par->xoffset, par->yoffset, p);
707                         
708                 return;
709         }
710         
711         p->par = *par;
712         cmode = p->par.cmode;
713         r = &par->regvals;
714         
715         /* Turn off display */
716         out_le32(CNTRL_REG(p,ctrl), 0x400 | par->ctrl);
717         
718         set_control_clock(r->clock_params);
719         
720         RADACAL_WRITE(0x20, r->radacal_ctrl);
721         RADACAL_WRITE(0x21, p->control_use_bank2 ? 0 : 1);
722         RADACAL_WRITE(0x10, 0);
723         RADACAL_WRITE(0x11, 0);
724
725         rp = &p->control_regs->vswin;
726         for (i = 0; i < 16; ++i, ++rp)
727                 out_le32(&rp->r, r->regs[i]);
728         
729         out_le32(CNTRL_REG(p,pitch), par->pitch);
730         out_le32(CNTRL_REG(p,mode), r->mode);
731         out_le32(CNTRL_REG(p,vram_attr), p->vram_attr);
732         out_le32(CNTRL_REG(p,start_addr), par->yoffset * par->pitch
733                  + (par->xoffset << cmode));
734         out_le32(CNTRL_REG(p,rfrcnt), 0x1e5);
735         out_le32(CNTRL_REG(p,intr_ena), 0);
736
737         /* Turn on display */
738         out_le32(CNTRL_REG(p,ctrl), par->ctrl);
739
740 #ifdef CONFIG_FB_COMPAT_XPMAC
741         /* And let the world know the truth. */
742         if (!console_fb_info || console_fb_info == &p->info) {
743                 display_info.height = p->par.yres;
744                 display_info.width = p->par.xres;
745                 display_info.depth = (cmode == CMODE_32) ? 32 :
746                         ((cmode == CMODE_16) ? 16 : 8);
747                 display_info.pitch = p->par.pitch;
748                 display_info.mode = p->par.vmode;
749                 strncpy(display_info.name, "control",
750                         sizeof(display_info.name));
751                 display_info.fb_address = p->frame_buffer_phys + CTRLFB_OFF;
752                 display_info.cmap_adr_address = p->cmap_regs_phys;
753                 display_info.cmap_data_address = p->cmap_regs_phys + 0x30;
754                 display_info.disp_reg_address = p->control_regs_phys;
755                 console_fb_info = &p->info;
756         }
757 #endif /* CONFIG_FB_COMPAT_XPMAC */
758 }
759
760
761 /*
762  * Called from fbmem.c for probing & intializing
763  */
764 int __init control_init(void)
765 {
766         struct device_node *dp;
767
768         dp = find_devices("control");
769         if (dp != 0 && !control_of_init(dp))
770                 return 0;
771
772         return -ENXIO;
773 }
774
775
776 /* Work out which banks of VRAM we have installed. */
777 /* danj: I guess the card just ignores writes to nonexistant VRAM... */
778
779 static void __init find_vram_size(struct fb_info_control *p)
780 {
781         int bank1, bank2;
782
783         /*
784          * Set VRAM in 2MB (bank 1) mode
785          * VRAM Bank 2 will be accessible through offset 0x600000 if present
786          * and VRAM Bank 1 will not respond at that offset even if present
787          */
788         out_le32(CNTRL_REG(p,vram_attr), 0x31);
789
790         out_8(&p->frame_buffer[0x600000], 0xb3);
791         out_8(&p->frame_buffer[0x600001], 0x71);
792         asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0x600000])
793                                         : "memory" );
794         mb();
795         asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0x600000])
796                                         : "memory" );
797         mb();
798
799         bank2 = (in_8(&p->frame_buffer[0x600000]) == 0xb3)
800                 && (in_8(&p->frame_buffer[0x600001]) == 0x71);
801
802         /*
803          * Set VRAM in 2MB (bank 2) mode
804          * VRAM Bank 1 will be accessible through offset 0x000000 if present
805          * and VRAM Bank 2 will not respond at that offset even if present
806          */
807         out_le32(CNTRL_REG(p,vram_attr), 0x39);
808
809         out_8(&p->frame_buffer[0], 0x5a);
810         out_8(&p->frame_buffer[1], 0xc7);
811         asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0])
812                                         : "memory" );
813         mb();
814         asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0])
815                                         : "memory" );
816         mb();
817
818         bank1 = (in_8(&p->frame_buffer[0]) == 0x5a)
819                 && (in_8(&p->frame_buffer[1]) == 0xc7);
820
821         if (bank2) {
822                 if (!bank1) {
823                         /*
824                          * vram bank 2 only
825                          */
826                         p->control_use_bank2 = 1;
827                         p->vram_attr = 0x39;
828                         p->frame_buffer += 0x600000;
829                         p->frame_buffer_phys += 0x600000;
830                 } else {
831                         /*
832                          * 4 MB vram
833                          */
834                         p->vram_attr = 0x51;
835                 }
836         } else {
837                 /*
838                  * vram bank 1 only
839                  */
840                 p->vram_attr = 0x31;
841         }
842
843         p->total_vram = (bank1 + bank2) * 0x200000;
844
845         printk(KERN_INFO "controlfb: VRAM Total = %dMB "
846                         "(%dMB @ bank 1, %dMB @ bank 2)\n",
847                         (bank1 + bank2) << 1, bank1 << 1, bank2 << 1);
848 }
849
850
851 /*
852  * find "control" and initialize
853  */
854 static int __init control_of_init(struct device_node *dp)
855 {
856         struct fb_info_control  *p;
857         unsigned long           addr;
858         int                     i;
859
860         if (control_fb) {
861                 printk(KERN_ERR "controlfb: only one control is supported\n");
862                 return -ENXIO;
863         }
864         if(dp->n_addrs != 2) {
865                 printk(KERN_ERR "expecting 2 address for control (got %d)", dp->n_addrs);
866                 return -ENXIO;
867         }
868         p = kmalloc(sizeof(*p), GFP_ATOMIC);
869         if (p == 0)
870                 return -ENXIO;
871         control_fb = p; /* save it for cleanups */
872         memset(p, 0, sizeof(*p));
873
874         /* Map in frame buffer and registers */
875         for (i = 0; i < dp->n_addrs; ++i) {
876                 addr = dp->addrs[i].address;
877                 if (dp->addrs[i].size >= 0x800000) {
878                         p->fb_orig_base = addr;
879                         p->fb_orig_size = dp->addrs[i].size;
880                         /* use the big-endian aperture (??) */
881                         p->frame_buffer_phys = addr + 0x800000;
882                 } else {
883                         p->control_regs_phys = addr;
884                         p->control_regs_size = dp->addrs[i].size;
885                 }
886         }
887
888         if (!p->fb_orig_base ||
889             !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
890                 p->fb_orig_base = 0;
891                 goto error_out;
892         }
893         /* map at most 8MB for the frame buffer */
894         p->frame_buffer = __ioremap(p->frame_buffer_phys, 0x800000,
895                                     _PAGE_WRITETHRU);
896
897         if (!p->control_regs_phys ||
898             !request_mem_region(p->control_regs_phys, p->control_regs_size,
899             "controlfb regs")) {
900                 p->control_regs_phys = 0;
901                 goto error_out;
902         }
903         p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
904
905         p->cmap_regs_phys = 0xf301b000;  /* XXX not in prom? */
906         if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
907                 p->cmap_regs_phys = 0;
908                 goto error_out;
909         }
910         p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
911
912         if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
913                 goto error_out;
914
915         find_vram_size(p);
916         if (!p->total_vram)
917                 goto error_out;
918
919         if (init_control(p) < 0)
920                 goto error_out;
921
922         return 0;
923
924 error_out:
925         control_cleanup();
926         return -ENXIO;
927 }
928
929 /*
930  * Get the monitor sense value.
931  * Note that this can be called before calibrate_delay,
932  * so we can't use udelay.
933  */
934 static int read_control_sense(struct fb_info_control *p)
935 {
936         int sense;
937
938         out_le32(CNTRL_REG(p,mon_sense), 7);    /* drive all lines high */
939         __delay(200);
940         out_le32(CNTRL_REG(p,mon_sense), 077);  /* turn off drivers */
941         __delay(2000);
942         sense = (in_le32(CNTRL_REG(p,mon_sense)) & 0x1c0) << 2;
943
944         /* drive each sense line low in turn and collect the other 2 */
945         out_le32(CNTRL_REG(p,mon_sense), 033);  /* drive A low */
946         __delay(2000);
947         sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0xc0) >> 2;
948         out_le32(CNTRL_REG(p,mon_sense), 055);  /* drive B low */
949         __delay(2000);
950         sense |= ((in_le32(CNTRL_REG(p,mon_sense)) & 0x100) >> 5)
951                 | ((in_le32(CNTRL_REG(p,mon_sense)) & 0x40) >> 4);
952         out_le32(CNTRL_REG(p,mon_sense), 066);  /* drive C low */
953         __delay(2000);
954         sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0x180) >> 7;
955
956         out_le32(CNTRL_REG(p,mon_sense), 077);  /* turn off drivers */
957         
958         return sense;
959 }
960
961 /**********************  Various translation functions  **********************/
962
963 #define CONTROL_PIXCLOCK_BASE   256016
964 #define CONTROL_PIXCLOCK_MIN    5000    /* ~ 200 MHz dot clock */
965
966 /*
967  * calculate the clock paramaters to be sent to CUDA according to given
968  * pixclock in pico second.
969  */
970 static int calc_clock_params(unsigned long clk, unsigned char *param)
971 {
972         unsigned long p0, p1, p2, k, l, m, n, min;
973
974         if (clk > (CONTROL_PIXCLOCK_BASE << 3))
975                 return 1;
976
977         p2 = ((clk << 4) < CONTROL_PIXCLOCK_BASE)? 3: 2;
978         l = clk << p2;
979         p0 = 0;
980         p1 = 0;
981         for (k = 1, min = l; k < 32; k++) {
982                 unsigned long rem;
983
984                 m = CONTROL_PIXCLOCK_BASE * k;
985                 n = m / l;
986                 rem = m % l;
987                 if (n && (n < 128) && rem < min) {
988                         p0 = k;
989                         p1 = n;
990                         min = rem;
991                 }
992         }
993         if (!p0 || !p1)
994                 return 1;
995
996         param[0] = p0;
997         param[1] = p1;
998         param[2] = p2;
999
1000         return 0;
1001 }
1002
1003
1004 /*
1005  * This routine takes a user-supplied var, and picks the best vmode/cmode
1006  * from it.
1007  */
1008
1009 static int control_var_to_par(struct fb_var_screeninfo *var,
1010         struct fb_par_control *par, const struct fb_info *fb_info)
1011 {
1012         int cmode, piped_diff, hstep;
1013         unsigned hperiod, hssync, hsblank, hesync, heblank, piped, heq, hlfln,
1014                  hserr, vperiod, vssync, vesync, veblank, vsblank, vswin, vewin;
1015         unsigned long pixclock;
1016         struct fb_info_control *p = (struct fb_info_control *) fb_info;
1017         struct control_regvals *r = &par->regvals;
1018
1019         switch (var->bits_per_pixel) {
1020         case 8:
1021                 par->cmode = CMODE_8;
1022                 if (p->total_vram > 0x200000) {
1023                         r->mode = 3;
1024                         r->radacal_ctrl = 0x20;
1025                         piped_diff = 13;
1026                 } else {
1027                         r->mode = 2;
1028                         r->radacal_ctrl = 0x10;
1029                         piped_diff = 9;
1030                 }
1031                 break;
1032         case 15:
1033         case 16:
1034                 par->cmode = CMODE_16;
1035                 if (p->total_vram > 0x200000) {
1036                         r->mode = 2;
1037                         r->radacal_ctrl = 0x24;
1038                         piped_diff = 5;
1039                 } else {
1040                         r->mode = 1;
1041                         r->radacal_ctrl = 0x14;
1042                         piped_diff = 3;
1043                 }
1044                 break;
1045         case 32:
1046                 par->cmode = CMODE_32;
1047                 if (p->total_vram > 0x200000) {
1048                         r->mode = 1;
1049                         r->radacal_ctrl = 0x28;
1050                 } else {
1051                         r->mode = 0;
1052                         r->radacal_ctrl = 0x18;
1053                 }
1054                 piped_diff = 1;
1055                 break;
1056         default:
1057                 return -EINVAL;
1058         }
1059
1060         /*
1061          * adjust xres and vxres so that the corresponding memory widths are
1062          * 32-byte aligned
1063          */
1064         hstep = 31 >> par->cmode;
1065         par->xres = (var->xres + hstep) & ~hstep;
1066         par->vxres = (var->xres_virtual + hstep) & ~hstep;
1067         par->xoffset = (var->xoffset + hstep) & ~hstep;
1068         if (par->vxres < par->xres)
1069                 par->vxres = par->xres;
1070         par->pitch = par->vxres << par->cmode;
1071
1072         par->yres = var->yres;
1073         par->vyres = var->yres_virtual;
1074         par->yoffset = var->yoffset;
1075         if (par->vyres < par->yres)
1076                 par->vyres = par->yres;
1077
1078         par->sync = var->sync;
1079
1080         if (par->pitch * par->vyres + CTRLFB_OFF > p->total_vram)
1081                 return -EINVAL;
1082
1083         if (par->xoffset + par->xres > par->vxres)
1084                 par->xoffset = par->vxres - par->xres;
1085         if (par->yoffset + par->yres > par->vyres)
1086                 par->yoffset = par->vyres - par->yres;
1087
1088         pixclock = (var->pixclock < CONTROL_PIXCLOCK_MIN)? CONTROL_PIXCLOCK_MIN:
1089                    var->pixclock;
1090         if (calc_clock_params(pixclock, r->clock_params))
1091                 return -EINVAL;
1092
1093         hperiod = ((var->left_margin + par->xres + var->right_margin
1094                     + var->hsync_len) >> 1) - 2;
1095         hssync = hperiod + 1;
1096         hsblank = hssync - (var->right_margin >> 1);
1097         hesync = (var->hsync_len >> 1) - 1;
1098         heblank = (var->left_margin >> 1) + hesync;
1099         piped = heblank - piped_diff;
1100         heq = var->hsync_len >> 2;
1101         hlfln = (hperiod+2) >> 1;
1102         hserr = hssync-hesync;
1103         vperiod = (var->vsync_len + var->lower_margin + par->yres
1104                    + var->upper_margin) << 1;
1105         vssync = vperiod - 2;
1106         vesync = (var->vsync_len << 1) - vperiod + vssync;
1107         veblank = (var->upper_margin << 1) + vesync;
1108         vsblank = vssync - (var->lower_margin << 1);
1109         vswin = (vsblank+vssync) >> 1;
1110         vewin = (vesync+veblank) >> 1;
1111
1112         r->regs[0] = vswin;
1113         r->regs[1] = vsblank;
1114         r->regs[2] = veblank;
1115         r->regs[3] = vewin;
1116         r->regs[4] = vesync;
1117         r->regs[5] = vssync;
1118         r->regs[6] = vperiod;
1119         r->regs[7] = piped;
1120         r->regs[8] = hperiod;
1121         r->regs[9] = hsblank;
1122         r->regs[10] = heblank;
1123         r->regs[11] = hesync;
1124         r->regs[12] = hssync;
1125         r->regs[13] = heq;
1126         r->regs[14] = hlfln;
1127         r->regs[15] = hserr;
1128
1129         if (par->xres >= 1280 && par->cmode >= CMODE_16)
1130                 par->ctrl = 0x7f;
1131         else
1132                 par->ctrl = 0x3b;
1133
1134         if (mac_var_to_vmode(var, &par->vmode, &cmode))
1135                 par->vmode = 0;
1136
1137         return 0;
1138 }
1139
1140
1141 /*
1142  * Convert hardware data in par to an fb_var_screeninfo
1143  */
1144
1145 static void control_par_to_var(struct fb_par_control *par, struct fb_var_screeninfo *var)
1146 {
1147         struct control_regints *rv;
1148         
1149         rv = (struct control_regints *) par->regvals.regs;
1150         
1151         memset(var, 0, sizeof(*var));
1152         var->xres = par->xres;
1153         var->yres = par->yres;
1154         var->xres_virtual = par->vxres;
1155         var->yres_virtual = par->vyres;
1156         var->xoffset = par->xoffset;
1157         var->yoffset = par->yoffset;
1158         
1159         switch(par->cmode) {
1160         default:
1161         case CMODE_8:
1162                 var->bits_per_pixel = 8;
1163                 var->red.length = 8;
1164                 var->green.length = 8;
1165                 var->blue.length = 8;
1166                 break;
1167         case CMODE_16:  /* RGB 555 */
1168                 var->bits_per_pixel = 16;
1169                 var->red.offset = 10;
1170                 var->red.length = 5;
1171                 var->green.offset = 5;
1172                 var->green.length = 5;
1173                 var->blue.length = 5;
1174                 break;
1175         case CMODE_32:  /* RGB 888 */
1176                 var->bits_per_pixel = 32;
1177                 var->red.offset = 16;
1178                 var->red.length = 8;
1179                 var->green.offset = 8;
1180                 var->green.length = 8;
1181                 var->blue.length = 8;
1182                 var->transp.offset = 24;
1183                 var->transp.length = 8;
1184                 break;
1185         }
1186         var->height = -1;
1187         var->width = -1;
1188         var->vmode = FB_VMODE_NONINTERLACED;
1189
1190         var->left_margin = (rv->heblank - rv->hesync) << 1;
1191         var->right_margin = (rv->hssync - rv->hsblank) << 1;
1192         var->hsync_len = (rv->hperiod + 2 - rv->hssync + rv->hesync) << 1;
1193
1194         var->upper_margin = (rv->veblank - rv->vesync) >> 1;
1195         var->lower_margin = (rv->vssync - rv->vsblank) >> 1;
1196         var->vsync_len = (rv->vperiod - rv->vssync + rv->vesync) >> 1;
1197
1198         var->sync = par->sync;
1199
1200         /*
1201          * 10^12 * clock_params[0] / (3906400 * clock_params[1]
1202          *                            * 2^clock_params[2])
1203          * (10^12 * clock_params[0] / (3906400 * clock_params[1]))
1204          * >> clock_params[2]
1205          */
1206         /* (255990.17 * clock_params[0] / clock_params[1]) >> clock_params[2] */
1207         var->pixclock = CONTROL_PIXCLOCK_BASE * par->regvals.clock_params[0];
1208         var->pixclock /= par->regvals.clock_params[1];
1209         var->pixclock >>= par->regvals.clock_params[2];
1210 }
1211
1212
1213 /*
1214  * init fix according to given par
1215  */
1216 static void control_par_to_fix(struct fb_par_control *par, struct fb_fix_screeninfo *fix,
1217         struct fb_info_control *p)
1218 {
1219         memset(fix, 0, sizeof(*fix));
1220         strcpy(fix->id, "control");
1221         fix->mmio_start = p->control_regs_phys;
1222         fix->mmio_len = sizeof(struct control_regs);
1223         fix->type = FB_TYPE_PACKED_PIXELS;
1224         
1225         fix->xpanstep = 32 >> par->cmode;
1226         fix->ypanstep = 1;
1227
1228         fix->smem_start = p->frame_buffer_phys + CTRLFB_OFF;
1229         fix->smem_len = p->total_vram - CTRLFB_OFF;
1230         fix->visual = (par->cmode == CMODE_8) ?
1231                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1232         fix->line_length = par->pitch;
1233 }
1234
1235 /*
1236  * initialize a portion of struct display which low level driver is responsible
1237  * for.
1238  */
1239 static void control_par_to_display(struct fb_par_control *par,
1240   struct display *disp, struct fb_fix_screeninfo *fix, struct fb_info_control *p)
1241 {
1242         disp->type = fix->type;
1243         disp->can_soft_blank = 1;
1244         disp->scrollmode = SCROLL_YNOMOVE | SCROLL_YNOPARTIAL;
1245         disp->ypanstep = fix->ypanstep;
1246         disp->ywrapstep = fix->ywrapstep;
1247
1248         control_par_to_var(par, &disp->var);
1249         disp->screen_base = (char *) p->frame_buffer + CTRLFB_OFF;
1250         disp->visual = fix->visual;
1251         disp->line_length = fix->line_length;
1252         control_set_dispsw(disp, par->cmode, p);
1253 }
1254
1255
1256 /*
1257  * our own _revc() routines since generic routines don't work for DIRECT Color
1258  * devices like control
1259  */
1260 static void control_cfb16_revc(struct display *p, int xx, int yy)
1261 {
1262     u8 *dest;
1263     int bytes = p->next_line, rows;
1264
1265     dest = p->screen_base + yy * fontheight(p) * bytes + xx * fontwidth(p)*2;
1266     for (rows = fontheight(p); rows--; dest += bytes) {
1267        switch (fontwidth(p)) {
1268        case 16:
1269            ((u32 *)dest)[6] ^= 0x3def3def; ((u32 *)dest)[7] ^= 0x3def3def;
1270            /* FALL THROUGH */
1271        case 12:
1272            ((u32 *)dest)[4] ^= 0x3def3def; ((u32 *)dest)[5] ^= 0x3def3def;
1273            /* FALL THROUGH */
1274        case 8:
1275            ((u32 *)dest)[2] ^= 0x3def3def; ((u32 *)dest)[3] ^= 0x3def3def;
1276            /* FALL THROUGH */
1277        case 4:
1278            ((u32 *)dest)[0] ^= 0x3def3def; ((u32 *)dest)[1] ^= 0x3def3def;
1279        }
1280     }
1281 }
1282
1283 static void control_cfb32_revc(struct display *p, int xx, int yy)
1284 {
1285     u8 *dest;
1286     int bytes = p->next_line, rows;
1287
1288     dest = p->screen_base + yy * fontheight(p) * bytes + xx * fontwidth(p) * 4;
1289     for (rows = fontheight(p); rows--; dest += bytes) {
1290        switch (fontwidth(p)) {
1291        case 16:
1292            ((u32 *)dest)[12] ^= 0x0f0f0f0f; ((u32 *)dest)[13] ^= 0x0f0f0f0f;
1293            ((u32 *)dest)[14] ^= 0x0f0f0f0f; ((u32 *)dest)[15] ^= 0x0f0f0f0f;
1294            /* FALL THROUGH */
1295        case 12:
1296            ((u32 *)dest)[8] ^= 0x0f0f0f0f; ((u32 *)dest)[9] ^= 0x0f0f0f0f;
1297            ((u32 *)dest)[10] ^= 0x0f0f0f0f; ((u32 *)dest)[11] ^= 0x0f0f0f0f;
1298            /* FALL THROUGH */
1299        case 8:
1300            ((u32 *)dest)[4] ^= 0x0f0f0f0f; ((u32 *)dest)[5] ^= 0x0f0f0f0f;
1301            ((u32 *)dest)[6] ^= 0x0f0f0f0f; ((u32 *)dest)[7] ^= 0x0f0f0f0f;
1302            /* FALL THROUGH */
1303        case 4:
1304            ((u32 *)dest)[0] ^= 0x0f0f0f0f; ((u32 *)dest)[1] ^= 0x0f0f0f0f;
1305            ((u32 *)dest)[2] ^= 0x0f0f0f0f; ((u32 *)dest)[3] ^= 0x0f0f0f0f;
1306            /* FALL THROUGH */
1307        }
1308     }
1309 }
1310
1311 static struct display_switch control_cfb16 = {
1312     setup:              fbcon_cfb16_setup,
1313     bmove:              fbcon_cfb16_bmove,
1314     clear:              fbcon_cfb16_clear,
1315     putc:               fbcon_cfb16_putc,
1316     putcs:              fbcon_cfb16_putcs,
1317     revc:               control_cfb16_revc,
1318     clear_margins:      fbcon_cfb16_clear_margins,
1319     fontwidthmask:      FONTWIDTH(4)|FONTWIDTH(8)|FONTWIDTH(12)|FONTWIDTH(16)
1320 };
1321
1322 static struct display_switch control_cfb32 = {
1323     setup:              fbcon_cfb32_setup,
1324     bmove:              fbcon_cfb32_bmove,
1325     clear:              fbcon_cfb32_clear,
1326     putc:               fbcon_cfb32_putc,
1327     putcs:              fbcon_cfb32_putcs,
1328     revc:               control_cfb32_revc,
1329     clear_margins:      fbcon_cfb32_clear_margins,
1330     fontwidthmask:      FONTWIDTH(4)|FONTWIDTH(8)|FONTWIDTH(12)|FONTWIDTH(16)
1331 };
1332
1333
1334 /*
1335  * Set struct dispsw according to given cmode
1336  */
1337 static void control_set_dispsw(struct display *disp, int cmode, struct fb_info_control *p)
1338 {
1339         switch (cmode) {
1340 #ifdef FBCON_HAS_CFB8
1341                 case CMODE_8:
1342                         disp->dispsw = &fbcon_cfb8;
1343                         break;
1344 #endif
1345 #ifdef FBCON_HAS_CFB16
1346                 case CMODE_16:
1347                         disp->dispsw = &control_cfb16;
1348                         disp->dispsw_data = p->fbcon_cmap.cfb16;
1349                         break;
1350 #endif
1351 #ifdef FBCON_HAS_CFB32
1352                 case CMODE_32:
1353                         disp->dispsw = &control_cfb32;
1354                         disp->dispsw_data = p->fbcon_cmap.cfb32;
1355                         break;
1356 #endif
1357                 default:
1358                         disp->dispsw = &fbcon_dummy;
1359                         break;
1360         }
1361 }
1362
1363
1364 /*
1365  * Set misc info vars for this driver
1366  */
1367 static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
1368 {
1369         strcpy(info->modename, "control");
1370         info->node = -1;        /* ??? danj */
1371         info->fbops = &controlfb_ops;
1372         info->disp = &p->display;
1373         strcpy(info->fontname, fontname);
1374         info->changevar = NULL;
1375         info->switch_con = &controlfb_switch;
1376         info->updatevar = &controlfb_updatevar;
1377         info->blank = &controlfb_blank;
1378 }
1379
1380
1381 static void control_cleanup(void)
1382 {
1383         struct fb_info_control  *p = control_fb;
1384
1385         if (!p)
1386                 return;
1387
1388         if (p->cmap_regs)
1389                 iounmap(p->cmap_regs);
1390         if (p->control_regs)
1391                 iounmap(p->control_regs);
1392         if (p->frame_buffer) {
1393                 if (p->control_use_bank2)
1394                         p->frame_buffer -= 0x600000;
1395                 iounmap(p->frame_buffer);
1396         }
1397         if (p->cmap_regs_phys)
1398                 release_mem_region(p->cmap_regs_phys, 0x1000);
1399         if (p->control_regs_phys)
1400                 release_mem_region(p->control_regs_phys, p->control_regs_size);
1401         if (p->fb_orig_base)
1402                 release_mem_region(p->fb_orig_base, p->fb_orig_size);
1403         kfree(p);
1404 }
1405
1406
1407 /*
1408  * Parse user speficied options (`video=controlfb:')
1409  */
1410 void __init control_setup(char *options)
1411 {
1412         char *this_opt;
1413
1414         if (!options || !*options)
1415                 return;
1416
1417         for (this_opt = strtok(options, ","); this_opt;
1418              this_opt = strtok(NULL, ",")) {
1419                 if (!strncmp(this_opt, "font:", 5)) {
1420                         char *p;
1421                         int i;
1422
1423                         p = this_opt +5;
1424                         for (i = 0; i < sizeof(fontname) - 1; i++)
1425                                 if (!*p || *p == ' ' || *p == ',')
1426                                         break;
1427                         memcpy(fontname, this_opt + 5, i);
1428                         fontname[i] = 0;
1429                 } else if (!strncmp(this_opt, "vmode:", 6)) {
1430                         int vmode = simple_strtoul(this_opt+6, NULL, 0);
1431                         if (vmode > 0 && vmode <= VMODE_MAX &&
1432                             control_mac_modes[vmode - 1].m[1] >= 0)
1433                                 default_vmode = vmode;
1434                 } else if (!strncmp(this_opt, "cmode:", 6)) {
1435                         int depth = simple_strtoul(this_opt+6, NULL, 0);
1436                         switch (depth) {
1437                          case CMODE_8:
1438                          case CMODE_16:
1439                          case CMODE_32:
1440                                 default_cmode = depth;
1441                                 break;
1442                          case 8:
1443                                 default_cmode = CMODE_8;
1444                                 break;
1445                          case 15:
1446                          case 16:
1447                                 default_cmode = CMODE_16;
1448                                 break;
1449                          case 24:
1450                          case 32:
1451                                 default_cmode = CMODE_32;
1452                                 break;
1453                         }
1454                 }
1455         }
1456 }
1457