1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * macfb.c: Generic framebuffer for Macs whose colourmaps/modes we
4 * don't know how to set.
8 * Primarily based on vesafb.c, by Gerd Knorr
11 * Also uses information and code from:
13 * The original macfb.c from Linux/mac68k 2.0, by Alan Cox, Juergen
14 * Mellinger, Mikael Forselius, Michael Schmitz, and others.
16 * valkyriefb.c, by Martin Costabel, Kevin Schoedel, Barry Nathan, Dan
17 * Jacobowitz, Paul Mackerras, Fabio Riccardi, and Geert Uytterhoeven.
19 * The VideoToolbox "Bugs" web page at
20 * http://rajsky.psych.nyu.edu/Tips/VideoBugs.html
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
28 #include <linux/delay.h>
29 #include <linux/nubus.h>
30 #include <linux/init.h>
33 #include <asm/setup.h>
34 #include <asm/macintosh.h>
37 /* Common DAC base address for the LC, RBV, Valkyrie, and IIvx */
38 #define DAC_BASE 0x50f24000
40 /* Some addresses for the DAFB */
41 #define DAFB_BASE 0xf9800200
43 /* Address for the built-in Civic framebuffer in Quadra AVs */
44 #define CIVIC_BASE 0x50f30800
46 /* GSC (Gray Scale Controller) base address */
47 #define GSC_BASE 0x50F20000
49 /* CSC (Color Screen Controller) base address */
50 #define CSC_BASE 0x50F20000
52 static int (*macfb_setpalette)(unsigned int regno, unsigned int red,
53 unsigned int green, unsigned int blue,
54 struct fb_info *info);
59 } __iomem *v8_brazil_cmap_regs;
63 char pad1[3]; /* word aligned */
65 char pad2[3]; /* word aligned */
66 unsigned char cntl; /* a guess as to purpose */
67 } __iomem *rbv_cmap_regs;
71 unsigned long pad1[3];
72 unsigned char pad2[3];
74 } __iomem *dafb_cmap_regs;
77 unsigned char addr; /* OFFSET: 0x00 */
78 unsigned char pad1[15];
79 unsigned char lut; /* OFFSET: 0x10 */
80 unsigned char pad2[15];
81 unsigned char status; /* OFFSET: 0x20 */
82 unsigned char pad3[7];
83 unsigned long vbl_addr; /* OFFSET: 0x28 */
84 unsigned int status2; /* OFFSET: 0x2C */
85 } __iomem *civic_cmap_regs;
89 unsigned char clut_waddr; /* 0x40 */
91 unsigned char clut_data; /* 0x42 */
93 unsigned char clut_raddr; /* 0x46 */
94 } __iomem *csc_cmap_regs;
96 /* The registers in these structs are in NuBus slot space */
97 struct mdc_cmap_regs {
104 struct toby_cmap_regs {
106 unsigned char lut; /* TFBClutWDataReg, offset 0x90018 */
108 unsigned char addr; /* TFBClutAddrReg, offset 0x9001C */
111 struct jet_cmap_regs {
117 #define PIXEL_TO_MM(a) (((a)*10)/28) /* width in mm at 72 dpi */
119 static struct fb_var_screeninfo macfb_defined = {
120 .activate = FB_ACTIVATE_NOW,
125 .vmode = FB_VMODE_NONINTERLACED,
128 static struct fb_fix_screeninfo macfb_fix = {
129 .type = FB_TYPE_PACKED_PIXELS,
130 .accel = FB_ACCEL_NONE,
133 static void *slot_addr;
134 static struct fb_info fb_info;
135 static u32 pseudo_palette[16];
139 * Unlike the Valkyrie, the DAFB cannot set individual colormap
140 * registers. Therefore, we do what the MacOS driver does (no
141 * kidding!) and simply set them one by one until we hit the one we
144 static int dafb_setpalette(unsigned int regno, unsigned int red,
145 unsigned int green, unsigned int blue,
146 struct fb_info *info)
148 static int lastreg = -2;
151 local_irq_save(flags);
154 * fbdev will set an entire colourmap, but X won't. Hopefully
155 * this should accommodate both of them
157 if (regno != lastreg + 1) {
160 /* Stab in the dark trying to reset the CLUT pointer */
161 nubus_writel(0, &dafb_cmap_regs->reset);
164 /* Loop until we get to the register we want */
165 for (i = 0; i < regno; i++) {
166 nubus_writeb(info->cmap.red[i] >> 8,
167 &dafb_cmap_regs->lut);
169 nubus_writeb(info->cmap.green[i] >> 8,
170 &dafb_cmap_regs->lut);
172 nubus_writeb(info->cmap.blue[i] >> 8,
173 &dafb_cmap_regs->lut);
178 nubus_writeb(red, &dafb_cmap_regs->lut);
180 nubus_writeb(green, &dafb_cmap_regs->lut);
182 nubus_writeb(blue, &dafb_cmap_regs->lut);
184 local_irq_restore(flags);
189 /* V8 and Brazil seem to use the same DAC. Sonora does as well. */
190 static int v8_brazil_setpalette(unsigned int regno, unsigned int red,
191 unsigned int green, unsigned int blue,
192 struct fb_info *info)
194 unsigned int bpp = info->var.bits_per_pixel;
197 local_irq_save(flags);
199 /* On these chips, the CLUT register numbers are spread out
200 * across the register space. Thus:
201 * In 8bpp, all regnos are valid.
202 * In 4bpp, the regnos are 0x0f, 0x1f, 0x2f, etc, etc
203 * In 2bpp, the regnos are 0x3f, 0x7f, 0xbf, 0xff
205 regno = (regno << (8 - bpp)) | (0xFF >> bpp);
206 nubus_writeb(regno, &v8_brazil_cmap_regs->addr);
209 /* send one color channel at a time */
210 nubus_writeb(red, &v8_brazil_cmap_regs->lut);
212 nubus_writeb(green, &v8_brazil_cmap_regs->lut);
214 nubus_writeb(blue, &v8_brazil_cmap_regs->lut);
216 local_irq_restore(flags);
220 /* RAM-Based Video */
221 static int rbv_setpalette(unsigned int regno, unsigned int red,
222 unsigned int green, unsigned int blue,
223 struct fb_info *info)
227 local_irq_save(flags);
229 /* From the VideoToolbox driver. Seems to be saying that
230 * regno #254 and #255 are the important ones for 1-bit color,
231 * regno #252-255 are the important ones for 2-bit color, etc.
233 regno += 256 - (1 << info->var.bits_per_pixel);
235 /* reset clut? (VideoToolbox sez "not necessary") */
236 nubus_writeb(0xFF, &rbv_cmap_regs->cntl);
239 /* tell clut which address to use. */
240 nubus_writeb(regno, &rbv_cmap_regs->addr);
243 /* send one color channel at a time. */
244 nubus_writeb(red, &rbv_cmap_regs->lut);
246 nubus_writeb(green, &rbv_cmap_regs->lut);
248 nubus_writeb(blue, &rbv_cmap_regs->lut);
250 local_irq_restore(flags);
254 /* Macintosh Display Card (8*24) */
255 static int mdc_setpalette(unsigned int regno, unsigned int red,
256 unsigned int green, unsigned int blue,
257 struct fb_info *info)
259 struct mdc_cmap_regs *cmap_regs = slot_addr;
262 local_irq_save(flags);
264 /* the nop's are there to order writes. */
265 nubus_writeb(regno, &cmap_regs->addr);
267 nubus_writeb(red, &cmap_regs->lut);
269 nubus_writeb(green, &cmap_regs->lut);
271 nubus_writeb(blue, &cmap_regs->lut);
273 local_irq_restore(flags);
277 /* Toby frame buffer */
278 static int toby_setpalette(unsigned int regno, unsigned int red,
279 unsigned int green, unsigned int blue,
280 struct fb_info *info)
282 struct toby_cmap_regs *cmap_regs = slot_addr;
283 unsigned int bpp = info->var.bits_per_pixel;
289 regno = (regno << (8 - bpp)) | (0xFF >> bpp);
291 local_irq_save(flags);
293 nubus_writeb(regno, &cmap_regs->addr);
295 nubus_writeb(red, &cmap_regs->lut);
297 nubus_writeb(green, &cmap_regs->lut);
299 nubus_writeb(blue, &cmap_regs->lut);
301 local_irq_restore(flags);
305 /* Jet frame buffer */
306 static int jet_setpalette(unsigned int regno, unsigned int red,
307 unsigned int green, unsigned int blue,
308 struct fb_info *info)
310 struct jet_cmap_regs *cmap_regs = slot_addr;
313 local_irq_save(flags);
315 nubus_writeb(regno, &cmap_regs->addr);
317 nubus_writeb(red, &cmap_regs->lut);
319 nubus_writeb(green, &cmap_regs->lut);
321 nubus_writeb(blue, &cmap_regs->lut);
323 local_irq_restore(flags);
328 * Civic framebuffer -- Quadra AV built-in video. A chip
329 * called Sebastian holds the actual color palettes, and
330 * apparently, there are two different banks of 512K RAM
331 * which can act as separate framebuffers for doing video
332 * input and viewing the screen at the same time! The 840AV
333 * Can add another 1MB RAM to give the two framebuffers
336 static int civic_setpalette(unsigned int regno, unsigned int red,
337 unsigned int green, unsigned int blue,
338 struct fb_info *info)
343 local_irq_save(flags);
345 /* Set the register address */
346 nubus_writeb(regno, &civic_cmap_regs->addr);
350 * Grab a status word and do some checking;
351 * Then finally write the clut!
353 clut_status = nubus_readb(&civic_cmap_regs->status2);
355 if ((clut_status & 0x0008) == 0)
358 if ((clut_status & 0x000D) != 0)
360 nubus_writeb(0x00, &civic_cmap_regs->lut);
362 nubus_writeb(0x00, &civic_cmap_regs->lut);
367 nubus_writeb(red, &civic_cmap_regs->lut);
369 nubus_writeb(green, &civic_cmap_regs->lut);
371 nubus_writeb(blue, &civic_cmap_regs->lut);
373 nubus_writeb(0x00, &civic_cmap_regs->lut);
379 junk = nubus_readb(&civic_cmap_regs->lut);
381 junk = nubus_readb(&civic_cmap_regs->lut);
383 junk = nubus_readb(&civic_cmap_regs->lut);
385 junk = nubus_readb(&civic_cmap_regs->lut);
388 if ((clut_status & 0x000D) != 0)
390 nubus_writeb(0x00, &civic_cmap_regs->lut);
392 nubus_writeb(0x00, &civic_cmap_regs->lut);
396 nubus_writeb(red, &civic_cmap_regs->lut);
398 nubus_writeb(green, &civic_cmap_regs->lut);
400 nubus_writeb(blue, &civic_cmap_regs->lut);
402 nubus_writeb(junk, &civic_cmap_regs->lut);
405 local_irq_restore(flags);
410 * The CSC is the framebuffer on the PowerBook 190 series
411 * (and the 5300 too, but that's a PowerMac). This function
412 * brought to you in part by the ECSC driver for MkLinux.
414 static int csc_setpalette(unsigned int regno, unsigned int red,
415 unsigned int green, unsigned int blue,
416 struct fb_info *info)
420 local_irq_save(flags);
422 udelay(1); /* mklinux on PB 5300 waits for 260 ns */
423 nubus_writeb(regno, &csc_cmap_regs->clut_waddr);
424 nubus_writeb(red, &csc_cmap_regs->clut_data);
425 nubus_writeb(green, &csc_cmap_regs->clut_data);
426 nubus_writeb(blue, &csc_cmap_regs->clut_data);
428 local_irq_restore(flags);
432 static int macfb_setcolreg(unsigned regno, unsigned red, unsigned green,
433 unsigned blue, unsigned transp,
434 struct fb_info *fb_info)
437 * Set a single color register. The values supplied are
438 * already rounded down to the hardware's capabilities
439 * (according to the entries in the `var' structure).
440 * Return non-zero for invalid regno.
443 if (regno >= fb_info->cmap.len)
446 if (fb_info->var.bits_per_pixel <= 8) {
447 switch (fb_info->var.bits_per_pixel) {
449 /* We shouldn't get here */
454 if (macfb_setpalette)
455 macfb_setpalette(regno, red >> 8, green >> 8,
461 } else if (regno < 16) {
462 switch (fb_info->var.bits_per_pixel) {
464 if (fb_info->var.red.offset == 10) {
466 ((u32*) (fb_info->pseudo_palette))[regno] =
467 ((red & 0xf800) >> 1) |
468 ((green & 0xf800) >> 6) |
469 ((blue & 0xf800) >> 11) |
470 ((transp != 0) << 15);
473 ((u32*) (fb_info->pseudo_palette))[regno] =
474 ((red & 0xf800) >> 0) |
475 ((green & 0xfc00) >> 5) |
476 ((blue & 0xf800) >> 11);
480 * 24-bit colour almost doesn't exist on 68k Macs --
481 * https://support.apple.com/kb/TA28634 (Old Article: 10992)
488 ((u32 *)(fb_info->pseudo_palette))[regno] =
489 (red << fb_info->var.red.offset) |
490 (green << fb_info->var.green.offset) |
491 (blue << fb_info->var.blue.offset);
499 static const struct fb_ops macfb_ops = {
500 .owner = THIS_MODULE,
501 FB_DEFAULT_IOMEM_OPS,
502 .fb_setcolreg = macfb_setcolreg,
505 static void __init macfb_setup(char *options)
509 if (!options || !*options)
512 while ((this_opt = strsep(&options, ",")) != NULL) {
516 if (!strcmp(this_opt, "inverse"))
519 if (!strcmp(this_opt, "vidtest"))
520 vidtest = 1; /* enable experimental CLUT code */
524 static void __init iounmap_macfb(void)
527 iounmap(dafb_cmap_regs);
528 if (v8_brazil_cmap_regs)
529 iounmap(v8_brazil_cmap_regs);
531 iounmap(rbv_cmap_regs);
533 iounmap(civic_cmap_regs);
535 iounmap(csc_cmap_regs);
538 static int __init macfb_init(void)
540 int video_cmap_len, video_is_nubus = 0;
541 struct nubus_rsrc *ndev = NULL;
545 if (fb_get_options("macfb", &option))
552 if (mac_bi_data.id == MAC_MODEL_Q630 ||
553 mac_bi_data.id == MAC_MODEL_P588)
554 return -ENODEV; /* See valkyriefb.c */
556 macfb_defined.xres = mac_bi_data.dimensions & 0xFFFF;
557 macfb_defined.yres = mac_bi_data.dimensions >> 16;
558 macfb_defined.bits_per_pixel = mac_bi_data.videodepth;
560 macfb_fix.line_length = mac_bi_data.videorow;
561 macfb_fix.smem_len = macfb_fix.line_length * macfb_defined.yres;
562 /* Note: physical address (since 2.1.127) */
563 macfb_fix.smem_start = mac_bi_data.videoaddr;
566 * This is actually redundant with the initial mappings.
567 * However, there are some non-obvious aspects to the way
568 * those mappings are set up, so this is in fact the safest
569 * way to ensure that this driver will work on every possible Mac
571 fb_info.screen_base = ioremap(mac_bi_data.videoaddr,
573 if (!fb_info.screen_base)
576 pr_info("macfb: framebuffer at 0x%08lx, mapped to 0x%p, size %dk\n",
577 macfb_fix.smem_start, fb_info.screen_base,
578 macfb_fix.smem_len / 1024);
579 pr_info("macfb: mode is %dx%dx%d, linelength=%d\n",
580 macfb_defined.xres, macfb_defined.yres,
581 macfb_defined.bits_per_pixel, macfb_fix.line_length);
583 /* Fill in the available video resolution */
584 macfb_defined.xres_virtual = macfb_defined.xres;
585 macfb_defined.yres_virtual = macfb_defined.yres;
586 macfb_defined.height = PIXEL_TO_MM(macfb_defined.yres);
587 macfb_defined.width = PIXEL_TO_MM(macfb_defined.xres);
589 /* Some dummy values for timing to make fbset happy */
590 macfb_defined.pixclock = 10000000 / macfb_defined.xres *
591 1000 / macfb_defined.yres;
592 macfb_defined.left_margin = (macfb_defined.xres / 8) & 0xf8;
593 macfb_defined.hsync_len = (macfb_defined.xres / 8) & 0xf8;
595 switch (macfb_defined.bits_per_pixel) {
597 macfb_defined.red.length = macfb_defined.bits_per_pixel;
598 macfb_defined.green.length = macfb_defined.bits_per_pixel;
599 macfb_defined.blue.length = macfb_defined.bits_per_pixel;
601 macfb_fix.visual = FB_VISUAL_MONO01;
606 macfb_defined.red.length = macfb_defined.bits_per_pixel;
607 macfb_defined.green.length = macfb_defined.bits_per_pixel;
608 macfb_defined.blue.length = macfb_defined.bits_per_pixel;
609 video_cmap_len = 1 << macfb_defined.bits_per_pixel;
610 macfb_fix.visual = FB_VISUAL_PSEUDOCOLOR;
613 macfb_defined.transp.offset = 15;
614 macfb_defined.transp.length = 1;
615 macfb_defined.red.offset = 10;
616 macfb_defined.red.length = 5;
617 macfb_defined.green.offset = 5;
618 macfb_defined.green.length = 5;
619 macfb_defined.blue.offset = 0;
620 macfb_defined.blue.length = 5;
623 * Should actually be FB_VISUAL_DIRECTCOLOR, but this
626 macfb_fix.visual = FB_VISUAL_TRUECOLOR;
630 macfb_defined.red.offset = 16;
631 macfb_defined.red.length = 8;
632 macfb_defined.green.offset = 8;
633 macfb_defined.green.length = 8;
634 macfb_defined.blue.offset = 0;
635 macfb_defined.blue.length = 8;
637 macfb_fix.visual = FB_VISUAL_TRUECOLOR;
640 pr_err("macfb: unknown or unsupported bit depth: %d\n",
641 macfb_defined.bits_per_pixel);
647 * We take a wild guess that if the video physical address is
648 * in nubus slot space, that the nubus card is driving video.
649 * Penguin really ought to tell us whether we are using internal
651 * Hopefully we only find one of them. Otherwise our NuBus
652 * code is really broken :-)
655 for_each_func_rsrc(ndev) {
656 unsigned long base = ndev->board->slot_addr;
658 if (mac_bi_data.videoaddr < base ||
659 mac_bi_data.videoaddr - base > 0xFFFFFF)
662 if (ndev->category != NUBUS_CAT_DISPLAY ||
663 ndev->type != NUBUS_TYPE_VIDEO)
667 slot_addr = (unsigned char *)base;
669 switch(ndev->dr_hw) {
670 case NUBUS_DRHW_APPLE_MDC:
671 strcpy(macfb_fix.id, "Mac Disp. Card");
672 macfb_setpalette = mdc_setpalette;
674 case NUBUS_DRHW_APPLE_TFB:
675 strcpy(macfb_fix.id, "Toby");
676 macfb_setpalette = toby_setpalette;
678 case NUBUS_DRHW_APPLE_JET:
679 strcpy(macfb_fix.id, "Jet");
680 macfb_setpalette = jet_setpalette;
683 strcpy(macfb_fix.id, "Generic NuBus");
688 /* If it's not a NuBus card, it must be internal video */
690 switch (mac_bi_data.id) {
693 * Note: these first four have the v7 DAFB, which is
694 * known to be rather unlike the ones used in the
698 case MAC_MODEL_P475F:
710 strcpy(macfb_fix.id, "DAFB");
711 macfb_setpalette = dafb_setpalette;
712 dafb_cmap_regs = ioremap(DAFB_BASE, 0x1000);
716 * LC II uses the V8 framebuffer
719 strcpy(macfb_fix.id, "V8");
720 macfb_setpalette = v8_brazil_setpalette;
721 v8_brazil_cmap_regs = ioremap(DAC_BASE, 0x1000);
725 * IIvi, IIvx use the "Brazil" framebuffer (which is
726 * very much like the V8, it seems, and probably uses
732 strcpy(macfb_fix.id, "Brazil");
733 macfb_setpalette = v8_brazil_setpalette;
734 v8_brazil_cmap_regs = ioremap(DAC_BASE, 0x1000);
738 * LC III (and friends) use the Sonora framebuffer
739 * Incidentally this is also used in the non-AV models
740 * of the x100 PowerMacs
741 * These do in fact seem to use the same DAC interface
744 case MAC_MODEL_LCIII:
748 strcpy(macfb_fix.id, "Sonora");
749 macfb_setpalette = v8_brazil_setpalette;
750 v8_brazil_cmap_regs = ioremap(DAC_BASE, 0x1000);
754 * IIci and IIsi use the infamous RBV chip
755 * (the IIsi is just a rebadged and crippled
756 * IIci in a different case, BTW)
760 strcpy(macfb_fix.id, "RBV");
761 macfb_setpalette = rbv_setpalette;
762 rbv_cmap_regs = ioremap(DAC_BASE, 0x1000);
766 * AVs use the Civic framebuffer
770 strcpy(macfb_fix.id, "Civic");
771 macfb_setpalette = civic_setpalette;
772 civic_cmap_regs = ioremap(CIVIC_BASE, 0x1000);
778 * We think this may be like the LC II
781 strcpy(macfb_fix.id, "LC");
783 macfb_setpalette = v8_brazil_setpalette;
784 v8_brazil_cmap_regs =
785 ioremap(DAC_BASE, 0x1000);
790 * We think this may be like the LC II
793 strcpy(macfb_fix.id, "Color Classic");
795 macfb_setpalette = v8_brazil_setpalette;
796 v8_brazil_cmap_regs =
797 ioremap(DAC_BASE, 0x1000);
802 * And we *do* mean "weirdos"
805 strcpy(macfb_fix.id, "Mac TV");
809 * These don't have colour, so no need to worry
813 strcpy(macfb_fix.id, "Monochrome");
817 * Powerbooks are particularly difficult. Many of
818 * them have separate framebuffers for external and
819 * internal video, which is admittedly pretty cool,
820 * but will be a bit of a headache to support here.
821 * Also, many of them are grayscale, and we don't
822 * really support that.
826 * Slot 0 ROM says TIM. No external video. B&W.
828 case MAC_MODEL_PB140:
829 case MAC_MODEL_PB145:
830 case MAC_MODEL_PB170:
831 strcpy(macfb_fix.id, "DDC");
835 * Internal is GSC, External (if present) is ViSC
837 case MAC_MODEL_PB150: /* no external video */
838 case MAC_MODEL_PB160:
839 case MAC_MODEL_PB165:
840 case MAC_MODEL_PB180:
841 case MAC_MODEL_PB210:
842 case MAC_MODEL_PB230:
843 strcpy(macfb_fix.id, "GSC");
847 * Internal is TIM, External is ViSC
849 case MAC_MODEL_PB165C:
850 case MAC_MODEL_PB180C:
851 strcpy(macfb_fix.id, "TIM");
855 * Internal is CSC, External is Keystone+Ariel.
857 case MAC_MODEL_PB190: /* external video is optional */
858 case MAC_MODEL_PB520:
859 case MAC_MODEL_PB250:
860 case MAC_MODEL_PB270C:
861 case MAC_MODEL_PB280:
862 case MAC_MODEL_PB280C:
863 strcpy(macfb_fix.id, "CSC");
864 macfb_setpalette = csc_setpalette;
865 csc_cmap_regs = ioremap(CSC_BASE, 0x1000);
869 strcpy(macfb_fix.id, "Unknown");
873 fb_info.fbops = &macfb_ops;
874 fb_info.var = macfb_defined;
875 fb_info.fix = macfb_fix;
876 fb_info.pseudo_palette = pseudo_palette;
878 err = fb_alloc_cmap(&fb_info.cmap, video_cmap_len, 0);
882 err = register_framebuffer(&fb_info);
886 fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
891 fb_dealloc_cmap(&fb_info.cmap);
893 iounmap(fb_info.screen_base);
898 module_init(macfb_init);
899 MODULE_LICENSE("GPL");