blob: 89bc0eeb680acd72bcd891248fa6e09215a832d9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shen8dd55282012-05-25 00:59:58 +00002/*
3 * Driver for AT91/AT32 MULTI LAYER LCD Controller
4 *
5 * Copyright (C) 2012 Atmel Corporation
Bo Shen8dd55282012-05-25 00:59:58 +00006 */
7
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <part.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Bo Shen8dd55282012-05-25 00:59:58 +000013#include <asm/io.h>
14#include <asm/arch/gpio.h>
15#include <asm/arch/clk.h>
Songjun Wu72ac56a2017-04-11 16:33:30 +080016#include <clk.h>
17#include <dm.h>
18#include <fdtdec.h>
Songjun Wu72ac56a2017-04-11 16:33:30 +080019#include <video.h>
20#include <wait_bit.h>
Bo Shen8dd55282012-05-25 00:59:58 +000021#include <atmel_hlcdc.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060022#include <linux/bug.h>
Bo Shen8dd55282012-05-25 00:59:58 +000023
Songjun Wu72ac56a2017-04-11 16:33:30 +080024DECLARE_GLOBAL_DATA_PTR;
25
Songjun Wu72ac56a2017-04-11 16:33:30 +080026enum {
27 LCD_MAX_WIDTH = 1024,
28 LCD_MAX_HEIGHT = 768,
29 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
30};
31
32struct atmel_hlcdc_priv {
33 struct atmel_hlcd_regs *regs;
34 struct display_timing timing;
35 unsigned int vl_bpix;
36 unsigned int output_mode;
37 unsigned int guard_time;
38 ulong clk_rate;
39};
40
41static int at91_hlcdc_enable_clk(struct udevice *dev)
42{
43 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
44 struct clk clk;
45 ulong clk_rate;
46 int ret;
47
48 ret = clk_get_by_index(dev, 0, &clk);
49 if (ret)
50 return -EINVAL;
51
52 ret = clk_enable(&clk);
53 if (ret)
54 return ret;
55
56 clk_rate = clk_get_rate(&clk);
57 if (!clk_rate) {
58 clk_disable(&clk);
59 return -ENODEV;
60 }
61
62 priv->clk_rate = clk_rate;
63
Songjun Wu72ac56a2017-04-11 16:33:30 +080064 return 0;
65}
66
67static void atmel_hlcdc_init(struct udevice *dev)
68{
Simon Glassb75b15b2020-12-03 16:55:23 -070069 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Songjun Wu72ac56a2017-04-11 16:33:30 +080070 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
71 struct atmel_hlcd_regs *regs = priv->regs;
72 struct display_timing *timing = &priv->timing;
73 struct lcd_dma_desc *desc;
74 unsigned long value, vl_clk_pol;
75 int ret;
76
77 /* Disable DISP signal */
78 writel(LCDC_LCDDIS_DISPDIS, &regs->lcdc_lcddis);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010079 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
80 false, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +080081 if (ret)
82 printf("%s: %d: Timeout!\n", __func__, __LINE__);
83 /* Disable synchronization */
84 writel(LCDC_LCDDIS_SYNCDIS, &regs->lcdc_lcddis);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010085 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
86 false, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +080087 if (ret)
88 printf("%s: %d: Timeout!\n", __func__, __LINE__);
89 /* Disable pixel clock */
90 writel(LCDC_LCDDIS_CLKDIS, &regs->lcdc_lcddis);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010091 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
92 false, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +080093 if (ret)
94 printf("%s: %d: Timeout!\n", __func__, __LINE__);
95 /* Disable PWM */
96 writel(LCDC_LCDDIS_PWMDIS, &regs->lcdc_lcddis);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010097 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
98 false, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +080099 if (ret)
100 printf("%s: %d: Timeout!\n", __func__, __LINE__);
101
102 /* Set pixel clock */
103 value = priv->clk_rate / timing->pixelclock.typ;
104 if (priv->clk_rate % timing->pixelclock.typ)
105 value++;
106
107 vl_clk_pol = 0;
108 if (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
109 vl_clk_pol = LCDC_LCDCFG0_CLKPOL;
110
111 if (value < 1) {
112 /* Using system clock as pixel clock */
113 writel(LCDC_LCDCFG0_CLKDIV(0)
114 | LCDC_LCDCFG0_CGDISHCR
115 | LCDC_LCDCFG0_CGDISHEO
116 | LCDC_LCDCFG0_CGDISOVR1
117 | LCDC_LCDCFG0_CGDISBASE
118 | vl_clk_pol
119 | LCDC_LCDCFG0_CLKSEL,
120 &regs->lcdc_lcdcfg0);
121
122 } else {
123 writel(LCDC_LCDCFG0_CLKDIV(value - 2)
124 | LCDC_LCDCFG0_CGDISHCR
125 | LCDC_LCDCFG0_CGDISHEO
126 | LCDC_LCDCFG0_CGDISOVR1
127 | LCDC_LCDCFG0_CGDISBASE
128 | vl_clk_pol,
129 &regs->lcdc_lcdcfg0);
130 }
131
132 /* Initialize control register 5 */
133 value = 0;
134
135 if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH))
136 value |= LCDC_LCDCFG5_HSPOL;
137 if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH))
138 value |= LCDC_LCDCFG5_VSPOL;
139
140 switch (priv->output_mode) {
141 case 12:
142 value |= LCDC_LCDCFG5_MODE_OUTPUT_12BPP;
143 break;
144 case 16:
145 value |= LCDC_LCDCFG5_MODE_OUTPUT_16BPP;
146 break;
147 case 18:
148 value |= LCDC_LCDCFG5_MODE_OUTPUT_18BPP;
149 break;
150 case 24:
151 value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP;
152 break;
153 default:
154 BUG();
155 break;
156 }
157
158 value |= LCDC_LCDCFG5_GUARDTIME(priv->guard_time);
159 value |= (LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS);
160 writel(value, &regs->lcdc_lcdcfg5);
161
162 /* Vertical & Horizontal Timing */
163 value = LCDC_LCDCFG1_VSPW(timing->vsync_len.typ - 1);
164 value |= LCDC_LCDCFG1_HSPW(timing->hsync_len.typ - 1);
165 writel(value, &regs->lcdc_lcdcfg1);
166
167 value = LCDC_LCDCFG2_VBPW(timing->vback_porch.typ);
168 value |= LCDC_LCDCFG2_VFPW(timing->vfront_porch.typ - 1);
169 writel(value, &regs->lcdc_lcdcfg2);
170
171 value = LCDC_LCDCFG3_HBPW(timing->hback_porch.typ - 1);
172 value |= LCDC_LCDCFG3_HFPW(timing->hfront_porch.typ - 1);
173 writel(value, &regs->lcdc_lcdcfg3);
174
175 /* Display size */
176 value = LCDC_LCDCFG4_RPF(timing->vactive.typ - 1);
177 value |= LCDC_LCDCFG4_PPL(timing->hactive.typ - 1);
178 writel(value, &regs->lcdc_lcdcfg4);
179
180 writel(LCDC_BASECFG0_BLEN_AHB_INCR4 | LCDC_BASECFG0_DLBO,
181 &regs->lcdc_basecfg0);
182
183 switch (VNBITS(priv->vl_bpix)) {
184 case 16:
185 writel(LCDC_BASECFG1_RGBMODE_16BPP_RGB_565,
186 &regs->lcdc_basecfg1);
187 break;
188 case 32:
189 writel(LCDC_BASECFG1_RGBMODE_24BPP_RGB_888,
190 &regs->lcdc_basecfg1);
191 break;
192 default:
193 BUG();
194 break;
195 }
196
197 writel(LCDC_BASECFG2_XSTRIDE(0), &regs->lcdc_basecfg2);
198 writel(0, &regs->lcdc_basecfg3);
199 writel(LCDC_BASECFG4_DMA, &regs->lcdc_basecfg4);
200
201 /* Disable all interrupts */
202 writel(~0UL, &regs->lcdc_lcdidr);
203 writel(~0UL, &regs->lcdc_baseidr);
204
205 /* Setup the DMA descriptor, this descriptor will loop to itself */
Wenyou Yangd21e0452017-06-02 11:29:04 +0800206 desc = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*desc));
207 if (!desc)
208 return;
Songjun Wu72ac56a2017-04-11 16:33:30 +0800209
210 desc->address = (u32)uc_plat->base;
211
212 /* Disable DMA transfer interrupt & descriptor loaded interrupt. */
213 desc->control = LCDC_BASECTRL_ADDIEN | LCDC_BASECTRL_DSCRIEN
214 | LCDC_BASECTRL_DMAIEN | LCDC_BASECTRL_DFETCH;
215 desc->next = (u32)desc;
216
217 /* Flush the DMA descriptor if we enabled dcache */
Wenyou Yangd21e0452017-06-02 11:29:04 +0800218 flush_dcache_range((u32)desc,
219 ALIGN(((u32)desc + sizeof(*desc)),
220 CONFIG_SYS_CACHELINE_SIZE));
Songjun Wu72ac56a2017-04-11 16:33:30 +0800221
222 writel(desc->address, &regs->lcdc_baseaddr);
223 writel(desc->control, &regs->lcdc_basectrl);
224 writel(desc->next, &regs->lcdc_basenext);
225 writel(LCDC_BASECHER_CHEN | LCDC_BASECHER_UPDATEEN,
226 &regs->lcdc_basecher);
227
228 /* Enable LCD */
229 value = readl(&regs->lcdc_lcden);
230 writel(value | LCDC_LCDEN_CLKEN, &regs->lcdc_lcden);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100231 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
232 true, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800233 if (ret)
234 printf("%s: %d: Timeout!\n", __func__, __LINE__);
235 value = readl(&regs->lcdc_lcden);
236 writel(value | LCDC_LCDEN_SYNCEN, &regs->lcdc_lcden);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100237 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
238 true, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800239 if (ret)
240 printf("%s: %d: Timeout!\n", __func__, __LINE__);
241 value = readl(&regs->lcdc_lcden);
242 writel(value | LCDC_LCDEN_DISPEN, &regs->lcdc_lcden);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100243 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
244 true, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800245 if (ret)
246 printf("%s: %d: Timeout!\n", __func__, __LINE__);
247 value = readl(&regs->lcdc_lcden);
248 writel(value | LCDC_LCDEN_PWMEN, &regs->lcdc_lcden);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100249 ret = wait_for_bit_le32(&regs->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
250 true, 1000, false);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800251 if (ret)
252 printf("%s: %d: Timeout!\n", __func__, __LINE__);
253}
254
255static int atmel_hlcdc_probe(struct udevice *dev)
256{
257 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
258 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
259 int ret;
260
261 ret = at91_hlcdc_enable_clk(dev);
262 if (ret)
263 return ret;
264
265 atmel_hlcdc_init(dev);
266
267 uc_priv->xsize = priv->timing.hactive.typ;
268 uc_priv->ysize = priv->timing.vactive.typ;
269 uc_priv->bpix = priv->vl_bpix;
270
271 /* Enable flushing if we enabled dcache */
272 video_set_flush_dcache(dev, true);
273
274 return 0;
275}
276
Simon Glassaad29ae2020-12-03 16:55:21 -0700277static int atmel_hlcdc_of_to_plat(struct udevice *dev)
Songjun Wu72ac56a2017-04-11 16:33:30 +0800278{
279 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
280 const void *blob = gd->fdt_blob;
Simon Glass7a494432017-05-17 17:18:09 -0600281 int node = dev_of_offset(dev);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800282
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900283 priv->regs = dev_read_addr_ptr(dev);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800284 if (!priv->regs) {
285 debug("%s: No display controller address\n", __func__);
286 return -EINVAL;
287 }
288
Simon Glass7a494432017-05-17 17:18:09 -0600289 if (fdtdec_decode_display_timing(blob, dev_of_offset(dev),
Songjun Wu72ac56a2017-04-11 16:33:30 +0800290 0, &priv->timing)) {
291 debug("%s: Failed to decode display timing\n", __func__);
292 return -EINVAL;
293 }
294
295 if (priv->timing.hactive.typ > LCD_MAX_WIDTH)
296 priv->timing.hactive.typ = LCD_MAX_WIDTH;
297
298 if (priv->timing.vactive.typ > LCD_MAX_HEIGHT)
299 priv->timing.vactive.typ = LCD_MAX_HEIGHT;
300
301 priv->vl_bpix = fdtdec_get_int(blob, node, "atmel,vl-bpix", 0);
302 if (!priv->vl_bpix) {
303 debug("%s: Failed to get bits per pixel\n", __func__);
304 return -EINVAL;
305 }
306
307 priv->output_mode = fdtdec_get_int(blob, node, "atmel,output-mode", 24);
308 priv->guard_time = fdtdec_get_int(blob, node, "atmel,guard-time", 1);
309
310 return 0;
311}
312
313static int atmel_hlcdc_bind(struct udevice *dev)
314{
Simon Glassb75b15b2020-12-03 16:55:23 -0700315 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Songjun Wu72ac56a2017-04-11 16:33:30 +0800316
317 uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
318 (1 << LCD_MAX_LOG2_BPP) / 8;
319
320 debug("%s: Frame buffer size %x\n", __func__, uc_plat->size);
321
322 return 0;
323}
324
325static const struct udevice_id atmel_hlcdc_ids[] = {
326 { .compatible = "atmel,sama5d2-hlcdc" },
327 { .compatible = "atmel,at91sam9x5-hlcdc" },
328 { }
329};
330
331U_BOOT_DRIVER(atmel_hlcdfb) = {
332 .name = "atmel_hlcdfb",
333 .id = UCLASS_VIDEO,
334 .of_match = atmel_hlcdc_ids,
335 .bind = atmel_hlcdc_bind,
336 .probe = atmel_hlcdc_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700337 .of_to_plat = atmel_hlcdc_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700338 .priv_auto = sizeof(struct atmel_hlcdc_priv),
Songjun Wu72ac56a2017-04-11 16:33:30 +0800339};