chipsfb.c 12 KB

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