bw2.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* bw2.c: BWTWO 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 bw2_blank(int, struct fb_info *);
  28. static int bw2_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma);
  29. static int bw2_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg);
  30. /*
  31. * Frame buffer operations
  32. */
  33. static const struct fb_ops bw2_ops = {
  34. .owner = THIS_MODULE,
  35. FB_DEFAULT_SBUS_OPS(bw2),
  36. .fb_blank = bw2_blank,
  37. };
  38. /* OBio addresses for the bwtwo registers */
  39. #define BWTWO_REGISTER_OFFSET 0x400000
  40. struct bt_regs {
  41. u32 addr;
  42. u32 color_map;
  43. u32 control;
  44. u32 cursor;
  45. };
  46. struct bw2_regs {
  47. struct bt_regs cmap;
  48. u8 control;
  49. u8 status;
  50. u8 cursor_start;
  51. u8 cursor_end;
  52. u8 h_blank_start;
  53. u8 h_blank_end;
  54. u8 h_sync_start;
  55. u8 h_sync_end;
  56. u8 comp_sync_end;
  57. u8 v_blank_start_high;
  58. u8 v_blank_start_low;
  59. u8 v_blank_end;
  60. u8 v_sync_start;
  61. u8 v_sync_end;
  62. u8 xfer_holdoff_start;
  63. u8 xfer_holdoff_end;
  64. };
  65. /* Status Register Constants */
  66. #define BWTWO_SR_RES_MASK 0x70
  67. #define BWTWO_SR_1600_1280 0x50
  68. #define BWTWO_SR_1152_900_76_A 0x40
  69. #define BWTWO_SR_1152_900_76_B 0x60
  70. #define BWTWO_SR_ID_MASK 0x0f
  71. #define BWTWO_SR_ID_MONO 0x02
  72. #define BWTWO_SR_ID_MONO_ECL 0x03
  73. #define BWTWO_SR_ID_MSYNC 0x04
  74. #define BWTWO_SR_ID_NOCONN 0x0a
  75. /* Control Register Constants */
  76. #define BWTWO_CTL_ENABLE_INTS 0x80
  77. #define BWTWO_CTL_ENABLE_VIDEO 0x40
  78. #define BWTWO_CTL_ENABLE_TIMING 0x20
  79. #define BWTWO_CTL_ENABLE_CURCMP 0x10
  80. #define BWTWO_CTL_XTAL_MASK 0x0C
  81. #define BWTWO_CTL_DIVISOR_MASK 0x03
  82. /* Status Register Constants */
  83. #define BWTWO_STAT_PENDING_INT 0x80
  84. #define BWTWO_STAT_MSENSE_MASK 0x70
  85. #define BWTWO_STAT_ID_MASK 0x0f
  86. struct bw2_par {
  87. spinlock_t lock;
  88. struct bw2_regs __iomem *regs;
  89. u32 flags;
  90. #define BW2_FLAG_BLANKED 0x00000001
  91. unsigned long which_io;
  92. };
  93. /**
  94. * bw2_blank - Optional function. Blanks the display.
  95. * @blank: the blank mode we want.
  96. * @info: frame buffer structure that represents a single frame buffer
  97. */
  98. static int
  99. bw2_blank(int blank, struct fb_info *info)
  100. {
  101. struct bw2_par *par = (struct bw2_par *) info->par;
  102. struct bw2_regs __iomem *regs = par->regs;
  103. unsigned long flags;
  104. u8 val;
  105. spin_lock_irqsave(&par->lock, flags);
  106. switch (blank) {
  107. case FB_BLANK_UNBLANK: /* Unblanking */
  108. val = sbus_readb(&regs->control);
  109. val |= BWTWO_CTL_ENABLE_VIDEO;
  110. sbus_writeb(val, &regs->control);
  111. par->flags &= ~BW2_FLAG_BLANKED;
  112. break;
  113. case FB_BLANK_NORMAL: /* Normal blanking */
  114. case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
  115. case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
  116. case FB_BLANK_POWERDOWN: /* Poweroff */
  117. val = sbus_readb(&regs->control);
  118. val &= ~BWTWO_CTL_ENABLE_VIDEO;
  119. sbus_writeb(val, &regs->control);
  120. par->flags |= BW2_FLAG_BLANKED;
  121. break;
  122. }
  123. spin_unlock_irqrestore(&par->lock, flags);
  124. return 0;
  125. }
  126. static const struct sbus_mmap_map bw2_mmap_map[] = {
  127. {
  128. .size = SBUS_MMAP_FBSIZE(1)
  129. },
  130. { .size = 0 }
  131. };
  132. static int bw2_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  133. {
  134. struct bw2_par *par = (struct bw2_par *)info->par;
  135. return sbusfb_mmap_helper(bw2_mmap_map,
  136. info->fix.smem_start, info->fix.smem_len,
  137. par->which_io,
  138. vma);
  139. }
  140. static int bw2_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  141. {
  142. return sbusfb_ioctl_helper(cmd, arg, info,
  143. FBTYPE_SUN2BW, 1, info->fix.smem_len);
  144. }
  145. /*
  146. * Initialisation
  147. */
  148. static void bw2_init_fix(struct fb_info *info, int linebytes)
  149. {
  150. strscpy(info->fix.id, "bwtwo", sizeof(info->fix.id));
  151. info->fix.type = FB_TYPE_PACKED_PIXELS;
  152. info->fix.visual = FB_VISUAL_MONO01;
  153. info->fix.line_length = linebytes;
  154. info->fix.accel = FB_ACCEL_SUN_BWTWO;
  155. }
  156. static u8 bw2regs_1600[] = {
  157. 0x14, 0x8b, 0x15, 0x28, 0x16, 0x03, 0x17, 0x13,
  158. 0x18, 0x7b, 0x19, 0x05, 0x1a, 0x34, 0x1b, 0x2e,
  159. 0x1c, 0x00, 0x1d, 0x0a, 0x1e, 0xff, 0x1f, 0x01,
  160. 0x10, 0x21, 0
  161. };
  162. static u8 bw2regs_ecl[] = {
  163. 0x14, 0x65, 0x15, 0x1e, 0x16, 0x04, 0x17, 0x0c,
  164. 0x18, 0x5e, 0x19, 0x03, 0x1a, 0xa7, 0x1b, 0x23,
  165. 0x1c, 0x00, 0x1d, 0x08, 0x1e, 0xff, 0x1f, 0x01,
  166. 0x10, 0x20, 0
  167. };
  168. static u8 bw2regs_analog[] = {
  169. 0x14, 0xbb, 0x15, 0x2b, 0x16, 0x03, 0x17, 0x13,
  170. 0x18, 0xb0, 0x19, 0x03, 0x1a, 0xa6, 0x1b, 0x22,
  171. 0x1c, 0x01, 0x1d, 0x05, 0x1e, 0xff, 0x1f, 0x01,
  172. 0x10, 0x20, 0
  173. };
  174. static u8 bw2regs_76hz[] = {
  175. 0x14, 0xb7, 0x15, 0x27, 0x16, 0x03, 0x17, 0x0f,
  176. 0x18, 0xae, 0x19, 0x03, 0x1a, 0xae, 0x1b, 0x2a,
  177. 0x1c, 0x01, 0x1d, 0x09, 0x1e, 0xff, 0x1f, 0x01,
  178. 0x10, 0x24, 0
  179. };
  180. static u8 bw2regs_66hz[] = {
  181. 0x14, 0xbb, 0x15, 0x2b, 0x16, 0x04, 0x17, 0x14,
  182. 0x18, 0xae, 0x19, 0x03, 0x1a, 0xa8, 0x1b, 0x24,
  183. 0x1c, 0x01, 0x1d, 0x05, 0x1e, 0xff, 0x1f, 0x01,
  184. 0x10, 0x20, 0
  185. };
  186. static int bw2_do_default_mode(struct bw2_par *par, struct fb_info *info,
  187. int *linebytes)
  188. {
  189. u8 status, mon;
  190. u8 *p;
  191. status = sbus_readb(&par->regs->status);
  192. mon = status & BWTWO_SR_RES_MASK;
  193. switch (status & BWTWO_SR_ID_MASK) {
  194. case BWTWO_SR_ID_MONO_ECL:
  195. if (mon == BWTWO_SR_1600_1280) {
  196. p = bw2regs_1600;
  197. info->var.xres = info->var.xres_virtual = 1600;
  198. info->var.yres = info->var.yres_virtual = 1280;
  199. *linebytes = 1600 / 8;
  200. } else
  201. p = bw2regs_ecl;
  202. break;
  203. case BWTWO_SR_ID_MONO:
  204. p = bw2regs_analog;
  205. break;
  206. case BWTWO_SR_ID_MSYNC:
  207. if (mon == BWTWO_SR_1152_900_76_A ||
  208. mon == BWTWO_SR_1152_900_76_B)
  209. p = bw2regs_76hz;
  210. else
  211. p = bw2regs_66hz;
  212. break;
  213. case BWTWO_SR_ID_NOCONN:
  214. return 0;
  215. default:
  216. printk(KERN_ERR "bw2: can't handle SR %02x\n",
  217. status);
  218. return -EINVAL;
  219. }
  220. for ( ; *p; p += 2) {
  221. u8 __iomem *regp = &((u8 __iomem *)par->regs)[p[0]];
  222. sbus_writeb(p[1], regp);
  223. }
  224. return 0;
  225. }
  226. static int bw2_probe(struct platform_device *op)
  227. {
  228. struct device_node *dp = op->dev.of_node;
  229. struct fb_info *info;
  230. struct bw2_par *par;
  231. int linebytes, err;
  232. info = framebuffer_alloc(sizeof(struct bw2_par), &op->dev);
  233. err = -ENOMEM;
  234. if (!info)
  235. goto out_err;
  236. par = info->par;
  237. spin_lock_init(&par->lock);
  238. info->fix.smem_start = op->resource[0].start;
  239. par->which_io = op->resource[0].flags & IORESOURCE_BITS;
  240. sbusfb_fill_var(&info->var, dp, 1);
  241. linebytes = of_getintprop_default(dp, "linebytes",
  242. info->var.xres);
  243. info->var.red.length = info->var.green.length =
  244. info->var.blue.length = info->var.bits_per_pixel;
  245. info->var.red.offset = info->var.green.offset =
  246. info->var.blue.offset = 0;
  247. par->regs = of_ioremap(&op->resource[0], BWTWO_REGISTER_OFFSET,
  248. sizeof(struct bw2_regs), "bw2 regs");
  249. if (!par->regs)
  250. goto out_release_fb;
  251. if (!of_property_present(dp, "width")) {
  252. err = bw2_do_default_mode(par, info, &linebytes);
  253. if (err)
  254. goto out_unmap_regs;
  255. }
  256. info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
  257. info->fbops = &bw2_ops;
  258. info->screen_base = of_ioremap(&op->resource[0], 0,
  259. info->fix.smem_len, "bw2 ram");
  260. if (!info->screen_base) {
  261. err = -ENOMEM;
  262. goto out_unmap_regs;
  263. }
  264. bw2_blank(FB_BLANK_UNBLANK, info);
  265. bw2_init_fix(info, linebytes);
  266. err = register_framebuffer(info);
  267. if (err < 0)
  268. goto out_unmap_screen;
  269. dev_set_drvdata(&op->dev, info);
  270. printk(KERN_INFO "%pOF: bwtwo at %lx:%lx\n",
  271. dp, par->which_io, info->fix.smem_start);
  272. return 0;
  273. out_unmap_screen:
  274. of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
  275. out_unmap_regs:
  276. of_iounmap(&op->resource[0], par->regs, sizeof(struct bw2_regs));
  277. out_release_fb:
  278. framebuffer_release(info);
  279. out_err:
  280. return err;
  281. }
  282. static void bw2_remove(struct platform_device *op)
  283. {
  284. struct fb_info *info = dev_get_drvdata(&op->dev);
  285. struct bw2_par *par = info->par;
  286. unregister_framebuffer(info);
  287. of_iounmap(&op->resource[0], par->regs, sizeof(struct bw2_regs));
  288. of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
  289. framebuffer_release(info);
  290. }
  291. static const struct of_device_id bw2_match[] = {
  292. {
  293. .name = "bwtwo",
  294. },
  295. {},
  296. };
  297. MODULE_DEVICE_TABLE(of, bw2_match);
  298. static struct platform_driver bw2_driver = {
  299. .driver = {
  300. .name = "bw2",
  301. .of_match_table = bw2_match,
  302. },
  303. .probe = bw2_probe,
  304. .remove = bw2_remove,
  305. };
  306. static int __init bw2_init(void)
  307. {
  308. if (fb_get_options("bw2fb", NULL))
  309. return -ENODEV;
  310. return platform_driver_register(&bw2_driver);
  311. }
  312. static void __exit bw2_exit(void)
  313. {
  314. platform_driver_unregister(&bw2_driver);
  315. }
  316. module_init(bw2_init);
  317. module_exit(bw2_exit);
  318. MODULE_DESCRIPTION("framebuffer driver for BWTWO chipsets");
  319. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  320. MODULE_VERSION("2.0");
  321. MODULE_LICENSE("GPL");