Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  HP300 Topcat framebuffer support (derived from macfb of all things)
0004  *  Phil Blundell <philb@gnu.org> 1998
0005  *  DIO-II, colour map and Catseye support by
0006  *  Kars de Jong <jongk@linux-m68k.org>, May 2004.
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/errno.h>
0012 #include <linux/string.h>
0013 #include <linux/mm.h>
0014 #include <linux/delay.h>
0015 #include <linux/init.h>
0016 #include <linux/fb.h>
0017 #include <linux/dio.h>
0018 
0019 #include <asm/io.h>
0020 #include <linux/uaccess.h>
0021 
0022 static struct fb_info fb_info = {
0023     .fix = {
0024         .id     = "HP300 ",
0025         .type       = FB_TYPE_PACKED_PIXELS,
0026         .visual     = FB_VISUAL_PSEUDOCOLOR,
0027         .accel      = FB_ACCEL_NONE,
0028     }
0029 };
0030 
0031 static unsigned long fb_regs;
0032 static unsigned char fb_bitmask;
0033 
0034 #define TC_NBLANK   0x4080
0035 #define TC_WEN      0x4088
0036 #define TC_REN      0x408c
0037 #define TC_FBEN     0x4090
0038 #define TC_PRR      0x40ea
0039 
0040 /* These defines match the X window system */
0041 #define RR_CLEAR    0x0
0042 #define RR_COPY     0x3
0043 #define RR_NOOP     0x5
0044 #define RR_XOR      0x6
0045 #define RR_INVERT   0xa
0046 #define RR_COPYINVERTED 0xc
0047 #define RR_SET      0xf
0048 
0049 /* blitter regs */
0050 #define BUSY        0x4044
0051 #define WMRR        0x40ef
0052 #define SOURCE_X    0x40f2
0053 #define SOURCE_Y    0x40f6
0054 #define DEST_X      0x40fa
0055 #define DEST_Y      0x40fe
0056 #define WHEIGHT     0x4106
0057 #define WWIDTH      0x4102
0058 #define WMOVE       0x409c
0059 
0060 static struct fb_var_screeninfo hpfb_defined = {
0061     .red        = {
0062         .length = 8,
0063     },
0064     .green      = {
0065         .length = 8,
0066     },
0067     .blue       = {
0068         .length = 8,
0069     },
0070     .activate   = FB_ACTIVATE_NOW,
0071     .height     = -1,
0072     .width      = -1,
0073     .vmode      = FB_VMODE_NONINTERLACED,
0074 };
0075 
0076 static int hpfb_setcolreg(unsigned regno, unsigned red, unsigned green,
0077               unsigned blue, unsigned transp,
0078               struct fb_info *info)
0079 {
0080     /* use MSBs */
0081     unsigned char _red  =red>>8;
0082     unsigned char _green=green>>8;
0083     unsigned char _blue =blue>>8;
0084     unsigned char _regno=regno;
0085 
0086     /*
0087      *  Set a single color register. The values supplied are
0088      *  already rounded down to the hardware's capabilities
0089      *  (according to the entries in the `var' structure). Return
0090      *  != 0 for invalid regno.
0091      */
0092 
0093     if (regno >= info->cmap.len)
0094         return 1;
0095     
0096     while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
0097 
0098     out_be16(fb_regs + 0x60ba, 0xff);
0099 
0100     out_be16(fb_regs + 0x60b2, _red);
0101     out_be16(fb_regs + 0x60b4, _green);
0102     out_be16(fb_regs + 0x60b6, _blue);
0103     out_be16(fb_regs + 0x60b8, ~_regno);
0104     out_be16(fb_regs + 0x60f0, 0xff);
0105 
0106     udelay(100);
0107 
0108     while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
0109     out_be16(fb_regs + 0x60b2, 0);
0110     out_be16(fb_regs + 0x60b4, 0);
0111     out_be16(fb_regs + 0x60b6, 0);
0112     out_be16(fb_regs + 0x60b8, 0);
0113 
0114     return 0;
0115 }
0116 
0117 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
0118 
0119 static int hpfb_blank(int blank, struct fb_info *info)
0120 {
0121     out_8(fb_regs + TC_NBLANK, (blank ? 0x00 : fb_bitmask));
0122 
0123     return 0;
0124 }
0125 
0126 static void topcat_blit(int x0, int y0, int x1, int y1, int w, int h, int rr)
0127 {
0128     if (rr >= 0) {
0129         while (in_8(fb_regs + BUSY) & fb_bitmask)
0130             ;
0131     }
0132     out_8(fb_regs + TC_FBEN, fb_bitmask);
0133     if (rr >= 0) {
0134         out_8(fb_regs + TC_WEN, fb_bitmask);
0135         out_8(fb_regs + WMRR, rr);
0136     }
0137     out_be16(fb_regs + SOURCE_X, x0);
0138     out_be16(fb_regs + SOURCE_Y, y0);
0139     out_be16(fb_regs + DEST_X, x1);
0140     out_be16(fb_regs + DEST_Y, y1);
0141     out_be16(fb_regs + WWIDTH, w);
0142     out_be16(fb_regs + WHEIGHT, h);
0143     out_8(fb_regs + WMOVE, fb_bitmask);
0144 }
0145 
0146 static void hpfb_copyarea(struct fb_info *info, const struct fb_copyarea *area) 
0147 {
0148     topcat_blit(area->sx, area->sy, area->dx, area->dy, area->width, area->height, RR_COPY);
0149 }
0150 
0151 static void hpfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
0152 {
0153     u8 clr;
0154 
0155     clr = region->color & 0xff;
0156 
0157     while (in_8(fb_regs + BUSY) & fb_bitmask)
0158         ;
0159 
0160     /* Foreground */
0161     out_8(fb_regs + TC_WEN, fb_bitmask & clr);
0162     out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_SET : RR_INVERT));
0163 
0164     /* Background */
0165     out_8(fb_regs + TC_WEN, fb_bitmask & ~clr);
0166     out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_CLEAR : RR_NOOP));
0167 
0168     topcat_blit(region->dx, region->dy, region->dx, region->dy, region->width, region->height, -1);
0169 }
0170 
0171 static int hpfb_sync(struct fb_info *info)
0172 {
0173     /*
0174      * Since we also access the framebuffer directly, we have to wait
0175      * until the block mover is finished
0176      */
0177     while (in_8(fb_regs + BUSY) & fb_bitmask)
0178         ;
0179 
0180     out_8(fb_regs + TC_WEN, fb_bitmask);
0181     out_8(fb_regs + TC_PRR, RR_COPY);
0182     out_8(fb_regs + TC_FBEN, fb_bitmask);
0183 
0184     return 0;
0185 }
0186 
0187 static const struct fb_ops hpfb_ops = {
0188     .owner      = THIS_MODULE,
0189     .fb_setcolreg   = hpfb_setcolreg,
0190     .fb_blank   = hpfb_blank,
0191     .fb_fillrect    = hpfb_fillrect,
0192     .fb_copyarea    = hpfb_copyarea,
0193     .fb_imageblit   = cfb_imageblit,
0194     .fb_sync    = hpfb_sync,
0195 };
0196 
0197 /* Common to all HP framebuffers */
0198 #define HPFB_FBWMSB 0x05    /* Frame buffer width       */
0199 #define HPFB_FBWLSB 0x07
0200 #define HPFB_FBHMSB 0x09    /* Frame buffer height      */
0201 #define HPFB_FBHLSB 0x0b
0202 #define HPFB_DWMSB  0x0d    /* Display width        */
0203 #define HPFB_DWLSB  0x0f
0204 #define HPFB_DHMSB  0x11    /* Display height       */
0205 #define HPFB_DHLSB  0x13
0206 #define HPFB_NUMPLANES  0x5b    /* Number of colour planes  */
0207 #define HPFB_FBOMSB 0x5d    /* Frame buffer offset      */
0208 #define HPFB_FBOLSB 0x5f
0209 
0210 static int hpfb_init_one(unsigned long phys_base, unsigned long virt_base)
0211 {
0212     unsigned long fboff, fb_width, fb_height, fb_start;
0213     int ret;
0214 
0215     fb_regs = virt_base;
0216     fboff = (in_8(fb_regs + HPFB_FBOMSB) << 8) | in_8(fb_regs + HPFB_FBOLSB);
0217 
0218     fb_info.fix.smem_start = (in_8(fb_regs + fboff) << 16);
0219 
0220     if (phys_base >= DIOII_BASE) {
0221         fb_info.fix.smem_start += phys_base;
0222     }
0223 
0224     if (DIO_SECID(fb_regs) != DIO_ID2_TOPCAT) {
0225         /* This is the magic incantation the HP X server uses to make Catseye boards work. */
0226         while (in_be16(fb_regs+0x4800) & 1)
0227             ;
0228         out_be16(fb_regs+0x4800, 0);    /* Catseye status */
0229         out_be16(fb_regs+0x4510, 0);    /* VB */
0230         out_be16(fb_regs+0x4512, 0);    /* TCNTRL */
0231         out_be16(fb_regs+0x4514, 0);    /* ACNTRL */
0232         out_be16(fb_regs+0x4516, 0);    /* PNCNTRL */
0233         out_be16(fb_regs+0x4206, 0x90); /* RUG Command/Status */
0234         out_be16(fb_regs+0x60a2, 0);    /* Overlay Mask */
0235         out_be16(fb_regs+0x60bc, 0);    /* Ram Select */
0236     }
0237 
0238     /*
0239      *  Fill in the available video resolution
0240      */
0241     fb_width = (in_8(fb_regs + HPFB_FBWMSB) << 8) | in_8(fb_regs + HPFB_FBWLSB);
0242     fb_info.fix.line_length = fb_width;
0243     fb_height = (in_8(fb_regs + HPFB_FBHMSB) << 8) | in_8(fb_regs + HPFB_FBHLSB);
0244     fb_info.fix.smem_len = fb_width * fb_height;
0245     fb_start = (unsigned long)ioremap_wt(fb_info.fix.smem_start,
0246                          fb_info.fix.smem_len);
0247     hpfb_defined.xres = (in_8(fb_regs + HPFB_DWMSB) << 8) | in_8(fb_regs + HPFB_DWLSB);
0248     hpfb_defined.yres = (in_8(fb_regs + HPFB_DHMSB) << 8) | in_8(fb_regs + HPFB_DHLSB);
0249     hpfb_defined.xres_virtual = hpfb_defined.xres;
0250     hpfb_defined.yres_virtual = hpfb_defined.yres;
0251     hpfb_defined.bits_per_pixel = in_8(fb_regs + HPFB_NUMPLANES);
0252 
0253     printk(KERN_INFO "hpfb: framebuffer at 0x%lx, mapped to 0x%lx, size %dk\n",
0254            fb_info.fix.smem_start, fb_start, fb_info.fix.smem_len/1024);
0255     printk(KERN_INFO "hpfb: mode is %dx%dx%d, linelength=%d\n",
0256            hpfb_defined.xres, hpfb_defined.yres, hpfb_defined.bits_per_pixel, fb_info.fix.line_length);
0257 
0258     /*
0259      *  Give the hardware a bit of a prod and work out how many bits per
0260      *  pixel are supported.
0261      */
0262     out_8(fb_regs + TC_WEN, 0xff);
0263     out_8(fb_regs + TC_PRR, RR_COPY);
0264     out_8(fb_regs + TC_FBEN, 0xff);
0265     out_8(fb_start, 0xff);
0266     fb_bitmask = in_8(fb_start);
0267     out_8(fb_start, 0);
0268 
0269     /*
0270      *  Enable reading/writing of all the planes.
0271      */
0272     out_8(fb_regs + TC_WEN, fb_bitmask);
0273     out_8(fb_regs + TC_PRR, RR_COPY);
0274     out_8(fb_regs + TC_REN, fb_bitmask);
0275     out_8(fb_regs + TC_FBEN, fb_bitmask);
0276 
0277     /*
0278      *  Clear the screen.
0279      */
0280     topcat_blit(0, 0, 0, 0, fb_width, fb_height, RR_CLEAR);
0281 
0282     /*
0283      *  Let there be consoles..
0284      */
0285     if (DIO_SECID(fb_regs) == DIO_ID2_TOPCAT)
0286         strcat(fb_info.fix.id, "Topcat");
0287     else
0288         strcat(fb_info.fix.id, "Catseye");
0289     fb_info.fbops = &hpfb_ops;
0290     fb_info.flags = FBINFO_DEFAULT;
0291     fb_info.var   = hpfb_defined;
0292     fb_info.screen_base = (char *)fb_start;
0293 
0294     ret = fb_alloc_cmap(&fb_info.cmap, 1 << hpfb_defined.bits_per_pixel, 0);
0295     if (ret < 0)
0296         goto unmap_screen_base;
0297 
0298     ret = register_framebuffer(&fb_info);
0299     if (ret < 0)
0300         goto dealloc_cmap;
0301 
0302     fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
0303 
0304     return 0;
0305 
0306 dealloc_cmap:
0307     fb_dealloc_cmap(&fb_info.cmap);
0308 
0309 unmap_screen_base:
0310     if (fb_info.screen_base) {
0311         iounmap(fb_info.screen_base);
0312         fb_info.screen_base = NULL;
0313     }
0314 
0315     return ret;
0316 }
0317 
0318 /* 
0319  * Check that the secondary ID indicates that we have some hope of working with this
0320  * framebuffer.  The catseye boards are pretty much like topcats and we can muddle through.
0321  */
0322 
0323 #define topcat_sid_ok(x)  (((x) == DIO_ID2_LRCATSEYE) || ((x) == DIO_ID2_HRCCATSEYE)    \
0324                || ((x) == DIO_ID2_HRMCATSEYE) || ((x) == DIO_ID2_TOPCAT))
0325 
0326 /* 
0327  * Initialise the framebuffer
0328  */
0329 static int hpfb_dio_probe(struct dio_dev *d, const struct dio_device_id *ent)
0330 {
0331     unsigned long paddr, vaddr;
0332 
0333     paddr = d->resource.start;
0334     if (!request_mem_region(d->resource.start, resource_size(&d->resource), d->name))
0335                 return -EBUSY;
0336 
0337     if (d->scode >= DIOII_SCBASE) {
0338         vaddr = (unsigned long)ioremap(paddr, resource_size(&d->resource));
0339     } else {
0340         vaddr = paddr + DIO_VIRADDRBASE;
0341     }
0342     printk(KERN_INFO "Topcat found at DIO select code %d "
0343            "(secondary id %02x)\n", d->scode, (d->id >> 8) & 0xff);
0344     if (hpfb_init_one(paddr, vaddr)) {
0345         if (d->scode >= DIOII_SCBASE)
0346             iounmap((void *)vaddr);
0347         return -ENOMEM;
0348     }
0349     return 0;
0350 }
0351 
0352 static void hpfb_remove_one(struct dio_dev *d)
0353 {
0354     unregister_framebuffer(&fb_info);
0355     if (d->scode >= DIOII_SCBASE)
0356         iounmap((void *)fb_regs);
0357     release_mem_region(d->resource.start, resource_size(&d->resource));
0358     fb_dealloc_cmap(&fb_info.cmap);
0359     if (fb_info.screen_base)
0360         iounmap(fb_info.screen_base);
0361 }
0362 
0363 static struct dio_device_id hpfb_dio_tbl[] = {
0364     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_LRCATSEYE) },
0365     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRCCATSEYE) },
0366     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRMCATSEYE) },
0367     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_TOPCAT) },
0368     { 0 }
0369 };
0370 
0371 static struct dio_driver hpfb_driver = {
0372     .name      = "hpfb",
0373     .id_table  = hpfb_dio_tbl,
0374     .probe     = hpfb_dio_probe,
0375     .remove    = hpfb_remove_one,
0376 };
0377 
0378 static int __init hpfb_init(void)
0379 {
0380     unsigned int sid;
0381     unsigned char i;
0382     int err;
0383 
0384     /* Topcats can be on the internal IO bus or real DIO devices.
0385      * The internal variant sits at 0x560000; it has primary
0386      * and secondary ID registers just like the DIO version.
0387      * So we merge the two detection routines.
0388      *
0389      * Perhaps this #define should be in a global header file:
0390      * I believe it's common to all internal fbs, not just topcat.
0391      */
0392 #define INTFBVADDR 0xf0560000
0393 #define INTFBPADDR 0x560000
0394 
0395     if (!MACH_IS_HP300)
0396         return -ENODEV;
0397 
0398     if (fb_get_options("hpfb", NULL))
0399         return -ENODEV;
0400 
0401     err = dio_register_driver(&hpfb_driver);
0402     if (err)
0403         return err;
0404 
0405     err = copy_from_kernel_nofault(&i, (unsigned char *)INTFBVADDR + DIO_IDOFF, 1);
0406 
0407     if (!err && (i == DIO_ID_FBUFFER) && topcat_sid_ok(sid = DIO_SECID(INTFBVADDR))) {
0408         if (!request_mem_region(INTFBPADDR, DIO_DEVSIZE, "Internal Topcat"))
0409             return -EBUSY;
0410         printk(KERN_INFO "Internal Topcat found (secondary id %02x)\n", sid);
0411         if (hpfb_init_one(INTFBPADDR, INTFBVADDR)) {
0412             return -ENOMEM;
0413         }
0414     }
0415     return 0;
0416 }
0417 
0418 static void __exit hpfb_cleanup_module(void)
0419 {
0420     dio_unregister_driver(&hpfb_driver);
0421 }
0422 
0423 module_init(hpfb_init);
0424 module_exit(hpfb_cleanup_module);
0425 
0426 MODULE_LICENSE("GPL");