chipsfb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/fb.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. #ifdef CONFIG_PMAC_BACKLIGHT
  131. mutex_lock(&pmac_backlight_mutex);
  132. if (pmac_backlight) {
  133. /* used to disable backlight only for blank > 1, but it seems
  134. * useful at blank = 1 too (saves battery, extends backlight
  135. * life)
  136. */
  137. down(&pmac_backlight->sem);
  138. if (blank)
  139. pmac_backlight->props->power = FB_BLANK_POWERDOWN;
  140. else
  141. pmac_backlight->props->power = FB_BLANK_UNBLANK;
  142. pmac_backlight->props->update_status(pmac_backlight);
  143. up(&pmac_backlight->sem);
  144. }
  145. mutex_unlock(&pmac_backlight_mutex);
  146. #endif /* CONFIG_PMAC_BACKLIGHT */
  147. return 1; /* get fb_blank to set the colormap to all black */
  148. }
  149. static int chipsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  150. u_int transp, struct fb_info *info)
  151. {
  152. if (regno > 255)
  153. return 1;
  154. red >>= 8;
  155. green >>= 8;
  156. blue >>= 8;
  157. outb(regno, 0x3c8);
  158. udelay(1);
  159. outb(red, 0x3c9);
  160. outb(green, 0x3c9);
  161. outb(blue, 0x3c9);
  162. return 0;
  163. }
  164. struct chips_init_reg {
  165. unsigned char addr;
  166. unsigned char data;
  167. };
  168. static struct chips_init_reg chips_init_sr[] = {
  169. { 0x00, 0x03 },
  170. { 0x01, 0x01 },
  171. { 0x02, 0x0f },
  172. { 0x04, 0x0e }
  173. };
  174. static struct chips_init_reg chips_init_gr[] = {
  175. { 0x05, 0x00 },
  176. { 0x06, 0x0d },
  177. { 0x08, 0xff }
  178. };
  179. static struct chips_init_reg chips_init_ar[] = {
  180. { 0x10, 0x01 },
  181. { 0x12, 0x0f },
  182. { 0x13, 0x00 }
  183. };
  184. static struct chips_init_reg chips_init_cr[] = {
  185. { 0x00, 0x7f },
  186. { 0x01, 0x63 },
  187. { 0x02, 0x63 },
  188. { 0x03, 0x83 },
  189. { 0x04, 0x66 },
  190. { 0x05, 0x10 },
  191. { 0x06, 0x72 },
  192. { 0x07, 0x3e },
  193. { 0x08, 0x00 },
  194. { 0x09, 0x40 },
  195. { 0x0c, 0x00 },
  196. { 0x0d, 0x00 },
  197. { 0x10, 0x59 },
  198. { 0x11, 0x0d },
  199. { 0x12, 0x57 },
  200. { 0x13, 0x64 },
  201. { 0x14, 0x00 },
  202. { 0x15, 0x57 },
  203. { 0x16, 0x73 },
  204. { 0x17, 0xe3 },
  205. { 0x18, 0xff },
  206. { 0x30, 0x02 },
  207. { 0x31, 0x02 },
  208. { 0x32, 0x02 },
  209. { 0x33, 0x02 },
  210. { 0x40, 0x00 },
  211. { 0x41, 0x00 },
  212. { 0x40, 0x80 }
  213. };
  214. static struct chips_init_reg chips_init_fr[] = {
  215. { 0x01, 0x02 },
  216. { 0x03, 0x08 },
  217. { 0x04, 0x81 },
  218. { 0x05, 0x21 },
  219. { 0x08, 0x0c },
  220. { 0x0a, 0x74 },
  221. { 0x0b, 0x11 },
  222. { 0x10, 0x0c },
  223. { 0x11, 0xe0 },
  224. /* { 0x12, 0x40 }, -- 3400 needs 40, 2400 needs 48, no way to tell */
  225. { 0x20, 0x63 },
  226. { 0x21, 0x68 },
  227. { 0x22, 0x19 },
  228. { 0x23, 0x7f },
  229. { 0x24, 0x68 },
  230. { 0x26, 0x00 },
  231. { 0x27, 0x0f },
  232. { 0x30, 0x57 },
  233. { 0x31, 0x58 },
  234. { 0x32, 0x0d },
  235. { 0x33, 0x72 },
  236. { 0x34, 0x02 },
  237. { 0x35, 0x22 },
  238. { 0x36, 0x02 },
  239. { 0x37, 0x00 }
  240. };
  241. static struct chips_init_reg chips_init_xr[] = {
  242. { 0xce, 0x00 }, /* set default memory clock */
  243. { 0xcc, 0x43 }, /* memory clock ratio */
  244. { 0xcd, 0x18 },
  245. { 0xce, 0xa1 },
  246. { 0xc8, 0x84 },
  247. { 0xc9, 0x0a },
  248. { 0xca, 0x00 },
  249. { 0xcb, 0x20 },
  250. { 0xcf, 0x06 },
  251. { 0xd0, 0x0e },
  252. { 0x09, 0x01 },
  253. { 0x0a, 0x02 },
  254. { 0x0b, 0x01 },
  255. { 0x20, 0x00 },
  256. { 0x40, 0x03 },
  257. { 0x41, 0x01 },
  258. { 0x42, 0x00 },
  259. { 0x80, 0x82 },
  260. { 0x81, 0x12 },
  261. { 0x82, 0x08 },
  262. { 0xa0, 0x00 },
  263. { 0xa8, 0x00 }
  264. };
  265. static void __init chips_hw_init(void)
  266. {
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(chips_init_xr); ++i)
  269. write_xr(chips_init_xr[i].addr, chips_init_xr[i].data);
  270. outb(0x29, 0x3c2); /* set misc output reg */
  271. for (i = 0; i < ARRAY_SIZE(chips_init_sr); ++i)
  272. write_sr(chips_init_sr[i].addr, chips_init_sr[i].data);
  273. for (i = 0; i < ARRAY_SIZE(chips_init_gr); ++i)
  274. write_gr(chips_init_gr[i].addr, chips_init_gr[i].data);
  275. for (i = 0; i < ARRAY_SIZE(chips_init_ar); ++i)
  276. write_ar(chips_init_ar[i].addr, chips_init_ar[i].data);
  277. for (i = 0; i < ARRAY_SIZE(chips_init_cr); ++i)
  278. write_cr(chips_init_cr[i].addr, chips_init_cr[i].data);
  279. for (i = 0; i < ARRAY_SIZE(chips_init_fr); ++i)
  280. write_fr(chips_init_fr[i].addr, chips_init_fr[i].data);
  281. }
  282. static struct fb_fix_screeninfo chipsfb_fix __initdata = {
  283. .id = "C&T 65550",
  284. .type = FB_TYPE_PACKED_PIXELS,
  285. .visual = FB_VISUAL_PSEUDOCOLOR,
  286. .accel = FB_ACCEL_NONE,
  287. .line_length = 800,
  288. // FIXME: Assumes 1MB frame buffer, but 65550 supports 1MB or 2MB.
  289. // * "3500" PowerBook G3 (the original PB G3) has 2MB.
  290. // * 2400 has 1MB composed of 2 Mitsubishi M5M4V4265CTP DRAM chips.
  291. // Motherboard actually supports 2MB -- there are two blank locations
  292. // for a second pair of DRAMs. (Thanks, Apple!)
  293. // * 3400 has 1MB (I think). Don't know if it's expandable.
  294. // -- Tim Seufert
  295. .smem_len = 0x100000, /* 1MB */
  296. };
  297. static struct fb_var_screeninfo chipsfb_var __initdata = {
  298. .xres = 800,
  299. .yres = 600,
  300. .xres_virtual = 800,
  301. .yres_virtual = 600,
  302. .bits_per_pixel = 8,
  303. .red = { .length = 8 },
  304. .green = { .length = 8 },
  305. .blue = { .length = 8 },
  306. .height = -1,
  307. .width = -1,
  308. .vmode = FB_VMODE_NONINTERLACED,
  309. .pixclock = 10000,
  310. .left_margin = 16,
  311. .right_margin = 16,
  312. .upper_margin = 16,
  313. .lower_margin = 16,
  314. .hsync_len = 8,
  315. .vsync_len = 8,
  316. };
  317. static void __init init_chips(struct fb_info *p, unsigned long addr)
  318. {
  319. memset(p->screen_base, 0, 0x100000);
  320. p->fix = chipsfb_fix;
  321. p->fix.smem_start = addr;
  322. p->var = chipsfb_var;
  323. p->fbops = &chipsfb_ops;
  324. p->flags = FBINFO_DEFAULT;
  325. fb_alloc_cmap(&p->cmap, 256, 0);
  326. chips_hw_init();
  327. }
  328. static int __devinit
  329. chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *ent)
  330. {
  331. struct fb_info *p;
  332. unsigned long addr, size;
  333. unsigned short cmd;
  334. int rc = -ENODEV;
  335. if (pci_enable_device(dp) < 0) {
  336. dev_err(&dp->dev, "Cannot enable PCI device\n");
  337. goto err_out;
  338. }
  339. if ((dp->resource[0].flags & IORESOURCE_MEM) == 0)
  340. goto err_disable;
  341. addr = pci_resource_start(dp, 0);
  342. size = pci_resource_len(dp, 0);
  343. if (addr == 0)
  344. goto err_disable;
  345. p = framebuffer_alloc(0, &dp->dev);
  346. if (p == NULL) {
  347. dev_err(&dp->dev, "Cannot allocate framebuffer structure\n");
  348. rc = -ENOMEM;
  349. goto err_disable;
  350. }
  351. if (pci_request_region(dp, 0, "chipsfb") != 0) {
  352. dev_err(&dp->dev, "Cannot request framebuffer\n");
  353. rc = -EBUSY;
  354. goto err_release_fb;
  355. }
  356. #ifdef __BIG_ENDIAN
  357. addr += 0x800000; // Use big-endian aperture
  358. #endif
  359. /* we should use pci_enable_device here, but,
  360. the device doesn't declare its I/O ports in its BARs
  361. so pci_enable_device won't turn on I/O responses */
  362. pci_read_config_word(dp, PCI_COMMAND, &cmd);
  363. cmd |= 3; /* enable memory and IO space */
  364. pci_write_config_word(dp, PCI_COMMAND, cmd);
  365. #ifdef CONFIG_PMAC_BACKLIGHT
  366. /* turn on the backlight */
  367. mutex_lock(&pmac_backlight_mutex);
  368. if (pmac_backlight) {
  369. down(&pmac_backlight->sem);
  370. pmac_backlight->props->power = FB_BLANK_UNBLANK;
  371. pmac_backlight->props->update_status(pmac_backlight);
  372. up(&pmac_backlight->sem);
  373. }
  374. mutex_unlock(&pmac_backlight_mutex);
  375. #endif /* CONFIG_PMAC_BACKLIGHT */
  376. #ifdef CONFIG_PPC
  377. p->screen_base = __ioremap(addr, 0x200000, _PAGE_NO_CACHE);
  378. #else
  379. p->screen_base = ioremap(addr, 0x200000);
  380. #endif
  381. if (p->screen_base == NULL) {
  382. dev_err(&dp->dev, "Cannot map framebuffer\n");
  383. rc = -ENOMEM;
  384. goto err_release_pci;
  385. }
  386. pci_set_drvdata(dp, p);
  387. p->device = &dp->dev;
  388. init_chips(p, addr);
  389. if (register_framebuffer(p) < 0) {
  390. dev_err(&dp->dev,"C&T 65550 framebuffer failed to register\n");
  391. goto err_unmap;
  392. }
  393. dev_info(&dp->dev,"fb%d: Chips 65550 frame buffer"
  394. " (%dK RAM detected)\n",
  395. p->node, p->fix.smem_len / 1024);
  396. return 0;
  397. err_unmap:
  398. iounmap(p->screen_base);
  399. err_release_pci:
  400. pci_release_region(dp, 0);
  401. err_release_fb:
  402. framebuffer_release(p);
  403. err_disable:
  404. err_out:
  405. return rc;
  406. }
  407. static void __devexit chipsfb_remove(struct pci_dev *dp)
  408. {
  409. struct fb_info *p = pci_get_drvdata(dp);
  410. if (p->screen_base == NULL)
  411. return;
  412. unregister_framebuffer(p);
  413. iounmap(p->screen_base);
  414. p->screen_base = NULL;
  415. pci_release_region(dp, 0);
  416. }
  417. #ifdef CONFIG_PM
  418. static int chipsfb_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  419. {
  420. struct fb_info *p = pci_get_drvdata(pdev);
  421. if (state.event == pdev->dev.power.power_state.event)
  422. return 0;
  423. if (state.event != PM_SUSPEND_MEM)
  424. goto done;
  425. acquire_console_sem();
  426. chipsfb_blank(1, p);
  427. fb_set_suspend(p, 1);
  428. release_console_sem();
  429. done:
  430. pdev->dev.power.power_state = state;
  431. return 0;
  432. }
  433. static int chipsfb_pci_resume(struct pci_dev *pdev)
  434. {
  435. struct fb_info *p = pci_get_drvdata(pdev);
  436. acquire_console_sem();
  437. fb_set_suspend(p, 0);
  438. chipsfb_blank(0, p);
  439. release_console_sem();
  440. pdev->dev.power.power_state = PMSG_ON;
  441. return 0;
  442. }
  443. #endif /* CONFIG_PM */
  444. static struct pci_device_id chipsfb_pci_tbl[] = {
  445. { PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_65550, PCI_ANY_ID, PCI_ANY_ID },
  446. { 0 }
  447. };
  448. MODULE_DEVICE_TABLE(pci, chipsfb_pci_tbl);
  449. static struct pci_driver chipsfb_driver = {
  450. .name = "chipsfb",
  451. .id_table = chipsfb_pci_tbl,
  452. .probe = chipsfb_pci_init,
  453. .remove = __devexit_p(chipsfb_remove),
  454. #ifdef CONFIG_PM
  455. .suspend = chipsfb_pci_suspend,
  456. .resume = chipsfb_pci_resume,
  457. #endif
  458. };
  459. int __init chips_init(void)
  460. {
  461. if (fb_get_options("chipsfb", NULL))
  462. return -ENODEV;
  463. return pci_register_driver(&chipsfb_driver);
  464. }
  465. module_init(chips_init);
  466. static void __exit chipsfb_exit(void)
  467. {
  468. pci_unregister_driver(&chipsfb_driver);
  469. }
  470. MODULE_LICENSE("GPL");