cg3.c 11 KB

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