gbefb.c 33 KB

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