make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / video / lynxfb.c
1 /*
2  *  drivers/video/lynxfb.c -- frame buffer device for
3  *  Silicon Motion Lynx3DM chip.
4  *
5  *  This was written to drive the Lynx board in the Embedded Planet
6  *  Icebox (embedded PPC machine) and is only tested on that machine.
7  *  It should really be generalised quite a bit - for one thing it is
8  *  hard-wired to 640x480.
9  *
10  *  Copyright (C) 2002 David Gibson, IBM Corporation.
11  *
12  *  Based on chipsfb.c:
13  *  Copyright (C) 1998 Paul Mackerras,
14  *  which in turn is derived from the Powermac "chips" driver:
15  *  Copyright (C) 1997 Fabio Riccardi.
16  *  And from the frame buffer device for Open Firmware-initialized devices:
17  *  Copyright (C) 1997 Geert Uytterhoeven.
18  *
19  *  This file is subject to the terms and conditions of the GNU General Public
20  *  License. See the file COPYING in the main directory of this archive for
21  *  more details.  */
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include <linux/tty.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/fb.h>
35 #include <linux/selection.h>
36 #include <linux/init.h>
37 #include <linux/pci.h>
38 #include <asm/io.h>
39
40 #include <video/fbcon.h>
41 #include <video/fbcon-cfb8.h>
42 #include <video/fbcon-cfb16.h>
43 #include <video/fbcon-cfb24.h>
44 #include <video/macmodes.h>
45
46 MODULE_LICENSE("GPL");
47
48 struct fb_info_lynx {
49         struct fb_info info;
50         struct fb_fix_screeninfo fix;
51         struct fb_var_screeninfo var;
52         struct display disp;
53         struct {
54                 u8 red, green, blue;
55         } palette[256];
56         struct pci_dev *pdev;
57
58         u8 chiprev;
59         unsigned long ramsize;
60
61         unsigned long aperture_phys;
62         u8 *aperture;
63         unsigned long frame_buffer_phys;
64         u8 *frame_buffer;
65
66         struct fb_info_lynx *next;
67 #ifdef FBCON_HAS_CFB16
68         u16 fbcon_cfb16_cmap[16];
69 #endif
70 #ifdef FBCON_HAS_CFB16
71         u32 fbcon_cfb24_cmap[16];
72 #endif
73 };
74 static struct fb_info_lynx *all_lynx;
75 static int currcon; /* = 0 */
76
77 #define MMIO_DPR_OFFSET (0x000)
78 #define MMIO_VPR_OFFSET (0x800)
79 #define MMIO_IO_OFFSET  (0xc0000)
80
81 #if 0
82 #define write_ind(lp, num, val, ap, dp) do { \
83                 writeb((num), (lp)->aperture + MMIO_IO_OFFSET + (ap)); \
84                 writeb((val), (lp)->aperture + MMIO_IO_OFFSET + (dp)); \
85         } while (0)
86 #define read_ind(lp, num, ap, dp)       ({ writeb((num), (lp)->aperture + MMIO_IO_OFFSET + (ap)); \
87         readb((lp)->aperture + MMIO_IO_OFFSET + (dp));})
88 #else
89 #define write_ind(lp, num, val, ap, dp) do { \
90                 outb((num), (ap)); outb((val), (dp)); \
91         } while (0)
92 #define read_ind(lp, num, ap, dp)       ({ outb((num), (ap)); inb((dp)); })
93 #endif
94
95 /* CRTC registers */
96 #define write_crt(lp, num, val) write_ind(lp, num, val, 0x3d4, 0x3d5)
97 #define read_crt(lp, num)       read_ind(lp, num, 0x3d4, 0x3d5)
98 /* graphics registers */
99 #define write_grx(lp, num, val) write_ind(lp, num, val, 0x3ce, 0x3cf)
100 #define read_grx(lp, num)       read_ind(lp, num, 0x3ce, 0x3cf)
101 /* sequencer registers */
102 #define write_seq(lp, num, val) write_ind(lp, num, val, 0x3c4, 0x3c5)
103 #define read_seq(lp, num)       read_ind(lp, num, 0x3c4, 0x3c5)
104 /* system control registers */
105 #define write_scr write_seq
106 #define read_scr read_seq
107 /* flat panel registers */
108 #define write_fpr write_seq
109 #define read_fpr read_fpr
110 /* memory control registers */
111 #define write_mcr write_seq
112 #define read_mcr read_seq
113
114 /* attribute controller */
115 #define write_atr(lp, num, val) do { \
116                 inb(0x3da); write_ind(lp, num, val, 0x3c0, 0x3c0); \
117         } while (0)
118 #define read_atr(lp, num) ({inb(0x3da); read_ind(lp, num, 0x3c0, 0x3c1); })
119
120 /* Drawing processor */
121 #define write_dprw(lp, off, val) writew((val), (lp)->aperture + MMIO_DPR_OFFSET + (off))
122 #define read_dprw(lp, off) readw((lp)->aperture + MMIO_DPR_OFFSET + (off))
123 #define write_dprl(lp, off, val) writel((val), (lp)->aperture + MMIO_DPR_OFFSET + (off))
124 #define read_dprl(lp, off) readl((lp)->aperture + MMIO_DPR_OFFSET + (off))
125 /* Video processor */
126 #define write_vprw(lp, off, val) writew((val), (lp)->aperture + MMIO_VPR_OFFSET + (off))
127 #define read_vprw(lp, off) readw((lp)->aperture + MMIO_VPR_OFFSET + (off))
128 #define write_vprl(lp, off, val) writel((val), (lp)->aperture + MMIO_VPR_OFFSET + (off))
129 #define read_vprl(lp, off) readl((lp)->aperture + MMIO_VPR_OFFSET + (off))
130
131
132 static int __init lynxfb_init_one(struct pci_dev *dp, const struct pci_device_id *ent);
133 static void lynx_init_hw(struct fb_info_lynx *lynx);
134 static int lynx_get_fix(struct fb_fix_screeninfo *fix, int con, struct fb_info *info);
135 static int lynx_get_var(struct fb_var_screeninfo *var, int con, struct fb_info *info);
136 static int lynx_set_var(struct fb_var_screeninfo *var, int con, struct fb_info *info);
137 static int lynxfb_switch_con(int con, struct fb_info *info);
138 static int lynxfb_updatevar(int con, struct fb_info *info);
139 static void lynxfb_blank(int blank, struct fb_info *info);
140 static int lynx_get_cmap(struct fb_cmap *cmap, int kspc, int con,
141                          struct fb_info *info);
142 static int lynx_set_cmap(struct fb_cmap *cmap, int kspc, int con,
143                          struct fb_info *info);
144 static void lynx_set_bitdepth(struct fb_info_lynx *p, struct display *disp, int con, int bpp);
145 static void do_install_cmap(int con, struct fb_info *info);
146
147
148 static struct fb_ops lynxfb_ops = {
149         owner:          THIS_MODULE,
150         fb_get_fix:     lynx_get_fix,
151         fb_get_var:     lynx_get_var,
152         fb_set_var:     lynx_set_var,
153         fb_get_cmap:    lynx_get_cmap,
154         fb_set_cmap:    lynx_set_cmap,
155 };
156
157 static struct pci_device_id lynxfb_pci_tbl[] __devinitdata = {
158         {PCI_VENDOR_ID_SILICONMOTION, PCI_DEVICE_ID_SILICONMOTION_SM720,
159                 PCI_ANY_ID, PCI_ANY_ID,},
160         {0,},
161 };
162
163 static struct pci_driver lynxfb_pci_driver = {
164         name:           "lynxfb",
165         id_table:       lynxfb_pci_tbl,
166         probe:          lynxfb_init_one,
167         remove:         NULL,
168         suspend:        NULL,
169         resume:         NULL,
170 };
171
172 int __init lynx_init(void)
173 {
174         printk(KERN_DEBUG "lynxfb.c: lynx_init()\n");
175         return pci_module_init(&lynxfb_pci_driver);
176 }
177
178 module_init(lynx_init);
179
180 static int __init lynxfb_init_one(struct pci_dev *dp, const struct pci_device_id *ent)
181 {
182         struct fb_info_lynx *p = NULL;
183         int err = 0;
184         unsigned long addr, size;
185         int i;
186         u16 cmd;
187
188         printk(KERN_DEBUG "lynxfb.c: lynxfb_init_one() slot=%s\n", dp->slot_name);
189
190         if ((dp->resource[0].flags & IORESOURCE_MEM) == 0)
191                 return -EBUSY; /* FIXME: not sure what error codes we should use here */
192         addr = dp->resource[0].start;
193         size = dp->resource[0].end + 1 - addr;
194         if (addr == 0)
195                 return -EBUSY;
196
197         err = pci_request_regions(dp, "lynxfb");
198         if (err)
199                 return err;
200
201         p = kmalloc(sizeof(*p), GFP_ATOMIC);
202         if (! p) {
203                 err = -ENOMEM;
204                 goto fail;
205         }
206         memset(p, 0, sizeof(*p));
207
208         pci_set_drvdata(dp, p);
209
210 //#ifdef __BIG_ENDIAN
211 //      addr += 0x2000000;
212 //#endif
213         p->pdev = dp;
214         p->aperture_phys = addr;
215         p->aperture = ioremap(addr, 0x2000000);
216
217 #if 0
218         /* we should use pci_enable_device here, but,
219            the device doesn't declare its I/O ports in its BARs
220            so pci_enable_device won't turn on I/O responses */
221         pci_read_config_word(dp, PCI_COMMAND, &cmd);
222         cmd |= 3;       /* enable memory and IO space */
223         pci_write_config_word(dp, PCI_COMMAND, cmd);
224 #else
225         err = pci_enable_device(dp);
226         if (err) {
227                 err = -EBUSY;
228                 goto fail;
229         }
230 #endif
231
232         p->frame_buffer_phys = p->aperture_phys + 0x200000;
233         p->frame_buffer = p->aperture + 0x200000;
234
235         pci_read_config_byte(dp, PCI_REVISION_ID, &p->chiprev);
236         printk(KERN_DEBUG "lynxfb.c: Chip Revision %x\n", (unsigned) p->chiprev);
237
238         lynx_init_hw(p);
239
240         printk(KERN_DEBUG "lynxfb.c: aperture physical addr 0x%08lx, virtual addr 0x%p\n",
241                p->aperture_phys, p->aperture);
242
243         strcpy(p->fix.id, "Lynx3DM");
244         p->fix.smem_start = p->frame_buffer_phys;
245         p->fix.smem_len = p->ramsize;
246         p->fix.type = FB_TYPE_PACKED_PIXELS;
247         p->fix.visual = FB_VISUAL_PSEUDOCOLOR;
248         p->fix.line_length = 640*3;
249
250         p->var.xres = 640;
251         p->var.yres = 480;
252         p->var.xres_virtual = 640;
253         p->var.yres_virtual = 480;
254         p->var.bits_per_pixel = 24;
255         p->var.red.offset = 0;
256         p->var.green.offset = 8;
257         p->var.blue.offset = 16;
258         p->var.red.length = p->var.green.length = p->var.blue.length = 8;
259         p->var.height = p->var.width = -1;
260         p->var.vmode = FB_VMODE_NONINTERLACED;
261         p->var.pixclock = 10000;
262         p->var.left_margin = p->var.right_margin = 16;
263         p->var.upper_margin = p->var.lower_margin = 16;
264         p->var.hsync_len = p->var.vsync_len = 8;
265
266         p->disp.var = p->var;
267         p->disp.cmap.red = NULL;
268         p->disp.cmap.green = NULL;
269         p->disp.cmap.blue = NULL;
270         p->disp.cmap.transp = NULL;
271         p->disp.screen_base = p->frame_buffer;
272         p->disp.visual = p->fix.visual;
273         p->disp.type = p->fix.type;
274         p->disp.type_aux = p->fix.type_aux;
275         p->disp.line_length = p->fix.line_length;
276         p->disp.can_soft_blank = 1;
277 #ifdef FBCON_HAS_CFB24
278         p->disp.dispsw = &fbcon_cfb24;
279         p->disp.dispsw_data = p->fbcon_cfb24_cmap;
280 #else
281         p->disp.dispsw = &fbcon_dummy;
282 #endif
283         p->disp.scrollmode = SCROLL_YREDRAW;
284
285         strcpy(p->info.modename, p->fix.id);
286         p->info.node = -1;
287         p->info.fbops = &lynxfb_ops;
288         p->info.disp = &p->disp;
289         p->info.fontname[0] = 0;
290         p->info.changevar = NULL;
291         p->info.switch_con = &lynxfb_switch_con;
292         p->info.updatevar = &lynxfb_updatevar;
293         p->info.blank = &lynxfb_blank;
294         p->info.flags = FBINFO_FLAG_DEFAULT;
295
296         for (i = 0; i < 16; ++i) {
297                 int j = color_table[i];
298                 p->palette[i].red = default_red[j];
299                 p->palette[i].green = default_grn[j];
300                 p->palette[i].blue = default_blu[j];
301         }
302
303         err = register_framebuffer(&p->info);
304         if (err < 0)
305                 goto fail;
306
307         p->next = all_lynx;
308         all_lynx = p;
309
310         return 0;
311
312  fail:
313         if (p) {
314                 if (p->aperture)
315                         iounmap(p->aperture);
316                 kfree(p);
317         }
318
319         pci_release_regions(dp);
320         return err;
321 }
322
323 #if 0
324 static void lynxfb_remove_one(struct pci_dev *pdev)
325 {
326         struct fb_info_lynx *lynx;
327
328         lynx = (struct fb_info_lynx *)pci_get_drvdata(pdev); 
329
330         /* FIXME: unregister framebuffer */
331
332         if (lynx->aperture) {
333                 iounmap(lynx->aperture);
334         }
335         kfree(lynx);
336         pci_release_regions(pdev);
337 }
338 #endif
339
340 /* The memory initialization and detection code is inspired by some
341    code provided by Silicon Motion Inc. */
342 #define MEM_BLOCK_SIZE          (512 * 1024)    /* check memory in 512kB blocks */
343
344 unsigned long __init test_memory(u8 *fb, unsigned long range)
345 {
346         unsigned long offset;
347         unsigned long memsize = 0;
348
349         for (offset = range; offset != 0; offset -= MEM_BLOCK_SIZE) {
350                 *(u32 *) (fb + offset - 4) = offset ^ 0x01234567;
351                 *(u32 *) (fb + offset - 8) = offset ^ 0x456789AB;
352                 *(u32 *) (fb + offset - 12) = offset ^ 0x89ABCDEF;
353                 *(u32 *) (fb + offset - 16) = offset ^ 0xCDEF0123;
354         }
355
356         for (offset = MEM_BLOCK_SIZE; offset <= range; offset += MEM_BLOCK_SIZE) {
357                 // In each block, verify the 128 bits.
358                 if ((*(u32 *) (fb + offset - 4) == (offset ^ 0x01234567))
359                     && (*(u32 *) (fb + offset - 8) == (offset ^ 0x456789AB))
360                     && (*(u32 *) (fb + offset - 12) == (offset ^ 0x89ABCDEF))
361                     && (*(u32 *) (fb + offset - 16) == (offset ^ 0xCDEF0123))) {
362                         memsize += MEM_BLOCK_SIZE;
363                 } else
364                         break;
365         }
366
367         return memsize;
368 }
369
370 void __init lynx_ram_init(struct fb_info_lynx *lynx)
371 {
372         unsigned long range;
373         u8 *fb;
374
375         lynx->ramsize = 0;
376
377         range = 0x1000000;      // 16MB
378
379         write_scr(lynx, 0x18, read_scr(lynx, 0x18) | 0x41);
380
381         fb = lynx->frame_buffer;
382
383         write_mcr(lynx, 0x62, 0xff);
384         write_mcr(lynx, 0x76, 0x7f);
385         write_crt(lynx, 0xff, 0x00);
386         
387         if (lynx->chiprev >= 0xc0) {
388                 *((u16 *)(fb + 4)) = 0x5AA5;
389                 if (*((u16 *)(fb + 4)) != 0x5AA5)
390                         write_mcr(lynx, 0x62, 0xfe);
391         }
392                 
393         write_mcr(lynx, 0x76, 0x7b);
394
395         if ( (test_memory(fb, 0x100000) != 0) && (lynx->chiprev < 0xc0) )
396                 write_mcr(lynx, 0x62, 0xbb);
397         else
398                 write_crt(lynx, 0xff, 0x80);
399
400         write_mcr(lynx, 0x76, 0x7f);
401
402         lynx->ramsize = test_memory(fb, 0x1000000); /* Test a 16MB range */
403                 
404         if (lynx->ramsize == 0x400000)
405                 write_mcr(lynx, 0x76, 0xff);
406         else
407                 write_mcr(lynx, 0x76, 0x3f);
408
409         
410 }
411
412 struct regval {
413         u8 index, val;
414 };
415
416 /* FIXME: these tables can probably be trimmed rather a lot */
417 static struct regval crt_init_tbl[] = {
418         {0x00, 0x5f},
419         {0x01, 0x4f},
420         {0x02, 0x4f},
421         {0x03, 0x00},
422         {0x04, 0x52},
423         {0x05, 0x1e},
424         {0x06, 0x0b},
425         {0x07, 0x3e},
426         {0x08, 0x00},
427         {0x09, 0x40},
428         {0x0a, 0x00},
429         {0x0b, 0x00},
430         {0x0c, 0x00},
431         {0x0d, 0x00},
432         {0x0e, 0x00},
433         {0x0f, 0x00},
434
435         {0x10, 0xea},
436         {0x11, 0x2c},
437         {0x12, 0xdf},
438         {0x13, 0x28},
439         {0x14, 0x00},
440         {0x15, 0xdf},
441         {0x16, 0x0b},
442         {0x17, 0xc3},
443         {0x18, 0xff},
444
445         /* extended */
446         {0x30, 0x00},
447         {0x31, 0x00},
448         {0x32, 0x00},
449         {0x33, 0x00},
450         {0x34, 0x00},
451         {0x35, 0x55},
452         {0x36, 0x03},
453         {0x37, 0x10},
454         {0x38, 0x00},
455         {0x39, 0x00},
456         {0x3a, 0x00},
457         {0x3b, 0x40},
458         {0x3c, 0x00},
459         {0x3d, 0x20},
460         {0x3e, 0x00},
461         {0x3f, 0x00},
462         {0x9e, 0x03},
463         {0x9f, 0x00},
464         {0x90, 0x56},
465         {0x91, 0xdd},
466         {0x92, 0x5e},
467         {0x93, 0xea},
468         {0x94, 0x87},
469         {0x95, 0x44},
470         {0x96, 0x8f},
471         {0x97, 0x55},
472         {0x98, 0xdf},
473         {0x99, 0xff},
474         {0x9a, 0x55},
475         {0x9b, 0x0a},
476         {0xa0, 0x00},
477         {0xa1, 0x00},
478         {0xa2, 0x00},
479         {0xa3, 0x00},
480         {0xa4, 0x00},
481         {0xa5, 0x00},
482         {0xa6, 0x00},
483         {0xa7, 0x00}, 
484         {0xa8, 0x00},
485         {0xa9, 0x00},
486         {0xaa, 0x00},
487         {0xab, 0x00},
488         {0xac, 0x00},
489         {0xad, 0x00},
490         /* SVRs */
491         {0x40, 0x5f},
492         {0x41, 0x4f},
493         {0x42, 0x00},
494         {0x43, 0x52},
495         {0x44, 0x1e},
496         {0x45, 0x0b},
497         {0x46, 0xdf},
498         {0x47, 0x00},
499         {0x48, 0xe9},
500         {0x49, 0x0b},
501         {0x4a, 0x2e},
502         {0x4b, 0x00},
503         {0x4c, 0x4f},
504         {0x4d, 0xdf},
505 };
506
507 static struct regval grx_init_tbl[] = {
508         {0x00, 0x00},
509         {0x01, 0x00},
510         {0x02, 0x00},
511         {0x03, 0x00},
512         {0x04, 0x00},
513         {0x05, 0x40},
514         {0x06, 0x05},
515         {0x07, 0x0f},
516         {0x08, 0xff},
517 };
518
519 static struct regval atr_init_tbl[] = {
520         {0x00, 0x3f},
521         {0x01, 0x01},
522         {0x02, 0x02},
523         {0x03, 0x03},
524         {0x04, 0x04},
525         {0x05, 0x05},
526         {0x06, 0x06},
527         {0x07, 0x07},
528         {0x08, 0x08},
529         {0x09, 0x09},
530         {0x0a, 0x0a},
531         {0x0b, 0x0b},
532         {0x0c, 0x0c},
533         {0x0e, 0x0e},
534         {0x0f, 0x0f},
535
536         {0x10, 0x41},
537         {0x11, 0x00},
538         {0x12, 0x0f},
539         {0x13, 0x00},
540         {0x14, 0x00},
541 };
542
543 static struct regval seq_init_tbl[] = {
544         {0x00, 0x01},
545         {0x01, 0x01},
546         {0x02, 0x0f},
547         {0x03, 0x00},
548         {0x04, 0x0e},
549         /* SCRs */
550         {0x17, 0x0c},
551         {0x18, 0x51},
552         {0x19, 0x00},
553         /* PDRs */
554         {0x20, 0x84},
555         {0x21, 0xb0},
556         {0x22, 0x32},
557         {0x23, 0x00},
558         {0x24, 0x01},
559         /* FPRs */
560         {0x30, 0x22},
561         {0x31, 0x01},
562         {0x32, 0x24},
563         {0x33, 0x00},
564         {0x34, 0xc0},
565         {0x3e, 0x03},
566         {0x3f, 0xff},
567         {0x40, 0x00},
568         {0x41, 0xfc},
569         {0x42, 0x00},
570         {0x43, 0x00},
571         {0x44, 0x20},
572         {0x45, 0xf0},
573         {0x46, 0x00},
574         {0x47, 0xfc},
575         {0x48, 0x20},
576         {0x49, 0x3c},
577         {0x4a, 0x44},
578         {0x4b, 0x20},
579         {0x4c, 0x00},
580         {0x4d, 0x00},
581         {0x50, 0x04},
582         {0x51, 0x24},
583         {0x52, 0x63},
584         {0x53, 0x4f},
585         {0x54, 0x52},
586         {0x55, 0x0b},
587         {0x56, 0xdf},
588         {0x57, 0xe9},
589         {0x58, 0x04},
590         {0x59, 0x03},
591         {0x5a, 0x19},
592         {0xa0, 0x00},
593         {0xa1, 0x00},
594         {0xa2, 0x00},
595         {0xa3, 0x01},
596         {0xa4, 0x00},
597         {0xa5, 0xed},
598         {0xa6, 0xed},
599         {0xa7, 0xed},
600         {0xa8, 0x00},
601         {0xa9, 0x00},
602         {0xaa, 0x00},
603         {0xab, 0x00},
604         {0xac, 0x00},
605         {0xad, 0x02},
606         {0xae, 0x00},
607         {0xaf, 0x00},
608         /* MCRs */
609         {0x60, 0x00},
610         {0x61, 0x00},
611         {0x62, 0xff},
612         {0x76, 0x3f},
613         /* CCRs */
614         {0x65, 0x00},
615         {0x66, 0x03},
616         {0x68, 0x54},
617         {0x69, 0x04},
618         {0x6a, 0x0d},
619         {0x6b, 0x02},
620         {0x6c, 0x07},
621         {0x6d, 0x82},
622         {0x6e, 0x07},
623         {0x6f, 0x82},
624         {0x7a, 0x30},
625         {0x7b, 0x30},
626         {0x7c, 0x30},
627         {0x7d, 0x00},
628         /* GPRs */
629         {0x70, 0x40},
630         {0x71, 0xdd},
631         {0x72, 0x0c},
632         {0x73, 0x0c},
633         {0x74, 0x24},
634         {0x75, 0xa0},
635         /* PHRs */
636         {0x80, 0xff},
637         {0x81, 0x00},
638         /* POPs */
639         {0x82, 0x00},
640         {0x84, 0x24},
641         {0x85, 0xa0},
642         {0x86, 0xc7},
643         {0x90, 0x00},
644         {0x91, 0x00},
645         {0x92, 0x00},
646         {0x93, 0x01},
647         /* HCRs */
648         {0x88, 0x00},
649         {0x89, 0x00},
650         {0x8a, 0x00},
651         {0x8b, 0x00},
652         {0x8c, 0xff},
653         {0x8d, 0xff},
654 };
655
656 #define N_ELTS(x)       (sizeof(x) / sizeof(x[0]))
657
658 static void __init lynx_init_hw(struct fb_info_lynx *lynx)
659 {
660         int i, j;
661
662         outb(0xe3, 0x3c2); /* set misc output reg */
663
664         for (i = 0; i < N_ELTS(seq_init_tbl); ++i)
665                 write_seq(lynx, seq_init_tbl[i].index, seq_init_tbl[i].val);
666         for (i = 0; i < N_ELTS(grx_init_tbl); ++i)
667                 write_grx(lynx, grx_init_tbl[i].index, grx_init_tbl[i].val);
668         for (i = 0; i < N_ELTS(atr_init_tbl); ++i)
669                 write_atr(lynx, atr_init_tbl[i].index, atr_init_tbl[i].val);
670         for (i = 0; i < N_ELTS(crt_init_tbl); ++i)
671                 write_crt(lynx, crt_init_tbl[i].index, crt_init_tbl[i].val);
672
673 #if 0
674         for (i = 0; i < 0x100; i += 0x10) {
675                 printk("CRT %02X  ", i);
676                 for (j = 0; j < 0x10; j++)
677                         printk(" %02x", read_crt(lynx, i+j));
678                 printk("\n");
679         }
680         for (i = 0; i < 0x100; i += 0x10) {
681                 printk("SEQ %02X  ", i);
682                 for (j = 0; j < 0x10; j++)
683                         printk(" %02x", read_seq(lynx, i+j));
684                 printk("\n");
685         }
686 #endif
687         /* Initialize the RAM - this should be unecessary on machines
688            with a BIOS, but it's needed on the Icebox */
689         lynx_ram_init(lynx);
690
691         write_dprw(lynx, 0x1e, 0x0);
692         write_vprl(lynx, 0x00, 0x0);
693
694         printk(KERN_DEBUG "lynxfb.c: Found %ldk of video memory.\n",
695                lynx->ramsize / 1024);
696         memset(lynx->frame_buffer, 0, lynx->ramsize);
697
698         /* FIXME: poke GPR71 in the way that X expects */
699 }
700
701 static int lynx_get_fix(struct fb_fix_screeninfo *fix, int con,
702                          struct fb_info *info)
703 {
704         struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
705
706         *fix = lp->fix;
707         return 0;
708 }
709
710 static int lynx_get_var(struct fb_var_screeninfo *var, int con,
711                         struct fb_info *info)
712 {
713
714         struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
715
716         *var = lp->var;
717         return 0;
718 }
719
720 static int lynx_set_var(struct fb_var_screeninfo *var, int con,
721                          struct fb_info *info)
722 {
723         struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
724         struct display *disp = (con >= 0)? &fb_display[con] : &lp->disp;
725
726         if (var->xres != 640 || var->yres != 480
727             || var->xres_virtual != 640 || var->yres_virtual != 480
728             || (var->bits_per_pixel != 8 && var->bits_per_pixel != 16)
729             || var->nonstd
730             || (var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
731                 return -EINVAL;
732
733         if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW &&
734             var->bits_per_pixel != disp->var.bits_per_pixel) {
735                 lynx_set_bitdepth(lp, disp, con, var->bits_per_pixel);
736         }
737         
738         return 0;
739 }
740
741 static int lynxfb_getcolreg(u_int regno, u_int *red, u_int *green,
742                              u_int *blue, u_int *transp, struct fb_info *info)
743 {
744         struct fb_info_lynx *p = (struct fb_info_lynx *) info;
745
746         if (regno > 255)
747                 return 1;
748         *red = (p->palette[regno].red<<8) | p->palette[regno].red;
749         *green = (p->palette[regno].green<<8) | p->palette[regno].green;
750         *blue = (p->palette[regno].blue<<8) | p->palette[regno].blue;
751         *transp = 0;
752         return 0;
753 }
754
755 static int lynxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
756                              u_int transp, struct fb_info *info)
757 {
758         struct fb_info_lynx *p = (struct fb_info_lynx *) info;
759
760         if (regno > 255)
761                 return 1;
762         red >>= 8;
763         green >>= 8;
764         blue >>= 8;
765         p->palette[regno].red = red;
766         p->palette[regno].green = green;
767         p->palette[regno].blue = blue;
768         /* FIXME: allow mmio */
769         outb(regno, 0x3c8);
770         udelay(1);
771         outb(red, 0x3c9);
772         outb(green, 0x3c9);
773         outb(blue, 0x3c9);
774
775 #ifdef FBCON_HAS_CFB16
776         if (regno < 16)
777                 p->fbcon_cfb16_cmap[regno] = ((red & 0xf8) << 7)
778                         | ((green & 0xf8) << 2) | ((blue & 0xf8) >> 3);
779 #endif
780 #ifdef FBCON_HAS_CFB24
781         if (regno < 16)
782                 p->fbcon_cfb24_cmap[regno] = (red << 16)
783                         | (green << 8) | (blue);
784 #endif
785
786         return 0;
787 }
788
789 static int lynx_get_cmap(struct fb_cmap *cmap, int kspc, int con,
790                           struct fb_info *info)
791 {
792         if (con == currcon)             /* current console? */
793                 return fb_get_cmap(cmap, kspc, lynxfb_getcolreg, info);
794         if (fb_display[con].cmap.len)   /* non default colormap? */
795                 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
796         else {
797                 int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
798                 fb_copy_cmap(fb_default_cmap(size), cmap, kspc ? 0 : 2);
799         }
800         return 0;
801 }
802
803 static int lynx_set_cmap(struct fb_cmap *cmap, int kspc, int con,
804                          struct fb_info *info)
805 {
806         int err;
807         if (!fb_display[con].cmap.len) {        /* no colormap allocated? */
808                 int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
809                 if ((err = fb_alloc_cmap(&fb_display[con].cmap, size, 0)))
810                         return err;
811         }
812         if (con == currcon)                     /* current console? */
813                 return fb_set_cmap(cmap, kspc, lynxfb_setcolreg, info);
814         else
815                 fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
816         return 0;
817 }
818
819 static int lynxfb_switch_con(int con, struct fb_info *info)
820 {
821         struct fb_info_lynx *p = (struct fb_info_lynx *) info;
822         int new_bpp, old_bpp;
823
824         /* Do we have to save the colormap? */
825         if (fb_display[currcon].cmap.len)
826                 fb_get_cmap(&fb_display[currcon].cmap, 1, lynxfb_getcolreg, info);
827
828         new_bpp = fb_display[con].var.bits_per_pixel;
829         old_bpp = fb_display[currcon].var.bits_per_pixel;
830         currcon = con;
831
832         if (new_bpp != old_bpp)
833                 lynx_set_bitdepth(p, &fb_display[con], con, new_bpp);
834         
835         do_install_cmap(con, info);
836         return 0;
837 }
838
839 static int lynxfb_updatevar(int con, struct fb_info *info)
840 {
841         return 0;
842 }
843
844 static void lynxfb_blank(int blank, struct fb_info *info)
845 {
846         struct fb_info_lynx *p = (struct fb_info_lynx *) info;
847         int i;
848
849         if (blank) {
850                 /* get the palette from the chip */
851                 for (i = 0; i < 256; ++i) {
852                         outb(i, 0x3c7);
853                         udelay(1);
854                         p->palette[i].red = inb(0x3c9);
855                         p->palette[i].green = inb(0x3c9);
856                         p->palette[i].blue = inb(0x3c9);
857                 }
858                 for (i = 0; i < 256; ++i) {
859                         outb(i, 0x3c8);
860                         udelay(1);
861                         outb(0, 0x3c9);
862                         outb(0, 0x3c9);
863                         outb(0, 0x3c9);
864                 }
865         } else {
866                 for (i = 0; i < 256; ++i) {
867                         outb(i, 0x3c8);
868                         udelay(1);
869                         outb(p->palette[i].red, 0x3c9);
870                         outb(p->palette[i].green, 0x3c9);
871                         outb(p->palette[i].blue, 0x3c9);
872                 }
873         }
874 }
875
876 /*
877  * Exported functions
878  */
879 static void do_install_cmap(int con, struct fb_info *info)
880 {
881         if (con != currcon)
882                 return;
883         if (fb_display[con].cmap.len)
884                 fb_set_cmap(&fb_display[con].cmap, 1, lynxfb_setcolreg, info);
885         else {
886                 int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
887                 fb_set_cmap(fb_default_cmap(size), 1, lynxfb_setcolreg, info);
888         }
889 }
890
891 static void lynx_set_bitdepth(struct fb_info_lynx *p, struct display *disp, int con, int bpp)
892 {
893         int err;
894         struct fb_fix_screeninfo* fix = &p->fix;
895         struct fb_var_screeninfo* var = &p->var;
896
897         if (bpp == 24) {
898                 if (con == currcon) {
899                         write_crt(p, 0x13, 240);        // Set line length (doublewords)
900                         write_dprw(p, 0x1e,
901                                    (read_dprw(p, 0x1e) & 0xffcf) | 0x30);
902                         write_vprl(p, 0x00,
903                                    (read_vprl(p, 0x00) & 0xfff8ffff) | 0x40000);
904                 }
905
906                 fix->line_length = 640*3;
907                 fix->visual = FB_VISUAL_TRUECOLOR;
908
909                 var->red.offset = 16;
910                 var->green.offset = 8;
911                 var->blue.offset = 0;
912                 var->red.length = var->green.length = var->blue.length = 8;
913                 
914 #ifdef FBCON_HAS_CFB24
915                 disp->dispsw = &fbcon_cfb24;
916                 disp->dispsw_data = p->fbcon_cfb24_cmap;
917 #else
918                 disp->dispsw = &fbcon_dummy;
919 #endif
920         } else if (bpp == 16) {
921                 if (con == currcon) {
922                         write_crt(p, 0x13, 160);        // Set line length (doublewords)
923                         write_dprw(p, 0x1e,
924                                    (read_dprw(p, 0x1e) & 0xffcf) | 0x10);
925                         write_vprl(p, 0x00,
926                                    (read_vprl(p, 0x00) & 0xfff8ffff) | 0x20000);
927                 }
928
929                 fix->line_length = 640*2;
930                 fix->visual = FB_VISUAL_TRUECOLOR;
931
932                 var->red.offset = 10;
933                 var->green.offset = 5;
934                 var->blue.offset = 0;
935                 var->red.length = var->blue.length = 5;
936                 var->green.length = 6;
937                 
938 #ifdef FBCON_HAS_CFB16
939                 disp->dispsw = &fbcon_cfb16;
940                 disp->dispsw_data = p->fbcon_cfb16_cmap;
941 #else
942                 disp->dispsw = &fbcon_dummy;
943 #endif
944         } else if (bpp == 8) {
945                 if (con == currcon) {
946                         write_crt(p, 0x13, 80);         // Set line length (doublewords)
947                         write_dprw(p, 0x1e,
948                                    (read_dprw(p, 0x1e) & 0xffcf) | 0x0);
949                         write_vprl(p, 0x00,
950                                    (read_vprl(p, 0x00) & 0xfff8ffff) | 0x0);
951                 }
952
953                 fix->line_length = 640;
954                 fix->visual = FB_VISUAL_PSEUDOCOLOR;            
955
956                 var->red.offset = var->green.offset = var->blue.offset = 0;
957                 var->red.length = var->green.length = var->blue.length = 8;
958                 
959 #ifdef FBCON_HAS_CFB8
960                 disp->dispsw = &fbcon_cfb8;
961 #else
962                 disp->dispsw = &fbcon_dummy;
963 #endif
964         }
965
966         var->bits_per_pixel = bpp;
967         disp->line_length = p->fix.line_length;
968         disp->visual = fix->visual;
969         disp->var = *var;
970
971         if (p->info.changevar)
972                 (*p->info.changevar)(con);
973
974         if ((err = fb_alloc_cmap(&disp->cmap, 0, 0)))
975                 return;
976         do_install_cmap(con, (struct fb_info *)p);
977 }