hpfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/fb.h>
  15. #include <linux/dio.h>
  16. #include <asm/io.h>
  17. #include <asm/uaccess.h>
  18. static struct fb_info fb_info = {
  19. .fix = {
  20. .id = "HP300 ",
  21. .type = FB_TYPE_PACKED_PIXELS,
  22. .visual = FB_VISUAL_PSEUDOCOLOR,
  23. .accel = FB_ACCEL_NONE,
  24. }
  25. };
  26. static unsigned long fb_regs;
  27. static unsigned char fb_bitmask;
  28. #define TC_NBLANK 0x4080
  29. #define TC_WEN 0x4088
  30. #define TC_REN 0x408c
  31. #define TC_FBEN 0x4090
  32. #define TC_PRR 0x40ea
  33. /* These defines match the X window system */
  34. #define RR_CLEAR 0x0
  35. #define RR_COPY 0x3
  36. #define RR_NOOP 0x5
  37. #define RR_XOR 0x6
  38. #define RR_INVERT 0xa
  39. #define RR_COPYINVERTED 0xc
  40. #define RR_SET 0xf
  41. /* blitter regs */
  42. #define BUSY 0x4044
  43. #define WMRR 0x40ef
  44. #define SOURCE_X 0x40f2
  45. #define SOURCE_Y 0x40f6
  46. #define DEST_X 0x40fa
  47. #define DEST_Y 0x40fe
  48. #define WHEIGHT 0x4106
  49. #define WWIDTH 0x4102
  50. #define WMOVE 0x409c
  51. static struct fb_var_screeninfo hpfb_defined = {
  52. .red = {
  53. .length = 8,
  54. },
  55. .green = {
  56. .length = 8,
  57. },
  58. .blue = {
  59. .length = 8,
  60. },
  61. .activate = FB_ACTIVATE_NOW,
  62. .height = -1,
  63. .width = -1,
  64. .vmode = FB_VMODE_NONINTERLACED,
  65. };
  66. static int hpfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  67. unsigned blue, unsigned transp,
  68. struct fb_info *info)
  69. {
  70. /* use MSBs */
  71. unsigned char _red =red>>8;
  72. unsigned char _green=green>>8;
  73. unsigned char _blue =blue>>8;
  74. unsigned char _regno=regno;
  75. /*
  76. * Set a single color register. The values supplied are
  77. * already rounded down to the hardware's capabilities
  78. * (according to the entries in the `var' structure). Return
  79. * != 0 for invalid regno.
  80. */
  81. if (regno >= info->cmap.len)
  82. return 1;
  83. while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
  84. out_be16(fb_regs + 0x60ba, 0xff);
  85. out_be16(fb_regs + 0x60b2, _red);
  86. out_be16(fb_regs + 0x60b4, _green);
  87. out_be16(fb_regs + 0x60b6, _blue);
  88. out_be16(fb_regs + 0x60b8, ~_regno);
  89. out_be16(fb_regs + 0x60f0, 0xff);
  90. udelay(100);
  91. while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
  92. out_be16(fb_regs + 0x60b2, 0);
  93. out_be16(fb_regs + 0x60b4, 0);
  94. out_be16(fb_regs + 0x60b6, 0);
  95. out_be16(fb_regs + 0x60b8, 0);
  96. return 0;
  97. }
  98. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  99. static int hpfb_blank(int blank, struct fb_info *info)
  100. {
  101. out_8(fb_regs + TC_NBLANK, (blank ? 0x00 : fb_bitmask));
  102. return 0;
  103. }
  104. static void topcat_blit(int x0, int y0, int x1, int y1, int w, int h, int rr)
  105. {
  106. if (rr >= 0) {
  107. while (in_8(fb_regs + BUSY) & fb_bitmask)
  108. ;
  109. }
  110. out_8(fb_regs + TC_FBEN, fb_bitmask);
  111. if (rr >= 0) {
  112. out_8(fb_regs + TC_WEN, fb_bitmask);
  113. out_8(fb_regs + WMRR, rr);
  114. }
  115. out_be16(fb_regs + SOURCE_X, x0);
  116. out_be16(fb_regs + SOURCE_Y, y0);
  117. out_be16(fb_regs + DEST_X, x1);
  118. out_be16(fb_regs + DEST_Y, y1);
  119. out_be16(fb_regs + WWIDTH, w);
  120. out_be16(fb_regs + WHEIGHT, h);
  121. out_8(fb_regs + WMOVE, fb_bitmask);
  122. }
  123. static void hpfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  124. {
  125. topcat_blit(area->sx, area->sy, area->dx, area->dy, area->width, area->height, RR_COPY);
  126. }
  127. static void hpfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
  128. {
  129. u8 clr;
  130. clr = region->color & 0xff;
  131. while (in_8(fb_regs + BUSY) & fb_bitmask)
  132. ;
  133. /* Foreground */
  134. out_8(fb_regs + TC_WEN, fb_bitmask & clr);
  135. out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_SET : RR_INVERT));
  136. /* Background */
  137. out_8(fb_regs + TC_WEN, fb_bitmask & ~clr);
  138. out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_CLEAR : RR_NOOP));
  139. topcat_blit(region->dx, region->dy, region->dx, region->dy, region->width, region->height, -1);
  140. }
  141. static int hpfb_sync(struct fb_info *info)
  142. {
  143. /*
  144. * Since we also access the framebuffer directly, we have to wait
  145. * until the block mover is finished
  146. */
  147. while (in_8(fb_regs + BUSY) & fb_bitmask)
  148. ;
  149. out_8(fb_regs + TC_WEN, fb_bitmask);
  150. out_8(fb_regs + TC_PRR, RR_COPY);
  151. out_8(fb_regs + TC_FBEN, fb_bitmask);
  152. return 0;
  153. }
  154. static struct fb_ops hpfb_ops = {
  155. .owner = THIS_MODULE,
  156. .fb_setcolreg = hpfb_setcolreg,
  157. .fb_blank = hpfb_blank,
  158. .fb_fillrect = hpfb_fillrect,
  159. .fb_copyarea = hpfb_copyarea,
  160. .fb_imageblit = cfb_imageblit,
  161. .fb_sync = hpfb_sync,
  162. };
  163. /* Common to all HP framebuffers */
  164. #define HPFB_FBWMSB 0x05 /* Frame buffer width */
  165. #define HPFB_FBWLSB 0x07
  166. #define HPFB_FBHMSB 0x09 /* Frame buffer height */
  167. #define HPFB_FBHLSB 0x0b
  168. #define HPFB_DWMSB 0x0d /* Display width */
  169. #define HPFB_DWLSB 0x0f
  170. #define HPFB_DHMSB 0x11 /* Display height */
  171. #define HPFB_DHLSB 0x13
  172. #define HPFB_NUMPLANES 0x5b /* Number of colour planes */
  173. #define HPFB_FBOMSB 0x5d /* Frame buffer offset */
  174. #define HPFB_FBOLSB 0x5f
  175. static int __devinit hpfb_init_one(unsigned long phys_base,
  176. 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, resource_size(&d->resource), d->name))
  273. return -EBUSY;
  274. if (d->scode >= DIOII_SCBASE) {
  275. vaddr = (unsigned long)ioremap(paddr, resource_size(&d->resource));
  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, resource_size(&d->resource));
  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 -ENODEV;
  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");