cg3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* cg3.c: CGTHREE frame buffer driver
  2. *
  3. * Copyright (C) 2003, 2006 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1996,1998 Jakub Jelinek (jj@ultra.linux.cz)
  5. * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
  6. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  7. *
  8. * Driver layout based loosely on tgafb.c, see that file for credits.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/fb.h>
  17. #include <linux/mm.h>
  18. #include <linux/of_device.h>
  19. #include <asm/io.h>
  20. #include <asm/fbio.h>
  21. #include "sbuslib.h"
  22. /*
  23. * Local functions.
  24. */
  25. static int cg3_setcolreg(unsigned, unsigned, unsigned, unsigned,
  26. unsigned, struct fb_info *);
  27. static int cg3_blank(int, struct fb_info *);
  28. static int cg3_mmap(struct fb_info *, struct vm_area_struct *);
  29. static int cg3_ioctl(struct fb_info *, unsigned int, unsigned long);
  30. /*
  31. * Frame buffer operations
  32. */
  33. static struct fb_ops cg3_ops = {
  34. .owner = THIS_MODULE,
  35. .fb_setcolreg = cg3_setcolreg,
  36. .fb_blank = cg3_blank,
  37. .fb_fillrect = cfb_fillrect,
  38. .fb_copyarea = cfb_copyarea,
  39. .fb_imageblit = cfb_imageblit,
  40. .fb_mmap = cg3_mmap,
  41. .fb_ioctl = cg3_ioctl,
  42. #ifdef CONFIG_COMPAT
  43. .fb_compat_ioctl = sbusfb_compat_ioctl,
  44. #endif
  45. };
  46. /* Control Register Constants */
  47. #define CG3_CR_ENABLE_INTS 0x80
  48. #define CG3_CR_ENABLE_VIDEO 0x40
  49. #define CG3_CR_ENABLE_TIMING 0x20
  50. #define CG3_CR_ENABLE_CURCMP 0x10
  51. #define CG3_CR_XTAL_MASK 0x0c
  52. #define CG3_CR_DIVISOR_MASK 0x03
  53. /* Status Register Constants */
  54. #define CG3_SR_PENDING_INT 0x80
  55. #define CG3_SR_RES_MASK 0x70
  56. #define CG3_SR_1152_900_76_A 0x40
  57. #define CG3_SR_1152_900_76_B 0x60
  58. #define CG3_SR_ID_MASK 0x0f
  59. #define CG3_SR_ID_COLOR 0x01
  60. #define CG3_SR_ID_MONO 0x02
  61. #define CG3_SR_ID_MONO_ECL 0x03
  62. enum cg3_type {
  63. CG3_AT_66HZ = 0,
  64. CG3_AT_76HZ,
  65. CG3_RDI
  66. };
  67. struct bt_regs {
  68. u32 addr;
  69. u32 color_map;
  70. u32 control;
  71. u32 cursor;
  72. };
  73. struct cg3_regs {
  74. struct bt_regs cmap;
  75. u8 control;
  76. u8 status;
  77. u8 cursor_start;
  78. u8 cursor_end;
  79. u8 h_blank_start;
  80. u8 h_blank_end;
  81. u8 h_sync_start;
  82. u8 h_sync_end;
  83. u8 comp_sync_end;
  84. u8 v_blank_start_high;
  85. u8 v_blank_start_low;
  86. u8 v_blank_end;
  87. u8 v_sync_start;
  88. u8 v_sync_end;
  89. u8 xfer_holdoff_start;
  90. u8 xfer_holdoff_end;
  91. };
  92. /* Offset of interesting structures in the OBIO space */
  93. #define CG3_REGS_OFFSET 0x400000UL
  94. #define CG3_RAM_OFFSET 0x800000UL
  95. struct cg3_par {
  96. spinlock_t lock;
  97. struct cg3_regs __iomem *regs;
  98. u32 sw_cmap[((256 * 3) + 3) / 4];
  99. u32 flags;
  100. #define CG3_FLAG_BLANKED 0x00000001
  101. #define CG3_FLAG_RDI 0x00000002
  102. unsigned long which_io;
  103. };
  104. /**
  105. * cg3_setcolreg - Optional function. Sets a color register.
  106. * @regno: boolean, 0 copy local, 1 get_user() function
  107. * @red: frame buffer colormap structure
  108. * @green: The green value which can be up to 16 bits wide
  109. * @blue: The blue value which can be up to 16 bits wide.
  110. * @transp: If supported the alpha value which can be up to 16 bits wide.
  111. * @info: frame buffer info structure
  112. *
  113. * The cg3 palette is loaded with 4 color values at each time
  114. * so you end up with: (rgb)(r), (gb)(rg), (b)(rgb), and so on.
  115. * We keep a sw copy of the hw cmap to assist us in this esoteric
  116. * loading procedure.
  117. */
  118. static int cg3_setcolreg(unsigned regno,
  119. unsigned red, unsigned green, unsigned blue,
  120. unsigned transp, struct fb_info *info)
  121. {
  122. struct cg3_par *par = (struct cg3_par *) info->par;
  123. struct bt_regs __iomem *bt = &par->regs->cmap;
  124. unsigned long flags;
  125. u32 *p32;
  126. u8 *p8;
  127. int count;
  128. if (regno >= 256)
  129. return 1;
  130. red >>= 8;
  131. green >>= 8;
  132. blue >>= 8;
  133. spin_lock_irqsave(&par->lock, flags);
  134. p8 = (u8 *)par->sw_cmap + (regno * 3);
  135. p8[0] = red;
  136. p8[1] = green;
  137. p8[2] = blue;
  138. #define D4M3(x) ((((x)>>2)<<1) + ((x)>>2)) /* (x/4)*3 */
  139. #define D4M4(x) ((x)&~0x3) /* (x/4)*4 */
  140. count = 3;
  141. p32 = &par->sw_cmap[D4M3(regno)];
  142. sbus_writel(D4M4(regno), &bt->addr);
  143. while (count--)
  144. sbus_writel(*p32++, &bt->color_map);
  145. #undef D4M3
  146. #undef D4M4
  147. spin_unlock_irqrestore(&par->lock, flags);
  148. return 0;
  149. }
  150. /**
  151. * cg3_blank - Optional function. Blanks the display.
  152. * @blank_mode: the blank mode we want.
  153. * @info: frame buffer structure that represents a single frame buffer
  154. */
  155. static int cg3_blank(int blank, struct fb_info *info)
  156. {
  157. struct cg3_par *par = (struct cg3_par *) info->par;
  158. struct cg3_regs __iomem *regs = par->regs;
  159. unsigned long flags;
  160. u8 val;
  161. spin_lock_irqsave(&par->lock, flags);
  162. switch (blank) {
  163. case FB_BLANK_UNBLANK: /* Unblanking */
  164. val = sbus_readb(&regs->control);
  165. val |= CG3_CR_ENABLE_VIDEO;
  166. sbus_writeb(val, &regs->control);
  167. par->flags &= ~CG3_FLAG_BLANKED;
  168. break;
  169. case FB_BLANK_NORMAL: /* Normal blanking */
  170. case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
  171. case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
  172. case FB_BLANK_POWERDOWN: /* Poweroff */
  173. val = sbus_readb(&regs->control);
  174. val &= ~CG3_CR_ENABLE_VIDEO;
  175. sbus_writeb(val, &regs->control);
  176. par->flags |= CG3_FLAG_BLANKED;
  177. break;
  178. }
  179. spin_unlock_irqrestore(&par->lock, flags);
  180. return 0;
  181. }
  182. static struct sbus_mmap_map cg3_mmap_map[] = {
  183. {
  184. .voff = CG3_MMAP_OFFSET,
  185. .poff = CG3_RAM_OFFSET,
  186. .size = SBUS_MMAP_FBSIZE(1)
  187. },
  188. { .size = 0 }
  189. };
  190. static int cg3_mmap(struct fb_info *info, struct vm_area_struct *vma)
  191. {
  192. struct cg3_par *par = (struct cg3_par *)info->par;
  193. return sbusfb_mmap_helper(cg3_mmap_map,
  194. info->fix.smem_start, info->fix.smem_len,
  195. par->which_io,
  196. vma);
  197. }
  198. static int cg3_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  199. {
  200. return sbusfb_ioctl_helper(cmd, arg, info,
  201. FBTYPE_SUN3COLOR, 8, info->fix.smem_len);
  202. }
  203. /*
  204. * Initialisation
  205. */
  206. static void __devinit cg3_init_fix(struct fb_info *info, int linebytes,
  207. struct device_node *dp)
  208. {
  209. strlcpy(info->fix.id, dp->name, sizeof(info->fix.id));
  210. info->fix.type = FB_TYPE_PACKED_PIXELS;
  211. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  212. info->fix.line_length = linebytes;
  213. info->fix.accel = FB_ACCEL_SUN_CGTHREE;
  214. }
  215. static void __devinit cg3_rdi_maybe_fixup_var(struct fb_var_screeninfo *var,
  216. struct device_node *dp)
  217. {
  218. const char *params;
  219. char *p;
  220. int ww, hh;
  221. params = of_get_property(dp, "params", NULL);
  222. if (params) {
  223. ww = simple_strtoul(params, &p, 10);
  224. if (ww && *p == 'x') {
  225. hh = simple_strtoul(p + 1, &p, 10);
  226. if (hh && *p == '-') {
  227. if (var->xres != ww ||
  228. var->yres != hh) {
  229. var->xres = var->xres_virtual = ww;
  230. var->yres = var->yres_virtual = hh;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. static u8 cg3regvals_66hz[] __devinitdata = { /* 1152 x 900, 66 Hz */
  237. 0x14, 0xbb, 0x15, 0x2b, 0x16, 0x04, 0x17, 0x14,
  238. 0x18, 0xae, 0x19, 0x03, 0x1a, 0xa8, 0x1b, 0x24,
  239. 0x1c, 0x01, 0x1d, 0x05, 0x1e, 0xff, 0x1f, 0x01,
  240. 0x10, 0x20, 0
  241. };
  242. static u8 cg3regvals_76hz[] __devinitdata = { /* 1152 x 900, 76 Hz */
  243. 0x14, 0xb7, 0x15, 0x27, 0x16, 0x03, 0x17, 0x0f,
  244. 0x18, 0xae, 0x19, 0x03, 0x1a, 0xae, 0x1b, 0x2a,
  245. 0x1c, 0x01, 0x1d, 0x09, 0x1e, 0xff, 0x1f, 0x01,
  246. 0x10, 0x24, 0
  247. };
  248. static u8 cg3regvals_rdi[] __devinitdata = { /* 640 x 480, cgRDI */
  249. 0x14, 0x70, 0x15, 0x20, 0x16, 0x08, 0x17, 0x10,
  250. 0x18, 0x06, 0x19, 0x02, 0x1a, 0x31, 0x1b, 0x51,
  251. 0x1c, 0x06, 0x1d, 0x0c, 0x1e, 0xff, 0x1f, 0x01,
  252. 0x10, 0x22, 0
  253. };
  254. static u8 *cg3_regvals[] __devinitdata = {
  255. cg3regvals_66hz, cg3regvals_76hz, cg3regvals_rdi
  256. };
  257. static u_char cg3_dacvals[] __devinitdata = {
  258. 4, 0xff, 5, 0x00, 6, 0x70, 7, 0x00, 0
  259. };
  260. static int __devinit cg3_do_default_mode(struct cg3_par *par)
  261. {
  262. enum cg3_type type;
  263. u8 *p;
  264. if (par->flags & CG3_FLAG_RDI)
  265. type = CG3_RDI;
  266. else {
  267. u8 status = sbus_readb(&par->regs->status), mon;
  268. if ((status & CG3_SR_ID_MASK) == CG3_SR_ID_COLOR) {
  269. mon = status & CG3_SR_RES_MASK;
  270. if (mon == CG3_SR_1152_900_76_A ||
  271. mon == CG3_SR_1152_900_76_B)
  272. type = CG3_AT_76HZ;
  273. else
  274. type = CG3_AT_66HZ;
  275. } else {
  276. printk(KERN_ERR "cgthree: can't handle SR %02x\n",
  277. status);
  278. return -EINVAL;
  279. }
  280. }
  281. for (p = cg3_regvals[type]; *p; p += 2) {
  282. u8 __iomem *regp = &((u8 __iomem *)par->regs)[p[0]];
  283. sbus_writeb(p[1], regp);
  284. }
  285. for (p = cg3_dacvals; *p; p += 2) {
  286. u8 __iomem *regp;
  287. regp = (u8 __iomem *)&par->regs->cmap.addr;
  288. sbus_writeb(p[0], regp);
  289. regp = (u8 __iomem *)&par->regs->cmap.control;
  290. sbus_writeb(p[1], regp);
  291. }
  292. return 0;
  293. }
  294. static int __devinit cg3_probe(struct platform_device *op)
  295. {
  296. struct device_node *dp = op->dev.of_node;
  297. struct fb_info *info;
  298. struct cg3_par *par;
  299. int linebytes, err;
  300. info = framebuffer_alloc(sizeof(struct cg3_par), &op->dev);
  301. err = -ENOMEM;
  302. if (!info)
  303. goto out_err;
  304. par = info->par;
  305. spin_lock_init(&par->lock);
  306. info->fix.smem_start = op->resource[0].start;
  307. par->which_io = op->resource[0].flags & IORESOURCE_BITS;
  308. sbusfb_fill_var(&info->var, dp, 8);
  309. info->var.red.length = 8;
  310. info->var.green.length = 8;
  311. info->var.blue.length = 8;
  312. if (!strcmp(dp->name, "cgRDI"))
  313. par->flags |= CG3_FLAG_RDI;
  314. if (par->flags & CG3_FLAG_RDI)
  315. cg3_rdi_maybe_fixup_var(&info->var, dp);
  316. linebytes = of_getintprop_default(dp, "linebytes",
  317. info->var.xres);
  318. info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
  319. par->regs = of_ioremap(&op->resource[0], CG3_REGS_OFFSET,
  320. sizeof(struct cg3_regs), "cg3 regs");
  321. if (!par->regs)
  322. goto out_release_fb;
  323. info->flags = FBINFO_DEFAULT;
  324. info->fbops = &cg3_ops;
  325. info->screen_base = of_ioremap(&op->resource[0], CG3_RAM_OFFSET,
  326. info->fix.smem_len, "cg3 ram");
  327. if (!info->screen_base)
  328. goto out_unmap_regs;
  329. cg3_blank(FB_BLANK_UNBLANK, info);
  330. if (!of_find_property(dp, "width", NULL)) {
  331. err = cg3_do_default_mode(par);
  332. if (err)
  333. goto out_unmap_screen;
  334. }
  335. if (fb_alloc_cmap(&info->cmap, 256, 0))
  336. goto out_unmap_screen;
  337. fb_set_cmap(&info->cmap, info);
  338. cg3_init_fix(info, linebytes, dp);
  339. err = register_framebuffer(info);
  340. if (err < 0)
  341. goto out_dealloc_cmap;
  342. dev_set_drvdata(&op->dev, info);
  343. printk(KERN_INFO "%s: cg3 at %lx:%lx\n",
  344. dp->full_name, par->which_io, info->fix.smem_start);
  345. return 0;
  346. out_dealloc_cmap:
  347. fb_dealloc_cmap(&info->cmap);
  348. out_unmap_screen:
  349. of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
  350. out_unmap_regs:
  351. of_iounmap(&op->resource[0], par->regs, sizeof(struct cg3_regs));
  352. out_release_fb:
  353. framebuffer_release(info);
  354. out_err:
  355. return err;
  356. }
  357. static int __devexit cg3_remove(struct platform_device *op)
  358. {
  359. struct fb_info *info = dev_get_drvdata(&op->dev);
  360. struct cg3_par *par = info->par;
  361. unregister_framebuffer(info);
  362. fb_dealloc_cmap(&info->cmap);
  363. of_iounmap(&op->resource[0], par->regs, sizeof(struct cg3_regs));
  364. of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
  365. framebuffer_release(info);
  366. dev_set_drvdata(&op->dev, NULL);
  367. return 0;
  368. }
  369. static const struct of_device_id cg3_match[] = {
  370. {
  371. .name = "cgthree",
  372. },
  373. {
  374. .name = "cgRDI",
  375. },
  376. {},
  377. };
  378. MODULE_DEVICE_TABLE(of, cg3_match);
  379. static struct platform_driver cg3_driver = {
  380. .driver = {
  381. .name = "cg3",
  382. .owner = THIS_MODULE,
  383. .of_match_table = cg3_match,
  384. },
  385. .probe = cg3_probe,
  386. .remove = __devexit_p(cg3_remove),
  387. };
  388. static int __init cg3_init(void)
  389. {
  390. if (fb_get_options("cg3fb", NULL))
  391. return -ENODEV;
  392. return platform_driver_register(&cg3_driver);
  393. }
  394. static void __exit cg3_exit(void)
  395. {
  396. platform_driver_unregister(&cg3_driver);
  397. }
  398. module_init(cg3_init);
  399. module_exit(cg3_exit);
  400. MODULE_DESCRIPTION("framebuffer driver for CGthree chipsets");
  401. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  402. MODULE_VERSION("2.0");
  403. MODULE_LICENSE("GPL");