2 * controlfb.c -- frame buffer device for the PowerMac 'control' display
4 * Created 12 July 1998 by Dan Jacobowitz <dan@debian.org>
5 * Copyright (C) 1998 Dan Jacobowitz
6 * Copyright (C) 2001 Takashi Oe
8 * Mmap code by Michel Lanners <mlan@cpu.lu>
10 * Frame buffer structure from:
11 * drivers/video/chipsfb.c -- frame buffer device for
12 * Chips & Technologies 65550 chip.
14 * Copyright (C) 1998 Paul Mackerras
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.
21 * Hardware information from:
22 * control.c: Console support for PowerMac "control" display adaptor.
23 * Copyright (C) 1996 Paul Mackerras
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
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>
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>
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>
49 #include <linux/adb.h>
50 #include <linux/cuda.h>
53 #include <asm/pgtable.h>
54 #include <asm/btext.h>
56 #include <video/fbcon.h>
57 #include <video/fbcon-cfb8.h>
58 #include <video/fbcon-cfb16.h>
59 #include <video/fbcon-cfb32.h>
60 #include <video/macmodes.h>
62 #include "controlfb.h"
64 struct fb_par_control {
70 struct control_regvals regvals;
75 #define DIRTY(z) ((x)->z != (y)->z)
76 #define DIRTY_CMAP(z) (memcmp(&((x)->z), &((y)->z), sizeof((y)->z)))
77 static inline int PAR_EQUAL(struct fb_par_control *x, struct fb_par_control *y)
82 for (i = 0; i < 3; i++)
83 results &= !DIRTY(regvals.clock_params[i]);
86 for (i = 0; i < 16; i++)
87 results &= !DIRTY(regvals.regs[i]);
90 return (!DIRTY(cmode) && !DIRTY(xres) && !DIRTY(yres)
91 && !DIRTY(vxres) && !DIRTY(vyres));
93 static inline int VAR_MATCH(struct fb_var_screeninfo *x, struct fb_var_screeninfo *y)
95 return (!DIRTY(bits_per_pixel) && !DIRTY(xres)
96 && !DIRTY(yres) && !DIRTY(xres_virtual)
97 && !DIRTY(yres_virtual)
98 && !DIRTY_CMAP(red) && !DIRTY_CMAP(green) && !DIRTY_CMAP(blue));
101 struct fb_info_control {
103 struct display display;
104 struct fb_par_control par;
106 __u8 red, green, blue;
109 struct cmap_regs *cmap_regs;
110 unsigned long cmap_regs_phys;
112 struct control_regs *control_regs;
113 unsigned long control_regs_phys;
114 unsigned long control_regs_size;
117 unsigned long frame_buffer_phys;
118 unsigned long fb_orig_base;
119 unsigned long fb_orig_size;
121 int control_use_bank2;
122 unsigned long total_vram;
123 unsigned char vram_attr;
125 #ifdef FBCON_HAS_CFB16
128 #ifdef FBCON_HAS_CFB32
134 /* control register access macro */
135 #define CNTRL_REG(INFO,REG) (&(((INFO)->control_regs->REG).r))
138 /******************** Prototypes for exported functions ********************/
142 static int control_get_fix(struct fb_fix_screeninfo *fix, int con,
143 struct fb_info *info);
144 static int control_get_var(struct fb_var_screeninfo *var, int con,
145 struct fb_info *info);
146 static int control_set_var(struct fb_var_screeninfo *var, int con,
147 struct fb_info *info);
148 static int control_pan_display(struct fb_var_screeninfo *var, int con,
149 struct fb_info *info);
150 static int control_get_cmap(struct fb_cmap *cmap, int kspc, int con,
151 struct fb_info *info);
152 static int control_set_cmap(struct fb_cmap *cmap, int kspc, int con,
153 struct fb_info *info);
154 static int control_mmap(struct fb_info *info, struct file *file,
155 struct vm_area_struct *vma);
158 * low level fbcon ops
160 static int controlfb_switch(int con, struct fb_info *info);
161 static int controlfb_updatevar(int con, struct fb_info *info);
162 static void controlfb_blank(int blank_mode, struct fb_info *info);
165 * low level cmap set/get ops
167 static int controlfb_getcolreg(u_int regno, u_int *red, u_int *green,
168 u_int *blue, u_int *transp, struct fb_info *info);
169 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
170 u_int transp, struct fb_info *info);
175 int control_init(void);
176 void control_setup(char *);
179 * low level fbcon revc ops
181 static void control_cfb16_revc(struct display *p, int xx, int yy);
182 static void control_cfb32_revc(struct display *p, int xx, int yy);
185 /******************** Prototypes for internal functions **********************/
187 static void do_install_cmap(struct display *disp, struct fb_info *info);
188 static void set_control_clock(unsigned char *params);
189 static int init_control(struct fb_info_control *p);
190 static void control_set_hardware(struct fb_info_control *p,
191 struct fb_par_control *par);
192 static int control_of_init(struct device_node *dp);
193 static void control_par_to_fix(struct fb_par_control *par,
194 struct fb_fix_screeninfo *fix, struct fb_info_control *p);
195 static void control_set_dispsw(struct display *disp, int cmode,
196 struct fb_info_control *p);
197 static void find_vram_size(struct fb_info_control *p);
198 static int read_control_sense(struct fb_info_control *p);
199 static int calc_clock_params(unsigned long clk, unsigned char *param);
200 static int control_var_to_par(struct fb_var_screeninfo *var,
201 struct fb_par_control *par, const struct fb_info *fb_info);
202 static inline void control_par_to_var(struct fb_par_control *par,
203 struct fb_var_screeninfo *var);
204 static void control_par_to_fix(struct fb_par_control *par,
205 struct fb_fix_screeninfo *fix, struct fb_info_control *p);
206 static void control_par_to_display(struct fb_par_control *par,
207 struct display *disp, struct fb_fix_screeninfo *fix,
208 struct fb_info_control *p);
209 static void control_set_dispsw(struct display *disp, int cmode,
210 struct fb_info_control *p);
211 static void control_init_info(struct fb_info *info, struct fb_info_control *p);
212 static void control_cleanup(void);
215 /************************** Internal variables *******************************/
218 static struct fb_info_control *control_fb;
220 static char fontname[40] __initdata = { 0 };
221 static int default_vmode __initdata = VMODE_NVRAM;
222 static int default_cmode __initdata = CMODE_NVRAM;
225 static struct fb_ops controlfb_ops = {
227 fb_get_fix: control_get_fix,
228 fb_get_var: control_get_var,
229 fb_set_var: control_set_var,
230 fb_get_cmap: control_get_cmap,
231 fb_set_cmap: control_set_cmap,
232 fb_pan_display: control_pan_display,
233 fb_mmap: control_mmap,
237 /******************** The functions for controlfb_ops ********************/
240 MODULE_LICENSE("GPL");
242 int init_module(void)
244 struct device_node *dp;
246 dp = find_devices("control");
247 if (dp != 0 && !control_of_init(dp))
253 void cleanup_module(void)
259 /*********** Providing our information to the user ************/
261 static int control_get_fix(struct fb_fix_screeninfo *fix, int con,
262 struct fb_info *info)
264 struct fb_info_control *p = (struct fb_info_control *) info;
267 control_par_to_fix(&p->par, fix, p);
269 struct fb_par_control par;
271 control_var_to_par(&fb_display[con].var, &par, info);
272 control_par_to_fix(&par, fix, p);
277 static int control_get_var(struct fb_var_screeninfo *var, int con,
278 struct fb_info *info)
280 struct fb_info_control *p = (struct fb_info_control *) info;
283 control_par_to_var(&p->par, var);
285 *var = fb_display[con].var;
292 * Sets everything according to var
294 static int control_set_var(struct fb_var_screeninfo *var, int con,
295 struct fb_info *info)
297 struct fb_info_control *p = (struct fb_info_control *) info;
298 struct display *disp;
299 struct fb_par_control par;
300 int depthchange, err;
301 int activate = var->activate;
303 if((err = control_var_to_par(var, &par, info))) {
305 printk (KERN_ERR "control_set_var: error calling"
306 " control_var_to_par: %d.\n", err);
310 control_par_to_var(&par, var);
312 if ((activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW)
315 disp = (con >= 0) ? &fb_display[con] : info->disp;
317 depthchange = (disp->var.bits_per_pixel != var->bits_per_pixel);
318 if(!VAR_MATCH(&disp->var, var)) {
319 struct fb_fix_screeninfo fix;
320 control_par_to_fix(&par, &fix, p);
321 control_par_to_display(&par, disp, &fix, p);
323 (*info->changevar)(con);
328 control_set_hardware(p, &par);
330 if((err = fb_alloc_cmap(&disp->cmap, 0, 0)))
332 do_install_cmap(disp, info);
341 * Set screen start address according to var offset values
343 static inline void set_screen_start(int xoffset, int yoffset,
344 struct fb_info_control *p)
346 struct fb_par_control *par = &p->par;
348 par->xoffset = xoffset;
349 par->yoffset = yoffset;
350 out_le32(CNTRL_REG(p,start_addr),
351 par->yoffset * par->pitch + (par->xoffset << par->cmode));
355 static int control_pan_display(struct fb_var_screeninfo *var, int con,
356 struct fb_info *info)
358 unsigned int xoffset, hstep;
359 struct fb_info_control *p = (struct fb_info_control *)info;
360 struct fb_par_control *par = &p->par;
363 * make sure start addr will be 32-byte aligned
365 hstep = 0x1f >> par->cmode;
366 xoffset = (var->xoffset + hstep) & ~hstep;
368 if (xoffset+par->xres > par->vxres ||
369 var->yoffset+par->yres > par->vyres)
372 set_screen_start(xoffset, var->yoffset, p);
377 static int control_get_cmap(struct fb_cmap *cmap, int kspc, int con,
378 struct fb_info *info)
380 if (con == currcon) /* current console? */
381 return fb_get_cmap(cmap, kspc, controlfb_getcolreg, info);
382 if (fb_display[con].cmap.len) /* non default colormap? */
383 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0: 2);
385 int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
386 fb_copy_cmap(fb_default_cmap(size), cmap, kspc ? 0 : 2);
391 static int control_set_cmap(struct fb_cmap *cmap, int kspc, int con,
392 struct fb_info *info)
394 struct display *disp = (con < 0)? info->disp: &fb_display[con];
395 int err, size = disp->var.bits_per_pixel == 16 ? 32 : 256;
397 if (disp->cmap.len != size) {
398 err = fb_alloc_cmap(&disp->cmap, size, 0);
403 return fb_set_cmap(cmap, kspc, controlfb_setcolreg, info);
404 fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
410 * Private mmap since we want to have a different caching on the framebuffer
412 * Note there's no locking in here; it's done in fb_mmap() in fbmem.c.
414 static int control_mmap(struct fb_info *info, struct file *file,
415 struct vm_area_struct *vma)
417 struct fb_ops *fb = info->fbops;
418 struct fb_fix_screeninfo fix;
419 struct fb_var_screeninfo var;
420 unsigned long off, start;
423 fb->fb_get_fix(&fix, PROC_CONSOLE(info), info);
424 off = vma->vm_pgoff << PAGE_SHIFT;
426 /* frame buffer memory */
427 start = fix.smem_start;
428 len = PAGE_ALIGN((start & ~PAGE_MASK)+fix.smem_len);
430 /* memory mapped io */
432 fb->fb_get_var(&var, PROC_CONSOLE(info), info);
435 start = fix.mmio_start;
436 len = PAGE_ALIGN((start & ~PAGE_MASK)+fix.mmio_len);
437 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
440 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
443 if ((vma->vm_end - vma->vm_start + off) > len)
446 vma->vm_pgoff = off >> PAGE_SHIFT;
447 if (io_remap_page_range(vma->vm_start, off,
448 vma->vm_end - vma->vm_start, vma->vm_page_prot))
455 /******************** End of controlfb_ops implementation ******************/
458 * low level fbcon ops
461 static int controlfb_switch(int con, struct fb_info *info)
463 struct fb_info_control *p = (struct fb_info_control *)info;
464 struct fb_par_control par;
466 if (currcon >= 0 && fb_display[currcon].cmap.len)
467 fb_get_cmap(&fb_display[currcon].cmap, 1, controlfb_getcolreg,
471 fb_display[con].var.activate = FB_ACTIVATE_NOW;
472 control_var_to_par(&fb_display[con].var, &par, info);
473 control_set_hardware(p, &par);
474 control_set_dispsw(&fb_display[con], par.cmode, p);
475 do_install_cmap(&fb_display[con], info);
481 static int controlfb_updatevar(int con, struct fb_info *info)
483 struct fb_var_screeninfo *var = &fb_display[con].var;
484 struct fb_info_control *p = (struct fb_info_control *) info;
486 set_screen_start(var->xoffset, var->yoffset, p);
492 static void controlfb_blank(int blank_mode, struct fb_info *info)
494 struct fb_info_control *p = (struct fb_info_control *) info;
497 ctrl = ld_le32(CNTRL_REG(p,ctrl));
499 switch (blank_mode - 1) {
500 case VESA_VSYNC_SUSPEND:
503 case VESA_HSYNC_SUSPEND:
509 case VESA_NO_BLANKING:
519 out_le32(CNTRL_REG(p,ctrl), ctrl);
526 * low level cmap set/get ops
529 static int controlfb_getcolreg(u_int regno, u_int *red, u_int *green,
530 u_int *blue, u_int *transp, struct fb_info *info)
532 struct fb_info_control *p = (struct fb_info_control *) info;
536 *red = (p->palette[regno].red<<8) | p->palette[regno].red;
537 *green = (p->palette[regno].green<<8) | p->palette[regno].green;
538 *blue = (p->palette[regno].blue<<8) | p->palette[regno].blue;
543 static int controlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
544 u_int transp, struct fb_info *info)
546 struct fb_info_control *p = (struct fb_info_control *) info;
557 p->palette[regno].red = r;
558 p->palette[regno].green = g;
559 p->palette[regno].blue = b;
561 out_8(&p->cmap_regs->addr, regno); /* tell clut what addr to fill */
562 out_8(&p->cmap_regs->lut, r); /* send one color channel at */
563 out_8(&p->cmap_regs->lut, g); /* a time... */
564 out_8(&p->cmap_regs->lut, b);
567 switch (p->par.cmode) {
568 #ifdef FBCON_HAS_CFB16
570 p->fbcon_cmap.cfb16[regno] = (regno << 10) | (regno << 5) | regno;
573 #ifdef FBCON_HAS_CFB32
575 i = (regno << 8) | regno;
576 p->fbcon_cmap.cfb32[regno] = (i << 16) | i;
583 static void do_install_cmap(struct display *disp, struct fb_info *info)
586 fb_set_cmap(&disp->cmap, 1, controlfb_setcolreg,
589 int size = disp->var.bits_per_pixel == 16 ? 32 : 256;
590 fb_set_cmap(fb_default_cmap(size), 1, controlfb_setcolreg,
596 static void set_control_clock(unsigned char *params)
598 #ifdef CONFIG_ADB_CUDA
599 struct adb_request req;
602 for (i = 0; i < 3; ++i) {
603 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
604 0x50, i + 1, params[i]);
605 while (!req.complete)
613 * finish off the driver initialization and register
615 static int __init init_control(struct fb_info_control *p)
617 int full, sense, vmode, cmode, vyres;
618 struct fb_var_screeninfo var;
620 printk(KERN_INFO "controlfb: ");
622 full = p->total_vram == 0x400000;
625 /* Try to pick a video mode out of NVRAM if we have one. */
626 if (default_cmode == CMODE_NVRAM)
627 cmode = nvram_read_byte(NV_CMODE);
628 if (default_vmode == VMODE_NVRAM) {
629 vmode = nvram_read_byte(NV_VMODE);
630 if (vmode < 1 || vmode > VMODE_MAX ||
631 control_mac_modes[vmode - 1].m[full] < cmode) {
632 sense = read_control_sense(p);
633 printk("Monitor sense value = 0x%x, ", sense);
634 vmode = mac_map_monitor_sense(sense);
635 if (control_mac_modes[vmode - 1].m[full] < cmode)
636 vmode = VMODE_640_480_60;
641 /* If we didn't get something from NVRAM, pick a
644 if (vmode <= 0 || vmode > VMODE_MAX)
645 vmode = VMODE_640_480_67;
646 if (cmode < CMODE_8 || cmode > CMODE_32)
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);
653 vmode = VMODE_640_480_60;
655 if (mac_vmode_to_var(vmode, cmode, &var) < 0) {
656 printk(KERN_ERR "controlfb: mac_vmode_to_var() failed\n");
659 printk(KERN_INFO "controlfb: ");
661 printk("using video mode %d and color mode %d.\n", vmode, cmode);
663 vyres = (p->total_vram - CTRLFB_OFF) / (var.xres << cmode);
664 if (vyres > var.yres)
665 var.yres_virtual = vyres;
667 control_init_info(&p->info, p);
669 var.activate = FB_ACTIVATE_NOW;
671 if (control_set_var(&var, -1, &p->info) < 0) {
672 if (vmode != VMODE_640_480_60 || cmode != CMODE_8)
674 printk(KERN_ERR "controlfb: initilization failed\n");
678 p->info.flags = FBINFO_FLAG_DEFAULT;
679 if (register_framebuffer(&p->info) < 0)
682 printk(KERN_INFO "fb%d: control display adapter\n", GET_FB_IDX(p->info.node));
687 #define RADACAL_WRITE(a,d) \
688 out_8(&p->cmap_regs->addr, (a)); \
689 out_8(&p->cmap_regs->dat, (d))
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)
695 struct control_regvals *r;
696 volatile struct preg *rp;
699 if (PAR_EQUAL(&p->par, par)) {
701 * check if only xoffset or yoffset differs.
702 * this prevents flickers in typical VT switch case.
704 if (p->par.xoffset != par->xoffset ||
705 p->par.yoffset != par->yoffset)
706 set_screen_start(par->xoffset, par->yoffset, p);
712 cmode = p->par.cmode;
715 /* Turn off display */
716 out_le32(CNTRL_REG(p,ctrl), 0x400 | par->ctrl);
718 set_control_clock(r->clock_params);
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);
725 rp = &p->control_regs->vswin;
726 for (i = 0; i < 16; ++i, ++rp)
727 out_le32(&rp->r, r->regs[i]);
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);
737 /* Turn on display */
738 out_le32(CNTRL_REG(p,ctrl), par->ctrl);
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;
757 #endif /* CONFIG_FB_COMPAT_XPMAC */
758 #ifdef CONFIG_BOOTX_TEXT
759 btext_update_display(p->frame_buffer_phys + CTRLFB_OFF,
760 p->par.xres, p->par.yres,
761 (cmode == CMODE_32? 32: cmode == CMODE_16? 16: 8),
763 #endif /* CONFIG_BOOTX_TEXT */
768 * Called from fbmem.c for probing & intializing
770 int __init control_init(void)
772 struct device_node *dp;
774 dp = find_devices("control");
775 if (dp != 0 && !control_of_init(dp))
782 /* Work out which banks of VRAM we have installed. */
783 /* danj: I guess the card just ignores writes to nonexistant VRAM... */
785 static void __init find_vram_size(struct fb_info_control *p)
790 * Set VRAM in 2MB (bank 1) mode
791 * VRAM Bank 2 will be accessible through offset 0x600000 if present
792 * and VRAM Bank 1 will not respond at that offset even if present
794 out_le32(CNTRL_REG(p,vram_attr), 0x31);
796 out_8(&p->frame_buffer[0x600000], 0xb3);
797 out_8(&p->frame_buffer[0x600001], 0x71);
798 asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0x600000])
801 asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0x600000])
805 bank2 = (in_8(&p->frame_buffer[0x600000]) == 0xb3)
806 && (in_8(&p->frame_buffer[0x600001]) == 0x71);
809 * Set VRAM in 2MB (bank 2) mode
810 * VRAM Bank 1 will be accessible through offset 0x000000 if present
811 * and VRAM Bank 2 will not respond at that offset even if present
813 out_le32(CNTRL_REG(p,vram_attr), 0x39);
815 out_8(&p->frame_buffer[0], 0x5a);
816 out_8(&p->frame_buffer[1], 0xc7);
817 asm volatile("eieio; dcbf 0,%0" : : "r" (&p->frame_buffer[0])
820 asm volatile("eieio; dcbi 0,%0" : : "r" (&p->frame_buffer[0])
824 bank1 = (in_8(&p->frame_buffer[0]) == 0x5a)
825 && (in_8(&p->frame_buffer[1]) == 0xc7);
832 p->control_use_bank2 = 1;
834 p->frame_buffer += 0x600000;
835 p->frame_buffer_phys += 0x600000;
849 p->total_vram = (bank1 + bank2) * 0x200000;
851 printk(KERN_INFO "controlfb: VRAM Total = %dMB "
852 "(%dMB @ bank 1, %dMB @ bank 2)\n",
853 (bank1 + bank2) << 1, bank1 << 1, bank2 << 1);
858 * find "control" and initialize
860 static int __init control_of_init(struct device_node *dp)
862 struct fb_info_control *p;
867 printk(KERN_ERR "controlfb: only one control is supported\n");
870 if(dp->n_addrs != 2) {
871 printk(KERN_ERR "expecting 2 address for control (got %d)", dp->n_addrs);
874 p = kmalloc(sizeof(*p), GFP_ATOMIC);
877 control_fb = p; /* save it for cleanups */
878 memset(p, 0, sizeof(*p));
880 /* Map in frame buffer and registers */
881 for (i = 0; i < dp->n_addrs; ++i) {
882 addr = dp->addrs[i].address;
883 if (dp->addrs[i].size >= 0x800000) {
884 p->fb_orig_base = addr;
885 p->fb_orig_size = dp->addrs[i].size;
886 /* use the big-endian aperture (??) */
887 p->frame_buffer_phys = addr + 0x800000;
889 p->control_regs_phys = addr;
890 p->control_regs_size = dp->addrs[i].size;
894 if (!p->fb_orig_base ||
895 !request_mem_region(p->fb_orig_base,p->fb_orig_size,"controlfb")) {
899 /* map at most 8MB for the frame buffer */
900 p->frame_buffer = __ioremap(p->frame_buffer_phys, 0x800000,
903 if (!p->control_regs_phys ||
904 !request_mem_region(p->control_regs_phys, p->control_regs_size,
906 p->control_regs_phys = 0;
909 p->control_regs = ioremap(p->control_regs_phys, p->control_regs_size);
911 p->cmap_regs_phys = 0xf301b000; /* XXX not in prom? */
912 if (!request_mem_region(p->cmap_regs_phys, 0x1000, "controlfb cmap")) {
913 p->cmap_regs_phys = 0;
916 p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
918 if (!p->cmap_regs || !p->control_regs || !p->frame_buffer)
925 if (init_control(p) < 0)
936 * Get the monitor sense value.
937 * Note that this can be called before calibrate_delay,
938 * so we can't use udelay.
940 static int read_control_sense(struct fb_info_control *p)
944 out_le32(CNTRL_REG(p,mon_sense), 7); /* drive all lines high */
946 out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
948 sense = (in_le32(CNTRL_REG(p,mon_sense)) & 0x1c0) << 2;
950 /* drive each sense line low in turn and collect the other 2 */
951 out_le32(CNTRL_REG(p,mon_sense), 033); /* drive A low */
953 sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0xc0) >> 2;
954 out_le32(CNTRL_REG(p,mon_sense), 055); /* drive B low */
956 sense |= ((in_le32(CNTRL_REG(p,mon_sense)) & 0x100) >> 5)
957 | ((in_le32(CNTRL_REG(p,mon_sense)) & 0x40) >> 4);
958 out_le32(CNTRL_REG(p,mon_sense), 066); /* drive C low */
960 sense |= (in_le32(CNTRL_REG(p,mon_sense)) & 0x180) >> 7;
962 out_le32(CNTRL_REG(p,mon_sense), 077); /* turn off drivers */
967 /********************** Various translation functions **********************/
969 #define CONTROL_PIXCLOCK_BASE 256016
970 #define CONTROL_PIXCLOCK_MIN 5000 /* ~ 200 MHz dot clock */
973 * calculate the clock paramaters to be sent to CUDA according to given
974 * pixclock in pico second.
976 static int calc_clock_params(unsigned long clk, unsigned char *param)
978 unsigned long p0, p1, p2, k, l, m, n, min;
980 if (clk > (CONTROL_PIXCLOCK_BASE << 3))
983 p2 = ((clk << 4) < CONTROL_PIXCLOCK_BASE)? 3: 2;
987 for (k = 1, min = l; k < 32; k++) {
990 m = CONTROL_PIXCLOCK_BASE * k;
993 if (n && (n < 128) && rem < min) {
1011 * This routine takes a user-supplied var, and picks the best vmode/cmode
1015 static int control_var_to_par(struct fb_var_screeninfo *var,
1016 struct fb_par_control *par, const struct fb_info *fb_info)
1018 int cmode, piped_diff, hstep;
1019 unsigned hperiod, hssync, hsblank, hesync, heblank, piped, heq, hlfln,
1020 hserr, vperiod, vssync, vesync, veblank, vsblank, vswin, vewin;
1021 unsigned long pixclock;
1022 struct fb_info_control *p = (struct fb_info_control *) fb_info;
1023 struct control_regvals *r = &par->regvals;
1025 switch (var->bits_per_pixel) {
1027 par->cmode = CMODE_8;
1028 if (p->total_vram > 0x200000) {
1030 r->radacal_ctrl = 0x20;
1034 r->radacal_ctrl = 0x10;
1040 par->cmode = CMODE_16;
1041 if (p->total_vram > 0x200000) {
1043 r->radacal_ctrl = 0x24;
1047 r->radacal_ctrl = 0x14;
1052 par->cmode = CMODE_32;
1053 if (p->total_vram > 0x200000) {
1055 r->radacal_ctrl = 0x28;
1058 r->radacal_ctrl = 0x18;
1067 * adjust xres and vxres so that the corresponding memory widths are
1070 hstep = 31 >> par->cmode;
1071 par->xres = (var->xres + hstep) & ~hstep;
1072 par->vxres = (var->xres_virtual + hstep) & ~hstep;
1073 par->xoffset = (var->xoffset + hstep) & ~hstep;
1074 if (par->vxres < par->xres)
1075 par->vxres = par->xres;
1076 par->pitch = par->vxres << par->cmode;
1078 par->yres = var->yres;
1079 par->vyres = var->yres_virtual;
1080 par->yoffset = var->yoffset;
1081 if (par->vyres < par->yres)
1082 par->vyres = par->yres;
1084 par->sync = var->sync;
1086 if (par->pitch * par->vyres + CTRLFB_OFF > p->total_vram)
1089 if (par->xoffset + par->xres > par->vxres)
1090 par->xoffset = par->vxres - par->xres;
1091 if (par->yoffset + par->yres > par->vyres)
1092 par->yoffset = par->vyres - par->yres;
1094 pixclock = (var->pixclock < CONTROL_PIXCLOCK_MIN)? CONTROL_PIXCLOCK_MIN:
1096 if (calc_clock_params(pixclock, r->clock_params))
1099 hperiod = ((var->left_margin + par->xres + var->right_margin
1100 + var->hsync_len) >> 1) - 2;
1101 hssync = hperiod + 1;
1102 hsblank = hssync - (var->right_margin >> 1);
1103 hesync = (var->hsync_len >> 1) - 1;
1104 heblank = (var->left_margin >> 1) + hesync;
1105 piped = heblank - piped_diff;
1106 heq = var->hsync_len >> 2;
1107 hlfln = (hperiod+2) >> 1;
1108 hserr = hssync-hesync;
1109 vperiod = (var->vsync_len + var->lower_margin + par->yres
1110 + var->upper_margin) << 1;
1111 vssync = vperiod - 2;
1112 vesync = (var->vsync_len << 1) - vperiod + vssync;
1113 veblank = (var->upper_margin << 1) + vesync;
1114 vsblank = vssync - (var->lower_margin << 1);
1115 vswin = (vsblank+vssync) >> 1;
1116 vewin = (vesync+veblank) >> 1;
1119 r->regs[1] = vsblank;
1120 r->regs[2] = veblank;
1122 r->regs[4] = vesync;
1123 r->regs[5] = vssync;
1124 r->regs[6] = vperiod;
1126 r->regs[8] = hperiod;
1127 r->regs[9] = hsblank;
1128 r->regs[10] = heblank;
1129 r->regs[11] = hesync;
1130 r->regs[12] = hssync;
1132 r->regs[14] = hlfln;
1133 r->regs[15] = hserr;
1135 if (par->xres >= 1280 && par->cmode >= CMODE_16)
1140 if (mac_var_to_vmode(var, &par->vmode, &cmode))
1148 * Convert hardware data in par to an fb_var_screeninfo
1151 static void control_par_to_var(struct fb_par_control *par, struct fb_var_screeninfo *var)
1153 struct control_regints *rv;
1155 rv = (struct control_regints *) par->regvals.regs;
1157 memset(var, 0, sizeof(*var));
1158 var->xres = par->xres;
1159 var->yres = par->yres;
1160 var->xres_virtual = par->vxres;
1161 var->yres_virtual = par->vyres;
1162 var->xoffset = par->xoffset;
1163 var->yoffset = par->yoffset;
1165 switch(par->cmode) {
1168 var->bits_per_pixel = 8;
1169 var->red.length = 8;
1170 var->green.length = 8;
1171 var->blue.length = 8;
1173 case CMODE_16: /* RGB 555 */
1174 var->bits_per_pixel = 16;
1175 var->red.offset = 10;
1176 var->red.length = 5;
1177 var->green.offset = 5;
1178 var->green.length = 5;
1179 var->blue.length = 5;
1181 case CMODE_32: /* RGB 888 */
1182 var->bits_per_pixel = 32;
1183 var->red.offset = 16;
1184 var->red.length = 8;
1185 var->green.offset = 8;
1186 var->green.length = 8;
1187 var->blue.length = 8;
1188 var->transp.offset = 24;
1189 var->transp.length = 8;
1194 var->vmode = FB_VMODE_NONINTERLACED;
1196 var->left_margin = (rv->heblank - rv->hesync) << 1;
1197 var->right_margin = (rv->hssync - rv->hsblank) << 1;
1198 var->hsync_len = (rv->hperiod + 2 - rv->hssync + rv->hesync) << 1;
1200 var->upper_margin = (rv->veblank - rv->vesync) >> 1;
1201 var->lower_margin = (rv->vssync - rv->vsblank) >> 1;
1202 var->vsync_len = (rv->vperiod - rv->vssync + rv->vesync) >> 1;
1204 var->sync = par->sync;
1207 * 10^12 * clock_params[0] / (3906400 * clock_params[1]
1208 * * 2^clock_params[2])
1209 * (10^12 * clock_params[0] / (3906400 * clock_params[1]))
1210 * >> clock_params[2]
1212 /* (255990.17 * clock_params[0] / clock_params[1]) >> clock_params[2] */
1213 var->pixclock = CONTROL_PIXCLOCK_BASE * par->regvals.clock_params[0];
1214 var->pixclock /= par->regvals.clock_params[1];
1215 var->pixclock >>= par->regvals.clock_params[2];
1220 * init fix according to given par
1222 static void control_par_to_fix(struct fb_par_control *par, struct fb_fix_screeninfo *fix,
1223 struct fb_info_control *p)
1225 memset(fix, 0, sizeof(*fix));
1226 strcpy(fix->id, "control");
1227 fix->mmio_start = p->control_regs_phys;
1228 fix->mmio_len = sizeof(struct control_regs);
1229 fix->type = FB_TYPE_PACKED_PIXELS;
1231 fix->xpanstep = 32 >> par->cmode;
1234 fix->smem_start = p->frame_buffer_phys + CTRLFB_OFF;
1235 fix->smem_len = p->total_vram - CTRLFB_OFF;
1236 fix->visual = (par->cmode == CMODE_8) ?
1237 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1238 fix->line_length = par->pitch;
1242 * initialize a portion of struct display which low level driver is responsible
1245 static void control_par_to_display(struct fb_par_control *par,
1246 struct display *disp, struct fb_fix_screeninfo *fix, struct fb_info_control *p)
1248 disp->type = fix->type;
1249 disp->can_soft_blank = 1;
1250 disp->scrollmode = SCROLL_YNOMOVE | SCROLL_YNOPARTIAL;
1251 disp->ypanstep = fix->ypanstep;
1252 disp->ywrapstep = fix->ywrapstep;
1254 control_par_to_var(par, &disp->var);
1255 disp->screen_base = (char *) p->frame_buffer + CTRLFB_OFF;
1256 disp->visual = fix->visual;
1257 disp->line_length = fix->line_length;
1258 control_set_dispsw(disp, par->cmode, p);
1263 * our own _revc() routines since generic routines don't work for DIRECT Color
1264 * devices like control
1266 static void control_cfb16_revc(struct display *p, int xx, int yy)
1269 int bytes = p->next_line, rows;
1271 dest = p->screen_base + yy * fontheight(p) * bytes + xx * fontwidth(p)*2;
1272 for (rows = fontheight(p); rows--; dest += bytes) {
1273 switch (fontwidth(p)) {
1275 ((u32 *)dest)[6] ^= 0x3def3def; ((u32 *)dest)[7] ^= 0x3def3def;
1278 ((u32 *)dest)[4] ^= 0x3def3def; ((u32 *)dest)[5] ^= 0x3def3def;
1281 ((u32 *)dest)[2] ^= 0x3def3def; ((u32 *)dest)[3] ^= 0x3def3def;
1284 ((u32 *)dest)[0] ^= 0x3def3def; ((u32 *)dest)[1] ^= 0x3def3def;
1289 static void control_cfb32_revc(struct display *p, int xx, int yy)
1292 int bytes = p->next_line, rows;
1294 dest = p->screen_base + yy * fontheight(p) * bytes + xx * fontwidth(p) * 4;
1295 for (rows = fontheight(p); rows--; dest += bytes) {
1296 switch (fontwidth(p)) {
1298 ((u32 *)dest)[12] ^= 0x0f0f0f0f; ((u32 *)dest)[13] ^= 0x0f0f0f0f;
1299 ((u32 *)dest)[14] ^= 0x0f0f0f0f; ((u32 *)dest)[15] ^= 0x0f0f0f0f;
1302 ((u32 *)dest)[8] ^= 0x0f0f0f0f; ((u32 *)dest)[9] ^= 0x0f0f0f0f;
1303 ((u32 *)dest)[10] ^= 0x0f0f0f0f; ((u32 *)dest)[11] ^= 0x0f0f0f0f;
1306 ((u32 *)dest)[4] ^= 0x0f0f0f0f; ((u32 *)dest)[5] ^= 0x0f0f0f0f;
1307 ((u32 *)dest)[6] ^= 0x0f0f0f0f; ((u32 *)dest)[7] ^= 0x0f0f0f0f;
1310 ((u32 *)dest)[0] ^= 0x0f0f0f0f; ((u32 *)dest)[1] ^= 0x0f0f0f0f;
1311 ((u32 *)dest)[2] ^= 0x0f0f0f0f; ((u32 *)dest)[3] ^= 0x0f0f0f0f;
1317 static struct display_switch control_cfb16 = {
1318 setup: fbcon_cfb16_setup,
1319 bmove: fbcon_cfb16_bmove,
1320 clear: fbcon_cfb16_clear,
1321 putc: fbcon_cfb16_putc,
1322 putcs: fbcon_cfb16_putcs,
1323 revc: control_cfb16_revc,
1324 clear_margins: fbcon_cfb16_clear_margins,
1325 fontwidthmask: FONTWIDTH(4)|FONTWIDTH(8)|FONTWIDTH(12)|FONTWIDTH(16)
1328 static struct display_switch control_cfb32 = {
1329 setup: fbcon_cfb32_setup,
1330 bmove: fbcon_cfb32_bmove,
1331 clear: fbcon_cfb32_clear,
1332 putc: fbcon_cfb32_putc,
1333 putcs: fbcon_cfb32_putcs,
1334 revc: control_cfb32_revc,
1335 clear_margins: fbcon_cfb32_clear_margins,
1336 fontwidthmask: FONTWIDTH(4)|FONTWIDTH(8)|FONTWIDTH(12)|FONTWIDTH(16)
1341 * Set struct dispsw according to given cmode
1343 static void control_set_dispsw(struct display *disp, int cmode, struct fb_info_control *p)
1346 #ifdef FBCON_HAS_CFB8
1348 disp->dispsw = &fbcon_cfb8;
1351 #ifdef FBCON_HAS_CFB16
1353 disp->dispsw = &control_cfb16;
1354 disp->dispsw_data = p->fbcon_cmap.cfb16;
1357 #ifdef FBCON_HAS_CFB32
1359 disp->dispsw = &control_cfb32;
1360 disp->dispsw_data = p->fbcon_cmap.cfb32;
1364 disp->dispsw = &fbcon_dummy;
1371 * Set misc info vars for this driver
1373 static void __init control_init_info(struct fb_info *info, struct fb_info_control *p)
1375 strcpy(info->modename, "control");
1376 info->node = -1; /* ??? danj */
1377 info->fbops = &controlfb_ops;
1378 info->disp = &p->display;
1379 strcpy(info->fontname, fontname);
1380 info->changevar = NULL;
1381 info->switch_con = &controlfb_switch;
1382 info->updatevar = &controlfb_updatevar;
1383 info->blank = &controlfb_blank;
1387 static void control_cleanup(void)
1389 struct fb_info_control *p = control_fb;
1395 iounmap(p->cmap_regs);
1396 if (p->control_regs)
1397 iounmap(p->control_regs);
1398 if (p->frame_buffer) {
1399 if (p->control_use_bank2)
1400 p->frame_buffer -= 0x600000;
1401 iounmap(p->frame_buffer);
1403 if (p->cmap_regs_phys)
1404 release_mem_region(p->cmap_regs_phys, 0x1000);
1405 if (p->control_regs_phys)
1406 release_mem_region(p->control_regs_phys, p->control_regs_size);
1407 if (p->fb_orig_base)
1408 release_mem_region(p->fb_orig_base, p->fb_orig_size);
1414 * Parse user speficied options (`video=controlfb:')
1416 void __init control_setup(char *options)
1420 if (!options || !*options)
1423 while ((this_opt = strsep(&options, ",")) != NULL) {
1424 if (!strncmp(this_opt, "font:", 5)) {
1429 for (i = 0; i < sizeof(fontname) - 1; i++)
1430 if (!*p || *p == ' ' || *p == ',')
1432 memcpy(fontname, this_opt + 5, i);
1434 } else if (!strncmp(this_opt, "vmode:", 6)) {
1435 int vmode = simple_strtoul(this_opt+6, NULL, 0);
1436 if (vmode > 0 && vmode <= VMODE_MAX &&
1437 control_mac_modes[vmode - 1].m[1] >= 0)
1438 default_vmode = vmode;
1439 } else if (!strncmp(this_opt, "cmode:", 6)) {
1440 int depth = simple_strtoul(this_opt+6, NULL, 0);
1445 default_cmode = depth;
1448 default_cmode = CMODE_8;
1452 default_cmode = CMODE_16;
1456 default_cmode = CMODE_32;