blob: 39cd7caff16dbf25c3ff9e1f89bb95b032a71bf8 [file] [log] [blame]
Stelian Pop39cf4802008-05-09 21:57:18 +02001/*
2 * Driver for AT91/AT32 LCD Controller
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02006 * SPDX-License-Identifier: GPL-2.0+
Stelian Pop39cf4802008-05-09 21:57:18 +02007 */
8
9#include <common.h>
Simon Glass9dc89a02016-05-05 07:28:20 -060010#include <atmel_lcd.h>
11#include <dm.h>
Simon Glassd63ec262016-05-05 07:28:19 -060012#include <fdtdec.h>
Simon Glass9dc89a02016-05-05 07:28:20 -060013#include <video.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020014#include <asm/io.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020015#include <asm/arch/gpio.h>
16#include <asm/arch/clk.h>
17#include <lcd.h>
Nikita Kiryanov0b29a892015-02-03 13:32:27 +020018#include <bmp_layout.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020019#include <atmel_lcdc.h>
20
Simon Glass9dc89a02016-05-05 07:28:20 -060021DECLARE_GLOBAL_DATA_PTR;
22
23#ifdef CONFIG_DM_VIDEO
24enum {
25 /* Maximum LCD size we support */
26 LCD_MAX_WIDTH = 1366,
27 LCD_MAX_HEIGHT = 768,
28 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
29};
30#endif
31
32struct atmel_fb_priv {
33 struct display_timing timing;
34};
35
Stelian Pop39cf4802008-05-09 21:57:18 +020036/* configurable parameters */
37#define ATMEL_LCDC_CVAL_DEFAULT 0xc8
38#define ATMEL_LCDC_DMA_BURST_LEN 8
Mark Jackson6bbced62009-06-29 15:59:10 +010039#ifndef ATMEL_LCDC_GUARD_TIME
40#define ATMEL_LCDC_GUARD_TIME 1
41#endif
Stelian Pop39cf4802008-05-09 21:57:18 +020042
Bo Shenc6941e12015-01-16 10:55:46 +080043#if defined(CONFIG_AT91SAM9263)
Stelian Pop39cf4802008-05-09 21:57:18 +020044#define ATMEL_LCDC_FIFO_SIZE 2048
45#else
46#define ATMEL_LCDC_FIFO_SIZE 512
47#endif
48
49#define lcdc_readl(mmio, reg) __raw_readl((mmio)+(reg))
50#define lcdc_writel(mmio, reg, val) __raw_writel((val), (mmio)+(reg))
51
Simon Glass9dc89a02016-05-05 07:28:20 -060052#ifndef CONFIG_DM_VIDEO
Nikita Kiryanov38b55082015-02-03 13:32:21 +020053ushort *configuration_get_cmap(void)
54{
55 return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
56}
57
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +020058#if defined(CONFIG_BMP_16BPP) && defined(CONFIG_ATMEL_LCD_BGR555)
59void fb_put_word(uchar **fb, uchar **from)
60{
61 *(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
62 *(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
63 *from += 2;
64}
65#endif
66
Nikita Kiryanova02e9482015-02-03 13:32:24 +020067#ifdef CONFIG_LCD_LOGO
68#include <bmp_logo.h>
69void lcd_logo_set_cmap(void)
70{
71 int i;
72 uint lut_entry;
73 ushort colreg;
74 uint *cmap = (uint *)configuration_get_cmap();
75
76 for (i = 0; i < BMP_LOGO_COLORS; ++i) {
77 colreg = bmp_logo_palette[i];
78#ifdef CONFIG_ATMEL_LCD_BGR555
79 lut_entry = ((colreg & 0x000F) << 11) |
80 ((colreg & 0x00F0) << 2) |
81 ((colreg & 0x0F00) >> 7);
82#else
83 lut_entry = ((colreg & 0x000F) << 1) |
84 ((colreg & 0x00F0) << 3) |
85 ((colreg & 0x0F00) << 4);
86#endif
87 *(cmap + BMP_LOGO_OFFSET) = lut_entry;
88 cmap++;
89 }
90}
91#endif
92
Stelian Pop39cf4802008-05-09 21:57:18 +020093void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
94{
95#if defined(CONFIG_ATMEL_LCD_BGR555)
96 lcdc_writel(panel_info.mmio, ATMEL_LCDC_LUT(regno),
97 (red >> 3) | ((green & 0xf8) << 2) | ((blue & 0xf8) << 7));
98#else
99 lcdc_writel(panel_info.mmio, ATMEL_LCDC_LUT(regno),
100 (blue >> 3) | ((green & 0xfc) << 3) | ((red & 0xf8) << 8));
101#endif
102}
103
Simon Glass1c3dbe52015-05-13 07:02:27 -0600104void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200105{
106 int i;
107
108 for (i = 0; i < colors; ++i) {
Simon Glass1c3dbe52015-05-13 07:02:27 -0600109 struct bmp_color_table_entry cte = bmp->color_table[i];
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200110 lcd_setcolreg(i, cte.red, cte.green, cte.blue);
111 }
112}
Simon Glass9dc89a02016-05-05 07:28:20 -0600113#endif
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200114
Simon Glassd63ec262016-05-05 07:28:19 -0600115static void atmel_fb_init(ulong addr, struct display_timing *timing, int bpix,
116 bool tft, bool cont_pol_low, ulong lcdbase)
Stelian Pop39cf4802008-05-09 21:57:18 +0200117{
118 unsigned long value;
Simon Glassd63ec262016-05-05 07:28:19 -0600119 void *reg = (void *)addr;
Stelian Pop39cf4802008-05-09 21:57:18 +0200120
121 /* Turn off the LCD controller and the DMA controller */
Simon Glassd63ec262016-05-05 07:28:19 -0600122 lcdc_writel(reg, ATMEL_LCDC_PWRCON,
Mark Jackson6bbced62009-06-29 15:59:10 +0100123 ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET);
Stelian Pop39cf4802008-05-09 21:57:18 +0200124
125 /* Wait for the LCDC core to become idle */
Simon Glassd63ec262016-05-05 07:28:19 -0600126 while (lcdc_readl(reg, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
Stelian Pop39cf4802008-05-09 21:57:18 +0200127 udelay(10);
128
Simon Glassd63ec262016-05-05 07:28:19 -0600129 lcdc_writel(reg, ATMEL_LCDC_DMACON, 0);
Stelian Pop39cf4802008-05-09 21:57:18 +0200130
131 /* Reset LCDC DMA */
Simon Glassd63ec262016-05-05 07:28:19 -0600132 lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMARST);
Stelian Pop39cf4802008-05-09 21:57:18 +0200133
134 /* ...set frame size and burst length = 8 words (?) */
Simon Glassd63ec262016-05-05 07:28:19 -0600135 value = (timing->hactive.typ * timing->vactive.typ *
136 (1 << bpix)) / 32;
Stelian Pop39cf4802008-05-09 21:57:18 +0200137 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
Simon Glassd63ec262016-05-05 07:28:19 -0600138 lcdc_writel(reg, ATMEL_LCDC_DMAFRMCFG, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200139
140 /* Set pixel clock */
Simon Glassd63ec262016-05-05 07:28:19 -0600141 value = get_lcdc_clk_rate(0) / timing->pixelclock.typ;
142 if (get_lcdc_clk_rate(0) % timing->pixelclock.typ)
Stelian Pop39cf4802008-05-09 21:57:18 +0200143 value++;
144 value = (value / 2) - 1;
145
146 if (!value) {
Simon Glassd63ec262016-05-05 07:28:19 -0600147 lcdc_writel(reg, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Stelian Pop39cf4802008-05-09 21:57:18 +0200148 } else
Simon Glassd63ec262016-05-05 07:28:19 -0600149 lcdc_writel(reg, ATMEL_LCDC_LCDCON1,
Stelian Pop39cf4802008-05-09 21:57:18 +0200150 value << ATMEL_LCDC_CLKVAL_OFFSET);
151
152 /* Initialize control register 2 */
Stefan Roesef2302d42008-08-06 14:05:38 +0200153#ifdef CONFIG_AVR32
154 value = ATMEL_LCDC_MEMOR_BIG | ATMEL_LCDC_CLKMOD_ALWAYSACTIVE;
155#else
Stelian Pop39cf4802008-05-09 21:57:18 +0200156 value = ATMEL_LCDC_MEMOR_LITTLE | ATMEL_LCDC_CLKMOD_ALWAYSACTIVE;
Stefan Roesef2302d42008-08-06 14:05:38 +0200157#endif
Simon Glassd63ec262016-05-05 07:28:19 -0600158 if (tft)
Stelian Pop39cf4802008-05-09 21:57:18 +0200159 value |= ATMEL_LCDC_DISTYPE_TFT;
160
Simon Glassd63ec262016-05-05 07:28:19 -0600161 if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH))
162 value |= ATMEL_LCDC_INVLINE_INVERTED;
163 if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH))
164 value |= ATMEL_LCDC_INVFRAME_INVERTED;
165 value |= bpix << 5;
166 lcdc_writel(reg, ATMEL_LCDC_LCDCON2, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200167
168 /* Vertical timing */
Simon Glassd63ec262016-05-05 07:28:19 -0600169 value = (timing->vsync_len.typ - 1) << ATMEL_LCDC_VPW_OFFSET;
170 value |= timing->vback_porch.typ << ATMEL_LCDC_VBP_OFFSET;
171 value |= timing->vfront_porch.typ;
172 /* Magic! (Datasheet says "Bit 31 must be written to 1") */
173 value |= 1U << 31;
174 lcdc_writel(reg, ATMEL_LCDC_TIM1, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200175
176 /* Horizontal timing */
Simon Glassd63ec262016-05-05 07:28:19 -0600177 value = (timing->hfront_porch.typ - 1) << ATMEL_LCDC_HFP_OFFSET;
178 value |= (timing->hsync_len.typ - 1) << ATMEL_LCDC_HPW_OFFSET;
179 value |= (timing->hback_porch.typ - 1);
180 lcdc_writel(reg, ATMEL_LCDC_TIM2, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200181
182 /* Display size */
Simon Glassd63ec262016-05-05 07:28:19 -0600183 value = (timing->hactive.typ - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
184 value |= timing->vactive.typ - 1;
185 lcdc_writel(reg, ATMEL_LCDC_LCDFRMCFG, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200186
187 /* FIFO Threshold: Use formula from data sheet */
188 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
Simon Glassd63ec262016-05-05 07:28:19 -0600189 lcdc_writel(reg, ATMEL_LCDC_FIFO, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200190
191 /* Toggle LCD_MODE every frame */
Simon Glassd63ec262016-05-05 07:28:19 -0600192 lcdc_writel(reg, ATMEL_LCDC_MVAL, 0);
Stelian Pop39cf4802008-05-09 21:57:18 +0200193
194 /* Disable all interrupts */
Simon Glassd63ec262016-05-05 07:28:19 -0600195 lcdc_writel(reg, ATMEL_LCDC_IDR, ~0UL);
Stelian Pop39cf4802008-05-09 21:57:18 +0200196
197 /* Set contrast */
198 value = ATMEL_LCDC_PS_DIV8 |
Stelian Pop39cf4802008-05-09 21:57:18 +0200199 ATMEL_LCDC_ENA_PWMENABLE;
Simon Glassd63ec262016-05-05 07:28:19 -0600200 if (!cont_pol_low)
Alexander Steincdfcedb2010-07-20 08:55:40 +0200201 value |= ATMEL_LCDC_POL_POSITIVE;
Simon Glassd63ec262016-05-05 07:28:19 -0600202 lcdc_writel(reg, ATMEL_LCDC_CONTRAST_CTR, value);
203 lcdc_writel(reg, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
Stelian Pop39cf4802008-05-09 21:57:18 +0200204
205 /* Set framebuffer DMA base address and pixel offset */
Simon Glassd63ec262016-05-05 07:28:19 -0600206 lcdc_writel(reg, ATMEL_LCDC_DMABADDR1, lcdbase);
Stelian Pop39cf4802008-05-09 21:57:18 +0200207
Simon Glassd63ec262016-05-05 07:28:19 -0600208 lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMAEN);
209 lcdc_writel(reg, ATMEL_LCDC_PWRCON,
Mark Jackson6bbced62009-06-29 15:59:10 +0100210 (ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
Stelian Pop39cf4802008-05-09 21:57:18 +0200211}
212
Simon Glass9dc89a02016-05-05 07:28:20 -0600213#ifndef CONFIG_DM_VIDEO
Simon Glassd63ec262016-05-05 07:28:19 -0600214void lcd_ctrl_init(void *lcdbase)
215{
216 struct display_timing timing;
217
218 timing.flags = 0;
219 if (!(panel_info.vl_sync & ATMEL_LCDC_INVLINE_INVERTED))
220 timing.flags |= DISPLAY_FLAGS_HSYNC_HIGH;
221 if (!(panel_info.vl_sync & ATMEL_LCDC_INVFRAME_INVERTED))
222 timing.flags |= DISPLAY_FLAGS_VSYNC_LOW;
223 timing.pixelclock.typ = panel_info.vl_clk;
224
225 timing.hactive.typ = panel_info.vl_col;
226 timing.hfront_porch.typ = panel_info.vl_right_margin;
227 timing.hback_porch.typ = panel_info.vl_left_margin;
228 timing.hsync_len.typ = panel_info.vl_hsync_len;
229
230 timing.vactive.typ = panel_info.vl_row;
231 timing.vfront_porch.typ = panel_info.vl_clk;
232 timing.vback_porch.typ = panel_info.vl_clk;
233 timing.vsync_len.typ = panel_info.vl_clk;
234
235 atmel_fb_init(panel_info.mmio, &timing, panel_info.vl_bpix,
236 panel_info.vl_tft, panel_info.vl_cont_pol_low,
237 (ulong)lcdbase);
238}
239
Stelian Pop39cf4802008-05-09 21:57:18 +0200240ulong calc_fbsize(void)
241{
242 return ((panel_info.vl_col * panel_info.vl_row *
243 NBITS(panel_info.vl_bpix)) / 8) + PAGE_SIZE;
244}
Simon Glass9dc89a02016-05-05 07:28:20 -0600245#endif
246
247#ifdef CONFIG_DM_VIDEO
248static int atmel_fb_lcd_probe(struct udevice *dev)
249{
250 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
251 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
252 struct atmel_fb_priv *priv = dev_get_priv(dev);
253 struct display_timing *timing = &priv->timing;
254
255 /*
256 * For now some values are hard-coded. We could use the device tree
257 * bindings in simple-framebuffer.txt to specify the format/bpp and
258 * some Atmel-specific binding for tft and cont_pol_low.
259 */
260 atmel_fb_init(ATMEL_BASE_LCDC, timing, VIDEO_BPP16, true, false,
261 uc_plat->base);
262 uc_priv->xsize = timing->hactive.typ;
263 uc_priv->ysize = timing->vactive.typ;
264 uc_priv->bpix = VIDEO_BPP16;
265 video_set_flush_dcache(dev, true);
266 debug("LCD frame buffer at %lx, size %x, %dx%d pixels\n", uc_plat->base,
267 uc_plat->size, uc_priv->xsize, uc_priv->ysize);
268
269 return 0;
270}
271
272static int atmel_fb_ofdata_to_platdata(struct udevice *dev)
273{
274 struct atmel_lcd_platdata *plat = dev_get_platdata(dev);
275 struct atmel_fb_priv *priv = dev_get_priv(dev);
276 struct display_timing *timing = &priv->timing;
277 const void *blob = gd->fdt_blob;
278
279 if (fdtdec_decode_display_timing(blob, dev->of_offset,
280 plat->timing_index, timing)) {
281 debug("%s: Failed to decode display timing\n", __func__);
282 return -EINVAL;
283 }
284
285 return 0;
286}
287
288static int atmel_fb_lcd_bind(struct udevice *dev)
289{
290 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
291
292 uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
293 (1 << VIDEO_BPP16) / 8;
294 debug("%s: Frame buffer size %x\n", __func__, uc_plat->size);
295
296 return 0;
297}
298
299static const struct udevice_id atmel_fb_lcd_ids[] = {
300 { .compatible = "atmel,at91sam9g45-lcdc" },
301 { }
302};
303
304U_BOOT_DRIVER(atmel_fb) = {
305 .name = "atmel_fb",
306 .id = UCLASS_VIDEO,
307 .of_match = atmel_fb_lcd_ids,
308 .bind = atmel_fb_lcd_bind,
309 .ofdata_to_platdata = atmel_fb_ofdata_to_platdata,
310 .probe = atmel_fb_lcd_probe,
311 .platdata_auto_alloc_size = sizeof(struct atmel_lcd_platdata),
312 .priv_auto_alloc_size = sizeof(struct atmel_fb_priv),
313};
314#endif