2 * linux/drivers/video/mbx/mbxfb.c
4 * Copyright (C) 2006 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
13 * Intel 2700G (Marathon) Graphics Accelerator Frame Buffer Driver
17 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
25 #include <video/mbxfb.h>
30 static unsigned long virt_base_2700;
37 #define MAX_PALETTES 16
39 /* FIXME: take care of different chip revisions with different sizes
41 #define MEMORY_OFFSET 0x60000
46 struct resource *fb_res;
47 struct resource *fb_req;
49 struct resource *reg_res;
50 struct resource *reg_req;
52 void __iomem *fb_virt_addr;
53 unsigned long fb_phys_addr;
55 void __iomem *reg_virt_addr;
56 unsigned long reg_phys_addr;
58 int (*platform_probe) (struct fb_info * fb);
59 int (*platform_remove) (struct fb_info * fb);
61 u32 pseudo_palette[MAX_PALETTES];
62 #ifdef CONFIG_FB_MBX_DEBUG
68 static struct fb_var_screeninfo mbxfb_default __devinitdata = {
77 .activate = FB_ACTIVATE_TEST,
87 .vmode = FB_VMODE_NONINTERLACED,
88 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
91 static struct fb_fix_screeninfo mbxfb_fix __devinitdata = {
93 .type = FB_TYPE_PACKED_PIXELS,
94 .visual = FB_VISUAL_TRUECOLOR,
98 .accel = FB_ACCEL_NONE,
101 struct pixclock_div {
107 static unsigned int mbxfb_get_pixclock(unsigned int pixclock_ps,
108 struct pixclock_div *div)
111 unsigned int err = 0;
112 unsigned int min_err = ~0x0;
114 unsigned int best_clk = 0;
115 unsigned int ref_clk = 13000; /* FIXME: take from platform data */
116 unsigned int pixclock;
118 /* convert pixclock to KHz */
119 pixclock = PICOS2KHZ(pixclock_ps);
121 for (m = 1; m < 64; m++) {
122 for (n = 1; n < 8; n++) {
123 for (p = 0; p < 8; p++) {
124 clk = (ref_clk * m) / (n * (1 << p));
125 err = (clk > pixclock) ? (clk - pixclock) :
137 return KHZ2PICOS(best_clk);
140 static int mbxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
141 u_int trans, struct fb_info *info)
145 if (regno < MAX_PALETTES) {
146 u32 *pal = info->pseudo_palette;
148 val = (red & 0xf800) | ((green & 0xfc00) >> 5) |
149 ((blue & 0xf800) >> 11);
157 static int mbxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
159 struct pixclock_div div;
161 var->pixclock = mbxfb_get_pixclock(var->pixclock, &div);
163 if (var->xres < MIN_XRES)
164 var->xres = MIN_XRES;
165 if (var->yres < MIN_YRES)
166 var->yres = MIN_YRES;
167 if (var->xres > MAX_XRES)
169 if (var->yres > MAX_YRES)
171 var->xres_virtual = max(var->xres_virtual, var->xres);
172 var->yres_virtual = max(var->yres_virtual, var->yres);
174 switch (var->bits_per_pixel) {
175 /* 8 bits-per-pixel is not supported yet */
179 var->green.length = (var->green.length == 5) ? 5 : 6;
181 var->blue.length = 5;
182 var->transp.length = 6 - var->green.length;
183 var->blue.offset = 0;
184 var->green.offset = 5;
185 var->red.offset = 5 + var->green.length;
186 var->transp.offset = (5 + var->red.offset) & 15;
188 case 24: /* RGB 888 */
189 case 32: /* RGBA 8888 */
190 var->red.offset = 16;
192 var->green.offset = 8;
193 var->green.length = 8;
194 var->blue.offset = 0;
195 var->blue.length = 8;
196 var->transp.length = var->bits_per_pixel - 24;
197 var->transp.offset = (var->transp.length) ? 24 : 0;
200 var->red.msb_right = 0;
201 var->green.msb_right = 0;
202 var->blue.msb_right = 0;
203 var->transp.msb_right = 0;
208 static int mbxfb_set_par(struct fb_info *info)
210 struct fb_var_screeninfo *var = &info->var;
211 struct pixclock_div div;
212 ushort hbps, ht, hfps, has;
213 ushort vbps, vt, vfps, vas;
214 u32 gsctrl = readl(GSCTRL);
215 u32 gsadr = readl(GSADR);
217 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
219 /* setup color mode */
220 gsctrl &= ~(FMsk(GSCTRL_GPIXFMT));
221 /* FIXME: add *WORKING* support for 8-bits per color */
222 if (info->var.bits_per_pixel == 8) {
225 fb_dealloc_cmap(&info->cmap);
226 gsctrl &= ~GSCTRL_LUT_EN;
228 info->fix.visual = FB_VISUAL_TRUECOLOR;
229 switch (info->var.bits_per_pixel) {
231 if (info->var.green.length == 5)
232 gsctrl |= GSCTRL_GPIXFMT_ARGB1555;
234 gsctrl |= GSCTRL_GPIXFMT_RGB565;
237 gsctrl |= GSCTRL_GPIXFMT_RGB888;
240 gsctrl |= GSCTRL_GPIXFMT_ARGB8888;
245 /* setup resolution */
246 gsctrl &= ~(FMsk(GSCTRL_GSWIDTH) | FMsk(GSCTRL_GSHEIGHT));
247 gsctrl |= Gsctrl_Width(info->var.xres - 1) |
248 Gsctrl_Height(info->var.yres - 1);
249 writel(gsctrl, GSCTRL);
252 gsadr &= ~(FMsk(GSADR_SRCSTRIDE));
253 gsadr |= Gsadr_Srcstride(info->var.xres * info->var.bits_per_pixel /
255 writel(gsadr, GSADR);
259 var->pixclock = mbxfb_get_pixclock(info->var.pixclock, &div);
261 writel((Disp_Pll_M(div.m) | Disp_Pll_N(div.n) |
262 Disp_Pll_P(div.p) | DISP_PLL_EN), DISPPLL);
264 hbps = var->hsync_len;
265 has = hbps + var->left_margin;
266 hfps = has + var->xres;
267 ht = hfps + var->right_margin;
269 vbps = var->vsync_len;
270 vas = vbps + var->upper_margin;
271 vfps = vas + var->yres;
272 vt = vfps + var->lower_margin;
274 writel((Dht01_Hbps(hbps) | Dht01_Ht(ht)), DHT01);
275 writel((Dht02_Hlbs(has) | Dht02_Has(has)), DHT02);
276 writel((Dht03_Hfps(hfps) | Dht03_Hrbs(hfps)), DHT03);
277 writel((Dhdet_Hdes(has) | Dhdet_Hdef(hfps)), DHDET);
279 writel((Dvt01_Vbps(vbps) | Dvt01_Vt(vt)), DVT01);
280 writel((Dvt02_Vtbs(vas) | Dvt02_Vas(vas)), DVT02);
281 writel((Dvt03_Vfps(vfps) | Dvt03_Vbbs(vfps)), DVT03);
282 writel((Dvdet_Vdes(vas) | Dvdet_Vdef(vfps)), DVDET);
283 writel((Dvectrl_Vevent(vfps) | Dvectrl_Vfetch(vbps)), DVECTRL);
285 writel((readl(DSCTRL) | DSCTRL_SYNCGEN_EN), DSCTRL);
290 static int mbxfb_blank(int blank, struct fb_info *info)
293 case FB_BLANK_POWERDOWN:
294 case FB_BLANK_VSYNC_SUSPEND:
295 case FB_BLANK_HSYNC_SUSPEND:
296 case FB_BLANK_NORMAL:
297 writel((readl(DSCTRL) & ~DSCTRL_SYNCGEN_EN), DSCTRL);
299 writel((readl(PIXCLK) & ~PIXCLK_EN), PIXCLK);
301 writel((readl(VOVRCLK) & ~VOVRCLK_EN), VOVRCLK);
304 case FB_BLANK_UNBLANK:
305 writel((readl(DSCTRL) | DSCTRL_SYNCGEN_EN), DSCTRL);
307 writel((readl(PIXCLK) | PIXCLK_EN), PIXCLK);
314 static struct fb_ops mbxfb_ops = {
315 .owner = THIS_MODULE,
316 .fb_check_var = mbxfb_check_var,
317 .fb_set_par = mbxfb_set_par,
318 .fb_setcolreg = mbxfb_setcolreg,
319 .fb_fillrect = cfb_fillrect,
320 .fb_copyarea = cfb_copyarea,
321 .fb_imageblit = cfb_imageblit,
322 .fb_blank = mbxfb_blank,
326 Enable external SDRAM controller. Assume that all clocks are active
329 static void __devinit setup_memc(struct fb_info *fbi)
331 struct mbxfb_info *mfbi = fbi->par;
335 /* FIXME: use platfrom specific parameters */
336 /* setup SDRAM controller */
337 writel((LMCFG_LMC_DS | LMCFG_LMC_TS | LMCFG_LMD_TS |
342 writel(LMPWR_MC_PWR_ACT, LMPWR);
345 /* setup SDRAM timings */
346 writel((Lmtim_Tras(7) | Lmtim_Trp(3) | Lmtim_Trcd(3) |
347 Lmtim_Trc(9) | Lmtim_Tdpl(2)),
350 /* setup SDRAM refresh rate */
351 writel(0xc2b, LMREFRESH);
353 /* setup SDRAM type parameters */
354 writel((LMTYPE_CASLAT_3 | LMTYPE_BKSZ_2 | LMTYPE_ROWSZ_11 |
358 /* enable memory controller */
359 writel(LMPWR_MC_PWR_ACT, LMPWR);
362 /* perform dummy reads */
363 for ( i = 0; i < 16; i++ ) {
364 tmp = readl(fbi->screen_base);
368 static void enable_clocks(struct fb_info *fbi)
371 writel(SYSCLKSRC_PLL_2, SYSCLKSRC);
373 writel(PIXCLKSRC_PLL_1, PIXCLKSRC);
375 writel(0x00000000, CLKSLEEP);
377 writel((Core_Pll_M(0x17) | Core_Pll_N(0x3) | Core_Pll_P(0x0) |
381 writel((Disp_Pll_M(0x1b) | Disp_Pll_N(0x7) | Disp_Pll_P(0x1) |
385 writel(0x00000000, VOVRCLK);
387 writel(PIXCLK_EN, PIXCLK);
389 writel(MEMCLK_EN, MEMCLK);
391 writel(0x00000006, M24CLK);
393 writel(0x00000006, MBXCLK);
395 writel(SDCLK_EN, SDCLK);
397 writel(0x00000001, PIXCLKDIV);
401 static void __devinit setup_graphics(struct fb_info *fbi)
403 unsigned long gsctrl;
405 gsctrl = GSCTRL_GAMMA_EN | Gsctrl_Width(fbi->var.xres - 1) |
406 Gsctrl_Height(fbi->var.yres - 1);
407 switch (fbi->var.bits_per_pixel) {
409 if (fbi->var.green.length == 5)
410 gsctrl |= GSCTRL_GPIXFMT_ARGB1555;
412 gsctrl |= GSCTRL_GPIXFMT_RGB565;
415 gsctrl |= GSCTRL_GPIXFMT_RGB888;
418 gsctrl |= GSCTRL_GPIXFMT_ARGB8888;
422 writel(gsctrl, GSCTRL);
424 writel(0x00000000, GBBASE);
426 writel(0x00ffffff, GDRCTRL);
428 writel((GSCADR_STR_EN | Gscadr_Gbase_Adr(0x6000)), GSCADR);
430 writel(0x00000000, GPLUT);
434 static void __devinit setup_display(struct fb_info *fbi)
436 unsigned long dsctrl = 0;
438 dsctrl = DSCTRL_BLNK_POL;
439 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
440 dsctrl |= DSCTRL_HS_POL;
441 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
442 dsctrl |= DSCTRL_VS_POL;
443 writel(dsctrl, DSCTRL);
445 writel(0xd0303010, DMCTRL);
447 writel((readl(DSCTRL) | DSCTRL_SYNCGEN_EN), DSCTRL);
450 static void __devinit enable_controller(struct fb_info *fbi)
452 writel(SYSRST_RST, SYSRST);
464 * Power management hooks. Note that we won't be called from IRQ context,
465 * unlike the blank functions above, so we may sleep.
467 static int mbxfb_suspend(struct platform_device *dev, pm_message_t state)
469 /* make frame buffer memory enter self-refresh mode */
470 writel(LMPWR_MC_PWR_SRM, LMPWR);
471 while (LMPWRSTAT != LMPWRSTAT_MC_PWR_SRM)
472 ; /* empty statement */
474 /* reset the device, since it's initial state is 'mostly sleeping' */
475 writel(SYSRST_RST, SYSRST);
479 static int mbxfb_resume(struct platform_device *dev)
481 struct fb_info *fbi = platform_get_drvdata(dev);
484 /* setup_graphics(fbi); */
485 /* setup_display(fbi); */
487 writel((readl(DSCTRL) | DSCTRL_SYNCGEN_EN), DSCTRL);
491 #define mbxfb_suspend NULL
492 #define mbxfb_resume NULL
495 /* debugfs entries */
496 #ifndef CONFIG_FB_MBX_DEBUG
497 #define mbxfb_debugfs_init(x) do {} while(0)
498 #define mbxfb_debugfs_remove(x) do {} while(0)
501 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
503 static int __devinit mbxfb_probe(struct platform_device *dev)
507 struct mbxfb_info *mfbi;
508 struct mbxfb_platform_data *pdata;
510 dev_dbg(dev, "mbxfb_probe\n");
512 fbi = framebuffer_alloc(sizeof(struct mbxfb_info), &dev->dev);
514 dev_err(&dev->dev, "framebuffer_alloc failed\n");
519 fbi->pseudo_palette = mfbi->pseudo_palette;
520 pdata = dev->dev.platform_data;
522 mfbi->platform_probe = pdata->probe;
524 mfbi->platform_remove = pdata->remove;
526 mfbi->fb_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
527 mfbi->reg_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
529 if (!mfbi->fb_res || !mfbi->reg_res) {
530 dev_err(&dev->dev, "no resources found\n");
535 mfbi->fb_req = request_mem_region(mfbi->fb_res->start,
536 res_size(mfbi->fb_res), dev->name);
537 if (mfbi->fb_req == NULL) {
538 dev_err(&dev->dev, "failed to claim framebuffer memory\n");
542 mfbi->fb_phys_addr = mfbi->fb_res->start;
544 mfbi->reg_req = request_mem_region(mfbi->reg_res->start,
545 res_size(mfbi->reg_res), dev->name);
546 if (mfbi->reg_req == NULL) {
547 dev_err(&dev->dev, "failed to claim Marathon registers\n");
551 mfbi->reg_phys_addr = mfbi->reg_res->start;
553 mfbi->reg_virt_addr = ioremap_nocache(mfbi->reg_phys_addr,
554 res_size(mfbi->reg_req));
555 if (!mfbi->reg_virt_addr) {
556 dev_err(&dev->dev, "failed to ioremap Marathon registers\n");
560 virt_base_2700 = (unsigned long)mfbi->reg_virt_addr;
562 mfbi->fb_virt_addr = ioremap_nocache(mfbi->fb_phys_addr,
563 res_size(mfbi->fb_req));
564 if (!mfbi->reg_virt_addr) {
565 dev_err(&dev->dev, "failed to ioremap frame buffer\n");
570 /* FIXME: get from platform */
571 fbi->screen_base = (char __iomem *)(mfbi->fb_virt_addr + 0x60000);
572 fbi->screen_size = 8 * 1024 * 1024; /* 8 Megs */
573 fbi->fbops = &mbxfb_ops;
575 fbi->var = mbxfb_default;
576 fbi->fix = mbxfb_fix;
577 fbi->fix.smem_start = mfbi->fb_phys_addr + 0x60000;
578 fbi->fix.smem_len = 8 * 1024 * 1024;
579 fbi->fix.line_length = 640 * 2;
581 ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
583 dev_err(&dev->dev, "fb_alloc_cmap failed\n");
588 platform_set_drvdata(dev, fbi);
590 printk(KERN_INFO "fb%d: mbx frame buffer device\n", fbi->node);
592 if (mfbi->platform_probe)
593 mfbi->platform_probe(fbi);
595 enable_controller(fbi);
597 mbxfb_debugfs_init(fbi);
599 ret = register_framebuffer(fbi);
601 dev_err(&dev->dev, "register_framebuffer failed\n");
609 fb_dealloc_cmap(&fbi->cmap);
611 iounmap(mfbi->fb_virt_addr);
613 iounmap(mfbi->reg_virt_addr);
615 release_mem_region(mfbi->reg_res->start, res_size(mfbi->reg_res));
617 release_mem_region(mfbi->fb_res->start, res_size(mfbi->fb_res));
619 framebuffer_release(fbi);
624 static int __devexit mbxfb_remove(struct platform_device *dev)
626 struct fb_info *fbi = platform_get_drvdata(dev);
628 writel(SYSRST_RST, SYSRST);
631 mbxfb_debugfs_remove(fbi);
634 struct mbxfb_info *mfbi = fbi->par;
636 unregister_framebuffer(fbi);
638 if (mfbi->platform_remove)
639 mfbi->platform_remove(fbi);
641 if (mfbi->fb_virt_addr)
642 iounmap(mfbi->fb_virt_addr);
643 if (mfbi->reg_virt_addr)
644 iounmap(mfbi->reg_virt_addr);
646 release_mem_region(mfbi->reg_req->start,
647 res_size(mfbi->reg_req));
649 release_mem_region(mfbi->fb_req->start,
650 res_size(mfbi->fb_req));
652 framebuffer_release(fbi);
658 static struct platform_driver mbxfb_driver = {
659 .probe = mbxfb_probe,
660 .remove = mbxfb_remove,
661 .suspend = mbxfb_suspend,
662 .resume = mbxfb_resume,
668 int __devinit mbxfb_init(void)
670 return platform_driver_register(&mbxfb_driver);
673 static void __devexit mbxfb_exit(void)
675 platform_driver_unregister(&mbxfb_driver);
678 module_init(mbxfb_init);
679 module_exit(mbxfb_exit);
681 MODULE_DESCRIPTION("loadable framebuffer driver for Marathon device");
682 MODULE_AUTHOR("Mike Rapoport, Compulab");
683 MODULE_LICENSE("GPL");