1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for AT91/AT32 MULTI LAYER LCD Controller
5 * Copyright (C) 2012 Atmel Corporation
10 #include <asm/arch/gpio.h>
11 #include <asm/arch/clk.h>
18 #include <atmel_hlcdc.h>
20 #if defined(CONFIG_LCD_LOGO)
24 DECLARE_GLOBAL_DATA_PTR;
26 #ifndef CONFIG_DM_VIDEO
28 /* configurable parameters */
29 #define ATMEL_LCDC_CVAL_DEFAULT 0xc8
30 #define ATMEL_LCDC_DMA_BURST_LEN 8
31 #ifndef ATMEL_LCDC_GUARD_TIME
32 #define ATMEL_LCDC_GUARD_TIME 1
35 #define ATMEL_LCDC_FIFO_SIZE 512
38 * the CLUT register map as following
39 * RCLUT(24 ~ 16), GCLUT(15 ~ 8), BCLUT(7 ~ 0)
41 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
43 writel(panel_info.mmio + ATMEL_LCDC_LUT(regno),
44 ((red << LCDC_BASECLUT_RCLUT_Pos) & LCDC_BASECLUT_RCLUT_Msk)
45 | ((green << LCDC_BASECLUT_GCLUT_Pos) & LCDC_BASECLUT_GCLUT_Msk)
46 | ((blue << LCDC_BASECLUT_BCLUT_Pos) & LCDC_BASECLUT_BCLUT_Msk));
49 ushort *configuration_get_cmap(void)
51 #if defined(CONFIG_LCD_LOGO)
52 return bmp_logo_palette;
58 void lcd_ctrl_init(void *lcdbase)
61 struct lcd_dma_desc *desc;
62 struct atmel_hlcd_regs *regs;
68 regs = (struct atmel_hlcd_regs *)panel_info.mmio;
70 /* Disable DISP signal */
71 writel(LCDC_LCDDIS_DISPDIS, ®s->lcdc_lcddis);
72 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
75 printf("%s: %d: Timeout!\n", __func__, __LINE__);
76 /* Disable synchronization */
77 writel(LCDC_LCDDIS_SYNCDIS, ®s->lcdc_lcddis);
78 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
81 printf("%s: %d: Timeout!\n", __func__, __LINE__);
82 /* Disable pixel clock */
83 writel(LCDC_LCDDIS_CLKDIS, ®s->lcdc_lcddis);
84 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
87 printf("%s: %d: Timeout!\n", __func__, __LINE__);
89 writel(LCDC_LCDDIS_PWMDIS, ®s->lcdc_lcddis);
90 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
93 printf("%s: %d: Timeout!\n", __func__, __LINE__);
96 value = get_lcdc_clk_rate(0) / panel_info.vl_clk;
97 if (get_lcdc_clk_rate(0) % panel_info.vl_clk)
101 /* Using system clock as pixel clock */
102 writel(LCDC_LCDCFG0_CLKDIV(0)
103 | LCDC_LCDCFG0_CGDISHCR
104 | LCDC_LCDCFG0_CGDISHEO
105 | LCDC_LCDCFG0_CGDISOVR1
106 | LCDC_LCDCFG0_CGDISBASE
107 | panel_info.vl_clk_pol
108 | LCDC_LCDCFG0_CLKSEL,
109 ®s->lcdc_lcdcfg0);
112 writel(LCDC_LCDCFG0_CLKDIV(value - 2)
113 | LCDC_LCDCFG0_CGDISHCR
114 | LCDC_LCDCFG0_CGDISHEO
115 | LCDC_LCDCFG0_CGDISOVR1
116 | LCDC_LCDCFG0_CGDISBASE
117 | panel_info.vl_clk_pol,
118 ®s->lcdc_lcdcfg0);
121 /* Initialize control register 5 */
124 value |= panel_info.vl_sync;
126 #ifndef LCD_OUTPUT_BPP
127 /* Output is 24bpp */
128 value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP;
130 switch (LCD_OUTPUT_BPP) {
132 value |= LCDC_LCDCFG5_MODE_OUTPUT_12BPP;
135 value |= LCDC_LCDCFG5_MODE_OUTPUT_16BPP;
138 value |= LCDC_LCDCFG5_MODE_OUTPUT_18BPP;
141 value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP;
149 value |= LCDC_LCDCFG5_GUARDTIME(ATMEL_LCDC_GUARD_TIME);
150 value |= (LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS);
151 writel(value, ®s->lcdc_lcdcfg5);
153 /* Vertical & Horizontal Timing */
154 value = LCDC_LCDCFG1_VSPW(panel_info.vl_vsync_len - 1);
155 value |= LCDC_LCDCFG1_HSPW(panel_info.vl_hsync_len - 1);
156 writel(value, ®s->lcdc_lcdcfg1);
158 value = LCDC_LCDCFG2_VBPW(panel_info.vl_upper_margin);
159 value |= LCDC_LCDCFG2_VFPW(panel_info.vl_lower_margin - 1);
160 writel(value, ®s->lcdc_lcdcfg2);
162 value = LCDC_LCDCFG3_HBPW(panel_info.vl_left_margin - 1);
163 value |= LCDC_LCDCFG3_HFPW(panel_info.vl_right_margin - 1);
164 writel(value, ®s->lcdc_lcdcfg3);
167 value = LCDC_LCDCFG4_RPF(panel_info.vl_row - 1);
168 value |= LCDC_LCDCFG4_PPL(panel_info.vl_col - 1);
169 writel(value, ®s->lcdc_lcdcfg4);
171 writel(LCDC_BASECFG0_BLEN_AHB_INCR4 | LCDC_BASECFG0_DLBO,
172 ®s->lcdc_basecfg0);
174 switch (NBITS(panel_info.vl_bpix)) {
176 writel(LCDC_BASECFG1_RGBMODE_16BPP_RGB_565,
177 ®s->lcdc_basecfg1);
180 writel(LCDC_BASECFG1_RGBMODE_24BPP_RGB_888,
181 ®s->lcdc_basecfg1);
188 writel(LCDC_BASECFG2_XSTRIDE(0), ®s->lcdc_basecfg2);
189 writel(0, ®s->lcdc_basecfg3);
190 writel(LCDC_BASECFG4_DMA, ®s->lcdc_basecfg4);
192 /* Disable all interrupts */
193 writel(~0UL, ®s->lcdc_lcdidr);
194 writel(~0UL, ®s->lcdc_baseidr);
196 /* Setup the DMA descriptor, this descriptor will loop to itself */
197 desc = (struct lcd_dma_desc *)(lcdbase - 16);
199 desc->address = (u32)lcdbase;
200 /* Disable DMA transfer interrupt & descriptor loaded interrupt. */
201 desc->control = LCDC_BASECTRL_ADDIEN | LCDC_BASECTRL_DSCRIEN
202 | LCDC_BASECTRL_DMAIEN | LCDC_BASECTRL_DFETCH;
203 desc->next = (u32)desc;
205 /* Flush the DMA descriptor if we enabled dcache */
206 flush_dcache_range((u32)desc, (u32)desc + sizeof(*desc));
208 writel(desc->address, ®s->lcdc_baseaddr);
209 writel(desc->control, ®s->lcdc_basectrl);
210 writel(desc->next, ®s->lcdc_basenext);
211 writel(LCDC_BASECHER_CHEN | LCDC_BASECHER_UPDATEEN,
212 ®s->lcdc_basecher);
215 value = readl(®s->lcdc_lcden);
216 writel(value | LCDC_LCDEN_CLKEN, ®s->lcdc_lcden);
217 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
220 printf("%s: %d: Timeout!\n", __func__, __LINE__);
221 value = readl(®s->lcdc_lcden);
222 writel(value | LCDC_LCDEN_SYNCEN, ®s->lcdc_lcden);
223 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
226 printf("%s: %d: Timeout!\n", __func__, __LINE__);
227 value = readl(®s->lcdc_lcden);
228 writel(value | LCDC_LCDEN_DISPEN, ®s->lcdc_lcden);
229 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
232 printf("%s: %d: Timeout!\n", __func__, __LINE__);
233 value = readl(®s->lcdc_lcden);
234 writel(value | LCDC_LCDEN_PWMEN, ®s->lcdc_lcden);
235 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
238 printf("%s: %d: Timeout!\n", __func__, __LINE__);
240 /* Enable flushing if we enabled dcache */
241 lcd_set_flush_dcache(1);
247 LCD_MAX_WIDTH = 1024,
248 LCD_MAX_HEIGHT = 768,
249 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
252 struct atmel_hlcdc_priv {
253 struct atmel_hlcd_regs *regs;
254 struct display_timing timing;
255 unsigned int vl_bpix;
256 unsigned int output_mode;
257 unsigned int guard_time;
261 static int at91_hlcdc_enable_clk(struct udevice *dev)
263 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
268 ret = clk_get_by_index(dev, 0, &clk);
272 ret = clk_enable(&clk);
276 clk_rate = clk_get_rate(&clk);
282 priv->clk_rate = clk_rate;
289 static void atmel_hlcdc_init(struct udevice *dev)
291 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
292 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
293 struct atmel_hlcd_regs *regs = priv->regs;
294 struct display_timing *timing = &priv->timing;
295 struct lcd_dma_desc *desc;
296 unsigned long value, vl_clk_pol;
299 /* Disable DISP signal */
300 writel(LCDC_LCDDIS_DISPDIS, ®s->lcdc_lcddis);
301 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
304 printf("%s: %d: Timeout!\n", __func__, __LINE__);
305 /* Disable synchronization */
306 writel(LCDC_LCDDIS_SYNCDIS, ®s->lcdc_lcddis);
307 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
310 printf("%s: %d: Timeout!\n", __func__, __LINE__);
311 /* Disable pixel clock */
312 writel(LCDC_LCDDIS_CLKDIS, ®s->lcdc_lcddis);
313 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
316 printf("%s: %d: Timeout!\n", __func__, __LINE__);
318 writel(LCDC_LCDDIS_PWMDIS, ®s->lcdc_lcddis);
319 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
322 printf("%s: %d: Timeout!\n", __func__, __LINE__);
324 /* Set pixel clock */
325 value = priv->clk_rate / timing->pixelclock.typ;
326 if (priv->clk_rate % timing->pixelclock.typ)
330 if (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
331 vl_clk_pol = LCDC_LCDCFG0_CLKPOL;
334 /* Using system clock as pixel clock */
335 writel(LCDC_LCDCFG0_CLKDIV(0)
336 | LCDC_LCDCFG0_CGDISHCR
337 | LCDC_LCDCFG0_CGDISHEO
338 | LCDC_LCDCFG0_CGDISOVR1
339 | LCDC_LCDCFG0_CGDISBASE
341 | LCDC_LCDCFG0_CLKSEL,
342 ®s->lcdc_lcdcfg0);
345 writel(LCDC_LCDCFG0_CLKDIV(value - 2)
346 | LCDC_LCDCFG0_CGDISHCR
347 | LCDC_LCDCFG0_CGDISHEO
348 | LCDC_LCDCFG0_CGDISOVR1
349 | LCDC_LCDCFG0_CGDISBASE
351 ®s->lcdc_lcdcfg0);
354 /* Initialize control register 5 */
357 if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH))
358 value |= LCDC_LCDCFG5_HSPOL;
359 if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH))
360 value |= LCDC_LCDCFG5_VSPOL;
362 switch (priv->output_mode) {
364 value |= LCDC_LCDCFG5_MODE_OUTPUT_12BPP;
367 value |= LCDC_LCDCFG5_MODE_OUTPUT_16BPP;
370 value |= LCDC_LCDCFG5_MODE_OUTPUT_18BPP;
373 value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP;
380 value |= LCDC_LCDCFG5_GUARDTIME(priv->guard_time);
381 value |= (LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS);
382 writel(value, ®s->lcdc_lcdcfg5);
384 /* Vertical & Horizontal Timing */
385 value = LCDC_LCDCFG1_VSPW(timing->vsync_len.typ - 1);
386 value |= LCDC_LCDCFG1_HSPW(timing->hsync_len.typ - 1);
387 writel(value, ®s->lcdc_lcdcfg1);
389 value = LCDC_LCDCFG2_VBPW(timing->vback_porch.typ);
390 value |= LCDC_LCDCFG2_VFPW(timing->vfront_porch.typ - 1);
391 writel(value, ®s->lcdc_lcdcfg2);
393 value = LCDC_LCDCFG3_HBPW(timing->hback_porch.typ - 1);
394 value |= LCDC_LCDCFG3_HFPW(timing->hfront_porch.typ - 1);
395 writel(value, ®s->lcdc_lcdcfg3);
398 value = LCDC_LCDCFG4_RPF(timing->vactive.typ - 1);
399 value |= LCDC_LCDCFG4_PPL(timing->hactive.typ - 1);
400 writel(value, ®s->lcdc_lcdcfg4);
402 writel(LCDC_BASECFG0_BLEN_AHB_INCR4 | LCDC_BASECFG0_DLBO,
403 ®s->lcdc_basecfg0);
405 switch (VNBITS(priv->vl_bpix)) {
407 writel(LCDC_BASECFG1_RGBMODE_16BPP_RGB_565,
408 ®s->lcdc_basecfg1);
411 writel(LCDC_BASECFG1_RGBMODE_24BPP_RGB_888,
412 ®s->lcdc_basecfg1);
419 writel(LCDC_BASECFG2_XSTRIDE(0), ®s->lcdc_basecfg2);
420 writel(0, ®s->lcdc_basecfg3);
421 writel(LCDC_BASECFG4_DMA, ®s->lcdc_basecfg4);
423 /* Disable all interrupts */
424 writel(~0UL, ®s->lcdc_lcdidr);
425 writel(~0UL, ®s->lcdc_baseidr);
427 /* Setup the DMA descriptor, this descriptor will loop to itself */
428 desc = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*desc));
432 desc->address = (u32)uc_plat->base;
434 /* Disable DMA transfer interrupt & descriptor loaded interrupt. */
435 desc->control = LCDC_BASECTRL_ADDIEN | LCDC_BASECTRL_DSCRIEN
436 | LCDC_BASECTRL_DMAIEN | LCDC_BASECTRL_DFETCH;
437 desc->next = (u32)desc;
439 /* Flush the DMA descriptor if we enabled dcache */
440 flush_dcache_range((u32)desc,
441 ALIGN(((u32)desc + sizeof(*desc)),
442 CONFIG_SYS_CACHELINE_SIZE));
444 writel(desc->address, ®s->lcdc_baseaddr);
445 writel(desc->control, ®s->lcdc_basectrl);
446 writel(desc->next, ®s->lcdc_basenext);
447 writel(LCDC_BASECHER_CHEN | LCDC_BASECHER_UPDATEEN,
448 ®s->lcdc_basecher);
451 value = readl(®s->lcdc_lcden);
452 writel(value | LCDC_LCDEN_CLKEN, ®s->lcdc_lcden);
453 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS,
456 printf("%s: %d: Timeout!\n", __func__, __LINE__);
457 value = readl(®s->lcdc_lcden);
458 writel(value | LCDC_LCDEN_SYNCEN, ®s->lcdc_lcden);
459 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS,
462 printf("%s: %d: Timeout!\n", __func__, __LINE__);
463 value = readl(®s->lcdc_lcden);
464 writel(value | LCDC_LCDEN_DISPEN, ®s->lcdc_lcden);
465 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS,
468 printf("%s: %d: Timeout!\n", __func__, __LINE__);
469 value = readl(®s->lcdc_lcden);
470 writel(value | LCDC_LCDEN_PWMEN, ®s->lcdc_lcden);
471 ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS,
474 printf("%s: %d: Timeout!\n", __func__, __LINE__);
477 static int atmel_hlcdc_probe(struct udevice *dev)
479 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
480 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
483 ret = at91_hlcdc_enable_clk(dev);
487 atmel_hlcdc_init(dev);
489 uc_priv->xsize = priv->timing.hactive.typ;
490 uc_priv->ysize = priv->timing.vactive.typ;
491 uc_priv->bpix = priv->vl_bpix;
493 /* Enable flushing if we enabled dcache */
494 video_set_flush_dcache(dev, true);
499 static int atmel_hlcdc_ofdata_to_platdata(struct udevice *dev)
501 struct atmel_hlcdc_priv *priv = dev_get_priv(dev);
502 const void *blob = gd->fdt_blob;
503 int node = dev_of_offset(dev);
505 priv->regs = (struct atmel_hlcd_regs *)devfdt_get_addr(dev);
507 debug("%s: No display controller address\n", __func__);
511 if (fdtdec_decode_display_timing(blob, dev_of_offset(dev),
513 debug("%s: Failed to decode display timing\n", __func__);
517 if (priv->timing.hactive.typ > LCD_MAX_WIDTH)
518 priv->timing.hactive.typ = LCD_MAX_WIDTH;
520 if (priv->timing.vactive.typ > LCD_MAX_HEIGHT)
521 priv->timing.vactive.typ = LCD_MAX_HEIGHT;
523 priv->vl_bpix = fdtdec_get_int(blob, node, "atmel,vl-bpix", 0);
524 if (!priv->vl_bpix) {
525 debug("%s: Failed to get bits per pixel\n", __func__);
529 priv->output_mode = fdtdec_get_int(blob, node, "atmel,output-mode", 24);
530 priv->guard_time = fdtdec_get_int(blob, node, "atmel,guard-time", 1);
535 static int atmel_hlcdc_bind(struct udevice *dev)
537 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
539 uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
540 (1 << LCD_MAX_LOG2_BPP) / 8;
542 debug("%s: Frame buffer size %x\n", __func__, uc_plat->size);
547 static const struct udevice_id atmel_hlcdc_ids[] = {
548 { .compatible = "atmel,sama5d2-hlcdc" },
549 { .compatible = "atmel,at91sam9x5-hlcdc" },
553 U_BOOT_DRIVER(atmel_hlcdfb) = {
554 .name = "atmel_hlcdfb",
556 .of_match = atmel_hlcdc_ids,
557 .bind = atmel_hlcdc_bind,
558 .probe = atmel_hlcdc_probe,
559 .ofdata_to_platdata = atmel_hlcdc_ofdata_to_platdata,
560 .priv_auto_alloc_size = sizeof(struct atmel_hlcdc_priv),