1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX23/i.MX28 LCDIF driver
11 #include <dm/device_compat.h>
12 #include <linux/errno.h>
17 #include <asm/arch/clock.h>
18 #include <asm/arch/imx-regs.h>
19 #include <asm/arch/sys_proto.h>
20 #include <asm/mach-imx/dma.h>
23 #include "videomodes.h"
25 #define PS2KHZ(ps) (1000000000UL / (ps))
26 #define HZ2PS(hz) (1000000000UL / ((hz) / 1000))
31 struct mxs_dma_desc desc;
34 * mxsfb_system_setup() - Fine-tune LCDIF configuration
36 * This function is used to adjust the LCDIF configuration. This is usually
37 * needed when driving the controller in System-Mode to operate an 8080 or
38 * 6800 connected SmartLCD.
40 __weak void mxsfb_system_setup(void)
47 * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
48 * le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
50 * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
52 * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
53 * le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
56 static void mxs_lcd_init(struct udevice *dev, u32 fb_addr,
57 struct display_timing *timings, int bpp)
59 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
60 const enum display_flags flags = timings->flags;
61 uint32_t word_len = 0, bus_width = 0;
62 uint8_t valid_data = 0;
65 #if CONFIG_IS_ENABLED(CLK)
69 ret = clk_get_by_name(dev, "per", &per_clk);
71 dev_err(dev, "Failed to get mxs clk: %d\n", ret);
75 ret = clk_set_rate(&per_clk, timings->pixelclock.typ);
77 dev_err(dev, "Failed to set mxs clk: %d\n", ret);
81 /* Kick in the LCDIF clock */
82 mxs_set_lcdclk(MXS_LCDIF_BASE, timings->pixelclock.typ / 1000);
85 /* Restart the LCDIF block */
86 mxs_reset_block(®s->hw_lcdif_ctrl_reg);
90 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
91 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
95 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
96 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
100 word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
101 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
105 word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
106 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
111 writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
112 LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
113 ®s->hw_lcdif_ctrl);
115 writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
116 ®s->hw_lcdif_ctrl1);
118 mxsfb_system_setup();
120 writel((timings->vactive.typ << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) |
121 timings->hactive.typ, ®s->hw_lcdif_transfer_count);
123 vdctrl0 = LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
124 LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
125 LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
126 timings->vsync_len.typ;
128 if(flags & DISPLAY_FLAGS_HSYNC_HIGH)
129 vdctrl0 |= LCDIF_VDCTRL0_HSYNC_POL;
130 writel(vdctrl0, ®s->hw_lcdif_vdctrl0);
131 writel(timings->vback_porch.typ + timings->vfront_porch.typ +
132 timings->vsync_len.typ + timings->vactive.typ,
133 ®s->hw_lcdif_vdctrl1);
134 writel((timings->hsync_len.typ << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
135 (timings->hback_porch.typ + timings->hfront_porch.typ +
136 timings->hsync_len.typ + timings->hactive.typ),
137 ®s->hw_lcdif_vdctrl2);
138 writel(((timings->hback_porch.typ + timings->hsync_len.typ) <<
139 LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
140 (timings->vback_porch.typ + timings->vsync_len.typ),
141 ®s->hw_lcdif_vdctrl3);
142 writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | timings->hactive.typ,
143 ®s->hw_lcdif_vdctrl4);
145 writel(fb_addr, ®s->hw_lcdif_cur_buf);
146 writel(fb_addr, ®s->hw_lcdif_next_buf);
148 /* Flush FIFO first */
149 writel(LCDIF_CTRL1_FIFO_CLEAR, ®s->hw_lcdif_ctrl1_set);
151 #ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
152 /* Sync signals ON */
153 setbits_le32(®s->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
157 writel(LCDIF_CTRL1_FIFO_CLEAR, ®s->hw_lcdif_ctrl1_clr);
160 writel(LCDIF_CTRL_RUN, ®s->hw_lcdif_ctrl_set);
163 static int mxs_probe_common(struct udevice *dev, struct display_timing *timings,
166 /* Start framebuffer */
167 mxs_lcd_init(dev, fb, timings, bpp);
169 #ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
171 * If the LCD runs in system mode, the LCD refresh has to be triggered
172 * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
173 * having to set this bit manually after every single change in the
174 * framebuffer memory, we set up specially crafted circular DMA, which
175 * sets the RUN bit, then waits until it gets cleared and repeats this
176 * infinitelly. This way, we get smooth continuous updates of the LCD.
178 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
180 memset(&desc, 0, sizeof(struct mxs_dma_desc));
181 desc.address = (dma_addr_t)&desc;
182 desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
183 MXS_DMA_DESC_WAIT4END |
184 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
185 desc.cmd.pio_words[0] = readl(®s->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
186 desc.cmd.next = (uint32_t)&desc.cmd;
188 /* Execute the DMA chain. */
189 mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
195 static int mxs_remove_common(u32 fb)
197 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
198 int timeout = 1000000;
203 writel(fb, ®s->hw_lcdif_cur_buf_reg);
204 writel(fb, ®s->hw_lcdif_next_buf_reg);
205 writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, ®s->hw_lcdif_ctrl1_clr);
207 if (readl(®s->hw_lcdif_ctrl1_reg) &
208 LCDIF_CTRL1_VSYNC_EDGE_IRQ)
212 mxs_reset_block((struct mxs_register_32 *)®s->hw_lcdif_ctrl_reg);
217 #ifndef CONFIG_DM_VIDEO
219 static GraphicDevice panel;
221 void lcdif_power_down(void)
223 mxs_remove_common(panel.frameAdrs);
226 void *video_hw_init(void)
232 struct ctfb_res_modes mode;
233 struct display_timing timings;
237 /* Suck display configuration from "videomode" variable */
238 penv = env_get("videomode");
240 puts("MXSFB: 'videomode' variable not set!\n");
244 bpp = video_get_params(&mode, penv);
246 /* fill in Graphic device struct */
247 sprintf(panel.modeIdent, "%dx%dx%d", mode.xres, mode.yres, bpp);
249 panel.winSizeX = mode.xres;
250 panel.winSizeY = mode.yres;
251 panel.plnSizeX = mode.xres;
252 panel.plnSizeY = mode.yres;
257 panel.gdfBytesPP = 4;
258 panel.gdfIndex = GDF_32BIT_X888RGB;
261 panel.gdfBytesPP = 2;
262 panel.gdfIndex = GDF_16BIT_565RGB;
265 panel.gdfBytesPP = 1;
266 panel.gdfIndex = GDF__8BIT_INDEX;
269 printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
273 panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
275 /* Allocate framebuffer */
276 fb = memalign(ARCH_DMA_MINALIGN,
277 roundup(panel.memSize, ARCH_DMA_MINALIGN));
279 printf("MXSFB: Error allocating framebuffer!\n");
283 /* Wipe framebuffer */
284 memset(fb, 0, panel.memSize);
286 panel.frameAdrs = (u32)fb;
288 printf("%s\n", panel.modeIdent);
290 video_ctfb_mode_to_display_timing(&mode, &timings);
292 ret = mxs_probe_common(NULL, &timings, bpp, (u32)fb);
296 return (void *)&panel;
303 #else /* ifndef CONFIG_DM_VIDEO */
305 static int mxs_of_get_timings(struct udevice *dev,
306 struct display_timing *timings,
313 ret = ofnode_read_u32(dev_ofnode(dev), "display", &display_phandle);
315 dev_err(dev, "required display property isn't provided\n");
319 display_node = ofnode_get_by_phandle(display_phandle);
320 if (!ofnode_valid(display_node)) {
321 dev_err(dev, "failed to find display subnode\n");
325 ret = ofnode_read_u32(display_node, "bits-per-pixel", bpp);
328 "required bits-per-pixel property isn't provided\n");
332 ret = ofnode_decode_display_timing(display_node, 0, timings);
334 dev_err(dev, "failed to get any display timings\n");
341 static int mxs_video_probe(struct udevice *dev)
343 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
344 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
346 struct display_timing timings;
348 u32 fb_start, fb_end;
351 debug("%s() plat: base 0x%lx, size 0x%x\n",
352 __func__, plat->base, plat->size);
354 ret = mxs_of_get_timings(dev, &timings, &bpp);
358 ret = mxs_probe_common(dev, &timings, bpp, plat->base);
366 uc_priv->bpix = VIDEO_BPP32;
369 uc_priv->bpix = VIDEO_BPP16;
372 uc_priv->bpix = VIDEO_BPP8;
375 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
379 uc_priv->xsize = timings.hactive.typ;
380 uc_priv->ysize = timings.vactive.typ;
382 /* Enable dcache for the frame buffer */
383 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
384 fb_end = plat->base + plat->size;
385 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
386 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
388 video_set_flush_dcache(dev, true);
389 gd->fb_base = plat->base;
394 static int mxs_video_bind(struct udevice *dev)
396 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
397 struct display_timing timings;
402 ret = mxs_of_get_timings(dev, &timings, &bpp);
419 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
423 plat->size = timings.hactive.typ * timings.vactive.typ * bytes_pp;
428 static int mxs_video_remove(struct udevice *dev)
430 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
432 mxs_remove_common(plat->base);
437 static const struct udevice_id mxs_video_ids[] = {
438 { .compatible = "fsl,imx23-lcdif" },
439 { .compatible = "fsl,imx28-lcdif" },
440 { .compatible = "fsl,imx7ulp-lcdif" },
441 { .compatible = "fsl,imxrt-lcdif" },
445 U_BOOT_DRIVER(mxs_video) = {
448 .of_match = mxs_video_ids,
449 .bind = mxs_video_bind,
450 .probe = mxs_video_probe,
451 .remove = mxs_video_remove,
452 .flags = DM_FLAG_PRE_RELOC | DM_FLAG_OS_PREPARE,
454 #endif /* ifndef CONFIG_DM_VIDEO */