blob: 585af3d5714635c774b3554aa487581fc445c8f5 [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>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030011#include <linux/errno.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000012#include <malloc.h>
Igor Opaniuk8c1df092019-06-04 00:05:59 +030013#include <video.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000014#include <video_fb.h>
15
Marek Vasutfb8ddc22013-04-28 09:20:03 +000016#include <asm/arch/clock.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030017#include <asm/arch/imx-regs.h>
Marek Vasutfb8ddc22013-04-28 09:20:03 +000018#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020019#include <asm/mach-imx/dma.h>
Igor Opaniuk23816322019-06-04 00:05:57 +030020#include <asm/io.h>
Marek Vasut84f957f2013-07-30 23:37:54 +020021
Marek Vasutfb8ddc22013-04-28 09:20:03 +000022#include "videomodes.h"
23
24#define PS2KHZ(ps) (1000000000UL / (ps))
Igor Opaniuk8c1df092019-06-04 00:05:59 +030025#define HZ2PS(hz) (1000000000UL / ((hz) / 1000))
Marek Vasutfb8ddc22013-04-28 09:20:03 +000026
Igor Opaniuk8c1df092019-06-04 00:05:59 +030027#define BITS_PP 18
28#define BYTES_PP 4
29
Marek Vasut84f957f2013-07-30 23:37:54 +020030struct mxs_dma_desc desc;
Marek Vasutfb8ddc22013-04-28 09:20:03 +000031
Marek Vasut9de4b722013-07-30 23:37:53 +020032/**
33 * mxsfb_system_setup() - Fine-tune LCDIF configuration
34 *
35 * This function is used to adjust the LCDIF configuration. This is usually
36 * needed when driving the controller in System-Mode to operate an 8080 or
37 * 6800 connected SmartLCD.
38 */
39__weak void mxsfb_system_setup(void)
40{
41}
42
Marek Vasutfb8ddc22013-04-28 09:20:03 +000043/*
Marek Vasutfcea4802017-04-05 13:31:01 +020044 * ARIES M28EVK:
Marek Vasutfb8ddc22013-04-28 09:20:03 +000045 * setenv videomode
46 * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
47 * le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
Fabio Estevam11f98d12013-05-10 09:14:11 +000048 *
49 * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
50 * setenv videomode
51 * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
52 * le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
Marek Vasutfb8ddc22013-04-28 09:20:03 +000053 */
54
Igor Opaniukdcd91a62019-06-04 00:05:56 +030055static void mxs_lcd_init(u32 fb_addr, struct ctfb_res_modes *mode, int bpp)
Marek Vasutfb8ddc22013-04-28 09:20:03 +000056{
57 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
58 uint32_t word_len = 0, bus_width = 0;
59 uint8_t valid_data = 0;
60
Fabio Estevambeeb57f2019-11-24 17:37:52 -030061 /* Kick in the LCDIF clock */
62 mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
63
Marek Vasutfb8ddc22013-04-28 09:20:03 +000064 /* Restart the LCDIF block */
65 mxs_reset_block(&regs->hw_lcdif_ctrl_reg);
66
67 switch (bpp) {
68 case 24:
69 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
70 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
71 valid_data = 0x7;
72 break;
73 case 18:
74 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
75 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
76 valid_data = 0x7;
77 break;
78 case 16:
79 word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
80 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
81 valid_data = 0xf;
82 break;
83 case 8:
84 word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
85 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
86 valid_data = 0xf;
87 break;
88 }
89
90 writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
91 LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
92 &regs->hw_lcdif_ctrl);
93
94 writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
95 &regs->hw_lcdif_ctrl1);
Marek Vasut9de4b722013-07-30 23:37:53 +020096
97 mxsfb_system_setup();
98
Marek Vasutfb8ddc22013-04-28 09:20:03 +000099 writel((mode->yres << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) | mode->xres,
100 &regs->hw_lcdif_transfer_count);
101
102 writel(LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
103 LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
104 LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
105 mode->vsync_len, &regs->hw_lcdif_vdctrl0);
106 writel(mode->upper_margin + mode->lower_margin +
107 mode->vsync_len + mode->yres,
108 &regs->hw_lcdif_vdctrl1);
109 writel((mode->hsync_len << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
110 (mode->left_margin + mode->right_margin +
111 mode->hsync_len + mode->xres),
112 &regs->hw_lcdif_vdctrl2);
113 writel(((mode->left_margin + mode->hsync_len) <<
114 LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
115 (mode->upper_margin + mode->vsync_len),
116 &regs->hw_lcdif_vdctrl3);
117 writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | mode->xres,
118 &regs->hw_lcdif_vdctrl4);
119
Igor Opaniukdcd91a62019-06-04 00:05:56 +0300120 writel(fb_addr, &regs->hw_lcdif_cur_buf);
121 writel(fb_addr, &regs->hw_lcdif_next_buf);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000122
123 /* Flush FIFO first */
124 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
125
Marek Vasut9de4b722013-07-30 23:37:53 +0200126#ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000127 /* Sync signals ON */
128 setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
Marek Vasut9de4b722013-07-30 23:37:53 +0200129#endif
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000130
131 /* FIFO cleared */
132 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_clr);
133
134 /* RUN! */
135 writel(LCDIF_CTRL_RUN, &regs->hw_lcdif_ctrl_set);
136}
137
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300138static int mxs_probe_common(struct ctfb_res_modes *mode, int bpp, u32 fb)
Igor Opaniuk9a672052019-06-04 00:05:58 +0300139{
140 /* Start framebuffer */
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300141 mxs_lcd_init(fb, mode, bpp);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300142
143#ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
144 /*
145 * If the LCD runs in system mode, the LCD refresh has to be triggered
146 * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
147 * having to set this bit manually after every single change in the
148 * framebuffer memory, we set up specially crafted circular DMA, which
149 * sets the RUN bit, then waits until it gets cleared and repeats this
150 * infinitelly. This way, we get smooth continuous updates of the LCD.
151 */
152 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
153
154 memset(&desc, 0, sizeof(struct mxs_dma_desc));
155 desc.address = (dma_addr_t)&desc;
156 desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
157 MXS_DMA_DESC_WAIT4END |
158 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
159 desc.cmd.pio_words[0] = readl(&regs->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
160 desc.cmd.next = (uint32_t)&desc.cmd;
161
162 /* Execute the DMA chain. */
163 mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
164#endif
165
166 return 0;
167}
168
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300169static int mxs_remove_common(u32 fb)
Peng Fana3c252d2015-10-29 15:54:49 +0800170{
171 struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
172 int timeout = 1000000;
173
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300174 if (!fb)
175 return -EINVAL;
Fabio Estevamb24cf852017-02-22 10:40:22 -0300176
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300177 writel(fb, &regs->hw_lcdif_cur_buf_reg);
178 writel(fb, &regs->hw_lcdif_next_buf_reg);
Peng Fana3c252d2015-10-29 15:54:49 +0800179 writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, &regs->hw_lcdif_ctrl1_clr);
180 while (--timeout) {
181 if (readl(&regs->hw_lcdif_ctrl1_reg) &
182 LCDIF_CTRL1_VSYNC_EDGE_IRQ)
183 break;
184 udelay(1);
185 }
186 mxs_reset_block((struct mxs_register_32 *)&regs->hw_lcdif_ctrl_reg);
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300187
188 return 0;
189}
190
191#ifndef CONFIG_DM_VIDEO
192
193static GraphicDevice panel;
194
195void lcdif_power_down(void)
196{
197 mxs_remove_common(panel.frameAdrs);
Peng Fana3c252d2015-10-29 15:54:49 +0800198}
199
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000200void *video_hw_init(void)
201{
202 int bpp = -1;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300203 int ret = 0;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000204 char *penv;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300205 void *fb = NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000206 struct ctfb_res_modes mode;
207
208 puts("Video: ");
209
210 /* Suck display configuration from "videomode" variable */
Simon Glass00caae62017-08-03 12:22:12 -0600211 penv = env_get("videomode");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000212 if (!penv) {
Fabio Estevam620ca1c2013-06-26 16:08:13 -0300213 puts("MXSFB: 'videomode' variable not set!\n");
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000214 return NULL;
215 }
216
217 bpp = video_get_params(&mode, penv);
218
219 /* fill in Graphic device struct */
Igor Opaniuk9a672052019-06-04 00:05:58 +0300220 sprintf(panel.modeIdent, "%dx%dx%d", mode.xres, mode.yres, bpp);
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000221
222 panel.winSizeX = mode.xres;
223 panel.winSizeY = mode.yres;
224 panel.plnSizeX = mode.xres;
225 panel.plnSizeY = mode.yres;
226
227 switch (bpp) {
228 case 24:
229 case 18:
230 panel.gdfBytesPP = 4;
231 panel.gdfIndex = GDF_32BIT_X888RGB;
232 break;
233 case 16:
234 panel.gdfBytesPP = 2;
235 panel.gdfIndex = GDF_16BIT_565RGB;
236 break;
237 case 8:
238 panel.gdfBytesPP = 1;
239 panel.gdfIndex = GDF__8BIT_INDEX;
240 break;
241 default:
242 printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
243 return NULL;
244 }
245
246 panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
247
248 /* Allocate framebuffer */
Marek Vasute57baf52013-07-30 23:37:52 +0200249 fb = memalign(ARCH_DMA_MINALIGN,
250 roundup(panel.memSize, ARCH_DMA_MINALIGN));
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000251 if (!fb) {
252 printf("MXSFB: Error allocating framebuffer!\n");
253 return NULL;
254 }
255
256 /* Wipe framebuffer */
257 memset(fb, 0, panel.memSize);
258
259 panel.frameAdrs = (u32)fb;
260
261 printf("%s\n", panel.modeIdent);
262
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300263 ret = mxs_probe_common(&mode, bpp, (u32)fb);
Igor Opaniuk9a672052019-06-04 00:05:58 +0300264 if (ret)
265 goto dealloc_fb;
Marek Vasut84f957f2013-07-30 23:37:54 +0200266
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000267 return (void *)&panel;
Igor Opaniuk9a672052019-06-04 00:05:58 +0300268
269dealloc_fb:
270 free(fb);
271
272 return NULL;
Marek Vasutfb8ddc22013-04-28 09:20:03 +0000273}
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300274#else /* ifndef CONFIG_DM_VIDEO */
275
Igor Opaniuke19441e2019-06-19 11:47:05 +0300276static int mxs_of_get_timings(struct udevice *dev,
277 struct display_timing *timings,
278 u32 *bpp)
279{
280 int ret = 0;
281 u32 display_phandle;
282 ofnode display_node;
283
284 ret = ofnode_read_u32(dev_ofnode(dev), "display", &display_phandle);
285 if (ret) {
286 dev_err(dev, "required display property isn't provided\n");
287 return -EINVAL;
288 }
289
290 display_node = ofnode_get_by_phandle(display_phandle);
291 if (!ofnode_valid(display_node)) {
292 dev_err(dev, "failed to find display subnode\n");
293 return -EINVAL;
294 }
295
296 ret = ofnode_read_u32(display_node, "bits-per-pixel", bpp);
297 if (ret) {
298 dev_err(dev,
299 "required bits-per-pixel property isn't provided\n");
300 return -EINVAL;
301 }
302
303 ret = ofnode_decode_display_timing(display_node, 0, timings);
304 if (ret) {
305 dev_err(dev, "failed to get any display timings\n");
306 return -EINVAL;
307 }
308
309 return ret;
310}
311
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300312static int mxs_video_probe(struct udevice *dev)
313{
314 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
315 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
316
317 struct ctfb_res_modes mode;
318 struct display_timing timings;
Igor Opaniuke19441e2019-06-19 11:47:05 +0300319 u32 bpp = 0;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300320 u32 fb_start, fb_end;
321 int ret;
322
323 debug("%s() plat: base 0x%lx, size 0x%x\n",
324 __func__, plat->base, plat->size);
325
Igor Opaniuke19441e2019-06-19 11:47:05 +0300326 ret = mxs_of_get_timings(dev, &timings, &bpp);
327 if (ret)
328 return ret;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300329
330 mode.xres = timings.hactive.typ;
331 mode.yres = timings.vactive.typ;
332 mode.left_margin = timings.hback_porch.typ;
333 mode.right_margin = timings.hfront_porch.typ;
334 mode.upper_margin = timings.vback_porch.typ;
335 mode.lower_margin = timings.vfront_porch.typ;
336 mode.hsync_len = timings.hsync_len.typ;
337 mode.vsync_len = timings.vsync_len.typ;
338 mode.pixclock = HZ2PS(timings.pixelclock.typ);
339
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300340 ret = mxs_probe_common(&mode, bpp, plat->base);
341 if (ret)
342 return ret;
343
344 switch (bpp) {
Igor Opaniuke19441e2019-06-19 11:47:05 +0300345 case 32:
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300346 case 24:
347 case 18:
348 uc_priv->bpix = VIDEO_BPP32;
349 break;
350 case 16:
351 uc_priv->bpix = VIDEO_BPP16;
352 break;
353 case 8:
354 uc_priv->bpix = VIDEO_BPP8;
355 break;
356 default:
357 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
358 return -EINVAL;
359 }
360
361 uc_priv->xsize = mode.xres;
362 uc_priv->ysize = mode.yres;
363
364 /* Enable dcache for the frame buffer */
365 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
366 fb_end = plat->base + plat->size;
367 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
368 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
369 DCACHE_WRITEBACK);
370 video_set_flush_dcache(dev, true);
Sébastien Szymanskicde421c2019-10-21 15:33:04 +0200371 gd->fb_base = plat->base;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300372
373 return ret;
374}
375
376static int mxs_video_bind(struct udevice *dev)
377{
378 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
379 struct display_timing timings;
Igor Opaniuke19441e2019-06-19 11:47:05 +0300380 u32 bpp = 0;
381 u32 bytes_pp = 0;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300382 int ret;
383
Igor Opaniuke19441e2019-06-19 11:47:05 +0300384 ret = mxs_of_get_timings(dev, &timings, &bpp);
385 if (ret)
386 return ret;
387
388 switch (bpp) {
389 case 32:
390 case 24:
391 case 18:
392 bytes_pp = 4;
393 break;
394 case 16:
395 bytes_pp = 2;
396 break;
397 case 8:
398 bytes_pp = 1;
399 break;
400 default:
401 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300402 return -EINVAL;
403 }
404
Igor Opaniuke19441e2019-06-19 11:47:05 +0300405 plat->size = timings.hactive.typ * timings.vactive.typ * bytes_pp;
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300406
407 return 0;
408}
409
410static int mxs_video_remove(struct udevice *dev)
411{
412 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
413
414 mxs_remove_common(plat->base);
415
416 return 0;
417}
418
419static const struct udevice_id mxs_video_ids[] = {
420 { .compatible = "fsl,imx23-lcdif" },
421 { .compatible = "fsl,imx28-lcdif" },
422 { .compatible = "fsl,imx7ulp-lcdif" },
423 { /* sentinel */ }
424};
425
426U_BOOT_DRIVER(mxs_video) = {
427 .name = "mxs_video",
428 .id = UCLASS_VIDEO,
429 .of_match = mxs_video_ids,
430 .bind = mxs_video_bind,
431 .probe = mxs_video_probe,
432 .remove = mxs_video_remove,
Anatolij Gustschin8382b102020-01-25 23:44:56 +0100433 .flags = DM_FLAG_PRE_RELOC | DM_FLAG_OS_PREPARE,
Igor Opaniuk8c1df092019-06-04 00:05:59 +0300434};
435#endif /* ifndef CONFIG_DM_VIDEO */