gbefb.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * SGI GBE frame buffer driver
  3. *
  4. * Copyright (C) 1999 Silicon Graphics, Inc. - Jeffrey Newquist
  5. * Copyright (C) 2002 Vivien Chappelier <vivien.chappelier@linux-mips.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/errno.h>
  15. #include <linux/gfp.h>
  16. #include <linux/fb.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #ifdef CONFIG_X86
  23. #include <asm/mtrr.h>
  24. #endif
  25. #ifdef CONFIG_MIPS
  26. #include <asm/addrspace.h>
  27. #endif
  28. #include <asm/byteorder.h>
  29. #include <asm/io.h>
  30. #include <asm/tlbflush.h>
  31. #include <video/gbe.h>
  32. static struct sgi_gbe *gbe;
  33. struct gbefb_par {
  34. struct fb_var_screeninfo var;
  35. struct gbe_timing_info timing;
  36. int valid;
  37. };
  38. #ifdef CONFIG_SGI_IP32
  39. #define GBE_BASE 0x16000000 /* SGI O2 */
  40. #endif
  41. #ifdef CONFIG_X86_VISWS
  42. #define GBE_BASE 0xd0000000 /* SGI Visual Workstation */
  43. #endif
  44. /* macro for fastest write-though access to the framebuffer */
  45. #ifdef CONFIG_MIPS
  46. #ifdef CONFIG_CPU_R10000
  47. #define pgprot_fb(_prot) (((_prot) & (~_CACHE_MASK)) | _CACHE_UNCACHED_ACCELERATED)
  48. #else
  49. #define pgprot_fb(_prot) (((_prot) & (~_CACHE_MASK)) | _CACHE_CACHABLE_NO_WA)
  50. #endif
  51. #endif
  52. #ifdef CONFIG_X86
  53. #define pgprot_fb(_prot) ((_prot) | _PAGE_PCD)
  54. #endif
  55. /*
  56. * RAM we reserve for the frame buffer. This defines the maximum screen
  57. * size
  58. */
  59. #if CONFIG_FB_GBE_MEM > 8
  60. #error GBE Framebuffer cannot use more than 8MB of memory
  61. #endif
  62. #define TILE_SHIFT 16
  63. #define TILE_SIZE (1 << TILE_SHIFT)
  64. #define TILE_MASK (TILE_SIZE - 1)
  65. static unsigned int gbe_mem_size = CONFIG_FB_GBE_MEM * 1024*1024;
  66. static void *gbe_mem;
  67. static dma_addr_t gbe_dma_addr;
  68. static unsigned long gbe_mem_phys;
  69. static struct {
  70. uint16_t *cpu;
  71. dma_addr_t dma;
  72. } gbe_tiles;
  73. static int gbe_revision;
  74. static int ypan, ywrap;
  75. static uint32_t pseudo_palette[16];
  76. static uint32_t gbe_cmap[256];
  77. static int gbe_turned_on; /* 0 turned off, 1 turned on */
  78. static char *mode_option __devinitdata = NULL;
  79. /* default CRT mode */
  80. static struct fb_var_screeninfo default_var_CRT __devinitdata = {
  81. /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
  82. .xres = 640,
  83. .yres = 480,
  84. .xres_virtual = 640,
  85. .yres_virtual = 480,
  86. .xoffset = 0,
  87. .yoffset = 0,
  88. .bits_per_pixel = 8,
  89. .grayscale = 0,
  90. .red = { 0, 8, 0 },
  91. .green = { 0, 8, 0 },
  92. .blue = { 0, 8, 0 },
  93. .transp = { 0, 0, 0 },
  94. .nonstd = 0,
  95. .activate = 0,
  96. .height = -1,
  97. .width = -1,
  98. .accel_flags = 0,
  99. .pixclock = 39722, /* picoseconds */
  100. .left_margin = 48,
  101. .right_margin = 16,
  102. .upper_margin = 33,
  103. .lower_margin = 10,
  104. .hsync_len = 96,
  105. .vsync_len = 2,
  106. .sync = 0,
  107. .vmode = FB_VMODE_NONINTERLACED,
  108. };
  109. /* default LCD mode */
  110. static struct fb_var_screeninfo default_var_LCD __devinitdata = {
  111. /* 1600x1024, 8 bpp */
  112. .xres = 1600,
  113. .yres = 1024,
  114. .xres_virtual = 1600,
  115. .yres_virtual = 1024,
  116. .xoffset = 0,
  117. .yoffset = 0,
  118. .bits_per_pixel = 8,
  119. .grayscale = 0,
  120. .red = { 0, 8, 0 },
  121. .green = { 0, 8, 0 },
  122. .blue = { 0, 8, 0 },
  123. .transp = { 0, 0, 0 },
  124. .nonstd = 0,
  125. .activate = 0,
  126. .height = -1,
  127. .width = -1,
  128. .accel_flags = 0,
  129. .pixclock = 9353,
  130. .left_margin = 20,
  131. .right_margin = 30,
  132. .upper_margin = 37,
  133. .lower_margin = 3,
  134. .hsync_len = 20,
  135. .vsync_len = 3,
  136. .sync = 0,
  137. .vmode = FB_VMODE_NONINTERLACED
  138. };
  139. /* default modedb mode */
  140. /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
  141. static struct fb_videomode default_mode_CRT __devinitdata = {
  142. .refresh = 60,
  143. .xres = 640,
  144. .yres = 480,
  145. .pixclock = 39722,
  146. .left_margin = 48,
  147. .right_margin = 16,
  148. .upper_margin = 33,
  149. .lower_margin = 10,
  150. .hsync_len = 96,
  151. .vsync_len = 2,
  152. .sync = 0,
  153. .vmode = FB_VMODE_NONINTERLACED,
  154. };
  155. /* 1600x1024 SGI flatpanel 1600sw */
  156. static struct fb_videomode default_mode_LCD __devinitdata = {
  157. /* 1600x1024, 8 bpp */
  158. .xres = 1600,
  159. .yres = 1024,
  160. .pixclock = 9353,
  161. .left_margin = 20,
  162. .right_margin = 30,
  163. .upper_margin = 37,
  164. .lower_margin = 3,
  165. .hsync_len = 20,
  166. .vsync_len = 3,
  167. .vmode = FB_VMODE_NONINTERLACED,
  168. };
  169. static struct fb_videomode *default_mode __devinitdata = &default_mode_CRT;
  170. static struct fb_var_screeninfo *default_var __devinitdata = &default_var_CRT;
  171. static int flat_panel_enabled = 0;
  172. static void gbe_reset(void)
  173. {
  174. /* Turn on dotclock PLL */
  175. gbe->ctrlstat = 0x300aa000;
  176. }
  177. /*
  178. * Function: gbe_turn_off
  179. * Parameters: (None)
  180. * Description: This should turn off the monitor and gbe. This is used
  181. * when switching between the serial console and the graphics
  182. * console.
  183. */
  184. static void gbe_turn_off(void)
  185. {
  186. int i;
  187. unsigned int val, x, y, vpixen_off;
  188. gbe_turned_on = 0;
  189. /* check if pixel counter is on */
  190. val = gbe->vt_xy;
  191. if (GET_GBE_FIELD(VT_XY, FREEZE, val) == 1)
  192. return;
  193. /* turn off DMA */
  194. val = gbe->ovr_control;
  195. SET_GBE_FIELD(OVR_CONTROL, OVR_DMA_ENABLE, val, 0);
  196. gbe->ovr_control = val;
  197. udelay(1000);
  198. val = gbe->frm_control;
  199. SET_GBE_FIELD(FRM_CONTROL, FRM_DMA_ENABLE, val, 0);
  200. gbe->frm_control = val;
  201. udelay(1000);
  202. val = gbe->did_control;
  203. SET_GBE_FIELD(DID_CONTROL, DID_DMA_ENABLE, val, 0);
  204. gbe->did_control = val;
  205. udelay(1000);
  206. /* We have to wait through two vertical retrace periods before
  207. * the pixel DMA is turned off for sure. */
  208. for (i = 0; i < 10000; i++) {
  209. val = gbe->frm_inhwctrl;
  210. if (GET_GBE_FIELD(FRM_INHWCTRL, FRM_DMA_ENABLE, val)) {
  211. udelay(10);
  212. } else {
  213. val = gbe->ovr_inhwctrl;
  214. if (GET_GBE_FIELD(OVR_INHWCTRL, OVR_DMA_ENABLE, val)) {
  215. udelay(10);
  216. } else {
  217. val = gbe->did_inhwctrl;
  218. if (GET_GBE_FIELD(DID_INHWCTRL, DID_DMA_ENABLE, val)) {
  219. udelay(10);
  220. } else
  221. break;
  222. }
  223. }
  224. }
  225. if (i == 10000)
  226. printk(KERN_ERR "gbefb: turn off DMA timed out\n");
  227. /* wait for vpixen_off */
  228. val = gbe->vt_vpixen;
  229. vpixen_off = GET_GBE_FIELD(VT_VPIXEN, VPIXEN_OFF, val);
  230. for (i = 0; i < 100000; i++) {
  231. val = gbe->vt_xy;
  232. x = GET_GBE_FIELD(VT_XY, X, val);
  233. y = GET_GBE_FIELD(VT_XY, Y, val);
  234. if (y < vpixen_off)
  235. break;
  236. udelay(1);
  237. }
  238. if (i == 100000)
  239. printk(KERN_ERR
  240. "gbefb: wait for vpixen_off timed out\n");
  241. for (i = 0; i < 10000; i++) {
  242. val = gbe->vt_xy;
  243. x = GET_GBE_FIELD(VT_XY, X, val);
  244. y = GET_GBE_FIELD(VT_XY, Y, val);
  245. if (y > vpixen_off)
  246. break;
  247. udelay(1);
  248. }
  249. if (i == 10000)
  250. printk(KERN_ERR "gbefb: wait for vpixen_off timed out\n");
  251. /* turn off pixel counter */
  252. val = 0;
  253. SET_GBE_FIELD(VT_XY, FREEZE, val, 1);
  254. gbe->vt_xy = val;
  255. udelay(10000);
  256. for (i = 0; i < 10000; i++) {
  257. val = gbe->vt_xy;
  258. if (GET_GBE_FIELD(VT_XY, FREEZE, val) != 1)
  259. udelay(10);
  260. else
  261. break;
  262. }
  263. if (i == 10000)
  264. printk(KERN_ERR "gbefb: turn off pixel clock timed out\n");
  265. /* turn off dot clock */
  266. val = gbe->dotclock;
  267. SET_GBE_FIELD(DOTCLK, RUN, val, 0);
  268. gbe->dotclock = val;
  269. udelay(10000);
  270. for (i = 0; i < 10000; i++) {
  271. val = gbe->dotclock;
  272. if (GET_GBE_FIELD(DOTCLK, RUN, val))
  273. udelay(10);
  274. else
  275. break;
  276. }
  277. if (i == 10000)
  278. printk(KERN_ERR "gbefb: turn off dotclock timed out\n");
  279. /* reset the frame DMA FIFO */
  280. val = gbe->frm_size_tile;
  281. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_FIFO_RESET, val, 1);
  282. gbe->frm_size_tile = val;
  283. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_FIFO_RESET, val, 0);
  284. gbe->frm_size_tile = val;
  285. }
  286. static void gbe_turn_on(void)
  287. {
  288. unsigned int val, i;
  289. /*
  290. * Check if pixel counter is off, for unknown reason this
  291. * code hangs Visual Workstations
  292. */
  293. if (gbe_revision < 2) {
  294. val = gbe->vt_xy;
  295. if (GET_GBE_FIELD(VT_XY, FREEZE, val) == 0)
  296. return;
  297. }
  298. /* turn on dot clock */
  299. val = gbe->dotclock;
  300. SET_GBE_FIELD(DOTCLK, RUN, val, 1);
  301. gbe->dotclock = val;
  302. udelay(10000);
  303. for (i = 0; i < 10000; i++) {
  304. val = gbe->dotclock;
  305. if (GET_GBE_FIELD(DOTCLK, RUN, val) != 1)
  306. udelay(10);
  307. else
  308. break;
  309. }
  310. if (i == 10000)
  311. printk(KERN_ERR "gbefb: turn on dotclock timed out\n");
  312. /* turn on pixel counter */
  313. val = 0;
  314. SET_GBE_FIELD(VT_XY, FREEZE, val, 0);
  315. gbe->vt_xy = val;
  316. udelay(10000);
  317. for (i = 0; i < 10000; i++) {
  318. val = gbe->vt_xy;
  319. if (GET_GBE_FIELD(VT_XY, FREEZE, val))
  320. udelay(10);
  321. else
  322. break;
  323. }
  324. if (i == 10000)
  325. printk(KERN_ERR "gbefb: turn on pixel clock timed out\n");
  326. /* turn on DMA */
  327. val = gbe->frm_control;
  328. SET_GBE_FIELD(FRM_CONTROL, FRM_DMA_ENABLE, val, 1);
  329. gbe->frm_control = val;
  330. udelay(1000);
  331. for (i = 0; i < 10000; i++) {
  332. val = gbe->frm_inhwctrl;
  333. if (GET_GBE_FIELD(FRM_INHWCTRL, FRM_DMA_ENABLE, val) != 1)
  334. udelay(10);
  335. else
  336. break;
  337. }
  338. if (i == 10000)
  339. printk(KERN_ERR "gbefb: turn on DMA timed out\n");
  340. gbe_turned_on = 1;
  341. }
  342. static void gbe_loadcmap(void)
  343. {
  344. int i, j;
  345. for (i = 0; i < 256; i++) {
  346. for (j = 0; j < 1000 && gbe->cm_fifo >= 63; j++)
  347. udelay(10);
  348. if (j == 1000)
  349. printk(KERN_ERR "gbefb: cmap FIFO timeout\n");
  350. gbe->cmap[i] = gbe_cmap[i];
  351. }
  352. }
  353. /*
  354. * Blank the display.
  355. */
  356. static int gbefb_blank(int blank, struct fb_info *info)
  357. {
  358. /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
  359. switch (blank) {
  360. case FB_BLANK_UNBLANK: /* unblank */
  361. gbe_turn_on();
  362. gbe_loadcmap();
  363. break;
  364. case FB_BLANK_NORMAL: /* blank */
  365. gbe_turn_off();
  366. break;
  367. default:
  368. /* Nothing */
  369. break;
  370. }
  371. return 0;
  372. }
  373. /*
  374. * Setup flatpanel related registers.
  375. */
  376. static void gbefb_setup_flatpanel(struct gbe_timing_info *timing)
  377. {
  378. int fp_wid, fp_hgt, fp_vbs, fp_vbe;
  379. u32 outputVal = 0;
  380. SET_GBE_FIELD(VT_FLAGS, HDRV_INVERT, outputVal,
  381. (timing->flags & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1);
  382. SET_GBE_FIELD(VT_FLAGS, VDRV_INVERT, outputVal,
  383. (timing->flags & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1);
  384. gbe->vt_flags = outputVal;
  385. /* Turn on the flat panel */
  386. fp_wid = 1600;
  387. fp_hgt = 1024;
  388. fp_vbs = 0;
  389. fp_vbe = 1600;
  390. timing->pll_m = 4;
  391. timing->pll_n = 1;
  392. timing->pll_p = 0;
  393. outputVal = 0;
  394. SET_GBE_FIELD(FP_DE, ON, outputVal, fp_vbs);
  395. SET_GBE_FIELD(FP_DE, OFF, outputVal, fp_vbe);
  396. gbe->fp_de = outputVal;
  397. outputVal = 0;
  398. SET_GBE_FIELD(FP_HDRV, OFF, outputVal, fp_wid);
  399. gbe->fp_hdrv = outputVal;
  400. outputVal = 0;
  401. SET_GBE_FIELD(FP_VDRV, ON, outputVal, 1);
  402. SET_GBE_FIELD(FP_VDRV, OFF, outputVal, fp_hgt + 1);
  403. gbe->fp_vdrv = outputVal;
  404. }
  405. struct gbe_pll_info {
  406. int clock_rate;
  407. int fvco_min;
  408. int fvco_max;
  409. };
  410. static struct gbe_pll_info gbe_pll_table[2] = {
  411. { 20, 80, 220 },
  412. { 27, 80, 220 },
  413. };
  414. static int compute_gbe_timing(struct fb_var_screeninfo *var,
  415. struct gbe_timing_info *timing)
  416. {
  417. int pll_m, pll_n, pll_p, error, best_m, best_n, best_p, best_error;
  418. int pixclock;
  419. struct gbe_pll_info *gbe_pll;
  420. if (gbe_revision < 2)
  421. gbe_pll = &gbe_pll_table[0];
  422. else
  423. gbe_pll = &gbe_pll_table[1];
  424. /* Determine valid resolution and timing
  425. * GBE crystal runs at 20Mhz or 27Mhz
  426. * pll_m, pll_n, pll_p define the following frequencies
  427. * fvco = pll_m * 20Mhz / pll_n
  428. * fout = fvco / (2**pll_p) */
  429. best_error = 1000000000;
  430. best_n = best_m = best_p = 0;
  431. for (pll_p = 0; pll_p < 4; pll_p++)
  432. for (pll_m = 1; pll_m < 256; pll_m++)
  433. for (pll_n = 1; pll_n < 64; pll_n++) {
  434. pixclock = (1000000 / gbe_pll->clock_rate) *
  435. (pll_n << pll_p) / pll_m;
  436. error = var->pixclock - pixclock;
  437. if (error < 0)
  438. error = -error;
  439. if (error < best_error &&
  440. pll_m / pll_n >
  441. gbe_pll->fvco_min / gbe_pll->clock_rate &&
  442. pll_m / pll_n <
  443. gbe_pll->fvco_max / gbe_pll->clock_rate) {
  444. best_error = error;
  445. best_m = pll_m;
  446. best_n = pll_n;
  447. best_p = pll_p;
  448. }
  449. }
  450. if (!best_n || !best_m)
  451. return -EINVAL; /* Resolution to high */
  452. pixclock = (1000000 / gbe_pll->clock_rate) *
  453. (best_n << best_p) / best_m;
  454. /* set video timing information */
  455. if (timing) {
  456. timing->width = var->xres;
  457. timing->height = var->yres;
  458. timing->pll_m = best_m;
  459. timing->pll_n = best_n;
  460. timing->pll_p = best_p;
  461. timing->cfreq = gbe_pll->clock_rate * 1000 * timing->pll_m /
  462. (timing->pll_n << timing->pll_p);
  463. timing->htotal = var->left_margin + var->xres +
  464. var->right_margin + var->hsync_len;
  465. timing->vtotal = var->upper_margin + var->yres +
  466. var->lower_margin + var->vsync_len;
  467. timing->fields_sec = 1000 * timing->cfreq / timing->htotal *
  468. 1000 / timing->vtotal;
  469. timing->hblank_start = var->xres;
  470. timing->vblank_start = var->yres;
  471. timing->hblank_end = timing->htotal;
  472. timing->hsync_start = var->xres + var->right_margin + 1;
  473. timing->hsync_end = timing->hsync_start + var->hsync_len;
  474. timing->vblank_end = timing->vtotal;
  475. timing->vsync_start = var->yres + var->lower_margin + 1;
  476. timing->vsync_end = timing->vsync_start + var->vsync_len;
  477. }
  478. return pixclock;
  479. }
  480. static void gbe_set_timing_info(struct gbe_timing_info *timing)
  481. {
  482. int temp;
  483. unsigned int val;
  484. /* setup dot clock PLL */
  485. val = 0;
  486. SET_GBE_FIELD(DOTCLK, M, val, timing->pll_m - 1);
  487. SET_GBE_FIELD(DOTCLK, N, val, timing->pll_n - 1);
  488. SET_GBE_FIELD(DOTCLK, P, val, timing->pll_p);
  489. SET_GBE_FIELD(DOTCLK, RUN, val, 0); /* do not start yet */
  490. gbe->dotclock = val;
  491. udelay(10000);
  492. /* setup pixel counter */
  493. val = 0;
  494. SET_GBE_FIELD(VT_XYMAX, MAXX, val, timing->htotal);
  495. SET_GBE_FIELD(VT_XYMAX, MAXY, val, timing->vtotal);
  496. gbe->vt_xymax = val;
  497. /* setup video timing signals */
  498. val = 0;
  499. SET_GBE_FIELD(VT_VSYNC, VSYNC_ON, val, timing->vsync_start);
  500. SET_GBE_FIELD(VT_VSYNC, VSYNC_OFF, val, timing->vsync_end);
  501. gbe->vt_vsync = val;
  502. val = 0;
  503. SET_GBE_FIELD(VT_HSYNC, HSYNC_ON, val, timing->hsync_start);
  504. SET_GBE_FIELD(VT_HSYNC, HSYNC_OFF, val, timing->hsync_end);
  505. gbe->vt_hsync = val;
  506. val = 0;
  507. SET_GBE_FIELD(VT_VBLANK, VBLANK_ON, val, timing->vblank_start);
  508. SET_GBE_FIELD(VT_VBLANK, VBLANK_OFF, val, timing->vblank_end);
  509. gbe->vt_vblank = val;
  510. val = 0;
  511. SET_GBE_FIELD(VT_HBLANK, HBLANK_ON, val,
  512. timing->hblank_start - 5);
  513. SET_GBE_FIELD(VT_HBLANK, HBLANK_OFF, val,
  514. timing->hblank_end - 3);
  515. gbe->vt_hblank = val;
  516. /* setup internal timing signals */
  517. val = 0;
  518. SET_GBE_FIELD(VT_VCMAP, VCMAP_ON, val, timing->vblank_start);
  519. SET_GBE_FIELD(VT_VCMAP, VCMAP_OFF, val, timing->vblank_end);
  520. gbe->vt_vcmap = val;
  521. val = 0;
  522. SET_GBE_FIELD(VT_HCMAP, HCMAP_ON, val, timing->hblank_start);
  523. SET_GBE_FIELD(VT_HCMAP, HCMAP_OFF, val, timing->hblank_end);
  524. gbe->vt_hcmap = val;
  525. val = 0;
  526. temp = timing->vblank_start - timing->vblank_end - 1;
  527. if (temp > 0)
  528. temp = -temp;
  529. if (flat_panel_enabled)
  530. gbefb_setup_flatpanel(timing);
  531. SET_GBE_FIELD(DID_START_XY, DID_STARTY, val, (u32) temp);
  532. if (timing->hblank_end >= 20)
  533. SET_GBE_FIELD(DID_START_XY, DID_STARTX, val,
  534. timing->hblank_end - 20);
  535. else
  536. SET_GBE_FIELD(DID_START_XY, DID_STARTX, val,
  537. timing->htotal - (20 - timing->hblank_end));
  538. gbe->did_start_xy = val;
  539. val = 0;
  540. SET_GBE_FIELD(CRS_START_XY, CRS_STARTY, val, (u32) (temp + 1));
  541. if (timing->hblank_end >= GBE_CRS_MAGIC)
  542. SET_GBE_FIELD(CRS_START_XY, CRS_STARTX, val,
  543. timing->hblank_end - GBE_CRS_MAGIC);
  544. else
  545. SET_GBE_FIELD(CRS_START_XY, CRS_STARTX, val,
  546. timing->htotal - (GBE_CRS_MAGIC -
  547. timing->hblank_end));
  548. gbe->crs_start_xy = val;
  549. val = 0;
  550. SET_GBE_FIELD(VC_START_XY, VC_STARTY, val, (u32) temp);
  551. SET_GBE_FIELD(VC_START_XY, VC_STARTX, val, timing->hblank_end - 4);
  552. gbe->vc_start_xy = val;
  553. val = 0;
  554. temp = timing->hblank_end - GBE_PIXEN_MAGIC_ON;
  555. if (temp < 0)
  556. temp += timing->htotal; /* allow blank to wrap around */
  557. SET_GBE_FIELD(VT_HPIXEN, HPIXEN_ON, val, temp);
  558. SET_GBE_FIELD(VT_HPIXEN, HPIXEN_OFF, val,
  559. ((temp + timing->width -
  560. GBE_PIXEN_MAGIC_OFF) % timing->htotal));
  561. gbe->vt_hpixen = val;
  562. val = 0;
  563. SET_GBE_FIELD(VT_VPIXEN, VPIXEN_ON, val, timing->vblank_end);
  564. SET_GBE_FIELD(VT_VPIXEN, VPIXEN_OFF, val, timing->vblank_start);
  565. gbe->vt_vpixen = val;
  566. /* turn off sync on green */
  567. val = 0;
  568. SET_GBE_FIELD(VT_FLAGS, SYNC_LOW, val, 1);
  569. gbe->vt_flags = val;
  570. }
  571. /*
  572. * Set the hardware according to 'par'.
  573. */
  574. static int gbefb_set_par(struct fb_info *info)
  575. {
  576. int i;
  577. unsigned int val;
  578. int wholeTilesX, partTilesX, maxPixelsPerTileX;
  579. int height_pix;
  580. int xpmax, ypmax; /* Monitor resolution */
  581. int bytesPerPixel; /* Bytes per pixel */
  582. struct gbefb_par *par = (struct gbefb_par *) info->par;
  583. compute_gbe_timing(&info->var, &par->timing);
  584. bytesPerPixel = info->var.bits_per_pixel / 8;
  585. info->fix.line_length = info->var.xres_virtual * bytesPerPixel;
  586. xpmax = par->timing.width;
  587. ypmax = par->timing.height;
  588. /* turn off GBE */
  589. gbe_turn_off();
  590. /* set timing info */
  591. gbe_set_timing_info(&par->timing);
  592. /* initialize DIDs */
  593. val = 0;
  594. switch (bytesPerPixel) {
  595. case 1:
  596. SET_GBE_FIELD(WID, TYP, val, GBE_CMODE_I8);
  597. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  598. break;
  599. case 2:
  600. SET_GBE_FIELD(WID, TYP, val, GBE_CMODE_ARGB5);
  601. info->fix.visual = FB_VISUAL_TRUECOLOR;
  602. break;
  603. case 4:
  604. SET_GBE_FIELD(WID, TYP, val, GBE_CMODE_RGB8);
  605. info->fix.visual = FB_VISUAL_TRUECOLOR;
  606. break;
  607. }
  608. SET_GBE_FIELD(WID, BUF, val, GBE_BMODE_BOTH);
  609. for (i = 0; i < 32; i++)
  610. gbe->mode_regs[i] = val;
  611. /* Initialize interrupts */
  612. gbe->vt_intr01 = 0xffffffff;
  613. gbe->vt_intr23 = 0xffffffff;
  614. /* HACK:
  615. The GBE hardware uses a tiled memory to screen mapping. Tiles are
  616. blocks of 512x128, 256x128 or 128x128 pixels, respectively for 8bit,
  617. 16bit and 32 bit modes (64 kB). They cover the screen with partial
  618. tiles on the right and/or bottom of the screen if needed.
  619. For example in 640x480 8 bit mode the mapping is:
  620. <-------- 640 ----->
  621. <---- 512 ----><128|384 offscreen>
  622. ^ ^
  623. | 128 [tile 0] [tile 1]
  624. | v
  625. ^
  626. 4 128 [tile 2] [tile 3]
  627. 8 v
  628. 0 ^
  629. 128 [tile 4] [tile 5]
  630. | v
  631. | ^
  632. v 96 [tile 6] [tile 7]
  633. 32 offscreen
  634. Tiles have the advantage that they can be allocated individually in
  635. memory. However, this mapping is not linear at all, which is not
  636. really convenient. In order to support linear addressing, the GBE
  637. DMA hardware is fooled into thinking the screen is only one tile
  638. large and but has a greater height, so that the DMA transfer covers
  639. the same region.
  640. Tiles are still allocated as independent chunks of 64KB of
  641. continuous physical memory and remapped so that the kernel sees the
  642. framebuffer as a continuous virtual memory. The GBE tile table is
  643. set up so that each tile references one of these 64k blocks:
  644. GBE -> tile list framebuffer TLB <------------ CPU
  645. [ tile 0 ] -> [ 64KB ] <- [ 16x 4KB page entries ] ^
  646. ... ... ... linear virtual FB
  647. [ tile n ] -> [ 64KB ] <- [ 16x 4KB page entries ] v
  648. The GBE hardware is then told that the buffer is 512*tweaked_height,
  649. with tweaked_height = real_width*real_height/pixels_per_tile.
  650. Thus the GBE hardware will scan the first tile, filing the first 64k
  651. covered region of the screen, and then will proceed to the next
  652. tile, until the whole screen is covered.
  653. Here is what would happen at 640x480 8bit:
  654. normal tiling linear
  655. ^ 11111111111111112222 11111111111111111111 ^
  656. 128 11111111111111112222 11111111111111111111 102 lines
  657. 11111111111111112222 11111111111111111111 v
  658. V 11111111111111112222 11111111222222222222
  659. 33333333333333334444 22222222222222222222
  660. 33333333333333334444 22222222222222222222
  661. < 512 > < 256 > 102*640+256 = 64k
  662. NOTE: The only mode for which this is not working is 800x600 8bit,
  663. as 800*600/512 = 937.5 which is not integer and thus causes
  664. flickering.
  665. I guess this is not so important as one can use 640x480 8bit or
  666. 800x600 16bit anyway.
  667. */
  668. /* Tell gbe about the tiles table location */
  669. /* tile_ptr -> [ tile 1 ] -> FB mem */
  670. /* [ tile 2 ] -> FB mem */
  671. /* ... */
  672. val = 0;
  673. SET_GBE_FIELD(FRM_CONTROL, FRM_TILE_PTR, val, gbe_tiles.dma >> 9);
  674. SET_GBE_FIELD(FRM_CONTROL, FRM_DMA_ENABLE, val, 0); /* do not start */
  675. SET_GBE_FIELD(FRM_CONTROL, FRM_LINEAR, val, 0);
  676. gbe->frm_control = val;
  677. maxPixelsPerTileX = 512 / bytesPerPixel;
  678. wholeTilesX = 1;
  679. partTilesX = 0;
  680. /* Initialize the framebuffer */
  681. val = 0;
  682. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_WIDTH_TILE, val, wholeTilesX);
  683. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_RHS, val, partTilesX);
  684. switch (bytesPerPixel) {
  685. case 1:
  686. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, val,
  687. GBE_FRM_DEPTH_8);
  688. break;
  689. case 2:
  690. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, val,
  691. GBE_FRM_DEPTH_16);
  692. break;
  693. case 4:
  694. SET_GBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, val,
  695. GBE_FRM_DEPTH_32);
  696. break;
  697. }
  698. gbe->frm_size_tile = val;
  699. /* compute tweaked height */
  700. height_pix = xpmax * ypmax / maxPixelsPerTileX;
  701. val = 0;
  702. SET_GBE_FIELD(FRM_SIZE_PIXEL, FB_HEIGHT_PIX, val, height_pix);
  703. gbe->frm_size_pixel = val;
  704. /* turn off DID and overlay DMA */
  705. gbe->did_control = 0;
  706. gbe->ovr_width_tile = 0;
  707. /* Turn off mouse cursor */
  708. gbe->crs_ctl = 0;
  709. /* Turn on GBE */
  710. gbe_turn_on();
  711. /* Initialize the gamma map */
  712. udelay(10);
  713. for (i = 0; i < 256; i++)
  714. gbe->gmap[i] = (i << 24) | (i << 16) | (i << 8);
  715. /* Initialize the color map */
  716. for (i = 0; i < 256; i++)
  717. gbe_cmap[i] = (i << 8) | (i << 16) | (i << 24);
  718. gbe_loadcmap();
  719. return 0;
  720. }
  721. static void gbefb_encode_fix(struct fb_fix_screeninfo *fix,
  722. struct fb_var_screeninfo *var)
  723. {
  724. memset(fix, 0, sizeof(struct fb_fix_screeninfo));
  725. strcpy(fix->id, "SGI GBE");
  726. fix->smem_start = (unsigned long) gbe_mem;
  727. fix->smem_len = gbe_mem_size;
  728. fix->type = FB_TYPE_PACKED_PIXELS;
  729. fix->type_aux = 0;
  730. fix->accel = FB_ACCEL_NONE;
  731. switch (var->bits_per_pixel) {
  732. case 8:
  733. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  734. break;
  735. default:
  736. fix->visual = FB_VISUAL_TRUECOLOR;
  737. break;
  738. }
  739. fix->ywrapstep = 0;
  740. fix->xpanstep = 0;
  741. fix->ypanstep = 0;
  742. fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
  743. fix->mmio_start = GBE_BASE;
  744. fix->mmio_len = sizeof(struct sgi_gbe);
  745. }
  746. /*
  747. * Set a single color register. The values supplied are already
  748. * rounded down to the hardware's capabilities (according to the
  749. * entries in the var structure). Return != 0 for invalid regno.
  750. */
  751. static int gbefb_setcolreg(unsigned regno, unsigned red, unsigned green,
  752. unsigned blue, unsigned transp,
  753. struct fb_info *info)
  754. {
  755. int i;
  756. if (regno > 255)
  757. return 1;
  758. red >>= 8;
  759. green >>= 8;
  760. blue >>= 8;
  761. if (info->var.bits_per_pixel <= 8) {
  762. gbe_cmap[regno] = (red << 24) | (green << 16) | (blue << 8);
  763. if (gbe_turned_on) {
  764. /* wait for the color map FIFO to have a free entry */
  765. for (i = 0; i < 1000 && gbe->cm_fifo >= 63; i++)
  766. udelay(10);
  767. if (i == 1000) {
  768. printk(KERN_ERR "gbefb: cmap FIFO timeout\n");
  769. return 1;
  770. }
  771. gbe->cmap[regno] = gbe_cmap[regno];
  772. }
  773. } else if (regno < 16) {
  774. switch (info->var.bits_per_pixel) {
  775. case 15:
  776. case 16:
  777. red >>= 3;
  778. green >>= 3;
  779. blue >>= 3;
  780. pseudo_palette[regno] =
  781. (red << info->var.red.offset) |
  782. (green << info->var.green.offset) |
  783. (blue << info->var.blue.offset);
  784. break;
  785. case 32:
  786. pseudo_palette[regno] =
  787. (red << info->var.red.offset) |
  788. (green << info->var.green.offset) |
  789. (blue << info->var.blue.offset);
  790. break;
  791. }
  792. }
  793. return 0;
  794. }
  795. /*
  796. * Check video mode validity, eventually modify var to best match.
  797. */
  798. static int gbefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  799. {
  800. unsigned int line_length;
  801. struct gbe_timing_info timing;
  802. int ret;
  803. /* Limit bpp to 8, 16, and 32 */
  804. if (var->bits_per_pixel <= 8)
  805. var->bits_per_pixel = 8;
  806. else if (var->bits_per_pixel <= 16)
  807. var->bits_per_pixel = 16;
  808. else if (var->bits_per_pixel <= 32)
  809. var->bits_per_pixel = 32;
  810. else
  811. return -EINVAL;
  812. /* Check the mode can be mapped linearly with the tile table trick. */
  813. /* This requires width x height x bytes/pixel be a multiple of 512 */
  814. if ((var->xres * var->yres * var->bits_per_pixel) & 4095)
  815. return -EINVAL;
  816. var->grayscale = 0; /* No grayscale for now */
  817. ret = compute_gbe_timing(var, &timing);
  818. var->pixclock = ret;
  819. if (ret < 0)
  820. return -EINVAL;
  821. /* Adjust virtual resolution, if necessary */
  822. if (var->xres > var->xres_virtual || (!ywrap && !ypan))
  823. var->xres_virtual = var->xres;
  824. if (var->yres > var->yres_virtual || (!ywrap && !ypan))
  825. var->yres_virtual = var->yres;
  826. if (var->vmode & FB_VMODE_CONUPDATE) {
  827. var->vmode |= FB_VMODE_YWRAP;
  828. var->xoffset = info->var.xoffset;
  829. var->yoffset = info->var.yoffset;
  830. }
  831. /* No grayscale for now */
  832. var->grayscale = 0;
  833. /* Memory limit */
  834. line_length = var->xres_virtual * var->bits_per_pixel / 8;
  835. if (line_length * var->yres_virtual > gbe_mem_size)
  836. return -ENOMEM; /* Virtual resolution too high */
  837. switch (var->bits_per_pixel) {
  838. case 8:
  839. var->red.offset = 0;
  840. var->red.length = 8;
  841. var->green.offset = 0;
  842. var->green.length = 8;
  843. var->blue.offset = 0;
  844. var->blue.length = 8;
  845. var->transp.offset = 0;
  846. var->transp.length = 0;
  847. break;
  848. case 16: /* RGB 1555 */
  849. var->red.offset = 10;
  850. var->red.length = 5;
  851. var->green.offset = 5;
  852. var->green.length = 5;
  853. var->blue.offset = 0;
  854. var->blue.length = 5;
  855. var->transp.offset = 0;
  856. var->transp.length = 0;
  857. break;
  858. case 32: /* RGB 8888 */
  859. var->red.offset = 24;
  860. var->red.length = 8;
  861. var->green.offset = 16;
  862. var->green.length = 8;
  863. var->blue.offset = 8;
  864. var->blue.length = 8;
  865. var->transp.offset = 0;
  866. var->transp.length = 8;
  867. break;
  868. }
  869. var->red.msb_right = 0;
  870. var->green.msb_right = 0;
  871. var->blue.msb_right = 0;
  872. var->transp.msb_right = 0;
  873. var->left_margin = timing.htotal - timing.hsync_end;
  874. var->right_margin = timing.hsync_start - timing.width;
  875. var->upper_margin = timing.vtotal - timing.vsync_end;
  876. var->lower_margin = timing.vsync_start - timing.height;
  877. var->hsync_len = timing.hsync_end - timing.hsync_start;
  878. var->vsync_len = timing.vsync_end - timing.vsync_start;
  879. return 0;
  880. }
  881. static int gbefb_mmap(struct fb_info *info,
  882. struct vm_area_struct *vma)
  883. {
  884. unsigned long size = vma->vm_end - vma->vm_start;
  885. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  886. unsigned long addr;
  887. unsigned long phys_addr, phys_size;
  888. u16 *tile;
  889. /* check range */
  890. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  891. return -EINVAL;
  892. if (offset + size > gbe_mem_size)
  893. return -EINVAL;
  894. /* remap using the fastest write-through mode on architecture */
  895. /* try not polluting the cache when possible */
  896. pgprot_val(vma->vm_page_prot) =
  897. pgprot_fb(pgprot_val(vma->vm_page_prot));
  898. vma->vm_flags |= VM_IO | VM_RESERVED;
  899. /* look for the starting tile */
  900. tile = &gbe_tiles.cpu[offset >> TILE_SHIFT];
  901. addr = vma->vm_start;
  902. offset &= TILE_MASK;
  903. /* remap each tile separately */
  904. do {
  905. phys_addr = (((unsigned long) (*tile)) << TILE_SHIFT) + offset;
  906. if ((offset + size) < TILE_SIZE)
  907. phys_size = size;
  908. else
  909. phys_size = TILE_SIZE - offset;
  910. if (remap_pfn_range(vma, addr, phys_addr >> PAGE_SHIFT,
  911. phys_size, vma->vm_page_prot))
  912. return -EAGAIN;
  913. offset = 0;
  914. size -= phys_size;
  915. addr += phys_size;
  916. tile++;
  917. } while (size);
  918. return 0;
  919. }
  920. static struct fb_ops gbefb_ops = {
  921. .owner = THIS_MODULE,
  922. .fb_check_var = gbefb_check_var,
  923. .fb_set_par = gbefb_set_par,
  924. .fb_setcolreg = gbefb_setcolreg,
  925. .fb_mmap = gbefb_mmap,
  926. .fb_blank = gbefb_blank,
  927. .fb_fillrect = cfb_fillrect,
  928. .fb_copyarea = cfb_copyarea,
  929. .fb_imageblit = cfb_imageblit,
  930. };
  931. /*
  932. * sysfs
  933. */
  934. static ssize_t gbefb_show_memsize(struct device *dev, struct device_attribute *attr, char *buf)
  935. {
  936. return snprintf(buf, PAGE_SIZE, "%d\n", gbe_mem_size);
  937. }
  938. static DEVICE_ATTR(size, S_IRUGO, gbefb_show_memsize, NULL);
  939. static ssize_t gbefb_show_rev(struct device *device, struct device_attribute *attr, char *buf)
  940. {
  941. return snprintf(buf, PAGE_SIZE, "%d\n", gbe_revision);
  942. }
  943. static DEVICE_ATTR(revision, S_IRUGO, gbefb_show_rev, NULL);
  944. static void __devexit gbefb_remove_sysfs(struct device *dev)
  945. {
  946. device_remove_file(dev, &dev_attr_size);
  947. device_remove_file(dev, &dev_attr_revision);
  948. }
  949. static void gbefb_create_sysfs(struct device *dev)
  950. {
  951. device_create_file(dev, &dev_attr_size);
  952. device_create_file(dev, &dev_attr_revision);
  953. }
  954. /*
  955. * Initialization
  956. */
  957. static int __devinit gbefb_setup(char *options)
  958. {
  959. char *this_opt;
  960. if (!options || !*options)
  961. return 0;
  962. while ((this_opt = strsep(&options, ",")) != NULL) {
  963. if (!strncmp(this_opt, "monitor:", 8)) {
  964. if (!strncmp(this_opt + 8, "crt", 3)) {
  965. flat_panel_enabled = 0;
  966. default_var = &default_var_CRT;
  967. default_mode = &default_mode_CRT;
  968. } else if (!strncmp(this_opt + 8, "1600sw", 6) ||
  969. !strncmp(this_opt + 8, "lcd", 3)) {
  970. flat_panel_enabled = 1;
  971. default_var = &default_var_LCD;
  972. default_mode = &default_mode_LCD;
  973. }
  974. } else if (!strncmp(this_opt, "mem:", 4)) {
  975. gbe_mem_size = memparse(this_opt + 4, &this_opt);
  976. if (gbe_mem_size > CONFIG_FB_GBE_MEM * 1024 * 1024)
  977. gbe_mem_size = CONFIG_FB_GBE_MEM * 1024 * 1024;
  978. if (gbe_mem_size < TILE_SIZE)
  979. gbe_mem_size = TILE_SIZE;
  980. } else
  981. mode_option = this_opt;
  982. }
  983. return 0;
  984. }
  985. static int __devinit gbefb_probe(struct platform_device *p_dev)
  986. {
  987. int i, ret = 0;
  988. struct fb_info *info;
  989. struct gbefb_par *par;
  990. #ifndef MODULE
  991. char *options = NULL;
  992. #endif
  993. info = framebuffer_alloc(sizeof(struct gbefb_par), &p_dev->dev);
  994. if (!info)
  995. return -ENOMEM;
  996. #ifndef MODULE
  997. if (fb_get_options("gbefb", &options)) {
  998. ret = -ENODEV;
  999. goto out_release_framebuffer;
  1000. }
  1001. gbefb_setup(options);
  1002. #endif
  1003. if (!request_mem_region(GBE_BASE, sizeof(struct sgi_gbe), "GBE")) {
  1004. printk(KERN_ERR "gbefb: couldn't reserve mmio region\n");
  1005. ret = -EBUSY;
  1006. goto out_release_framebuffer;
  1007. }
  1008. gbe = (struct sgi_gbe *) ioremap(GBE_BASE, sizeof(struct sgi_gbe));
  1009. if (!gbe) {
  1010. printk(KERN_ERR "gbefb: couldn't map mmio region\n");
  1011. ret = -ENXIO;
  1012. goto out_release_mem_region;
  1013. }
  1014. gbe_revision = gbe->ctrlstat & 15;
  1015. gbe_tiles.cpu =
  1016. dma_alloc_coherent(NULL, GBE_TLB_SIZE * sizeof(uint16_t),
  1017. &gbe_tiles.dma, GFP_KERNEL);
  1018. if (!gbe_tiles.cpu) {
  1019. printk(KERN_ERR "gbefb: couldn't allocate tiles table\n");
  1020. ret = -ENOMEM;
  1021. goto out_unmap;
  1022. }
  1023. if (gbe_mem_phys) {
  1024. /* memory was allocated at boot time */
  1025. gbe_mem = ioremap_nocache(gbe_mem_phys, gbe_mem_size);
  1026. if (!gbe_mem) {
  1027. printk(KERN_ERR "gbefb: couldn't map framebuffer\n");
  1028. ret = -ENOMEM;
  1029. goto out_tiles_free;
  1030. }
  1031. gbe_dma_addr = 0;
  1032. } else {
  1033. /* try to allocate memory with the classical allocator
  1034. * this has high chance to fail on low memory machines */
  1035. gbe_mem = dma_alloc_coherent(NULL, gbe_mem_size, &gbe_dma_addr,
  1036. GFP_KERNEL);
  1037. if (!gbe_mem) {
  1038. printk(KERN_ERR "gbefb: couldn't allocate framebuffer memory\n");
  1039. ret = -ENOMEM;
  1040. goto out_tiles_free;
  1041. }
  1042. gbe_mem_phys = (unsigned long) gbe_dma_addr;
  1043. }
  1044. #ifdef CONFIG_X86
  1045. mtrr_add(gbe_mem_phys, gbe_mem_size, MTRR_TYPE_WRCOMB, 1);
  1046. #endif
  1047. /* map framebuffer memory into tiles table */
  1048. for (i = 0; i < (gbe_mem_size >> TILE_SHIFT); i++)
  1049. gbe_tiles.cpu[i] = (gbe_mem_phys >> TILE_SHIFT) + i;
  1050. info->fbops = &gbefb_ops;
  1051. info->pseudo_palette = pseudo_palette;
  1052. info->flags = FBINFO_DEFAULT;
  1053. info->screen_base = gbe_mem;
  1054. fb_alloc_cmap(&info->cmap, 256, 0);
  1055. /* reset GBE */
  1056. gbe_reset();
  1057. par = info->par;
  1058. /* turn on default video mode */
  1059. if (fb_find_mode(&par->var, info, mode_option, NULL, 0,
  1060. default_mode, 8) == 0)
  1061. par->var = *default_var;
  1062. info->var = par->var;
  1063. gbefb_check_var(&par->var, info);
  1064. gbefb_encode_fix(&info->fix, &info->var);
  1065. if (register_framebuffer(info) < 0) {
  1066. printk(KERN_ERR "gbefb: couldn't register framebuffer\n");
  1067. ret = -ENXIO;
  1068. goto out_gbe_unmap;
  1069. }
  1070. platform_set_drvdata(p_dev, info);
  1071. gbefb_create_sysfs(&p_dev->dev);
  1072. printk(KERN_INFO "fb%d: %s rev %d @ 0x%08x using %dkB memory\n",
  1073. info->node, info->fix.id, gbe_revision, (unsigned) GBE_BASE,
  1074. gbe_mem_size >> 10);
  1075. return 0;
  1076. out_gbe_unmap:
  1077. if (gbe_dma_addr)
  1078. dma_free_coherent(NULL, gbe_mem_size, gbe_mem, gbe_mem_phys);
  1079. else
  1080. iounmap(gbe_mem);
  1081. out_tiles_free:
  1082. dma_free_coherent(NULL, GBE_TLB_SIZE * sizeof(uint16_t),
  1083. (void *)gbe_tiles.cpu, gbe_tiles.dma);
  1084. out_unmap:
  1085. iounmap(gbe);
  1086. out_release_mem_region:
  1087. release_mem_region(GBE_BASE, sizeof(struct sgi_gbe));
  1088. out_release_framebuffer:
  1089. framebuffer_release(info);
  1090. return ret;
  1091. }
  1092. static int __devexit gbefb_remove(struct platform_device* p_dev)
  1093. {
  1094. struct fb_info *info = platform_get_drvdata(p_dev);
  1095. unregister_framebuffer(info);
  1096. gbe_turn_off();
  1097. if (gbe_dma_addr)
  1098. dma_free_coherent(NULL, gbe_mem_size, gbe_mem, gbe_mem_phys);
  1099. else
  1100. iounmap(gbe_mem);
  1101. dma_free_coherent(NULL, GBE_TLB_SIZE * sizeof(uint16_t),
  1102. (void *)gbe_tiles.cpu, gbe_tiles.dma);
  1103. release_mem_region(GBE_BASE, sizeof(struct sgi_gbe));
  1104. iounmap(gbe);
  1105. gbefb_remove_sysfs(&p_dev->dev);
  1106. framebuffer_release(info);
  1107. return 0;
  1108. }
  1109. static struct platform_driver gbefb_driver = {
  1110. .probe = gbefb_probe,
  1111. .remove = __devexit_p(gbefb_remove),
  1112. .driver = {
  1113. .name = "gbefb",
  1114. },
  1115. };
  1116. static struct platform_device *gbefb_device;
  1117. static int __init gbefb_init(void)
  1118. {
  1119. int ret = platform_driver_register(&gbefb_driver);
  1120. if (!ret) {
  1121. gbefb_device = platform_device_alloc("gbefb", 0);
  1122. if (gbefb_device) {
  1123. ret = platform_device_add(gbefb_device);
  1124. } else {
  1125. ret = -ENOMEM;
  1126. }
  1127. if (ret) {
  1128. platform_device_put(gbefb_device);
  1129. platform_driver_unregister(&gbefb_driver);
  1130. }
  1131. }
  1132. return ret;
  1133. }
  1134. static void __exit gbefb_exit(void)
  1135. {
  1136. platform_device_unregister(gbefb_device);
  1137. platform_driver_unregister(&gbefb_driver);
  1138. }
  1139. module_init(gbefb_init);
  1140. module_exit(gbefb_exit);
  1141. MODULE_LICENSE("GPL");