cg14.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* cg14.c: CGFOURTEEN 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) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
  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/uaccess.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 cg14_setcolreg(unsigned, unsigned, unsigned, unsigned,
  28. unsigned, struct fb_info *);
  29. static int cg14_pan_display(struct fb_var_screeninfo *, struct fb_info *);
  30. static int cg14_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma);
  31. static int cg14_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg);
  32. /*
  33. * Frame buffer operations
  34. */
  35. static const struct fb_ops cg14_ops = {
  36. .owner = THIS_MODULE,
  37. FB_DEFAULT_SBUS_OPS(cg14),
  38. .fb_setcolreg = cg14_setcolreg,
  39. .fb_pan_display = cg14_pan_display,
  40. };
  41. #define CG14_MCR_INTENABLE_SHIFT 7
  42. #define CG14_MCR_INTENABLE_MASK 0x80
  43. #define CG14_MCR_VIDENABLE_SHIFT 6
  44. #define CG14_MCR_VIDENABLE_MASK 0x40
  45. #define CG14_MCR_PIXMODE_SHIFT 4
  46. #define CG14_MCR_PIXMODE_MASK 0x30
  47. #define CG14_MCR_TMR_SHIFT 2
  48. #define CG14_MCR_TMR_MASK 0x0c
  49. #define CG14_MCR_TMENABLE_SHIFT 1
  50. #define CG14_MCR_TMENABLE_MASK 0x02
  51. #define CG14_MCR_RESET_SHIFT 0
  52. #define CG14_MCR_RESET_MASK 0x01
  53. #define CG14_REV_REVISION_SHIFT 4
  54. #define CG14_REV_REVISION_MASK 0xf0
  55. #define CG14_REV_IMPL_SHIFT 0
  56. #define CG14_REV_IMPL_MASK 0x0f
  57. #define CG14_VBR_FRAMEBASE_SHIFT 12
  58. #define CG14_VBR_FRAMEBASE_MASK 0x00fff000
  59. #define CG14_VMCR1_SETUP_SHIFT 0
  60. #define CG14_VMCR1_SETUP_MASK 0x000001ff
  61. #define CG14_VMCR1_VCONFIG_SHIFT 9
  62. #define CG14_VMCR1_VCONFIG_MASK 0x00000e00
  63. #define CG14_VMCR2_REFRESH_SHIFT 0
  64. #define CG14_VMCR2_REFRESH_MASK 0x00000001
  65. #define CG14_VMCR2_TESTROWCNT_SHIFT 1
  66. #define CG14_VMCR2_TESTROWCNT_MASK 0x00000002
  67. #define CG14_VMCR2_FBCONFIG_SHIFT 2
  68. #define CG14_VMCR2_FBCONFIG_MASK 0x0000000c
  69. #define CG14_VCR_REFRESHREQ_SHIFT 0
  70. #define CG14_VCR_REFRESHREQ_MASK 0x000003ff
  71. #define CG14_VCR1_REFRESHENA_SHIFT 10
  72. #define CG14_VCR1_REFRESHENA_MASK 0x00000400
  73. #define CG14_VCA_CAD_SHIFT 0
  74. #define CG14_VCA_CAD_MASK 0x000003ff
  75. #define CG14_VCA_VERS_SHIFT 10
  76. #define CG14_VCA_VERS_MASK 0x00000c00
  77. #define CG14_VCA_RAMSPEED_SHIFT 12
  78. #define CG14_VCA_RAMSPEED_MASK 0x00001000
  79. #define CG14_VCA_8MB_SHIFT 13
  80. #define CG14_VCA_8MB_MASK 0x00002000
  81. #define CG14_MCR_PIXMODE_8 0
  82. #define CG14_MCR_PIXMODE_16 2
  83. #define CG14_MCR_PIXMODE_32 3
  84. struct cg14_regs{
  85. u8 mcr; /* Master Control Reg */
  86. u8 ppr; /* Packed Pixel Reg */
  87. u8 tms[2]; /* Test Mode Status Regs */
  88. u8 msr; /* Master Status Reg */
  89. u8 fsr; /* Fault Status Reg */
  90. u8 rev; /* Revision & Impl */
  91. u8 ccr; /* Clock Control Reg */
  92. u32 tmr; /* Test Mode Read Back */
  93. u8 mod; /* Monitor Operation Data Reg */
  94. u8 acr; /* Aux Control */
  95. u8 xxx0[6];
  96. u16 hct; /* Hor Counter */
  97. u16 vct; /* Vert Counter */
  98. u16 hbs; /* Hor Blank Start */
  99. u16 hbc; /* Hor Blank Clear */
  100. u16 hss; /* Hor Sync Start */
  101. u16 hsc; /* Hor Sync Clear */
  102. u16 csc; /* Composite Sync Clear */
  103. u16 vbs; /* Vert Blank Start */
  104. u16 vbc; /* Vert Blank Clear */
  105. u16 vss; /* Vert Sync Start */
  106. u16 vsc; /* Vert Sync Clear */
  107. u16 xcs;
  108. u16 xcc;
  109. u16 fsa; /* Fault Status Address */
  110. u16 adr; /* Address Registers */
  111. u8 xxx1[0xce];
  112. u8 pcg[0x100]; /* Pixel Clock Generator */
  113. u32 vbr; /* Frame Base Row */
  114. u32 vmcr; /* VBC Master Control */
  115. u32 vcr; /* VBC refresh */
  116. u32 vca; /* VBC Config */
  117. };
  118. #define CG14_CCR_ENABLE 0x04
  119. #define CG14_CCR_SELECT 0x02 /* HW/Full screen */
  120. struct cg14_cursor {
  121. u32 cpl0[32]; /* Enable plane 0 */
  122. u32 cpl1[32]; /* Color selection plane */
  123. u8 ccr; /* Cursor Control Reg */
  124. u8 xxx0[3];
  125. u16 cursx; /* Cursor x,y position */
  126. u16 cursy; /* Cursor x,y position */
  127. u32 color0;
  128. u32 color1;
  129. u32 xxx1[0x1bc];
  130. u32 cpl0i[32]; /* Enable plane 0 autoinc */
  131. u32 cpl1i[32]; /* Color selection autoinc */
  132. };
  133. struct cg14_dac {
  134. u8 addr; /* Address Register */
  135. u8 xxx0[255];
  136. u8 glut; /* Gamma table */
  137. u8 xxx1[255];
  138. u8 select; /* Register Select */
  139. u8 xxx2[255];
  140. u8 mode; /* Mode Register */
  141. };
  142. struct cg14_xlut{
  143. u8 x_xlut [256];
  144. u8 x_xlutd [256];
  145. u8 xxx0[0x600];
  146. u8 x_xlut_inc [256];
  147. u8 x_xlutd_inc [256];
  148. };
  149. /* Color look up table (clut) */
  150. /* Each one of these arrays hold the color lookup table (for 256
  151. * colors) for each MDI page (I assume then there should be 4 MDI
  152. * pages, I still wonder what they are. I have seen NeXTStep split
  153. * the screen in four parts, while operating in 24 bits mode. Each
  154. * integer holds 4 values: alpha value (transparency channel, thanks
  155. * go to John Stone (johns@umr.edu) from OpenBSD), red, green and blue
  156. *
  157. * I currently use the clut instead of the Xlut
  158. */
  159. struct cg14_clut {
  160. u32 c_clut [256];
  161. u32 c_clutd [256]; /* i wonder what the 'd' is for */
  162. u32 c_clut_inc [256];
  163. u32 c_clutd_inc [256];
  164. };
  165. #define CG14_MMAP_ENTRIES 16
  166. struct cg14_par {
  167. spinlock_t lock;
  168. struct cg14_regs __iomem *regs;
  169. struct cg14_clut __iomem *clut;
  170. struct cg14_cursor __iomem *cursor;
  171. u32 flags;
  172. #define CG14_FLAG_BLANKED 0x00000001
  173. unsigned long iospace;
  174. struct sbus_mmap_map mmap_map[CG14_MMAP_ENTRIES];
  175. int mode;
  176. int ramsize;
  177. };
  178. static void __cg14_reset(struct cg14_par *par)
  179. {
  180. struct cg14_regs __iomem *regs = par->regs;
  181. u8 val;
  182. val = sbus_readb(&regs->mcr);
  183. val &= ~(CG14_MCR_PIXMODE_MASK);
  184. sbus_writeb(val, &regs->mcr);
  185. }
  186. static int cg14_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  187. {
  188. struct cg14_par *par = (struct cg14_par *) info->par;
  189. unsigned long flags;
  190. /* We just use this to catch switches out of
  191. * graphics mode.
  192. */
  193. spin_lock_irqsave(&par->lock, flags);
  194. __cg14_reset(par);
  195. spin_unlock_irqrestore(&par->lock, flags);
  196. if (var->xoffset || var->yoffset || var->vmode)
  197. return -EINVAL;
  198. return 0;
  199. }
  200. /**
  201. * cg14_setcolreg - Optional function. Sets a color register.
  202. * @regno: boolean, 0 copy local, 1 get_user() function
  203. * @red: frame buffer colormap structure
  204. * @green: The green value which can be up to 16 bits wide
  205. * @blue: The blue value which can be up to 16 bits wide.
  206. * @transp: If supported the alpha value which can be up to 16 bits wide.
  207. * @info: frame buffer info structure
  208. */
  209. static int cg14_setcolreg(unsigned regno,
  210. unsigned red, unsigned green, unsigned blue,
  211. unsigned transp, struct fb_info *info)
  212. {
  213. struct cg14_par *par = (struct cg14_par *) info->par;
  214. struct cg14_clut __iomem *clut = par->clut;
  215. unsigned long flags;
  216. u32 val;
  217. if (regno >= 256)
  218. return 1;
  219. red >>= 8;
  220. green >>= 8;
  221. blue >>= 8;
  222. val = (red | (green << 8) | (blue << 16));
  223. spin_lock_irqsave(&par->lock, flags);
  224. sbus_writel(val, &clut->c_clut[regno]);
  225. spin_unlock_irqrestore(&par->lock, flags);
  226. return 0;
  227. }
  228. static int cg14_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  229. {
  230. struct cg14_par *par = (struct cg14_par *) info->par;
  231. return sbusfb_mmap_helper(par->mmap_map,
  232. info->fix.smem_start, info->fix.smem_len,
  233. par->iospace, vma);
  234. }
  235. static int cg14_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  236. {
  237. struct cg14_par *par = (struct cg14_par *) info->par;
  238. struct cg14_regs __iomem *regs = par->regs;
  239. struct mdi_cfginfo kmdi, __user *mdii;
  240. unsigned long flags;
  241. int cur_mode, mode, ret = 0;
  242. switch (cmd) {
  243. case MDI_RESET:
  244. spin_lock_irqsave(&par->lock, flags);
  245. __cg14_reset(par);
  246. spin_unlock_irqrestore(&par->lock, flags);
  247. break;
  248. case MDI_GET_CFGINFO:
  249. memset(&kmdi, 0, sizeof(kmdi));
  250. spin_lock_irqsave(&par->lock, flags);
  251. kmdi.mdi_type = FBTYPE_MDICOLOR;
  252. kmdi.mdi_height = info->var.yres;
  253. kmdi.mdi_width = info->var.xres;
  254. kmdi.mdi_mode = par->mode;
  255. kmdi.mdi_pixfreq = 72; /* FIXME */
  256. kmdi.mdi_size = par->ramsize;
  257. spin_unlock_irqrestore(&par->lock, flags);
  258. mdii = (struct mdi_cfginfo __user *) arg;
  259. if (copy_to_user(mdii, &kmdi, sizeof(kmdi)))
  260. ret = -EFAULT;
  261. break;
  262. case MDI_SET_PIXELMODE:
  263. if (get_user(mode, (int __user *) arg)) {
  264. ret = -EFAULT;
  265. break;
  266. }
  267. spin_lock_irqsave(&par->lock, flags);
  268. cur_mode = sbus_readb(&regs->mcr);
  269. cur_mode &= ~CG14_MCR_PIXMODE_MASK;
  270. switch(mode) {
  271. case MDI_32_PIX:
  272. cur_mode |= (CG14_MCR_PIXMODE_32 <<
  273. CG14_MCR_PIXMODE_SHIFT);
  274. break;
  275. case MDI_16_PIX:
  276. cur_mode |= (CG14_MCR_PIXMODE_16 <<
  277. CG14_MCR_PIXMODE_SHIFT);
  278. break;
  279. case MDI_8_PIX:
  280. break;
  281. default:
  282. ret = -ENOSYS;
  283. break;
  284. }
  285. if (!ret) {
  286. sbus_writeb(cur_mode, &regs->mcr);
  287. par->mode = mode;
  288. }
  289. spin_unlock_irqrestore(&par->lock, flags);
  290. break;
  291. default:
  292. ret = sbusfb_ioctl_helper(cmd, arg, info,
  293. FBTYPE_MDICOLOR, 8,
  294. info->fix.smem_len);
  295. break;
  296. }
  297. return ret;
  298. }
  299. /*
  300. * Initialisation
  301. */
  302. static void cg14_init_fix(struct fb_info *info, int linebytes,
  303. struct device_node *dp)
  304. {
  305. snprintf(info->fix.id, sizeof(info->fix.id), "%pOFn", dp);
  306. info->fix.type = FB_TYPE_PACKED_PIXELS;
  307. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  308. info->fix.line_length = linebytes;
  309. info->fix.accel = FB_ACCEL_SUN_CG14;
  310. }
  311. static const struct sbus_mmap_map __cg14_mmap_map[CG14_MMAP_ENTRIES] = {
  312. {
  313. .voff = CG14_REGS,
  314. .poff = 0x80000000,
  315. .size = 0x1000
  316. },
  317. {
  318. .voff = CG14_XLUT,
  319. .poff = 0x80003000,
  320. .size = 0x1000
  321. },
  322. {
  323. .voff = CG14_CLUT1,
  324. .poff = 0x80004000,
  325. .size = 0x1000
  326. },
  327. {
  328. .voff = CG14_CLUT2,
  329. .poff = 0x80005000,
  330. .size = 0x1000
  331. },
  332. {
  333. .voff = CG14_CLUT3,
  334. .poff = 0x80006000,
  335. .size = 0x1000
  336. },
  337. {
  338. .voff = CG3_MMAP_OFFSET - 0x7000,
  339. .poff = 0x80000000,
  340. .size = 0x7000
  341. },
  342. {
  343. .voff = CG3_MMAP_OFFSET,
  344. .poff = 0x00000000,
  345. .size = SBUS_MMAP_FBSIZE(1)
  346. },
  347. {
  348. .voff = MDI_CURSOR_MAP,
  349. .poff = 0x80001000,
  350. .size = 0x1000
  351. },
  352. {
  353. .voff = MDI_CHUNKY_BGR_MAP,
  354. .poff = 0x01000000,
  355. .size = 0x400000
  356. },
  357. {
  358. .voff = MDI_PLANAR_X16_MAP,
  359. .poff = 0x02000000,
  360. .size = 0x200000
  361. },
  362. {
  363. .voff = MDI_PLANAR_C16_MAP,
  364. .poff = 0x02800000,
  365. .size = 0x200000
  366. },
  367. {
  368. .voff = MDI_PLANAR_X32_MAP,
  369. .poff = 0x03000000,
  370. .size = 0x100000
  371. },
  372. {
  373. .voff = MDI_PLANAR_B32_MAP,
  374. .poff = 0x03400000,
  375. .size = 0x100000
  376. },
  377. {
  378. .voff = MDI_PLANAR_G32_MAP,
  379. .poff = 0x03800000,
  380. .size = 0x100000
  381. },
  382. {
  383. .voff = MDI_PLANAR_R32_MAP,
  384. .poff = 0x03c00000,
  385. .size = 0x100000
  386. },
  387. { .size = 0 }
  388. };
  389. static void cg14_unmap_regs(struct platform_device *op, struct fb_info *info,
  390. struct cg14_par *par)
  391. {
  392. if (par->regs)
  393. of_iounmap(&op->resource[0],
  394. par->regs, sizeof(struct cg14_regs));
  395. if (par->clut)
  396. of_iounmap(&op->resource[0],
  397. par->clut, sizeof(struct cg14_clut));
  398. if (par->cursor)
  399. of_iounmap(&op->resource[0],
  400. par->cursor, sizeof(struct cg14_cursor));
  401. if (info->screen_base)
  402. of_iounmap(&op->resource[1],
  403. info->screen_base, info->fix.smem_len);
  404. }
  405. static int cg14_probe(struct platform_device *op)
  406. {
  407. struct device_node *dp = op->dev.of_node;
  408. struct fb_info *info;
  409. struct cg14_par *par;
  410. int is_8mb, linebytes, i, err;
  411. info = framebuffer_alloc(sizeof(struct cg14_par), &op->dev);
  412. err = -ENOMEM;
  413. if (!info)
  414. goto out_err;
  415. par = info->par;
  416. spin_lock_init(&par->lock);
  417. sbusfb_fill_var(&info->var, dp, 8);
  418. info->var.red.length = 8;
  419. info->var.green.length = 8;
  420. info->var.blue.length = 8;
  421. linebytes = of_getintprop_default(dp, "linebytes",
  422. info->var.xres);
  423. info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
  424. if (of_node_name_eq(dp->parent, "sbus") ||
  425. of_node_name_eq(dp->parent, "sbi")) {
  426. info->fix.smem_start = op->resource[0].start;
  427. par->iospace = op->resource[0].flags & IORESOURCE_BITS;
  428. } else {
  429. info->fix.smem_start = op->resource[1].start;
  430. par->iospace = op->resource[0].flags & IORESOURCE_BITS;
  431. }
  432. par->regs = of_ioremap(&op->resource[0], 0,
  433. sizeof(struct cg14_regs), "cg14 regs");
  434. par->clut = of_ioremap(&op->resource[0], CG14_CLUT1,
  435. sizeof(struct cg14_clut), "cg14 clut");
  436. par->cursor = of_ioremap(&op->resource[0], CG14_CURSORREGS,
  437. sizeof(struct cg14_cursor), "cg14 cursor");
  438. info->screen_base = of_ioremap(&op->resource[1], 0,
  439. info->fix.smem_len, "cg14 ram");
  440. if (!par->regs || !par->clut || !par->cursor || !info->screen_base)
  441. goto out_unmap_regs;
  442. is_8mb = (resource_size(&op->resource[1]) == (8 * 1024 * 1024));
  443. BUILD_BUG_ON(sizeof(par->mmap_map) != sizeof(__cg14_mmap_map));
  444. memcpy(&par->mmap_map, &__cg14_mmap_map, sizeof(par->mmap_map));
  445. for (i = 0; i < CG14_MMAP_ENTRIES; i++) {
  446. struct sbus_mmap_map *map = &par->mmap_map[i];
  447. if (!map->size)
  448. break;
  449. if (map->poff & 0x80000000)
  450. map->poff = (map->poff & 0x7fffffff) +
  451. (op->resource[0].start -
  452. op->resource[1].start);
  453. if (is_8mb &&
  454. map->size >= 0x100000 &&
  455. map->size <= 0x400000)
  456. map->size *= 2;
  457. }
  458. par->mode = MDI_8_PIX;
  459. par->ramsize = (is_8mb ? 0x800000 : 0x400000);
  460. info->flags = FBINFO_HWACCEL_YPAN;
  461. info->fbops = &cg14_ops;
  462. __cg14_reset(par);
  463. if (fb_alloc_cmap(&info->cmap, 256, 0))
  464. goto out_unmap_regs;
  465. fb_set_cmap(&info->cmap, info);
  466. cg14_init_fix(info, linebytes, dp);
  467. err = register_framebuffer(info);
  468. if (err < 0)
  469. goto out_dealloc_cmap;
  470. dev_set_drvdata(&op->dev, info);
  471. printk(KERN_INFO "%pOF: cgfourteen at %lx:%lx, %dMB\n",
  472. dp,
  473. par->iospace, info->fix.smem_start,
  474. par->ramsize >> 20);
  475. return 0;
  476. out_dealloc_cmap:
  477. fb_dealloc_cmap(&info->cmap);
  478. out_unmap_regs:
  479. cg14_unmap_regs(op, info, par);
  480. framebuffer_release(info);
  481. out_err:
  482. return err;
  483. }
  484. static void cg14_remove(struct platform_device *op)
  485. {
  486. struct fb_info *info = dev_get_drvdata(&op->dev);
  487. struct cg14_par *par = info->par;
  488. unregister_framebuffer(info);
  489. fb_dealloc_cmap(&info->cmap);
  490. cg14_unmap_regs(op, info, par);
  491. framebuffer_release(info);
  492. }
  493. static const struct of_device_id cg14_match[] = {
  494. {
  495. .name = "cgfourteen",
  496. },
  497. {},
  498. };
  499. MODULE_DEVICE_TABLE(of, cg14_match);
  500. static struct platform_driver cg14_driver = {
  501. .driver = {
  502. .name = "cg14",
  503. .of_match_table = cg14_match,
  504. },
  505. .probe = cg14_probe,
  506. .remove = cg14_remove,
  507. };
  508. static int __init cg14_init(void)
  509. {
  510. if (fb_get_options("cg14fb", NULL))
  511. return -ENODEV;
  512. return platform_driver_register(&cg14_driver);
  513. }
  514. static void __exit cg14_exit(void)
  515. {
  516. platform_driver_unregister(&cg14_driver);
  517. }
  518. module_init(cg14_init);
  519. module_exit(cg14_exit);
  520. MODULE_DESCRIPTION("framebuffer driver for CGfourteen chipsets");
  521. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  522. MODULE_VERSION("2.0");
  523. MODULE_LICENSE("GPL");