blob: f02ba201384619d8e9b07a78ed90a9f56ba79383 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutfb8ddc22013-04-28 09:20:03 +00002/*
3 * Freescale i.MX23/i.MX28 LCDIF driver
4 *
5 * Copyright (C) 2011-2013 Marek Vasut <marex@denx.de>
Marek Vasutfb8ddc22013-04-28 09:20:03 +00006 */
7#include <common.h>
Igor Opaniuk8c1df092019-06-04 00:05:59 +03008#include <dm.h>
Igor Opaniuk23816322019-06-04 00:05:57 +03009#include <linux/errno.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000010#include <malloc.h>
Igor Opaniuk8c1df092019-06-04 00:05:59 +030011#include <video.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000012#include <video_fb.h>
13
Marek Vasutfb8ddc22013-04-28 09:20:03 +000014#include <asm/arch/clock.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030015#include <asm/arch/imx-regs.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000016#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020017#include <asm/mach-imx/dma.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030018#include <asm/io.h>
Marek Vasut84f957f2013-07-30 23:37:54 +020019
Marek Vasutfb8ddc22013-04-28 09:20:03 +000020#include "videomodes.h"
21
22#define PS2KHZ(ps) (1000000000UL / (ps))
Igor Opaniuk8c1df092019-06-04 00:05:59 +030023#define HZ2PS(hz) (1000000000UL / ((hz) / 1000))
Marek Vasutfb8ddc22013-04-28 09:20:03 +000024
Igor Opaniuk8c1df092019-06-04 00:05:59 +030025#define BITS_PP 18
26#define BYTES_PP 4
27
Marek Vasut84f957f2013-07-30 23:37:54 +020028struct mxs_dma_desc desc;
Marek Vasutfb8ddc22013-04-28 09:20:03 +000029
Marek Vasut9de4b722013-07-30 23:37:53 +020030/**
31 * mxsfb_system_setup() - Fine-tune LCDIF configuration
32 *
33 * This function is used to adjust the LCDIF configuration. This is usually
34 * needed when driving the controller in System-Mode to operate an 8080 or
35 * 6800 connected SmartLCD.
36 */
37__weak void mxsfb_system_setup(void)
38{
39}
40
Marek Vasutfb8ddc22013-04-28 09:20:03 +000041/*
Marek Vasutfcea4802017-04-05 13:31:01 +020042 * ARIES M28EVK:
Marek Vasutfb8ddc22013-04-28 09:20:03 +000043 * setenv videomode
44 * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
45 * le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
Fabio Estevam11f98d12013-05-10 09:14:11 +000046 *
47 * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
48 * setenv videomode
49 * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
50 * le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
Marek Vasutfb8ddc22013-04-28 09:20:03 +000051 */
52
Igor Opaniukdcd91a62019-06-04 00:05:56 +030053static void mxs_lcd_init(u32 fb_addr, struct ctfb_res_modes *mode, int bpp)
Marek Vasutfb8ddc22013-04-28 09:20:03 +000054{
55 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
56 uint32_t word_len = 0, bus_width = 0;
57 uint8_t valid_data = 0;
58
59 /* Kick in the LCDIF clock */
Peng Fan95ae7002015-10-29 15:54:39 +080060 mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
Marek Vasutfb8ddc22013-04-28 09:20:03 +000061
62 /* Restart the LCDIF block */
63 mxs_reset_block(&regs->hw_lcdif_ctrl_reg);
64
65 switch (bpp) {
66 case 24:
67 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
68 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
69 valid_data = 0x7;
70 break;
71 case 18:
72 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
73 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
74 valid_data = 0x7;
75 break;
76 case 16:
77 word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
78 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
79 valid_data = 0xf;
80 break;
81 case 8:
82 word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
83 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
84 valid_data = 0xf;
85 break;
86 }
87
88 writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
89 LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
90 &regs->hw_lcdif_ctrl);
91
92 writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
93 &regs->hw_lcdif_ctrl1);
Marek Vasut9de4b722013-07-30 23:37:53 +020094
95 mxsfb_system_setup();
96
Marek Vasutfb8ddc22013-04-28 09:20:03 +000097 writel((mode->yres << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) | mode->xres,
98 &regs->hw_lcdif_transfer_count);
99
100 writel(LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
101 LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
102 LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
103 mode->vsync_len, &regs->hw_lcdif_vdctrl0);
104 writel(mode->upper_margin + mode->lower_margin +
105 mode->vsync_len + mode->yres,
106 &regs->hw_lcdif_vdctrl1);
107 writel((mode->hsync_len << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
108 (mode->left_margin + mode->right_margin +
109 mode->hsync_len + mode->xres),
110 &regs->hw_lcdif_vdctrl2);
111 writel(((mode->left_margin + mode->hsync_len) <<
112 LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
113 (mode->upper_margin + mode->vsync_len),
114 &regs->hw_lcdif_vdctrl3);
115 writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | mode->xres,
116 &regs->hw_lcdif_vdctrl4);
117
Igor Opaniukdcd91a62019-06-04 00:05:56 +0300118 writel(fb_addr, &regs->hw_lcdif_cur_buf);
119 writel(fb_addr, &regs->hw_lcdif_next_buf);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000120
121 /* Flush FIFO first */
122 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
123
Marek Vasut9de4b722013-07-30 23:37:53 +0200124#ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000125 /* Sync signals ON */
126 setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
Marek Vasut9de4b722013-07-30 23:37:53 +0200127#endif
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000128
129 /* FIFO cleared */
130 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_clr);
131
132 /* RUN! */
133 writel(LCDIF_CTRL_RUN, &regs->hw_lcdif_ctrl_set);
134}
135
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300136static int mxs_probe_common(struct ctfb_res_modes *mode, int bpp, u32 fb)
Igor Opaniuk9a672052019-06-04 00:05:58 +0300137{
138 /* Start framebuffer */
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300139 mxs_lcd_init(fb, mode, bpp);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300140
141#ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
142 /*
143 * If the LCD runs in system mode, the LCD refresh has to be triggered
144 * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
145 * having to set this bit manually after every single change in the
146 * framebuffer memory, we set up specially crafted circular DMA, which
147 * sets the RUN bit, then waits until it gets cleared and repeats this
148 * infinitelly. This way, we get smooth continuous updates of the LCD.
149 */
150 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
151
152 memset(&desc, 0, sizeof(struct mxs_dma_desc));
153 desc.address = (dma_addr_t)&desc;
154 desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
155 MXS_DMA_DESC_WAIT4END |
156 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
157 desc.cmd.pio_words[0] = readl(&regs->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
158 desc.cmd.next = (uint32_t)&desc.cmd;
159
160 /* Execute the DMA chain. */
161 mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
162#endif
163
164 return 0;
165}
166
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300167static int mxs_remove_common(u32 fb)
Peng Fana3c252d2015-10-29 15:54:49 +0800168{
169 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
170 int timeout = 1000000;
171
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300172 if (!fb)
173 return -EINVAL;
Fabio Estevamb24cf852017-02-22 10:40:22 -0300174
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300175 writel(fb, &regs->hw_lcdif_cur_buf_reg);
176 writel(fb, &regs->hw_lcdif_next_buf_reg);
Peng Fana3c252d2015-10-29 15:54:49 +0800177 writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, &regs->hw_lcdif_ctrl1_clr);
178 while (--timeout) {
179 if (readl(&regs->hw_lcdif_ctrl1_reg) &
180 LCDIF_CTRL1_VSYNC_EDGE_IRQ)
181 break;
182 udelay(1);
183 }
184 mxs_reset_block((struct mxs_register_32 *)&regs->hw_lcdif_ctrl_reg);
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300185
186 return 0;
187}
188
189#ifndef CONFIG_DM_VIDEO
190
191static GraphicDevice panel;
192
193void lcdif_power_down(void)
194{
195 mxs_remove_common(panel.frameAdrs);
Peng Fana3c252d2015-10-29 15:54:49 +0800196}
197
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000198void *video_hw_init(void)
199{
200 int bpp = -1;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300201 int ret = 0;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000202 char *penv;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300203 void *fb = NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000204 struct ctfb_res_modes mode;
205
206 puts("Video: ");
207
208 /* Suck display configuration from "videomode" variable */
Simon Glass00caae62017-08-03 12:22:12 -0600209 penv = env_get("videomode");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000210 if (!penv) {
Fabio Estevam620ca1c2013-06-26 16:08:13 -0300211 puts("MXSFB: 'videomode' variable not set!\n");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000212 return NULL;
213 }
214
215 bpp = video_get_params(&mode, penv);
216
217 /* fill in Graphic device struct */
Igor Opaniuk9a672052019-06-04 00:05:58 +0300218 sprintf(panel.modeIdent, "%dx%dx%d", mode.xres, mode.yres, bpp);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000219
220 panel.winSizeX = mode.xres;
221 panel.winSizeY = mode.yres;
222 panel.plnSizeX = mode.xres;
223 panel.plnSizeY = mode.yres;
224
225 switch (bpp) {
226 case 24:
227 case 18:
228 panel.gdfBytesPP = 4;
229 panel.gdfIndex = GDF_32BIT_X888RGB;
230 break;
231 case 16:
232 panel.gdfBytesPP = 2;
233 panel.gdfIndex = GDF_16BIT_565RGB;
234 break;
235 case 8:
236 panel.gdfBytesPP = 1;
237 panel.gdfIndex = GDF__8BIT_INDEX;
238 break;
239 default:
240 printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
241 return NULL;
242 }
243
244 panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
245
246 /* Allocate framebuffer */
Marek Vasute57baf52013-07-30 23:37:52 +0200247 fb = memalign(ARCH_DMA_MINALIGN,
248 roundup(panel.memSize, ARCH_DMA_MINALIGN));
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000249 if (!fb) {
250 printf("MXSFB: Error allocating framebuffer!\n");
251 return NULL;
252 }
253
254 /* Wipe framebuffer */
255 memset(fb, 0, panel.memSize);
256
257 panel.frameAdrs = (u32)fb;
258
259 printf("%s\n", panel.modeIdent);
260
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300261 ret = mxs_probe_common(&mode, bpp, (u32)fb);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300262 if (ret)
263 goto dealloc_fb;
Marek Vasut84f957f2013-07-30 23:37:54 +0200264
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000265 return (void *)&panel;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300266
267dealloc_fb:
268 free(fb);
269
270 return NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000271}
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300272#else /* ifndef CONFIG_DM_VIDEO */
273
274static int mxs_video_probe(struct udevice *dev)
275{
276 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
277 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
278
279 struct ctfb_res_modes mode;
280 struct display_timing timings;
281 int bpp = -1;
282 u32 fb_start, fb_end;
283 int ret;
284
285 debug("%s() plat: base 0x%lx, size 0x%x\n",
286 __func__, plat->base, plat->size);
287
288 ret = ofnode_decode_display_timing(dev_ofnode(dev), 0, &timings);
289 if (ret) {
290 dev_err(dev, "failed to get any display timings\n");
291 return -EINVAL;
292 }
293
294 mode.xres = timings.hactive.typ;
295 mode.yres = timings.vactive.typ;
296 mode.left_margin = timings.hback_porch.typ;
297 mode.right_margin = timings.hfront_porch.typ;
298 mode.upper_margin = timings.vback_porch.typ;
299 mode.lower_margin = timings.vfront_porch.typ;
300 mode.hsync_len = timings.hsync_len.typ;
301 mode.vsync_len = timings.vsync_len.typ;
302 mode.pixclock = HZ2PS(timings.pixelclock.typ);
303
304 bpp = BITS_PP;
305
306 ret = mxs_probe_common(&mode, bpp, plat->base);
307 if (ret)
308 return ret;
309
310 switch (bpp) {
311 case 24:
312 case 18:
313 uc_priv->bpix = VIDEO_BPP32;
314 break;
315 case 16:
316 uc_priv->bpix = VIDEO_BPP16;
317 break;
318 case 8:
319 uc_priv->bpix = VIDEO_BPP8;
320 break;
321 default:
322 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
323 return -EINVAL;
324 }
325
326 uc_priv->xsize = mode.xres;
327 uc_priv->ysize = mode.yres;
328
329 /* Enable dcache for the frame buffer */
330 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
331 fb_end = plat->base + plat->size;
332 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
333 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
334 DCACHE_WRITEBACK);
335 video_set_flush_dcache(dev, true);
336
337 return ret;
338}
339
340static int mxs_video_bind(struct udevice *dev)
341{
342 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
343 struct display_timing timings;
344 int ret;
345
346 ret = ofnode_decode_display_timing(dev_ofnode(dev), 0, &timings);
347 if (ret) {
348 dev_err(dev, "failed to get any display timings\n");
349 return -EINVAL;
350 }
351
352 plat->size = timings.hactive.typ * timings.vactive.typ * BYTES_PP;
353
354 return 0;
355}
356
357static int mxs_video_remove(struct udevice *dev)
358{
359 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
360
361 mxs_remove_common(plat->base);
362
363 return 0;
364}
365
366static const struct udevice_id mxs_video_ids[] = {
367 { .compatible = "fsl,imx23-lcdif" },
368 { .compatible = "fsl,imx28-lcdif" },
369 { .compatible = "fsl,imx7ulp-lcdif" },
370 { /* sentinel */ }
371};
372
373U_BOOT_DRIVER(mxs_video) = {
374 .name = "mxs_video",
375 .id = UCLASS_VIDEO,
376 .of_match = mxs_video_ids,
377 .bind = mxs_video_bind,
378 .probe = mxs_video_probe,
379 .remove = mxs_video_remove,
380 .flags = DM_FLAG_PRE_RELOC,
381};
382#endif /* ifndef CONFIG_DM_VIDEO */