mxsfb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Freescale i.MX23/i.MX28 LCDIF driver
  3. *
  4. * Copyright (C) 2011-2013 Marek Vasut <marex@denx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <video_fb.h>
  11. #include <asm/arch/imx-regs.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/sys_proto.h>
  14. #include <linux/errno.h>
  15. #include <asm/io.h>
  16. #include <asm/imx-common/dma.h>
  17. #include "videomodes.h"
  18. #define PS2KHZ(ps) (1000000000UL / (ps))
  19. static GraphicDevice panel;
  20. struct mxs_dma_desc desc;
  21. /**
  22. * mxsfb_system_setup() - Fine-tune LCDIF configuration
  23. *
  24. * This function is used to adjust the LCDIF configuration. This is usually
  25. * needed when driving the controller in System-Mode to operate an 8080 or
  26. * 6800 connected SmartLCD.
  27. */
  28. __weak void mxsfb_system_setup(void)
  29. {
  30. }
  31. /*
  32. * DENX M28EVK:
  33. * setenv videomode
  34. * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
  35. * le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
  36. *
  37. * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
  38. * setenv videomode
  39. * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
  40. * le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
  41. */
  42. static void mxs_lcd_init(GraphicDevice *panel,
  43. struct ctfb_res_modes *mode, int bpp)
  44. {
  45. struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
  46. uint32_t word_len = 0, bus_width = 0;
  47. uint8_t valid_data = 0;
  48. /* Kick in the LCDIF clock */
  49. mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
  50. /* Restart the LCDIF block */
  51. mxs_reset_block(&regs->hw_lcdif_ctrl_reg);
  52. switch (bpp) {
  53. case 24:
  54. word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
  55. bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
  56. valid_data = 0x7;
  57. break;
  58. case 18:
  59. word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
  60. bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
  61. valid_data = 0x7;
  62. break;
  63. case 16:
  64. word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
  65. bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
  66. valid_data = 0xf;
  67. break;
  68. case 8:
  69. word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
  70. bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
  71. valid_data = 0xf;
  72. break;
  73. }
  74. writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
  75. LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
  76. &regs->hw_lcdif_ctrl);
  77. writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
  78. &regs->hw_lcdif_ctrl1);
  79. mxsfb_system_setup();
  80. writel((mode->yres << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) | mode->xres,
  81. &regs->hw_lcdif_transfer_count);
  82. writel(LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
  83. LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
  84. LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
  85. mode->vsync_len, &regs->hw_lcdif_vdctrl0);
  86. writel(mode->upper_margin + mode->lower_margin +
  87. mode->vsync_len + mode->yres,
  88. &regs->hw_lcdif_vdctrl1);
  89. writel((mode->hsync_len << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
  90. (mode->left_margin + mode->right_margin +
  91. mode->hsync_len + mode->xres),
  92. &regs->hw_lcdif_vdctrl2);
  93. writel(((mode->left_margin + mode->hsync_len) <<
  94. LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
  95. (mode->upper_margin + mode->vsync_len),
  96. &regs->hw_lcdif_vdctrl3);
  97. writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | mode->xres,
  98. &regs->hw_lcdif_vdctrl4);
  99. writel(panel->frameAdrs, &regs->hw_lcdif_cur_buf);
  100. writel(panel->frameAdrs, &regs->hw_lcdif_next_buf);
  101. /* Flush FIFO first */
  102. writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
  103. #ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
  104. /* Sync signals ON */
  105. setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
  106. #endif
  107. /* FIFO cleared */
  108. writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_clr);
  109. /* RUN! */
  110. writel(LCDIF_CTRL_RUN, &regs->hw_lcdif_ctrl_set);
  111. }
  112. void lcdif_power_down(void)
  113. {
  114. struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
  115. int timeout = 1000000;
  116. writel(panel.frameAdrs, &regs->hw_lcdif_cur_buf_reg);
  117. writel(panel.frameAdrs, &regs->hw_lcdif_next_buf_reg);
  118. writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, &regs->hw_lcdif_ctrl1_clr);
  119. while (--timeout) {
  120. if (readl(&regs->hw_lcdif_ctrl1_reg) &
  121. LCDIF_CTRL1_VSYNC_EDGE_IRQ)
  122. break;
  123. udelay(1);
  124. }
  125. mxs_reset_block((struct mxs_register_32 *)&regs->hw_lcdif_ctrl_reg);
  126. }
  127. void *video_hw_init(void)
  128. {
  129. int bpp = -1;
  130. char *penv;
  131. void *fb;
  132. struct ctfb_res_modes mode;
  133. puts("Video: ");
  134. /* Suck display configuration from "videomode" variable */
  135. penv = getenv("videomode");
  136. if (!penv) {
  137. puts("MXSFB: 'videomode' variable not set!\n");
  138. return NULL;
  139. }
  140. bpp = video_get_params(&mode, penv);
  141. /* fill in Graphic device struct */
  142. sprintf(panel.modeIdent, "%dx%dx%d",
  143. mode.xres, mode.yres, bpp);
  144. panel.winSizeX = mode.xres;
  145. panel.winSizeY = mode.yres;
  146. panel.plnSizeX = mode.xres;
  147. panel.plnSizeY = mode.yres;
  148. switch (bpp) {
  149. case 24:
  150. case 18:
  151. panel.gdfBytesPP = 4;
  152. panel.gdfIndex = GDF_32BIT_X888RGB;
  153. break;
  154. case 16:
  155. panel.gdfBytesPP = 2;
  156. panel.gdfIndex = GDF_16BIT_565RGB;
  157. break;
  158. case 8:
  159. panel.gdfBytesPP = 1;
  160. panel.gdfIndex = GDF__8BIT_INDEX;
  161. break;
  162. default:
  163. printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
  164. return NULL;
  165. }
  166. panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
  167. /* Allocate framebuffer */
  168. fb = memalign(ARCH_DMA_MINALIGN,
  169. roundup(panel.memSize, ARCH_DMA_MINALIGN));
  170. if (!fb) {
  171. printf("MXSFB: Error allocating framebuffer!\n");
  172. return NULL;
  173. }
  174. /* Wipe framebuffer */
  175. memset(fb, 0, panel.memSize);
  176. panel.frameAdrs = (u32)fb;
  177. printf("%s\n", panel.modeIdent);
  178. /* Start framebuffer */
  179. mxs_lcd_init(&panel, &mode, bpp);
  180. #ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
  181. /*
  182. * If the LCD runs in system mode, the LCD refresh has to be triggered
  183. * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
  184. * having to set this bit manually after every single change in the
  185. * framebuffer memory, we set up specially crafted circular DMA, which
  186. * sets the RUN bit, then waits until it gets cleared and repeats this
  187. * infinitelly. This way, we get smooth continuous updates of the LCD.
  188. */
  189. struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
  190. memset(&desc, 0, sizeof(struct mxs_dma_desc));
  191. desc.address = (dma_addr_t)&desc;
  192. desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  193. MXS_DMA_DESC_WAIT4END |
  194. (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  195. desc.cmd.pio_words[0] = readl(&regs->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
  196. desc.cmd.next = (uint32_t)&desc.cmd;
  197. /* Execute the DMA chain. */
  198. mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
  199. #endif
  200. return (void *)&panel;
  201. }