pmagb-b-fb.c 10 KB

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