mxsfb.c 6.4 KB

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