2 * drivers/video/lynxfb.c -- frame buffer device for
3 * Silicon Motion Lynx3DM chip.
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.
10 * Copyright (C) 2002 David Gibson, IBM Corporation.
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.
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
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>
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>
35 #include <linux/selection.h>
36 #include <linux/init.h>
37 #include <linux/pci.h>
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>
46 MODULE_LICENSE("GPL");
50 struct fb_fix_screeninfo fix;
51 struct fb_var_screeninfo var;
59 unsigned long ramsize;
61 unsigned long aperture_phys;
63 unsigned long frame_buffer_phys;
66 struct fb_info_lynx *next;
67 #ifdef FBCON_HAS_CFB16
68 u16 fbcon_cfb16_cmap[16];
70 #ifdef FBCON_HAS_CFB16
71 u32 fbcon_cfb24_cmap[16];
74 static struct fb_info_lynx *all_lynx;
75 static int currcon; /* = 0 */
77 #define MMIO_DPR_OFFSET (0x000)
78 #define MMIO_VPR_OFFSET (0x800)
79 #define MMIO_IO_OFFSET (0xc0000)
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)); \
86 #define read_ind(lp, num, ap, dp) ({ writeb((num), (lp)->aperture + MMIO_IO_OFFSET + (ap)); \
87 readb((lp)->aperture + MMIO_IO_OFFSET + (dp));})
89 #define write_ind(lp, num, val, ap, dp) do { \
90 outb((num), (ap)); outb((val), (dp)); \
92 #define read_ind(lp, num, ap, dp) ({ outb((num), (ap)); inb((dp)); })
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
114 /* attribute controller */
115 #define write_atr(lp, num, val) do { \
116 inb(0x3da); write_ind(lp, num, val, 0x3c0, 0x3c0); \
118 #define read_atr(lp, num) ({inb(0x3da); read_ind(lp, num, 0x3c0, 0x3c1); })
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))
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);
148 static struct fb_ops lynxfb_ops = {
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,
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,},
163 static struct pci_driver lynxfb_pci_driver = {
165 id_table: lynxfb_pci_tbl,
166 probe: lynxfb_init_one,
172 int __init lynx_init(void)
174 printk(KERN_DEBUG "lynxfb.c: lynx_init()\n");
175 return pci_module_init(&lynxfb_pci_driver);
178 module_init(lynx_init);
180 static int __init lynxfb_init_one(struct pci_dev *dp, const struct pci_device_id *ent)
182 struct fb_info_lynx *p = NULL;
184 unsigned long addr, size;
188 printk(KERN_DEBUG "lynxfb.c: lynxfb_init_one() slot=%s\n", dp->slot_name);
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;
197 err = pci_request_regions(dp, "lynxfb");
201 p = kmalloc(sizeof(*p), GFP_ATOMIC);
206 memset(p, 0, sizeof(*p));
208 pci_set_drvdata(dp, p);
210 //#ifdef __BIG_ENDIAN
211 // addr += 0x2000000;
214 p->aperture_phys = addr;
215 p->aperture = ioremap(addr, 0x2000000);
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);
225 err = pci_enable_device(dp);
232 p->frame_buffer_phys = p->aperture_phys + 0x200000;
233 p->frame_buffer = p->aperture + 0x200000;
235 pci_read_config_byte(dp, PCI_REVISION_ID, &p->chiprev);
236 printk(KERN_DEBUG "lynxfb.c: Chip Revision %x\n", (unsigned) p->chiprev);
240 printk(KERN_DEBUG "lynxfb.c: aperture physical addr 0x%08lx, virtual addr 0x%p\n",
241 p->aperture_phys, p->aperture);
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;
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;
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;
281 p->disp.dispsw = &fbcon_dummy;
283 p->disp.scrollmode = SCROLL_YREDRAW;
285 strcpy(p->info.modename, p->fix.id);
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;
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];
303 err = register_framebuffer(&p->info);
315 iounmap(p->aperture);
319 pci_release_regions(dp);
324 static void lynxfb_remove_one(struct pci_dev *pdev)
326 struct fb_info_lynx *lynx;
328 lynx = (struct fb_info_lynx *)pci_get_drvdata(pdev);
330 /* FIXME: unregister framebuffer */
332 if (lynx->aperture) {
333 iounmap(lynx->aperture);
336 pci_release_regions(pdev);
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 */
344 unsigned long __init test_memory(u8 *fb, unsigned long range)
346 unsigned long offset;
347 unsigned long memsize = 0;
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;
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;
370 void __init lynx_ram_init(struct fb_info_lynx *lynx)
377 range = 0x1000000; // 16MB
379 write_scr(lynx, 0x18, read_scr(lynx, 0x18) | 0x41);
381 fb = lynx->frame_buffer;
383 write_mcr(lynx, 0x62, 0xff);
384 write_mcr(lynx, 0x76, 0x7f);
385 write_crt(lynx, 0xff, 0x00);
387 if (lynx->chiprev >= 0xc0) {
388 *((u16 *)(fb + 4)) = 0x5AA5;
389 if (*((u16 *)(fb + 4)) != 0x5AA5)
390 write_mcr(lynx, 0x62, 0xfe);
393 write_mcr(lynx, 0x76, 0x7b);
395 if ( (test_memory(fb, 0x100000) != 0) && (lynx->chiprev < 0xc0) )
396 write_mcr(lynx, 0x62, 0xbb);
398 write_crt(lynx, 0xff, 0x80);
400 write_mcr(lynx, 0x76, 0x7f);
402 lynx->ramsize = test_memory(fb, 0x1000000); /* Test a 16MB range */
404 if (lynx->ramsize == 0x400000)
405 write_mcr(lynx, 0x76, 0xff);
407 write_mcr(lynx, 0x76, 0x3f);
416 /* FIXME: these tables can probably be trimmed rather a lot */
417 static struct regval crt_init_tbl[] = {
507 static struct regval grx_init_tbl[] = {
519 static struct regval atr_init_tbl[] = {
543 static struct regval seq_init_tbl[] = {
656 #define N_ELTS(x) (sizeof(x) / sizeof(x[0]))
658 static void __init lynx_init_hw(struct fb_info_lynx *lynx)
662 outb(0xe3, 0x3c2); /* set misc output reg */
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);
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));
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));
687 /* Initialize the RAM - this should be unecessary on machines
688 with a BIOS, but it's needed on the Icebox */
691 write_dprw(lynx, 0x1e, 0x0);
692 write_vprl(lynx, 0x00, 0x0);
694 printk(KERN_DEBUG "lynxfb.c: Found %ldk of video memory.\n",
695 lynx->ramsize / 1024);
696 memset(lynx->frame_buffer, 0, lynx->ramsize);
698 /* FIXME: poke GPR71 in the way that X expects */
701 static int lynx_get_fix(struct fb_fix_screeninfo *fix, int con,
702 struct fb_info *info)
704 struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
710 static int lynx_get_var(struct fb_var_screeninfo *var, int con,
711 struct fb_info *info)
714 struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
720 static int lynx_set_var(struct fb_var_screeninfo *var, int con,
721 struct fb_info *info)
723 struct fb_info_lynx *lp = (struct fb_info_lynx *) info;
724 struct display *disp = (con >= 0)? &fb_display[con] : &lp->disp;
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)
730 || (var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
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);
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)
744 struct fb_info_lynx *p = (struct fb_info_lynx *) info;
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;
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)
758 struct fb_info_lynx *p = (struct fb_info_lynx *) info;
765 p->palette[regno].red = red;
766 p->palette[regno].green = green;
767 p->palette[regno].blue = blue;
768 /* FIXME: allow mmio */
775 #ifdef FBCON_HAS_CFB16
777 p->fbcon_cfb16_cmap[regno] = ((red & 0xf8) << 7)
778 | ((green & 0xf8) << 2) | ((blue & 0xf8) >> 3);
780 #ifdef FBCON_HAS_CFB24
782 p->fbcon_cfb24_cmap[regno] = (red << 16)
783 | (green << 8) | (blue);
789 static int lynx_get_cmap(struct fb_cmap *cmap, int kspc, int con,
790 struct fb_info *info)
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);
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);
803 static int lynx_set_cmap(struct fb_cmap *cmap, int kspc, int con,
804 struct fb_info *info)
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)))
812 if (con == currcon) /* current console? */
813 return fb_set_cmap(cmap, kspc, lynxfb_setcolreg, info);
815 fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
819 static int lynxfb_switch_con(int con, struct fb_info *info)
821 struct fb_info_lynx *p = (struct fb_info_lynx *) info;
822 int new_bpp, old_bpp;
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);
828 new_bpp = fb_display[con].var.bits_per_pixel;
829 old_bpp = fb_display[currcon].var.bits_per_pixel;
832 if (new_bpp != old_bpp)
833 lynx_set_bitdepth(p, &fb_display[con], con, new_bpp);
835 do_install_cmap(con, info);
839 static int lynxfb_updatevar(int con, struct fb_info *info)
844 static void lynxfb_blank(int blank, struct fb_info *info)
846 struct fb_info_lynx *p = (struct fb_info_lynx *) info;
850 /* get the palette from the chip */
851 for (i = 0; i < 256; ++i) {
854 p->palette[i].red = inb(0x3c9);
855 p->palette[i].green = inb(0x3c9);
856 p->palette[i].blue = inb(0x3c9);
858 for (i = 0; i < 256; ++i) {
866 for (i = 0; i < 256; ++i) {
869 outb(p->palette[i].red, 0x3c9);
870 outb(p->palette[i].green, 0x3c9);
871 outb(p->palette[i].blue, 0x3c9);
879 static void do_install_cmap(int con, struct fb_info *info)
883 if (fb_display[con].cmap.len)
884 fb_set_cmap(&fb_display[con].cmap, 1, lynxfb_setcolreg, info);
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);
891 static void lynx_set_bitdepth(struct fb_info_lynx *p, struct display *disp, int con, int bpp)
894 struct fb_fix_screeninfo* fix = &p->fix;
895 struct fb_var_screeninfo* var = &p->var;
898 if (con == currcon) {
899 write_crt(p, 0x13, 240); // Set line length (doublewords)
901 (read_dprw(p, 0x1e) & 0xffcf) | 0x30);
903 (read_vprl(p, 0x00) & 0xfff8ffff) | 0x40000);
906 fix->line_length = 640*3;
907 fix->visual = FB_VISUAL_TRUECOLOR;
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;
914 #ifdef FBCON_HAS_CFB24
915 disp->dispsw = &fbcon_cfb24;
916 disp->dispsw_data = p->fbcon_cfb24_cmap;
918 disp->dispsw = &fbcon_dummy;
920 } else if (bpp == 16) {
921 if (con == currcon) {
922 write_crt(p, 0x13, 160); // Set line length (doublewords)
924 (read_dprw(p, 0x1e) & 0xffcf) | 0x10);
926 (read_vprl(p, 0x00) & 0xfff8ffff) | 0x20000);
929 fix->line_length = 640*2;
930 fix->visual = FB_VISUAL_TRUECOLOR;
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;
938 #ifdef FBCON_HAS_CFB16
939 disp->dispsw = &fbcon_cfb16;
940 disp->dispsw_data = p->fbcon_cfb16_cmap;
942 disp->dispsw = &fbcon_dummy;
944 } else if (bpp == 8) {
945 if (con == currcon) {
946 write_crt(p, 0x13, 80); // Set line length (doublewords)
948 (read_dprw(p, 0x1e) & 0xffcf) | 0x0);
950 (read_vprl(p, 0x00) & 0xfff8ffff) | 0x0);
953 fix->line_length = 640;
954 fix->visual = FB_VISUAL_PSEUDOCOLOR;
956 var->red.offset = var->green.offset = var->blue.offset = 0;
957 var->red.length = var->green.length = var->blue.length = 8;
959 #ifdef FBCON_HAS_CFB8
960 disp->dispsw = &fbcon_cfb8;
962 disp->dispsw = &fbcon_dummy;
966 var->bits_per_pixel = bpp;
967 disp->line_length = p->fix.line_length;
968 disp->visual = fix->visual;
971 if (p->info.changevar)
972 (*p->info.changevar)(con);
974 if ((err = fb_alloc_cmap(&disp->cmap, 0, 0)))
976 do_install_cmap(con, (struct fb_info *)p);