xref: /linux/drivers/video/fbdev/atafb.c (revision f0486dd91e27d2eaf62abbd0c2af73a9c04eac48)
1 /*
2  * linux/drivers/video/atafb.c -- Atari builtin chipset frame buffer device
3  *
4  *  Copyright (C) 1994 Martin Schaller & Roman Hodek
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file COPYING in the main directory of this archive
8  * for more details.
9  *
10  * History:
11  *   - 03 Jan 95: Original version by Martin Schaller: The TT driver and
12  *                all the device independent stuff
13  *   - 09 Jan 95: Roman: I've added the hardware abstraction (hw_switch)
14  *                and wrote the Falcon, ST(E), and External drivers
15  *                based on the original TT driver.
16  *   - 07 May 95: Martin: Added colormap operations for the external driver
17  *   - 21 May 95: Martin: Added support for overscan
18  *		  Andreas: some bug fixes for this
19  *   -    Jul 95: Guenther Kelleter <guenther@pool.informatik.rwth-aachen.de>:
20  *                Programmable Falcon video modes
21  *                (thanks to Christian Cartus for documentation
22  *                of VIDEL registers).
23  *   - 27 Dec 95: Guenther: Implemented user definable video modes "user[0-7]"
24  *                on minor 24...31. "user0" may be set on commandline by
25  *                "R<x>;<y>;<depth>". (Makes sense only on Falcon)
26  *                Video mode switch on Falcon now done at next VBL interrupt
27  *                to avoid the annoying right shift of the screen.
28  *   - 23 Sep 97: Juergen: added xres_virtual for cards like ProMST
29  *                The external-part is legacy, therefore hardware-specific
30  *                functions like panning/hardwarescrolling/blanking isn't
31  *				  supported.
32  *   - 29 Sep 97: Juergen: added Romans suggestion for pan_display
33  *				  (var->xoffset was changed even if no set_screen_base avail.)
34  *	 - 05 Oct 97: Juergen: extfb (PACKED_PIXEL) is FB_PSEUDOCOLOR 'cause
35  *				  we know how to set the colors
36  *				  ext_*palette: read from ext_colors (former MV300_colors)
37  *							    write to ext_colors and RAMDAC
38  *
39  * To do:
40  *   - For the Falcon it is not possible to set random video modes on
41  *     SM124 and SC/TV, only the bootup resolution is supported.
42  *
43  */
44 
45 #define ATAFB_TT
46 #define ATAFB_STE
47 #define ATAFB_EXT
48 #define ATAFB_FALCON
49 
50 #include <linux/kernel.h>
51 #include <linux/errno.h>
52 #include <linux/string.h>
53 #include <linux/mm.h>
54 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/interrupt.h>
57 #include <linux/platform_device.h>
58 
59 #include <asm/setup.h>
60 #include <linux/uaccess.h>
61 #include <asm/irq.h>
62 #include <asm/io.h>
63 
64 #include <asm/atarihw.h>
65 #include <asm/atariints.h>
66 #include <asm/atari_stram.h>
67 
68 #include <linux/fb.h>
69 #include <asm/atarikb.h>
70 
71 #include "c2p.h"
72 #include "atafb.h"
73 
74 #define SWITCH_ACIA 0x01		/* modes for switch on OverScan */
75 #define SWITCH_SND6 0x40
76 #define SWITCH_SND7 0x80
77 #define SWITCH_NONE 0x00
78 
79 
80 static int default_par;		/* default resolution (0=none) */
81 
82 static unsigned long default_mem_req;
83 
84 static int hwscroll = -1;
85 
86 static int use_hwscroll = 1;
87 
88 static int sttt_xres = 640, st_yres = 400, tt_yres = 480;
89 static int sttt_xres_virtual = 640, sttt_yres_virtual = 400;
90 static int ovsc_offset, ovsc_addlen;
91 
92 	/*
93 	 * Hardware parameters for current mode
94 	 */
95 
96 static struct atafb_par {
97 	void *screen_base;
98 	int yres_virtual;
99 	u_long next_line;
100 #if defined ATAFB_TT || defined ATAFB_STE
101 	union {
102 		struct {
103 			int mode;
104 			int sync;
105 		} tt, st;
106 #endif
107 #ifdef ATAFB_FALCON
108 		struct falcon_hw {
109 			/* Here are fields for storing a video mode, as direct
110 			 * parameters for the hardware.
111 			 */
112 			short sync;
113 			short line_width;
114 			short line_offset;
115 			short st_shift;
116 			short f_shift;
117 			short vid_control;
118 			short vid_mode;
119 			short xoffset;
120 			short hht, hbb, hbe, hdb, hde, hss;
121 			short vft, vbb, vbe, vdb, vde, vss;
122 			/* auxiliary information */
123 			short mono;
124 			short ste_mode;
125 			short bpp;
126 			u32 pseudo_palette[16];
127 		} falcon;
128 #endif
129 		/* Nothing needed for external mode */
130 	} hw;
131 } current_par;
132 
133 /* Don't calculate an own resolution, and thus don't change the one found when
134  * booting (currently used for the Falcon to keep settings for internal video
135  * hardware extensions (e.g. ScreenBlaster)  */
136 static int DontCalcRes = 0;
137 
138 #ifdef ATAFB_FALCON
139 #define HHT hw.falcon.hht
140 #define HBB hw.falcon.hbb
141 #define HBE hw.falcon.hbe
142 #define HDB hw.falcon.hdb
143 #define HDE hw.falcon.hde
144 #define HSS hw.falcon.hss
145 #define VFT hw.falcon.vft
146 #define VBB hw.falcon.vbb
147 #define VBE hw.falcon.vbe
148 #define VDB hw.falcon.vdb
149 #define VDE hw.falcon.vde
150 #define VSS hw.falcon.vss
151 #define VCO_CLOCK25		0x04
152 #define VCO_CSYPOS		0x10
153 #define VCO_VSYPOS		0x20
154 #define VCO_HSYPOS		0x40
155 #define VCO_SHORTOFFS	0x100
156 #define VMO_DOUBLE		0x01
157 #define VMO_INTER		0x02
158 #define VMO_PREMASK		0x0c
159 #endif
160 
161 static void *screen_base;	/* base address of screen */
162 static unsigned long phys_screen_base;	/* (only for Overscan) */
163 
164 static int screen_len;
165 
166 static int current_par_valid;
167 
168 static int mono_moni;
169 
170 /* monspecs passed by user */
171 
172 static __u32 mcap_hmin;		/* hfreq lower limit (Hz) */
173 static __u32 mcap_hmax;		/* hfreq upper limit (Hz) */
174 static __u16 mcap_vmin;		/* vfreq lower limit (Hz) */
175 static __u16 mcap_vmax;		/* vfreq upper limit (Hz) */
176 
177 #ifdef ATAFB_EXT
178 
179 /* external video handling */
180 static unsigned int external_xres;
181 static unsigned int external_xres_virtual;
182 static unsigned int external_yres;
183 
184 /*
185  * not needed - atafb will never support panning/hardwarescroll with external
186  * static unsigned int external_yres_virtual;
187  */
188 static unsigned int external_depth;
189 static int external_pmode;
190 static void *external_screen_base;
191 static unsigned long external_addr;
192 static unsigned long external_len;
193 static unsigned long external_vgaiobase;
194 static unsigned int external_bitspercol = 6;
195 
196 /*
197  * JOE <joe@amber.dinoco.de>:
198  * added card type for external driver, is only needed for
199  * colormap handling.
200  */
201 enum cardtype { IS_VGA, IS_MV300 };
202 static enum cardtype external_card_type = IS_VGA;
203 
204 /*
205  * The MV300 mixes the color registers. So we need an array of munged
206  * indices in order to access the correct reg.
207  */
208 static int MV300_reg_1bit[2] = {
209 	0, 1
210 };
211 static int MV300_reg_4bit[16] = {
212 	0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
213 };
214 static int MV300_reg_8bit[256] = {
215 	0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
216 	8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
217 	4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
218 	12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
219 	2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
220 	10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
221 	6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
222 	14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
223 	1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
224 	9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
225 	5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
226 	13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
227 	3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
228 	11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
229 	7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
230 	15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
231 };
232 
233 static int *MV300_reg = MV300_reg_8bit;
234 #endif /* ATAFB_EXT */
235 
236 
237 /*
238  * struct fb_ops {
239  *	* open/release and usage marking
240  *	struct module *owner;
241  *	int (*fb_open)(struct fb_info *info, int user);
242  *	int (*fb_release)(struct fb_info *info, int user);
243  *
244  *	* For framebuffers with strange non linear layouts or that do not
245  *	* work with normal memory mapped access
246  *	ssize_t (*fb_read)(struct file *file, char __user *buf, size_t count, loff_t *ppos);
247  *	ssize_t (*fb_write)(struct file *file, const char __user *buf, size_t count, loff_t *ppos);
248  *
249  *	* checks var and eventually tweaks it to something supported,
250  *	* DOES NOT MODIFY PAR *
251  *	int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
252  *
253  *	* set the video mode according to info->var *
254  *	int (*fb_set_par)(struct fb_info *info);
255  *
256  *	* set color register *
257  *	int (*fb_setcolreg)(unsigned int regno, unsigned int red, unsigned int green,
258  *			    unsigned int blue, unsigned int transp, struct fb_info *info);
259  *
260  *	* set color registers in batch *
261  *	int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
262  *
263  *	* blank display *
264  *	int (*fb_blank)(int blank, struct fb_info *info);
265  *
266  *	* pan display *
267  *	int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
268  *
269  *	*** The meat of the drawing engine ***
270  *	* Draws a rectangle *
271  *	void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
272  *	* Copy data from area to another *
273  *	void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
274  *	* Draws a image to the display *
275  *	void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
276  *
277  *	* Draws cursor *
278  *	int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
279  *
280  *	* wait for blit idle, optional *
281  *	int (*fb_sync)(struct fb_info *info);
282  *
283  *	* perform fb specific ioctl (optional) *
284  *	int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
285  *			unsigned long arg);
286  *
287  *	* Handle 32bit compat ioctl (optional) *
288  *	int (*fb_compat_ioctl)(struct fb_info *info, unsigned int cmd,
289  *			unsigned long arg);
290  *
291  *	* perform fb specific mmap *
292  *	int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
293  * } ;
294  */
295 
296 
297 /* ++roman: This structure abstracts from the underlying hardware (ST(e),
298  * TT, or Falcon.
299  *
300  * int (*detect)(struct fb_info *info)
301  *   This function should detect the current video mode settings and
302  *   store them in atafb_predefined[0] for later reference by the
303  *   user. Return the index+1 of an equivalent predefined mode or 0
304  *   if there is no such.
305  *
306  * int (*encode_fix)(struct fb_fix_screeninfo *fix,
307  *                   struct atafb_par *par)
308  *   This function should fill in the 'fix' structure based on the
309  *   values in the 'par' structure.
310  * !!! Obsolete, perhaps !!!
311  *
312  * int (*decode_var)(struct fb_info *info, struct fb_var_screeninfo *var,
313  *                   struct atafb_par *par)
314  *   Get the video params out of 'var'. If a value doesn't fit, round
315  *   it up, if it's too big, return EINVAL.
316  *   Round up in the following order: bits_per_pixel, xres, yres,
317  *   xres_virtual, yres_virtual, xoffset, yoffset, grayscale, bitfields,
318  *   horizontal timing, vertical timing.
319  *
320  * int (*encode_var)(struct fb_var_screeninfo *var,
321  *                   struct atafb_par *par);
322  *   Fill the 'var' structure based on the values in 'par' and maybe
323  *   other values read out of the hardware.
324  *
325  * void (*get_par)(struct atafb_par *par)
326  *   Fill the hardware's 'par' structure.
327  *   !!! Used only by detect() !!!
328  *
329  * void (*set_par)(struct atafb_par *par)
330  *   Set the hardware according to 'par'.
331  *
332  * void (*set_screen_base)(void *s_base)
333  *   Set the base address of the displayed frame buffer. Only called
334  *   if yres_virtual > yres or xres_virtual > xres.
335  *
336  * int (*blank)(int blank_mode)
337  *   Blank the screen if blank_mode != 0, else unblank. If blank == NULL then
338  *   the caller blanks by setting the CLUT to all black. Return 0 if blanking
339  *   succeeded, !=0 if un-/blanking failed due to e.g. a video mode which
340  *   doesn't support it. Implements VESA suspend and powerdown modes on
341  *   hardware that supports disabling hsync/vsync:
342  *       blank_mode == 2: suspend vsync, 3:suspend hsync, 4: powerdown.
343  */
344 
345 static struct fb_hwswitch {
346 	int (*detect)(struct fb_info *info);
347 	int (*encode_fix)(struct fb_fix_screeninfo *fix,
348 			  struct atafb_par *par);
349 	int (*decode_var)(struct fb_info *info, struct fb_var_screeninfo *var,
350 			  struct atafb_par *par);
351 	int (*encode_var)(struct fb_var_screeninfo *var,
352 			  struct atafb_par *par);
353 	void (*get_par)(struct atafb_par *par);
354 	void (*set_par)(struct atafb_par *par);
355 	void (*set_screen_base)(void *s_base);
356 	int (*blank)(int blank_mode);
357 	int (*pan_display)(struct fb_var_screeninfo *var,
358 			   struct fb_info *info);
359 } *fbhw;
360 
361 static char *autodetect_names[] = { "autodetect", NULL };
362 static char *stlow_names[] = { "stlow", NULL };
363 static char *stmid_names[] = { "stmid", "default5", NULL };
364 static char *sthigh_names[] = { "sthigh", "default4", NULL };
365 static char *ttlow_names[] = { "ttlow", NULL };
366 static char *ttmid_names[] = { "ttmid", "default1", NULL };
367 static char *tthigh_names[] = { "tthigh", "default2", NULL };
368 static char *vga2_names[] = { "vga2", NULL };
369 static char *vga4_names[] = { "vga4", NULL };
370 static char *vga16_names[] = { "vga16", "default3", NULL };
371 static char *vga256_names[] = { "vga256", NULL };
372 static char *falh2_names[] = { "falh2", NULL };
373 static char *falh16_names[] = { "falh16", NULL };
374 
375 static char **fb_var_names[] = {
376 	autodetect_names,
377 	stlow_names,
378 	stmid_names,
379 	sthigh_names,
380 	ttlow_names,
381 	ttmid_names,
382 	tthigh_names,
383 	vga2_names,
384 	vga4_names,
385 	vga16_names,
386 	vga256_names,
387 	falh2_names,
388 	falh16_names,
389 	NULL
390 };
391 
392 static struct fb_var_screeninfo atafb_predefined[] = {
393 	/*
394 	 * yres_virtual == 0 means use hw-scrolling if possible, else yres
395 	 */
396 	{ /* autodetect */
397 	  0, 0, 0, 0, 0, 0, 0, 0,		/* xres-grayscale */
398 	  {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},	/* red green blue tran*/
399 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
400 	{ /* st low */
401 	  320, 200, 320, 0, 0, 0, 4, 0,
402 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
403 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
404 	{ /* st mid */
405 	  640, 200, 640, 0, 0, 0, 2, 0,
406 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
407 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
408 	{ /* st high */
409 	  640, 400, 640, 0, 0, 0, 1, 0,
410 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
411 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
412 	{ /* tt low */
413 	  320, 480, 320, 0, 0, 0, 8, 0,
414 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
415 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
416 	{ /* tt mid */
417 	  640, 480, 640, 0, 0, 0, 4, 0,
418 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
419 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
420 	{ /* tt high */
421 	  1280, 960, 1280, 0, 0, 0, 1, 0,
422 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
423 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
424 	{ /* vga2 */
425 	  640, 480, 640, 0, 0, 0, 1, 0,
426 	  {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
427 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
428 	{ /* vga4 */
429 	  640, 480, 640, 0, 0, 0, 2, 0,
430 	  {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0},
431 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
432 	{ /* vga16 */
433 	  640, 480, 640, 0, 0, 0, 4, 0,
434 	  {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
435 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
436 	{ /* vga256 */
437 	  640, 480, 640, 0, 0, 0, 8, 0,
438 	  {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
439 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
440 	{ /* falh2 */
441 	  896, 608, 896, 0, 0, 0, 1, 0,
442 	  {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
443 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
444 	{ /* falh16 */
445 	  896, 608, 896, 0, 0, 0, 4, 0,
446 	  {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0},
447 	  0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0 },
448 };
449 
450 static int num_atafb_predefined = ARRAY_SIZE(atafb_predefined);
451 
452 static struct fb_videomode atafb_modedb[] __initdata = {
453 	/*
454 	 *  Atari Video Modes
455 	 *
456 	 *  If you change these, make sure to update DEFMODE_* as well!
457 	 */
458 
459 	/*
460 	 *  ST/TT Video Modes
461 	 */
462 
463 	{
464 		/* 320x200, 15 kHz, 60 Hz (ST low) */
465 		"st-low", 60, 320, 200, 32000, 32, 16, 31, 14, 96, 4,
466 		0, FB_VMODE_NONINTERLACED
467 	}, {
468 		/* 640x200, 15 kHz, 60 Hz (ST medium) */
469 		"st-mid", 60, 640, 200, 32000, 32, 16, 31, 14, 96, 4,
470 		0, FB_VMODE_NONINTERLACED
471 	}, {
472 		/* 640x400, 30.25 kHz, 63.5 Hz (ST high) */
473 		"st-high", 63, 640, 400, 32000, 128, 0, 40, 14, 128, 4,
474 		0, FB_VMODE_NONINTERLACED
475 	}, {
476 		/* 320x480, 15 kHz, 60 Hz (TT low) */
477 		"tt-low", 60, 320, 480, 31041, 120, 100, 8, 16, 140, 30,
478 		0, FB_VMODE_NONINTERLACED
479 	}, {
480 		/* 640x480, 29 kHz, 57 Hz (TT medium) */
481 		"tt-mid", 60, 640, 480, 31041, 120, 100, 8, 16, 140, 30,
482 		0, FB_VMODE_NONINTERLACED
483 	}, {
484 		/* 1280x960, 72 kHz, 72 Hz (TT high) */
485 		"tt-high", 72, 1280, 960, 7760, 260, 60, 36, 4, 192, 4,
486 		0, FB_VMODE_NONINTERLACED
487 	},
488 
489 	/*
490 	 *  VGA Video Modes
491 	 */
492 
493 	{
494 		/* 640x480, 31 kHz, 60 Hz (VGA) */
495 		"vga", 60, 640, 480, 39721, 42, 18, 31, 11, 100, 3,
496 		0, FB_VMODE_NONINTERLACED
497 	}, {
498 		/* 640x400, 31 kHz, 70 Hz (VGA) */
499 		"vga70", 70, 640, 400, 39721, 42, 18, 31, 11, 100, 3,
500 		FB_SYNC_VERT_HIGH_ACT | FB_SYNC_COMP_HIGH_ACT, FB_VMODE_NONINTERLACED
501 	},
502 
503 	/*
504 	 *  Falcon HiRes Video Modes
505 	 */
506 
507 	{
508 		/* 896x608, 31 kHz, 60 Hz (Falcon High) */
509 		"falh", 60, 896, 608, 32000, 18, 42, 31, 1, 96,3,
510 		0, FB_VMODE_NONINTERLACED
511 	},
512 };
513 
514 #define NUM_TOTAL_MODES  ARRAY_SIZE(atafb_modedb)
515 
516 static char *mode_option __initdata = NULL;
517 
518  /* default modes */
519 
520 #define DEFMODE_TT	5		/* "tt-high" for TT */
521 #define DEFMODE_F30	7		/* "vga70" for Falcon */
522 #define DEFMODE_STE	2		/* "st-high" for ST/E */
523 #define DEFMODE_EXT	6		/* "vga" for external */
524 
525 
526 static int get_video_mode(char *vname)
527 {
528 	char ***name_list;
529 	char **name;
530 	int i;
531 
532 	name_list = fb_var_names;
533 	for (i = 0; i < num_atafb_predefined; i++) {
534 		name = *name_list++;
535 		if (!name || !*name)
536 			break;
537 		while (*name) {
538 			if (!strcmp(vname, *name))
539 				return i + 1;
540 			name++;
541 		}
542 	}
543 	return 0;
544 }
545 
546 
547 
548 /* ------------------- TT specific functions ---------------------- */
549 
550 #ifdef ATAFB_TT
551 
552 static int tt_encode_fix(struct fb_fix_screeninfo *fix, struct atafb_par *par)
553 {
554 	int mode;
555 
556 	strcpy(fix->id, "Atari Builtin");
557 	fix->smem_start = phys_screen_base;
558 	fix->smem_len = screen_len;
559 	fix->type = FB_TYPE_INTERLEAVED_PLANES;
560 	fix->type_aux = 2;
561 	fix->visual = FB_VISUAL_PSEUDOCOLOR;
562 	mode = par->hw.tt.mode & TT_SHIFTER_MODEMASK;
563 	if (mode == TT_SHIFTER_TTHIGH || mode == TT_SHIFTER_STHIGH) {
564 		fix->type = FB_TYPE_PACKED_PIXELS;
565 		fix->type_aux = 0;
566 		if (mode == TT_SHIFTER_TTHIGH)
567 			fix->visual = FB_VISUAL_MONO01;
568 	}
569 	fix->xpanstep = 0;
570 	fix->ypanstep = 1;
571 	fix->ywrapstep = 0;
572 	fix->line_length = par->next_line;
573 	fix->accel = FB_ACCEL_ATARIBLITT;
574 	return 0;
575 }
576 
577 static int tt_decode_var(struct fb_info *info, struct fb_var_screeninfo *var, struct atafb_par *par)
578 {
579 	int xres = var->xres;
580 	int yres = var->yres;
581 	int bpp = var->bits_per_pixel;
582 	int linelen;
583 	int yres_virtual = var->yres_virtual;
584 
585 	if (mono_moni) {
586 		if (bpp > 1 || xres > sttt_xres * 2 || yres > tt_yres * 2)
587 			return -EINVAL;
588 		par->hw.tt.mode = TT_SHIFTER_TTHIGH;
589 		xres = sttt_xres * 2;
590 		yres = tt_yres * 2;
591 		bpp = 1;
592 	} else {
593 		if (bpp > 8 || xres > sttt_xres || yres > tt_yres)
594 			return -EINVAL;
595 		if (bpp > 4) {
596 			if (xres > sttt_xres / 2 || yres > tt_yres)
597 				return -EINVAL;
598 			par->hw.tt.mode = TT_SHIFTER_TTLOW;
599 			xres = sttt_xres / 2;
600 			yres = tt_yres;
601 			bpp = 8;
602 		} else if (bpp > 2) {
603 			if (xres > sttt_xres || yres > tt_yres)
604 				return -EINVAL;
605 			if (xres > sttt_xres / 2 || yres > st_yres / 2) {
606 				par->hw.tt.mode = TT_SHIFTER_TTMID;
607 				xres = sttt_xres;
608 				yres = tt_yres;
609 				bpp = 4;
610 			} else {
611 				par->hw.tt.mode = TT_SHIFTER_STLOW;
612 				xres = sttt_xres / 2;
613 				yres = st_yres / 2;
614 				bpp = 4;
615 			}
616 		} else if (bpp > 1) {
617 			if (xres > sttt_xres || yres > st_yres / 2)
618 				return -EINVAL;
619 			par->hw.tt.mode = TT_SHIFTER_STMID;
620 			xres = sttt_xres;
621 			yres = st_yres / 2;
622 			bpp = 2;
623 		} else if (var->xres > sttt_xres || var->yres > st_yres) {
624 			return -EINVAL;
625 		} else {
626 			par->hw.tt.mode = TT_SHIFTER_STHIGH;
627 			xres = sttt_xres;
628 			yres = st_yres;
629 			bpp = 1;
630 		}
631 	}
632 	if (yres_virtual <= 0)
633 		yres_virtual = 0;
634 	else if (yres_virtual < yres)
635 		yres_virtual = yres;
636 	if (var->sync & FB_SYNC_EXT)
637 		par->hw.tt.sync = 0;
638 	else
639 		par->hw.tt.sync = 1;
640 	linelen = xres * bpp / 8;
641 	if (yres_virtual * linelen > screen_len && screen_len)
642 		return -EINVAL;
643 	if (yres * linelen > screen_len && screen_len)
644 		return -EINVAL;
645 	if (var->yoffset + yres > yres_virtual && yres_virtual)
646 		return -EINVAL;
647 	par->yres_virtual = yres_virtual;
648 	par->screen_base = screen_base + var->yoffset * linelen;
649 	par->next_line = linelen;
650 	return 0;
651 }
652 
653 static int tt_encode_var(struct fb_var_screeninfo *var, struct atafb_par *par)
654 {
655 	int linelen;
656 	memset(var, 0, sizeof(struct fb_var_screeninfo));
657 	var->red.offset = 0;
658 	var->red.length = 4;
659 	var->red.msb_right = 0;
660 	var->grayscale = 0;
661 
662 	var->pixclock = 31041;
663 	var->left_margin = 120;		/* these may be incorrect */
664 	var->right_margin = 100;
665 	var->upper_margin = 8;
666 	var->lower_margin = 16;
667 	var->hsync_len = 140;
668 	var->vsync_len = 30;
669 
670 	var->height = -1;
671 	var->width = -1;
672 
673 	if (par->hw.tt.sync & 1)
674 		var->sync = 0;
675 	else
676 		var->sync = FB_SYNC_EXT;
677 
678 	switch (par->hw.tt.mode & TT_SHIFTER_MODEMASK) {
679 	case TT_SHIFTER_STLOW:
680 		var->xres = sttt_xres / 2;
681 		var->xres_virtual = sttt_xres_virtual / 2;
682 		var->yres = st_yres / 2;
683 		var->bits_per_pixel = 4;
684 		break;
685 	case TT_SHIFTER_STMID:
686 		var->xres = sttt_xres;
687 		var->xres_virtual = sttt_xres_virtual;
688 		var->yres = st_yres / 2;
689 		var->bits_per_pixel = 2;
690 		break;
691 	case TT_SHIFTER_STHIGH:
692 		var->xres = sttt_xres;
693 		var->xres_virtual = sttt_xres_virtual;
694 		var->yres = st_yres;
695 		var->bits_per_pixel = 1;
696 		break;
697 	case TT_SHIFTER_TTLOW:
698 		var->xres = sttt_xres / 2;
699 		var->xres_virtual = sttt_xres_virtual / 2;
700 		var->yres = tt_yres;
701 		var->bits_per_pixel = 8;
702 		break;
703 	case TT_SHIFTER_TTMID:
704 		var->xres = sttt_xres;
705 		var->xres_virtual = sttt_xres_virtual;
706 		var->yres = tt_yres;
707 		var->bits_per_pixel = 4;
708 		break;
709 	case TT_SHIFTER_TTHIGH:
710 		var->red.length = 0;
711 		var->xres = sttt_xres * 2;
712 		var->xres_virtual = sttt_xres_virtual * 2;
713 		var->yres = tt_yres * 2;
714 		var->bits_per_pixel = 1;
715 		break;
716 	}
717 	var->blue = var->green = var->red;
718 	var->transp.offset = 0;
719 	var->transp.length = 0;
720 	var->transp.msb_right = 0;
721 	linelen = var->xres_virtual * var->bits_per_pixel / 8;
722 	if (!use_hwscroll)
723 		var->yres_virtual = var->yres;
724 	else if (screen_len) {
725 		if (par->yres_virtual)
726 			var->yres_virtual = par->yres_virtual;
727 		else
728 			/* yres_virtual == 0 means use maximum */
729 			var->yres_virtual = screen_len / linelen;
730 	} else {
731 		if (hwscroll < 0)
732 			var->yres_virtual = 2 * var->yres;
733 		else
734 			var->yres_virtual = var->yres + hwscroll * 16;
735 	}
736 	var->xoffset = 0;
737 	if (screen_base)
738 		var->yoffset = (par->screen_base - screen_base) / linelen;
739 	else
740 		var->yoffset = 0;
741 	var->nonstd = 0;
742 	var->activate = 0;
743 	var->vmode = FB_VMODE_NONINTERLACED;
744 	return 0;
745 }
746 
747 static void tt_get_par(struct atafb_par *par)
748 {
749 	unsigned long addr;
750 	par->hw.tt.mode = shifter_tt.tt_shiftmode;
751 	par->hw.tt.sync = shifter_st.syncmode;
752 	addr = ((shifter_st.bas_hi & 0xff) << 16) |
753 	       ((shifter_st.bas_md & 0xff) << 8)  |
754 	       ((shifter_st.bas_lo & 0xff));
755 	par->screen_base = atari_stram_to_virt(addr);
756 }
757 
758 static void tt_set_par(struct atafb_par *par)
759 {
760 	shifter_tt.tt_shiftmode = par->hw.tt.mode;
761 	shifter_st.syncmode = par->hw.tt.sync;
762 	/* only set screen_base if really necessary */
763 	if (current_par.screen_base != par->screen_base)
764 		fbhw->set_screen_base(par->screen_base);
765 }
766 
767 static int tt_setcolreg(unsigned int regno, unsigned int red,
768 			unsigned int green, unsigned int blue,
769 			unsigned int transp, struct fb_info *info)
770 {
771 	if ((shifter_tt.tt_shiftmode & TT_SHIFTER_MODEMASK) == TT_SHIFTER_STHIGH)
772 		regno += 254;
773 	if (regno > 255)
774 		return 1;
775 	tt_palette[regno] = (((red >> 12) << 8) | ((green >> 12) << 4) |
776 			     (blue >> 12));
777 	if ((shifter_tt.tt_shiftmode & TT_SHIFTER_MODEMASK) ==
778 	    TT_SHIFTER_STHIGH && regno == 254)
779 		tt_palette[0] = 0;
780 	return 0;
781 }
782 
783 static int tt_detect(struct fb_info *info)
784 {
785 	struct atafb_par par;
786 
787 	/* Determine the connected monitor: The DMA sound must be
788 	 * disabled before reading the MFP GPIP, because the Sound
789 	 * Done Signal and the Monochrome Detect are XORed together!
790 	 *
791 	 * Even on a TT, we should look if there is a DMA sound. It was
792 	 * announced that the Eagle is TT compatible, but only the PCM is
793 	 * missing...
794 	 */
795 	if (ATARIHW_PRESENT(PCM_8BIT)) {
796 		tt_dmasnd.ctrl = DMASND_CTRL_OFF;
797 		udelay(20);		/* wait a while for things to settle down */
798 	}
799 	mono_moni = (st_mfp.par_dt_reg & 0x80) == 0;
800 
801 	tt_get_par(&par);
802 	tt_encode_var(&atafb_predefined[0], &par);
803 
804 	return 1;
805 }
806 
807 #endif /* ATAFB_TT */
808 
809 /* ------------------- Falcon specific functions ---------------------- */
810 
811 #ifdef ATAFB_FALCON
812 
813 static int mon_type;		/* Falcon connected monitor */
814 static int f030_bus_width;	/* Falcon ram bus width (for vid_control) */
815 #define F_MON_SM	0
816 #define F_MON_SC	1
817 #define F_MON_VGA	2
818 #define F_MON_TV	3
819 
820 static struct pixel_clock {
821 	unsigned long f;	/* f/[Hz] */
822 	unsigned long t;	/* t/[ps] (=1/f) */
823 	int right, hsync, left;	/* standard timing in clock cycles, not pixel */
824 	/* hsync initialized in falcon_detect() */
825 	int sync_mask;		/* or-mask for hw.falcon.sync to set this clock */
826 	int control_mask;	/* ditto, for hw.falcon.vid_control */
827 } f25 = {
828 	25175000, 39721, 18, 0, 42, 0x0, VCO_CLOCK25
829 }, f32 = {
830 	32000000, 31250, 18, 0, 42, 0x0, 0
831 }, fext = {
832 	0, 0, 18, 0, 42, 0x1, 0
833 };
834 
835 /* VIDEL-prescale values [mon_type][pixel_length from VCO] */
836 static int vdl_prescale[4][3] = {
837 	{ 4,2,1 }, { 4,2,1 }, { 4,2,2 }, { 4,2,1 }
838 };
839 
840 /* Default hsync timing [mon_type] in picoseconds */
841 static long h_syncs[4] = { 3000000, 4875000, 4000000, 4875000 };
842 
843 static inline int hxx_prescale(struct falcon_hw *hw)
844 {
845 	return hw->ste_mode ? 16
846 			    : vdl_prescale[mon_type][hw->vid_mode >> 2 & 0x3];
847 }
848 
849 static int falcon_encode_fix(struct fb_fix_screeninfo *fix,
850 			     struct atafb_par *par)
851 {
852 	strcpy(fix->id, "Atari Builtin");
853 	fix->smem_start = phys_screen_base;
854 	fix->smem_len = screen_len;
855 	fix->type = FB_TYPE_INTERLEAVED_PLANES;
856 	fix->type_aux = 2;
857 	fix->visual = FB_VISUAL_PSEUDOCOLOR;
858 	fix->xpanstep = 1;
859 	fix->ypanstep = 1;
860 	fix->ywrapstep = 0;
861 	if (par->hw.falcon.mono) {
862 		fix->type = FB_TYPE_PACKED_PIXELS;
863 		fix->type_aux = 0;
864 		/* no smooth scrolling with longword aligned video mem */
865 		fix->xpanstep = 32;
866 	} else if (par->hw.falcon.f_shift & 0x100) {
867 		fix->type = FB_TYPE_PACKED_PIXELS;
868 		fix->type_aux = 0;
869 		/* Is this ok or should it be DIRECTCOLOR? */
870 		fix->visual = FB_VISUAL_TRUECOLOR;
871 		fix->xpanstep = 2;
872 	}
873 	fix->line_length = par->next_line;
874 	fix->accel = FB_ACCEL_ATARIBLITT;
875 	return 0;
876 }
877 
878 static int falcon_decode_var(struct fb_info *info, struct fb_var_screeninfo *var,
879 			     struct atafb_par *par)
880 {
881 	int bpp = var->bits_per_pixel;
882 	int xres = var->xres;
883 	int yres = var->yres;
884 	int xres_virtual = var->xres_virtual;
885 	int yres_virtual = var->yres_virtual;
886 	int left_margin, right_margin, hsync_len;
887 	int upper_margin, lower_margin, vsync_len;
888 	int linelen;
889 	int interlace = 0, doubleline = 0;
890 	struct pixel_clock *pclock;
891 	int plen;			/* width of pixel in clock cycles */
892 	int xstretch;
893 	int prescale;
894 	int longoffset = 0;
895 	int hfreq, vfreq;
896 	int hdb_off, hde_off, base_off;
897 	int gstart, gend1, gend2, align;
898 
899 /*
900 	Get the video params out of 'var'. If a value doesn't fit, round
901 	it up, if it's too big, return EINVAL.
902 	Round up in the following order: bits_per_pixel, xres, yres,
903 	xres_virtual, yres_virtual, xoffset, yoffset, grayscale, bitfields,
904 	horizontal timing, vertical timing.
905 
906 	There is a maximum of screen resolution determined by pixelclock
907 	and minimum frame rate -- (X+hmarg.)*(Y+vmarg.)*vfmin <= pixelclock.
908 	In interlace mode this is     "     *    "     *vfmin <= pixelclock.
909 	Additional constraints: hfreq.
910 	Frequency range for multisync monitors is given via command line.
911 	For TV and SM124 both frequencies are fixed.
912 
913 	X % 16 == 0 to fit 8x?? font (except 1 bitplane modes must use X%32 == 0)
914 	Y % 16 == 0 to fit 8x16 font
915 	Y % 8 == 0 if Y<400
916 
917 	Currently interlace and doubleline mode in var are ignored.
918 	On SM124 and TV only the standard resolutions can be used.
919 */
920 
921 	/* Reject uninitialized mode */
922 	if (!xres || !yres || !bpp)
923 		return -EINVAL;
924 
925 	if (mon_type == F_MON_SM && bpp != 1)
926 		return -EINVAL;
927 
928 	if (bpp <= 1) {
929 		bpp = 1;
930 		par->hw.falcon.f_shift = 0x400;
931 		par->hw.falcon.st_shift = 0x200;
932 	} else if (bpp <= 2) {
933 		bpp = 2;
934 		par->hw.falcon.f_shift = 0x000;
935 		par->hw.falcon.st_shift = 0x100;
936 	} else if (bpp <= 4) {
937 		bpp = 4;
938 		par->hw.falcon.f_shift = 0x000;
939 		par->hw.falcon.st_shift = 0x000;
940 	} else if (bpp <= 8) {
941 		bpp = 8;
942 		par->hw.falcon.f_shift = 0x010;
943 	} else if (bpp <= 16) {
944 		bpp = 16;		/* packed pixel mode */
945 		par->hw.falcon.f_shift = 0x100;	/* hicolor, no overlay */
946 	} else
947 		return -EINVAL;
948 	par->hw.falcon.bpp = bpp;
949 
950 	if (mon_type == F_MON_SM || DontCalcRes) {
951 		/* Skip all calculations. VGA/TV/SC1224 only supported. */
952 		struct fb_var_screeninfo *myvar = &atafb_predefined[0];
953 
954 		if (bpp > myvar->bits_per_pixel ||
955 		    var->xres > myvar->xres ||
956 		    var->yres > myvar->yres)
957 			return -EINVAL;
958 		fbhw->get_par(par);	/* Current par will be new par */
959 		goto set_screen_base;	/* Don't forget this */
960 	}
961 
962 	/* Only some fixed resolutions < 640x400 */
963 	if (xres <= 320)
964 		xres = 320;
965 	else if (xres <= 640 && bpp != 16)
966 		xres = 640;
967 	if (yres <= 200)
968 		yres = 200;
969 	else if (yres <= 240)
970 		yres = 240;
971 	else if (yres <= 400)
972 		yres = 400;
973 
974 	/* 2 planes must use STE compatibility mode */
975 	par->hw.falcon.ste_mode = bpp == 2;
976 	par->hw.falcon.mono = bpp == 1;
977 
978 	/* Total and visible scanline length must be a multiple of one longword,
979 	 * this and the console fontwidth yields the alignment for xres and
980 	 * xres_virtual.
981 	 * TODO: this way "odd" fontheights are not supported
982 	 *
983 	 * Special case in STE mode: blank and graphic positions don't align,
984 	 * avoid trash at right margin
985 	 */
986 	if (par->hw.falcon.ste_mode)
987 		xres = (xres + 63) & ~63;
988 	else if (bpp == 1)
989 		xres = (xres + 31) & ~31;
990 	else
991 		xres = (xres + 15) & ~15;
992 	if (yres >= 400)
993 		yres = (yres + 15) & ~15;
994 	else
995 		yres = (yres + 7) & ~7;
996 
997 	if (xres_virtual < xres)
998 		xres_virtual = xres;
999 	else if (bpp == 1)
1000 		xres_virtual = (xres_virtual + 31) & ~31;
1001 	else
1002 		xres_virtual = (xres_virtual + 15) & ~15;
1003 
1004 	if (yres_virtual <= 0)
1005 		yres_virtual = 0;
1006 	else if (yres_virtual < yres)
1007 		yres_virtual = yres;
1008 
1009 	par->hw.falcon.line_width = bpp * xres / 16;
1010 	par->hw.falcon.line_offset = bpp * (xres_virtual - xres) / 16;
1011 
1012 	/* single or double pixel width */
1013 	xstretch = (xres < 640) ? 2 : 1;
1014 
1015 #if 0 /* SM124 supports only 640x400, this is rejected above */
1016 	if (mon_type == F_MON_SM) {
1017 		if (xres != 640 && yres != 400)
1018 			return -EINVAL;
1019 		plen = 1;
1020 		pclock = &f32;
1021 		/* SM124-mode is special */
1022 		par->hw.falcon.ste_mode = 1;
1023 		par->hw.falcon.f_shift = 0x000;
1024 		par->hw.falcon.st_shift = 0x200;
1025 		left_margin = hsync_len = 128 / plen;
1026 		right_margin = 0;
1027 		/* TODO set all margins */
1028 	} else
1029 #endif
1030 	if (mon_type == F_MON_SC || mon_type == F_MON_TV) {
1031 		plen = 2 * xstretch;
1032 		if (var->pixclock > f32.t * plen)
1033 			return -EINVAL;
1034 		pclock = &f32;
1035 		if (yres > 240)
1036 			interlace = 1;
1037 		if (var->pixclock == 0) {
1038 			/* set some minimal margins which center the screen */
1039 			left_margin = 32;
1040 			right_margin = 18;
1041 			hsync_len = pclock->hsync / plen;
1042 			upper_margin = 31;
1043 			lower_margin = 14;
1044 			vsync_len = interlace ? 3 : 4;
1045 		} else {
1046 			left_margin = var->left_margin;
1047 			right_margin = var->right_margin;
1048 			hsync_len = var->hsync_len;
1049 			upper_margin = var->upper_margin;
1050 			lower_margin = var->lower_margin;
1051 			vsync_len = var->vsync_len;
1052 			if (var->vmode & FB_VMODE_INTERLACED) {
1053 				upper_margin = (upper_margin + 1) / 2;
1054 				lower_margin = (lower_margin + 1) / 2;
1055 				vsync_len = (vsync_len + 1) / 2;
1056 			} else if (var->vmode & FB_VMODE_DOUBLE) {
1057 				upper_margin *= 2;
1058 				lower_margin *= 2;
1059 				vsync_len *= 2;
1060 			}
1061 		}
1062 	} else {			/* F_MON_VGA */
1063 		if (bpp == 16)
1064 			xstretch = 2;	/* Double pixel width only for hicolor */
1065 		/* Default values are used for vert./hor. timing if no pixelclock given. */
1066 		if (var->pixclock == 0) {
1067 			/* Choose master pixelclock depending on hor. timing */
1068 			plen = 1 * xstretch;
1069 			if ((plen * xres + f25.right + f25.hsync + f25.left) *
1070 			    info->monspecs.hfmin < f25.f)
1071 				pclock = &f25;
1072 			else if ((plen * xres + f32.right + f32.hsync +
1073 				  f32.left) * info->monspecs.hfmin < f32.f)
1074 				pclock = &f32;
1075 			else if ((plen * xres + fext.right + fext.hsync +
1076 				  fext.left) * info->monspecs.hfmin < fext.f &&
1077 			         fext.f)
1078 				pclock = &fext;
1079 			else
1080 				return -EINVAL;
1081 
1082 			left_margin = pclock->left / plen;
1083 			right_margin = pclock->right / plen;
1084 			hsync_len = pclock->hsync / plen;
1085 			upper_margin = 31;
1086 			lower_margin = 11;
1087 			vsync_len = 3;
1088 		} else {
1089 			/* Choose largest pixelclock <= wanted clock */
1090 			int i;
1091 			unsigned long pcl = ULONG_MAX;
1092 			pclock = NULL;
1093 			for (i = 1; i <= 4; i *= 2) {
1094 				if (f25.t * i >= var->pixclock &&
1095 				    f25.t * i < pcl) {
1096 					pcl = f25.t * i;
1097 					pclock = &f25;
1098 				}
1099 				if (f32.t * i >= var->pixclock &&
1100 				    f32.t * i < pcl) {
1101 					pcl = f32.t * i;
1102 					pclock = &f32;
1103 				}
1104 				if (fext.t && fext.t * i >= var->pixclock &&
1105 				    fext.t * i < pcl) {
1106 					pcl = fext.t * i;
1107 					pclock = &fext;
1108 				}
1109 			}
1110 			if (!pclock)
1111 				return -EINVAL;
1112 			plen = pcl / pclock->t;
1113 
1114 			left_margin = var->left_margin;
1115 			right_margin = var->right_margin;
1116 			hsync_len = var->hsync_len;
1117 			upper_margin = var->upper_margin;
1118 			lower_margin = var->lower_margin;
1119 			vsync_len = var->vsync_len;
1120 			/* Internal unit is [single lines per (half-)frame] */
1121 			if (var->vmode & FB_VMODE_INTERLACED) {
1122 				/* # lines in half frame */
1123 				/* External unit is [lines per full frame] */
1124 				upper_margin = (upper_margin + 1) / 2;
1125 				lower_margin = (lower_margin + 1) / 2;
1126 				vsync_len = (vsync_len + 1) / 2;
1127 			} else if (var->vmode & FB_VMODE_DOUBLE) {
1128 				/* External unit is [double lines per frame] */
1129 				upper_margin *= 2;
1130 				lower_margin *= 2;
1131 				vsync_len *= 2;
1132 			}
1133 		}
1134 		if (pclock == &fext)
1135 			longoffset = 1;	/* VIDEL doesn't synchronize on short offset */
1136 	}
1137 	/* Is video bus bandwidth (32MB/s) too low for this resolution? */
1138 	/* this is definitely wrong if bus clock != 32MHz */
1139 	if (pclock->f / plen / 8 * bpp > 32000000L)
1140 		return -EINVAL;
1141 
1142 	if (vsync_len < 1)
1143 		vsync_len = 1;
1144 
1145 	/* include sync lengths in right/lower margin for all calculations */
1146 	right_margin += hsync_len;
1147 	lower_margin += vsync_len;
1148 
1149 	/* ! In all calculations of margins we use # of lines in half frame
1150 	 * (which is a full frame in non-interlace mode), so we can switch
1151 	 * between interlace and non-interlace without messing around
1152 	 * with these.
1153 	 */
1154 again:
1155 	/* Set base_offset 128 and video bus width */
1156 	par->hw.falcon.vid_control = mon_type | f030_bus_width;
1157 	if (!longoffset)
1158 		par->hw.falcon.vid_control |= VCO_SHORTOFFS;	/* base_offset 64 */
1159 	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
1160 		par->hw.falcon.vid_control |= VCO_HSYPOS;
1161 	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
1162 		par->hw.falcon.vid_control |= VCO_VSYPOS;
1163 	/* Pixelclock */
1164 	par->hw.falcon.vid_control |= pclock->control_mask;
1165 	/* External or internal clock */
1166 	par->hw.falcon.sync = pclock->sync_mask | 0x2;
1167 	/* Pixellength and prescale */
1168 	par->hw.falcon.vid_mode = (2 / plen) << 2;
1169 	if (doubleline)
1170 		par->hw.falcon.vid_mode |= VMO_DOUBLE;
1171 	if (interlace)
1172 		par->hw.falcon.vid_mode |= VMO_INTER;
1173 
1174 	/*********************
1175 	 * Horizontal timing: unit = [master clock cycles]
1176 	 * unit of hxx-registers: [master clock cycles * prescale]
1177 	 * Hxx-registers are 9 bit wide
1178 	 *
1179 	 * 1 line = ((hht + 2) * 2 * prescale) clock cycles
1180 	 *
1181 	 * graphic output = hdb & 0x200 ?
1182 	 *        ((hht + 2) * 2 - hdb + hde) * prescale - hdboff + hdeoff:
1183 	 *        (hht + 2  - hdb + hde) * prescale - hdboff + hdeoff
1184 	 * (this must be a multiple of plen*128/bpp, on VGA pixels
1185 	 *  to the right may be cut off with a bigger right margin)
1186 	 *
1187 	 * start of graphics relative to start of 1st halfline = hdb & 0x200 ?
1188 	 *        (hdb - hht - 2) * prescale + hdboff :
1189 	 *        hdb * prescale + hdboff
1190 	 *
1191 	 * end of graphics relative to start of 1st halfline =
1192 	 *        (hde + hht + 2) * prescale + hdeoff
1193 	 *********************/
1194 	/* Calculate VIDEL registers */
1195 {
1196 	prescale = hxx_prescale(&par->hw.falcon);
1197 	base_off = par->hw.falcon.vid_control & VCO_SHORTOFFS ? 64 : 128;
1198 
1199 	/* Offsets depend on video mode */
1200 	/* Offsets are in clock cycles, divide by prescale to
1201 	 * calculate hd[be]-registers
1202 	 */
1203 	if (par->hw.falcon.f_shift & 0x100) {
1204 		align = 1;
1205 		hde_off = 0;
1206 		hdb_off = (base_off + 16 * plen) + prescale;
1207 	} else {
1208 		align = 128 / bpp;
1209 		hde_off = ((128 / bpp + 2) * plen);
1210 		if (par->hw.falcon.ste_mode)
1211 			hdb_off = (64 + base_off + (128 / bpp + 2) * plen) + prescale;
1212 		else
1213 			hdb_off = (base_off + (128 / bpp + 18) * plen) + prescale;
1214 	}
1215 
1216 	gstart = (prescale / 2 + plen * left_margin) / prescale;
1217 	/* gend1 is for hde (gend-gstart multiple of align), shifter's xres */
1218 	gend1 = gstart + roundup(xres, align) * plen / prescale;
1219 	/* gend2 is for hbb, visible xres (rest to gend1 is cut off by hblank) */
1220 	gend2 = gstart + xres * plen / prescale;
1221 	par->HHT = plen * (left_margin + xres + right_margin) /
1222 			   (2 * prescale) - 2;
1223 /*	par->HHT = (gend2 + plen * right_margin / prescale) / 2 - 2;*/
1224 
1225 	par->HDB = gstart - hdb_off / prescale;
1226 	par->HBE = gstart;
1227 	if (par->HDB < 0)
1228 		par->HDB += par->HHT + 2 + 0x200;
1229 	par->HDE = gend1 - par->HHT - 2 - hde_off / prescale;
1230 	par->HBB = gend2 - par->HHT - 2;
1231 #if 0
1232 	/* One more Videl constraint: data fetch of two lines must not overlap */
1233 	if ((par->HDB & 0x200) && (par->HDB & ~0x200) - par->HDE <= 5) {
1234 		/* if this happens increase margins, decrease hfreq. */
1235 	}
1236 #endif
1237 	if (hde_off % prescale)
1238 		par->HBB++;		/* compensate for non matching hde and hbb */
1239 	par->HSS = par->HHT + 2 - plen * hsync_len / prescale;
1240 	if (par->HSS < par->HBB)
1241 		par->HSS = par->HBB;
1242 }
1243 
1244 	/*  check hor. frequency */
1245 	hfreq = pclock->f / ((par->HHT + 2) * prescale * 2);
1246 	if (hfreq > info->monspecs.hfmax && mon_type != F_MON_VGA) {
1247 		/* ++guenther:   ^^^^^^^^^^^^^^^^^^^ can't remember why I did this */
1248 		/* Too high -> enlarge margin */
1249 		left_margin += 1;
1250 		right_margin += 1;
1251 		goto again;
1252 	}
1253 	if (hfreq > info->monspecs.hfmax || hfreq < info->monspecs.hfmin)
1254 		return -EINVAL;
1255 
1256 	/* Vxx-registers */
1257 	/* All Vxx must be odd in non-interlace, since frame starts in the middle
1258 	 * of the first displayed line!
1259 	 * One frame consists of VFT+1 half lines. VFT+1 must be even in
1260 	 * non-interlace, odd in interlace mode for synchronisation.
1261 	 * Vxx-registers are 11 bit wide
1262 	 */
1263 	par->VBE = (upper_margin * 2 + 1); /* must begin on odd halfline */
1264 	par->VDB = par->VBE;
1265 	par->VDE = yres;
1266 	if (!interlace)
1267 		par->VDE <<= 1;
1268 	if (doubleline)
1269 		par->VDE <<= 1;		/* VDE now half lines per (half-)frame */
1270 	par->VDE += par->VDB;
1271 	par->VBB = par->VDE;
1272 	par->VFT = par->VBB + (lower_margin * 2 - 1) - 1;
1273 	par->VSS = par->VFT + 1 - (vsync_len * 2 - 1);
1274 	/* vbb,vss,vft must be even in interlace mode */
1275 	if (interlace) {
1276 		par->VBB++;
1277 		par->VSS++;
1278 		par->VFT++;
1279 	}
1280 
1281 	/* V-frequency check, hope I didn't create any loop here. */
1282 	/* Interlace and doubleline are mutually exclusive. */
1283 	vfreq = (hfreq * 2) / (par->VFT + 1);
1284 	if (vfreq > info->monspecs.vfmax && !doubleline && !interlace) {
1285 		/* Too high -> try again with doubleline */
1286 		doubleline = 1;
1287 		goto again;
1288 	} else if (vfreq < info->monspecs.vfmin && !interlace && !doubleline) {
1289 		/* Too low -> try again with interlace */
1290 		interlace = 1;
1291 		goto again;
1292 	} else if (vfreq < info->monspecs.vfmin && doubleline) {
1293 		/* Doubleline too low -> clear doubleline and enlarge margins */
1294 		int lines;
1295 		doubleline = 0;
1296 		for (lines = 0;
1297 		     (hfreq * 2) / (par->VFT + 1 + 4 * lines - 2 * yres) >
1298 		     info->monspecs.vfmax;
1299 		     lines++)
1300 			;
1301 		upper_margin += lines;
1302 		lower_margin += lines;
1303 		goto again;
1304 	} else if (vfreq > info->monspecs.vfmax && doubleline) {
1305 		/* Doubleline too high -> enlarge margins */
1306 		int lines;
1307 		for (lines = 0;
1308 		     (hfreq * 2) / (par->VFT + 1 + 4 * lines) >
1309 		     info->monspecs.vfmax;
1310 		     lines += 2)
1311 			;
1312 		upper_margin += lines;
1313 		lower_margin += lines;
1314 		goto again;
1315 	} else if (vfreq > info->monspecs.vfmax && interlace) {
1316 		/* Interlace, too high -> enlarge margins */
1317 		int lines;
1318 		for (lines = 0;
1319 		     (hfreq * 2) / (par->VFT + 1 + 4 * lines) >
1320 		     info->monspecs.vfmax;
1321 		     lines++)
1322 			;
1323 		upper_margin += lines;
1324 		lower_margin += lines;
1325 		goto again;
1326 	} else if (vfreq < info->monspecs.vfmin ||
1327 		   vfreq > info->monspecs.vfmax)
1328 		return -EINVAL;
1329 
1330 set_screen_base:
1331 	linelen = xres_virtual * bpp / 8;
1332 	if (yres_virtual * linelen > screen_len && screen_len)
1333 		return -EINVAL;
1334 	if (yres * linelen > screen_len && screen_len)
1335 		return -EINVAL;
1336 	if (var->yoffset + yres > yres_virtual && yres_virtual)
1337 		return -EINVAL;
1338 	par->yres_virtual = yres_virtual;
1339 	par->screen_base = screen_base + var->yoffset * linelen;
1340 	par->hw.falcon.xoffset = 0;
1341 
1342 	par->next_line = linelen;
1343 
1344 	return 0;
1345 }
1346 
1347 static int falcon_encode_var(struct fb_var_screeninfo *var,
1348 			     struct atafb_par *par)
1349 {
1350 /* !!! only for VGA !!! */
1351 	int linelen;
1352 	int prescale, plen;
1353 	int hdb_off, hde_off, base_off;
1354 	struct falcon_hw *hw = &par->hw.falcon;
1355 
1356 	memset(var, 0, sizeof(struct fb_var_screeninfo));
1357 	/* possible frequencies: 25.175 or 32MHz */
1358 	var->pixclock = hw->sync & 0x1 ? fext.t :
1359 	                hw->vid_control & VCO_CLOCK25 ? f25.t : f32.t;
1360 
1361 	var->height = -1;
1362 	var->width = -1;
1363 
1364 	var->sync = 0;
1365 	if (hw->vid_control & VCO_HSYPOS)
1366 		var->sync |= FB_SYNC_HOR_HIGH_ACT;
1367 	if (hw->vid_control & VCO_VSYPOS)
1368 		var->sync |= FB_SYNC_VERT_HIGH_ACT;
1369 
1370 	var->vmode = FB_VMODE_NONINTERLACED;
1371 	if (hw->vid_mode & VMO_INTER)
1372 		var->vmode |= FB_VMODE_INTERLACED;
1373 	if (hw->vid_mode & VMO_DOUBLE)
1374 		var->vmode |= FB_VMODE_DOUBLE;
1375 
1376 	/* visible y resolution:
1377 	 * Graphics display starts at line VDB and ends at line
1378 	 * VDE. If interlace mode off unit of VC-registers is
1379 	 * half lines, else lines.
1380 	 */
1381 	var->yres = hw->vde - hw->vdb;
1382 	if (!(var->vmode & FB_VMODE_INTERLACED))
1383 		var->yres >>= 1;
1384 	if (var->vmode & FB_VMODE_DOUBLE)
1385 		var->yres >>= 1;
1386 
1387 	/*
1388 	 * to get bpp, we must examine f_shift and st_shift.
1389 	 * f_shift is valid if any of bits no. 10, 8 or 4
1390 	 * is set. Priority in f_shift is: 10 ">" 8 ">" 4, i.e.
1391 	 * if bit 10 set then bit 8 and bit 4 don't care...
1392 	 * If all these bits are 0 get display depth from st_shift
1393 	 * (as for ST and STE)
1394 	 */
1395 	if (hw->f_shift & 0x400)	/* 2 colors */
1396 		var->bits_per_pixel = 1;
1397 	else if (hw->f_shift & 0x100)	/* hicolor */
1398 		var->bits_per_pixel = 16;
1399 	else if (hw->f_shift & 0x010)	/* 8 bitplanes */
1400 		var->bits_per_pixel = 8;
1401 	else if (hw->st_shift == 0)
1402 		var->bits_per_pixel = 4;
1403 	else if (hw->st_shift == 0x100)
1404 		var->bits_per_pixel = 2;
1405 	else				/* if (hw->st_shift == 0x200) */
1406 		var->bits_per_pixel = 1;
1407 
1408 	var->xres = hw->line_width * 16 / var->bits_per_pixel;
1409 	var->xres_virtual = var->xres + hw->line_offset * 16 / var->bits_per_pixel;
1410 	if (hw->xoffset)
1411 		var->xres_virtual += 16;
1412 
1413 	if (var->bits_per_pixel == 16) {
1414 		var->red.offset = 11;
1415 		var->red.length = 5;
1416 		var->red.msb_right = 0;
1417 		var->green.offset = 5;
1418 		var->green.length = 6;
1419 		var->green.msb_right = 0;
1420 		var->blue.offset = 0;
1421 		var->blue.length = 5;
1422 		var->blue.msb_right = 0;
1423 	} else {
1424 		var->red.offset = 0;
1425 		var->red.length = hw->ste_mode ? 4 : 6;
1426 		if (var->red.length > var->bits_per_pixel)
1427 			var->red.length = var->bits_per_pixel;
1428 		var->red.msb_right = 0;
1429 		var->grayscale = 0;
1430 		var->blue = var->green = var->red;
1431 	}
1432 	var->transp.offset = 0;
1433 	var->transp.length = 0;
1434 	var->transp.msb_right = 0;
1435 
1436 	linelen = var->xres_virtual * var->bits_per_pixel / 8;
1437 	if (screen_len) {
1438 		if (par->yres_virtual)
1439 			var->yres_virtual = par->yres_virtual;
1440 		else
1441 			/* yres_virtual == 0 means use maximum */
1442 			var->yres_virtual = screen_len / linelen;
1443 	} else {
1444 		if (hwscroll < 0)
1445 			var->yres_virtual = 2 * var->yres;
1446 		else
1447 			var->yres_virtual = var->yres + hwscroll * 16;
1448 	}
1449 	var->xoffset = 0;		/* TODO change this */
1450 
1451 	/* hdX-offsets */
1452 	prescale = hxx_prescale(hw);
1453 	plen = 4 >> (hw->vid_mode >> 2 & 0x3);
1454 	base_off = hw->vid_control & VCO_SHORTOFFS ? 64 : 128;
1455 	if (hw->f_shift & 0x100) {
1456 		hde_off = 0;
1457 		hdb_off = (base_off + 16 * plen) + prescale;
1458 	} else {
1459 		hde_off = ((128 / var->bits_per_pixel + 2) * plen);
1460 		if (hw->ste_mode)
1461 			hdb_off = (64 + base_off + (128 / var->bits_per_pixel + 2) * plen)
1462 					 + prescale;
1463 		else
1464 			hdb_off = (base_off + (128 / var->bits_per_pixel + 18) * plen)
1465 					 + prescale;
1466 	}
1467 
1468 	/* Right margin includes hsync */
1469 	var->left_margin = hdb_off + prescale * ((hw->hdb & 0x1ff) -
1470 					   (hw->hdb & 0x200 ? 2 + hw->hht : 0));
1471 	if (hw->ste_mode || mon_type != F_MON_VGA)
1472 		var->right_margin = prescale * (hw->hht + 2 - hw->hde) - hde_off;
1473 	else
1474 		/* can't use this in ste_mode, because hbb is +1 off */
1475 		var->right_margin = prescale * (hw->hht + 2 - hw->hbb);
1476 	var->hsync_len = prescale * (hw->hht + 2 - hw->hss);
1477 
1478 	/* Lower margin includes vsync */
1479 	var->upper_margin = hw->vdb / 2;	/* round down to full lines */
1480 	var->lower_margin = (hw->vft + 1 - hw->vde + 1) / 2;	/* round up */
1481 	var->vsync_len = (hw->vft + 1 - hw->vss + 1) / 2;	/* round up */
1482 	if (var->vmode & FB_VMODE_INTERLACED) {
1483 		var->upper_margin *= 2;
1484 		var->lower_margin *= 2;
1485 		var->vsync_len *= 2;
1486 	} else if (var->vmode & FB_VMODE_DOUBLE) {
1487 		var->upper_margin = (var->upper_margin + 1) / 2;
1488 		var->lower_margin = (var->lower_margin + 1) / 2;
1489 		var->vsync_len = (var->vsync_len + 1) / 2;
1490 	}
1491 
1492 	var->pixclock *= plen;
1493 	var->left_margin /= plen;
1494 	var->right_margin /= plen;
1495 	var->hsync_len /= plen;
1496 
1497 	var->right_margin -= var->hsync_len;
1498 	var->lower_margin -= var->vsync_len;
1499 
1500 	if (screen_base)
1501 		var->yoffset = (par->screen_base - screen_base) / linelen;
1502 	else
1503 		var->yoffset = 0;
1504 	var->nonstd = 0;		/* what is this for? */
1505 	var->activate = 0;
1506 	return 0;
1507 }
1508 
1509 static int f_change_mode;
1510 static struct falcon_hw f_new_mode;
1511 static int f_pan_display;
1512 
1513 static void falcon_get_par(struct atafb_par *par)
1514 {
1515 	unsigned long addr;
1516 	struct falcon_hw *hw = &par->hw.falcon;
1517 
1518 	hw->line_width = shifter_f030.scn_width;
1519 	hw->line_offset = shifter_f030.off_next;
1520 	hw->st_shift = videl.st_shift & 0x300;
1521 	hw->f_shift = videl.f_shift;
1522 	hw->vid_control = videl.control;
1523 	hw->vid_mode = videl.mode;
1524 	hw->sync = shifter_st.syncmode & 0x1;
1525 	hw->xoffset = videl.xoffset & 0xf;
1526 	hw->hht = videl.hht;
1527 	hw->hbb = videl.hbb;
1528 	hw->hbe = videl.hbe;
1529 	hw->hdb = videl.hdb;
1530 	hw->hde = videl.hde;
1531 	hw->hss = videl.hss;
1532 	hw->vft = videl.vft;
1533 	hw->vbb = videl.vbb;
1534 	hw->vbe = videl.vbe;
1535 	hw->vdb = videl.vdb;
1536 	hw->vde = videl.vde;
1537 	hw->vss = videl.vss;
1538 
1539 	addr = (shifter_st.bas_hi & 0xff) << 16 |
1540 	       (shifter_st.bas_md & 0xff) << 8  |
1541 	       (shifter_st.bas_lo & 0xff);
1542 	par->screen_base = atari_stram_to_virt(addr);
1543 
1544 	/* derived parameters */
1545 	hw->ste_mode = (hw->f_shift & 0x510) == 0 && hw->st_shift == 0x100;
1546 	hw->mono = (hw->f_shift & 0x400) ||
1547 	           ((hw->f_shift & 0x510) == 0 && hw->st_shift == 0x200);
1548 }
1549 
1550 static void falcon_set_par(struct atafb_par *par)
1551 {
1552 	f_change_mode = 0;
1553 
1554 	/* only set screen_base if really necessary */
1555 	if (current_par.screen_base != par->screen_base)
1556 		fbhw->set_screen_base(par->screen_base);
1557 
1558 	/* Don't touch any other registers if we keep the default resolution */
1559 	if (DontCalcRes)
1560 		return;
1561 
1562 	/* Tell vbl-handler to change video mode.
1563 	 * We change modes only on next VBL, to avoid desynchronisation
1564 	 * (a shift to the right and wrap around by a random number of pixels
1565 	 * in all monochrome modes).
1566 	 * This seems to work on my Falcon.
1567 	 */
1568 	f_new_mode = par->hw.falcon;
1569 	f_change_mode = 1;
1570 }
1571 
1572 static irqreturn_t falcon_vbl_switcher(int irq, void *dummy)
1573 {
1574 	struct falcon_hw *hw = &f_new_mode;
1575 
1576 	if (f_change_mode) {
1577 		f_change_mode = 0;
1578 
1579 		if (hw->sync & 0x1) {
1580 			/* Enable external pixelclock. This code only for ScreenWonder */
1581 			*(volatile unsigned short *)0xffff9202 = 0xffbf;
1582 		} else {
1583 			/* Turn off external clocks. Read sets all output bits to 1. */
1584 			*(volatile unsigned short *)0xffff9202;
1585 		}
1586 		shifter_st.syncmode = hw->sync;
1587 
1588 		videl.hht = hw->hht;
1589 		videl.hbb = hw->hbb;
1590 		videl.hbe = hw->hbe;
1591 		videl.hdb = hw->hdb;
1592 		videl.hde = hw->hde;
1593 		videl.hss = hw->hss;
1594 		videl.vft = hw->vft;
1595 		videl.vbb = hw->vbb;
1596 		videl.vbe = hw->vbe;
1597 		videl.vdb = hw->vdb;
1598 		videl.vde = hw->vde;
1599 		videl.vss = hw->vss;
1600 
1601 		videl.f_shift = 0;	/* write enables Falcon palette, 0: 4 planes */
1602 		if (hw->ste_mode) {
1603 			videl.st_shift = hw->st_shift;	/* write enables STE palette */
1604 		} else {
1605 			/* IMPORTANT:
1606 			 * set st_shift 0, so we can tell the screen-depth if f_shift == 0.
1607 			 * Writing 0 to f_shift enables 4 plane Falcon mode but
1608 			 * doesn't set st_shift. st_shift != 0 (!= 4planes) is impossible
1609 			 * with Falcon palette.
1610 			 */
1611 			videl.st_shift = 0;
1612 			/* now back to Falcon palette mode */
1613 			videl.f_shift = hw->f_shift;
1614 		}
1615 		/* writing to st_shift changed scn_width and vid_mode */
1616 		videl.xoffset = hw->xoffset;
1617 		shifter_f030.scn_width = hw->line_width;
1618 		shifter_f030.off_next = hw->line_offset;
1619 		videl.control = hw->vid_control;
1620 		videl.mode = hw->vid_mode;
1621 	}
1622 	if (f_pan_display) {
1623 		f_pan_display = 0;
1624 		videl.xoffset = current_par.hw.falcon.xoffset;
1625 		shifter_f030.off_next = current_par.hw.falcon.line_offset;
1626 	}
1627 	return IRQ_HANDLED;
1628 }
1629 
1630 static int falcon_pan_display(struct fb_var_screeninfo *var,
1631 			      struct fb_info *info)
1632 {
1633 	struct atafb_par *par = info->par;
1634 
1635 	int xoffset;
1636 	int bpp = info->var.bits_per_pixel;
1637 
1638 	if (bpp == 1)
1639 		var->xoffset = round_up(var->xoffset, 32);
1640 	if (bpp != 16)
1641 		par->hw.falcon.xoffset = var->xoffset & 15;
1642 	else {
1643 		par->hw.falcon.xoffset = 0;
1644 		var->xoffset = round_up(var->xoffset, 2);
1645 	}
1646 	par->hw.falcon.line_offset = bpp *
1647 		(info->var.xres_virtual - info->var.xres) / 16;
1648 	if (par->hw.falcon.xoffset)
1649 		par->hw.falcon.line_offset -= bpp;
1650 	xoffset = var->xoffset - par->hw.falcon.xoffset;
1651 
1652 	par->screen_base = screen_base +
1653 	        (var->yoffset * info->var.xres_virtual + xoffset) * bpp / 8;
1654 	if (fbhw->set_screen_base)
1655 		fbhw->set_screen_base(par->screen_base);
1656 	else
1657 		return -EINVAL;		/* shouldn't happen */
1658 	f_pan_display = 1;
1659 	return 0;
1660 }
1661 
1662 static int falcon_setcolreg(unsigned int regno, unsigned int red,
1663 			    unsigned int green, unsigned int blue,
1664 			    unsigned int transp, struct fb_info *info)
1665 {
1666 	if (regno > 255)
1667 		return 1;
1668 	f030_col[regno] = (((red & 0xfc00) << 16) |
1669 			   ((green & 0xfc00) << 8) |
1670 			   ((blue & 0xfc00) >> 8));
1671 	if (regno < 16) {
1672 		shifter_tt.color_reg[regno] =
1673 			((((red & 0xe000) >> 13)   | ((red & 0x1000) >> 12)) << 8)   |
1674 			((((green & 0xe000) >> 13) | ((green & 0x1000) >> 12)) << 4) |
1675 			   ((blue & 0xe000) >> 13) | ((blue & 0x1000) >> 12);
1676 		((u32 *)info->pseudo_palette)[regno] = ((red & 0xf800) |
1677 						       ((green & 0xfc00) >> 5) |
1678 						       ((blue & 0xf800) >> 11));
1679 	}
1680 	return 0;
1681 }
1682 
1683 static int falcon_blank(int blank_mode)
1684 {
1685 	/* ++guenther: we can switch off graphics by changing VDB and VDE,
1686 	 * so VIDEL doesn't hog the bus while saving.
1687 	 * (this may affect usleep()).
1688 	 */
1689 	int vdb, vss, hbe, hss;
1690 
1691 	if (mon_type == F_MON_SM)	/* this doesn't work on SM124 */
1692 		return 1;
1693 
1694 	vdb = current_par.VDB;
1695 	vss = current_par.VSS;
1696 	hbe = current_par.HBE;
1697 	hss = current_par.HSS;
1698 
1699 	if (blank_mode >= 1) {
1700 		/* disable graphics output (this speeds up the CPU) ... */
1701 		vdb = current_par.VFT + 1;
1702 		/* ... and blank all lines */
1703 		hbe = current_par.HHT + 2;
1704 	}
1705 	/* use VESA suspend modes on VGA monitors */
1706 	if (mon_type == F_MON_VGA) {
1707 		if (blank_mode == 2 || blank_mode == 4)
1708 			vss = current_par.VFT + 1;
1709 		if (blank_mode == 3 || blank_mode == 4)
1710 			hss = current_par.HHT + 2;
1711 	}
1712 
1713 	videl.vdb = vdb;
1714 	videl.vss = vss;
1715 	videl.hbe = hbe;
1716 	videl.hss = hss;
1717 
1718 	return 0;
1719 }
1720 
1721 static int falcon_detect(struct fb_info *info)
1722 {
1723 	struct atafb_par par;
1724 	unsigned char fhw;
1725 
1726 	/* Determine connected monitor and set monitor parameters */
1727 	fhw = *(unsigned char *)0xffff8006;
1728 	mon_type = fhw >> 6 & 0x3;
1729 	/* bit 1 of fhw: 1=32 bit ram bus, 0=16 bit */
1730 	f030_bus_width = fhw << 6 & 0x80;
1731 	switch (mon_type) {
1732 	case F_MON_SM:
1733 		info->monspecs.vfmin = 70;
1734 		info->monspecs.vfmax = 72;
1735 		info->monspecs.hfmin = 35713;
1736 		info->monspecs.hfmax = 35715;
1737 		break;
1738 	case F_MON_SC:
1739 	case F_MON_TV:
1740 		/* PAL...NTSC */
1741 		info->monspecs.vfmin = 49;	/* not 50, since TOS defaults to 49.9x Hz */
1742 		info->monspecs.vfmax = 60;
1743 		info->monspecs.hfmin = 15620;
1744 		info->monspecs.hfmax = 15755;
1745 		break;
1746 	}
1747 	/* initialize hsync-len */
1748 	f25.hsync = h_syncs[mon_type] / f25.t;
1749 	f32.hsync = h_syncs[mon_type] / f32.t;
1750 	if (fext.t)
1751 		fext.hsync = h_syncs[mon_type] / fext.t;
1752 
1753 	falcon_get_par(&par);
1754 	falcon_encode_var(&atafb_predefined[0], &par);
1755 
1756 	/* Detected mode is always the "autodetect" slot */
1757 	return 1;
1758 }
1759 
1760 #endif /* ATAFB_FALCON */
1761 
1762 /* ------------------- ST(E) specific functions ---------------------- */
1763 
1764 #ifdef ATAFB_STE
1765 
1766 static int stste_encode_fix(struct fb_fix_screeninfo *fix,
1767 			    struct atafb_par *par)
1768 {
1769 	int mode;
1770 
1771 	strcpy(fix->id, "Atari Builtin");
1772 	fix->smem_start = phys_screen_base;
1773 	fix->smem_len = screen_len;
1774 	fix->type = FB_TYPE_INTERLEAVED_PLANES;
1775 	fix->type_aux = 2;
1776 	fix->visual = FB_VISUAL_PSEUDOCOLOR;
1777 	mode = par->hw.st.mode & 3;
1778 	if (mode == ST_HIGH) {
1779 		fix->type = FB_TYPE_PACKED_PIXELS;
1780 		fix->type_aux = 0;
1781 		fix->visual = FB_VISUAL_MONO10;
1782 	}
1783 	if (ATARIHW_PRESENT(EXTD_SHIFTER)) {
1784 		fix->xpanstep = 16;
1785 		fix->ypanstep = 1;
1786 	} else {
1787 		fix->xpanstep = 0;
1788 		fix->ypanstep = 0;
1789 	}
1790 	fix->ywrapstep = 0;
1791 	fix->line_length = par->next_line;
1792 	fix->accel = FB_ACCEL_ATARIBLITT;
1793 	return 0;
1794 }
1795 
1796 static int stste_decode_var(struct fb_info *info, struct fb_var_screeninfo *var,
1797 			    struct atafb_par *par)
1798 {
1799 	int xres = var->xres;
1800 	int yres = var->yres;
1801 	int bpp = var->bits_per_pixel;
1802 	int linelen;
1803 	int yres_virtual = var->yres_virtual;
1804 
1805 	if (mono_moni) {
1806 		if (bpp > 1 || xres > sttt_xres || yres > st_yres)
1807 			return -EINVAL;
1808 		par->hw.st.mode = ST_HIGH;
1809 		xres = sttt_xres;
1810 		yres = st_yres;
1811 		bpp = 1;
1812 	} else {
1813 		if (bpp > 4 || xres > sttt_xres || yres > st_yres)
1814 			return -EINVAL;
1815 		if (bpp > 2) {
1816 			if (xres > sttt_xres / 2 || yres > st_yres / 2)
1817 				return -EINVAL;
1818 			par->hw.st.mode = ST_LOW;
1819 			xres = sttt_xres / 2;
1820 			yres = st_yres / 2;
1821 			bpp = 4;
1822 		} else if (bpp > 1) {
1823 			if (xres > sttt_xres || yres > st_yres / 2)
1824 				return -EINVAL;
1825 			par->hw.st.mode = ST_MID;
1826 			xres = sttt_xres;
1827 			yres = st_yres / 2;
1828 			bpp = 2;
1829 		} else
1830 			return -EINVAL;
1831 	}
1832 	if (yres_virtual <= 0)
1833 		yres_virtual = 0;
1834 	else if (yres_virtual < yres)
1835 		yres_virtual = yres;
1836 	if (var->sync & FB_SYNC_EXT)
1837 		par->hw.st.sync = (par->hw.st.sync & ~1) | 1;
1838 	else
1839 		par->hw.st.sync = (par->hw.st.sync & ~1);
1840 	linelen = xres * bpp / 8;
1841 	if (yres_virtual * linelen > screen_len && screen_len)
1842 		return -EINVAL;
1843 	if (yres * linelen > screen_len && screen_len)
1844 		return -EINVAL;
1845 	if (var->yoffset + yres > yres_virtual && yres_virtual)
1846 		return -EINVAL;
1847 	par->yres_virtual = yres_virtual;
1848 	par->screen_base = screen_base + var->yoffset * linelen;
1849 	par->next_line = linelen;
1850 	return 0;
1851 }
1852 
1853 static int stste_encode_var(struct fb_var_screeninfo *var,
1854 			    struct atafb_par *par)
1855 {
1856 	int linelen;
1857 	memset(var, 0, sizeof(struct fb_var_screeninfo));
1858 	var->red.offset = 0;
1859 	var->red.length = ATARIHW_PRESENT(EXTD_SHIFTER) ? 4 : 3;
1860 	var->red.msb_right = 0;
1861 	var->grayscale = 0;
1862 
1863 	var->pixclock = 31041;
1864 	var->left_margin = 120;		/* these are incorrect */
1865 	var->right_margin = 100;
1866 	var->upper_margin = 8;
1867 	var->lower_margin = 16;
1868 	var->hsync_len = 140;
1869 	var->vsync_len = 30;
1870 
1871 	var->height = -1;
1872 	var->width = -1;
1873 
1874 	if (!(par->hw.st.sync & 1))
1875 		var->sync = 0;
1876 	else
1877 		var->sync = FB_SYNC_EXT;
1878 
1879 	switch (par->hw.st.mode & 3) {
1880 	case ST_LOW:
1881 		var->xres = sttt_xres / 2;
1882 		var->yres = st_yres / 2;
1883 		var->bits_per_pixel = 4;
1884 		break;
1885 	case ST_MID:
1886 		var->xres = sttt_xres;
1887 		var->yres = st_yres / 2;
1888 		var->bits_per_pixel = 2;
1889 		break;
1890 	case ST_HIGH:
1891 		var->xres = sttt_xres;
1892 		var->yres = st_yres;
1893 		var->bits_per_pixel = 1;
1894 		break;
1895 	}
1896 	var->blue = var->green = var->red;
1897 	var->transp.offset = 0;
1898 	var->transp.length = 0;
1899 	var->transp.msb_right = 0;
1900 	var->xres_virtual = sttt_xres_virtual;
1901 	linelen = var->xres_virtual * var->bits_per_pixel / 8;
1902 	ovsc_addlen = linelen * (sttt_yres_virtual - st_yres);
1903 
1904 	if (!use_hwscroll)
1905 		var->yres_virtual = var->yres;
1906 	else if (screen_len) {
1907 		if (par->yres_virtual)
1908 			var->yres_virtual = par->yres_virtual;
1909 		else
1910 			/* yres_virtual == 0 means use maximum */
1911 			var->yres_virtual = screen_len / linelen;
1912 	} else {
1913 		if (hwscroll < 0)
1914 			var->yres_virtual = 2 * var->yres;
1915 		else
1916 			var->yres_virtual = var->yres + hwscroll * 16;
1917 	}
1918 	var->xoffset = 0;
1919 	if (screen_base)
1920 		var->yoffset = (par->screen_base - screen_base) / linelen;
1921 	else
1922 		var->yoffset = 0;
1923 	var->nonstd = 0;
1924 	var->activate = 0;
1925 	var->vmode = FB_VMODE_NONINTERLACED;
1926 	return 0;
1927 }
1928 
1929 static void stste_get_par(struct atafb_par *par)
1930 {
1931 	unsigned long addr;
1932 	par->hw.st.mode = shifter_tt.st_shiftmode;
1933 	par->hw.st.sync = shifter_st.syncmode;
1934 	addr = ((shifter_st.bas_hi & 0xff) << 16) |
1935 	       ((shifter_st.bas_md & 0xff) << 8);
1936 	if (ATARIHW_PRESENT(EXTD_SHIFTER))
1937 		addr |= (shifter_st.bas_lo & 0xff);
1938 	par->screen_base = atari_stram_to_virt(addr);
1939 }
1940 
1941 static void stste_set_par(struct atafb_par *par)
1942 {
1943 	shifter_tt.st_shiftmode = par->hw.st.mode;
1944 	shifter_st.syncmode = par->hw.st.sync;
1945 	/* only set screen_base if really necessary */
1946 	if (current_par.screen_base != par->screen_base)
1947 		fbhw->set_screen_base(par->screen_base);
1948 }
1949 
1950 static int stste_setcolreg(unsigned int regno, unsigned int red,
1951 			   unsigned int green, unsigned int blue,
1952 			   unsigned int transp, struct fb_info *info)
1953 {
1954 	if (regno > 15)
1955 		return 1;
1956 	red >>= 12;
1957 	blue >>= 12;
1958 	green >>= 12;
1959 	if (ATARIHW_PRESENT(EXTD_SHIFTER))
1960 		shifter_tt.color_reg[regno] =
1961 			((((red & 0xe)   >> 1) | ((red & 1)   << 3)) << 8) |
1962 			((((green & 0xe) >> 1) | ((green & 1) << 3)) << 4) |
1963 			  ((blue & 0xe)  >> 1) | ((blue & 1)  << 3);
1964 	else
1965 		shifter_tt.color_reg[regno] =
1966 			((red & 0xe) << 7) |
1967 			((green & 0xe) << 3) |
1968 			((blue & 0xe) >> 1);
1969 	return 0;
1970 }
1971 
1972 static int stste_detect(struct fb_info *info)
1973 {
1974 	struct atafb_par par;
1975 
1976 	/* Determine the connected monitor: The DMA sound must be
1977 	 * disabled before reading the MFP GPIP, because the Sound
1978 	 * Done Signal and the Monochrome Detect are XORed together!
1979 	 */
1980 	if (ATARIHW_PRESENT(PCM_8BIT)) {
1981 		tt_dmasnd.ctrl = DMASND_CTRL_OFF;
1982 		udelay(20);		/* wait a while for things to settle down */
1983 	}
1984 	mono_moni = (st_mfp.par_dt_reg & 0x80) == 0;
1985 
1986 	stste_get_par(&par);
1987 	stste_encode_var(&atafb_predefined[0], &par);
1988 
1989 	if (!ATARIHW_PRESENT(EXTD_SHIFTER))
1990 		use_hwscroll = 0;
1991 	return 1;
1992 }
1993 
1994 static void stste_set_screen_base(void *s_base)
1995 {
1996 	unsigned long addr;
1997 	addr = atari_stram_to_phys(s_base);
1998 	/* Setup Screen Memory */
1999 	shifter_st.bas_hi = (unsigned char)((addr & 0xff0000) >> 16);
2000 	shifter_st.bas_md = (unsigned char)((addr & 0x00ff00) >> 8);
2001 	if (ATARIHW_PRESENT(EXTD_SHIFTER))
2002 		shifter_st.bas_lo = (unsigned char)(addr & 0x0000ff);
2003 }
2004 
2005 #endif /* ATAFB_STE */
2006 
2007 /* Switching the screen size should be done during vsync, otherwise
2008  * the margins may get messed up. This is a well known problem of
2009  * the ST's video system.
2010  *
2011  * Unfortunately there is hardly any way to find the vsync, as the
2012  * vertical blank interrupt is no longer in time on machines with
2013  * overscan type modifications.
2014  *
2015  * We can, however, use Timer B to safely detect the black shoulder,
2016  * but then we've got to guess an appropriate delay to find the vsync.
2017  * This might not work on every machine.
2018  *
2019  * martin_rogge @ ki.maus.de, 8th Aug 1995
2020  */
2021 
2022 #define LINE_DELAY  (mono_moni ? 30 : 70)
2023 #define SYNC_DELAY  (mono_moni ? 1500 : 2000)
2024 
2025 /* SWITCH_ACIA may be used for Falcon (ScreenBlaster III internal!) */
2026 static void st_ovsc_switch(void)
2027 {
2028 	unsigned long flags;
2029 	register unsigned char old, new;
2030 
2031 	if (!(atari_switches & ATARI_SWITCH_OVSC_MASK))
2032 		return;
2033 	local_irq_save(flags);
2034 
2035 	st_mfp.tim_ct_b = 0x10;
2036 	st_mfp.active_edge |= 8;
2037 	st_mfp.tim_ct_b = 0;
2038 	st_mfp.tim_dt_b = 0xf0;
2039 	st_mfp.tim_ct_b = 8;
2040 	while (st_mfp.tim_dt_b > 1)	/* TOS does it this way, don't ask why */
2041 		;
2042 	new = st_mfp.tim_dt_b;
2043 	do {
2044 		udelay(LINE_DELAY);
2045 		old = new;
2046 		new = st_mfp.tim_dt_b;
2047 	} while (old != new);
2048 	st_mfp.tim_ct_b = 0x10;
2049 	udelay(SYNC_DELAY);
2050 
2051 	if (atari_switches & ATARI_SWITCH_OVSC_IKBD)
2052 		acia.key_ctrl = ACIA_DIV64 | ACIA_D8N1S | ACIA_RHTID | ACIA_RIE;
2053 	if (atari_switches & ATARI_SWITCH_OVSC_MIDI)
2054 		acia.mid_ctrl = ACIA_DIV16 | ACIA_D8N1S | ACIA_RHTID;
2055 	if (atari_switches & (ATARI_SWITCH_OVSC_SND6|ATARI_SWITCH_OVSC_SND7)) {
2056 		sound_ym.rd_data_reg_sel = 14;
2057 		sound_ym.wd_data = sound_ym.rd_data_reg_sel |
2058 				   ((atari_switches & ATARI_SWITCH_OVSC_SND6) ? 0x40:0) |
2059 				   ((atari_switches & ATARI_SWITCH_OVSC_SND7) ? 0x80:0);
2060 	}
2061 	local_irq_restore(flags);
2062 }
2063 
2064 /* ------------------- External Video ---------------------- */
2065 
2066 #ifdef ATAFB_EXT
2067 
2068 /*
2069  * Only a Falcon can have a SuperVidel; a TT with a VME graphics card can
2070  * have its framebuffer in the same address range.
2071  */
2072 static bool ext_is_supervidel(void)
2073 {
2074 	return ATARIHW_PRESENT(VIDEL_SHIFTER) && external_addr >= 0xa0000000;
2075 }
2076 
2077 static int ext_encode_fix(struct fb_fix_screeninfo *fix, struct atafb_par *par)
2078 {
2079 	strcpy(fix->id, "Unknown Extern");
2080 	fix->smem_start = external_addr;
2081 	fix->smem_len = PAGE_ALIGN(external_len);
2082 	if (external_depth == 1) {
2083 		fix->type = FB_TYPE_PACKED_PIXELS;
2084 		/* The letters 'n' and 'i' in the "atavideo=external:" stand
2085 		 * for "normal" and "inverted", rsp., in the monochrome case */
2086 		fix->visual =
2087 			(external_pmode == FB_TYPE_INTERLEAVED_PLANES ||
2088 			 external_pmode == FB_TYPE_PACKED_PIXELS) ?
2089 				FB_VISUAL_MONO10 : FB_VISUAL_MONO01;
2090 	} else {
2091 		/* Use STATIC if we don't know how to access color registers;
2092 		 * SuperVidel 8bpp chunky (fb in SV RAM) uses the Falcon palette
2093 		 */
2094 		int visual = (external_vgaiobase ||
2095 			      (external_depth == 8 && ext_is_supervidel())) ?
2096 					 FB_VISUAL_PSEUDOCOLOR :
2097 					 FB_VISUAL_STATIC_PSEUDOCOLOR;
2098 		switch (external_pmode) {
2099 		case -1:		/* truecolor */
2100 			fix->type = FB_TYPE_PACKED_PIXELS;
2101 			fix->visual = FB_VISUAL_TRUECOLOR;
2102 			break;
2103 		case FB_TYPE_PACKED_PIXELS:
2104 			fix->type = FB_TYPE_PACKED_PIXELS;
2105 			fix->visual = visual;
2106 			break;
2107 		case FB_TYPE_PLANES:
2108 			fix->type = FB_TYPE_PLANES;
2109 			fix->visual = visual;
2110 			break;
2111 		case FB_TYPE_INTERLEAVED_PLANES:
2112 			fix->type = FB_TYPE_INTERLEAVED_PLANES;
2113 			fix->type_aux = 2;
2114 			fix->visual = visual;
2115 			break;
2116 		}
2117 	}
2118 	fix->xpanstep = 0;
2119 	fix->ypanstep = 0;
2120 	fix->ywrapstep = 0;
2121 	fix->line_length = par->next_line;
2122 	return 0;
2123 }
2124 
2125 static int ext_decode_var(struct fb_info *info, struct fb_var_screeninfo *var,
2126 			struct atafb_par *par)
2127 {
2128 	struct fb_var_screeninfo *myvar = &atafb_predefined[0];
2129 
2130 	if (var->bits_per_pixel > myvar->bits_per_pixel ||
2131 	    var->xres > myvar->xres ||
2132 	    var->xres_virtual > myvar->xres_virtual ||
2133 	    var->yres > myvar->yres ||
2134 	    var->xoffset > 0 ||
2135 	    var->yoffset > 0)
2136 		return -EINVAL;
2137 
2138 	par->next_line = external_xres_virtual * external_depth / 8;
2139 	return 0;
2140 }
2141 
2142 static int ext_encode_var(struct fb_var_screeninfo *var, struct atafb_par *par)
2143 {
2144 	memset(var, 0, sizeof(struct fb_var_screeninfo));
2145 	var->red.offset = 0;
2146 	var->red.length = (external_pmode == -1) ? external_depth / 3 :
2147 			(external_vgaiobase ? external_bitspercol : 0);
2148 	var->red.msb_right = 0;
2149 	var->grayscale = 0;
2150 
2151 	var->pixclock = 31041;
2152 	var->left_margin = 120;		/* these are surely incorrect */
2153 	var->right_margin = 100;
2154 	var->upper_margin = 8;
2155 	var->lower_margin = 16;
2156 	var->hsync_len = 140;
2157 	var->vsync_len = 30;
2158 
2159 	var->height = -1;
2160 	var->width = -1;
2161 
2162 	var->sync = 0;
2163 
2164 	var->xres = external_xres;
2165 	var->yres = external_yres;
2166 	var->xres_virtual = external_xres_virtual;
2167 	var->bits_per_pixel = external_depth;
2168 
2169 	var->blue = var->green = var->red;
2170 	var->transp.offset = 0;
2171 	var->transp.length = 0;
2172 	var->transp.msb_right = 0;
2173 	if (external_pmode == -1 && external_depth == 16) {
2174 		/* RGB565 truecolor (e.g. SuperVidel native mode) */
2175 		var->red.offset = 11;
2176 		var->red.length = 5;
2177 		var->green.offset = 5;
2178 		var->green.length = 6;
2179 		var->blue.offset = 0;
2180 		var->blue.length = 5;
2181 	} else if (external_pmode == -1 && external_depth == 32) {
2182 		/* ARGB8888 truecolor (e.g. SuperVidel native mode) */
2183 		var->red.offset = 16;
2184 		var->red.length = 8;
2185 		var->green.offset = 8;
2186 		var->green.length = 8;
2187 		var->blue.offset = 0;
2188 		var->blue.length = 8;
2189 		var->transp.offset = 24;
2190 		var->transp.length = 8;
2191 	} else if (external_pmode == FB_TYPE_PACKED_PIXELS &&
2192 		   external_depth == 8 && ext_is_supervidel()) {
2193 		/* SuperVidel 8bpp chunky: palette has 8 bits per channel.
2194 		 * Without this, fb_get_color_depth() sees length 0 and
2195 		 * fbcon falls back to its 2-color palette — the console
2196 		 * text (color 7) stays black on black.
2197 		 */
2198 		var->red.length = 8;
2199 		var->green.length = 8;
2200 		var->blue.length = 8;
2201 	}
2202 	var->yres_virtual = var->yres;
2203 	var->xoffset = 0;
2204 	var->yoffset = 0;
2205 	var->nonstd = 0;
2206 	var->activate = 0;
2207 	var->vmode = FB_VMODE_NONINTERLACED;
2208 	return 0;
2209 }
2210 
2211 static void ext_get_par(struct atafb_par *par)
2212 {
2213 	par->screen_base = external_screen_base;
2214 }
2215 
2216 static void ext_set_par(struct atafb_par *par)
2217 {
2218 }
2219 
2220 #define OUTB(port,val) \
2221 	*((unsigned volatile char *) ((port)+external_vgaiobase)) = (val)
2222 #define INB(port) \
2223 	(*((unsigned volatile char *) ((port)+external_vgaiobase)))
2224 #define DACDelay				\
2225 	do {					\
2226 		unsigned char tmp = INB(0x3da);	\
2227 		tmp = INB(0x3da);			\
2228 	} while (0)
2229 
2230 static int ext_setcolreg(unsigned int regno, unsigned int red,
2231 			 unsigned int green, unsigned int blue,
2232 			 unsigned int transp, struct fb_info *info)
2233 {
2234 	unsigned char colmask = (1 << external_bitspercol) - 1;
2235 
2236 	if (external_pmode == -1 && external_depth == 16) {
2237 		/* truecolor: only the pseudo palette for fbcon is needed */
2238 		if (regno > 15)
2239 			return 1;
2240 		((u32 *)info->pseudo_palette)[regno] = (red & 0xf800) |
2241 						       ((green & 0xfc00) >> 5) |
2242 						       ((blue & 0xf800) >> 11);
2243 		return 0;
2244 	}
2245 	if (external_pmode == -1 && external_depth == 32) {
2246 		/* ARGB8888, alpha forced opaque */
2247 		if (regno > 15)
2248 			return 1;
2249 		((u32 *)info->pseudo_palette)[regno] = 0xff000000 |
2250 						       ((red & 0xff00) << 8) |
2251 						       (green & 0xff00) |
2252 						       ((blue & 0xff00) >> 8);
2253 		return 0;
2254 	}
2255 	if (external_pmode == FB_TYPE_PACKED_PIXELS && external_depth == 8 &&
2256 	    ext_is_supervidel()) {
2257 		/* SuperVidel native 8bpp chunky scans out via the Falcon
2258 		 * palette registers, honoring all 8 bits per channel
2259 		 */
2260 		if (regno > 255)
2261 			return 1;
2262 		f030_col[regno] = ((red & 0xff00) << 16) |
2263 				  ((green & 0xff00) << 8) |
2264 				  ((blue & 0xff00) >> 8);
2265 		return 0;
2266 	}
2267 
2268 	if (!external_vgaiobase)
2269 		return 1;
2270 
2271 	if (regno > 255)
2272 		return 1;
2273 
2274 	red >>= 8;
2275 	green >>= 8;
2276 	blue >>= 8;
2277 
2278 	switch (external_card_type) {
2279 	case IS_VGA:
2280 		OUTB(0x3c8, regno);
2281 		DACDelay;
2282 		OUTB(0x3c9, red & colmask);
2283 		DACDelay;
2284 		OUTB(0x3c9, green & colmask);
2285 		DACDelay;
2286 		OUTB(0x3c9, blue & colmask);
2287 		DACDelay;
2288 		return 0;
2289 
2290 	case IS_MV300:
2291 		OUTB((MV300_reg[regno] << 2) + 1, red);
2292 		OUTB((MV300_reg[regno] << 2) + 1, green);
2293 		OUTB((MV300_reg[regno] << 2) + 1, blue);
2294 		return 0;
2295 
2296 	default:
2297 		return 1;
2298 	}
2299 }
2300 
2301 static int ext_detect(struct fb_info *info)
2302 {
2303 	struct fb_var_screeninfo *myvar = &atafb_predefined[0];
2304 	struct atafb_par dummy_par;
2305 
2306 	myvar->xres = external_xres;
2307 	myvar->xres_virtual = external_xres_virtual;
2308 	myvar->yres = external_yres;
2309 	myvar->bits_per_pixel = external_depth;
2310 	ext_encode_var(myvar, &dummy_par);
2311 	return 1;
2312 }
2313 
2314 /* ------------------- SuperVidel SuperBlitter ---------------------- */
2315 
2316 /*
2317  * Hardware blitter in the SuperVidel FPGA, operating within SV DDR2 RAM.
2318  * FW revision >= 9 provides a command FIFO (async operation); older
2319  * revisions are programmed directly with busy-polling.
2320  */
2321 #define SVBLIT_REGS_PHYS	0x80010000
2322 #define SVBLIT_SRC1		0x58	/* bits 26:0 */
2323 #define SVBLIT_SRC2		0x5c
2324 #define SVBLIT_DST		0x60
2325 #define SVBLIT_COUNT		0x64	/* bytes per line - 1 */
2326 #define SVBLIT_SRC1_OFFSET	0x68	/* line start to next line start */
2327 #define SVBLIT_SRC2_OFFSET	0x6c
2328 #define SVBLIT_DST_OFFSET	0x70
2329 #define SVBLIT_MASK_AND_LINES	0x74	/* bits 11:0: number of lines */
2330 #define SVBLIT_CONTROL		0x78	/* bit 0: busy/start, bits 4:1: mode */
2331 #define SVBLIT_VERSION		0x7c	/* bits 9:0: FW revision */
2332 #define SVBLIT_FIFO		0x80	/* wr: data; rd: bit 0 empty, bit 1 full */
2333 
2334 /*
2335  * SuperBlitter bug: Instead of declared 2048 bytes, 2032 is the real maximum.
2336  */
2337 #define SVBLIT_MAX_SPAN	2032
2338 
2339 static void __iomem *svblit_regs;
2340 static int svblit_fw;
2341 
2342 static inline u32 svblit_rd(unsigned int reg)
2343 {
2344 	return __raw_readl(svblit_regs + reg);
2345 }
2346 
2347 static inline void svblit_wr(unsigned int reg, u32 val)
2348 {
2349 	__raw_writel(val, svblit_regs + reg);
2350 }
2351 
2352 /* wait until all queued blits have finished */
2353 static void svblit_wait(void)
2354 {
2355 	if (svblit_fw >= 9)
2356 		/* FIFO empty flag = fewer than 9 longwords queued */
2357 		while (!(svblit_rd(SVBLIT_FIFO) & 1))
2358 			cpu_relax();
2359 	while (svblit_rd(SVBLIT_CONTROL) & 1)
2360 		cpu_relax();
2361 }
2362 
2363 /*
2364  * FW >= 9 queues commands through the 512-longword FIFO: a command is
2365  * 9 longwords (registers 0x58..0x78 in order), executed whenever >= 9
2366  * words are queued and the blitter is idle. The full flag rises at
2367  * >= 500 queued words, so below it there is always room for a whole
2368  * command — one flag check per command prevents overflow (dropped
2369  * words would desync the 9-word framing until an SV reinit, which is
2370  * exactly what overflowing did before this guard existed). Older FW
2371  * is programmed directly with busy-polling.
2372  *
2373  * The line byte count field is 11 bits but see SVBLIT_MAX_SPAN.
2374  */
2375 static void svblit_copy(u32 src, u32 dst, u32 nbytes, u32 src_offset,
2376 			u32 dst_offset, u32 lines)
2377 {
2378 	while (nbytes) {
2379 		u32 chunk = min(nbytes, SVBLIT_MAX_SPAN);
2380 
2381 		if (svblit_fw >= 9) {
2382 			while (svblit_rd(SVBLIT_FIFO) & 2)
2383 				cpu_relax();
2384 			svblit_wr(SVBLIT_FIFO, src);
2385 			svblit_wr(SVBLIT_FIFO, 0);
2386 			svblit_wr(SVBLIT_FIFO, dst);
2387 			svblit_wr(SVBLIT_FIFO, chunk - 1);
2388 			svblit_wr(SVBLIT_FIFO, src_offset);
2389 			svblit_wr(SVBLIT_FIFO, 0);
2390 			svblit_wr(SVBLIT_FIFO, dst_offset);
2391 			svblit_wr(SVBLIT_FIFO, lines);
2392 			svblit_wr(SVBLIT_FIFO, 0x01);
2393 		} else {
2394 			while (svblit_rd(SVBLIT_CONTROL) & 1)
2395 				cpu_relax();
2396 			svblit_wr(SVBLIT_SRC1, src);
2397 			svblit_wr(SVBLIT_SRC2, 0);
2398 			svblit_wr(SVBLIT_DST, dst);
2399 			svblit_wr(SVBLIT_COUNT, chunk - 1);
2400 			svblit_wr(SVBLIT_SRC1_OFFSET, src_offset);
2401 			svblit_wr(SVBLIT_SRC2_OFFSET, 0);
2402 			svblit_wr(SVBLIT_DST_OFFSET, dst_offset);
2403 			svblit_wr(SVBLIT_MASK_AND_LINES, lines);
2404 			svblit_wr(SVBLIT_CONTROL, 0x01);
2405 		}
2406 
2407 		src += chunk;
2408 		dst += chunk;
2409 		nbytes -= chunk;
2410 	}
2411 }
2412 
2413 static int svblit_sync(struct fb_info *info)
2414 {
2415 	svblit_wait();
2416 	return 0;
2417 }
2418 
2419 static void svblit_copyarea(struct fb_info *info,
2420 			    const struct fb_copyarea *area)
2421 {
2422 	u32 bytespp = info->var.bits_per_pixel / 8;
2423 	u32 pitch = info->fix.line_length;
2424 
2425 	/*
2426 	 * The blitter walks lines in ascending order, so overlapping
2427 	 * moves down/right would read already overwritten data. Those
2428 	 * are rare for fbcon (scrolling backwards); leave them and
2429 	 * oversized areas to the CPU.
2430 	 */
2431 	if (area->height > 4095 ||
2432 	    area->dy > area->sy ||
2433 	    (area->dy == area->sy && area->dx > area->sx)) {
2434 		svblit_wait();
2435 		cfb_copyarea(info, area);
2436 		return;
2437 	}
2438 
2439 	svblit_copy(external_addr + area->sy * pitch + area->sx * bytespp,
2440 		    external_addr + area->dy * pitch + area->dx * bytespp,
2441 		    area->width * bytespp, pitch, pitch, area->height);
2442 	/* async: every CPU access to the fb goes through svblit_wait() */
2443 }
2444 
2445 static void svblit_fillrect(struct fb_info *info,
2446 			    const struct fb_fillrect *rect)
2447 {
2448 	u32 bytespp = info->var.bits_per_pixel / 8;
2449 	u32 pitch = info->fix.line_length;
2450 	u8 *line;
2451 	u32 pix;
2452 
2453 	svblit_wait();		/* the CPU is about to touch the fb */
2454 
2455 	if (rect->rop != ROP_COPY || rect->height <= 1 ||
2456 	    rect->height > 4096) {
2457 		cfb_fillrect(info, rect);
2458 		return;
2459 	}
2460 
2461 	pix = (info->fix.visual == FB_VISUAL_TRUECOLOR) ?
2462 		((u32 *)info->pseudo_palette)[rect->color] : rect->color;
2463 
2464 	/* draw the first line with the CPU ... */
2465 	line = info->screen_buffer + rect->dy * pitch +
2466 	       rect->dx * bytespp;
2467 	switch (bytespp) {
2468 	case 1:
2469 		memset(line, pix, rect->width);
2470 		break;
2471 	case 2:
2472 		memset16((u16 *)line, pix, rect->width);
2473 		break;
2474 	default:
2475 		memset32((u32 *)line, pix, rect->width);
2476 		break;
2477 	}
2478 
2479 	/* ... and let the blitter replicate it into the other lines */
2480 	svblit_copy(external_addr + rect->dy * pitch + rect->dx * bytespp,
2481 		    external_addr + (rect->dy + 1) * pitch +
2482 		    rect->dx * bytespp,
2483 		    rect->width * bytespp, 0, pitch, rect->height - 1);
2484 }
2485 
2486 static void svblit_imageblit(struct fb_info *info,
2487 			     const struct fb_image *image)
2488 {
2489 	svblit_wait();		/* CPU rendering must not race queued blits */
2490 	cfb_imageblit(info, image);
2491 }
2492 
2493 #endif /* ATAFB_EXT */
2494 
2495 /* ------ This is the same for most hardware types -------- */
2496 
2497 static void set_screen_base(void *s_base)
2498 {
2499 	unsigned long addr;
2500 
2501 	addr = atari_stram_to_phys(s_base);
2502 	/* Setup Screen Memory */
2503 	shifter_st.bas_hi = (unsigned char)((addr & 0xff0000) >> 16);
2504 	shifter_st.bas_md = (unsigned char)((addr & 0x00ff00) >> 8);
2505 	shifter_st.bas_lo = (unsigned char)(addr & 0x0000ff);
2506 }
2507 
2508 static int pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
2509 {
2510 	struct atafb_par *par = info->par;
2511 
2512 	if (!fbhw->set_screen_base ||
2513 	    (!ATARIHW_PRESENT(EXTD_SHIFTER) && var->xoffset))
2514 		return -EINVAL;
2515 	var->xoffset = round_up(var->xoffset, 16);
2516 	par->screen_base = screen_base +
2517 	        (var->yoffset * info->var.xres_virtual + var->xoffset)
2518 	        * info->var.bits_per_pixel / 8;
2519 	fbhw->set_screen_base(par->screen_base);
2520 	return 0;
2521 }
2522 
2523 /* ------------ Interfaces to hardware functions ------------ */
2524 
2525 #ifdef ATAFB_TT
2526 static struct fb_hwswitch tt_switch = {
2527 	.detect		= tt_detect,
2528 	.encode_fix	= tt_encode_fix,
2529 	.decode_var	= tt_decode_var,
2530 	.encode_var	= tt_encode_var,
2531 	.get_par	= tt_get_par,
2532 	.set_par	= tt_set_par,
2533 	.set_screen_base = set_screen_base,
2534 	.pan_display	= pan_display,
2535 };
2536 #endif
2537 
2538 #ifdef ATAFB_FALCON
2539 static struct fb_hwswitch falcon_switch = {
2540 	.detect		= falcon_detect,
2541 	.encode_fix	= falcon_encode_fix,
2542 	.decode_var	= falcon_decode_var,
2543 	.encode_var	= falcon_encode_var,
2544 	.get_par	= falcon_get_par,
2545 	.set_par	= falcon_set_par,
2546 	.set_screen_base = set_screen_base,
2547 	.blank		= falcon_blank,
2548 	.pan_display	= falcon_pan_display,
2549 };
2550 #endif
2551 
2552 #ifdef ATAFB_STE
2553 static struct fb_hwswitch st_switch = {
2554 	.detect		= stste_detect,
2555 	.encode_fix	= stste_encode_fix,
2556 	.decode_var	= stste_decode_var,
2557 	.encode_var	= stste_encode_var,
2558 	.get_par	= stste_get_par,
2559 	.set_par	= stste_set_par,
2560 	.set_screen_base = stste_set_screen_base,
2561 	.pan_display	= pan_display
2562 };
2563 #endif
2564 
2565 #ifdef ATAFB_EXT
2566 static struct fb_hwswitch ext_switch = {
2567 	.detect		= ext_detect,
2568 	.encode_fix	= ext_encode_fix,
2569 	.decode_var	= ext_decode_var,
2570 	.encode_var	= ext_encode_var,
2571 	.get_par	= ext_get_par,
2572 	.set_par	= ext_set_par,
2573 };
2574 #endif
2575 
2576 static void ata_get_par(struct atafb_par *par)
2577 {
2578 	if (current_par_valid)
2579 		*par = current_par;
2580 	else
2581 		fbhw->get_par(par);
2582 }
2583 
2584 static void ata_set_par(struct atafb_par *par)
2585 {
2586 	fbhw->set_par(par);
2587 	current_par = *par;
2588 	current_par_valid = 1;
2589 }
2590 
2591 
2592 /* =========================================================== */
2593 /* ============== Hardware Independent Functions ============= */
2594 /* =========================================================== */
2595 
2596 /* used for hardware scrolling */
2597 
2598 static int do_fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var, int isactive)
2599 {
2600 	int err, activate;
2601 	struct atafb_par par;
2602 
2603 	err = fbhw->decode_var(info, var, &par);
2604 	if (err)
2605 		return err;
2606 	activate = var->activate;
2607 	if (((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) && isactive)
2608 		ata_set_par(&par);
2609 	fbhw->encode_var(var, &par);
2610 	var->activate = activate;
2611 	return 0;
2612 }
2613 
2614 /* fbhw->encode_fix() must be called with fb_info->mm_lock held
2615  * if it is called after the register_framebuffer() - not a case here
2616  */
2617 static int atafb_get_fix(struct fb_fix_screeninfo *fix, struct fb_info *info)
2618 {
2619 	struct atafb_par par;
2620 	int err;
2621 	// Get fix directly (case con == -1 before)??
2622 	err = fbhw->decode_var(info, &info->var, &par);
2623 	if (err)
2624 		return err;
2625 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
2626 	err = fbhw->encode_fix(fix, &par);
2627 	return err;
2628 }
2629 
2630 static int atafb_get_var(struct fb_var_screeninfo *var, struct fb_info *info)
2631 {
2632 	struct atafb_par par;
2633 
2634 	ata_get_par(&par);
2635 	fbhw->encode_var(var, &par);
2636 
2637 	return 0;
2638 }
2639 
2640 // No longer called by fbcon!
2641 // Still called by set_var internally
2642 
2643 static void atafb_set_disp(struct fb_info *info)
2644 {
2645 	atafb_get_var(&info->var, info);
2646 	atafb_get_fix(&info->fix, info);
2647 
2648 	/* Note: smem_start derives from phys_screen_base, not screen_base! */
2649 	info->screen_base = (external_addr ? external_screen_base :
2650 				atari_stram_to_virt(info->fix.smem_start));
2651 }
2652 
2653 static int
2654 atafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
2655 {
2656 	if (!fbhw->pan_display)
2657 		return -EINVAL;
2658 
2659 	return fbhw->pan_display(var, info);
2660 }
2661 
2662 /*
2663  * generic drawing routines; imageblit needs updating for image depth > 1
2664  */
2665 
2666 static void atafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
2667 {
2668 	struct atafb_par *par = info->par;
2669 	int x2, y2;
2670 	u32 width, height;
2671 
2672 	if (!rect->width || !rect->height)
2673 		return;
2674 
2675 #ifdef ATAFB_FALCON
2676 	/* chunky modes (Falcon hicolor, external packed/truecolor) */
2677 	if (info->fix.type == FB_TYPE_PACKED_PIXELS &&
2678 	    info->var.bits_per_pixel > 1) {
2679 		cfb_fillrect(info, rect);
2680 		return;
2681 	}
2682 #endif
2683 
2684 	/*
2685 	 * We could use hardware clipping but on many cards you get around
2686 	 * hardware clipping by writing to framebuffer directly.
2687 	 * */
2688 	x2 = rect->dx + rect->width;
2689 	y2 = rect->dy + rect->height;
2690 	x2 = x2 < info->var.xres_virtual ? x2 : info->var.xres_virtual;
2691 	y2 = y2 < info->var.yres_virtual ? y2 : info->var.yres_virtual;
2692 	width = x2 - rect->dx;
2693 	height = y2 - rect->dy;
2694 
2695 	if (info->var.bits_per_pixel == 1)
2696 		atafb_mfb_fillrect(info, par->next_line, rect->color,
2697 				   rect->dy, rect->dx, height, width);
2698 	else if (info->var.bits_per_pixel == 2)
2699 		atafb_iplan2p2_fillrect(info, par->next_line, rect->color,
2700 					rect->dy, rect->dx, height, width);
2701 	else if (info->var.bits_per_pixel == 4)
2702 		atafb_iplan2p4_fillrect(info, par->next_line, rect->color,
2703 					rect->dy, rect->dx, height, width);
2704 	else
2705 		atafb_iplan2p8_fillrect(info, par->next_line, rect->color,
2706 					rect->dy, rect->dx, height, width);
2707 
2708 	return;
2709 }
2710 
2711 static void atafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
2712 {
2713 	struct atafb_par *par = info->par;
2714 	int x2, y2;
2715 	u32 dx, dy, sx, sy, width, height;
2716 	int rev_copy = 0;
2717 
2718 #ifdef ATAFB_FALCON
2719 	/* chunky modes (Falcon hicolor, external packed/truecolor) */
2720 	if (info->fix.type == FB_TYPE_PACKED_PIXELS &&
2721 	    info->var.bits_per_pixel > 1) {
2722 		cfb_copyarea(info, area);
2723 		return;
2724 	}
2725 #endif
2726 
2727 	/* clip the destination */
2728 	x2 = area->dx + area->width;
2729 	y2 = area->dy + area->height;
2730 	dx = area->dx > 0 ? area->dx : 0;
2731 	dy = area->dy > 0 ? area->dy : 0;
2732 	x2 = x2 < info->var.xres_virtual ? x2 : info->var.xres_virtual;
2733 	y2 = y2 < info->var.yres_virtual ? y2 : info->var.yres_virtual;
2734 	width = x2 - dx;
2735 	height = y2 - dy;
2736 
2737 	if (area->sx + dx < area->dx || area->sy + dy < area->dy)
2738 		return;
2739 
2740 	/* update sx,sy */
2741 	sx = area->sx + (dx - area->dx);
2742 	sy = area->sy + (dy - area->dy);
2743 
2744 	/* the source must be completely inside the virtual screen */
2745 	if (sx + width > info->var.xres_virtual ||
2746 			sy + height > info->var.yres_virtual)
2747 		return;
2748 
2749 	if (dy > sy || (dy == sy && dx > sx)) {
2750 		dy += height;
2751 		sy += height;
2752 		rev_copy = 1;
2753 	}
2754 
2755 	if (info->var.bits_per_pixel == 1)
2756 		atafb_mfb_copyarea(info, par->next_line, sy, sx, dy, dx, height, width);
2757 	else if (info->var.bits_per_pixel == 2)
2758 		atafb_iplan2p2_copyarea(info, par->next_line, sy, sx, dy, dx, height, width);
2759 	else if (info->var.bits_per_pixel == 4)
2760 		atafb_iplan2p4_copyarea(info, par->next_line, sy, sx, dy, dx, height, width);
2761 	else
2762 		atafb_iplan2p8_copyarea(info, par->next_line, sy, sx, dy, dx, height, width);
2763 
2764 	return;
2765 }
2766 
2767 static void atafb_imageblit(struct fb_info *info, const struct fb_image *image)
2768 {
2769 	struct atafb_par *par = info->par;
2770 	int x2, y2;
2771 	const char *src;
2772 	u32 dx, dy, width, height, pitch;
2773 
2774 #ifdef ATAFB_FALCON
2775 	/* chunky modes (Falcon hicolor, external packed/truecolor) */
2776 	if (info->fix.type == FB_TYPE_PACKED_PIXELS &&
2777 	    info->var.bits_per_pixel > 1) {
2778 		cfb_imageblit(info, image);
2779 		return;
2780 	}
2781 #endif
2782 
2783 	/*
2784 	 * We could use hardware clipping but on many cards you get around
2785 	 * hardware clipping by writing to framebuffer directly like we are
2786 	 * doing here.
2787 	 */
2788 	x2 = image->dx + image->width;
2789 	y2 = image->dy + image->height;
2790 	dx = image->dx;
2791 	dy = image->dy;
2792 	x2 = x2 < info->var.xres_virtual ? x2 : info->var.xres_virtual;
2793 	y2 = y2 < info->var.yres_virtual ? y2 : info->var.yres_virtual;
2794 	width = x2 - dx;
2795 	height = y2 - dy;
2796 
2797 	if (image->depth == 1) {
2798 		// used for font data
2799 		src = image->data;
2800 		pitch = (image->width + 7) / 8;
2801 		while (height--) {
2802 
2803 			if (info->var.bits_per_pixel == 1)
2804 				atafb_mfb_linefill(info, par->next_line,
2805 						   dy, dx, width, src,
2806 						   image->bg_color, image->fg_color);
2807 			else if (info->var.bits_per_pixel == 2)
2808 				atafb_iplan2p2_linefill(info, par->next_line,
2809 							dy, dx, width, src,
2810 							image->bg_color, image->fg_color);
2811 			else if (info->var.bits_per_pixel == 4)
2812 				atafb_iplan2p4_linefill(info, par->next_line,
2813 							dy, dx, width, src,
2814 							image->bg_color, image->fg_color);
2815 			else
2816 				atafb_iplan2p8_linefill(info, par->next_line,
2817 							dy, dx, width, src,
2818 							image->bg_color, image->fg_color);
2819 			dy++;
2820 			src += pitch;
2821 		}
2822 	} else {
2823 		c2p_iplan2(info->screen_base, image->data, dx, dy, width,
2824 			   height, par->next_line, image->width,
2825 			   info->var.bits_per_pixel);
2826 	}
2827 }
2828 
2829 /* (un)blank/poweroff
2830  * 0 = unblank
2831  * 1 = blank
2832  * 2 = suspend vsync
2833  * 3 = suspend hsync
2834  * 4 = off
2835  */
2836 static int atafb_blank(int blank, struct fb_info *info)
2837 {
2838 	unsigned short black[16];
2839 	struct fb_cmap cmap;
2840 	if (fbhw->blank && !fbhw->blank(blank))
2841 		return 1;
2842 	if (blank) {
2843 		memset(black, 0, 16 * sizeof(unsigned short));
2844 		cmap.red = black;
2845 		cmap.green = black;
2846 		cmap.blue = black;
2847 		cmap.transp = NULL;
2848 		cmap.start = 0;
2849 		cmap.len = 16;
2850 		fb_set_cmap(&cmap, info);
2851 	}
2852 #if 0
2853 	else
2854 		do_install_cmap(info);
2855 #endif
2856 	return 0;
2857 }
2858 
2859 	/*
2860 	 * New fbcon interface ...
2861 	 */
2862 
2863 	 /* check var by decoding var into hw par, rounding if necessary,
2864 	  * then encoding hw par back into new, validated var */
2865 static int atafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
2866 {
2867 	int err;
2868 	struct atafb_par par;
2869 
2870 	/* Validate wanted screen parameters */
2871 	// if ((err = ata_decode_var(var, &par)))
2872 	err = fbhw->decode_var(info, var, &par);
2873 	if (err)
2874 		return err;
2875 
2876 	/* Encode (possibly rounded) screen parameters */
2877 	fbhw->encode_var(var, &par);
2878 	return 0;
2879 }
2880 
2881 	/* actually set hw par by decoding var, then setting hardware from
2882 	 * hw par just decoded */
2883 static int atafb_set_par(struct fb_info *info)
2884 {
2885 	struct atafb_par *par = info->par;
2886 
2887 	/* Decode wanted screen parameters */
2888 	fbhw->decode_var(info, &info->var, par);
2889 	mutex_lock(&info->mm_lock);
2890 	fbhw->encode_fix(&info->fix, par);
2891 	mutex_unlock(&info->mm_lock);
2892 
2893 	/* Set new videomode */
2894 	ata_set_par(par);
2895 
2896 	return 0;
2897 }
2898 
2899 
2900 static struct fb_ops atafb_ops = {
2901 	.owner =	THIS_MODULE,
2902 	__FB_DEFAULT_IOMEM_OPS_RDWR,
2903 	.fb_check_var	= atafb_check_var,
2904 	.fb_set_par	= atafb_set_par,
2905 	.fb_blank =	atafb_blank,
2906 	.fb_pan_display	= atafb_pan_display,
2907 	.fb_fillrect	= atafb_fillrect,
2908 	.fb_copyarea	= atafb_copyarea,
2909 	.fb_imageblit	= atafb_imageblit,
2910 	__FB_DEFAULT_IOMEM_OPS_MMAP,
2911 };
2912 
2913 static void check_default_par(struct fb_info *info, int detected_mode)
2914 {
2915 	char default_name[10];
2916 	int i;
2917 	struct fb_var_screeninfo var;
2918 	unsigned long min_mem;
2919 
2920 	/* First try the user supplied mode */
2921 	if (default_par) {
2922 		var = atafb_predefined[default_par - 1];
2923 		var.activate = FB_ACTIVATE_TEST;
2924 		if (do_fb_set_var(info, &var, 1))
2925 			default_par = 0;	/* failed */
2926 	}
2927 	/* Next is the autodetected one */
2928 	if (!default_par) {
2929 		var = atafb_predefined[detected_mode - 1]; /* autodetect */
2930 		var.activate = FB_ACTIVATE_TEST;
2931 		if (!do_fb_set_var(info, &var, 1))
2932 			default_par = detected_mode;
2933 	}
2934 	/* If that also failed, try some default modes... */
2935 	if (!default_par) {
2936 		/* try default1, default2... */
2937 		for (i = 1; i < 10; i++) {
2938 			sprintf(default_name,"default%d", i);
2939 			default_par = get_video_mode(default_name);
2940 			if (!default_par)
2941 				panic("can't set default video mode");
2942 			var = atafb_predefined[default_par - 1];
2943 			var.activate = FB_ACTIVATE_TEST;
2944 			if (!do_fb_set_var(info, &var, 1))
2945 				break;	/* ok */
2946 		}
2947 	}
2948 	min_mem = var.xres_virtual * var.yres_virtual * var.bits_per_pixel / 8;
2949 	if (default_mem_req < min_mem)
2950 		default_mem_req = min_mem;
2951 }
2952 
2953 #ifdef ATAFB_EXT
2954 static void __init atafb_setup_ext(char *spec)
2955 {
2956 	int xres, xres_virtual, yres, depth, planes;
2957 	unsigned long addr, len;
2958 	char *p;
2959 
2960 	/* Format is: <xres>;<yres>;<depth>;<plane organ.>;
2961 	 *            <screen mem addr>
2962 	 *	      [;<screen mem length>[;<vgaiobase>[;<bits-per-col>[;<colorreg-type>
2963 	 *	      [;<xres-virtual>]]]]]
2964 	 *
2965 	 * 09/23/97	Juergen
2966 	 * <xres_virtual>:	hardware's x-resolution (f.e. ProMST)
2967 	 *
2968 	 * Even xres_virtual is available, we neither support panning nor hw-scrolling!
2969 	 */
2970 	p = strsep(&spec, ";");
2971 	if (!p || !*p)
2972 		return;
2973 	xres_virtual = xres = simple_strtoul(p, NULL, 10);
2974 	if (xres <= 0)
2975 		return;
2976 
2977 	p = strsep(&spec, ";");
2978 	if (!p || !*p)
2979 		return;
2980 	yres = simple_strtoul(p, NULL, 10);
2981 	if (yres <= 0)
2982 		return;
2983 
2984 	p = strsep(&spec, ";");
2985 	if (!p || !*p)
2986 		return;
2987 	depth = simple_strtoul(p, NULL, 10);
2988 	if (depth != 1 && depth != 2 && depth != 4 && depth != 8 &&
2989 	    depth != 16 && depth != 24 && depth != 32)
2990 		return;
2991 
2992 	p = strsep(&spec, ";");
2993 	if (!p || !*p)
2994 		return;
2995 	if (*p == 'i')
2996 		planes = FB_TYPE_INTERLEAVED_PLANES;
2997 	else if (*p == 'p')
2998 		planes = FB_TYPE_PACKED_PIXELS;
2999 	else if (*p == 'n')
3000 		planes = FB_TYPE_PLANES;
3001 	else if (*p == 't')
3002 		planes = -1;		/* true color */
3003 	else
3004 		return;
3005 
3006 	p = strsep(&spec, ";");
3007 	if (!p || !*p)
3008 		return;
3009 	addr = simple_strtoul(p, NULL, 0);
3010 
3011 	p = strsep(&spec, ";");
3012 	if (!p || !*p)
3013 		len = xres * yres * depth / 8;
3014 	else
3015 		len = simple_strtoul(p, NULL, 0);
3016 
3017 	p = strsep(&spec, ";");
3018 	if (p && *p)
3019 		external_vgaiobase = simple_strtoul(p, NULL, 0);
3020 
3021 	p = strsep(&spec, ";");
3022 	if (p && *p) {
3023 		external_bitspercol = simple_strtoul(p, NULL, 0);
3024 		if (external_bitspercol > 8)
3025 			external_bitspercol = 8;
3026 		else if (external_bitspercol < 1)
3027 			external_bitspercol = 1;
3028 	}
3029 
3030 	p = strsep(&spec, ";");
3031 	if (p && *p) {
3032 		if (!strcmp(p, "vga"))
3033 			external_card_type = IS_VGA;
3034 		if (!strcmp(p, "mv300"))
3035 			external_card_type = IS_MV300;
3036 	}
3037 
3038 	p = strsep(&spec, ";");
3039 	if (p && *p) {
3040 		xres_virtual = simple_strtoul(p, NULL, 10);
3041 		if (xres_virtual < xres)
3042 			xres_virtual = xres;
3043 		if (xres_virtual * yres * depth / 8 > len)
3044 			len = xres_virtual * yres * depth / 8;
3045 	}
3046 
3047 	external_xres = xres;
3048 	external_xres_virtual = xres_virtual;
3049 	external_yres = yres;
3050 	external_depth = depth;
3051 	external_pmode = planes;
3052 	external_addr = addr;
3053 	external_len = len;
3054 
3055 	if (external_card_type == IS_MV300) {
3056 		switch (external_depth) {
3057 		case 1:
3058 			MV300_reg = MV300_reg_1bit;
3059 			break;
3060 		case 4:
3061 			MV300_reg = MV300_reg_4bit;
3062 			break;
3063 		case 8:
3064 			MV300_reg = MV300_reg_8bit;
3065 			break;
3066 		}
3067 	}
3068 }
3069 #endif /* ATAFB_EXT */
3070 
3071 static void __init atafb_setup_int(char *spec)
3072 {
3073 	/* Format to config extended internal video hardware like OverScan:
3074 	 * "internal:<xres>;<yres>;<xres_max>;<yres_max>;<offset>"
3075 	 * Explanation:
3076 	 * <xres>: x-resolution
3077 	 * <yres>: y-resolution
3078 	 * The following are only needed if you have an overscan which
3079 	 * needs a black border:
3080 	 * <xres_max>: max. length of a line in pixels your OverScan hardware would allow
3081 	 * <yres_max>: max. number of lines your OverScan hardware would allow
3082 	 * <offset>: Offset from physical beginning to visible beginning
3083 	 *	  of screen in bytes
3084 	 */
3085 	int xres;
3086 	char *p;
3087 
3088 	if (!(p = strsep(&spec, ";")) || !*p)
3089 		return;
3090 	xres = simple_strtoul(p, NULL, 10);
3091 	if (!(p = strsep(&spec, ";")) || !*p)
3092 		return;
3093 	sttt_xres = xres;
3094 	tt_yres = st_yres = simple_strtoul(p, NULL, 10);
3095 	if ((p = strsep(&spec, ";")) && *p)
3096 		sttt_xres_virtual = simple_strtoul(p, NULL, 10);
3097 	if ((p = strsep(&spec, ";")) && *p)
3098 		sttt_yres_virtual = simple_strtoul(p, NULL, 0);
3099 	if ((p = strsep(&spec, ";")) && *p)
3100 		ovsc_offset = simple_strtoul(p, NULL, 0);
3101 
3102 	if (ovsc_offset || (sttt_yres_virtual != st_yres))
3103 		use_hwscroll = 0;
3104 }
3105 
3106 #ifdef ATAFB_FALCON
3107 static void __init atafb_setup_mcap(char *spec)
3108 {
3109 	char *p;
3110 	int vmin, vmax, hmin, hmax;
3111 
3112 	/* Format for monitor capabilities is: <Vmin>;<Vmax>;<Hmin>;<Hmax>
3113 	 * <V*> vertical freq. in Hz
3114 	 * <H*> horizontal freq. in kHz
3115 	 */
3116 	if (!(p = strsep(&spec, ";")) || !*p)
3117 		return;
3118 	vmin = simple_strtoul(p, NULL, 10);
3119 	if (vmin <= 0)
3120 		return;
3121 	if (!(p = strsep(&spec, ";")) || !*p)
3122 		return;
3123 	vmax = simple_strtoul(p, NULL, 10);
3124 	if (vmax <= 0 || vmax <= vmin)
3125 		return;
3126 	if (!(p = strsep(&spec, ";")) || !*p)
3127 		return;
3128 	hmin = 1000 * simple_strtoul(p, NULL, 10);
3129 	if (hmin <= 0)
3130 		return;
3131 	if (!(p = strsep(&spec, "")) || !*p)
3132 		return;
3133 	hmax = 1000 * simple_strtoul(p, NULL, 10);
3134 	if (hmax <= 0 || hmax <= hmin)
3135 		return;
3136 
3137 	mcap_vmin = vmin;
3138 	mcap_vmax = vmax;
3139 	mcap_hmin = hmin;
3140 	mcap_hmax = hmax;
3141 }
3142 #endif /* ATAFB_FALCON */
3143 
3144 static void __init atafb_setup_user(char *spec)
3145 {
3146 	/* Format of user defined video mode is: <xres>;<yres>;<depth>
3147 	 */
3148 	char *p;
3149 	int xres, yres, depth, temp;
3150 
3151 	p = strsep(&spec, ";");
3152 	if (!p || !*p)
3153 		return;
3154 	xres = simple_strtoul(p, NULL, 10);
3155 	p = strsep(&spec, ";");
3156 	if (!p || !*p)
3157 		return;
3158 	yres = simple_strtoul(p, NULL, 10);
3159 	p = strsep(&spec, "");
3160 	if (!p || !*p)
3161 		return;
3162 	depth = simple_strtoul(p, NULL, 10);
3163 	temp = get_video_mode("user0");
3164 	if (temp) {
3165 		default_par = temp;
3166 		atafb_predefined[default_par - 1].xres = xres;
3167 		atafb_predefined[default_par - 1].yres = yres;
3168 		atafb_predefined[default_par - 1].bits_per_pixel = depth;
3169 	}
3170 }
3171 
3172 static int __init atafb_setup(char *options)
3173 {
3174 	char *this_opt;
3175 	int temp;
3176 
3177 	if (!options || !*options)
3178 		return 0;
3179 
3180 	while ((this_opt = strsep(&options, ",")) != NULL) {
3181 		if (!*this_opt)
3182 			continue;
3183 		if ((temp = get_video_mode(this_opt))) {
3184 			default_par = temp;
3185 			mode_option = this_opt;
3186 		} else if (!strcmp(this_opt, "inverse"))
3187 			fb_invert_cmaps();
3188 		else if (!strncmp(this_opt, "hwscroll_", 9)) {
3189 			hwscroll = simple_strtoul(this_opt + 9, NULL, 10);
3190 			if (hwscroll < 0)
3191 				hwscroll = 0;
3192 			if (hwscroll > 200)
3193 				hwscroll = 200;
3194 		}
3195 #ifdef ATAFB_EXT
3196 		else if (!strcmp(this_opt, "mv300")) {
3197 			external_bitspercol = 8;
3198 			external_card_type = IS_MV300;
3199 		} else if (!strncmp(this_opt, "external:", 9))
3200 			atafb_setup_ext(this_opt + 9);
3201 #endif
3202 		else if (!strncmp(this_opt, "internal:", 9))
3203 			atafb_setup_int(this_opt + 9);
3204 #ifdef ATAFB_FALCON
3205 		else if (!strncmp(this_opt, "eclock:", 7)) {
3206 			fext.f = simple_strtoul(this_opt + 7, NULL, 10);
3207 			/* external pixelclock in kHz --> ps */
3208 			fext.t = 1000000000 / fext.f;
3209 			fext.f *= 1000;
3210 		} else if (!strncmp(this_opt, "monitorcap:", 11))
3211 			atafb_setup_mcap(this_opt + 11);
3212 #endif
3213 		else if (!strcmp(this_opt, "keep"))
3214 			DontCalcRes = 1;
3215 		else if (!strncmp(this_opt, "R", 1))
3216 			atafb_setup_user(this_opt + 1);
3217 	}
3218 	return 0;
3219 }
3220 
3221 static int __init atafb_probe(struct platform_device *pdev)
3222 {
3223 	int pad, detected_mode, error;
3224 	unsigned int defmode = 0;
3225 	unsigned long mem_req;
3226 	char *option = NULL;
3227 	struct fb_info *fb_info;
3228 
3229 	if (fb_get_options("atafb", &option))
3230 		return -ENODEV;
3231 	atafb_setup(option);
3232 	dev_dbg(&pdev->dev, "%s: start\n", __func__);
3233 
3234 	fb_info =  framebuffer_alloc(sizeof(struct atafb_par), &pdev->dev);
3235 
3236 	strscpy(fb_info->fix.id, "Atari ");
3237 	fb_info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
3238 	fb_info->fix.accel = FB_ACCEL_NONE;
3239 
3240 	do {
3241 #ifdef ATAFB_EXT
3242 		if (external_addr) {
3243 			dev_dbg(&pdev->dev, "initializing external hw\n");
3244 			fbhw = &ext_switch;
3245 			atafb_ops.fb_setcolreg = &ext_setcolreg;
3246 			defmode = DEFMODE_EXT;
3247 			break;
3248 		}
3249 #endif
3250 #ifdef ATAFB_TT
3251 		if (ATARIHW_PRESENT(TT_SHIFTER)) {
3252 			dev_dbg(&pdev->dev, "initializing TT hw\n");
3253 			fbhw = &tt_switch;
3254 			atafb_ops.fb_setcolreg = &tt_setcolreg;
3255 			defmode = DEFMODE_TT;
3256 			break;
3257 		}
3258 #endif
3259 #ifdef ATAFB_FALCON
3260 		if (ATARIHW_PRESENT(VIDEL_SHIFTER)) {
3261 			dev_dbg(&pdev->dev, "initializing Falcon hw\n");
3262 			fbhw = &falcon_switch;
3263 			atafb_ops.fb_setcolreg = &falcon_setcolreg;
3264 			error = request_irq(IRQ_AUTO_4, falcon_vbl_switcher, 0,
3265 					    "framebuffer:modeswitch",
3266 					    falcon_vbl_switcher);
3267 			if (error)
3268 				return error;
3269 			defmode = DEFMODE_F30;
3270 			break;
3271 		}
3272 #endif
3273 #ifdef ATAFB_STE
3274 		if (ATARIHW_PRESENT(STND_SHIFTER) ||
3275 		    ATARIHW_PRESENT(EXTD_SHIFTER)) {
3276 			dev_dbg(&pdev->dev, "initializing ST/E hw\n");
3277 			fbhw = &st_switch;
3278 			atafb_ops.fb_setcolreg = &stste_setcolreg;
3279 			defmode = DEFMODE_STE;
3280 			break;
3281 		}
3282 		fbhw = &st_switch;
3283 		atafb_ops.fb_setcolreg = &stste_setcolreg;
3284 		dev_warn(&pdev->dev,
3285 			 "Cannot determine video hardware; defaulting to ST(e)\n");
3286 #else /* ATAFB_STE */
3287 		/* no default driver included */
3288 		/* Nobody will ever see this message :-) */
3289 		panic("Cannot initialize video hardware");
3290 #endif
3291 	} while (0);
3292 
3293 	/* Multisync monitor capabilities */
3294 	/* Atari-TOS defaults if no boot option present */
3295 	if (mcap_hmin == 0) {
3296 		fb_info->monspecs.hfmin = 31000;
3297 		fb_info->monspecs.hfmax = 32000;
3298 		fb_info->monspecs.vfmin = 58;
3299 		fb_info->monspecs.vfmax = 62;
3300 	} else {
3301 		fb_info->monspecs.vfmin = mcap_vmin;
3302 		fb_info->monspecs.vfmax = mcap_vmax;
3303 		fb_info->monspecs.hfmin = mcap_hmin;
3304 		fb_info->monspecs.hfmax = mcap_hmax;
3305 	}
3306 
3307 	detected_mode = fbhw->detect(fb_info);
3308 	check_default_par(fb_info, detected_mode);
3309 #ifdef ATAFB_EXT
3310 	if (!external_addr) {
3311 #endif /* ATAFB_EXT */
3312 		mem_req = default_mem_req + ovsc_offset + ovsc_addlen;
3313 		mem_req = PAGE_ALIGN(mem_req) + PAGE_SIZE;
3314 		screen_base = atari_stram_alloc(mem_req, "atafb");
3315 		if (!screen_base)
3316 			panic("Cannot allocate screen memory");
3317 		memset(screen_base, 0, mem_req);
3318 		pad = -(unsigned long)screen_base & (PAGE_SIZE - 1);
3319 		screen_base += pad;
3320 		phys_screen_base = atari_stram_to_phys(screen_base + ovsc_offset);
3321 		screen_len = (mem_req - pad - ovsc_offset) & PAGE_MASK;
3322 		st_ovsc_switch();
3323 		if (CPU_IS_040_OR_060) {
3324 			/* On a '040+, the cache mode of video RAM must be set to
3325 			 * write-through also for internal video hardware! */
3326 			cache_push(atari_stram_to_phys(screen_base), screen_len);
3327 			kernel_set_cachemode(screen_base, screen_len,
3328 					     IOMAP_WRITETHROUGH);
3329 		}
3330 		dev_info(&pdev->dev, "phys_screen_base %lx screen_len %d\n",
3331 			 phys_screen_base, screen_len);
3332 #ifdef ATAFB_EXT
3333 	} else {
3334 		/* Map the video memory (physical address given) to somewhere
3335 		 * in the kernel address space.
3336 		 */
3337 		external_screen_base = ioremap_wt(external_addr, external_len);
3338 		if (external_vgaiobase)
3339 			external_vgaiobase =
3340 			  (unsigned long)ioremap(external_vgaiobase, 0x10000);
3341 		screen_base = external_screen_base;
3342 		phys_screen_base = external_addr;
3343 		screen_len = external_len & PAGE_MASK;
3344 		memset (screen_base, 0, external_len);
3345 
3346 		/* framebuffer in SV RAM: enable the SuperBlitter */
3347 		if (ext_is_supervidel() &&
3348 		    ((external_pmode == FB_TYPE_PACKED_PIXELS &&
3349 		      external_depth == 8) ||
3350 		     (external_pmode == -1 &&
3351 		      (external_depth == 16 || external_depth == 32)))) {
3352 			svblit_regs = ioremap(SVBLIT_REGS_PHYS, 0x100);
3353 			if (svblit_regs) {
3354 				svblit_fw = svblit_rd(SVBLIT_VERSION) & 0x1ff;
3355 				atafb_ops.fb_fillrect = svblit_fillrect;
3356 				atafb_ops.fb_copyarea = svblit_copyarea;
3357 				atafb_ops.fb_imageblit = svblit_imageblit;
3358 				atafb_ops.fb_sync = svblit_sync;
3359 				fb_info->flags |= FBINFO_HWACCEL_COPYAREA |
3360 						  FBINFO_HWACCEL_FILLRECT;
3361 				dev_info(&pdev->dev,
3362 					 "SuperBlitter enabled, FW revision %d (%s)\n",
3363 					 svblit_fw, svblit_fw >= 9 ?
3364 					 "async FIFO" : "sync");
3365 			}
3366 		}
3367 	}
3368 #endif /* ATAFB_EXT */
3369 
3370 //	strcpy(fb_info->mode->name, "Atari Builtin ");
3371 	fb_info->fbops = &atafb_ops;
3372 	// try to set default (detected; requested) var
3373 	do_fb_set_var(fb_info, &atafb_predefined[default_par - 1], 1);
3374 	// reads hw state into current par, which may not be sane yet
3375 	ata_get_par(&current_par);
3376 	fb_info->par = &current_par;
3377 	// tries to read from HW which may not be initialized yet
3378 	// so set sane var first, then call atafb_set_par
3379 	atafb_get_var(&(fb_info->var), fb_info);
3380 
3381 #ifdef ATAFB_FALCON
3382 	fb_info->pseudo_palette = current_par.hw.falcon.pseudo_palette;
3383 #endif
3384 
3385 	if (!fb_find_mode(&(fb_info->var), fb_info, mode_option, atafb_modedb,
3386 			  NUM_TOTAL_MODES, &atafb_modedb[defmode],
3387 			  fb_info->var.bits_per_pixel)) {
3388 		return -EINVAL;
3389 	}
3390 
3391 	fb_videomode_to_modelist(atafb_modedb, NUM_TOTAL_MODES,
3392 				 &(fb_info->modelist));
3393 
3394 	atafb_set_disp(fb_info);
3395 
3396 	/* truecolor visuals only need the 16-entry console palette; this
3397 	 * also avoids 1 << 32 overflowing at 32bpp
3398 	 */
3399 	fb_alloc_cmap(&(fb_info->cmap), fb_info->var.bits_per_pixel > 8 ?
3400 		      16 : 1 << fb_info->var.bits_per_pixel, 0);
3401 
3402 	dev_info(&pdev->dev, "Determined %dx%d, depth %d\n", fb_info->var.xres,
3403 		 fb_info->var.yres, fb_info->var.bits_per_pixel);
3404 	if ((fb_info->var.xres != fb_info->var.xres_virtual) ||
3405 	    (fb_info->var.yres != fb_info->var.yres_virtual))
3406 		dev_info(&pdev->dev, "   virtual %dx%d\n",
3407 			 fb_info->var.xres_virtual, fb_info->var.yres_virtual);
3408 
3409 	if (register_framebuffer(fb_info) < 0) {
3410 #ifdef ATAFB_EXT
3411 		if (external_addr) {
3412 			iounmap(external_screen_base);
3413 			external_addr = 0;
3414 		}
3415 		if (external_vgaiobase) {
3416 			iounmap((void*)external_vgaiobase);
3417 			external_vgaiobase = 0;
3418 		}
3419 #endif
3420 		return -EINVAL;
3421 	}
3422 
3423 	fb_info(fb_info, "frame buffer device, using %dK of video memory\n",
3424 		screen_len >> 10);
3425 
3426 	/* TODO: This driver cannot be unloaded yet */
3427 	return 0;
3428 }
3429 
3430 static void atafb_shutdown(struct platform_device *pdev)
3431 {
3432 	/* Unblank before kexec */
3433 	if (fbhw->blank)
3434 		fbhw->blank(0);
3435 }
3436 
3437 static struct platform_driver atafb_driver = {
3438 	.shutdown	= atafb_shutdown,
3439 	.driver	= {
3440 		.name	= "atafb",
3441 	},
3442 };
3443 
3444 static int __init atafb_init(void)
3445 {
3446 	struct platform_device *pdev;
3447 
3448 	if (!MACH_IS_ATARI)
3449 		return -ENODEV;
3450 
3451 	pdev = platform_device_register_simple("atafb", -1, NULL, 0);
3452 	if (IS_ERR(pdev))
3453 		return PTR_ERR(pdev);
3454 
3455 	return platform_driver_probe(&atafb_driver, atafb_probe);
3456 }
3457 
3458 device_initcall(atafb_init);
3459