chipsfb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * drivers/video/chipsfb.c -- frame buffer device for
  3. * Chips & Technologies 65550 chip.
  4. *
  5. * Copyright (C) 1998-2002 Paul Mackerras
  6. *
  7. * This file is derived from the Powermac "chips" driver:
  8. * Copyright (C) 1997 Fabio Riccardi.
  9. * And from the frame buffer device for Open Firmware-initialized devices:
  10. * Copyright (C) 1997 Geert Uytterhoeven.
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file COPYING in the main directory of this archive for
  14. * more details.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/tty.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/fb.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/console.h>
  31. #include <asm/io.h>
  32. #ifdef CONFIG_PMAC_BACKLIGHT
  33. #include <asm/backlight.h>
  34. #endif
  35. /*
  36. * Since we access the display with inb/outb to fixed port numbers,
  37. * we can only handle one 6555x chip. -- paulus
  38. */
  39. #define write_ind(num, val, ap, dp) do { \
  40. outb((num), (ap)); outb((val), (dp)); \
  41. } while (0)
  42. #define read_ind(num, var, ap, dp) do { \
  43. outb((num), (ap)); var = inb((dp)); \
  44. } while (0)
  45. /* extension registers */
  46. #define write_xr(num, val) write_ind(num, val, 0x3d6, 0x3d7)
  47. #define read_xr(num, var) read_ind(num, var, 0x3d6, 0x3d7)
  48. /* flat panel registers */
  49. #define write_fr(num, val) write_ind(num, val, 0x3d0, 0x3d1)
  50. #define read_fr(num, var) read_ind(num, var, 0x3d0, 0x3d1)
  51. /* CRTC registers */
  52. #define write_cr(num, val) write_ind(num, val, 0x3d4, 0x3d5)
  53. #define read_cr(num, var) read_ind(num, var, 0x3d4, 0x3d5)
  54. /* graphics registers */
  55. #define write_gr(num, val) write_ind(num, val, 0x3ce, 0x3cf)
  56. #define read_gr(num, var) read_ind(num, var, 0x3ce, 0x3cf)
  57. /* sequencer registers */
  58. #define write_sr(num, val) write_ind(num, val, 0x3c4, 0x3c5)
  59. #define read_sr(num, var) read_ind(num, var, 0x3c4, 0x3c5)
  60. /* attribute registers - slightly strange */
  61. #define write_ar(num, val) do { \
  62. inb(0x3da); write_ind(num, val, 0x3c0, 0x3c0); \
  63. } while (0)
  64. #define read_ar(num, var) do { \
  65. inb(0x3da); read_ind(num, var, 0x3c0, 0x3c1); \
  66. } while (0)
  67. /*
  68. * Exported functions
  69. */
  70. int chips_init(void);
  71. static int chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *);
  72. static int chipsfb_check_var(struct fb_var_screeninfo *var,
  73. struct fb_info *info);
  74. static int chipsfb_set_par(struct fb_info *info);
  75. static int chipsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  76. u_int transp, struct fb_info *info);
  77. static int chipsfb_blank(int blank, struct fb_info *info);
  78. static struct fb_ops chipsfb_ops = {
  79. .owner = THIS_MODULE,
  80. .fb_check_var = chipsfb_check_var,
  81. .fb_set_par = chipsfb_set_par,
  82. .fb_setcolreg = chipsfb_setcolreg,
  83. .fb_blank = chipsfb_blank,
  84. .fb_fillrect = cfb_fillrect,
  85. .fb_copyarea = cfb_copyarea,
  86. .fb_imageblit = cfb_imageblit,
  87. };
  88. static int chipsfb_check_var(struct fb_var_screeninfo *var,
  89. struct fb_info *info)
  90. {
  91. if (var->xres > 800 || var->yres > 600
  92. || var->xres_virtual > 800 || var->yres_virtual > 600
  93. || (var->bits_per_pixel != 8 && var->bits_per_pixel != 16)
  94. || var->nonstd
  95. || (var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
  96. return -EINVAL;
  97. var->xres = var->xres_virtual = 800;
  98. var->yres = var->yres_virtual = 600;
  99. return 0;
  100. }
  101. static int chipsfb_set_par(struct fb_info *info)
  102. {
  103. if (info->var.bits_per_pixel == 16) {
  104. write_cr(0x13, 200); // Set line length (doublewords)
  105. write_xr(0x81, 0x14); // 15 bit (555) color mode
  106. write_xr(0x82, 0x00); // Disable palettes
  107. write_xr(0x20, 0x10); // 16 bit blitter mode
  108. info->fix.line_length = 800*2;
  109. info->fix.visual = FB_VISUAL_TRUECOLOR;
  110. info->var.red.offset = 10;
  111. info->var.green.offset = 5;
  112. info->var.blue.offset = 0;
  113. info->var.red.length = info->var.green.length =
  114. info->var.blue.length = 5;
  115. } else {
  116. /* p->var.bits_per_pixel == 8 */
  117. write_cr(0x13, 100); // Set line length (doublewords)
  118. write_xr(0x81, 0x12); // 8 bit color mode
  119. write_xr(0x82, 0x08); // Graphics gamma enable
  120. write_xr(0x20, 0x00); // 8 bit blitter mode
  121. info->fix.line_length = 800;
  122. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  123. info->var.red.offset = info->var.green.offset =
  124. info->var.blue.offset = 0;
  125. info->var.red.length = info->var.green.length =
  126. info->var.blue.length = 8;
  127. }
  128. return 0;
  129. }
  130. static int chipsfb_blank(int blank, struct fb_info *info)
  131. {
  132. #ifdef CONFIG_PMAC_BACKLIGHT
  133. // used to disable backlight only for blank > 1, but it seems
  134. // useful at blank = 1 too (saves battery, extends backlight life)
  135. set_backlight_enable(!blank);
  136. #endif /* CONFIG_PMAC_BACKLIGHT */
  137. return 1; /* get fb_blank to set the colormap to all black */
  138. }
  139. static int chipsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  140. u_int transp, struct fb_info *info)
  141. {
  142. if (regno > 255)
  143. return 1;
  144. red >>= 8;
  145. green >>= 8;
  146. blue >>= 8;
  147. outb(regno, 0x3c8);
  148. udelay(1);
  149. outb(red, 0x3c9);
  150. outb(green, 0x3c9);
  151. outb(blue, 0x3c9);
  152. return 0;
  153. }
  154. struct chips_init_reg {
  155. unsigned char addr;
  156. unsigned char data;
  157. };
  158. static struct chips_init_reg chips_init_sr[] = {
  159. { 0x00, 0x03 },
  160. { 0x01, 0x01 },
  161. { 0x02, 0x0f },
  162. { 0x04, 0x0e }
  163. };
  164. static struct chips_init_reg chips_init_gr[] = {
  165. { 0x05, 0x00 },
  166. { 0x06, 0x0d },
  167. { 0x08, 0xff }
  168. };
  169. static struct chips_init_reg chips_init_ar[] = {
  170. { 0x10, 0x01 },
  171. { 0x12, 0x0f },
  172. { 0x13, 0x00 }
  173. };
  174. static struct chips_init_reg chips_init_cr[] = {
  175. { 0x00, 0x7f },
  176. { 0x01, 0x63 },
  177. { 0x02, 0x63 },
  178. { 0x03, 0x83 },
  179. { 0x04, 0x66 },
  180. { 0x05, 0x10 },
  181. { 0x06, 0x72 },
  182. { 0x07, 0x3e },
  183. { 0x08, 0x00 },
  184. { 0x09, 0x40 },
  185. { 0x0c, 0x00 },
  186. { 0x0d, 0x00 },
  187. { 0x10, 0x59 },
  188. { 0x11, 0x0d },
  189. { 0x12, 0x57 },
  190. { 0x13, 0x64 },
  191. { 0x14, 0x00 },
  192. { 0x15, 0x57 },
  193. { 0x16, 0x73 },
  194. { 0x17, 0xe3 },
  195. { 0x18, 0xff },
  196. { 0x30, 0x02 },
  197. { 0x31, 0x02 },
  198. { 0x32, 0x02 },
  199. { 0x33, 0x02 },
  200. { 0x40, 0x00 },
  201. { 0x41, 0x00 },
  202. { 0x40, 0x80 }
  203. };
  204. static struct chips_init_reg chips_init_fr[] = {
  205. { 0x01, 0x02 },
  206. { 0x03, 0x08 },
  207. { 0x04, 0x81 },
  208. { 0x05, 0x21 },
  209. { 0x08, 0x0c },
  210. { 0x0a, 0x74 },
  211. { 0x0b, 0x11 },
  212. { 0x10, 0x0c },
  213. { 0x11, 0xe0 },
  214. /* { 0x12, 0x40 }, -- 3400 needs 40, 2400 needs 48, no way to tell */
  215. { 0x20, 0x63 },
  216. { 0x21, 0x68 },
  217. { 0x22, 0x19 },
  218. { 0x23, 0x7f },
  219. { 0x24, 0x68 },
  220. { 0x26, 0x00 },
  221. { 0x27, 0x0f },
  222. { 0x30, 0x57 },
  223. { 0x31, 0x58 },
  224. { 0x32, 0x0d },
  225. { 0x33, 0x72 },
  226. { 0x34, 0x02 },
  227. { 0x35, 0x22 },
  228. { 0x36, 0x02 },
  229. { 0x37, 0x00 }
  230. };
  231. static struct chips_init_reg chips_init_xr[] = {
  232. { 0xce, 0x00 }, /* set default memory clock */
  233. { 0xcc, 0x43 }, /* memory clock ratio */
  234. { 0xcd, 0x18 },
  235. { 0xce, 0xa1 },
  236. { 0xc8, 0x84 },
  237. { 0xc9, 0x0a },
  238. { 0xca, 0x00 },
  239. { 0xcb, 0x20 },
  240. { 0xcf, 0x06 },
  241. { 0xd0, 0x0e },
  242. { 0x09, 0x01 },
  243. { 0x0a, 0x02 },
  244. { 0x0b, 0x01 },
  245. { 0x20, 0x00 },
  246. { 0x40, 0x03 },
  247. { 0x41, 0x01 },
  248. { 0x42, 0x00 },
  249. { 0x80, 0x82 },
  250. { 0x81, 0x12 },
  251. { 0x82, 0x08 },
  252. { 0xa0, 0x00 },
  253. { 0xa8, 0x00 }
  254. };
  255. static void __init chips_hw_init(void)
  256. {
  257. int i;
  258. for (i = 0; i < ARRAY_SIZE(chips_init_xr); ++i)
  259. write_xr(chips_init_xr[i].addr, chips_init_xr[i].data);
  260. outb(0x29, 0x3c2); /* set misc output reg */
  261. for (i = 0; i < ARRAY_SIZE(chips_init_sr); ++i)
  262. write_sr(chips_init_sr[i].addr, chips_init_sr[i].data);
  263. for (i = 0; i < ARRAY_SIZE(chips_init_gr); ++i)
  264. write_gr(chips_init_gr[i].addr, chips_init_gr[i].data);
  265. for (i = 0; i < ARRAY_SIZE(chips_init_ar); ++i)
  266. write_ar(chips_init_ar[i].addr, chips_init_ar[i].data);
  267. for (i = 0; i < ARRAY_SIZE(chips_init_cr); ++i)
  268. write_cr(chips_init_cr[i].addr, chips_init_cr[i].data);
  269. for (i = 0; i < ARRAY_SIZE(chips_init_fr); ++i)
  270. write_fr(chips_init_fr[i].addr, chips_init_fr[i].data);
  271. }
  272. static struct fb_fix_screeninfo chipsfb_fix __initdata = {
  273. .id = "C&T 65550",
  274. .type = FB_TYPE_PACKED_PIXELS,
  275. .visual = FB_VISUAL_PSEUDOCOLOR,
  276. .accel = FB_ACCEL_NONE,
  277. .line_length = 800,
  278. // FIXME: Assumes 1MB frame buffer, but 65550 supports 1MB or 2MB.
  279. // * "3500" PowerBook G3 (the original PB G3) has 2MB.
  280. // * 2400 has 1MB composed of 2 Mitsubishi M5M4V4265CTP DRAM chips.
  281. // Motherboard actually supports 2MB -- there are two blank locations
  282. // for a second pair of DRAMs. (Thanks, Apple!)
  283. // * 3400 has 1MB (I think). Don't know if it's expandable.
  284. // -- Tim Seufert
  285. .smem_len = 0x100000, /* 1MB */
  286. };
  287. static struct fb_var_screeninfo chipsfb_var __initdata = {
  288. .xres = 800,
  289. .yres = 600,
  290. .xres_virtual = 800,
  291. .yres_virtual = 600,
  292. .bits_per_pixel = 8,
  293. .red = { .length = 8 },
  294. .green = { .length = 8 },
  295. .blue = { .length = 8 },
  296. .height = -1,
  297. .width = -1,
  298. .vmode = FB_VMODE_NONINTERLACED,
  299. .pixclock = 10000,
  300. .left_margin = 16,
  301. .right_margin = 16,
  302. .upper_margin = 16,
  303. .lower_margin = 16,
  304. .hsync_len = 8,
  305. .vsync_len = 8,
  306. };
  307. static void __init init_chips(struct fb_info *p, unsigned long addr)
  308. {
  309. memset(p->screen_base, 0, 0x100000);
  310. p->fix = chipsfb_fix;
  311. p->fix.smem_start = addr;
  312. p->var = chipsfb_var;
  313. p->fbops = &chipsfb_ops;
  314. p->flags = FBINFO_DEFAULT;
  315. fb_alloc_cmap(&p->cmap, 256, 0);
  316. chips_hw_init();
  317. }
  318. static int __devinit
  319. chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *ent)
  320. {
  321. struct fb_info *p;
  322. unsigned long addr, size;
  323. unsigned short cmd;
  324. int rc = -ENODEV;
  325. if (pci_enable_device(dp) < 0) {
  326. dev_err(&dp->dev, "Cannot enable PCI device\n");
  327. goto err_out;
  328. }
  329. if ((dp->resource[0].flags & IORESOURCE_MEM) == 0)
  330. goto err_disable;
  331. addr = pci_resource_start(dp, 0);
  332. size = pci_resource_len(dp, 0);
  333. if (addr == 0)
  334. goto err_disable;
  335. p = framebuffer_alloc(0, &dp->dev);
  336. if (p == NULL) {
  337. dev_err(&dp->dev, "Cannot allocate framebuffer structure\n");
  338. rc = -ENOMEM;
  339. goto err_disable;
  340. }
  341. if (pci_request_region(dp, 0, "chipsfb") != 0) {
  342. dev_err(&dp->dev, "Cannot request framebuffer\n");
  343. rc = -EBUSY;
  344. goto err_release_fb;
  345. }
  346. #ifdef __BIG_ENDIAN
  347. addr += 0x800000; // Use big-endian aperture
  348. #endif
  349. /* we should use pci_enable_device here, but,
  350. the device doesn't declare its I/O ports in its BARs
  351. so pci_enable_device won't turn on I/O responses */
  352. pci_read_config_word(dp, PCI_COMMAND, &cmd);
  353. cmd |= 3; /* enable memory and IO space */
  354. pci_write_config_word(dp, PCI_COMMAND, cmd);
  355. #ifdef CONFIG_PMAC_BACKLIGHT
  356. /* turn on the backlight */
  357. set_backlight_enable(1);
  358. #endif /* CONFIG_PMAC_BACKLIGHT */
  359. #ifdef CONFIG_PPC
  360. p->screen_base = __ioremap(addr, 0x200000, _PAGE_NO_CACHE);
  361. #else
  362. p->screen_base = ioremap(addr, 0x200000);
  363. #endif
  364. if (p->screen_base == NULL) {
  365. dev_err(&dp->dev, "Cannot map framebuffer\n");
  366. rc = -ENOMEM;
  367. goto err_release_pci;
  368. }
  369. pci_set_drvdata(dp, p);
  370. p->device = &dp->dev;
  371. init_chips(p, addr);
  372. if (register_framebuffer(p) < 0) {
  373. dev_err(&dp->dev,"C&T 65550 framebuffer failed to register\n");
  374. goto err_unmap;
  375. }
  376. dev_info(&dp->dev,"fb%d: Chips 65550 frame buffer"
  377. " (%dK RAM detected)\n",
  378. p->node, p->fix.smem_len / 1024);
  379. return 0;
  380. err_unmap:
  381. iounmap(p->screen_base);
  382. err_release_pci:
  383. pci_release_region(dp, 0);
  384. err_release_fb:
  385. framebuffer_release(p);
  386. err_disable:
  387. err_out:
  388. return rc;
  389. }
  390. static void __devexit chipsfb_remove(struct pci_dev *dp)
  391. {
  392. struct fb_info *p = pci_get_drvdata(dp);
  393. if (p->screen_base == NULL)
  394. return;
  395. unregister_framebuffer(p);
  396. iounmap(p->screen_base);
  397. p->screen_base = NULL;
  398. pci_release_region(dp, 0);
  399. }
  400. #ifdef CONFIG_PM
  401. static int chipsfb_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  402. {
  403. struct fb_info *p = pci_get_drvdata(pdev);
  404. if (state.event == pdev->dev.power.power_state.event)
  405. return 0;
  406. if (state.event != PM_SUSPEND_MEM)
  407. goto done;
  408. acquire_console_sem();
  409. chipsfb_blank(1, p);
  410. fb_set_suspend(p, 1);
  411. release_console_sem();
  412. done:
  413. pdev->dev.power.power_state = state;
  414. return 0;
  415. }
  416. static int chipsfb_pci_resume(struct pci_dev *pdev)
  417. {
  418. struct fb_info *p = pci_get_drvdata(pdev);
  419. acquire_console_sem();
  420. fb_set_suspend(p, 0);
  421. chipsfb_blank(0, p);
  422. release_console_sem();
  423. pdev->dev.power.power_state = PMSG_ON;
  424. return 0;
  425. }
  426. #endif /* CONFIG_PM */
  427. static struct pci_device_id chipsfb_pci_tbl[] = {
  428. { PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_65550, PCI_ANY_ID, PCI_ANY_ID },
  429. { 0 }
  430. };
  431. MODULE_DEVICE_TABLE(pci, chipsfb_pci_tbl);
  432. static struct pci_driver chipsfb_driver = {
  433. .name = "chipsfb",
  434. .id_table = chipsfb_pci_tbl,
  435. .probe = chipsfb_pci_init,
  436. .remove = __devexit_p(chipsfb_remove),
  437. #ifdef CONFIG_PM
  438. .suspend = chipsfb_pci_suspend,
  439. .resume = chipsfb_pci_resume,
  440. #endif
  441. };
  442. int __init chips_init(void)
  443. {
  444. if (fb_get_options("chipsfb", NULL))
  445. return -ENODEV;
  446. return pci_register_driver(&chipsfb_driver);
  447. }
  448. module_init(chips_init);
  449. static void __exit chipsfb_exit(void)
  450. {
  451. pci_unregister_driver(&chipsfb_driver);
  452. }
  453. MODULE_LICENSE("GPL");