pmagb-b-fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * linux/drivers/video/pmagb-b-fb.c
  3. *
  4. * PMAGB-B TURBOchannel Smart Frame Buffer (SFB) card support,
  5. * derived from:
  6. * "HP300 Topcat framebuffer support (derived from macfb of all things)
  7. * Phil Blundell <philb@gnu.org> 1998", the original code can be
  8. * found in the file hpfb.c in the same directory.
  9. *
  10. * DECstation related code Copyright (C) 1999, 2000, 2001 by
  11. * Michael Engel <engel@unix-ag.org>,
  12. * Karsten Merker <merker@linuxtag.org> and
  13. * Harald Koerfgen.
  14. * Copyright (c) 2005, 2006 Maciej W. Rozycki
  15. *
  16. * This file is subject to the terms and conditions of the GNU General
  17. * Public License. See the file COPYING in the main directory of this
  18. * archive for more details.
  19. */
  20. #include <linux/compiler.h>
  21. #include <linux/delay.h>
  22. #include <linux/errno.h>
  23. #include <linux/fb.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/tc.h>
  28. #include <linux/types.h>
  29. #include <asm/io.h>
  30. #include <video/pmagb-b-fb.h>
  31. struct pmagbbfb_par {
  32. volatile void __iomem *mmio;
  33. volatile void __iomem *smem;
  34. volatile u32 __iomem *sfb;
  35. volatile u32 __iomem *dac;
  36. unsigned int osc0;
  37. unsigned int osc1;
  38. int slot;
  39. };
  40. static struct fb_var_screeninfo pmagbbfb_defined = {
  41. .bits_per_pixel = 8,
  42. .red.length = 8,
  43. .green.length = 8,
  44. .blue.length = 8,
  45. .activate = FB_ACTIVATE_NOW,
  46. .height = -1,
  47. .width = -1,
  48. .accel_flags = FB_ACCEL_NONE,
  49. .sync = FB_SYNC_ON_GREEN,
  50. .vmode = FB_VMODE_NONINTERLACED,
  51. };
  52. static struct fb_fix_screeninfo pmagbbfb_fix = {
  53. .id = "PMAGB-BA",
  54. .smem_len = (2048 * 1024),
  55. .type = FB_TYPE_PACKED_PIXELS,
  56. .visual = FB_VISUAL_PSEUDOCOLOR,
  57. .mmio_len = PMAGB_B_FBMEM,
  58. };
  59. static inline void sfb_write(struct pmagbbfb_par *par, unsigned int reg, u32 v)
  60. {
  61. writel(v, par->sfb + reg / 4);
  62. }
  63. static inline u32 sfb_read(struct pmagbbfb_par *par, unsigned int reg)
  64. {
  65. return readl(par->sfb + reg / 4);
  66. }
  67. static inline void dac_write(struct pmagbbfb_par *par, unsigned int reg, u8 v)
  68. {
  69. writeb(v, par->dac + reg / 4);
  70. }
  71. static inline u8 dac_read(struct pmagbbfb_par *par, unsigned int reg)
  72. {
  73. return readb(par->dac + reg / 4);
  74. }
  75. static inline void gp0_write(struct pmagbbfb_par *par, u32 v)
  76. {
  77. writel(v, par->mmio + PMAGB_B_GP0);
  78. }
  79. /*
  80. * Set the palette.
  81. */
  82. static int pmagbbfb_setcolreg(unsigned int regno, unsigned int red,
  83. unsigned int green, unsigned int blue,
  84. unsigned int transp, struct fb_info *info)
  85. {
  86. struct pmagbbfb_par *par = info->par;
  87. if (regno >= info->cmap.len)
  88. return 1;
  89. red >>= 8; /* The cmap fields are 16 bits */
  90. green >>= 8; /* wide, but the hardware colormap */
  91. blue >>= 8; /* registers are only 8 bits wide */
  92. mb();
  93. dac_write(par, BT459_ADDR_LO, regno);
  94. dac_write(par, BT459_ADDR_HI, 0x00);
  95. wmb();
  96. dac_write(par, BT459_CMAP, red);
  97. wmb();
  98. dac_write(par, BT459_CMAP, green);
  99. wmb();
  100. dac_write(par, BT459_CMAP, blue);
  101. return 0;
  102. }
  103. static struct fb_ops pmagbbfb_ops = {
  104. .owner = THIS_MODULE,
  105. .fb_setcolreg = pmagbbfb_setcolreg,
  106. .fb_fillrect = cfb_fillrect,
  107. .fb_copyarea = cfb_copyarea,
  108. .fb_imageblit = cfb_imageblit,
  109. };
  110. /*
  111. * Turn the hardware cursor off.
  112. */
  113. static void __init pmagbbfb_erase_cursor(struct fb_info *info)
  114. {
  115. struct pmagbbfb_par *par = info->par;
  116. mb();
  117. dac_write(par, BT459_ADDR_LO, 0x00);
  118. dac_write(par, BT459_ADDR_HI, 0x03);
  119. wmb();
  120. dac_write(par, BT459_DATA, 0x00);
  121. }
  122. /*
  123. * Set up screen parameters.
  124. */
  125. static void pmagbbfb_screen_setup(struct fb_info *info)
  126. {
  127. struct pmagbbfb_par *par = info->par;
  128. info->var.xres = ((sfb_read(par, SFB_REG_VID_HOR) >>
  129. SFB_VID_HOR_PIX_SHIFT) & SFB_VID_HOR_PIX_MASK) * 4;
  130. info->var.xres_virtual = info->var.xres;
  131. info->var.yres = (sfb_read(par, SFB_REG_VID_VER) >>
  132. SFB_VID_VER_SL_SHIFT) & SFB_VID_VER_SL_MASK;
  133. info->var.yres_virtual = info->var.yres;
  134. info->var.left_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
  135. SFB_VID_HOR_BP_SHIFT) &
  136. SFB_VID_HOR_BP_MASK) * 4;
  137. info->var.right_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
  138. SFB_VID_HOR_FP_SHIFT) &
  139. SFB_VID_HOR_FP_MASK) * 4;
  140. info->var.upper_margin = (sfb_read(par, SFB_REG_VID_VER) >>
  141. SFB_VID_VER_BP_SHIFT) & SFB_VID_VER_BP_MASK;
  142. info->var.lower_margin = (sfb_read(par, SFB_REG_VID_VER) >>
  143. SFB_VID_VER_FP_SHIFT) & SFB_VID_VER_FP_MASK;
  144. info->var.hsync_len = ((sfb_read(par, SFB_REG_VID_HOR) >>
  145. SFB_VID_HOR_SYN_SHIFT) &
  146. SFB_VID_HOR_SYN_MASK) * 4;
  147. info->var.vsync_len = (sfb_read(par, SFB_REG_VID_VER) >>
  148. SFB_VID_VER_SYN_SHIFT) & SFB_VID_VER_SYN_MASK;
  149. info->fix.line_length = info->var.xres;
  150. };
  151. /*
  152. * Determine oscillator configuration.
  153. */
  154. static void pmagbbfb_osc_setup(struct fb_info *info)
  155. {
  156. static unsigned int pmagbbfb_freqs[] = {
  157. 130808, 119843, 104000, 92980, 74370, 72800,
  158. 69197, 66000, 65000, 50350, 36000, 32000, 25175
  159. };
  160. struct pmagbbfb_par *par = info->par;
  161. struct tc_bus *tbus = to_tc_dev(info->device)->bus;
  162. u32 count0 = 8, count1 = 8, counttc = 16 * 256 + 8;
  163. u32 freq0, freq1, freqtc = tc_get_speed(tbus) / 250;
  164. int i, j;
  165. gp0_write(par, 0); /* select Osc0 */
  166. for (j = 0; j < 16; j++) {
  167. mb();
  168. sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
  169. mb();
  170. for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
  171. if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
  172. break;
  173. udelay(1);
  174. }
  175. count0 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
  176. }
  177. gp0_write(par, 1); /* select Osc1 */
  178. for (j = 0; j < 16; j++) {
  179. mb();
  180. sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
  181. for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
  182. if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
  183. break;
  184. udelay(1);
  185. }
  186. count1 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
  187. }
  188. freq0 = (freqtc * count0 + counttc / 2) / counttc;
  189. par->osc0 = freq0;
  190. if (freq0 >= pmagbbfb_freqs[0] - (pmagbbfb_freqs[0] + 32) / 64 &&
  191. freq0 <= pmagbbfb_freqs[0] + (pmagbbfb_freqs[0] + 32) / 64)
  192. par->osc0 = pmagbbfb_freqs[0];
  193. freq1 = (par->osc0 * count1 + count0 / 2) / count0;
  194. par->osc1 = freq1;
  195. for (i = 0; i < ARRAY_SIZE(pmagbbfb_freqs); i++)
  196. if (freq1 >= pmagbbfb_freqs[i] -
  197. (pmagbbfb_freqs[i] + 128) / 256 &&
  198. freq1 <= pmagbbfb_freqs[i] +
  199. (pmagbbfb_freqs[i] + 128) / 256) {
  200. par->osc1 = pmagbbfb_freqs[i];
  201. break;
  202. }
  203. if (par->osc0 - par->osc1 <= (par->osc0 + par->osc1 + 256) / 512 ||
  204. par->osc1 - par->osc0 <= (par->osc0 + par->osc1 + 256) / 512)
  205. par->osc1 = 0;
  206. gp0_write(par, par->osc1 != 0); /* reselect OscX */
  207. info->var.pixclock = par->osc1 ?
  208. (1000000000 + par->osc1 / 2) / par->osc1 :
  209. (1000000000 + par->osc0 / 2) / par->osc0;
  210. };
  211. static int pmagbbfb_probe(struct device *dev)
  212. {
  213. struct tc_dev *tdev = to_tc_dev(dev);
  214. resource_size_t start, len;
  215. struct fb_info *info;
  216. struct pmagbbfb_par *par;
  217. char freq0[12], freq1[12];
  218. u32 vid_base;
  219. int err;
  220. info = framebuffer_alloc(sizeof(struct pmagbbfb_par), dev);
  221. if (!info) {
  222. printk(KERN_ERR "%s: Cannot allocate memory\n", dev_name(dev));
  223. return -ENOMEM;
  224. }
  225. par = info->par;
  226. dev_set_drvdata(dev, info);
  227. if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
  228. printk(KERN_ERR "%s: Cannot allocate color map\n",
  229. dev_name(dev));
  230. err = -ENOMEM;
  231. goto err_alloc;
  232. }
  233. info->fbops = &pmagbbfb_ops;
  234. info->fix = pmagbbfb_fix;
  235. info->var = pmagbbfb_defined;
  236. info->flags = FBINFO_DEFAULT;
  237. /* Request the I/O MEM resource. */
  238. start = tdev->resource.start;
  239. len = tdev->resource.end - start + 1;
  240. if (!request_mem_region(start, len, dev_name(dev))) {
  241. printk(KERN_ERR "%s: Cannot reserve FB region\n",
  242. dev_name(dev));
  243. err = -EBUSY;
  244. goto err_cmap;
  245. }
  246. /* MMIO mapping setup. */
  247. info->fix.mmio_start = start;
  248. par->mmio = ioremap_nocache(info->fix.mmio_start, info->fix.mmio_len);
  249. if (!par->mmio) {
  250. printk(KERN_ERR "%s: Cannot map MMIO\n", dev_name(dev));
  251. err = -ENOMEM;
  252. goto err_resource;
  253. }
  254. par->sfb = par->mmio + PMAGB_B_SFB;
  255. par->dac = par->mmio + PMAGB_B_BT459;
  256. /* Frame buffer mapping setup. */
  257. info->fix.smem_start = start + PMAGB_B_FBMEM;
  258. par->smem = ioremap_nocache(info->fix.smem_start, info->fix.smem_len);
  259. if (!par->smem) {
  260. printk(KERN_ERR "%s: Cannot map FB\n", dev_name(dev));
  261. err = -ENOMEM;
  262. goto err_mmio_map;
  263. }
  264. vid_base = sfb_read(par, SFB_REG_VID_BASE);
  265. info->screen_base = (void __iomem *)par->smem + vid_base * 0x1000;
  266. info->screen_size = info->fix.smem_len - 2 * vid_base * 0x1000;
  267. pmagbbfb_erase_cursor(info);
  268. pmagbbfb_screen_setup(info);
  269. pmagbbfb_osc_setup(info);
  270. err = register_framebuffer(info);
  271. if (err < 0) {
  272. printk(KERN_ERR "%s: Cannot register framebuffer\n",
  273. dev_name(dev));
  274. goto err_smem_map;
  275. }
  276. get_device(dev);
  277. snprintf(freq0, sizeof(freq0), "%u.%03uMHz",
  278. par->osc0 / 1000, par->osc0 % 1000);
  279. snprintf(freq1, sizeof(freq1), "%u.%03uMHz",
  280. par->osc1 / 1000, par->osc1 % 1000);
  281. fb_info(info, "%s frame buffer device at %s\n",
  282. info->fix.id, dev_name(dev));
  283. fb_info(info, "Osc0: %s, Osc1: %s, Osc%u selected\n",
  284. freq0, par->osc1 ? freq1 : "disabled", par->osc1 != 0);
  285. return 0;
  286. err_smem_map:
  287. iounmap(par->smem);
  288. err_mmio_map:
  289. iounmap(par->mmio);
  290. err_resource:
  291. release_mem_region(start, len);
  292. err_cmap:
  293. fb_dealloc_cmap(&info->cmap);
  294. err_alloc:
  295. framebuffer_release(info);
  296. return err;
  297. }
  298. static int __exit pmagbbfb_remove(struct device *dev)
  299. {
  300. struct tc_dev *tdev = to_tc_dev(dev);
  301. struct fb_info *info = dev_get_drvdata(dev);
  302. struct pmagbbfb_par *par = info->par;
  303. resource_size_t start, len;
  304. put_device(dev);
  305. unregister_framebuffer(info);
  306. iounmap(par->smem);
  307. iounmap(par->mmio);
  308. start = tdev->resource.start;
  309. len = tdev->resource.end - start + 1;
  310. release_mem_region(start, len);
  311. fb_dealloc_cmap(&info->cmap);
  312. framebuffer_release(info);
  313. return 0;
  314. }
  315. /*
  316. * Initialize the framebuffer.
  317. */
  318. static const struct tc_device_id pmagbbfb_tc_table[] = {
  319. { "DEC ", "PMAGB-BA" },
  320. { }
  321. };
  322. MODULE_DEVICE_TABLE(tc, pmagbbfb_tc_table);
  323. static struct tc_driver pmagbbfb_driver = {
  324. .id_table = pmagbbfb_tc_table,
  325. .driver = {
  326. .name = "pmagbbfb",
  327. .bus = &tc_bus_type,
  328. .probe = pmagbbfb_probe,
  329. .remove = __exit_p(pmagbbfb_remove),
  330. },
  331. };
  332. static int __init pmagbbfb_init(void)
  333. {
  334. #ifndef MODULE
  335. if (fb_get_options("pmagbbfb", NULL))
  336. return -ENXIO;
  337. #endif
  338. return tc_register_driver(&pmagbbfb_driver);
  339. }
  340. static void __exit pmagbbfb_exit(void)
  341. {
  342. tc_unregister_driver(&pmagbbfb_driver);
  343. }
  344. module_init(pmagbbfb_init);
  345. module_exit(pmagbbfb_exit);
  346. MODULE_LICENSE("GPL");