hpfb.c 11 KB

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