hpfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * HP300 Topcat framebuffer support (derived from macfb of all things)
  3. * Phil Blundell <philb@gnu.org> 1998
  4. * DIO-II, colour map and Catseye support by
  5. * Kars de Jong <jongk@linux-m68k.org>, May 2004.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/mm.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/fb.h>
  16. #include <linux/dio.h>
  17. #include <asm/io.h>
  18. #include <asm/uaccess.h>
  19. static struct fb_info fb_info = {
  20. .fix = {
  21. .id = "HP300 ",
  22. .type = FB_TYPE_PACKED_PIXELS,
  23. .visual = FB_VISUAL_PSEUDOCOLOR,
  24. .accel = FB_ACCEL_NONE,
  25. }
  26. };
  27. static unsigned long fb_regs;
  28. static unsigned char fb_bitmask;
  29. #define TC_NBLANK 0x4080
  30. #define TC_WEN 0x4088
  31. #define TC_REN 0x408c
  32. #define TC_FBEN 0x4090
  33. #define TC_PRR 0x40ea
  34. /* These defines match the X window system */
  35. #define RR_CLEAR 0x0
  36. #define RR_COPY 0x3
  37. #define RR_NOOP 0x5
  38. #define RR_XOR 0x6
  39. #define RR_INVERT 0xa
  40. #define RR_COPYINVERTED 0xc
  41. #define RR_SET 0xf
  42. /* blitter regs */
  43. #define BUSY 0x4044
  44. #define WMRR 0x40ef
  45. #define SOURCE_X 0x40f2
  46. #define SOURCE_Y 0x40f6
  47. #define DEST_X 0x40fa
  48. #define DEST_Y 0x40fe
  49. #define WHEIGHT 0x4106
  50. #define WWIDTH 0x4102
  51. #define WMOVE 0x409c
  52. static struct fb_var_screeninfo hpfb_defined = {
  53. .red = {
  54. .length = 8,
  55. },
  56. .green = {
  57. .length = 8,
  58. },
  59. .blue = {
  60. .length = 8,
  61. },
  62. .activate = FB_ACTIVATE_NOW,
  63. .height = -1,
  64. .width = -1,
  65. .vmode = FB_VMODE_NONINTERLACED,
  66. };
  67. static int hpfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  68. unsigned blue, unsigned transp,
  69. struct fb_info *info)
  70. {
  71. /* use MSBs */
  72. unsigned char _red =red>>8;
  73. unsigned char _green=green>>8;
  74. unsigned char _blue =blue>>8;
  75. unsigned char _regno=regno;
  76. /*
  77. * Set a single color register. The values supplied are
  78. * already rounded down to the hardware's capabilities
  79. * (according to the entries in the `var' structure). Return
  80. * != 0 for invalid regno.
  81. */
  82. if (regno >= info->cmap.len)
  83. return 1;
  84. while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
  85. out_be16(fb_regs + 0x60ba, 0xff);
  86. out_be16(fb_regs + 0x60b2, _red);
  87. out_be16(fb_regs + 0x60b4, _green);
  88. out_be16(fb_regs + 0x60b6, _blue);
  89. out_be16(fb_regs + 0x60b8, ~_regno);
  90. out_be16(fb_regs + 0x60f0, 0xff);
  91. udelay(100);
  92. while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
  93. out_be16(fb_regs + 0x60b2, 0);
  94. out_be16(fb_regs + 0x60b4, 0);
  95. out_be16(fb_regs + 0x60b6, 0);
  96. out_be16(fb_regs + 0x60b8, 0);
  97. return 0;
  98. }
  99. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  100. static int hpfb_blank(int blank, struct fb_info *info)
  101. {
  102. out_8(fb_regs + TC_NBLANK, (blank ? 0x00 : fb_bitmask));
  103. return 0;
  104. }
  105. static void topcat_blit(int x0, int y0, int x1, int y1, int w, int h, int rr)
  106. {
  107. if (rr >= 0) {
  108. while (in_8(fb_regs + BUSY) & fb_bitmask)
  109. ;
  110. }
  111. out_8(fb_regs + TC_FBEN, fb_bitmask);
  112. if (rr >= 0) {
  113. out_8(fb_regs + TC_WEN, fb_bitmask);
  114. out_8(fb_regs + WMRR, rr);
  115. }
  116. out_be16(fb_regs + SOURCE_X, x0);
  117. out_be16(fb_regs + SOURCE_Y, y0);
  118. out_be16(fb_regs + DEST_X, x1);
  119. out_be16(fb_regs + DEST_Y, y1);
  120. out_be16(fb_regs + WWIDTH, w);
  121. out_be16(fb_regs + WHEIGHT, h);
  122. out_8(fb_regs + WMOVE, fb_bitmask);
  123. }
  124. static void hpfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  125. {
  126. topcat_blit(area->sx, area->sy, area->dx, area->dy, area->width, area->height, RR_COPY);
  127. }
  128. static void hpfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
  129. {
  130. u8 clr;
  131. clr = region->color & 0xff;
  132. while (in_8(fb_regs + BUSY) & fb_bitmask)
  133. ;
  134. /* Foreground */
  135. out_8(fb_regs + TC_WEN, fb_bitmask & clr);
  136. out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_SET : RR_INVERT));
  137. /* Background */
  138. out_8(fb_regs + TC_WEN, fb_bitmask & ~clr);
  139. out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_CLEAR : RR_NOOP));
  140. topcat_blit(region->dx, region->dy, region->dx, region->dy, region->width, region->height, -1);
  141. }
  142. static int hpfb_sync(struct fb_info *info)
  143. {
  144. /*
  145. * Since we also access the framebuffer directly, we have to wait
  146. * until the block mover is finished
  147. */
  148. while (in_8(fb_regs + BUSY) & fb_bitmask)
  149. ;
  150. out_8(fb_regs + TC_WEN, fb_bitmask);
  151. out_8(fb_regs + TC_PRR, RR_COPY);
  152. out_8(fb_regs + TC_FBEN, fb_bitmask);
  153. return 0;
  154. }
  155. static struct fb_ops hpfb_ops = {
  156. .owner = THIS_MODULE,
  157. .fb_setcolreg = hpfb_setcolreg,
  158. .fb_blank = hpfb_blank,
  159. .fb_fillrect = hpfb_fillrect,
  160. .fb_copyarea = hpfb_copyarea,
  161. .fb_imageblit = cfb_imageblit,
  162. .fb_sync = hpfb_sync,
  163. };
  164. /* Common to all HP framebuffers */
  165. #define HPFB_FBWMSB 0x05 /* Frame buffer width */
  166. #define HPFB_FBWLSB 0x07
  167. #define HPFB_FBHMSB 0x09 /* Frame buffer height */
  168. #define HPFB_FBHLSB 0x0b
  169. #define HPFB_DWMSB 0x0d /* Display width */
  170. #define HPFB_DWLSB 0x0f
  171. #define HPFB_DHMSB 0x11 /* Display height */
  172. #define HPFB_DHLSB 0x13
  173. #define HPFB_NUMPLANES 0x5b /* Number of colour planes */
  174. #define HPFB_FBOMSB 0x5d /* Frame buffer offset */
  175. #define HPFB_FBOLSB 0x5f
  176. static int __init hpfb_init_one(unsigned long phys_base, unsigned long virt_base)
  177. {
  178. unsigned long fboff, fb_width, fb_height, fb_start;
  179. fb_regs = virt_base;
  180. fboff = (in_8(fb_regs + HPFB_FBOMSB) << 8) | in_8(fb_regs + HPFB_FBOLSB);
  181. fb_info.fix.smem_start = (in_8(fb_regs + fboff) << 16);
  182. if (phys_base >= DIOII_BASE) {
  183. fb_info.fix.smem_start += phys_base;
  184. }
  185. if (DIO_SECID(fb_regs) != DIO_ID2_TOPCAT) {
  186. /* This is the magic incantation the HP X server uses to make Catseye boards work. */
  187. while (in_be16(fb_regs+0x4800) & 1)
  188. ;
  189. out_be16(fb_regs+0x4800, 0); /* Catseye status */
  190. out_be16(fb_regs+0x4510, 0); /* VB */
  191. out_be16(fb_regs+0x4512, 0); /* TCNTRL */
  192. out_be16(fb_regs+0x4514, 0); /* ACNTRL */
  193. out_be16(fb_regs+0x4516, 0); /* PNCNTRL */
  194. out_be16(fb_regs+0x4206, 0x90); /* RUG Command/Status */
  195. out_be16(fb_regs+0x60a2, 0); /* Overlay Mask */
  196. out_be16(fb_regs+0x60bc, 0); /* Ram Select */
  197. }
  198. /*
  199. * Fill in the available video resolution
  200. */
  201. fb_width = (in_8(fb_regs + HPFB_FBWMSB) << 8) | in_8(fb_regs + HPFB_FBWLSB);
  202. fb_info.fix.line_length = fb_width;
  203. fb_height = (in_8(fb_regs + HPFB_FBHMSB) << 8) | in_8(fb_regs + HPFB_FBHLSB);
  204. fb_info.fix.smem_len = fb_width * fb_height;
  205. fb_start = (unsigned long)ioremap_writethrough(fb_info.fix.smem_start,
  206. fb_info.fix.smem_len);
  207. hpfb_defined.xres = (in_8(fb_regs + HPFB_DWMSB) << 8) | in_8(fb_regs + HPFB_DWLSB);
  208. hpfb_defined.yres = (in_8(fb_regs + HPFB_DHMSB) << 8) | in_8(fb_regs + HPFB_DHLSB);
  209. hpfb_defined.xres_virtual = hpfb_defined.xres;
  210. hpfb_defined.yres_virtual = hpfb_defined.yres;
  211. hpfb_defined.bits_per_pixel = in_8(fb_regs + HPFB_NUMPLANES);
  212. printk(KERN_INFO "hpfb: framebuffer at 0x%lx, mapped to 0x%lx, size %dk\n",
  213. fb_info.fix.smem_start, fb_start, fb_info.fix.smem_len/1024);
  214. printk(KERN_INFO "hpfb: mode is %dx%dx%d, linelength=%d\n",
  215. hpfb_defined.xres, hpfb_defined.yres, hpfb_defined.bits_per_pixel, fb_info.fix.line_length);
  216. /*
  217. * Give the hardware a bit of a prod and work out how many bits per
  218. * pixel are supported.
  219. */
  220. out_8(fb_regs + TC_WEN, 0xff);
  221. out_8(fb_regs + TC_PRR, RR_COPY);
  222. out_8(fb_regs + TC_FBEN, 0xff);
  223. out_8(fb_start, 0xff);
  224. fb_bitmask = in_8(fb_start);
  225. out_8(fb_start, 0);
  226. /*
  227. * Enable reading/writing of all the planes.
  228. */
  229. out_8(fb_regs + TC_WEN, fb_bitmask);
  230. out_8(fb_regs + TC_PRR, RR_COPY);
  231. out_8(fb_regs + TC_REN, fb_bitmask);
  232. out_8(fb_regs + TC_FBEN, fb_bitmask);
  233. /*
  234. * Clear the screen.
  235. */
  236. topcat_blit(0, 0, 0, 0, fb_width, fb_height, RR_CLEAR);
  237. /*
  238. * Let there be consoles..
  239. */
  240. if (DIO_SECID(fb_regs) == DIO_ID2_TOPCAT)
  241. strcat(fb_info.fix.id, "Topcat");
  242. else
  243. strcat(fb_info.fix.id, "Catseye");
  244. fb_info.fbops = &hpfb_ops;
  245. fb_info.flags = FBINFO_DEFAULT;
  246. fb_info.var = hpfb_defined;
  247. fb_info.screen_base = (char *)fb_start;
  248. fb_alloc_cmap(&fb_info.cmap, 1 << hpfb_defined.bits_per_pixel, 0);
  249. if (register_framebuffer(&fb_info) < 0) {
  250. fb_dealloc_cmap(&fb_info.cmap);
  251. iounmap(fb_info.screen_base);
  252. fb_info.screen_base = NULL;
  253. return 1;
  254. }
  255. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  256. fb_info.node, fb_info.fix.id);
  257. return 0;
  258. }
  259. /*
  260. * Check that the secondary ID indicates that we have some hope of working with this
  261. * framebuffer. The catseye boards are pretty much like topcats and we can muddle through.
  262. */
  263. #define topcat_sid_ok(x) (((x) == DIO_ID2_LRCATSEYE) || ((x) == DIO_ID2_HRCCATSEYE) \
  264. || ((x) == DIO_ID2_HRMCATSEYE) || ((x) == DIO_ID2_TOPCAT))
  265. /*
  266. * Initialise the framebuffer
  267. */
  268. static int __devinit hpfb_dio_probe(struct dio_dev * d, const struct dio_device_id * ent)
  269. {
  270. unsigned long paddr, vaddr;
  271. paddr = d->resource.start;
  272. if (!request_mem_region(d->resource.start, d->resource.end - d->resource.start, d->name))
  273. return -EBUSY;
  274. if (d->scode >= DIOII_SCBASE) {
  275. vaddr = (unsigned long)ioremap(paddr, d->resource.end - d->resource.start);
  276. } else {
  277. vaddr = paddr + DIO_VIRADDRBASE;
  278. }
  279. printk(KERN_INFO "Topcat found at DIO select code %d "
  280. "(secondary id %02x)\n", d->scode, (d->id >> 8) & 0xff);
  281. if (hpfb_init_one(paddr, vaddr)) {
  282. if (d->scode >= DIOII_SCBASE)
  283. iounmap((void *)vaddr);
  284. return -ENOMEM;
  285. }
  286. return 0;
  287. }
  288. static void __devexit hpfb_remove_one(struct dio_dev *d)
  289. {
  290. unregister_framebuffer(&fb_info);
  291. if (d->scode >= DIOII_SCBASE)
  292. iounmap((void *)fb_regs);
  293. release_mem_region(d->resource.start, d->resource.end - d->resource.start);
  294. }
  295. static struct dio_device_id hpfb_dio_tbl[] = {
  296. { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_LRCATSEYE) },
  297. { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRCCATSEYE) },
  298. { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRMCATSEYE) },
  299. { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_TOPCAT) },
  300. { 0 }
  301. };
  302. static struct dio_driver hpfb_driver = {
  303. .name = "hpfb",
  304. .id_table = hpfb_dio_tbl,
  305. .probe = hpfb_dio_probe,
  306. .remove = __devexit_p(hpfb_remove_one),
  307. };
  308. int __init hpfb_init(void)
  309. {
  310. unsigned int sid;
  311. mm_segment_t fs;
  312. unsigned char i;
  313. int err;
  314. /* Topcats can be on the internal IO bus or real DIO devices.
  315. * The internal variant sits at 0x560000; it has primary
  316. * and secondary ID registers just like the DIO version.
  317. * So we merge the two detection routines.
  318. *
  319. * Perhaps this #define should be in a global header file:
  320. * I believe it's common to all internal fbs, not just topcat.
  321. */
  322. #define INTFBVADDR 0xf0560000
  323. #define INTFBPADDR 0x560000
  324. if (!MACH_IS_HP300)
  325. return -ENXIO;
  326. if (fb_get_options("hpfb", NULL))
  327. return -ENODEV;
  328. err = dio_register_driver(&hpfb_driver);
  329. if (err)
  330. return err;
  331. fs = get_fs();
  332. set_fs(KERNEL_DS);
  333. err = get_user(i, (unsigned char *)INTFBVADDR + DIO_IDOFF);
  334. set_fs(fs);
  335. if (!err && (i == DIO_ID_FBUFFER) && topcat_sid_ok(sid = DIO_SECID(INTFBVADDR))) {
  336. if (!request_mem_region(INTFBPADDR, DIO_DEVSIZE, "Internal Topcat"))
  337. return -EBUSY;
  338. printk(KERN_INFO "Internal Topcat found (secondary id %02x)\n", sid);
  339. if (hpfb_init_one(INTFBPADDR, INTFBVADDR)) {
  340. return -ENOMEM;
  341. }
  342. }
  343. return 0;
  344. }
  345. void __exit hpfb_cleanup_module(void)
  346. {
  347. dio_unregister_driver(&hpfb_driver);
  348. }
  349. module_init(hpfb_init);
  350. module_exit(hpfb_cleanup_module);
  351. MODULE_LICENSE("GPL");