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