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/config.h>
43#include <linux/module.h>
44#include <linux/kernel.h>
45#include <linux/errno.h>
46#include <linux/string.h>
47#include <linux/mm.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>
53#include <linux/fb.h>
54#include <linux/selection.h>
55#include <linux/init.h>
56#include <linux/pci.h>
57#include <linux/nvram.h>
58#ifdef CONFIG_FB_COMPAT_XPMAC
59#include <asm/vc_ioctl.h>
60#endif
61#include <linux/adb.h>
62#include <linux/cuda.h>
63#include <asm/io.h>
64#ifdef CONFIG_MAC
65#include <asm/bootinfo.h>
66#include <asm/macintosh.h>
67#else
68#include <asm/prom.h>
69#endif
70#include <asm/pgtable.h>
71
72#include <video/fbcon.h>
73#include <video/fbcon-cfb8.h>
74#include <video/fbcon-cfb16.h>
75#include <video/macmodes.h>
76
77#include "valkyriefb.h"
78
79static int can_soft_blank = 1;
80
81#ifdef CONFIG_MAC
82/* We don't yet have functions to read the PRAM... perhaps we can
83   adapt them from the PPC code? */
84static int default_vmode = VMODE_640_480_67;
85static int default_cmode = CMODE_8;
86#else
87static int default_vmode = VMODE_NVRAM;
88static int default_cmode = CMODE_NVRAM;
89#endif
90static char fontname[40] __initdata = { 0 };
91
92static int currcon = 0;
93static int switching = 0;
94
95struct fb_par_valkyrie {
96	int	vmode, cmode;
97	int	xres, yres;
98	int	vxres, vyres;
99	int	xoffset, yoffset;
100};
101
102struct fb_info_valkyrie {
103	struct fb_info			info;
104	struct fb_fix_screeninfo	fix;
105	struct fb_var_screeninfo	var;
106	struct display			disp;
107	struct fb_par_valkyrie		par;
108	struct {
109	    __u8 red, green, blue;
110	}			palette[256];
111
112	struct cmap_regs	*cmap_regs;
113	unsigned long		cmap_regs_phys;
114
115	struct valkyrie_regs	*valkyrie_regs;
116	unsigned long		valkyrie_regs_phys;
117
118	__u8			*frame_buffer;
119	unsigned long		frame_buffer_phys;
120
121	int			sense;
122	unsigned long		total_vram;
123#ifdef FBCON_HAS_CFB16
124	u16 fbcon_cfb16_cmap[16];
125#endif
126};
127
128/*
129 * Exported functions
130 */
131int valkyriefb_init(void);
132int valkyriefb_setup(char*);
133
134static int valkyrie_get_fix(struct fb_fix_screeninfo *fix, int con,
135			 struct fb_info *info);
136static int valkyrie_get_var(struct fb_var_screeninfo *var, int con,
137			 struct fb_info *info);
138static int valkyrie_set_var(struct fb_var_screeninfo *var, int con,
139			 struct fb_info *info);
140static int valkyrie_get_cmap(struct fb_cmap *cmap, int kspc, int con,
141			  struct fb_info *info);
142static int valkyrie_set_cmap(struct fb_cmap *cmap, int kspc, int con,
143			  struct fb_info *info);
144
145static int read_valkyrie_sense(struct fb_info_valkyrie *p);
146static inline int valkyrie_vram_reqd(int video_mode, int color_mode);
147static void set_valkyrie_clock(unsigned char *params);
148static void valkyrie_set_par(const struct fb_par_valkyrie *p, struct fb_info_valkyrie *info);
149static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par, struct fb_var_screeninfo *var);
150static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
151	struct fb_par_valkyrie *par, const struct fb_info *fb_info);
152
153static void valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
154static void valkyrie_par_to_display(struct fb_par_valkyrie *par,
155  struct display *disp, struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
156static void valkyrie_init_display(struct display *disp);
157static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix,
158	struct fb_info_valkyrie *p);
159static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
160
161static struct fb_ops valkyriefb_ops = {
162	owner:		THIS_MODULE,
163	fb_get_fix:	valkyrie_get_fix,
164	fb_get_var:	valkyrie_get_var,
165	fb_set_var:	valkyrie_set_var,
166	fb_get_cmap:	valkyrie_get_cmap,
167	fb_set_cmap:	valkyrie_set_cmap,
168};
169
170static int valkyriefb_getcolreg(u_int regno, u_int *red, u_int *green,
171			     u_int *blue, u_int *transp, struct fb_info *info);
172static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
173			     u_int transp, struct fb_info *info);
174static void do_install_cmap(int con, struct fb_info *info);
175
176static int valkyrie_get_fix(struct fb_fix_screeninfo *fix, int con,
177			 struct fb_info *info)
178{
179	struct fb_info_valkyrie *cp = (struct fb_info_valkyrie *) info;
180
181	*fix = cp->fix;
182	return 0;
183}
184
185static int valkyrie_get_var(struct fb_var_screeninfo *var, int con,
186			 struct fb_info *info)
187{
188	struct fb_info_valkyrie *cp = (struct fb_info_valkyrie *) info;
189
190	*var = cp->var;
191	return 0;
192}
193
194/* Sets everything according to var */
195static int valkyrie_set_var(struct fb_var_screeninfo *var, int con,
196			 struct fb_info *info)
197{
198	struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
199	struct display *disp;
200	struct fb_par_valkyrie par;
201	int depthchange, err;
202
203	disp = (con >= 0) ? &fb_display[con] : &p->disp;
204	if ((err = valkyrie_var_to_par(var, &par, info))) {
205		 /* printk (KERN_ERR "Error in valkyrie_set_var, calling valkyrie_var_to_par: %d.\n", err); */
206		return err;
207	}
208
209	if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW) {
210		/* printk(KERN_ERR "Not activating, in valkyrie_set_var.\n"); */
211		valkyrie_par_to_var(&par, var);
212		return 0;
213	}
214
215	/*
216	 * I know, we want to use fb_display[con], but grab certain info
217	 * from p->var instead.
218	 */
219#define DIRTY(x) (p->var.x != var->x)
220	depthchange = DIRTY(bits_per_pixel);
221	/* adding "&& !DIRTY(pixclock)" corrects vmode-switching problems */
222	if(!DIRTY(xres) && !DIRTY(yres) && !DIRTY(xres_virtual) &&
223	   !DIRTY(yres_virtual) && !DIRTY(bits_per_pixel) && !DIRTY(pixclock)) {
224	   	valkyrie_par_to_var(&par, var);
225		p->var = disp->var = *var;
226		return 0;
227	}
228
229	p->par = par;
230	valkyrie_par_to_var(&par, var);
231	p->var = *var;
232	valkyrie_par_to_fix(&par, &p->fix, p);
233	valkyrie_par_to_display(&par, disp, &p->fix, p);
234	p->disp = *disp;
235
236	if (info->changevar && !switching) {
237		/* Don't want to do this if just switching consoles. */
238		(*info->changevar)(con);
239	}
240	if (con == currcon)
241		valkyrie_set_par(&par, p);
242	if (depthchange)
243		if ((err = fb_alloc_cmap(&disp->cmap, 0, 0)))
244			return err;
245	if (depthchange || switching)
246		do_install_cmap(con, info);
247	return 0;
248}
249
250static int valkyrie_get_cmap(struct fb_cmap *cmap, int kspc, int con,
251			  struct fb_info *info)
252{
253	if (con == currcon)	{
254		/* current console? */
255		return fb_get_cmap(cmap, kspc, valkyriefb_getcolreg, info);
256	}
257	if (fb_display[con].cmap.len) { /* non default colormap? */
258		fb_copy_cmap(&fb_display[con].cmap, cmap, kspc? 0: 2);
259	}
260	else {
261		int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
262		fb_copy_cmap(fb_default_cmap(size), cmap, kspc ? 0 : 2);
263	}
264	return 0;
265}
266
267static int valkyrie_set_cmap(struct fb_cmap *cmap, int kspc, int con,
268			 struct fb_info *info)
269{
270	struct display *disp = &fb_display[con];
271	int err;
272
273	if (disp->cmap.len == 0) {
274		int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
275		err = fb_alloc_cmap(&disp->cmap, size, 0);
276		if (err) {
277			return err;
278		}
279	}
280
281	if (con == currcon) {
282		return fb_set_cmap(cmap, kspc, valkyriefb_setcolreg, info);
283	}
284	fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
285	return 0;
286}
287
288static int valkyriefb_switch(int con, struct fb_info *fb)
289{
290	struct fb_info_valkyrie *info = (struct fb_info_valkyrie *) fb;
291	struct fb_par_valkyrie par;
292
293	if (fb_display[currcon].cmap.len)
294		fb_get_cmap(&fb_display[currcon].cmap, 1, valkyriefb_getcolreg,
295			    fb);
296	currcon = con;
297	valkyrie_var_to_par(&fb_display[currcon].var, &par, fb);
298	valkyrie_set_par(&par, info);
299	do_install_cmap(con, fb);
300	return 0;
301}
302
303static int valkyriefb_updatevar(int con, struct fb_info *info)
304{
305	return 0;
306}
307
308static void valkyriefb_blank(int blank_mode, struct fb_info *info)
309{
310/*
311 *  Blank the screen if blank_mode != 0, else unblank. If blank_mode == NULL
312 *  then the caller blanks by setting the CLUT (Color Look Up Table) to all
313 *  black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
314 *  to e.g. a video mode which doesn't support it. Implements VESA suspend
315 *  and powerdown modes on hardware that supports disabling hsync/vsync:
316 *    blank_mode == 2: suspend vsync
317 *    blank_mode == 3: suspend hsync
318 *    blank_mode == 4: powerdown
319 */
320	struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
321	struct valkyrie_regvals	*init;
322	unsigned char vmode;
323
324	if (p->disp.can_soft_blank
325	 && ((vmode = p->par.vmode) > 0)
326	 && (vmode <= VMODE_MAX)
327	 && ((init = valkyrie_reg_init[vmode - 1]) != NULL)) {
328		if (blank_mode)
329			--blank_mode;
330		switch (blank_mode) {
331		default:	/* unblank */
332			out_8(&p->valkyrie_regs->mode.r, init->mode);
333			break;
334		case VESA_VSYNC_SUSPEND:
335		case VESA_HSYNC_SUSPEND:
336			/*
337			 * [kps] Value extracted from MacOS. I don't know
338			 * whether this bit disables hsync or vsync, or
339			 * whether the hardware can do the other as well.
340			 */
341			out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
342			break;
343		case VESA_POWERDOWN:
344			out_8(&p->valkyrie_regs->mode.r, 0x66);
345			break;
346		}
347	}
348}
349
350static int valkyriefb_getcolreg(u_int regno, u_int *red, u_int *green,
351			     u_int *blue, u_int *transp, struct fb_info *info)
352{
353	struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
354
355	if (regno > 255)
356		return 1;
357	*red = (p->palette[regno].red<<8) | p->palette[regno].red;
358	*green = (p->palette[regno].green<<8) | p->palette[regno].green;
359	*blue = (p->palette[regno].blue<<8) | p->palette[regno].blue;
360
361	return 0;
362}
363
364static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
365			     u_int transp, struct fb_info *info)
366{
367	struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
368	volatile struct cmap_regs *cmap_regs = p->cmap_regs;
369
370
371	if (regno > 255)
372		return 1;
373	red >>= 8;
374	green >>= 8;
375	blue >>= 8;
376	p->palette[regno].red = red;
377	p->palette[regno].green = green;
378	p->palette[regno].blue = blue;
379
380	/* tell clut which address to fill */
381	out_8(&p->cmap_regs->addr, regno);
382	udelay(1);
383	/* send one color channel at a time */
384	out_8(&cmap_regs->lut, red);
385	out_8(&cmap_regs->lut, green);
386	out_8(&cmap_regs->lut, blue);
387
388	if (regno < 16) {
389#ifdef FBCON_HAS_CFB16
390		p->fbcon_cfb16_cmap[regno] = (regno << 10) | (regno << 5) | regno;
391#endif
392	}
393
394	return 0;
395}
396
397static void do_install_cmap(int con, struct fb_info *info)
398{
399	if (con != currcon)
400		return;
401	if (fb_display[con].cmap.len) {
402		fb_set_cmap(&fb_display[con].cmap, 1, valkyriefb_setcolreg,
403			    info);
404	}
405	else {
406		int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
407		fb_set_cmap(fb_default_cmap(size), 1, valkyriefb_setcolreg,
408			    info);
409	}
410}
411
412#ifdef CONFIG_FB_COMPAT_XPMAC
413extern struct vc_mode display_info;
414extern struct fb_info *console_fb_info;
415#endif /* CONFIG_FB_COMPAT_XPMAC */
416
417static int valkyrie_vram_reqd(int video_mode, int color_mode)
418{
419	int pitch;
420
421	if ((pitch = valkyrie_reg_init[video_mode-1]->pitch[color_mode]) == 0)
422		pitch = 2 * valkyrie_reg_init[video_mode-1]->pitch[0];
423	return valkyrie_reg_init[video_mode-1]->vres * pitch;
424}
425
426static void set_valkyrie_clock(unsigned char *params)
427{
428	struct adb_request req;
429	int i;
430
431#ifdef CONFIG_ADB_CUDA
432	for (i = 0; i < 3; ++i) {
433		cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
434			     0x50, i + 1, params[i]);
435		while (!req.complete)
436			cuda_poll();
437	}
438#endif
439}
440
441static void __init init_valkyrie(struct fb_info_valkyrie *p)
442{
443	struct fb_par_valkyrie *par = &p->par;
444	struct fb_var_screeninfo var;
445	int j, k;
446
447	p->sense = read_valkyrie_sense(p);
448	printk(KERN_INFO "Monitor sense value = 0x%x, ", p->sense);
449
450#ifdef CONFIG_NVRAM
451	/* Try to pick a video mode out of NVRAM if we have one. */
452	if (default_vmode == VMODE_NVRAM) {
453		default_vmode = nvram_read_byte(NV_VMODE);
454		if (default_vmode <= 0
455		 || default_vmode > VMODE_MAX
456		 || !valkyrie_reg_init[default_vmode - 1])
457			default_vmode = VMODE_CHOOSE;
458	}
459	if (default_cmode == CMODE_NVRAM)
460		default_cmode = nvram_read_byte(NV_CMODE);
461#endif
462	if (default_vmode == VMODE_CHOOSE)
463		default_vmode = mac_map_monitor_sense(p->sense);
464	if (!valkyrie_reg_init[default_vmode - 1])
465		default_vmode = VMODE_640_480_67;
466
467	/*
468	 * Reduce the pixel size if we don't have enough VRAM or bandwitdh.
469	 */
470	if (default_cmode < CMODE_8
471	 || default_cmode > CMODE_16
472	 || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
473	 || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
474		default_cmode = CMODE_8;
475
476	printk(KERN_INFO "using video mode %d and color mode %d.\n", default_vmode, default_cmode);
477
478	mac_vmode_to_var(default_vmode, default_cmode, &var);
479	if (valkyrie_var_to_par(&var, par, &p->info)) {
480	    printk(KERN_ERR "valkyriefb: can't set default video mode\n");
481	    return ;
482	}
483
484	valkyrie_init_fix(&p->fix, p);
485	valkyrie_par_to_fix(&p->par, &p->fix, p);
486	valkyrie_par_to_var(&p->par, &p->var);
487	valkyrie_init_display(&p->disp);
488	valkyrie_par_to_display(&p->par, &p->disp, &p->fix, p);
489	valkyrie_init_info(&p->info, p);
490
491	/* Initialize colormap */
492	for (j = 0; j < 16; j++) {
493		k = color_table[j];
494		p->palette[j].red = default_red[k];
495		p->palette[j].green = default_grn[k];
496		p->palette[j].blue = default_blu[k];
497	}
498
499	valkyrie_set_var (&var, -1, &p->info);
500
501	if (register_framebuffer(&p->info) < 0) {
502		kfree(p);
503		return;
504	}
505
506	printk(KERN_INFO "fb%d: valkyrie frame buffer device\n", GET_FB_IDX(p->info.node));
507}
508
509static void valkyrie_set_par(const struct fb_par_valkyrie *par,
510			     struct fb_info_valkyrie *p)
511{
512	struct valkyrie_regvals	*init;
513	volatile struct valkyrie_regs *valkyrie_regs = p->valkyrie_regs;
514	int vmode, cmode;
515
516	vmode = par->vmode;
517	cmode = par->cmode;
518
519	if (vmode <= 0
520	 || vmode > VMODE_MAX
521	 || (init = valkyrie_reg_init[vmode - 1]) == NULL)
522		panic("valkyrie: display mode %d not supported", vmode);
523
524	/* Reset the valkyrie */
525	out_8(&valkyrie_regs->status.r, 0);
526	udelay(100);
527
528	/* Initialize display timing registers */
529	out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
530	out_8(&valkyrie_regs->depth.r, cmode + 3);
531	set_valkyrie_clock(init->clock_params);
532	udelay(100);
533
534	/* Turn on display */
535	out_8(&valkyrie_regs->mode.r, init->mode);
536
537#ifdef CONFIG_FB_COMPAT_XPMAC
538	/* And let the world know the truth. */
539	if (!console_fb_info || console_fb_info == &p->info) {
540		display_info.height = p->var.yres;
541		display_info.width = p->var.xres;
542		display_info.depth = (cmode == CMODE_16) ? 16 : 8;
543		display_info.pitch = p->fix.line_length;
544		display_info.mode = vmode;
545		strncpy(display_info.name, "valkyrie",
546			sizeof(display_info.name));
547		display_info.fb_address = p->frame_buffer_phys + 0x1000;
548		display_info.cmap_adr_address = p->cmap_regs_phys;
549		display_info.cmap_data_address = p->cmap_regs_phys + 8;
550		display_info.disp_reg_address = p->valkyrie_regs_phys;
551		console_fb_info = &p->info;
552	}
553#endif /* CONFIG_FB_COMPAT_XPMAC */
554}
555
556int __init valkyriefb_init(void)
557{
558	struct fb_info_valkyrie	*p;
559	unsigned long frame_buffer_phys, cmap_regs_phys, flags;
560
561#ifdef CONFIG_MAC
562	if (!MACH_IS_MAC)
563		return 0;
564	if (!(mac_bi_data.id == MAC_MODEL_Q630
565	      /* I'm not sure about this one */
566	    || mac_bi_data.id == MAC_MODEL_P588))
567		return 0;
568
569	/* Hardcoded addresses... welcome to 68k Macintosh country :-) */
570	frame_buffer_phys = 0xf9000000;
571	cmap_regs_phys = 0x50f24000;
572	flags = IOMAP_NOCACHE_SER; /* IOMAP_WRITETHROUGH?? */
573#else /* ppc (!CONFIG_MAC) */
574	struct device_node *dp;
575
576	dp = find_devices("valkyrie");
577	if (dp == 0)
578		return 0;
579
580	if(dp->n_addrs != 1) {
581		printk(KERN_ERR "expecting 1 address for valkyrie (got %d)", dp->n_addrs);
582		return 0;
583	}
584
585	frame_buffer_phys = dp->addrs[0].address;
586	cmap_regs_phys = dp->addrs[0].address+0x304000;
587	flags = _PAGE_WRITETHRU;
588#endif /* ppc (!CONFIG_MAC) */
589
590	p = kmalloc(sizeof(*p), GFP_ATOMIC);
591	if (p == 0)
592		return 0;
593	memset(p, 0, sizeof(*p));
594
595	/* Map in frame buffer and registers */
596	if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
597		kfree(p);
598		return 0;
599	}
600	p->total_vram = 0x100000;
601	p->frame_buffer_phys  = frame_buffer_phys;
602	p->frame_buffer = __ioremap(frame_buffer_phys, p->total_vram, flags);
603	p->cmap_regs_phys = cmap_regs_phys;
604	p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
605	p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
606	p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
607	init_valkyrie(p);
608	return 0;
609}
610
611/*
612 * Get the monitor sense value.
613 */
614static int read_valkyrie_sense(struct fb_info_valkyrie *p)
615{
616	int sense, in;
617
618    out_8(&p->valkyrie_regs->msense.r, 0);   /* release all lines */
619    __delay(20000);
620    sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
621    /* drive each sense line low in turn and collect the other 2 */
622    out_8(&p->valkyrie_regs->msense.r, 4);   /* drive A low */
623    __delay(20000);
624    sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
625    out_8(&p->valkyrie_regs->msense.r, 2);   /* drive B low */
626    __delay(20000);
627    sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
628	sense |= (in & 0x10) >> 2;
629    out_8(&p->valkyrie_regs->msense.r, 1);   /* drive C low */
630    __delay(20000);
631    sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
632
633    out_8(&p->valkyrie_regs->msense.r, 7);
634
635	return sense;
636}
637
638/*
639 * This routine takes a user-supplied var,
640 * and picks the best vmode/cmode from it.
641 */
642static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
643	struct fb_par_valkyrie *par, const struct fb_info *fb_info)
644
645/* [bkn] I did a major overhaul of this function.
646 *
647 * Much of the old code was "swiped by jonh from atyfb.c". Because
648 * macmodes has mac_var_to_vmode, I felt that it would be better to
649 * rework this function to use that, instead of reinventing the wheel to
650 * add support for vmode 17. This was reinforced by the fact that
651 * the previously swiped atyfb.c code is no longer there.
652 *
653 * So, I swiped and adapted platinum_var_to_par (from platinumfb.c), replacing
654 * most, but not all, of the old code in the process. One side benefit of
655 * swiping the platinumfb code is that we now have more comprehensible error
656 * messages when a vmode/cmode switch fails. (Most of the error messages are
657 * platinumfb.c, but I added two of my own, and I also changed some commas
658 * into colons to make the messages more consistent with other Linux error
659 * messages.) In addition, I think the new code *might* fix some vmode-
660 * switching oddities, but I'm not sure.
661 *
662 * There may be some more opportunities for cleanup in here, but this is a
663 * good start...
664 */
665
666{
667	int bpp = var->bits_per_pixel;
668	struct valkyrie_regvals *init;
669	struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) fb_info;
670
671
672	if(mac_var_to_vmode(var, &par->vmode, &par->cmode) != 0) {
673		printk(KERN_ERR "valkyrie_var_to_par: %dx%dx%d unsuccessful.\n",var->xres,var->yres,var->bits_per_pixel);
674		return -EINVAL;
675	}
676
677	/* Check if we know about the wanted video mode */
678	if(!valkyrie_reg_init[par->vmode-1]) {
679		printk(KERN_ERR "valkyrie_var_to_par: vmode %d not valid.\n", par->vmode);
680		return -EINVAL;
681	}
682
683	par->xres = var->xres;
684	par->yres = var->yres;
685	par->xoffset = 0;
686	par->yoffset = 0;
687	par->vxres = par->xres;
688	par->vyres = par->yres;
689
690	if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
691		|| var->xoffset != 0 || var->yoffset != 0) {
692		return -EINVAL;
693	}
694
695	if (bpp <= 8)
696		par->cmode = CMODE_8;
697	else if (bpp <= 16)
698		par->cmode = CMODE_16;
699	else {
700		printk(KERN_ERR "valkyrie_var_to_par: cmode %d not supported.\n", par->cmode);
701		return -EINVAL;
702	}
703
704	init = valkyrie_reg_init[par->vmode-1];
705	if (init->pitch[par->cmode] == 0) {
706		printk(KERN_ERR "valkyrie_var_to_par: vmode %d does not support cmode %d.\n", par->vmode, par->cmode);
707		return -EINVAL;
708	}
709
710	if (valkyrie_vram_reqd(par->vmode, par->cmode) > p->total_vram) {
711		printk(KERN_ERR "valkyrie_var_to_par: not enough ram for vmode %d, cmode %d.\n", par->vmode, par->cmode);
712		return -EINVAL;
713	}
714
715	return 0;
716}
717
718static int valkyrie_par_to_var(struct fb_par_valkyrie *par, struct fb_var_screeninfo *var)
719{
720	return mac_vmode_to_var(par->vmode, par->cmode, var);
721}
722
723static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
724{
725	memset(fix, 0, sizeof(*fix));
726	strcpy(fix->id, "valkyrie");
727	fix->mmio_start = p->valkyrie_regs_phys;
728	fix->mmio_len = sizeof(struct valkyrie_regs);
729	fix->type = FB_TYPE_PACKED_PIXELS;
730
731	fix->type_aux = 0;
732	fix->ywrapstep = 0;
733	fix->ypanstep = 0;
734	fix->xpanstep = 0;
735
736}
737
738/* Fix must already be inited above */
739static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
740	struct fb_fix_screeninfo *fix,
741	struct fb_info_valkyrie *p)
742{
743	fix->smem_start = p->frame_buffer_phys + 0x1000;
744	fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
745	fix->visual = (par->cmode == CMODE_8) ?
746		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
747	fix->line_length = par->vxres << par->cmode;
748		/* ywrapstep, xpanstep, ypanstep */
749}
750
751static void valkyrie_init_display(struct display *disp)
752{
753	memset(disp, 0, sizeof(*disp));
754	disp->type = /* fix->type */ FB_TYPE_PACKED_PIXELS;
755	disp->can_soft_blank = can_soft_blank;
756	disp->scrollmode = SCROLL_YREDRAW;
757}
758
759static void valkyrie_par_to_display(struct fb_par_valkyrie *par,
760  struct display *disp, struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
761{
762	disp->var = p->var;
763	disp->screen_base = (char *) p->frame_buffer + 0x1000;
764	disp->visual = fix->visual;
765	disp->line_length = fix->line_length;
766
767	if(disp->scrollmode != SCROLL_YREDRAW) {
768		printk(KERN_ERR "Scroll mode not YREDRAW in valkyrie_par_to_display\n");
769		disp->scrollmode = SCROLL_YREDRAW;
770	}
771	switch (par->cmode) {
772#ifdef FBCON_HAS_CFB8
773                case CMODE_8:
774                        disp->dispsw = &fbcon_cfb8;
775                        break;
776#endif
777#ifdef FBCON_HAS_CFB16
778                case CMODE_16:
779                        disp->dispsw = &fbcon_cfb16;
780                        disp->dispsw_data = p->fbcon_cfb16_cmap;
781                        break;
782#endif
783                default:
784                        disp->dispsw = &fbcon_dummy;
785                        break;
786        }
787}
788
789static void __init valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p)
790{
791	strcpy(info->modename, p->fix.id);
792	info->node = -1;	/* ??? danj */
793	info->fbops = &valkyriefb_ops;
794	info->disp = &p->disp;
795	strcpy(info->fontname, fontname);
796	info->changevar = NULL;
797	info->switch_con = &valkyriefb_switch;
798	info->updatevar = &valkyriefb_updatevar;
799	info->blank = &valkyriefb_blank;
800	info->flags = FBINFO_FLAG_DEFAULT;
801}
802
803
804/*
805 * Parse user speficied options (`video=valkyriefb:')
806 */
807int __init valkyriefb_setup(char *options)
808{
809	char *this_opt;
810
811	if (!options || !*options)
812		return 0;
813
814	while ((this_opt = strsep(&options, ",")) != NULL) {
815		if (!strncmp(this_opt, "font:", 5)) {
816			char *p;
817			int i;
818
819			p = this_opt + 5;
820			for (i = 0; i < sizeof(fontname) - 1; i++)
821				if (!*p || *p == ' ' || *p == ',')
822					break;
823			memcpy(fontname, this_opt + 5, i);
824			fontname[i] = 0;
825		}
826		else if (!strncmp(this_opt, "vmode:", 6)) {
827	    		int vmode = simple_strtoul(this_opt+6, NULL, 0);
828	    	if (vmode > 0 && vmode <= VMODE_MAX)
829				default_vmode = vmode;
830		}
831		else if (!strncmp(this_opt, "cmode:", 6)) {
832			int depth = simple_strtoul(this_opt+6, NULL, 0);
833			switch (depth) {
834			 case 8:
835			    default_cmode = CMODE_8;
836			    break;
837			 case 15:
838			 case 16:
839			    default_cmode = CMODE_16;
840			    break;
841			}
842		}
843		else if (!strncmp(this_opt, "noblank", 7)) {
844			can_soft_blank = 0;
845		}
846		else if (!strncmp(this_opt, "blank", 5)) {
847			can_soft_blank = 1;
848		}
849	}
850	return 0;
851}
852
853MODULE_LICENSE("GPL");
854