blob: 6c9a7c05e828d6cc9e3ce6c099ac5eb4ecffe90c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutffdd4662013-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 Vasutffdd4662013-04-28 09:20:03 +00006 */
7#include <common.h>
Igor Opaniukf5abe402019-06-04 00:05:59 +03008#include <dm.h>
Igor Opaniuke2a8b182019-06-04 00:05:57 +03009#include <linux/errno.h>
Marek Vasutffdd4662013-04-28 09:20:03 +000010#include <malloc.h>
Igor Opaniukf5abe402019-06-04 00:05:59 +030011#include <video.h>
Marek Vasutffdd4662013-04-28 09:20:03 +000012#include <video_fb.h>
13
Marek Vasutffdd4662013-04-28 09:20:03 +000014#include <asm/arch/clock.h>
Igor Opaniuke2a8b182019-06-04 00:05:57 +030015#include <asm/arch/imx-regs.h>
Marek Vasutffdd4662013-04-28 09:20:03 +000016#include <asm/arch/sys_proto.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020017#include <asm/mach-imx/dma.h>
Igor Opaniuke2a8b182019-06-04 00:05:57 +030018#include <asm/io.h>
Marek Vasut8f15b5d2013-07-30 23:37:54 +020019
Marek Vasutffdd4662013-04-28 09:20:03 +000020#include "videomodes.h"
21
22#define PS2KHZ(ps) (1000000000UL / (ps))
Igor Opaniukf5abe402019-06-04 00:05:59 +030023#define HZ2PS(hz) (1000000000UL / ((hz) / 1000))
Marek Vasutffdd4662013-04-28 09:20:03 +000024
Igor Opaniukf5abe402019-06-04 00:05:59 +030025#define BITS_PP 18
26#define BYTES_PP 4
27
Marek Vasut8f15b5d2013-07-30 23:37:54 +020028struct mxs_dma_desc desc;
Marek Vasutffdd4662013-04-28 09:20:03 +000029
Marek Vasutcd701a12013-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 Vasutffdd4662013-04-28 09:20:03 +000041/*
Marek Vasutec58ab22017-04-05 13:31:01 +020042 * ARIES M28EVK:
Marek Vasutffdd4662013-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 Estevama2824192013-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 Vasutffdd4662013-04-28 09:20:03 +000051 */
52
Igor Opaniuk5fd35532019-06-04 00:05:56 +030053static void mxs_lcd_init(u32 fb_addr, struct ctfb_res_modes *mode, int bpp)
Marek Vasutffdd4662013-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 Fan93320422015-10-29 15:54:39 +080060 mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
Marek Vasutffdd4662013-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 Vasutcd701a12013-07-30 23:37:53 +020094
95 mxsfb_system_setup();
96
Marek Vasutffdd4662013-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 Opaniuk5fd35532019-06-04 00:05:56 +0300118 writel(fb_addr, &regs->hw_lcdif_cur_buf);
119 writel(fb_addr, &regs->hw_lcdif_next_buf);
Marek Vasutffdd4662013-04-28 09:20:03 +0000120
121 /* Flush FIFO first */
122 writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
123
Marek Vasutcd701a12013-07-30 23:37:53 +0200124#ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
Marek Vasutffdd4662013-04-28 09:20:03 +0000125 /* Sync signals ON */
126 setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
Marek Vasutcd701a12013-07-30 23:37:53 +0200127#endif
Marek Vasutffdd4662013-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 Opaniukf5abe402019-06-04 00:05:59 +0300136static int mxs_probe_common(struct ctfb_res_modes *mode, int bpp, u32 fb)
Igor Opaniuk36734922019-06-04 00:05:58 +0300137{
138 /* Start framebuffer */
Igor Opaniukf5abe402019-06-04 00:05:59 +0300139 mxs_lcd_init(fb, mode, bpp);
Igor Opaniuk36734922019-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 Opaniukf5abe402019-06-04 00:05:59 +0300167static int mxs_remove_common(u32 fb)
Peng Fan5f8dbf52015-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 Opaniukf5abe402019-06-04 00:05:59 +0300172 if (!fb)
173 return -EINVAL;
Fabio Estevamef84cf62017-02-22 10:40:22 -0300174
Igor Opaniukf5abe402019-06-04 00:05:59 +0300175 writel(fb, &regs->hw_lcdif_cur_buf_reg);
176 writel(fb, &regs->hw_lcdif_next_buf_reg);
Peng Fan5f8dbf52015-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 Opaniukf5abe402019-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 Fan5f8dbf52015-10-29 15:54:49 +0800196}
197
Marek Vasutffdd4662013-04-28 09:20:03 +0000198void *video_hw_init(void)
199{
200 int bpp = -1;
Igor Opaniuk36734922019-06-04 00:05:58 +0300201 int ret = 0;
Marek Vasutffdd4662013-04-28 09:20:03 +0000202 char *penv;
Igor Opaniuk36734922019-06-04 00:05:58 +0300203 void *fb = NULL;
Marek Vasutffdd4662013-04-28 09:20:03 +0000204 struct ctfb_res_modes mode;
205
206 puts("Video: ");
207
208 /* Suck display configuration from "videomode" variable */
Simon Glass64b723f2017-08-03 12:22:12 -0600209 penv = env_get("videomode");
Marek Vasutffdd4662013-04-28 09:20:03 +0000210 if (!penv) {
Fabio Estevam56147832013-06-26 16:08:13 -0300211 puts("MXSFB: 'videomode' variable not set!\n");
Marek Vasutffdd4662013-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 Opaniuk36734922019-06-04 00:05:58 +0300218 sprintf(panel.modeIdent, "%dx%dx%d", mode.xres, mode.yres, bpp);
Marek Vasutffdd4662013-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 Vasutce74fac2013-07-30 23:37:52 +0200247 fb = memalign(ARCH_DMA_MINALIGN,
248 roundup(panel.memSize, ARCH_DMA_MINALIGN));
Marek Vasutffdd4662013-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 Opaniukf5abe402019-06-04 00:05:59 +0300261 ret = mxs_probe_common(&mode, bpp, (u32)fb);
Igor Opaniuk36734922019-06-04 00:05:58 +0300262 if (ret)
263 goto dealloc_fb;
Marek Vasut8f15b5d2013-07-30 23:37:54 +0200264
Igor Opaniuk36734922019-06-04 00:05:58 +0300265 return (void *)&panel;
Marek Vasut8f15b5d2013-07-30 23:37:54 +0200266
Igor Opaniuk36734922019-06-04 00:05:58 +0300267dealloc_fb:
268 free(fb);
Marek Vasut8f15b5d2013-07-30 23:37:54 +0200269
Igor Opaniuk36734922019-06-04 00:05:58 +0300270 return NULL;
Marek Vasutffdd4662013-04-28 09:20:03 +0000271}
Igor Opaniukf5abe402019-06-04 00:05:59 +0300272#else /* ifndef CONFIG_DM_VIDEO */
273
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300274static int mxs_of_get_timings(struct udevice *dev,
275 struct display_timing *timings,
276 u32 *bpp)
277{
278 int ret = 0;
279 u32 display_phandle;
280 ofnode display_node;
281
282 ret = ofnode_read_u32(dev_ofnode(dev), "display", &display_phandle);
283 if (ret) {
284 dev_err(dev, "required display property isn't provided\n");
285 return -EINVAL;
286 }
287
288 display_node = ofnode_get_by_phandle(display_phandle);
289 if (!ofnode_valid(display_node)) {
290 dev_err(dev, "failed to find display subnode\n");
291 return -EINVAL;
292 }
293
294 ret = ofnode_read_u32(display_node, "bits-per-pixel", bpp);
295 if (ret) {
296 dev_err(dev,
297 "required bits-per-pixel property isn't provided\n");
298 return -EINVAL;
299 }
300
301 ret = ofnode_decode_display_timing(display_node, 0, timings);
302 if (ret) {
303 dev_err(dev, "failed to get any display timings\n");
304 return -EINVAL;
305 }
306
307 return ret;
308}
309
Igor Opaniukf5abe402019-06-04 00:05:59 +0300310static int mxs_video_probe(struct udevice *dev)
311{
312 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
313 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
314
315 struct ctfb_res_modes mode;
316 struct display_timing timings;
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300317 u32 bpp = 0;
Igor Opaniukf5abe402019-06-04 00:05:59 +0300318 u32 fb_start, fb_end;
319 int ret;
320
321 debug("%s() plat: base 0x%lx, size 0x%x\n",
322 __func__, plat->base, plat->size);
323
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300324 ret = mxs_of_get_timings(dev, &timings, &bpp);
325 if (ret)
326 return ret;
Igor Opaniukf5abe402019-06-04 00:05:59 +0300327
328 mode.xres = timings.hactive.typ;
329 mode.yres = timings.vactive.typ;
330 mode.left_margin = timings.hback_porch.typ;
331 mode.right_margin = timings.hfront_porch.typ;
332 mode.upper_margin = timings.vback_porch.typ;
333 mode.lower_margin = timings.vfront_porch.typ;
334 mode.hsync_len = timings.hsync_len.typ;
335 mode.vsync_len = timings.vsync_len.typ;
336 mode.pixclock = HZ2PS(timings.pixelclock.typ);
337
Igor Opaniukf5abe402019-06-04 00:05:59 +0300338 ret = mxs_probe_common(&mode, bpp, plat->base);
339 if (ret)
340 return ret;
341
342 switch (bpp) {
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300343 case 32:
Igor Opaniukf5abe402019-06-04 00:05:59 +0300344 case 24:
345 case 18:
346 uc_priv->bpix = VIDEO_BPP32;
347 break;
348 case 16:
349 uc_priv->bpix = VIDEO_BPP16;
350 break;
351 case 8:
352 uc_priv->bpix = VIDEO_BPP8;
353 break;
354 default:
355 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
356 return -EINVAL;
357 }
358
359 uc_priv->xsize = mode.xres;
360 uc_priv->ysize = mode.yres;
361
362 /* Enable dcache for the frame buffer */
363 fb_start = plat->base & ~(MMU_SECTION_SIZE - 1);
364 fb_end = plat->base + plat->size;
365 fb_end = ALIGN(fb_end, 1 << MMU_SECTION_SHIFT);
366 mmu_set_region_dcache_behaviour(fb_start, fb_end - fb_start,
367 DCACHE_WRITEBACK);
368 video_set_flush_dcache(dev, true);
369
370 return ret;
371}
372
373static int mxs_video_bind(struct udevice *dev)
374{
375 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
376 struct display_timing timings;
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300377 u32 bpp = 0;
378 u32 bytes_pp = 0;
Igor Opaniukf5abe402019-06-04 00:05:59 +0300379 int ret;
380
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300381 ret = mxs_of_get_timings(dev, &timings, &bpp);
382 if (ret)
383 return ret;
384
385 switch (bpp) {
386 case 32:
387 case 24:
388 case 18:
389 bytes_pp = 4;
390 break;
391 case 16:
392 bytes_pp = 2;
393 break;
394 case 8:
395 bytes_pp = 1;
396 break;
397 default:
398 dev_err(dev, "invalid bpp specified (bpp = %i)\n", bpp);
Igor Opaniukf5abe402019-06-04 00:05:59 +0300399 return -EINVAL;
400 }
401
Igor Opaniuk994f49a2019-06-19 11:47:05 +0300402 plat->size = timings.hactive.typ * timings.vactive.typ * bytes_pp;
Igor Opaniukf5abe402019-06-04 00:05:59 +0300403
404 return 0;
405}
406
407static int mxs_video_remove(struct udevice *dev)
408{
409 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
410
411 mxs_remove_common(plat->base);
412
413 return 0;
414}
415
416static const struct udevice_id mxs_video_ids[] = {
417 { .compatible = "fsl,imx23-lcdif" },
418 { .compatible = "fsl,imx28-lcdif" },
419 { .compatible = "fsl,imx7ulp-lcdif" },
420 { /* sentinel */ }
421};
422
423U_BOOT_DRIVER(mxs_video) = {
424 .name = "mxs_video",
425 .id = UCLASS_VIDEO,
426 .of_match = mxs_video_ids,
427 .bind = mxs_video_bind,
428 .probe = mxs_video_probe,
429 .remove = mxs_video_remove,
430 .flags = DM_FLAG_PRE_RELOC,
431};
432#endif /* ifndef CONFIG_DM_VIDEO */