2 * valkyriefb.c -- frame buffer device for the PowerMac 'valkyrie' display
4 * Created 8 August 1998 by
5 * Martin Costabel <costabel@wanadoo.fr> and Kevin Schoedel
7 * Vmode-switching changes and vmode 15/17 modifications created 29 August
8 * 1998 by Barry K. Nathan <barryn@pobox.com>.
10 * Ported to m68k Macintosh by David Huggins-Daines <dhd@debian.org>
12 * Derived directly from:
14 * controlfb.c -- frame buffer device for the PowerMac 'control' display
15 * Copyright (C) 1998 Dan Jacobowitz <dan@debian.org>
17 * pmc-valkyrie.c -- Console support for PowerMac "valkyrie" display adaptor.
18 * Copyright (C) 1997 Paul Mackerras.
22 * Frame buffer structure from:
23 * drivers/video/chipsfb.c -- frame buffer device for
24 * Chips & Technologies 65550 chip.
26 * Copyright (C) 1998 Paul Mackerras
28 * This file is derived from the Powermac "chips" driver:
29 * Copyright (C) 1997 Fabio Riccardi.
30 * And from the frame buffer device for Open Firmware-initialized devices:
31 * Copyright (C) 1997 Geert Uytterhoeven.
33 * Hardware information from:
34 * control.c: Console support for PowerMac "control" display adaptor.
35 * Copyright (C) 1996 Paul Mackerras
37 * This file is subject to the terms and conditions of the GNU General Public
38 * License. See the file COPYING in the main directory of this archive for
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/errno.h>
46 #include <linux/string.h>
48 #include <linux/tty.h>
49 #include <linux/slab.h>
50 #include <linux/vmalloc.h>
51 #include <linux/delay.h>
52 #include <linux/interrupt.h>
54 #include <linux/selection.h>
55 #include <linux/init.h>
56 #include <linux/pci.h>
57 #include <linux/nvram.h>
58 #include <linux/adb.h>
59 #include <linux/cuda.h>
62 #include <asm/bootinfo.h>
63 #include <asm/macintosh.h>
67 #include <asm/pgtable.h>
70 #include "valkyriefb.h"
73 /* We don't yet have functions to read the PRAM... perhaps we can
74 adapt them from the PPC code? */
75 static int default_vmode = VMODE_640_480_67;
76 static int default_cmode = CMODE_8;
78 static int default_vmode = VMODE_NVRAM;
79 static int default_cmode = CMODE_NVRAM;
82 struct fb_par_valkyrie {
86 struct valkyrie_regvals *init;
89 struct fb_info_valkyrie {
91 struct fb_par_valkyrie par;
92 struct cmap_regs *cmap_regs;
93 unsigned long cmap_regs_phys;
95 struct valkyrie_regs *valkyrie_regs;
96 unsigned long valkyrie_regs_phys;
99 unsigned long frame_buffer_phys;
102 unsigned long total_vram;
104 u32 pseudo_palette[16];
110 int valkyriefb_init(void);
111 int valkyriefb_setup(char*);
113 static int valkyriefb_check_var(struct fb_var_screeninfo *var,
114 struct fb_info *info);
115 static int valkyriefb_set_par(struct fb_info *info);
116 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
117 u_int transp, struct fb_info *info);
118 static int valkyriefb_blank(int blank_mode, struct fb_info *info);
120 static int read_valkyrie_sense(struct fb_info_valkyrie *p);
121 static inline int valkyrie_vram_reqd(int video_mode, int color_mode);
122 static void set_valkyrie_clock(unsigned char *params);
123 static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par, struct fb_var_screeninfo *var);
124 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
125 struct fb_par_valkyrie *par, const struct fb_info *fb_info);
127 static void valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
128 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix);
129 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
131 static struct fb_ops valkyriefb_ops = {
132 .owner = THIS_MODULE,
133 .fb_check_var = valkyriefb_check_var,
134 .fb_set_par = valkyriefb_set_par,
135 .fb_setcolreg = valkyriefb_setcolreg,
136 .fb_blank = valkyriefb_blank,
137 .fb_fillrect = cfb_fillrect,
138 .fb_copyarea = cfb_copyarea,
139 .fb_imageblit = cfb_imageblit,
140 .fb_cursor = soft_cursor,
143 /* Sets the video mode according to info->var */
144 static int valkyriefb_set_par(struct fb_info *info)
146 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
147 volatile struct valkyrie_regs *valkyrie_regs = p->valkyrie_regs;
148 struct fb_par_valkyrie *par = info->par;
149 struct valkyrie_regvals *init;
152 if ((err = valkyrie_var_to_par(&info->var, par, info)))
155 valkyrie_par_to_fix(par, &info->fix);
157 /* Reset the valkyrie */
158 out_8(&valkyrie_regs->status.r, 0);
161 /* Initialize display timing registers */
163 out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
164 out_8(&valkyrie_regs->depth.r, par->cmode + 3);
165 set_valkyrie_clock(init->clock_params);
168 /* Turn on display */
169 out_8(&valkyrie_regs->mode.r, init->mode);
175 valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
178 struct fb_par_valkyrie par;
180 if ((err = valkyrie_var_to_par(var, &par, info)))
182 valkyrie_par_to_var(&par, var);
187 * Blank the screen if blank_mode != 0, else unblank. If blank_mode == NULL
188 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
189 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
190 * to e.g. a video mode which doesn't support it. Implements VESA suspend
191 * and powerdown modes on hardware that supports disabling hsync/vsync:
192 * blank_mode == 2: suspend vsync
193 * blank_mode == 3: suspend hsync
194 * blank_mode == 4: powerdown
196 static int valkyriefb_blank(int blank_mode, struct fb_info *info)
198 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
199 struct fb_par_valkyrie *par = info->par;
200 struct valkyrie_regvals *init = par->init;
205 switch (blank_mode) {
206 case 0: /* unblank */
207 out_8(&p->valkyrie_regs->mode.r, init->mode);
210 return 1; /* get caller to set CLUT to all black */
211 case VESA_VSYNC_SUSPEND+1:
212 case VESA_HSYNC_SUSPEND+1:
214 * [kps] Value extracted from MacOS. I don't know
215 * whether this bit disables hsync or vsync, or
216 * whether the hardware can do the other as well.
218 out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
220 case VESA_POWERDOWN+1:
221 out_8(&p->valkyrie_regs->mode.r, 0x66);
227 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
228 u_int transp, struct fb_info *info)
230 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
231 volatile struct cmap_regs *cmap_regs = p->cmap_regs;
232 struct fb_par_valkyrie *par = info->par;
240 /* tell clut which address to fill */
241 out_8(&p->cmap_regs->addr, regno);
243 /* send one color channel at a time */
244 out_8(&cmap_regs->lut, red);
245 out_8(&cmap_regs->lut, green);
246 out_8(&cmap_regs->lut, blue);
248 if (regno < 16 && par->cmode == CMODE_16)
249 ((u32 *)info->pseudo_palette)[regno] =
250 (regno << 10) | (regno << 5) | regno;
255 static int valkyrie_vram_reqd(int video_mode, int color_mode)
258 struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
260 if ((pitch = init->pitch[color_mode]) == 0)
261 pitch = 2 * init->pitch[0];
262 return init->vres * pitch;
265 static void set_valkyrie_clock(unsigned char *params)
267 struct adb_request req;
270 #ifdef CONFIG_ADB_CUDA
271 for (i = 0; i < 3; ++i) {
272 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
273 0x50, i + 1, params[i]);
274 while (!req.complete)
280 static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
282 p->sense = read_valkyrie_sense(p);
283 printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
285 /* Try to pick a video mode out of NVRAM if we have one. */
287 if (default_vmode == VMODE_NVRAM) {
288 default_vmode = nvram_read_byte(NV_VMODE);
289 if (default_vmode <= 0
290 || default_vmode > VMODE_MAX
291 || !valkyrie_reg_init[default_vmode - 1])
292 default_vmode = VMODE_CHOOSE;
295 if (default_vmode == VMODE_CHOOSE)
296 default_vmode = mac_map_monitor_sense(p->sense);
297 if (!valkyrie_reg_init[default_vmode - 1])
298 default_vmode = VMODE_640_480_67;
300 if (default_cmode == CMODE_NVRAM)
301 default_cmode = nvram_read_byte(NV_CMODE);
305 * Reduce the pixel size if we don't have enough VRAM or bandwidth.
307 if (default_cmode < CMODE_8 || default_cmode > CMODE_16
308 || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
309 || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
310 default_cmode = CMODE_8;
312 printk(KERN_INFO "using video mode %d and color mode %d.\n",
313 default_vmode, default_cmode);
316 int __init valkyriefb_init(void)
318 struct fb_info_valkyrie *p;
319 unsigned long frame_buffer_phys, cmap_regs_phys, flags;
325 if (!(mac_bi_data.id == MAC_MODEL_Q630
326 /* I'm not sure about this one */
327 || mac_bi_data.id == MAC_MODEL_P588))
330 /* Hardcoded addresses... welcome to 68k Macintosh country :-) */
331 frame_buffer_phys = 0xf9000000;
332 cmap_regs_phys = 0x50f24000;
333 flags = IOMAP_NOCACHE_SER; /* IOMAP_WRITETHROUGH?? */
334 #else /* ppc (!CONFIG_MAC) */
335 struct device_node *dp;
337 dp = find_devices("valkyrie");
341 if (dp->n_addrs != 1) {
342 printk(KERN_ERR "expecting 1 address for valkyrie (got %d)\n",
347 frame_buffer_phys = dp->addrs[0].address;
348 cmap_regs_phys = dp->addrs[0].address+0x304000;
349 flags = _PAGE_WRITETHRU;
350 #endif /* ppc (!CONFIG_MAC) */
352 p = kmalloc(sizeof(*p), GFP_ATOMIC);
355 memset(p, 0, sizeof(*p));
357 /* Map in frame buffer and registers */
358 if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
362 p->total_vram = 0x100000;
363 p->frame_buffer_phys = frame_buffer_phys;
364 p->frame_buffer = __ioremap(frame_buffer_phys, p->total_vram, flags);
365 p->cmap_regs_phys = cmap_regs_phys;
366 p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
367 p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
368 p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
370 if (p->frame_buffer == NULL || p->cmap_regs == NULL
371 || p->valkyrie_regs == NULL) {
372 printk(KERN_ERR "valkyriefb: couldn't map resources\n");
376 valkyrie_choose_mode(p);
377 mac_vmode_to_var(default_vmode, default_cmode, &p->info.var);
378 valkyrie_init_info(&p->info, p);
379 valkyrie_init_fix(&p->info.fix, p);
380 if (valkyriefb_set_par(&p->info))
382 printk(KERN_ERR "valkyriefb: can't set default video mode\n");
384 if ((err = register_framebuffer(&p->info)) != 0)
387 printk(KERN_INFO "fb%d: valkyrie frame buffer device\n", p->info.node);
392 iounmap(p->frame_buffer);
394 iounmap(p->cmap_regs);
395 if (p->valkyrie_regs)
396 iounmap(p->valkyrie_regs);
402 * Get the monitor sense value.
404 static int read_valkyrie_sense(struct fb_info_valkyrie *p)
408 out_8(&p->valkyrie_regs->msense.r, 0); /* release all lines */
410 sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
411 /* drive each sense line low in turn and collect the other 2 */
412 out_8(&p->valkyrie_regs->msense.r, 4); /* drive A low */
414 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
415 out_8(&p->valkyrie_regs->msense.r, 2); /* drive B low */
417 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
418 sense |= (in & 0x10) >> 2;
419 out_8(&p->valkyrie_regs->msense.r, 1); /* drive C low */
421 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
423 out_8(&p->valkyrie_regs->msense.r, 7);
429 * This routine takes a user-supplied var,
430 * and picks the best vmode/cmode from it.
433 /* [bkn] I did a major overhaul of this function.
435 * Much of the old code was "swiped by jonh from atyfb.c". Because
436 * macmodes has mac_var_to_vmode, I felt that it would be better to
437 * rework this function to use that, instead of reinventing the wheel to
438 * add support for vmode 17. This was reinforced by the fact that
439 * the previously swiped atyfb.c code is no longer there.
441 * So, I swiped and adapted platinum_var_to_par (from platinumfb.c), replacing
442 * most, but not all, of the old code in the process. One side benefit of
443 * swiping the platinumfb code is that we now have more comprehensible error
444 * messages when a vmode/cmode switch fails. (Most of the error messages are
445 * platinumfb.c, but I added two of my own, and I also changed some commas
446 * into colons to make the messages more consistent with other Linux error
447 * messages.) In addition, I think the new code *might* fix some vmode-
448 * switching oddities, but I'm not sure.
450 * There may be some more opportunities for cleanup in here, but this is a
454 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
455 struct fb_par_valkyrie *par, const struct fb_info *fb_info)
458 struct valkyrie_regvals *init;
459 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) fb_info;
461 if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
462 printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
463 var->xres, var->yres, var->bits_per_pixel);
467 /* Check if we know about the wanted video mode */
468 if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
469 printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
473 if (cmode != CMODE_8 && cmode != CMODE_16) {
474 printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
478 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
479 || var->xoffset != 0 || var->yoffset != 0) {
483 init = valkyrie_reg_init[vmode-1];
484 if (init->pitch[cmode] == 0) {
485 printk(KERN_ERR "valkyriefb: vmode %d does not support "
486 "cmode %d.\n", vmode, cmode);
490 if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
491 printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
492 "cmode %d.\n", vmode, cmode);
499 par->xres = var->xres;
500 par->yres = var->yres;
501 par->vxres = par->xres;
502 par->vyres = par->yres;
507 static int valkyrie_par_to_var(struct fb_par_valkyrie *par, struct fb_var_screeninfo *var)
509 return mac_vmode_to_var(par->vmode, par->cmode, var);
512 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
514 memset(fix, 0, sizeof(*fix));
515 strcpy(fix->id, "valkyrie");
516 fix->mmio_start = p->valkyrie_regs_phys;
517 fix->mmio_len = sizeof(struct valkyrie_regs);
518 fix->type = FB_TYPE_PACKED_PIXELS;
519 fix->smem_start = p->frame_buffer_phys + 0x1000;
520 fix->smem_len = p->total_vram;
529 /* Fix must already be inited above */
530 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
531 struct fb_fix_screeninfo *fix)
533 fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
534 fix->visual = (par->cmode == CMODE_8) ?
535 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
536 fix->line_length = par->vxres << par->cmode;
537 /* ywrapstep, xpanstep, ypanstep */
540 static void __init valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p)
542 info->fbops = &valkyriefb_ops;
543 info->screen_base = (char *) p->frame_buffer + 0x1000;
544 info->flags = FBINFO_FLAG_DEFAULT;
545 info->pseudo_palette = p->pseudo_palette;
546 fb_alloc_cmap(&info->cmap, 256, 0);
552 * Parse user speficied options (`video=valkyriefb:')
554 int __init valkyriefb_setup(char *options)
558 if (!options || !*options)
561 while ((this_opt = strsep(&options, ",")) != NULL) {
562 if (!strncmp(this_opt, "vmode:", 6)) {
563 int vmode = simple_strtoul(this_opt+6, NULL, 0);
564 if (vmode > 0 && vmode <= VMODE_MAX)
565 default_vmode = vmode;
567 else if (!strncmp(this_opt, "cmode:", 6)) {
568 int depth = simple_strtoul(this_opt+6, NULL, 0);
571 default_cmode = CMODE_8;
575 default_cmode = CMODE_16;
583 MODULE_LICENSE("GPL");