xref: /linux/drivers/video/fbdev/valkyriefb.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 /*
2  *  valkyriefb.c -- frame buffer device for the PowerMac 'valkyrie' display
3  *
4  *  Created 8 August 1998 by
5  *  Martin Costabel <costabel@wanadoo.fr> and Kevin Schoedel
6  *
7  *  Vmode-switching changes and vmode 15/17 modifications created 29 August
8  *  1998 by Barry K. Nathan <barryn@pobox.com>.
9  *
10  *  Ported to m68k Macintosh by David Huggins-Daines <dhd@debian.org>
11  *
12  *  Derived directly from:
13  *
14  *   controlfb.c -- frame buffer device for the PowerMac 'control' display
15  *   Copyright (C) 1998 Dan Jacobowitz <dan@debian.org>
16  *
17  *   pmc-valkyrie.c -- Console support for PowerMac "valkyrie" display adaptor.
18  *   Copyright (C) 1997 Paul Mackerras.
19  *
20  *  and indirectly:
21  *
22  *  Frame buffer structure from:
23  *    drivers/video/chipsfb.c -- frame buffer device for
24  *    Chips & Technologies 65550 chip.
25  *
26  *    Copyright (C) 1998 Paul Mackerras
27  *
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.
32  *
33  *  Hardware information from:
34  *    control.c: Console support for PowerMac "control" display adaptor.
35  *    Copyright (C) 1996 Paul Mackerras
36  *
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
39  *  more details.
40  */
41 
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/mm.h>
47 #include <linux/slab.h>
48 #include <linux/vmalloc.h>
49 #include <linux/delay.h>
50 #include <linux/interrupt.h>
51 #include <linux/fb.h>
52 #include <linux/selection.h>
53 #include <linux/init.h>
54 #include <linux/nvram.h>
55 #include <linux/adb.h>
56 #include <linux/cuda.h>
57 #include <linux/of_address.h>
58 #ifdef CONFIG_MAC
59 #include <asm/macintosh.h>
60 #endif
61 
62 #include "macmodes.h"
63 #include "valkyriefb.h"
64 
65 static int default_vmode = VMODE_NVRAM;
66 static int default_cmode = CMODE_NVRAM;
67 
68 struct fb_par_valkyrie {
69 	int	vmode, cmode;
70 	int	xres, yres;
71 	int	vxres, vyres;
72 	struct valkyrie_regvals *init;
73 };
74 
75 struct fb_info_valkyrie {
76 	struct fb_info		info;
77 	struct fb_par_valkyrie	par;
78 	struct cmap_regs	__iomem *cmap_regs;
79 	unsigned long		cmap_regs_phys;
80 
81 	struct valkyrie_regs	__iomem *valkyrie_regs;
82 	unsigned long		valkyrie_regs_phys;
83 
84 	__u8			__iomem *frame_buffer;
85 	unsigned long		frame_buffer_phys;
86 
87 	int			sense;
88 	unsigned long		total_vram;
89 
90 	u32			pseudo_palette[16];
91 };
92 
93 static int valkyriefb_setup(char*);
94 
95 static int valkyriefb_check_var(struct fb_var_screeninfo *var,
96 				struct fb_info *info);
97 static int valkyriefb_set_par(struct fb_info *info);
98 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
99 			     u_int transp, struct fb_info *info);
100 static int valkyriefb_blank(int blank_mode, struct fb_info *info);
101 
102 static int read_valkyrie_sense(struct fb_info_valkyrie *p);
103 static void set_valkyrie_clock(unsigned char *params);
104 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
105 	struct fb_par_valkyrie *par, const struct fb_info *fb_info);
106 
107 static int valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
108 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix);
109 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
110 
111 static const struct fb_ops valkyriefb_ops = {
112 	.owner =	THIS_MODULE,
113 	.fb_check_var =	valkyriefb_check_var,
114 	.fb_set_par =	valkyriefb_set_par,
115 	.fb_setcolreg =	valkyriefb_setcolreg,
116 	.fb_blank =	valkyriefb_blank,
117 	.fb_fillrect	= cfb_fillrect,
118 	.fb_copyarea	= cfb_copyarea,
119 	.fb_imageblit	= cfb_imageblit,
120 };
121 
122 /* Sets the video mode according to info->var */
123 static int valkyriefb_set_par(struct fb_info *info)
124 {
125 	struct fb_info_valkyrie *p =
126 		container_of(info, struct fb_info_valkyrie, info);
127 	volatile struct valkyrie_regs __iomem *valkyrie_regs = p->valkyrie_regs;
128 	struct fb_par_valkyrie *par = info->par;
129 	struct valkyrie_regvals	*init;
130 	int err;
131 
132 	if ((err = valkyrie_var_to_par(&info->var, par, info)))
133 		return err;
134 
135 	valkyrie_par_to_fix(par, &info->fix);
136 
137 	/* Reset the valkyrie */
138 	out_8(&valkyrie_regs->status.r, 0);
139 	udelay(100);
140 
141 	/* Initialize display timing registers */
142 	init = par->init;
143 	out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
144 	out_8(&valkyrie_regs->depth.r, par->cmode + 3);
145 	set_valkyrie_clock(init->clock_params);
146 	udelay(100);
147 
148 	/* Turn on display */
149 	out_8(&valkyrie_regs->mode.r, init->mode);
150 
151 	return 0;
152 }
153 
154 static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par,
155 				      struct fb_var_screeninfo *var)
156 {
157 	return mac_vmode_to_var(par->vmode, par->cmode, var);
158 }
159 
160 static int
161 valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
162 {
163 	int err;
164 	struct fb_par_valkyrie par;
165 
166 	if ((err = valkyrie_var_to_par(var, &par, info)))
167 		return err;
168 	valkyrie_par_to_var(&par, var);
169 	return 0;
170 }
171 
172 /*
173  *  Blank the screen if blank_mode != 0, else unblank. If blank_mode == NULL
174  *  then the caller blanks by setting the CLUT (Color Look Up Table) to all
175  *  black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
176  *  to e.g. a video mode which doesn't support it. Implements VESA suspend
177  *  and powerdown modes on hardware that supports disabling hsync/vsync:
178  *    blank_mode == 2: suspend vsync
179  *    blank_mode == 3: suspend hsync
180  *    blank_mode == 4: powerdown
181  */
182 static int valkyriefb_blank(int blank_mode, struct fb_info *info)
183 {
184 	struct fb_info_valkyrie *p =
185 		container_of(info, struct fb_info_valkyrie, info);
186 	struct fb_par_valkyrie *par = info->par;
187 	struct valkyrie_regvals	*init = par->init;
188 
189 	if (init == NULL)
190 		return 1;
191 
192 	switch (blank_mode) {
193 	case FB_BLANK_UNBLANK:			/* unblank */
194 		out_8(&p->valkyrie_regs->mode.r, init->mode);
195 		break;
196 	case FB_BLANK_NORMAL:
197 		return 1;	/* get caller to set CLUT to all black */
198 	case FB_BLANK_VSYNC_SUSPEND:
199 	case FB_BLANK_HSYNC_SUSPEND:
200 		/*
201 		 * [kps] Value extracted from MacOS. I don't know
202 		 * whether this bit disables hsync or vsync, or
203 		 * whether the hardware can do the other as well.
204 		 */
205 		out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
206 		break;
207 	case FB_BLANK_POWERDOWN:
208 		out_8(&p->valkyrie_regs->mode.r, 0x66);
209 		break;
210 	}
211 	return 0;
212 }
213 
214 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
215 			     u_int transp, struct fb_info *info)
216 {
217 	struct fb_info_valkyrie *p =
218 		container_of(info, struct fb_info_valkyrie, info);
219 	volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs;
220 	struct fb_par_valkyrie *par = info->par;
221 
222 	if (regno > 255)
223 		return 1;
224 	red >>= 8;
225 	green >>= 8;
226 	blue >>= 8;
227 
228 	/* tell clut which address to fill */
229 	out_8(&p->cmap_regs->addr, regno);
230 	udelay(1);
231 	/* send one color channel at a time */
232 	out_8(&cmap_regs->lut, red);
233 	out_8(&cmap_regs->lut, green);
234 	out_8(&cmap_regs->lut, blue);
235 
236 	if (regno < 16 && par->cmode == CMODE_16)
237 		((u32 *)info->pseudo_palette)[regno] =
238 			(regno << 10) | (regno << 5) | regno;
239 
240 	return 0;
241 }
242 
243 static inline int valkyrie_vram_reqd(int video_mode, int color_mode)
244 {
245 	int pitch;
246 	struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
247 
248 	if ((pitch = init->pitch[color_mode]) == 0)
249 		pitch = 2 * init->pitch[0];
250 	return init->vres * pitch;
251 }
252 
253 static void set_valkyrie_clock(unsigned char *params)
254 {
255 #ifdef CONFIG_ADB_CUDA
256 	struct adb_request req;
257 	int i;
258 
259 	for (i = 0; i < 3; ++i) {
260 		cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
261 			     0x50, i + 1, params[i]);
262 		while (!req.complete)
263 			cuda_poll();
264 	}
265 #endif
266 }
267 
268 static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
269 {
270 	p->sense = read_valkyrie_sense(p);
271 	printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
272 
273 	/* Try to pick a video mode out of NVRAM if we have one. */
274 #ifdef CONFIG_PPC_PMAC
275 	if (IS_REACHABLE(CONFIG_NVRAM) && default_vmode == VMODE_NVRAM)
276 		default_vmode = nvram_read_byte(NV_VMODE);
277 #endif
278 	if (default_vmode <= 0 || default_vmode > VMODE_MAX ||
279 	    !valkyrie_reg_init[default_vmode - 1]) {
280 		default_vmode = mac_map_monitor_sense(p->sense);
281 		if (!valkyrie_reg_init[default_vmode - 1])
282 			default_vmode = VMODE_640_480_67;
283 	}
284 
285 #ifdef CONFIG_PPC_PMAC
286 	if (IS_REACHABLE(CONFIG_NVRAM) && default_cmode == CMODE_NVRAM)
287 		default_cmode = nvram_read_byte(NV_CMODE);
288 #endif
289 	/*
290 	 * Reduce the pixel size if we don't have enough VRAM or bandwidth.
291 	 */
292 	if (default_cmode < CMODE_8 || default_cmode > CMODE_16
293 	    || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
294 	    || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
295 		default_cmode = CMODE_8;
296 
297 	printk(KERN_INFO "using video mode %d and color mode %d.\n",
298 	       default_vmode, default_cmode);
299 }
300 
301 static int __init valkyriefb_init(void)
302 {
303 	struct fb_info_valkyrie	*p;
304 	unsigned long frame_buffer_phys, cmap_regs_phys;
305 	int err;
306 	char *option = NULL;
307 
308 	if (fb_get_options("valkyriefb", &option))
309 		return -ENODEV;
310 	valkyriefb_setup(option);
311 
312 #ifdef CONFIG_MAC
313 	if (!MACH_IS_MAC)
314 		return -ENODEV;
315 	if (!(mac_bi_data.id == MAC_MODEL_Q630
316 	      /* I'm not sure about this one */
317 	    || mac_bi_data.id == MAC_MODEL_P588))
318 		return -ENODEV;
319 
320 	/* Hardcoded addresses... welcome to 68k Macintosh country :-) */
321 	frame_buffer_phys = 0xf9000000;
322 	cmap_regs_phys = 0x50f24000;
323 #else /* ppc (!CONFIG_MAC) */
324 	{
325 		struct device_node *dp;
326 		struct resource r;
327 
328 		dp = of_find_node_by_name(NULL, "valkyrie");
329 		if (!dp)
330 			return 0;
331 
332 		if (of_address_to_resource(dp, 0, &r)) {
333 			printk(KERN_ERR "can't find address for valkyrie\n");
334 			return 0;
335 		}
336 
337 		frame_buffer_phys = r.start;
338 		cmap_regs_phys = r.start + 0x304000;
339 	}
340 #endif /* ppc (!CONFIG_MAC) */
341 
342 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
343 	if (!p)
344 		return -ENOMEM;
345 
346 	/* Map in frame buffer and registers */
347 	if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
348 		kfree(p);
349 		return 0;
350 	}
351 	p->total_vram = 0x100000;
352 	p->frame_buffer_phys = frame_buffer_phys;
353 #ifdef CONFIG_MAC
354 	p->frame_buffer = ioremap(frame_buffer_phys, p->total_vram);
355 #else
356 	p->frame_buffer = ioremap_wt(frame_buffer_phys, p->total_vram);
357 #endif
358 	p->cmap_regs_phys = cmap_regs_phys;
359 	p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
360 	p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
361 	p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
362 	err = -ENOMEM;
363 	if (p->frame_buffer == NULL || p->cmap_regs == NULL
364 	    || p->valkyrie_regs == NULL) {
365 		printk(KERN_ERR "valkyriefb: couldn't map resources\n");
366 		goto out_free;
367 	}
368 
369 	valkyrie_choose_mode(p);
370 	mac_vmode_to_var(default_vmode, default_cmode, &p->info.var);
371 	err = valkyrie_init_info(&p->info, p);
372 	if (err < 0)
373 		goto out_free;
374 	valkyrie_init_fix(&p->info.fix, p);
375 	if (valkyriefb_set_par(&p->info))
376 		/* "can't happen" */
377 		printk(KERN_ERR "valkyriefb: can't set default video mode\n");
378 
379 	if ((err = register_framebuffer(&p->info)) != 0)
380 		goto out_cmap_free;
381 
382 	fb_info(&p->info, "valkyrie frame buffer device\n");
383 	return 0;
384 
385  out_cmap_free:
386 	fb_dealloc_cmap(&p->info.cmap);
387  out_free:
388 	if (p->frame_buffer)
389 		iounmap(p->frame_buffer);
390 	if (p->cmap_regs)
391 		iounmap(p->cmap_regs);
392 	if (p->valkyrie_regs)
393 		iounmap(p->valkyrie_regs);
394 	kfree(p);
395 	return err;
396 }
397 
398 /*
399  * Get the monitor sense value.
400  */
401 static int read_valkyrie_sense(struct fb_info_valkyrie *p)
402 {
403 	int sense, in;
404 
405 	out_8(&p->valkyrie_regs->msense.r, 0);   /* release all lines */
406 	__delay(20000);
407 	sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
408 	/* drive each sense line low in turn and collect the other 2 */
409 	out_8(&p->valkyrie_regs->msense.r, 4);   /* drive A low */
410 	__delay(20000);
411 	sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
412 	out_8(&p->valkyrie_regs->msense.r, 2);   /* drive B low */
413 	__delay(20000);
414 	sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
415 	sense |= (in & 0x10) >> 2;
416 	out_8(&p->valkyrie_regs->msense.r, 1);   /* drive C low */
417 	__delay(20000);
418 	sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
419 
420 	out_8(&p->valkyrie_regs->msense.r, 7);
421 
422 	return sense;
423 }
424 
425 /*
426  * This routine takes a user-supplied var,
427  * and picks the best vmode/cmode from it.
428  */
429 
430 /* [bkn] I did a major overhaul of this function.
431  *
432  * Much of the old code was "swiped by jonh from atyfb.c". Because
433  * macmodes has mac_var_to_vmode, I felt that it would be better to
434  * rework this function to use that, instead of reinventing the wheel to
435  * add support for vmode 17. This was reinforced by the fact that
436  * the previously swiped atyfb.c code is no longer there.
437  *
438  * So, I swiped and adapted platinum_var_to_par (from platinumfb.c), replacing
439  * most, but not all, of the old code in the process. One side benefit of
440  * swiping the platinumfb code is that we now have more comprehensible error
441  * messages when a vmode/cmode switch fails. (Most of the error messages are
442  * platinumfb.c, but I added two of my own, and I also changed some commas
443  * into colons to make the messages more consistent with other Linux error
444  * messages.) In addition, I think the new code *might* fix some vmode-
445  * switching oddities, but I'm not sure.
446  *
447  * There may be some more opportunities for cleanup in here, but this is a
448  * good start...
449  */
450 
451 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
452 	struct fb_par_valkyrie *par, const struct fb_info *fb_info)
453 {
454 	int vmode, cmode;
455 	struct valkyrie_regvals *init;
456 	struct fb_info_valkyrie *p =
457 		container_of(fb_info, struct fb_info_valkyrie, info);
458 
459 	if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
460 		printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
461 		       var->xres, var->yres, var->bits_per_pixel);
462 		return -EINVAL;
463 	}
464 
465 	/* Check if we know about the wanted video mode */
466 	if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
467 		printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
468 		return -EINVAL;
469 	}
470 
471 	if (cmode != CMODE_8 && cmode != CMODE_16) {
472 		printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
473 		return -EINVAL;
474 	}
475 
476 	if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
477 	    || var->xoffset != 0 || var->yoffset != 0) {
478 		return -EINVAL;
479 	}
480 
481 	init = valkyrie_reg_init[vmode-1];
482 	if (init->pitch[cmode] == 0) {
483 		printk(KERN_ERR "valkyriefb: vmode %d does not support "
484 		       "cmode %d.\n", vmode, cmode);
485 		return -EINVAL;
486 	}
487 
488 	if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
489 		printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
490 		       "cmode %d.\n", vmode, cmode);
491 		return -EINVAL;
492 	}
493 
494 	par->vmode = vmode;
495 	par->cmode = cmode;
496 	par->init = init;
497 	par->xres = var->xres;
498 	par->yres = var->yres;
499 	par->vxres = par->xres;
500 	par->vyres = par->yres;
501 
502 	return 0;
503 }
504 
505 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
506 {
507 	memset(fix, 0, sizeof(*fix));
508 	strcpy(fix->id, "valkyrie");
509 	fix->mmio_start = p->valkyrie_regs_phys;
510 	fix->mmio_len = sizeof(struct valkyrie_regs);
511 	fix->type = FB_TYPE_PACKED_PIXELS;
512 	fix->smem_start = p->frame_buffer_phys + 0x1000;
513 	fix->smem_len = p->total_vram;
514 
515 	fix->type_aux = 0;
516 	fix->ywrapstep = 0;
517 	fix->ypanstep = 0;
518 	fix->xpanstep = 0;
519 
520 }
521 
522 /* Fix must already be inited above */
523 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
524 	struct fb_fix_screeninfo *fix)
525 {
526 	fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
527 	fix->visual = (par->cmode == CMODE_8) ?
528 		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
529 	fix->line_length = par->vxres << par->cmode;
530 		/* ywrapstep, xpanstep, ypanstep */
531 }
532 
533 static int __init valkyrie_init_info(struct fb_info *info,
534 		struct fb_info_valkyrie *p)
535 {
536 	info->fbops = &valkyriefb_ops;
537 	info->screen_base = p->frame_buffer + 0x1000;
538 	info->flags = FBINFO_DEFAULT;
539 	info->pseudo_palette = p->pseudo_palette;
540 	info->par = &p->par;
541 	return fb_alloc_cmap(&info->cmap, 256, 0);
542 }
543 
544 
545 /*
546  * Parse user specified options (`video=valkyriefb:')
547  */
548 static int __init valkyriefb_setup(char *options)
549 {
550 	char *this_opt;
551 
552 	if (!options || !*options)
553 		return 0;
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 				default_vmode = vmode;
560 		}
561 		else if (!strncmp(this_opt, "cmode:", 6)) {
562 			int depth = simple_strtoul(this_opt+6, NULL, 0);
563 			switch (depth) {
564 			case 8:
565 				default_cmode = CMODE_8;
566 				break;
567 			case 15:
568 			case 16:
569 				default_cmode = CMODE_16;
570 				break;
571 			}
572 		}
573 	}
574 	return 0;
575 }
576 
577 module_init(valkyriefb_init);
578 MODULE_LICENSE("GPL");
579