blob: 6922a130c61a12ac922c124485f017deb4befbab [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>
Simon Glass7b51b572019-08-01 09:46:52 -06009#include <env.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030010#include <linux/errno.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000011#include <malloc.h>
Igor Opaniuk8c1df092019-06-04 00:05:59 +030012#include <video.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000013#include <video_fb.h>
14
Marek Vasutfb8ddc22013-04-28 09:20:03 +000015#include <asm/arch/clock.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030016#include <asm/arch/imx-regs.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000017#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020018#include <asm/mach-imx/dma.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030019#include <asm/io.h>
Marek Vasut84f957f2013-07-30 23:37:54 +020020
Marek Vasutfb8ddc22013-04-28 09:20:03 +000021#include "videomodes.h"
22
23#define PS2KHZ(ps) (1000000000UL / (ps))
Igor Opaniuk8c1df092019-06-04 00:05:59 +030024#define HZ2PS(hz) (1000000000UL / ((hz) / 1000))
Marek Vasutfb8ddc22013-04-28 09:20:03 +000025
Igor Opaniuk8c1df092019-06-04 00:05:59 +030026#define BITS_PP 18
27#define BYTES_PP 4
28
Marek Vasut84f957f2013-07-30 23:37:54 +020029struct mxs_dma_desc desc;
Marek Vasutfb8ddc22013-04-28 09:20:03 +000030
Marek Vasut9de4b722013-07-30 23:37:53 +020031/**
32 * mxsfb_system_setup() - Fine-tune LCDIF configuration
33 *
34 * This function is used to adjust the LCDIF configuration. This is usually
35 * needed when driving the controller in System-Mode to operate an 8080 or
36 * 6800 connected SmartLCD.
37 */
38__weak void mxsfb_system_setup(void)
39{
40}
41
Marek Vasutfb8ddc22013-04-28 09:20:03 +000042/*
Marek Vasutfcea4802017-04-05 13:31:01 +020043 * ARIES M28EVK:
Marek Vasutfb8ddc22013-04-28 09:20:03 +000044 * setenv videomode
45 * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
46 * le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
Fabio Estevam11f98d12013-05-10 09:14:11 +000047 *
48 * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
49 * setenv videomode
50 * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
51 * le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
Marek Vasutfb8ddc22013-04-28 09:20:03 +000052 */
53
Igor Opaniukdcd91a62019-06-04 00:05:56 +030054static void mxs_lcd_init(u32 fb_addr, struct ctfb_res_modes *mode, int bpp)
Marek Vasutfb8ddc22013-04-28 09:20:03 +000055{
56 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
57 uint32_t word_len = 0, bus_width = 0;
58 uint8_t valid_data = 0;
59
60 /* Kick in the LCDIF clock */
Peng Fan95ae7002015-10-29 15:54:39 +080061 mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
Marek Vasutfb8ddc22013-04-28 09:20:03 +000062
63 /* Restart the LCDIF block */
64 mxs_reset_block(&regs->hw_lcdif_ctrl_reg);
65
66 switch (bpp) {
67 case 24:
68 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
69 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
70 valid_data = 0x7;
71 break;
72 case 18:
73 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
74 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
75 valid_data = 0x7;
76 break;
77 case 16:
78 word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
79 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
80 valid_data = 0xf;
81 break;
82 case 8:
83 word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
84 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
85 valid_data = 0xf;
86 break;
87 }
88
89 writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
90 LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
91 &regs->hw_lcdif_ctrl);
92
93 writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
94 &regs->hw_lcdif_ctrl1);
Marek Vasut9de4b722013-07-30 23:37:53 +020095
96 mxsfb_system_setup();
97
Marek Vasutfb8ddc22013-04-28 09:20:03 +000098 writel((mode->yres << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) | mode->xres,
99 &regs->hw_lcdif_transfer_count);
100
101 writel(LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
102 LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
103 LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
104 mode->vsync_len, &regs->hw_lcdif_vdctrl0);
105 writel(mode->upper_margin + mode->lower_margin +
106 mode->vsync_len + mode->yres,
107 &regs->hw_lcdif_vdctrl1);
108 writel((mode->hsync_len << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
109 (mode->left_margin + mode->right_margin +
110 mode->hsync_len + mode->xres),
111 &regs->hw_lcdif_vdctrl2);
112 writel(((mode->left_margin + mode->hsync_len) <<
113 LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
114 (mode->upper_margin + mode->vsync_len),
115 &regs->hw_lcdif_vdctrl3);
116 writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | mode->xres,
117 &regs->hw_lcdif_vdctrl4);
118
Igor Opaniukdcd91a62019-06-04 00:05:56 +0300119 writel(fb_addr, &regs->hw_lcdif_cur_buf);
120 writel(fb_addr, &regs->hw_lcdif_next_buf);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000121
122 /* Flush FIFO first */
123 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
124
Marek Vasut9de4b722013-07-30 23:37:53 +0200125#ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000126 /* Sync signals ON */
127 setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
Marek Vasut9de4b722013-07-30 23:37:53 +0200128#endif
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000129
130 /* FIFO cleared */
131 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_clr);
132
133 /* RUN! */
134 writel(LCDIF_CTRL_RUN, &regs->hw_lcdif_ctrl_set);
135}
136
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300137static int mxs_probe_common(struct ctfb_res_modes *mode, int bpp, u32 fb)
Igor Opaniuk9a672052019-06-04 00:05:58 +0300138{
139 /* Start framebuffer */
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300140 mxs_lcd_init(fb, mode, bpp);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300141
142#ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
143 /*
144 * If the LCD runs in system mode, the LCD refresh has to be triggered
145 * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
146 * having to set this bit manually after every single change in the
147 * framebuffer memory, we set up specially crafted circular DMA, which
148 * sets the RUN bit, then waits until it gets cleared and repeats this
149 * infinitelly. This way, we get smooth continuous updates of the LCD.
150 */
151 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
152
153 memset(&desc, 0, sizeof(struct mxs_dma_desc));
154 desc.address = (dma_addr_t)&desc;
155 desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
156 MXS_DMA_DESC_WAIT4END |
157 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
158 desc.cmd.pio_words[0] = readl(&regs->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
159 desc.cmd.next = (uint32_t)&desc.cmd;
160
161 /* Execute the DMA chain. */
162 mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
163#endif
164
165 return 0;
166}
167
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300168static int mxs_remove_common(u32 fb)
Peng Fana3c252d2015-10-29 15:54:49 +0800169{
170 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
171 int timeout = 1000000;
172
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300173 if (!fb)
174 return -EINVAL;
Fabio Estevamb24cf852017-02-22 10:40:22 -0300175
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300176 writel(fb, &regs->hw_lcdif_cur_buf_reg);
177 writel(fb, &regs->hw_lcdif_next_buf_reg);
Peng Fana3c252d2015-10-29 15:54:49 +0800178 writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, &regs->hw_lcdif_ctrl1_clr);
179 while (--timeout) {
180 if (readl(&regs->hw_lcdif_ctrl1_reg) &
181 LCDIF_CTRL1_VSYNC_EDGE_IRQ)
182 break;
183 udelay(1);
184 }
185 mxs_reset_block((struct mxs_register_32 *)&regs->hw_lcdif_ctrl_reg);
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300186
187 return 0;
188}
189
190#ifndef CONFIG_DM_VIDEO
191
192static GraphicDevice panel;
193
194void lcdif_power_down(void)
195{
196 mxs_remove_common(panel.frameAdrs);
Peng Fana3c252d2015-10-29 15:54:49 +0800197}
198
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000199void *video_hw_init(void)
200{
201 int bpp = -1;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300202 int ret = 0;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000203 char *penv;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300204 void *fb = NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000205 struct ctfb_res_modes mode;
206
207 puts("Video: ");
208
209 /* Suck display configuration from "videomode" variable */
Simon Glass00caae62017-08-03 12:22:12 -0600210 penv = env_get("videomode");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000211 if (!penv) {
Fabio Estevam620ca1c2013-06-26 16:08:13 -0300212 puts("MXSFB: 'videomode' variable not set!\n");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000213 return NULL;
214 }
215
216 bpp = video_get_params(&mode, penv);
217
218 /* fill in Graphic device struct */
Igor Opaniuk9a672052019-06-04 00:05:58 +0300219 sprintf(panel.modeIdent, "%dx%dx%d", mode.xres, mode.yres, bpp);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000220
221 panel.winSizeX = mode.xres;
222 panel.winSizeY = mode.yres;
223 panel.plnSizeX = mode.xres;
224 panel.plnSizeY = mode.yres;
225
226 switch (bpp) {
227 case 24:
228 case 18:
229 panel.gdfBytesPP = 4;
230 panel.gdfIndex = GDF_32BIT_X888RGB;
231 break;
232 case 16:
233 panel.gdfBytesPP = 2;
234 panel.gdfIndex = GDF_16BIT_565RGB;
235 break;
236 case 8:
237 panel.gdfBytesPP = 1;
238 panel.gdfIndex = GDF__8BIT_INDEX;
239 break;
240 default:
241 printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
242 return NULL;
243 }
244
245 panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
246
247 /* Allocate framebuffer */
Marek Vasute57baf52013-07-30 23:37:52 +0200248 fb = memalign(ARCH_DMA_MINALIGN,
249 roundup(panel.memSize, ARCH_DMA_MINALIGN));
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000250 if (!fb) {
251 printf("MXSFB: Error allocating framebuffer!\n");
252 return NULL;
253 }
254
255 /* Wipe framebuffer */
256 memset(fb, 0, panel.memSize);
257
258 panel.frameAdrs = (u32)fb;
259
260 printf("%s\n", panel.modeIdent);
261
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300262 ret = mxs_probe_common(&mode, bpp, (u32)fb);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300263 if (ret)
264 goto dealloc_fb;
Marek Vasut84f957f2013-07-30 23:37:54 +0200265
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000266 return (void *)&panel;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300267
268dealloc_fb:
269 free(fb);
270
271 return NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000272}
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300273#else /* ifndef CONFIG_DM_VIDEO */
274
Igor Opaniuke19441e2019-06-19 11:47:05 +0300275static int mxs_of_get_timings(struct udevice *dev,
276 struct display_timing *timings,
277 u32 *bpp)
278{
279 int ret = 0;
280 u32 display_phandle;
281 ofnode display_node;
282
283 ret = ofnode_read_u32(dev_ofnode(dev), "display", &display_phandle);
284 if (ret) {
285 dev_err(dev, "required display property isn't provided\n");
286 return -EINVAL;
287 }
288
289 display_node = ofnode_get_by_phandle(display_phandle);
290 if (!ofnode_valid(display_node)) {
291 dev_err(dev, "failed to find display subnode\n");
292 return -EINVAL;
293 }
294
295 ret = ofnode_read_u32(display_node, "bits-per-pixel", bpp);
296 if (ret) {
297 dev_err(dev,
298 "required bits-per-pixel property isn't provided\n");
299 return -EINVAL;
300 }
301
302 ret = ofnode_decode_display_timing(display_node, 0, timings);
303 if (ret) {
304 dev_err(dev, "failed to get any display timings\n");
305 return -EINVAL;
306 }
307
308 return ret;
309}
310
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300311static int mxs_video_probe(struct udevice *dev)
312{
313 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
314 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
315
316 struct ctfb_res_modes mode;
317 struct display_timing timings;
Igor Opaniuke19441e2019-06-19 11:47:05 +0300318 u32 bpp = 0;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300319 u32 fb_start, fb_end;
320 int ret;
321
322 debug("%s() plat: base 0x%lx, size 0x%x\n",
323 __func__, plat->base, plat->size);
324
Igor Opaniuke19441e2019-06-19 11:47:05 +0300325 ret = mxs_of_get_timings(dev, &timings, &bpp);
326 if (ret)
327 return ret;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300328
329 mode.xres = timings.hactive.typ;
330 mode.yres = timings.vactive.typ;
331 mode.left_margin = timings.hback_porch.typ;
332 mode.right_margin = timings.hfront_porch.typ;
333 mode.upper_margin = timings.vback_porch.typ;
334 mode.lower_margin = timings.vfront_porch.typ;
335 mode.hsync_len = timings.hsync_len.typ;
336 mode.vsync_len = timings.vsync_len.typ;
337 mode.pixclock = HZ2PS(timings.pixelclock.typ);
338
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300339 ret = mxs_probe_common(&mode, bpp, plat->base);
340 if (ret)
341 return ret;
342
343 switch (bpp) {
Igor Opaniuke19441e2019-06-19 11:47:05 +0300344 case 32:
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300345 case 24:
346 case 18:
347 uc_priv->bpix = VIDEO_BPP32;
348 break;
349 case 16:
350 uc_priv->bpix = VIDEO_BPP16;
351 break;
352 case 8:
353 uc_priv->bpix = VIDEO_BPP8;
354 break;
355 default:
356 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
357 return -EINVAL;
358 }
359
360 uc_priv->xsize = mode.xres;
361 uc_priv->ysize = mode.yres;
362
363 /* Enable dcache for the frame buffer */
364 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
365 fb_end = plat->base + plat->size;
366 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
367 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
368 DCACHE_WRITEBACK);
369 video_set_flush_dcache(dev, true);
370
371 return ret;
372}
373
374static int mxs_video_bind(struct udevice *dev)
375{
376 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
377 struct display_timing timings;
Igor Opaniuke19441e2019-06-19 11:47:05 +0300378 u32 bpp = 0;
379 u32 bytes_pp = 0;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300380 int ret;
381
Igor Opaniuke19441e2019-06-19 11:47:05 +0300382 ret = mxs_of_get_timings(dev, &timings, &bpp);
383 if (ret)
384 return ret;
385
386 switch (bpp) {
387 case 32:
388 case 24:
389 case 18:
390 bytes_pp = 4;
391 break;
392 case 16:
393 bytes_pp = 2;
394 break;
395 case 8:
396 bytes_pp = 1;
397 break;
398 default:
399 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300400 return -EINVAL;
401 }
402
Igor Opaniuke19441e2019-06-19 11:47:05 +0300403 plat->size = timings.hactive.typ * timings.vactive.typ * bytes_pp;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300404
405 return 0;
406}
407
408static int mxs_video_remove(struct udevice *dev)
409{
410 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
411
412 mxs_remove_common(plat->base);
413
414 return 0;
415}
416
417static const struct udevice_id mxs_video_ids[] = {
418 { .compatible = "fsl,imx23-lcdif" },
419 { .compatible = "fsl,imx28-lcdif" },
420 { .compatible = "fsl,imx7ulp-lcdif" },
421 { /* sentinel */ }
422};
423
424U_BOOT_DRIVER(mxs_video) = {
425 .name = "mxs_video",
426 .id = UCLASS_VIDEO,
427 .of_match = mxs_video_ids,
428 .bind = mxs_video_bind,
429 .probe = mxs_video_probe,
430 .remove = mxs_video_remove,
431 .flags = DM_FLAG_PRE_RELOC,
432};
433#endif /* ifndef CONFIG_DM_VIDEO */