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