tcx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* tcx.c: TCX 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) 1996 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 tcx_setcolreg(unsigned, unsigned, unsigned, unsigned,
  28. unsigned, struct fb_info *);
  29. static int tcx_blank(int, struct fb_info *);
  30. static int tcx_pan_display(struct fb_var_screeninfo *, struct fb_info *);
  31. static int tcx_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma);
  32. static int tcx_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg);
  33. /*
  34. * Frame buffer operations
  35. */
  36. static const struct fb_ops tcx_ops = {
  37. .owner = THIS_MODULE,
  38. FB_DEFAULT_SBUS_OPS(tcx),
  39. .fb_setcolreg = tcx_setcolreg,
  40. .fb_blank = tcx_blank,
  41. .fb_pan_display = tcx_pan_display,
  42. };
  43. /* THC definitions */
  44. #define TCX_THC_MISC_REV_SHIFT 16
  45. #define TCX_THC_MISC_REV_MASK 15
  46. #define TCX_THC_MISC_VSYNC_DIS (1 << 25)
  47. #define TCX_THC_MISC_HSYNC_DIS (1 << 24)
  48. #define TCX_THC_MISC_RESET (1 << 12)
  49. #define TCX_THC_MISC_VIDEO (1 << 10)
  50. #define TCX_THC_MISC_SYNC (1 << 9)
  51. #define TCX_THC_MISC_VSYNC (1 << 8)
  52. #define TCX_THC_MISC_SYNC_ENAB (1 << 7)
  53. #define TCX_THC_MISC_CURS_RES (1 << 6)
  54. #define TCX_THC_MISC_INT_ENAB (1 << 5)
  55. #define TCX_THC_MISC_INT (1 << 4)
  56. #define TCX_THC_MISC_INIT 0x9f
  57. #define TCX_THC_REV_REV_SHIFT 20
  58. #define TCX_THC_REV_REV_MASK 15
  59. #define TCX_THC_REV_MINREV_SHIFT 28
  60. #define TCX_THC_REV_MINREV_MASK 15
  61. /* The contents are unknown */
  62. struct tcx_tec {
  63. u32 tec_matrix;
  64. u32 tec_clip;
  65. u32 tec_vdc;
  66. };
  67. struct tcx_thc {
  68. u32 thc_rev;
  69. u32 thc_pad0[511];
  70. u32 thc_hs; /* hsync timing */
  71. u32 thc_hsdvs;
  72. u32 thc_hd;
  73. u32 thc_vs; /* vsync timing */
  74. u32 thc_vd;
  75. u32 thc_refresh;
  76. u32 thc_misc;
  77. u32 thc_pad1[56];
  78. u32 thc_cursxy; /* cursor x,y position (16 bits each) */
  79. u32 thc_cursmask[32]; /* cursor mask bits */
  80. u32 thc_cursbits[32]; /* what to show where mask enabled */
  81. };
  82. struct bt_regs {
  83. u32 addr;
  84. u32 color_map;
  85. u32 control;
  86. u32 cursor;
  87. };
  88. #define TCX_MMAP_ENTRIES 14
  89. struct tcx_par {
  90. spinlock_t lock;
  91. struct bt_regs __iomem *bt;
  92. struct tcx_thc __iomem *thc;
  93. struct tcx_tec __iomem *tec;
  94. u32 __iomem *cplane;
  95. u32 flags;
  96. #define TCX_FLAG_BLANKED 0x00000001
  97. unsigned long which_io;
  98. struct sbus_mmap_map mmap_map[TCX_MMAP_ENTRIES];
  99. int lowdepth;
  100. };
  101. /* Reset control plane so that WID is 8-bit plane. */
  102. static void __tcx_set_control_plane(struct fb_info *info)
  103. {
  104. struct tcx_par *par = info->par;
  105. u32 __iomem *p, *pend;
  106. if (par->lowdepth)
  107. return;
  108. p = par->cplane;
  109. if (p == NULL)
  110. return;
  111. for (pend = p + info->fix.smem_len; p < pend; p++) {
  112. u32 tmp = sbus_readl(p);
  113. tmp &= 0xffffff;
  114. sbus_writel(tmp, p);
  115. }
  116. }
  117. static void tcx_reset(struct fb_info *info)
  118. {
  119. struct tcx_par *par = (struct tcx_par *) info->par;
  120. unsigned long flags;
  121. spin_lock_irqsave(&par->lock, flags);
  122. __tcx_set_control_plane(info);
  123. spin_unlock_irqrestore(&par->lock, flags);
  124. }
  125. static int tcx_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  126. {
  127. tcx_reset(info);
  128. return 0;
  129. }
  130. /**
  131. * tcx_setcolreg - Optional function. Sets a color register.
  132. * @regno: boolean, 0 copy local, 1 get_user() function
  133. * @red: frame buffer colormap structure
  134. * @green: The green value which can be up to 16 bits wide
  135. * @blue: The blue value which can be up to 16 bits wide.
  136. * @transp: If supported the alpha value which can be up to 16 bits wide.
  137. * @info: frame buffer info structure
  138. */
  139. static int tcx_setcolreg(unsigned regno,
  140. unsigned red, unsigned green, unsigned blue,
  141. unsigned transp, struct fb_info *info)
  142. {
  143. struct tcx_par *par = (struct tcx_par *) info->par;
  144. struct bt_regs __iomem *bt = par->bt;
  145. unsigned long flags;
  146. if (regno >= 256)
  147. return 1;
  148. red >>= 8;
  149. green >>= 8;
  150. blue >>= 8;
  151. spin_lock_irqsave(&par->lock, flags);
  152. sbus_writel(regno << 24, &bt->addr);
  153. sbus_writel(red << 24, &bt->color_map);
  154. sbus_writel(green << 24, &bt->color_map);
  155. sbus_writel(blue << 24, &bt->color_map);
  156. spin_unlock_irqrestore(&par->lock, flags);
  157. return 0;
  158. }
  159. /**
  160. * tcx_blank - Optional function. Blanks the display.
  161. * @blank: the blank mode we want.
  162. * @info: frame buffer structure that represents a single frame buffer
  163. */
  164. static int
  165. tcx_blank(int blank, struct fb_info *info)
  166. {
  167. struct tcx_par *par = (struct tcx_par *) info->par;
  168. struct tcx_thc __iomem *thc = par->thc;
  169. unsigned long flags;
  170. u32 val;
  171. spin_lock_irqsave(&par->lock, flags);
  172. val = sbus_readl(&thc->thc_misc);
  173. switch (blank) {
  174. case FB_BLANK_UNBLANK: /* Unblanking */
  175. val &= ~(TCX_THC_MISC_VSYNC_DIS |
  176. TCX_THC_MISC_HSYNC_DIS);
  177. val |= TCX_THC_MISC_VIDEO;
  178. par->flags &= ~TCX_FLAG_BLANKED;
  179. break;
  180. case FB_BLANK_NORMAL: /* Normal blanking */
  181. val &= ~TCX_THC_MISC_VIDEO;
  182. par->flags |= TCX_FLAG_BLANKED;
  183. break;
  184. case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
  185. val |= TCX_THC_MISC_VSYNC_DIS;
  186. break;
  187. case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
  188. val |= TCX_THC_MISC_HSYNC_DIS;
  189. break;
  190. case FB_BLANK_POWERDOWN: /* Poweroff */
  191. break;
  192. }
  193. sbus_writel(val, &thc->thc_misc);
  194. spin_unlock_irqrestore(&par->lock, flags);
  195. return 0;
  196. }
  197. static const struct sbus_mmap_map __tcx_mmap_map[TCX_MMAP_ENTRIES] = {
  198. {
  199. .voff = TCX_RAM8BIT,
  200. .size = SBUS_MMAP_FBSIZE(1)
  201. },
  202. {
  203. .voff = TCX_RAM24BIT,
  204. .size = SBUS_MMAP_FBSIZE(4)
  205. },
  206. {
  207. .voff = TCX_UNK3,
  208. .size = SBUS_MMAP_FBSIZE(8)
  209. },
  210. {
  211. .voff = TCX_UNK4,
  212. .size = SBUS_MMAP_FBSIZE(8)
  213. },
  214. {
  215. .voff = TCX_CONTROLPLANE,
  216. .size = SBUS_MMAP_FBSIZE(4)
  217. },
  218. {
  219. .voff = TCX_UNK6,
  220. .size = SBUS_MMAP_FBSIZE(8)
  221. },
  222. {
  223. .voff = TCX_UNK7,
  224. .size = SBUS_MMAP_FBSIZE(8)
  225. },
  226. {
  227. .voff = TCX_TEC,
  228. .size = PAGE_SIZE
  229. },
  230. {
  231. .voff = TCX_BTREGS,
  232. .size = PAGE_SIZE
  233. },
  234. {
  235. .voff = TCX_THC,
  236. .size = PAGE_SIZE
  237. },
  238. {
  239. .voff = TCX_DHC,
  240. .size = PAGE_SIZE
  241. },
  242. {
  243. .voff = TCX_ALT,
  244. .size = PAGE_SIZE
  245. },
  246. {
  247. .voff = TCX_UNK2,
  248. .size = 0x20000
  249. },
  250. { .size = 0 }
  251. };
  252. static int tcx_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  253. {
  254. struct tcx_par *par = (struct tcx_par *)info->par;
  255. return sbusfb_mmap_helper(par->mmap_map,
  256. info->fix.smem_start, info->fix.smem_len,
  257. par->which_io, vma);
  258. }
  259. static int tcx_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  260. {
  261. struct tcx_par *par = (struct tcx_par *) info->par;
  262. return sbusfb_ioctl_helper(cmd, arg, info,
  263. FBTYPE_TCXCOLOR,
  264. (par->lowdepth ? 8 : 24),
  265. info->fix.smem_len);
  266. }
  267. /*
  268. * Initialisation
  269. */
  270. static void
  271. tcx_init_fix(struct fb_info *info, int linebytes)
  272. {
  273. struct tcx_par *par = (struct tcx_par *)info->par;
  274. const char *tcx_name;
  275. if (par->lowdepth)
  276. tcx_name = "TCX8";
  277. else
  278. tcx_name = "TCX24";
  279. strscpy(info->fix.id, tcx_name, sizeof(info->fix.id));
  280. info->fix.type = FB_TYPE_PACKED_PIXELS;
  281. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  282. info->fix.line_length = linebytes;
  283. info->fix.accel = FB_ACCEL_SUN_TCX;
  284. }
  285. static void tcx_unmap_regs(struct platform_device *op, struct fb_info *info,
  286. struct tcx_par *par)
  287. {
  288. if (par->tec)
  289. of_iounmap(&op->resource[7],
  290. par->tec, sizeof(struct tcx_tec));
  291. if (par->thc)
  292. of_iounmap(&op->resource[9],
  293. par->thc, sizeof(struct tcx_thc));
  294. if (par->bt)
  295. of_iounmap(&op->resource[8],
  296. par->bt, sizeof(struct bt_regs));
  297. if (par->cplane)
  298. of_iounmap(&op->resource[4],
  299. par->cplane, info->fix.smem_len * sizeof(u32));
  300. if (info->screen_base)
  301. of_iounmap(&op->resource[0],
  302. info->screen_base, info->fix.smem_len);
  303. }
  304. static int tcx_probe(struct platform_device *op)
  305. {
  306. struct device_node *dp = op->dev.of_node;
  307. struct fb_info *info;
  308. struct tcx_par *par;
  309. int linebytes, i, err;
  310. info = framebuffer_alloc(sizeof(struct tcx_par), &op->dev);
  311. err = -ENOMEM;
  312. if (!info)
  313. goto out_err;
  314. par = info->par;
  315. spin_lock_init(&par->lock);
  316. par->lowdepth = of_property_read_bool(dp, "tcx-8-bit");
  317. sbusfb_fill_var(&info->var, dp, 8);
  318. info->var.red.length = 8;
  319. info->var.green.length = 8;
  320. info->var.blue.length = 8;
  321. linebytes = of_getintprop_default(dp, "linebytes",
  322. info->var.xres);
  323. info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
  324. par->tec = of_ioremap(&op->resource[7], 0,
  325. sizeof(struct tcx_tec), "tcx tec");
  326. par->thc = of_ioremap(&op->resource[9], 0,
  327. sizeof(struct tcx_thc), "tcx thc");
  328. par->bt = of_ioremap(&op->resource[8], 0,
  329. sizeof(struct bt_regs), "tcx dac");
  330. info->screen_base = of_ioremap(&op->resource[0], 0,
  331. info->fix.smem_len, "tcx ram");
  332. if (!par->tec || !par->thc ||
  333. !par->bt || !info->screen_base)
  334. goto out_unmap_regs;
  335. memcpy(&par->mmap_map, &__tcx_mmap_map, sizeof(par->mmap_map));
  336. if (!par->lowdepth) {
  337. par->cplane = of_ioremap(&op->resource[4], 0,
  338. info->fix.smem_len * sizeof(u32),
  339. "tcx cplane");
  340. if (!par->cplane)
  341. goto out_unmap_regs;
  342. } else {
  343. par->mmap_map[1].size = SBUS_MMAP_EMPTY;
  344. par->mmap_map[4].size = SBUS_MMAP_EMPTY;
  345. par->mmap_map[5].size = SBUS_MMAP_EMPTY;
  346. par->mmap_map[6].size = SBUS_MMAP_EMPTY;
  347. }
  348. info->fix.smem_start = op->resource[0].start;
  349. par->which_io = op->resource[0].flags & IORESOURCE_BITS;
  350. for (i = 0; i < TCX_MMAP_ENTRIES; i++) {
  351. int j;
  352. switch (i) {
  353. case 10:
  354. j = 12;
  355. break;
  356. case 11: case 12:
  357. j = i - 1;
  358. break;
  359. default:
  360. j = i;
  361. break;
  362. }
  363. par->mmap_map[i].poff = op->resource[j].start - info->fix.smem_start;
  364. }
  365. info->fbops = &tcx_ops;
  366. /* Initialize brooktree DAC. */
  367. sbus_writel(0x04 << 24, &par->bt->addr); /* color planes */
  368. sbus_writel(0xff << 24, &par->bt->control);
  369. sbus_writel(0x05 << 24, &par->bt->addr);
  370. sbus_writel(0x00 << 24, &par->bt->control);
  371. sbus_writel(0x06 << 24, &par->bt->addr); /* overlay plane */
  372. sbus_writel(0x73 << 24, &par->bt->control);
  373. sbus_writel(0x07 << 24, &par->bt->addr);
  374. sbus_writel(0x00 << 24, &par->bt->control);
  375. tcx_reset(info);
  376. tcx_blank(FB_BLANK_UNBLANK, info);
  377. if (fb_alloc_cmap(&info->cmap, 256, 0))
  378. goto out_unmap_regs;
  379. fb_set_cmap(&info->cmap, info);
  380. tcx_init_fix(info, linebytes);
  381. err = register_framebuffer(info);
  382. if (err < 0)
  383. goto out_dealloc_cmap;
  384. dev_set_drvdata(&op->dev, info);
  385. printk(KERN_INFO "%pOF: TCX at %lx:%lx, %s\n",
  386. dp,
  387. par->which_io,
  388. info->fix.smem_start,
  389. par->lowdepth ? "8-bit only" : "24-bit depth");
  390. return 0;
  391. out_dealloc_cmap:
  392. fb_dealloc_cmap(&info->cmap);
  393. out_unmap_regs:
  394. tcx_unmap_regs(op, info, par);
  395. framebuffer_release(info);
  396. out_err:
  397. return err;
  398. }
  399. static void tcx_remove(struct platform_device *op)
  400. {
  401. struct fb_info *info = dev_get_drvdata(&op->dev);
  402. struct tcx_par *par = info->par;
  403. unregister_framebuffer(info);
  404. fb_dealloc_cmap(&info->cmap);
  405. tcx_unmap_regs(op, info, par);
  406. framebuffer_release(info);
  407. }
  408. static const struct of_device_id tcx_match[] = {
  409. {
  410. .name = "SUNW,tcx",
  411. },
  412. {},
  413. };
  414. MODULE_DEVICE_TABLE(of, tcx_match);
  415. static struct platform_driver tcx_driver = {
  416. .driver = {
  417. .name = "tcx",
  418. .of_match_table = tcx_match,
  419. },
  420. .probe = tcx_probe,
  421. .remove = tcx_remove,
  422. };
  423. static int __init tcx_init(void)
  424. {
  425. if (fb_get_options("tcxfb", NULL))
  426. return -ENODEV;
  427. return platform_driver_register(&tcx_driver);
  428. }
  429. static void __exit tcx_exit(void)
  430. {
  431. platform_driver_unregister(&tcx_driver);
  432. }
  433. module_init(tcx_init);
  434. module_exit(tcx_exit);
  435. MODULE_DESCRIPTION("framebuffer driver for TCX chipsets");
  436. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  437. MODULE_VERSION("2.0");
  438. MODULE_LICENSE("GPL");