Remove obsolete #include <linux/config.h>
[safe/jmp/linux-2.6] / 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  *  Updated to 2.5 framebuffer API by Ben Herrenschmidt
26  *  <benh@kernel.crashing.org>, Paul Mackerras <paulus@samba.org>,
27  *  and James Simmons <jsimmons@infradead.org>.
28  *
29  *  This file is subject to the terms and conditions of the GNU General Public
30  *  License. See the file COPYING in the main directory of this archive for
31  *  more details.
32  */
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 #include <linux/mm.h>
39 #include <linux/tty.h>
40 #include <linux/slab.h>
41 #include <linux/vmalloc.h>
42 #include <linux/delay.h>
43 #include <linux/interrupt.h>
44 #include <linux/fb.h>
45 #include <linux/init.h>
46 #include <linux/pci.h>
47 #include <linux/nvram.h>
48 #include <linux/adb.h>
49 #include <linux/cuda.h>
50 #include <asm/io.h>
51 #include <asm/prom.h>
52 #include <asm/pgtable.h>
53 #include <asm/btext.h>
54
55 #include "macmodes.h"
56 #include "controlfb.h"
57
58 struct fb_par_control {
59         int     vmode, cmode;
60         int     xres, yres;
61         int     vxres, vyres;
62         int     xoffset, yoffset;
63         int     pitch;
64         struct control_regvals  regvals;
65         unsigned long sync;
66         unsigned char ctrl;
67 };
68
69 #define DIRTY(z) ((x)->z != (y)->z)
70 #define DIRTY_CMAP(z) (memcmp(&((x)->z), &((y)->z), sizeof((y)->z)))
71 static inline int PAR_EQUAL(struct fb_par_control *x, struct fb_par_control *y)
72 {
73         int i, results;
74
75         results = 1;
76         for (i = 0; i < 3; i++)
77                 results &= !DIRTY(regvals.clock_params[i]);
78         if (!results)
79                 return 0;
80         for (i = 0; i < 16; i++)
81                 results &= !DIRTY(regvals.regs[i]);
82         if (!results)
83                 return 0;
84         return (!DIRTY(cmode) && !DIRTY(xres) && !DIRTY(yres)
85                 && !DIRTY(vxres) && !DIRTY(vyres));
86 }
87 static inline int VAR_MATCH(struct fb_var_screeninfo *x, struct fb_var_screeninfo *y)
88 {
89         return (!DIRTY(bits_per_pixel) && !DIRTY(xres)
90                 && !DIRTY(yres) && !DIRTY(xres_virtual)
91                 && !DIRTY(yres_virtual)
92                 && !DIRTY_CMAP(red) && !DIRTY_CMAP(green) && !DIRTY_CMAP(blue));
93 }
94
95 struct fb_info_control {
96         struct fb_info          info;
97         struct fb_par_control   par;
98         u32                     pseudo_palette[17];
99                 
100         struct cmap_regs        __iomem *cmap_regs;
101         unsigned long           cmap_regs_phys;
102         
103         struct control_regs     __iomem *control_regs;
104         unsigned long           control_regs_phys;
105         unsigned long           control_regs_size;
106         
107         __u8                    __iomem *frame_buffer;
108         unsigned long           frame_buffer_phys;
109         unsigned long           fb_orig_base;
110         unsigned long           fb_orig_size;
111
112         int                     control_use_bank2;
113         unsigned long           total_vram;
114         unsigned char           vram_attr;
115 };
116
117 /* control register access macro */
118 #define CNTRL_REG(INFO,REG) (&(((INFO)->control_regs->REG).r))
119
120
121 /******************** Prototypes for exported functions ********************/
122 /*
123  * struct fb_ops
124  */
125 static int controlfb_pan_display(struct fb_var_screeninfo *var,
126         struct fb_info *info);
127 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
128         u_int transp, struct fb_info *info);
129 static int controlfb_blank(int blank_mode, struct fb_info *info);
130 static int controlfb_mmap(struct fb_info *info,
131         struct vm_area_struct *vma);
132 static int controlfb_set_par (struct fb_info *info);
133 static int controlfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info);
134
135 /******************** Prototypes for internal functions **********************/
136
137 static void set_control_clock(unsigned char *params);
138 static int init_control(struct fb_info_control *p);
139 static void control_set_hardware(struct fb_info_control *p,
140         struct fb_par_control *par);
141 static int control_of_init(struct device_node *dp);
142 static void find_vram_size(struct fb_info_control *p);
143 static int read_control_sense(struct fb_info_control *p);
144 static int calc_clock_params(unsigned long clk, unsigned char *param);
145 static int control_var_to_par(struct fb_var_screeninfo *var,
146         struct fb_par_control *par, const struct fb_info *fb_info);
147 static inline void control_par_to_var(struct fb_par_control *par,
148         struct fb_var_screeninfo *var);
149 static void control_init_info(struct fb_info *info, struct fb_info_control *p);
150 static void control_cleanup(void);
151
152
153 /************************** Internal variables *******************************/
154
155 static struct fb_info_control *control_fb;
156
157 static int default_vmode __initdata = VMODE_NVRAM;
158 static int default_cmode __initdata = CMODE_NVRAM;
159
160
161 static struct fb_ops controlfb_ops = {
162         .owner          = THIS_MODULE,
163         .fb_check_var   = controlfb_check_var,
164         .fb_set_par     = controlfb_set_par,
165         .fb_setcolreg   = controlfb_setcolreg,
166         .fb_pan_display = controlfb_pan_display,
167         .fb_blank       = controlfb_blank,
168         .fb_mmap        = controlfb_mmap,
169         .fb_fillrect    = cfb_fillrect,
170         .fb_copyarea    = cfb_copyarea,
171         .fb_imageblit   = cfb_imageblit,
172 };
173
174
175 /********************  The functions for controlfb_ops ********************/
176
177 #ifdef MODULE
178 MODULE_LICENSE("GPL");
179
180 int init_module(void)
181 {
182         struct device_node *dp;
183
184         dp = find_devices("control");
185         if (dp != 0 && !control_of_init(dp))
186                 return 0;
187
188         return -ENXIO;
189 }
190
191 void cleanup_module(void)
192 {
193         control_cleanup();
194 }
195 #endif
196
197 /*
198  * Checks a var structure
199  */
200 static int controlfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info)
201 {
202         struct fb_par_control par;
203         int err;
204
205         err = control_var_to_par(var, &par, info);
206         if (err)
207                 return err;     
208         control_par_to_var(&par, var);
209
210         return 0;
211 }
212
213 /*
214  * Applies current var to display
215  */
216 static int controlfb_set_par (struct fb_info *info)
217 {
218         struct fb_info_control *p = (struct fb_info_control *) info;
219         struct fb_par_control par;
220         int err;
221
222         if((err = control_var_to_par(&info->var, &par, info))) {
223                 printk (KERN_ERR "controlfb_set_par: error calling"
224                                  " control_var_to_par: %d.\n", err);
225                 return err;
226         }
227         
228         control_set_hardware(p, &par);
229
230         info->fix.visual = (p->par.cmode == CMODE_8) ?
231                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
232         info->fix.line_length = p->par.pitch;
233         info->fix.xpanstep = 32 >> p->par.cmode;
234         info->fix.ypanstep = 1;
235
236         return 0;
237 }
238
239 /*
240  * Set screen start address according to var offset values
241  */
242 static inline void set_screen_start(int xoffset, int yoffset,
243         struct fb_info_control *p)
244 {
245         struct fb_par_control *par = &p->par;
246
247         par->xoffset = xoffset;
248         par->yoffset = yoffset;
249         out_le32(CNTRL_REG(p,start_addr),
250                  par->yoffset * par->pitch + (par->xoffset << par->cmode));
251 }
252
253
254 static int controlfb_pan_display(struct fb_var_screeninfo *var,
255                                  struct fb_info *info)
256 {
257         unsigned int xoffset, hstep;
258         struct fb_info_control *p = (struct fb_info_control *)info;
259         struct fb_par_control *par = &p->par;
260
261         /*
262          * make sure start addr will be 32-byte aligned
263          */
264         hstep = 0x1f >> par->cmode;
265         xoffset = (var->xoffset + hstep) & ~hstep;
266
267         if (xoffset+par->xres > par->vxres ||
268             var->yoffset+par->yres > par->vyres)
269                 return -EINVAL;
270
271         set_screen_start(xoffset, var->yoffset, p);
272
273         return 0;
274 }
275
276
277 /*
278  * Private mmap since we want to have a different caching on the framebuffer
279  * for controlfb.
280  * Note there's no locking in here; it's done in fb_mmap() in fbmem.c.
281  */
282 static int controlfb_mmap(struct fb_info *info,
283                        struct vm_area_struct *vma)
284 {
285        unsigned long off, start;
286        u32 len;
287
288        off = vma->vm_pgoff << PAGE_SHIFT;
289
290        /* frame buffer memory */
291        start = info->fix.smem_start;
292        len = PAGE_ALIGN((start & ~PAGE_MASK)+info->fix.smem_len);
293        if (off >= len) {
294                /* memory mapped io */
295                off -= len;
296                if (info->var.accel_flags)
297                        return -EINVAL;
298                start = info->fix.mmio_start;
299                len = PAGE_ALIGN((start & ~PAGE_MASK)+info->fix.mmio_len);
300                pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
301        } else {
302                /* framebuffer */
303                pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
304        }
305        start &= PAGE_MASK;
306        if ((vma->vm_end - vma->vm_start + off) > len)
307                 return -EINVAL;
308        off += start;
309        vma->vm_pgoff = off >> PAGE_SHIFT;
310        if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
311            vma->vm_end - vma->vm_start, vma->vm_page_prot))
312                return -EAGAIN;
313
314        return 0;
315 }
316
317 static int controlfb_blank(int blank_mode, struct fb_info *info)
318 {
319         struct fb_info_control *p = (struct fb_info_control *) info;
320         unsigned ctrl;
321
322         ctrl = ld_le32(CNTRL_REG(p,ctrl));
323         if (blank_mode > 0)
324                 switch (blank_mode) {
325                 case FB_BLANK_VSYNC_SUSPEND:
326                         ctrl &= ~3;
327                         break;
328                 case FB_BLANK_HSYNC_SUSPEND:
329                         ctrl &= ~0x30;
330                         break;
331                 case FB_BLANK_POWERDOWN:
332                         ctrl &= ~0x33;
333                         /* fall through */
334                 case FB_BLANK_NORMAL:
335                         ctrl |= 0x400;
336                         break;
337                 default:
338                         break;
339                 }
340         else {
341                 ctrl &= ~0x400;
342                 ctrl |= 0x33;
343         }
344         out_le32(CNTRL_REG(p,ctrl), ctrl);
345
346         return 0;
347 }
348
349 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
350                              u_int transp, struct fb_info *info)
351 {
352         struct fb_info_control *p = (struct fb_info_control *) info;
353         __u8 r, g, b;
354
355         if (regno > 255)
356                 return 1;
357
358         r = red >> 8;
359         g = green >> 8;
360         b = blue >> 8;
361
362         out_8(&p->cmap_regs->addr, regno);      /* tell clut what addr to fill  */
363         out_8(&p->cmap_regs->lut, r);           /* send one color channel at    */
364         out_8(&p->cmap_regs->lut, g);           /* a time...                    */
365         out_8(&p->cmap_regs->lut, b);
366
367         if (regno < 16) {
368                 int i;
369                 switch (p->par.cmode) {
370                 case CMODE_16:
371                         p->pseudo_palette[regno] =
372                             (regno << 10) | (regno << 5) | regno;
373                         break;
374                 case CMODE_32:
375                         i = (regno << 8) | regno;
376                         p->pseudo_palette[regno] = (i << 16) | i;
377                         break;
378                 }
379         }
380
381         return 0;
382 }
383
384
385 /********************  End of controlfb_ops implementation  ******************/
386
387
388
389 static void set_control_clock(unsigned char *params)
390 {
391 #ifdef CONFIG_ADB_CUDA
392         struct adb_request req;
393         int i;
394
395         for (i = 0; i < 3; ++i) {
396                 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
397                              0x50, i + 1, params[i]);
398                 while (!req.complete)
399                         cuda_poll();
400         }
401 #endif  
402 }
403
404
405 /*
406  * finish off the driver initialization and register
407  */
408 static int __init init_control(struct fb_info_control *p)
409 {
410         int full, sense, vmode, cmode, vyres;
411         struct fb_var_screeninfo var;
412         int rc;
413         
414         printk(KERN_INFO "controlfb: ");
415
416         full = p->total_vram == 0x400000;
417
418         /* Try to pick a video mode out of NVRAM if we have one. */
419         if (default_cmode == CMODE_NVRAM){
420                 cmode = nvram_read_byte(NV_CMODE);
421                 if(cmode < CMODE_8 || cmode > CMODE_32)
422                         cmode = CMODE_8;
423         } else
424                 cmode=default_cmode;
425
426         if (default_vmode == VMODE_NVRAM) {
427                 vmode = nvram_read_byte(NV_VMODE);
428                 if (vmode < 1 || vmode > VMODE_MAX ||
429                     control_mac_modes[vmode - 1].m[full] < cmode) {
430                         sense = read_control_sense(p);
431                         printk("Monitor sense value = 0x%x, ", sense);
432                         vmode = mac_map_monitor_sense(sense);
433                         if (control_mac_modes[vmode - 1].m[full] < cmode)
434                                 vmode = VMODE_640_480_60;
435                 }
436         } else {
437                 vmode=default_vmode;
438                 if (control_mac_modes[vmode - 1].m[full] < cmode) {
439                         if (cmode > CMODE_8)
440                                 cmode--;
441                         else
442                                 vmode = VMODE_640_480_60;
443                 }
444         }
445
446         /* Initialize info structure */
447         control_init_info(&p->info, p);
448
449         /* Setup default var */
450         if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
451                 /* This shouldn't happen! */
452                 printk("mac_vmode_to_var(%d, %d,) failed\n", vmode, cmode);
453 try_again:
454                 vmode = VMODE_640_480_60;
455                 cmode = CMODE_8;
456                 if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
457                         printk(KERN_ERR "controlfb: mac_vmode_to_var() failed\n");
458                         return -ENXIO;
459                 }
460                 printk(KERN_INFO "controlfb: ");
461         }
462         printk("using video mode %d and color mode %d.\n", vmode, cmode);
463
464         vyres = (p->total_vram - CTRLFB_OFF) / (var.xres << cmode);
465         if (vyres > var.yres)
466                 var.yres_virtual = vyres;
467
468         /* Apply default var */
469         var.activate = FB_ACTIVATE_NOW;
470         rc = fb_set_var(&p->info, &var);
471         if (rc && (vmode != VMODE_640_480_60 || cmode != CMODE_8))
472                 goto try_again;
473
474         /* Register with fbdev layer */
475         if (register_framebuffer(&p->info) < 0)
476                 return -ENXIO;
477         
478         printk(KERN_INFO "fb%d: control display adapter\n", p->info.node);      
479
480         return 0;
481 }
482
483 #define RADACAL_WRITE(a,d) \
484         out_8(&p->cmap_regs->addr, (a)); \
485         out_8(&p->cmap_regs->dat,   (d))
486
487 /* Now how about actually saying, Make it so! */
488 /* Some things in here probably don't need to be done each time. */
489 static void control_set_hardware(struct fb_info_control *p, struct fb_par_control *par)
490 {
491         struct control_regvals  *r;
492         volatile struct preg    __iomem *rp;
493         int                     i, cmode;
494
495         if (PAR_EQUAL(&p->par, par)) {
496                 /*
497                  * check if only xoffset or yoffset differs.
498                  * this prevents flickers in typical VT switch case.
499                  */
500                 if (p->par.xoffset != par->xoffset ||
501                     p->par.yoffset != par->yoffset)
502                         set_screen_start(par->xoffset, par->yoffset, p);
503                         
504                 return;
505         }
506         
507         p->par = *par;
508         cmode = p->par.cmode;
509         r = &par->regvals;
510         
511         /* Turn off display */
512         out_le32(CNTRL_REG(p,ctrl), 0x400 | par->ctrl);
513         
514         set_control_clock(r->clock_params);
515         
516         RADACAL_WRITE(0x20, r->radacal_ctrl);
517         RADACAL_WRITE(0x21, p->control_use_bank2 ? 0 : 1);
518         RADACAL_WRITE(0x10, 0);
519         RADACAL_WRITE(0x11, 0);
520
521         rp = &p->control_regs->vswin;
522         for (i = 0; i < 16; ++i, ++rp)
523                 out_le32(&rp->r, r->regs[i]);
524         
525         out_le32(CNTRL_REG(p,pitch), par->pitch);
526         out_le32(CNTRL_REG(p,mode), r->mode);
527         out_le32(CNTRL_REG(p,vram_attr), p->vram_attr);
528         out_le32(CNTRL_REG(p,start_addr), par->yoffset * par->pitch
529                  + (par->xoffset << cmode));
530         out_le32(CNTRL_REG(p,rfrcnt), 0x1e5);
531         out_le32(CNTRL_REG(p,intr_ena), 0);
532
533         /* Turn on display */
534         out_le32(CNTRL_REG(p,ctrl), par->ctrl);
535
536 #ifdef CONFIG_BOOTX_TEXT
537         btext_update_display(p->frame_buffer_phys + CTRLFB_OFF,
538                              p->par.xres, p->par.yres,
539                              (cmode == CMODE_32? 32: cmode == CMODE_16? 16: 8),
540                              p->par.pitch);
541 #endif /* CONFIG_BOOTX_TEXT */
542 }
543
544
545 /*
546  * Parse user speficied options (`video=controlfb:')
547  */
548 static void __init control_setup(char *options)
549 {
550         char *this_opt;
551
552         if (!options || !*options)
553                 return;
554
555         while ((this_opt = strsep(&options, ",")) != NULL) {
556                 if (!strncmp(this_opt, "vmode:", 6)) {
557                         int vmode = simple_strtoul(this_opt+6, NULL, 0);
558                         if (vmode > 0 && vmode <= VMODE_MAX &&
559                             control_mac_modes[vmode - 1].m[1] >= 0)
560                                 default_vmode = vmode;
561                 } else if (!strncmp(this_opt, "cmode:", 6)) {
562                         int depth = simple_strtoul(this_opt+6, NULL, 0);
563                         switch (depth) {
564                          case CMODE_8:
565                          case CMODE_16:
566                          case CMODE_32:
567                                 default_cmode = depth;
568                                 break;
569                          case 8:
570                                 default_cmode = CMODE_8;
571                                 break;
572                          case 15:
573                          case 16:
574                                 default_cmode = CMODE_16;
575                                 break;
576                          case 24:
577                          case 32:
578                                 default_cmode = CMODE_32;
579                                 break;
580                         }
581                 }
582         }
583 }
584
585 static int __init control_init(void)
586 {
587         struct device_node *dp;
588         char *option = NULL;
589
590         if (fb_get_options("controlfb", &option))
591                 return -ENODEV;
592         control_setup(option);
593
594         dp = find_devices("control");
595         if (dp != 0 && !control_of_init(dp))
596                 return 0;
597
598         return -ENXIO;
599 }
600
601 module_init(control_init);
602
603 /* Work out which banks of VRAM we have installed. */
604 /* danj: I guess the card just ignores writes to nonexistant VRAM... */
605
606 static void __init find_vram_size(struct fb_info_control *p)
607 {
608         int bank1, bank2;
609
610         /*
611          * Set VRAM in 2MB (bank 1) mode
612          * VRAM Bank 2 will be accessible through offset 0x600000 if present
613          * and VRAM Bank 1 will not respond at that offset even if present
614          */
615         out_le32(CNTRL_REG(p,vram_attr), 0x31);
616
617         out_8(&p->frame_buffer[0x600000], 0xb3);
618         out_8(&p->frame_buffer[0x600001], 0x71);
619         asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0x600000])
620                                         : "memory" );
621         mb();
622         asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0x600000])
623                                         : "memory" );
624         mb();
625
626         bank2 = (in_8(&p->frame_buffer[0x600000]) == 0xb3)
627                 && (in_8(&p->frame_buffer[0x600001]) == 0x71);
628
629         /*
630          * Set VRAM in 2MB (bank 2) mode
631          * VRAM Bank 1 will be accessible through offset 0x000000 if present
632          * and VRAM Bank 2 will not respond at that offset even if present
633          */
634         out_le32(CNTRL_REG(p,vram_attr), 0x39);
635
636         out_8(&p->frame_buffer[0], 0x5a);
637         out_8(&p->frame_buffer[1], 0xc7);
638         asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0])
639                                         : "memory" );
640         mb();
641         asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0])
642                                         : "memory" );
643         mb();
644
645         bank1 = (in_8(&p->frame_buffer[0]) == 0x5a)
646                 && (in_8(&p->frame_buffer[1]) == 0xc7);
647
648         if (bank2) {
649                 if (!bank1) {
650                         /*
651                          * vram bank 2 only
652                          */
653                         p->control_use_bank2 = 1;
654                         p->vram_attr = 0x39;
655                         p->frame_buffer += 0x600000;
656                         p->frame_buffer_phys += 0x600000;
657                 } else {
658                         /*
659                          * 4 MB vram
660                          */
661                         p->vram_attr = 0x51;
662                 }
663         } else {
664                 /*
665                  * vram bank 1 only
666                  */
667                 p->vram_attr = 0x31;
668         }
669
670         p->total_vram = (bank1 + bank2) * 0x200000;
671
672         printk(KERN_INFO "controlfb: VRAM Total = %dMB "
673                         "(%dMB @ bank 1, %dMB @ bank 2)\n",
674                         (bank1 + bank2) << 1, bank1 << 1, bank2 << 1);
675 }
676
677
678 /*
679  * find "control" and initialize
680  */
681 static int __init control_of_init(struct device_node *dp)
682 {
683         struct fb_info_control  *p;
684         struct resource         fb_res, reg_res;
685
686         if (control_fb) {
687                 printk(KERN_ERR "controlfb: only one control is supported\n");
688                 return -ENXIO;
689         }
690
691         if (of_pci_address_to_resource(dp, 2, &fb_res) ||
692             of_pci_address_to_resource(dp, 1, &reg_res)) {
693                 printk(KERN_ERR "can't get 2 addresses for control\n");
694                 return -ENXIO;
695         }
696         p = kmalloc(sizeof(*p), GFP_KERNEL);
697         if (p == 0)
698                 return -ENXIO;
699         control_fb = p; /* save it for cleanups */
700         memset(p, 0, sizeof(*p));
701
702         /* Map in frame buffer and registers */
703         p->fb_orig_base = fb_res.start;
704         p->fb_orig_size = fb_res.end - fb_res.start + 1;
705         /* use the big-endian aperture (??) */
706         p->frame_buffer_phys = fb_res.start + 0x800000;
707         p->control_regs_phys = reg_res.start;
708         p->control_regs_size = reg_res.end - reg_res.start + 1;
709
710         if (!p->fb_orig_base ||
711             !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
712                 p->fb_orig_base = 0;
713                 goto error_out;
714         }
715         /* map at most 8MB for the frame buffer */
716         p->frame_buffer = __ioremap(p->frame_buffer_phys, 0x800000,
717                                     _PAGE_WRITETHRU);
718
719         if (!p->control_regs_phys ||
720             !request_mem_region(p->control_regs_phys, p->control_regs_size,
721             "controlfb regs")) {
722                 p->control_regs_phys = 0;
723                 goto error_out;
724         }
725         p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
726
727         p->cmap_regs_phys = 0xf301b000;  /* XXX not in prom? */
728         if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
729                 p->cmap_regs_phys = 0;
730                 goto error_out;
731         }
732         p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
733
734         if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
735                 goto error_out;
736
737         find_vram_size(p);
738         if (!p->total_vram)
739                 goto error_out;
740
741         if (init_control(p) < 0)
742                 goto error_out;
743
744         return 0;
745
746 error_out:
747         control_cleanup();
748         return -ENXIO;
749 }
750
751 /*
752  * Get the monitor sense value.
753  * Note that this can be called before calibrate_delay,
754  * so we can't use udelay.
755  */
756 static int read_control_sense(struct fb_info_control *p)
757 {
758         int sense;
759
760         out_le32(CNTRL_REG(p,mon_sense), 7);    /* drive all lines high */
761         __delay(200);
762         out_le32(CNTRL_REG(p,mon_sense), 077);  /* turn off drivers */
763         __delay(2000);
764         sense = (in_le32(CNTRL_REG(p,mon_sense)) & 0x1c0) << 2;
765
766         /* drive each sense line low in turn and collect the other 2 */
767         out_le32(CNTRL_REG(p,mon_sense), 033);  /* drive A low */
768         __delay(2000);
769         sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0xc0) >> 2;
770         out_le32(CNTRL_REG(p,mon_sense), 055);  /* drive B low */
771         __delay(2000);
772         sense |= ((in_le32(CNTRL_REG(p,mon_sense)) & 0x100) >> 5)
773                 | ((in_le32(CNTRL_REG(p,mon_sense)) & 0x40) >> 4);
774         out_le32(CNTRL_REG(p,mon_sense), 066);  /* drive C low */
775         __delay(2000);
776         sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0x180) >> 7;
777
778         out_le32(CNTRL_REG(p,mon_sense), 077);  /* turn off drivers */
779         
780         return sense;
781 }
782
783 /**********************  Various translation functions  **********************/
784
785 #define CONTROL_PIXCLOCK_BASE   256016
786 #define CONTROL_PIXCLOCK_MIN    5000    /* ~ 200 MHz dot clock */
787
788 /*
789  * calculate the clock paramaters to be sent to CUDA according to given
790  * pixclock in pico second.
791  */
792 static int calc_clock_params(unsigned long clk, unsigned char *param)
793 {
794         unsigned long p0, p1, p2, k, l, m, n, min;
795
796         if (clk > (CONTROL_PIXCLOCK_BASE << 3))
797                 return 1;
798
799         p2 = ((clk << 4) < CONTROL_PIXCLOCK_BASE)? 3: 2;
800         l = clk << p2;
801         p0 = 0;
802         p1 = 0;
803         for (k = 1, min = l; k < 32; k++) {
804                 unsigned long rem;
805
806                 m = CONTROL_PIXCLOCK_BASE * k;
807                 n = m / l;
808                 rem = m % l;
809                 if (n && (n < 128) && rem < min) {
810                         p0 = k;
811                         p1 = n;
812                         min = rem;
813                 }
814         }
815         if (!p0 || !p1)
816                 return 1;
817
818         param[0] = p0;
819         param[1] = p1;
820         param[2] = p2;
821
822         return 0;
823 }
824
825
826 /*
827  * This routine takes a user-supplied var, and picks the best vmode/cmode
828  * from it.
829  */
830
831 static int control_var_to_par(struct fb_var_screeninfo *var,
832         struct fb_par_control *par, const struct fb_info *fb_info)
833 {
834         int cmode, piped_diff, hstep;
835         unsigned hperiod, hssync, hsblank, hesync, heblank, piped, heq, hlfln,
836                  hserr, vperiod, vssync, vesync, veblank, vsblank, vswin, vewin;
837         unsigned long pixclock;
838         struct fb_info_control *p = (struct fb_info_control *) fb_info;
839         struct control_regvals *r = &par->regvals;
840
841         switch (var->bits_per_pixel) {
842         case 8:
843                 par->cmode = CMODE_8;
844                 if (p->total_vram > 0x200000) {
845                         r->mode = 3;
846                         r->radacal_ctrl = 0x20;
847                         piped_diff = 13;
848                 } else {
849                         r->mode = 2;
850                         r->radacal_ctrl = 0x10;
851                         piped_diff = 9;
852                 }
853                 break;
854         case 15:
855         case 16:
856                 par->cmode = CMODE_16;
857                 if (p->total_vram > 0x200000) {
858                         r->mode = 2;
859                         r->radacal_ctrl = 0x24;
860                         piped_diff = 5;
861                 } else {
862                         r->mode = 1;
863                         r->radacal_ctrl = 0x14;
864                         piped_diff = 3;
865                 }
866                 break;
867         case 32:
868                 par->cmode = CMODE_32;
869                 if (p->total_vram > 0x200000) {
870                         r->mode = 1;
871                         r->radacal_ctrl = 0x28;
872                 } else {
873                         r->mode = 0;
874                         r->radacal_ctrl = 0x18;
875                 }
876                 piped_diff = 1;
877                 break;
878         default:
879                 return -EINVAL;
880         }
881
882         /*
883          * adjust xres and vxres so that the corresponding memory widths are
884          * 32-byte aligned
885          */
886         hstep = 31 >> par->cmode;
887         par->xres = (var->xres + hstep) & ~hstep;
888         par->vxres = (var->xres_virtual + hstep) & ~hstep;
889         par->xoffset = (var->xoffset + hstep) & ~hstep;
890         if (par->vxres < par->xres)
891                 par->vxres = par->xres;
892         par->pitch = par->vxres << par->cmode;
893
894         par->yres = var->yres;
895         par->vyres = var->yres_virtual;
896         par->yoffset = var->yoffset;
897         if (par->vyres < par->yres)
898                 par->vyres = par->yres;
899
900         par->sync = var->sync;
901
902         if (par->pitch * par->vyres + CTRLFB_OFF > p->total_vram)
903                 return -EINVAL;
904
905         if (par->xoffset + par->xres > par->vxres)
906                 par->xoffset = par->vxres - par->xres;
907         if (par->yoffset + par->yres > par->vyres)
908                 par->yoffset = par->vyres - par->yres;
909
910         pixclock = (var->pixclock < CONTROL_PIXCLOCK_MIN)? CONTROL_PIXCLOCK_MIN:
911                    var->pixclock;
912         if (calc_clock_params(pixclock, r->clock_params))
913                 return -EINVAL;
914
915         hperiod = ((var->left_margin + par->xres + var->right_margin
916                     + var->hsync_len) >> 1) - 2;
917         hssync = hperiod + 1;
918         hsblank = hssync - (var->right_margin >> 1);
919         hesync = (var->hsync_len >> 1) - 1;
920         heblank = (var->left_margin >> 1) + hesync;
921         piped = heblank - piped_diff;
922         heq = var->hsync_len >> 2;
923         hlfln = (hperiod+2) >> 1;
924         hserr = hssync-hesync;
925         vperiod = (var->vsync_len + var->lower_margin + par->yres
926                    + var->upper_margin) << 1;
927         vssync = vperiod - 2;
928         vesync = (var->vsync_len << 1) - vperiod + vssync;
929         veblank = (var->upper_margin << 1) + vesync;
930         vsblank = vssync - (var->lower_margin << 1);
931         vswin = (vsblank+vssync) >> 1;
932         vewin = (vesync+veblank) >> 1;
933
934         r->regs[0] = vswin;
935         r->regs[1] = vsblank;
936         r->regs[2] = veblank;
937         r->regs[3] = vewin;
938         r->regs[4] = vesync;
939         r->regs[5] = vssync;
940         r->regs[6] = vperiod;
941         r->regs[7] = piped;
942         r->regs[8] = hperiod;
943         r->regs[9] = hsblank;
944         r->regs[10] = heblank;
945         r->regs[11] = hesync;
946         r->regs[12] = hssync;
947         r->regs[13] = heq;
948         r->regs[14] = hlfln;
949         r->regs[15] = hserr;
950
951         if (par->xres >= 1280 && par->cmode >= CMODE_16)
952                 par->ctrl = 0x7f;
953         else
954                 par->ctrl = 0x3b;
955
956         if (mac_var_to_vmode(var, &par->vmode, &cmode))
957                 par->vmode = 0;
958
959         return 0;
960 }
961
962
963 /*
964  * Convert hardware data in par to an fb_var_screeninfo
965  */
966
967 static void control_par_to_var(struct fb_par_control *par, struct fb_var_screeninfo *var)
968 {
969         struct control_regints *rv;
970         
971         rv = (struct control_regints *) par->regvals.regs;
972         
973         memset(var, 0, sizeof(*var));
974         var->xres = par->xres;
975         var->yres = par->yres;
976         var->xres_virtual = par->vxres;
977         var->yres_virtual = par->vyres;
978         var->xoffset = par->xoffset;
979         var->yoffset = par->yoffset;
980         
981         switch(par->cmode) {
982         default:
983         case CMODE_8:
984                 var->bits_per_pixel = 8;
985                 var->red.length = 8;
986                 var->green.length = 8;
987                 var->blue.length = 8;
988                 break;
989         case CMODE_16:  /* RGB 555 */
990                 var->bits_per_pixel = 16;
991                 var->red.offset = 10;
992                 var->red.length = 5;
993                 var->green.offset = 5;
994                 var->green.length = 5;
995                 var->blue.length = 5;
996                 break;
997         case CMODE_32:  /* RGB 888 */
998                 var->bits_per_pixel = 32;
999                 var->red.offset = 16;
1000                 var->red.length = 8;
1001                 var->green.offset = 8;
1002                 var->green.length = 8;
1003                 var->blue.length = 8;
1004                 var->transp.offset = 24;
1005                 var->transp.length = 8;
1006                 break;
1007         }
1008         var->height = -1;
1009         var->width = -1;
1010         var->vmode = FB_VMODE_NONINTERLACED;
1011
1012         var->left_margin = (rv->heblank - rv->hesync) << 1;
1013         var->right_margin = (rv->hssync - rv->hsblank) << 1;
1014         var->hsync_len = (rv->hperiod + 2 - rv->hssync + rv->hesync) << 1;
1015
1016         var->upper_margin = (rv->veblank - rv->vesync) >> 1;
1017         var->lower_margin = (rv->vssync - rv->vsblank) >> 1;
1018         var->vsync_len = (rv->vperiod - rv->vssync + rv->vesync) >> 1;
1019
1020         var->sync = par->sync;
1021
1022         /*
1023          * 10^12 * clock_params[0] / (3906400 * clock_params[1]
1024          *                            * 2^clock_params[2])
1025          * (10^12 * clock_params[0] / (3906400 * clock_params[1]))
1026          * >> clock_params[2]
1027          */
1028         /* (255990.17 * clock_params[0] / clock_params[1]) >> clock_params[2] */
1029         var->pixclock = CONTROL_PIXCLOCK_BASE * par->regvals.clock_params[0];
1030         var->pixclock /= par->regvals.clock_params[1];
1031         var->pixclock >>= par->regvals.clock_params[2];
1032 }
1033
1034 /*
1035  * Set misc info vars for this driver
1036  */
1037 static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
1038 {
1039         /* Fill fb_info */
1040         info->par = &p->par;
1041         info->fbops = &controlfb_ops;
1042         info->pseudo_palette = p->pseudo_palette;
1043         info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1044         info->screen_base = p->frame_buffer + CTRLFB_OFF;
1045
1046         fb_alloc_cmap(&info->cmap, 256, 0);
1047
1048         /* Fill fix common fields */
1049         strcpy(info->fix.id, "control");
1050         info->fix.mmio_start = p->control_regs_phys;
1051         info->fix.mmio_len = sizeof(struct control_regs);
1052         info->fix.type = FB_TYPE_PACKED_PIXELS;
1053         info->fix.smem_start = p->frame_buffer_phys + CTRLFB_OFF;
1054         info->fix.smem_len = p->total_vram - CTRLFB_OFF;
1055         info->fix.ywrapstep = 0;
1056         info->fix.type_aux = 0;
1057         info->fix.accel = FB_ACCEL_NONE;
1058 }
1059
1060
1061 static void control_cleanup(void)
1062 {
1063         struct fb_info_control  *p = control_fb;
1064
1065         if (!p)
1066                 return;
1067
1068         if (p->cmap_regs)
1069                 iounmap(p->cmap_regs);
1070         if (p->control_regs)
1071                 iounmap(p->control_regs);
1072         if (p->frame_buffer) {
1073                 if (p->control_use_bank2)
1074                         p->frame_buffer -= 0x600000;
1075                 iounmap(p->frame_buffer);
1076         }
1077         if (p->cmap_regs_phys)
1078                 release_mem_region(p->cmap_regs_phys, 0x1000);
1079         if (p->control_regs_phys)
1080                 release_mem_region(p->control_regs_phys, p->control_regs_size);
1081         if (p->fb_orig_base)
1082                 release_mem_region(p->fb_orig_base, p->fb_orig_size);
1083         kfree(p);
1084 }
1085
1086