gxt4500.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Frame buffer device for IBM GXT4500P/6500P and GXT4000P/6000P
  3. * display adaptors
  4. *
  5. * Copyright (C) 2006 Paul Mackerras, IBM Corp. <paulus@samba.org>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/fb.h>
  10. #include <linux/console.h>
  11. #include <linux/pci.h>
  12. #include <linux/pci_ids.h>
  13. #include <linux/delay.h>
  14. #include <linux/string.h>
  15. #define PCI_DEVICE_ID_IBM_GXT4500P 0x21c
  16. #define PCI_DEVICE_ID_IBM_GXT6500P 0x21b
  17. #define PCI_DEVICE_ID_IBM_GXT4000P 0x16e
  18. #define PCI_DEVICE_ID_IBM_GXT6000P 0x170
  19. /* GXT4500P registers */
  20. /* Registers in PCI config space */
  21. #define CFG_ENDIAN0 0x40
  22. /* Misc control/status registers */
  23. #define STATUS 0x1000
  24. #define CTRL_REG0 0x1004
  25. #define CR0_HALT_DMA 0x4
  26. #define CR0_RASTER_RESET 0x8
  27. #define CR0_GEOM_RESET 0x10
  28. #define CR0_MEM_CTRLER_RESET 0x20
  29. /* Framebuffer control registers */
  30. #define FB_AB_CTRL 0x1100
  31. #define FB_CD_CTRL 0x1104
  32. #define FB_WID_CTRL 0x1108
  33. #define FB_Z_CTRL 0x110c
  34. #define FB_VGA_CTRL 0x1110
  35. #define REFRESH_AB_CTRL 0x1114
  36. #define REFRESH_CD_CTRL 0x1118
  37. #define FB_OVL_CTRL 0x111c
  38. #define FB_CTRL_TYPE 0x80000000
  39. #define FB_CTRL_WIDTH_MASK 0x007f0000
  40. #define FB_CTRL_WIDTH_SHIFT 16
  41. #define FB_CTRL_START_SEG_MASK 0x00003fff
  42. #define REFRESH_START 0x1098
  43. #define REFRESH_SIZE 0x109c
  44. /* "Direct" framebuffer access registers */
  45. #define DFA_FB_A 0x11e0
  46. #define DFA_FB_B 0x11e4
  47. #define DFA_FB_C 0x11e8
  48. #define DFA_FB_D 0x11ec
  49. #define DFA_FB_ENABLE 0x80000000
  50. #define DFA_FB_BASE_MASK 0x03f00000
  51. #define DFA_FB_STRIDE_1k 0x00000000
  52. #define DFA_FB_STRIDE_2k 0x00000010
  53. #define DFA_FB_STRIDE_4k 0x00000020
  54. #define DFA_PIX_8BIT 0x00000000
  55. #define DFA_PIX_16BIT_565 0x00000001
  56. #define DFA_PIX_16BIT_1555 0x00000002
  57. #define DFA_PIX_24BIT 0x00000004
  58. #define DFA_PIX_32BIT 0x00000005
  59. /* maps DFA_PIX_* to pixel size in bytes */
  60. static const unsigned char pixsize[] = {
  61. 1, 2, 2, 2, 4, 4
  62. };
  63. /* Display timing generator registers */
  64. #define DTG_CONTROL 0x1900
  65. #define DTG_CTL_SCREEN_REFRESH 2
  66. #define DTG_CTL_ENABLE 1
  67. #define DTG_HORIZ_EXTENT 0x1904
  68. #define DTG_HORIZ_DISPLAY 0x1908
  69. #define DTG_HSYNC_START 0x190c
  70. #define DTG_HSYNC_END 0x1910
  71. #define DTG_HSYNC_END_COMP 0x1914
  72. #define DTG_VERT_EXTENT 0x1918
  73. #define DTG_VERT_DISPLAY 0x191c
  74. #define DTG_VSYNC_START 0x1920
  75. #define DTG_VSYNC_END 0x1924
  76. #define DTG_VERT_SHORT 0x1928
  77. /* PLL/RAMDAC registers */
  78. #define DISP_CTL 0x402c
  79. #define DISP_CTL_OFF 2
  80. #define SYNC_CTL 0x4034
  81. #define SYNC_CTL_SYNC_ON_RGB 1
  82. #define SYNC_CTL_SYNC_OFF 2
  83. #define SYNC_CTL_HSYNC_INV 8
  84. #define SYNC_CTL_VSYNC_INV 0x10
  85. #define SYNC_CTL_HSYNC_OFF 0x20
  86. #define SYNC_CTL_VSYNC_OFF 0x40
  87. #define PLL_M 0x4040
  88. #define PLL_N 0x4044
  89. #define PLL_POSTDIV 0x4048
  90. #define PLL_C 0x404c
  91. /* Hardware cursor */
  92. #define CURSOR_X 0x4078
  93. #define CURSOR_Y 0x407c
  94. #define CURSOR_HOTSPOT 0x4080
  95. #define CURSOR_MODE 0x4084
  96. #define CURSOR_MODE_OFF 0
  97. #define CURSOR_MODE_4BPP 1
  98. #define CURSOR_PIXMAP 0x5000
  99. #define CURSOR_CMAP 0x7400
  100. /* Window attribute table */
  101. #define WAT_FMT 0x4100
  102. #define WAT_FMT_24BIT 0
  103. #define WAT_FMT_16BIT_565 1
  104. #define WAT_FMT_16BIT_1555 2
  105. #define WAT_FMT_32BIT 3 /* 0 vs. 3 is a guess */
  106. #define WAT_FMT_8BIT_332 9
  107. #define WAT_FMT_8BIT 0xa
  108. #define WAT_FMT_NO_CMAP 4 /* ORd in to other values */
  109. #define WAT_CMAP_OFFSET 0x4104 /* 4-bit value gets << 6 */
  110. #define WAT_CTRL 0x4108
  111. #define WAT_CTRL_SEL_B 1 /* select B buffer if 1 */
  112. #define WAT_CTRL_NO_INC 2
  113. #define WAT_GAMMA_CTRL 0x410c
  114. #define WAT_GAMMA_DISABLE 1 /* disables gamma cmap */
  115. #define WAT_OVL_CTRL 0x430c /* controls overlay */
  116. /* Indexed by DFA_PIX_* values */
  117. static const unsigned char watfmt[] = {
  118. WAT_FMT_8BIT, WAT_FMT_16BIT_565, WAT_FMT_16BIT_1555, 0,
  119. WAT_FMT_24BIT, WAT_FMT_32BIT
  120. };
  121. /* Colormap array; 1k entries of 4 bytes each */
  122. #define CMAP 0x6000
  123. #define readreg(par, reg) readl((par)->regs + (reg))
  124. #define writereg(par, reg, val) writel((val), (par)->regs + (reg))
  125. struct gxt4500_par {
  126. void __iomem *regs;
  127. int pixfmt; /* pixel format, see DFA_PIX_* values */
  128. /* PLL parameters */
  129. int refclk_ps; /* ref clock period in picoseconds */
  130. int pll_m; /* ref clock divisor */
  131. int pll_n; /* VCO divisor */
  132. int pll_pd1; /* first post-divisor */
  133. int pll_pd2; /* second post-divisor */
  134. u32 pseudo_palette[16]; /* used in color blits */
  135. };
  136. /* mode requested by user */
  137. static char *mode_option;
  138. /* default mode: 1280x1024 @ 60 Hz, 8 bpp */
  139. static const struct fb_videomode defaultmode = {
  140. .refresh = 60,
  141. .xres = 1280,
  142. .yres = 1024,
  143. .pixclock = 9295,
  144. .left_margin = 248,
  145. .right_margin = 48,
  146. .upper_margin = 38,
  147. .lower_margin = 1,
  148. .hsync_len = 112,
  149. .vsync_len = 3,
  150. .vmode = FB_VMODE_NONINTERLACED
  151. };
  152. /* List of supported cards */
  153. enum gxt_cards {
  154. GXT4500P,
  155. GXT6500P,
  156. GXT4000P,
  157. GXT6000P
  158. };
  159. /* Card-specific information */
  160. static const struct cardinfo {
  161. int refclk_ps; /* period of PLL reference clock in ps */
  162. const char *cardname;
  163. } cardinfo[] = {
  164. [GXT4500P] = { .refclk_ps = 9259, .cardname = "IBM GXT4500P" },
  165. [GXT6500P] = { .refclk_ps = 9259, .cardname = "IBM GXT6500P" },
  166. [GXT4000P] = { .refclk_ps = 40000, .cardname = "IBM GXT4000P" },
  167. [GXT6000P] = { .refclk_ps = 40000, .cardname = "IBM GXT6000P" },
  168. };
  169. /*
  170. * The refclk and VCO dividers appear to use a linear feedback shift
  171. * register, which gets reloaded when it reaches a terminal value, at
  172. * which point the divider output is toggled. Thus one can obtain
  173. * whatever divisor is required by putting the appropriate value into
  174. * the reload register. For a divisor of N, one puts the value from
  175. * the LFSR sequence that comes N-1 places before the terminal value
  176. * into the reload register.
  177. */
  178. static const unsigned char mdivtab[] = {
  179. /* 1 */ 0x3f, 0x00, 0x20, 0x10, 0x28, 0x14, 0x2a, 0x15, 0x0a,
  180. /* 10 */ 0x25, 0x32, 0x19, 0x0c, 0x26, 0x13, 0x09, 0x04, 0x22, 0x11,
  181. /* 20 */ 0x08, 0x24, 0x12, 0x29, 0x34, 0x1a, 0x2d, 0x36, 0x1b, 0x0d,
  182. /* 30 */ 0x06, 0x23, 0x31, 0x38, 0x1c, 0x2e, 0x17, 0x0b, 0x05, 0x02,
  183. /* 40 */ 0x21, 0x30, 0x18, 0x2c, 0x16, 0x2b, 0x35, 0x3a, 0x1d, 0x0e,
  184. /* 50 */ 0x27, 0x33, 0x39, 0x3c, 0x1e, 0x2f, 0x37, 0x3b, 0x3d, 0x3e,
  185. /* 60 */ 0x1f, 0x0f, 0x07, 0x03, 0x01,
  186. };
  187. static const unsigned char ndivtab[] = {
  188. /* 2 */ 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0x78, 0xbc, 0x5e,
  189. /* 10 */ 0x2f, 0x17, 0x0b, 0x85, 0xc2, 0xe1, 0x70, 0x38, 0x9c, 0x4e,
  190. /* 20 */ 0xa7, 0xd3, 0xe9, 0xf4, 0xfa, 0xfd, 0xfe, 0x7f, 0xbf, 0xdf,
  191. /* 30 */ 0xef, 0x77, 0x3b, 0x1d, 0x8e, 0xc7, 0xe3, 0x71, 0xb8, 0xdc,
  192. /* 40 */ 0x6e, 0xb7, 0x5b, 0x2d, 0x16, 0x8b, 0xc5, 0xe2, 0xf1, 0xf8,
  193. /* 50 */ 0xfc, 0x7e, 0x3f, 0x9f, 0xcf, 0x67, 0xb3, 0xd9, 0x6c, 0xb6,
  194. /* 60 */ 0xdb, 0x6d, 0x36, 0x9b, 0x4d, 0x26, 0x13, 0x89, 0xc4, 0x62,
  195. /* 70 */ 0xb1, 0xd8, 0xec, 0xf6, 0xfb, 0x7d, 0xbe, 0x5f, 0xaf, 0x57,
  196. /* 80 */ 0x2b, 0x95, 0x4a, 0x25, 0x92, 0x49, 0xa4, 0x52, 0x29, 0x94,
  197. /* 90 */ 0xca, 0x65, 0xb2, 0x59, 0x2c, 0x96, 0xcb, 0xe5, 0xf2, 0x79,
  198. /* 100 */ 0x3c, 0x1e, 0x0f, 0x07, 0x83, 0x41, 0x20, 0x90, 0x48, 0x24,
  199. /* 110 */ 0x12, 0x09, 0x84, 0x42, 0xa1, 0x50, 0x28, 0x14, 0x8a, 0x45,
  200. /* 120 */ 0xa2, 0xd1, 0xe8, 0x74, 0xba, 0xdd, 0xee, 0xf7, 0x7b, 0x3d,
  201. /* 130 */ 0x9e, 0x4f, 0x27, 0x93, 0xc9, 0xe4, 0x72, 0x39, 0x1c, 0x0e,
  202. /* 140 */ 0x87, 0xc3, 0x61, 0x30, 0x18, 0x8c, 0xc6, 0x63, 0x31, 0x98,
  203. /* 150 */ 0xcc, 0xe6, 0x73, 0xb9, 0x5c, 0x2e, 0x97, 0x4b, 0xa5, 0xd2,
  204. /* 160 */ 0x69,
  205. };
  206. static int calc_pll(int period_ps, struct gxt4500_par *par)
  207. {
  208. int m, n, pdiv1, pdiv2, postdiv;
  209. int pll_period, best_error, t, intf;
  210. /* only deal with range 5MHz - 300MHz */
  211. if (period_ps < 3333 || period_ps > 200000)
  212. return -1;
  213. best_error = 1000000;
  214. for (pdiv1 = 1; pdiv1 <= 8; ++pdiv1) {
  215. for (pdiv2 = 1; pdiv2 <= pdiv1; ++pdiv2) {
  216. postdiv = pdiv1 * pdiv2;
  217. pll_period = DIV_ROUND_UP(period_ps, postdiv);
  218. /* keep pll in range 350..600 MHz */
  219. if (pll_period < 1666 || pll_period > 2857)
  220. continue;
  221. for (m = 1; m <= 64; ++m) {
  222. intf = m * par->refclk_ps;
  223. if (intf > 500000)
  224. break;
  225. n = intf * postdiv / period_ps;
  226. if (n < 3 || n > 160)
  227. continue;
  228. t = par->refclk_ps * m * postdiv / n;
  229. t -= period_ps;
  230. if (t >= 0 && t < best_error) {
  231. par->pll_m = m;
  232. par->pll_n = n;
  233. par->pll_pd1 = pdiv1;
  234. par->pll_pd2 = pdiv2;
  235. best_error = t;
  236. }
  237. }
  238. }
  239. }
  240. if (best_error == 1000000)
  241. return -1;
  242. return 0;
  243. }
  244. static int calc_pixclock(struct gxt4500_par *par)
  245. {
  246. return par->refclk_ps * par->pll_m * par->pll_pd1 * par->pll_pd2
  247. / par->pll_n;
  248. }
  249. static int gxt4500_var_to_par(struct fb_var_screeninfo *var,
  250. struct gxt4500_par *par)
  251. {
  252. if (var->xres + var->xoffset > var->xres_virtual ||
  253. var->yres + var->yoffset > var->yres_virtual ||
  254. var->xres_virtual > 4096)
  255. return -EINVAL;
  256. if ((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
  257. return -EINVAL;
  258. if (calc_pll(var->pixclock, par) < 0)
  259. return -EINVAL;
  260. switch (var->bits_per_pixel) {
  261. case 32:
  262. if (var->transp.length)
  263. par->pixfmt = DFA_PIX_32BIT;
  264. else
  265. par->pixfmt = DFA_PIX_24BIT;
  266. break;
  267. case 24:
  268. par->pixfmt = DFA_PIX_24BIT;
  269. break;
  270. case 16:
  271. if (var->green.length == 5)
  272. par->pixfmt = DFA_PIX_16BIT_1555;
  273. else
  274. par->pixfmt = DFA_PIX_16BIT_565;
  275. break;
  276. case 8:
  277. par->pixfmt = DFA_PIX_8BIT;
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. return 0;
  283. }
  284. static const struct fb_bitfield eightbits = {0, 8};
  285. static const struct fb_bitfield nobits = {0, 0};
  286. static void gxt4500_unpack_pixfmt(struct fb_var_screeninfo *var,
  287. int pixfmt)
  288. {
  289. var->bits_per_pixel = pixsize[pixfmt] * 8;
  290. var->red = eightbits;
  291. var->green = eightbits;
  292. var->blue = eightbits;
  293. var->transp = nobits;
  294. switch (pixfmt) {
  295. case DFA_PIX_16BIT_565:
  296. var->red.length = 5;
  297. var->green.length = 6;
  298. var->blue.length = 5;
  299. break;
  300. case DFA_PIX_16BIT_1555:
  301. var->red.length = 5;
  302. var->green.length = 5;
  303. var->blue.length = 5;
  304. var->transp.length = 1;
  305. break;
  306. case DFA_PIX_32BIT:
  307. var->transp.length = 8;
  308. break;
  309. }
  310. if (pixfmt != DFA_PIX_8BIT) {
  311. var->green.offset = var->red.length;
  312. var->blue.offset = var->green.offset + var->green.length;
  313. if (var->transp.length)
  314. var->transp.offset =
  315. var->blue.offset + var->blue.length;
  316. }
  317. }
  318. static int gxt4500_check_var(struct fb_var_screeninfo *var,
  319. struct fb_info *info)
  320. {
  321. struct gxt4500_par par;
  322. int err;
  323. par = *(struct gxt4500_par *)info->par;
  324. err = gxt4500_var_to_par(var, &par);
  325. if (!err) {
  326. var->pixclock = calc_pixclock(&par);
  327. gxt4500_unpack_pixfmt(var, par.pixfmt);
  328. }
  329. return err;
  330. }
  331. static int gxt4500_set_par(struct fb_info *info)
  332. {
  333. struct gxt4500_par *par = info->par;
  334. struct fb_var_screeninfo *var = &info->var;
  335. int err;
  336. u32 ctrlreg, tmp;
  337. unsigned int dfa_ctl, pixfmt, stride;
  338. unsigned int wid_tiles, i;
  339. unsigned int prefetch_pix, htot;
  340. struct gxt4500_par save_par;
  341. save_par = *par;
  342. err = gxt4500_var_to_par(var, par);
  343. if (err) {
  344. *par = save_par;
  345. return err;
  346. }
  347. /* turn off DTG for now */
  348. ctrlreg = readreg(par, DTG_CONTROL);
  349. ctrlreg &= ~(DTG_CTL_ENABLE | DTG_CTL_SCREEN_REFRESH);
  350. writereg(par, DTG_CONTROL, ctrlreg);
  351. /* set PLL registers */
  352. tmp = readreg(par, PLL_C) & ~0x7f;
  353. if (par->pll_n < 38)
  354. tmp |= 0x29;
  355. if (par->pll_n < 69)
  356. tmp |= 0x35;
  357. else if (par->pll_n < 100)
  358. tmp |= 0x76;
  359. else
  360. tmp |= 0x7e;
  361. writereg(par, PLL_C, tmp);
  362. writereg(par, PLL_M, mdivtab[par->pll_m - 1]);
  363. writereg(par, PLL_N, ndivtab[par->pll_n - 2]);
  364. tmp = ((8 - par->pll_pd2) << 3) | (8 - par->pll_pd1);
  365. if (par->pll_pd1 == 8 || par->pll_pd2 == 8) {
  366. /* work around erratum */
  367. writereg(par, PLL_POSTDIV, tmp | 0x9);
  368. udelay(1);
  369. }
  370. writereg(par, PLL_POSTDIV, tmp);
  371. msleep(20);
  372. /* turn off hardware cursor */
  373. writereg(par, CURSOR_MODE, CURSOR_MODE_OFF);
  374. /* reset raster engine */
  375. writereg(par, CTRL_REG0, CR0_RASTER_RESET | (CR0_RASTER_RESET << 16));
  376. udelay(10);
  377. writereg(par, CTRL_REG0, CR0_RASTER_RESET << 16);
  378. /* set display timing generator registers */
  379. htot = var->xres + var->left_margin + var->right_margin +
  380. var->hsync_len;
  381. writereg(par, DTG_HORIZ_EXTENT, htot - 1);
  382. writereg(par, DTG_HORIZ_DISPLAY, var->xres - 1);
  383. writereg(par, DTG_HSYNC_START, var->xres + var->right_margin - 1);
  384. writereg(par, DTG_HSYNC_END,
  385. var->xres + var->right_margin + var->hsync_len - 1);
  386. writereg(par, DTG_HSYNC_END_COMP,
  387. var->xres + var->right_margin + var->hsync_len - 1);
  388. writereg(par, DTG_VERT_EXTENT,
  389. var->yres + var->upper_margin + var->lower_margin +
  390. var->vsync_len - 1);
  391. writereg(par, DTG_VERT_DISPLAY, var->yres - 1);
  392. writereg(par, DTG_VSYNC_START, var->yres + var->lower_margin - 1);
  393. writereg(par, DTG_VSYNC_END,
  394. var->yres + var->lower_margin + var->vsync_len - 1);
  395. prefetch_pix = 3300000 / var->pixclock;
  396. if (prefetch_pix >= htot)
  397. prefetch_pix = htot - 1;
  398. writereg(par, DTG_VERT_SHORT, htot - prefetch_pix - 1);
  399. ctrlreg |= DTG_CTL_ENABLE | DTG_CTL_SCREEN_REFRESH;
  400. writereg(par, DTG_CONTROL, ctrlreg);
  401. /* calculate stride in DFA aperture */
  402. if (var->xres_virtual > 2048) {
  403. stride = 4096;
  404. dfa_ctl = DFA_FB_STRIDE_4k;
  405. } else if (var->xres_virtual > 1024) {
  406. stride = 2048;
  407. dfa_ctl = DFA_FB_STRIDE_2k;
  408. } else {
  409. stride = 1024;
  410. dfa_ctl = DFA_FB_STRIDE_1k;
  411. }
  412. /* Set up framebuffer definition */
  413. wid_tiles = (var->xres_virtual + 63) >> 6;
  414. /* XXX add proper FB allocation here someday */
  415. writereg(par, FB_AB_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
  416. writereg(par, REFRESH_AB_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
  417. writereg(par, FB_CD_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
  418. writereg(par, REFRESH_CD_CTRL, FB_CTRL_TYPE | (wid_tiles << 16) | 0);
  419. writereg(par, REFRESH_START, (var->xoffset << 16) | var->yoffset);
  420. writereg(par, REFRESH_SIZE, (var->xres << 16) | var->yres);
  421. /* Set up framebuffer access by CPU */
  422. pixfmt = par->pixfmt;
  423. dfa_ctl |= DFA_FB_ENABLE | pixfmt;
  424. writereg(par, DFA_FB_A, dfa_ctl);
  425. /*
  426. * Set up window attribute table.
  427. * We set all WAT entries the same so it doesn't matter what the
  428. * window ID (WID) plane contains.
  429. */
  430. for (i = 0; i < 32; ++i) {
  431. writereg(par, WAT_FMT + (i << 4), watfmt[pixfmt]);
  432. writereg(par, WAT_CMAP_OFFSET + (i << 4), 0);
  433. writereg(par, WAT_CTRL + (i << 4), 0);
  434. writereg(par, WAT_GAMMA_CTRL + (i << 4), WAT_GAMMA_DISABLE);
  435. }
  436. /* Set sync polarity etc. */
  437. ctrlreg = readreg(par, SYNC_CTL) &
  438. ~(SYNC_CTL_SYNC_ON_RGB | SYNC_CTL_HSYNC_INV |
  439. SYNC_CTL_VSYNC_INV);
  440. if (var->sync & FB_SYNC_ON_GREEN)
  441. ctrlreg |= SYNC_CTL_SYNC_ON_RGB;
  442. if (!(var->sync & FB_SYNC_HOR_HIGH_ACT))
  443. ctrlreg |= SYNC_CTL_HSYNC_INV;
  444. if (!(var->sync & FB_SYNC_VERT_HIGH_ACT))
  445. ctrlreg |= SYNC_CTL_VSYNC_INV;
  446. writereg(par, SYNC_CTL, ctrlreg);
  447. info->fix.line_length = stride * pixsize[pixfmt];
  448. info->fix.visual = (pixfmt == DFA_PIX_8BIT)? FB_VISUAL_PSEUDOCOLOR:
  449. FB_VISUAL_DIRECTCOLOR;
  450. return 0;
  451. }
  452. static int gxt4500_setcolreg(unsigned int reg, unsigned int red,
  453. unsigned int green, unsigned int blue,
  454. unsigned int transp, struct fb_info *info)
  455. {
  456. u32 cmap_entry;
  457. struct gxt4500_par *par = info->par;
  458. if (reg > 1023)
  459. return 1;
  460. cmap_entry = ((transp & 0xff00) << 16) | ((red & 0xff00) << 8) |
  461. (green & 0xff00) | (blue >> 8);
  462. writereg(par, CMAP + reg * 4, cmap_entry);
  463. if (reg < 16 && par->pixfmt != DFA_PIX_8BIT) {
  464. u32 *pal = info->pseudo_palette;
  465. u32 val = reg;
  466. switch (par->pixfmt) {
  467. case DFA_PIX_16BIT_565:
  468. val |= (reg << 11) | (reg << 6);
  469. break;
  470. case DFA_PIX_16BIT_1555:
  471. val |= (reg << 10) | (reg << 5);
  472. break;
  473. case DFA_PIX_32BIT:
  474. val |= (reg << 24);
  475. /* fall through */
  476. case DFA_PIX_24BIT:
  477. val |= (reg << 16) | (reg << 8);
  478. break;
  479. }
  480. pal[reg] = val;
  481. }
  482. return 0;
  483. }
  484. static int gxt4500_pan_display(struct fb_var_screeninfo *var,
  485. struct fb_info *info)
  486. {
  487. struct gxt4500_par *par = info->par;
  488. if (var->xoffset & 7)
  489. return -EINVAL;
  490. if (var->xoffset + info->var.xres > info->var.xres_virtual ||
  491. var->yoffset + info->var.yres > info->var.yres_virtual)
  492. return -EINVAL;
  493. writereg(par, REFRESH_START, (var->xoffset << 16) | var->yoffset);
  494. return 0;
  495. }
  496. static int gxt4500_blank(int blank, struct fb_info *info)
  497. {
  498. struct gxt4500_par *par = info->par;
  499. int ctrl, dctl;
  500. ctrl = readreg(par, SYNC_CTL);
  501. ctrl &= ~(SYNC_CTL_SYNC_OFF | SYNC_CTL_HSYNC_OFF | SYNC_CTL_VSYNC_OFF);
  502. dctl = readreg(par, DISP_CTL);
  503. dctl |= DISP_CTL_OFF;
  504. switch (blank) {
  505. case FB_BLANK_UNBLANK:
  506. dctl &= ~DISP_CTL_OFF;
  507. break;
  508. case FB_BLANK_POWERDOWN:
  509. ctrl |= SYNC_CTL_SYNC_OFF;
  510. break;
  511. case FB_BLANK_HSYNC_SUSPEND:
  512. ctrl |= SYNC_CTL_HSYNC_OFF;
  513. break;
  514. case FB_BLANK_VSYNC_SUSPEND:
  515. ctrl |= SYNC_CTL_VSYNC_OFF;
  516. break;
  517. default: ;
  518. }
  519. writereg(par, SYNC_CTL, ctrl);
  520. writereg(par, DISP_CTL, dctl);
  521. return 0;
  522. }
  523. static const struct fb_fix_screeninfo gxt4500_fix = {
  524. .id = "IBM GXT4500P",
  525. .type = FB_TYPE_PACKED_PIXELS,
  526. .visual = FB_VISUAL_PSEUDOCOLOR,
  527. .xpanstep = 8,
  528. .ypanstep = 1,
  529. .mmio_len = 0x20000,
  530. };
  531. static struct fb_ops gxt4500_ops = {
  532. .owner = THIS_MODULE,
  533. .fb_check_var = gxt4500_check_var,
  534. .fb_set_par = gxt4500_set_par,
  535. .fb_setcolreg = gxt4500_setcolreg,
  536. .fb_pan_display = gxt4500_pan_display,
  537. .fb_blank = gxt4500_blank,
  538. .fb_fillrect = cfb_fillrect,
  539. .fb_copyarea = cfb_copyarea,
  540. .fb_imageblit = cfb_imageblit,
  541. };
  542. /* PCI functions */
  543. static int gxt4500_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  544. {
  545. int err;
  546. unsigned long reg_phys, fb_phys;
  547. struct gxt4500_par *par;
  548. struct fb_info *info;
  549. struct fb_var_screeninfo var;
  550. enum gxt_cards cardtype;
  551. err = pci_enable_device(pdev);
  552. if (err) {
  553. dev_err(&pdev->dev, "gxt4500: cannot enable PCI device: %d\n",
  554. err);
  555. return err;
  556. }
  557. reg_phys = pci_resource_start(pdev, 0);
  558. if (!request_mem_region(reg_phys, pci_resource_len(pdev, 0),
  559. "gxt4500 regs")) {
  560. dev_err(&pdev->dev, "gxt4500: cannot get registers\n");
  561. goto err_nodev;
  562. }
  563. fb_phys = pci_resource_start(pdev, 1);
  564. if (!request_mem_region(fb_phys, pci_resource_len(pdev, 1),
  565. "gxt4500 FB")) {
  566. dev_err(&pdev->dev, "gxt4500: cannot get framebuffer\n");
  567. goto err_free_regs;
  568. }
  569. info = framebuffer_alloc(sizeof(struct gxt4500_par), &pdev->dev);
  570. if (!info) {
  571. dev_err(&pdev->dev, "gxt4500: cannot alloc FB info record\n");
  572. goto err_free_fb;
  573. }
  574. par = info->par;
  575. cardtype = ent->driver_data;
  576. par->refclk_ps = cardinfo[cardtype].refclk_ps;
  577. info->fix = gxt4500_fix;
  578. strlcpy(info->fix.id, cardinfo[cardtype].cardname,
  579. sizeof(info->fix.id));
  580. info->pseudo_palette = par->pseudo_palette;
  581. info->fix.mmio_start = reg_phys;
  582. par->regs = pci_ioremap_bar(pdev, 0);
  583. if (!par->regs) {
  584. dev_err(&pdev->dev, "gxt4500: cannot map registers\n");
  585. goto err_free_all;
  586. }
  587. info->fix.smem_start = fb_phys;
  588. info->fix.smem_len = pci_resource_len(pdev, 1);
  589. info->screen_base = pci_ioremap_bar(pdev, 1);
  590. if (!info->screen_base) {
  591. dev_err(&pdev->dev, "gxt4500: cannot map framebuffer\n");
  592. goto err_unmap_regs;
  593. }
  594. pci_set_drvdata(pdev, info);
  595. /* Set byte-swapping for DFA aperture for all pixel sizes */
  596. pci_write_config_dword(pdev, CFG_ENDIAN0, 0x333300);
  597. info->fbops = &gxt4500_ops;
  598. info->flags = FBINFO_FLAG_DEFAULT;
  599. err = fb_alloc_cmap(&info->cmap, 256, 0);
  600. if (err) {
  601. dev_err(&pdev->dev, "gxt4500: cannot allocate cmap\n");
  602. goto err_unmap_all;
  603. }
  604. gxt4500_blank(FB_BLANK_UNBLANK, info);
  605. if (!fb_find_mode(&var, info, mode_option, NULL, 0, &defaultmode, 8)) {
  606. dev_err(&pdev->dev, "gxt4500: cannot find valid video mode\n");
  607. goto err_free_cmap;
  608. }
  609. info->var = var;
  610. if (gxt4500_set_par(info)) {
  611. printk(KERN_ERR "gxt4500: cannot set video mode\n");
  612. goto err_free_cmap;
  613. }
  614. if (register_framebuffer(info) < 0) {
  615. dev_err(&pdev->dev, "gxt4500: cannot register framebuffer\n");
  616. goto err_free_cmap;
  617. }
  618. fb_info(info, "%s frame buffer device\n", info->fix.id);
  619. return 0;
  620. err_free_cmap:
  621. fb_dealloc_cmap(&info->cmap);
  622. err_unmap_all:
  623. iounmap(info->screen_base);
  624. err_unmap_regs:
  625. iounmap(par->regs);
  626. err_free_all:
  627. framebuffer_release(info);
  628. err_free_fb:
  629. release_mem_region(fb_phys, pci_resource_len(pdev, 1));
  630. err_free_regs:
  631. release_mem_region(reg_phys, pci_resource_len(pdev, 0));
  632. err_nodev:
  633. return -ENODEV;
  634. }
  635. static void gxt4500_remove(struct pci_dev *pdev)
  636. {
  637. struct fb_info *info = pci_get_drvdata(pdev);
  638. struct gxt4500_par *par;
  639. if (!info)
  640. return;
  641. par = info->par;
  642. unregister_framebuffer(info);
  643. fb_dealloc_cmap(&info->cmap);
  644. iounmap(par->regs);
  645. iounmap(info->screen_base);
  646. release_mem_region(pci_resource_start(pdev, 0),
  647. pci_resource_len(pdev, 0));
  648. release_mem_region(pci_resource_start(pdev, 1),
  649. pci_resource_len(pdev, 1));
  650. framebuffer_release(info);
  651. }
  652. /* supported chipsets */
  653. static const struct pci_device_id gxt4500_pci_tbl[] = {
  654. { PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT4500P),
  655. .driver_data = GXT4500P },
  656. { PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT6500P),
  657. .driver_data = GXT6500P },
  658. { PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT4000P),
  659. .driver_data = GXT4000P },
  660. { PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_GXT6000P),
  661. .driver_data = GXT6000P },
  662. { 0 }
  663. };
  664. MODULE_DEVICE_TABLE(pci, gxt4500_pci_tbl);
  665. static struct pci_driver gxt4500_driver = {
  666. .name = "gxt4500",
  667. .id_table = gxt4500_pci_tbl,
  668. .probe = gxt4500_probe,
  669. .remove = gxt4500_remove,
  670. };
  671. static int gxt4500_init(void)
  672. {
  673. #ifndef MODULE
  674. if (fb_get_options("gxt4500", &mode_option))
  675. return -ENODEV;
  676. #endif
  677. return pci_register_driver(&gxt4500_driver);
  678. }
  679. module_init(gxt4500_init);
  680. static void __exit gxt4500_exit(void)
  681. {
  682. pci_unregister_driver(&gxt4500_driver);
  683. }
  684. module_exit(gxt4500_exit);
  685. MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  686. MODULE_DESCRIPTION("FBDev driver for IBM GXT4500P/6500P and GXT4000P/6000P");
  687. MODULE_LICENSE("GPL");
  688. module_param(mode_option, charp, 0);
  689. MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\"");