Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Geode GX framebuffer driver.
0004  *
0005  *   Copyright (C) 2006 Arcom Control Systems Ltd.
0006  *
0007  * This driver assumes that the BIOS has created a virtual PCI device header
0008  * for the video device. The PCI header is assumed to contain the following
0009  * BARs:
0010  *
0011  *    BAR0 - framebuffer memory
0012  *    BAR1 - graphics processor registers
0013  *    BAR2 - display controller registers
0014  *    BAR3 - video processor and flat panel control registers.
0015  *
0016  * 16 MiB of framebuffer memory is assumed to be available.
0017  */
0018 #include <linux/module.h>
0019 #include <linux/kernel.h>
0020 #include <linux/errno.h>
0021 #include <linux/string.h>
0022 #include <linux/mm.h>
0023 #include <linux/delay.h>
0024 #include <linux/fb.h>
0025 #include <linux/console.h>
0026 #include <linux/suspend.h>
0027 #include <linux/init.h>
0028 #include <linux/pci.h>
0029 #include <linux/cs5535.h>
0030 
0031 #include <asm/olpc.h>
0032 
0033 #include "gxfb.h"
0034 
0035 static char *mode_option;
0036 static int vram;
0037 static int vt_switch;
0038 
0039 /* Modes relevant to the GX (taken from modedb.c) */
0040 static struct fb_videomode gx_modedb[] = {
0041     /* 640x480-60 VESA */
0042     { NULL, 60, 640, 480, 39682,  48, 16, 33, 10, 96, 2,
0043       0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0044     /* 640x480-75 VESA */
0045     { NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
0046       0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0047     /* 640x480-85 VESA */
0048     { NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
0049       0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0050     /* 800x600-60 VESA */
0051     { NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
0052       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0053       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0054     /* 800x600-75 VESA */
0055     { NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
0056       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0057       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0058     /* 800x600-85 VESA */
0059     { NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
0060       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0061       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0062     /* 1024x768-60 VESA */
0063     { NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
0064       0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0065     /* 1024x768-75 VESA */
0066     { NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
0067       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0068       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0069     /* 1024x768-85 VESA */
0070     { NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
0071       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0072       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0073     /* 1280x960-60 VESA */
0074     { NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
0075       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0076       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0077     /* 1280x960-85 VESA */
0078     { NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
0079       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0080       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0081     /* 1280x1024-60 VESA */
0082     { NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
0083       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0084       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0085     /* 1280x1024-75 VESA */
0086     { NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
0087       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0088       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0089     /* 1280x1024-85 VESA */
0090     { NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
0091       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0092       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0093     /* 1600x1200-60 VESA */
0094     { NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
0095       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0096       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0097     /* 1600x1200-75 VESA */
0098     { NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
0099       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0100       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0101     /* 1600x1200-85 VESA */
0102     { NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
0103       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0104       FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
0105 };
0106 
0107 static struct fb_videomode gx_dcon_modedb[] = {
0108     /* The only mode the DCON has is 1200x900 */
0109     { NULL, 50, 1200, 900, 17460, 24, 8, 4, 5, 8, 3,
0110       FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
0111       FB_VMODE_NONINTERLACED, 0 }
0112 };
0113 
0114 static void get_modedb(struct fb_videomode **modedb, unsigned int *size)
0115 {
0116     if (olpc_has_dcon()) {
0117         *modedb = (struct fb_videomode *) gx_dcon_modedb;
0118         *size = ARRAY_SIZE(gx_dcon_modedb);
0119     } else {
0120         *modedb = (struct fb_videomode *) gx_modedb;
0121         *size = ARRAY_SIZE(gx_modedb);
0122     }
0123 }
0124 
0125 static int gxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
0126 {
0127     if (var->xres > 1600 || var->yres > 1200)
0128         return -EINVAL;
0129     if ((var->xres > 1280 || var->yres > 1024) && var->bits_per_pixel > 16)
0130         return -EINVAL;
0131 
0132     if (var->bits_per_pixel == 32) {
0133         var->red.offset   = 16; var->red.length   = 8;
0134         var->green.offset =  8; var->green.length = 8;
0135         var->blue.offset  =  0; var->blue.length  = 8;
0136     } else if (var->bits_per_pixel == 16) {
0137         var->red.offset   = 11; var->red.length   = 5;
0138         var->green.offset =  5; var->green.length = 6;
0139         var->blue.offset  =  0; var->blue.length  = 5;
0140     } else if (var->bits_per_pixel == 8) {
0141         var->red.offset   = 0; var->red.length   = 8;
0142         var->green.offset = 0; var->green.length = 8;
0143         var->blue.offset  = 0; var->blue.length  = 8;
0144     } else
0145         return -EINVAL;
0146     var->transp.offset = 0; var->transp.length = 0;
0147 
0148     /* Enough video memory? */
0149     if (gx_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
0150         return -EINVAL;
0151 
0152     /* FIXME: Check timing parameters here? */
0153 
0154     return 0;
0155 }
0156 
0157 static int gxfb_set_par(struct fb_info *info)
0158 {
0159     if (info->var.bits_per_pixel > 8)
0160         info->fix.visual = FB_VISUAL_TRUECOLOR;
0161     else
0162         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
0163 
0164     info->fix.line_length = gx_line_delta(info->var.xres, info->var.bits_per_pixel);
0165 
0166     gx_set_mode(info);
0167 
0168     return 0;
0169 }
0170 
0171 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
0172 {
0173     chan &= 0xffff;
0174     chan >>= 16 - bf->length;
0175     return chan << bf->offset;
0176 }
0177 
0178 static int gxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
0179                unsigned blue, unsigned transp,
0180                struct fb_info *info)
0181 {
0182     if (info->var.grayscale) {
0183         /* grayscale = 0.30*R + 0.59*G + 0.11*B */
0184         red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
0185     }
0186 
0187     /* Truecolor has hardware independent palette */
0188     if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
0189         u32 *pal = info->pseudo_palette;
0190         u32 v;
0191 
0192         if (regno >= 16)
0193             return -EINVAL;
0194 
0195         v  = chan_to_field(red, &info->var.red);
0196         v |= chan_to_field(green, &info->var.green);
0197         v |= chan_to_field(blue, &info->var.blue);
0198 
0199         pal[regno] = v;
0200     } else {
0201         if (regno >= 256)
0202             return -EINVAL;
0203 
0204         gx_set_hw_palette_reg(info, regno, red, green, blue);
0205     }
0206 
0207     return 0;
0208 }
0209 
0210 static int gxfb_blank(int blank_mode, struct fb_info *info)
0211 {
0212     return gx_blank_display(info, blank_mode);
0213 }
0214 
0215 static int gxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
0216 {
0217     struct gxfb_par *par = info->par;
0218     int ret;
0219 
0220     ret = pci_enable_device(dev);
0221     if (ret < 0)
0222         return ret;
0223 
0224     ret = pci_request_region(dev, 3, "gxfb (video processor)");
0225     if (ret < 0)
0226         return ret;
0227     par->vid_regs = pci_ioremap_bar(dev, 3);
0228     if (!par->vid_regs)
0229         return -ENOMEM;
0230 
0231     ret = pci_request_region(dev, 2, "gxfb (display controller)");
0232     if (ret < 0)
0233         return ret;
0234     par->dc_regs = pci_ioremap_bar(dev, 2);
0235     if (!par->dc_regs)
0236         return -ENOMEM;
0237 
0238     ret = pci_request_region(dev, 1, "gxfb (graphics processor)");
0239     if (ret < 0)
0240         return ret;
0241     par->gp_regs = pci_ioremap_bar(dev, 1);
0242 
0243     if (!par->gp_regs)
0244         return -ENOMEM;
0245 
0246     ret = pci_request_region(dev, 0, "gxfb (framebuffer)");
0247     if (ret < 0)
0248         return ret;
0249 
0250     info->fix.smem_start = pci_resource_start(dev, 0);
0251     info->fix.smem_len = vram ? vram : gx_frame_buffer_size();
0252     info->screen_base = ioremap_wc(info->fix.smem_start,
0253                        info->fix.smem_len);
0254     if (!info->screen_base)
0255         return -ENOMEM;
0256 
0257     /* Set the 16MiB aligned base address of the graphics memory region
0258      * in the display controller */
0259 
0260     write_dc(par, DC_GLIU0_MEM_OFFSET, info->fix.smem_start & 0xFF000000);
0261 
0262     dev_info(&dev->dev, "%d KiB of video memory at 0x%lx\n",
0263          info->fix.smem_len / 1024, info->fix.smem_start);
0264 
0265     return 0;
0266 }
0267 
0268 static const struct fb_ops gxfb_ops = {
0269     .owner      = THIS_MODULE,
0270     .fb_check_var   = gxfb_check_var,
0271     .fb_set_par = gxfb_set_par,
0272     .fb_setcolreg   = gxfb_setcolreg,
0273     .fb_blank       = gxfb_blank,
0274     /* No HW acceleration for now. */
0275     .fb_fillrect    = cfb_fillrect,
0276     .fb_copyarea    = cfb_copyarea,
0277     .fb_imageblit   = cfb_imageblit,
0278 };
0279 
0280 static struct fb_info *gxfb_init_fbinfo(struct device *dev)
0281 {
0282     struct gxfb_par *par;
0283     struct fb_info *info;
0284 
0285     /* Alloc enough space for the pseudo palette. */
0286     info = framebuffer_alloc(sizeof(struct gxfb_par) + sizeof(u32) * 16,
0287             dev);
0288     if (!info)
0289         return NULL;
0290 
0291     par = info->par;
0292 
0293     strcpy(info->fix.id, "Geode GX");
0294 
0295     info->fix.type      = FB_TYPE_PACKED_PIXELS;
0296     info->fix.type_aux  = 0;
0297     info->fix.xpanstep  = 0;
0298     info->fix.ypanstep  = 0;
0299     info->fix.ywrapstep = 0;
0300     info->fix.accel     = FB_ACCEL_NONE;
0301 
0302     info->var.nonstd    = 0;
0303     info->var.activate  = FB_ACTIVATE_NOW;
0304     info->var.height    = -1;
0305     info->var.width = -1;
0306     info->var.accel_flags = 0;
0307     info->var.vmode = FB_VMODE_NONINTERLACED;
0308 
0309     info->fbops     = &gxfb_ops;
0310     info->flags     = FBINFO_DEFAULT;
0311     info->node      = -1;
0312 
0313     info->pseudo_palette    = (void *)par + sizeof(struct gxfb_par);
0314 
0315     info->var.grayscale = 0;
0316 
0317     if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
0318         framebuffer_release(info);
0319         return NULL;
0320     }
0321 
0322     return info;
0323 }
0324 
0325 static int __maybe_unused gxfb_suspend(struct device *dev)
0326 {
0327     struct fb_info *info = dev_get_drvdata(dev);
0328 
0329     console_lock();
0330     gx_powerdown(info);
0331     fb_set_suspend(info, 1);
0332     console_unlock();
0333 
0334     /* there's no point in setting PCI states; we emulate PCI, so
0335      * we don't end up getting power savings anyways */
0336 
0337     return 0;
0338 }
0339 
0340 static int __maybe_unused gxfb_resume(struct device *dev)
0341 {
0342     struct fb_info *info = dev_get_drvdata(dev);
0343     int ret;
0344 
0345     console_lock();
0346     ret = gx_powerup(info);
0347     if (ret) {
0348         printk(KERN_ERR "gxfb:  power up failed!\n");
0349         return ret;
0350     }
0351 
0352     fb_set_suspend(info, 0);
0353     console_unlock();
0354     return 0;
0355 }
0356 
0357 static int gxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
0358 {
0359     struct gxfb_par *par;
0360     struct fb_info *info;
0361     int ret;
0362     unsigned long val;
0363 
0364     struct fb_videomode *modedb_ptr;
0365     unsigned int modedb_size;
0366 
0367     info = gxfb_init_fbinfo(&pdev->dev);
0368     if (!info)
0369         return -ENOMEM;
0370     par = info->par;
0371 
0372     if ((ret = gxfb_map_video_memory(info, pdev)) < 0) {
0373         dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
0374         goto err;
0375     }
0376 
0377     /* Figure out if this is a TFT or CRT part */
0378 
0379     rdmsrl(MSR_GX_GLD_MSR_CONFIG, val);
0380 
0381     if ((val & MSR_GX_GLD_MSR_CONFIG_FP) == MSR_GX_GLD_MSR_CONFIG_FP)
0382         par->enable_crt = 0;
0383     else
0384         par->enable_crt = 1;
0385 
0386     get_modedb(&modedb_ptr, &modedb_size);
0387     ret = fb_find_mode(&info->var, info, mode_option,
0388                modedb_ptr, modedb_size, NULL, 16);
0389     if (ret == 0 || ret == 4) {
0390         dev_err(&pdev->dev, "could not find valid video mode\n");
0391         ret = -EINVAL;
0392         goto err;
0393     }
0394 
0395 
0396     /* Clear the frame buffer of garbage. */
0397         memset_io(info->screen_base, 0, info->fix.smem_len);
0398 
0399     gxfb_check_var(&info->var, info);
0400     gxfb_set_par(info);
0401 
0402     pm_set_vt_switch(vt_switch);
0403 
0404     if (register_framebuffer(info) < 0) {
0405         ret = -EINVAL;
0406         goto err;
0407     }
0408     pci_set_drvdata(pdev, info);
0409     fb_info(info, "%s frame buffer device\n", info->fix.id);
0410     return 0;
0411 
0412   err:
0413     if (info->screen_base) {
0414         iounmap(info->screen_base);
0415         pci_release_region(pdev, 0);
0416     }
0417     if (par->vid_regs) {
0418         iounmap(par->vid_regs);
0419         pci_release_region(pdev, 3);
0420     }
0421     if (par->dc_regs) {
0422         iounmap(par->dc_regs);
0423         pci_release_region(pdev, 2);
0424     }
0425     if (par->gp_regs) {
0426         iounmap(par->gp_regs);
0427         pci_release_region(pdev, 1);
0428     }
0429 
0430     fb_dealloc_cmap(&info->cmap);
0431     framebuffer_release(info);
0432     return ret;
0433 }
0434 
0435 static void gxfb_remove(struct pci_dev *pdev)
0436 {
0437     struct fb_info *info = pci_get_drvdata(pdev);
0438     struct gxfb_par *par = info->par;
0439 
0440     unregister_framebuffer(info);
0441 
0442     iounmap((void __iomem *)info->screen_base);
0443     pci_release_region(pdev, 0);
0444 
0445     iounmap(par->vid_regs);
0446     pci_release_region(pdev, 3);
0447 
0448     iounmap(par->dc_regs);
0449     pci_release_region(pdev, 2);
0450 
0451     iounmap(par->gp_regs);
0452     pci_release_region(pdev, 1);
0453 
0454     fb_dealloc_cmap(&info->cmap);
0455 
0456     framebuffer_release(info);
0457 }
0458 
0459 static const struct pci_device_id gxfb_id_table[] = {
0460     { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_GX_VIDEO) },
0461     { 0, }
0462 };
0463 
0464 MODULE_DEVICE_TABLE(pci, gxfb_id_table);
0465 
0466 static const struct dev_pm_ops gxfb_pm_ops = {
0467 #ifdef CONFIG_PM_SLEEP
0468     .suspend    = gxfb_suspend,
0469     .resume     = gxfb_resume,
0470     .freeze     = NULL,
0471     .thaw       = gxfb_resume,
0472     .poweroff   = NULL,
0473     .restore    = gxfb_resume,
0474 #endif
0475 };
0476 
0477 static struct pci_driver gxfb_driver = {
0478     .name       = "gxfb",
0479     .id_table   = gxfb_id_table,
0480     .probe      = gxfb_probe,
0481     .remove     = gxfb_remove,
0482     .driver.pm  = &gxfb_pm_ops,
0483 };
0484 
0485 #ifndef MODULE
0486 static int __init gxfb_setup(char *options)
0487 {
0488 
0489     char *opt;
0490 
0491     if (!options || !*options)
0492         return 0;
0493 
0494     while ((opt = strsep(&options, ",")) != NULL) {
0495         if (!*opt)
0496             continue;
0497 
0498         mode_option = opt;
0499     }
0500 
0501     return 0;
0502 }
0503 #endif
0504 
0505 static int __init gxfb_init(void)
0506 {
0507 #ifndef MODULE
0508     char *option = NULL;
0509 
0510     if (fb_get_options("gxfb", &option))
0511         return -ENODEV;
0512 
0513     gxfb_setup(option);
0514 #endif
0515     return pci_register_driver(&gxfb_driver);
0516 }
0517 
0518 static void __exit gxfb_cleanup(void)
0519 {
0520     pci_unregister_driver(&gxfb_driver);
0521 }
0522 
0523 module_init(gxfb_init);
0524 module_exit(gxfb_cleanup);
0525 
0526 module_param(mode_option, charp, 0);
0527 MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
0528 
0529 module_param(vram, int, 0);
0530 MODULE_PARM_DESC(vram, "video memory size");
0531 
0532 module_param(vt_switch, int, 0);
0533 MODULE_PARM_DESC(vt_switch, "enable VT switch during suspend/resume");
0534 
0535 MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode GX");
0536 MODULE_LICENSE("GPL");