fsl_diu_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright 2007, 2010 Freescale Semiconductor, Inc.
  3. * York Sun <yorksun@freescale.com>
  4. *
  5. * FSL DIU Framebuffer driver
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <i2c.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include <fsl_diu_fb.h>
  30. struct fb_videomode {
  31. const char *name; /* optional */
  32. unsigned int refresh; /* optional */
  33. unsigned int xres;
  34. unsigned int yres;
  35. unsigned int pixclock;
  36. unsigned int left_margin;
  37. unsigned int right_margin;
  38. unsigned int upper_margin;
  39. unsigned int lower_margin;
  40. unsigned int hsync_len;
  41. unsigned int vsync_len;
  42. unsigned int sync;
  43. unsigned int vmode;
  44. unsigned int flag;
  45. };
  46. #define FB_SYNC_VERT_HIGH_ACT 2 /* vertical sync high active */
  47. #define FB_SYNC_COMP_HIGH_ACT 8 /* composite sync high active */
  48. #define FB_VMODE_NONINTERLACED 0 /* non interlaced */
  49. /* This setting is used for the ifm pdm360ng with PRIMEVIEW PM070WL3 */
  50. static struct fb_videomode fsl_diu_mode_800 = {
  51. .refresh = 60,
  52. .xres = 800,
  53. .yres = 480,
  54. .pixclock = 31250,
  55. .left_margin = 86,
  56. .right_margin = 42,
  57. .upper_margin = 33,
  58. .lower_margin = 10,
  59. .hsync_len = 128,
  60. .vsync_len = 2,
  61. .sync = 0,
  62. .vmode = FB_VMODE_NONINTERLACED
  63. };
  64. /*
  65. * These parameters give default parameters
  66. * for video output 1024x768,
  67. * FIXME - change timing to proper amounts
  68. * hsync 31.5kHz, vsync 60Hz
  69. */
  70. static struct fb_videomode fsl_diu_mode_1024 = {
  71. .refresh = 60,
  72. .xres = 1024,
  73. .yres = 768,
  74. .pixclock = 15385,
  75. .left_margin = 160,
  76. .right_margin = 24,
  77. .upper_margin = 29,
  78. .lower_margin = 3,
  79. .hsync_len = 136,
  80. .vsync_len = 6,
  81. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  82. .vmode = FB_VMODE_NONINTERLACED
  83. };
  84. static struct fb_videomode fsl_diu_mode_1280 = {
  85. .name = "1280x1024-60",
  86. .refresh = 60,
  87. .xres = 1280,
  88. .yres = 1024,
  89. .pixclock = 9375,
  90. .left_margin = 38,
  91. .right_margin = 128,
  92. .upper_margin = 2,
  93. .lower_margin = 7,
  94. .hsync_len = 216,
  95. .vsync_len = 37,
  96. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  97. .vmode = FB_VMODE_NONINTERLACED
  98. };
  99. /*
  100. * These are the fields of area descriptor(in DDR memory) for every plane
  101. */
  102. struct diu_ad {
  103. /* Word 0(32-bit) in DDR memory */
  104. unsigned int pix_fmt; /* hard coding pixel format */
  105. /* Word 1(32-bit) in DDR memory */
  106. unsigned int addr;
  107. /* Word 2(32-bit) in DDR memory */
  108. unsigned int src_size_g_alpha;
  109. /* Word 3(32-bit) in DDR memory */
  110. unsigned int aoi_size;
  111. /* Word 4(32-bit) in DDR memory */
  112. unsigned int offset_xyi;
  113. /* Word 5(32-bit) in DDR memory */
  114. unsigned int offset_xyd;
  115. /* Word 6(32-bit) in DDR memory */
  116. unsigned int ckmax_r:8;
  117. unsigned int ckmax_g:8;
  118. unsigned int ckmax_b:8;
  119. unsigned int res9:8;
  120. /* Word 7(32-bit) in DDR memory */
  121. unsigned int ckmin_r:8;
  122. unsigned int ckmin_g:8;
  123. unsigned int ckmin_b:8;
  124. unsigned int res10:8;
  125. /* Word 8(32-bit) in DDR memory */
  126. unsigned int next_ad;
  127. /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
  128. unsigned int res1;
  129. unsigned int res2;
  130. unsigned int res3;
  131. }__attribute__ ((packed));
  132. /*
  133. * DIU register map
  134. */
  135. struct diu {
  136. unsigned int desc[3];
  137. unsigned int gamma;
  138. unsigned int pallete;
  139. unsigned int cursor;
  140. unsigned int curs_pos;
  141. unsigned int diu_mode;
  142. unsigned int bgnd;
  143. unsigned int bgnd_wb;
  144. unsigned int disp_size;
  145. unsigned int wb_size;
  146. unsigned int wb_mem_addr;
  147. unsigned int hsyn_para;
  148. unsigned int vsyn_para;
  149. unsigned int syn_pol;
  150. unsigned int thresholds;
  151. unsigned int int_status;
  152. unsigned int int_mask;
  153. unsigned int colorbar[8];
  154. unsigned int filling;
  155. unsigned int plut;
  156. } __attribute__ ((packed));
  157. struct diu_hw {
  158. struct diu *diu_reg;
  159. volatile unsigned int mode; /* DIU operation mode */
  160. };
  161. struct diu_addr {
  162. unsigned char * paddr; /* Virtual address */
  163. unsigned int offset;
  164. };
  165. /*
  166. * Modes of operation of DIU
  167. */
  168. #define MFB_MODE0 0 /* DIU off */
  169. #define MFB_MODE1 1 /* All three planes output to display */
  170. #define MFB_MODE2 2 /* Plane 1 to display,
  171. * planes 2+3 written back to memory */
  172. #define MFB_MODE3 3 /* All three planes written back to memory */
  173. #define MFB_MODE4 4 /* Color bar generation */
  174. #define MAX_CURS 32
  175. static struct fb_info fsl_fb_info;
  176. static struct diu_addr gamma, cursor;
  177. static struct diu_ad fsl_diu_fb_ad __attribute__ ((aligned(32)));
  178. static struct diu_ad dummy_ad __attribute__ ((aligned(32)));
  179. static unsigned char *dummy_fb;
  180. static struct diu_hw dr = {
  181. .mode = MFB_MODE1,
  182. };
  183. int fb_enabled = 0;
  184. int fb_initialized = 0;
  185. const int default_xres = 1280;
  186. const int default_pixel_format = 0x88882317;
  187. static int map_video_memory(struct fb_info *info, unsigned long bytes_align);
  188. static void enable_lcdc(void);
  189. static void disable_lcdc(void);
  190. static int fsl_diu_enable_panel(struct fb_info *info);
  191. static int fsl_diu_disable_panel(struct fb_info *info);
  192. static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align);
  193. void diu_set_pixel_clock(unsigned int pixclock);
  194. int fsl_diu_init(int xres, unsigned int pixel_format, int gamma_fix)
  195. {
  196. struct fb_videomode *fsl_diu_mode_db;
  197. struct diu_ad *ad = &fsl_diu_fb_ad;
  198. struct diu *hw;
  199. struct fb_info *info = &fsl_fb_info;
  200. struct fb_var_screeninfo *var = &info->var;
  201. unsigned char *gamma_table_base;
  202. unsigned int i, j;
  203. debug("Enter fsl_diu_init\n");
  204. dr.diu_reg = (struct diu *) (CONFIG_SYS_DIU_ADDR);
  205. hw = (struct diu *) dr.diu_reg;
  206. disable_lcdc();
  207. switch (xres) {
  208. case 800:
  209. fsl_diu_mode_db = &fsl_diu_mode_800;
  210. break;
  211. case 1280:
  212. fsl_diu_mode_db = &fsl_diu_mode_1280;
  213. break;
  214. default:
  215. fsl_diu_mode_db = &fsl_diu_mode_1024;
  216. }
  217. if (0 == fb_initialized) {
  218. allocate_buf(&gamma, 768, 32);
  219. debug("gamma is allocated @ 0x%x\n",
  220. (unsigned int)gamma.paddr);
  221. allocate_buf(&cursor, MAX_CURS * MAX_CURS * 2, 32);
  222. debug("curosr is allocated @ 0x%x\n",
  223. (unsigned int)cursor.paddr);
  224. /* create a dummy fb and dummy ad */
  225. dummy_fb = malloc(64);
  226. if (NULL == dummy_fb) {
  227. printf("Cannot allocate dummy fb\n");
  228. return -1;
  229. }
  230. dummy_ad.addr = cpu_to_le32((unsigned int)dummy_fb);
  231. dummy_ad.pix_fmt = 0x88882317;
  232. dummy_ad.src_size_g_alpha = 0x04400000; /* alpha = 0 */
  233. dummy_ad.aoi_size = 0x02000400;
  234. dummy_ad.offset_xyi = 0;
  235. dummy_ad.offset_xyd = 0;
  236. dummy_ad.next_ad = 0;
  237. /* Memory allocation for framebuffer */
  238. if (map_video_memory(info, 32)) {
  239. printf("Unable to allocate fb memory 1\n");
  240. return -1;
  241. }
  242. }
  243. memset(info->screen_base, 0, info->smem_len);
  244. out_be32(&dr.diu_reg->desc[0], (int)&dummy_ad);
  245. out_be32(&dr.diu_reg->desc[1], (int)&dummy_ad);
  246. out_be32(&dr.diu_reg->desc[2], (int)&dummy_ad);
  247. debug("dummy dr.diu_reg->desc[0] = 0x%x\n", dr.diu_reg->desc[0]);
  248. debug("dummy desc[0] = 0x%x\n", hw->desc[0]);
  249. /* read mode info */
  250. var->xres = fsl_diu_mode_db->xres;
  251. var->yres = fsl_diu_mode_db->yres;
  252. var->bits_per_pixel = 32;
  253. var->pixclock = fsl_diu_mode_db->pixclock;
  254. var->left_margin = fsl_diu_mode_db->left_margin;
  255. var->right_margin = fsl_diu_mode_db->right_margin;
  256. var->upper_margin = fsl_diu_mode_db->upper_margin;
  257. var->lower_margin = fsl_diu_mode_db->lower_margin;
  258. var->hsync_len = fsl_diu_mode_db->hsync_len;
  259. var->vsync_len = fsl_diu_mode_db->vsync_len;
  260. var->sync = fsl_diu_mode_db->sync;
  261. var->vmode = fsl_diu_mode_db->vmode;
  262. info->line_length = var->xres * var->bits_per_pixel / 8;
  263. ad->pix_fmt = pixel_format;
  264. ad->addr = cpu_to_le32((unsigned int)info->screen_base);
  265. ad->src_size_g_alpha
  266. = cpu_to_le32((var->yres << 12) | var->xres);
  267. /* fix me. AOI should not be greater than display size */
  268. ad->aoi_size = cpu_to_le32(( var->yres << 16) | var->xres);
  269. ad->offset_xyi = 0;
  270. ad->offset_xyd = 0;
  271. /* Disable chroma keying function */
  272. ad->ckmax_r = 0;
  273. ad->ckmax_g = 0;
  274. ad->ckmax_b = 0;
  275. ad->ckmin_r = 255;
  276. ad->ckmin_g = 255;
  277. ad->ckmin_b = 255;
  278. gamma_table_base = gamma.paddr;
  279. debug("gamma_table_base is allocated @ 0x%x\n",
  280. (unsigned int)gamma_table_base);
  281. /* Prep for DIU init - gamma table */
  282. for (i = 0; i <= 2; i++)
  283. for (j = 0; j <= 255; j++)
  284. *gamma_table_base++ = j;
  285. if (gamma_fix == 1) { /* fix the gamma */
  286. debug("Fix gamma table\n");
  287. gamma_table_base = gamma.paddr;
  288. for (i = 0; i < 256*3; i++) {
  289. gamma_table_base[i] = (gamma_table_base[i] << 2)
  290. | ((gamma_table_base[i] >> 6) & 0x03);
  291. }
  292. }
  293. debug("update-lcdc: HW - %p\n Disabling DIU\n", hw);
  294. /* Program DIU registers */
  295. out_be32(&hw->gamma, (int)gamma.paddr);
  296. out_be32(&hw->cursor, (int)cursor.paddr);
  297. out_be32(&hw->bgnd, 0x007F7F7F);
  298. out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
  299. out_be32(&hw->disp_size, var->yres << 16 | var->xres); /* DISP SIZE */
  300. out_be32(&hw->wb_size, 0); /* WB SIZE */
  301. out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
  302. out_be32(&hw->hsyn_para, var->left_margin << 22 | /* BP_H */
  303. var->hsync_len << 11 | /* PW_H */
  304. var->right_margin); /* FP_H */
  305. out_be32(&hw->vsyn_para, var->upper_margin << 22 | /* BP_V */
  306. var->vsync_len << 11 | /* PW_V */
  307. var->lower_margin); /* FP_V */
  308. out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
  309. out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
  310. out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
  311. out_be32(&hw->int_mask, 0); /* INT MASK */
  312. out_be32(&hw->plut, 0x01F5F666);
  313. /* Pixel Clock configuration */
  314. debug("DIU pixclock in ps - %d\n", var->pixclock);
  315. diu_set_pixel_clock(var->pixclock);
  316. fb_initialized = 1;
  317. /* Enable the DIU */
  318. fsl_diu_enable_panel(info);
  319. enable_lcdc();
  320. return 0;
  321. }
  322. char *fsl_fb_open(struct fb_info **info)
  323. {
  324. *info = &fsl_fb_info;
  325. return fsl_fb_info.screen_base;
  326. }
  327. void fsl_diu_close(void)
  328. {
  329. struct fb_info *info = &fsl_fb_info;
  330. fsl_diu_disable_panel(info);
  331. }
  332. static int fsl_diu_enable_panel(struct fb_info *info)
  333. {
  334. struct diu *hw = dr.diu_reg;
  335. struct diu_ad *ad = &fsl_diu_fb_ad;
  336. debug("Entered: enable_panel\n");
  337. if (in_be32(&hw->desc[0]) != (unsigned)ad)
  338. out_be32(&hw->desc[0], (unsigned)ad);
  339. debug("desc[0] = 0x%x\n", hw->desc[0]);
  340. return 0;
  341. }
  342. static int fsl_diu_disable_panel(struct fb_info *info)
  343. {
  344. struct diu *hw = dr.diu_reg;
  345. debug("Entered: disable_panel\n");
  346. if (in_be32(&hw->desc[0]) != (unsigned)&dummy_ad)
  347. out_be32(&hw->desc[0], (unsigned)&dummy_ad);
  348. return 0;
  349. }
  350. static int map_video_memory(struct fb_info *info, unsigned long bytes_align)
  351. {
  352. unsigned long offset;
  353. unsigned long mask;
  354. debug("Entered: map_video_memory\n");
  355. /* allocate maximum 1280*1024 with 32bpp */
  356. info->smem_len = 1280 * 4 *1024 + bytes_align;
  357. debug("MAP_VIDEO_MEMORY: smem_len = %d\n", info->smem_len);
  358. info->screen_base = malloc(info->smem_len);
  359. if (info->screen_base == NULL) {
  360. printf("Unable to allocate fb memory\n");
  361. return -1;
  362. }
  363. info->smem_start = (unsigned int) info->screen_base;
  364. mask = bytes_align - 1;
  365. offset = (unsigned long)info->screen_base & mask;
  366. if (offset) {
  367. info->screen_base += (bytes_align - offset);
  368. info->smem_len = info->smem_len - (bytes_align - offset);
  369. } else
  370. info->smem_len = info->smem_len - bytes_align;
  371. info->screen_size = info->smem_len;
  372. debug("Allocated fb @ 0x%08lx, size=%d.\n",
  373. info->smem_start, info->smem_len);
  374. return 0;
  375. }
  376. static void enable_lcdc(void)
  377. {
  378. struct diu *hw = dr.diu_reg;
  379. debug("Entered: enable_lcdc, fb_enabled = %d\n", fb_enabled);
  380. if (!fb_enabled) {
  381. out_be32(&hw->diu_mode, dr.mode);
  382. fb_enabled++;
  383. }
  384. debug("diu_mode = %d\n", hw->diu_mode);
  385. }
  386. static void disable_lcdc(void)
  387. {
  388. struct diu *hw = dr.diu_reg;
  389. debug("Entered: disable_lcdc, fb_enabled = %d\n", fb_enabled);
  390. if (fb_enabled) {
  391. out_be32(&hw->diu_mode, 0);
  392. fb_enabled = 0;
  393. }
  394. }
  395. /*
  396. * Align to 64-bit(8-byte), 32-byte, etc.
  397. */
  398. static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
  399. {
  400. u32 offset, ssize;
  401. u32 mask;
  402. debug("Entered: allocate_buf\n");
  403. ssize = size + bytes_align;
  404. buf->paddr = malloc(ssize);
  405. if (!buf->paddr)
  406. return -1;
  407. memset(buf->paddr, 0, ssize);
  408. mask = bytes_align - 1;
  409. offset = (u32)buf->paddr & mask;
  410. if (offset) {
  411. buf->offset = bytes_align - offset;
  412. buf->paddr = (unsigned char *) ((u32)buf->paddr + offset);
  413. } else
  414. buf->offset = 0;
  415. return 0;
  416. }
  417. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  418. #include <stdio_dev.h>
  419. #include <video_fb.h>
  420. /*
  421. * The Graphic Device
  422. */
  423. static GraphicDevice ctfb;
  424. void *video_hw_init(void)
  425. {
  426. struct fb_info *info;
  427. if (platform_diu_init(&ctfb.winSizeX, &ctfb.winSizeY) < 0)
  428. return NULL;
  429. /* fill in Graphic device struct */
  430. sprintf(ctfb.modeIdent, "%ix%ix%i %ikHz %iHz",
  431. ctfb.winSizeX, ctfb.winSizeY, 32, 64, 60);
  432. ctfb.frameAdrs = (unsigned int)fsl_fb_open(&info);
  433. ctfb.plnSizeX = ctfb.winSizeX;
  434. ctfb.plnSizeY = ctfb.winSizeY;
  435. ctfb.gdfBytesPP = 4;
  436. ctfb.gdfIndex = GDF_32BIT_X888RGB;
  437. ctfb.isaBase = 0;
  438. ctfb.pciBase = 0;
  439. ctfb.memSize = info->screen_size;
  440. /* Cursor Start Address */
  441. ctfb.dprBase = 0;
  442. ctfb.vprBase = 0;
  443. ctfb.cprBase = 0;
  444. return &ctfb;
  445. }
  446. #endif /* defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE) */