Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for AT91/AT32 LCD Controller |
| 4 | * |
| 5 | * Copyright (C) 2007 Atmel Corporation |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 9 | #include <atmel_lcd.h> |
| 10 | #include <dm.h> |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 11 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <part.h> |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 14 | #include <video.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 16 | #include <asm/io.h> |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 17 | #include <asm/arch/gpio.h> |
| 18 | #include <asm/arch/clk.h> |
Nikita Kiryanov | 0b29a89 | 2015-02-03 13:32:27 +0200 | [diff] [blame] | 19 | #include <bmp_layout.h> |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 20 | #include <atmel_lcdc.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 22 | |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 25 | enum { |
| 26 | /* Maximum LCD size we support */ |
| 27 | LCD_MAX_WIDTH = 1366, |
| 28 | LCD_MAX_HEIGHT = 768, |
| 29 | LCD_MAX_LOG2_BPP = VIDEO_BPP16, |
| 30 | }; |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 31 | |
| 32 | struct atmel_fb_priv { |
| 33 | struct display_timing timing; |
| 34 | }; |
| 35 | |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 36 | /* configurable parameters */ |
| 37 | #define ATMEL_LCDC_CVAL_DEFAULT 0xc8 |
| 38 | #define ATMEL_LCDC_DMA_BURST_LEN 8 |
Mark Jackson | 6bbced6 | 2009-06-29 15:59:10 +0100 | [diff] [blame] | 39 | #ifndef ATMEL_LCDC_GUARD_TIME |
| 40 | #define ATMEL_LCDC_GUARD_TIME 1 |
| 41 | #endif |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 42 | |
Bo Shen | c6941e1 | 2015-01-16 10:55:46 +0800 | [diff] [blame] | 43 | #if defined(CONFIG_AT91SAM9263) |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 44 | #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 Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 52 | static void atmel_fb_init(ulong addr, struct display_timing *timing, int bpix, |
| 53 | bool tft, bool cont_pol_low, ulong lcdbase) |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 54 | { |
| 55 | unsigned long value; |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 56 | void *reg = (void *)addr; |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 57 | |
| 58 | /* Turn off the LCD controller and the DMA controller */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 59 | lcdc_writel(reg, ATMEL_LCDC_PWRCON, |
Mark Jackson | 6bbced6 | 2009-06-29 15:59:10 +0100 | [diff] [blame] | 60 | ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 61 | |
| 62 | /* Wait for the LCDC core to become idle */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 63 | while (lcdc_readl(reg, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY) |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 64 | udelay(10); |
| 65 | |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 66 | lcdc_writel(reg, ATMEL_LCDC_DMACON, 0); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 67 | |
| 68 | /* Reset LCDC DMA */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 69 | lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMARST); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 70 | |
| 71 | /* ...set frame size and burst length = 8 words (?) */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 72 | value = (timing->hactive.typ * timing->vactive.typ * |
| 73 | (1 << bpix)) / 32; |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 74 | value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET); |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 75 | lcdc_writel(reg, ATMEL_LCDC_DMAFRMCFG, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 76 | |
| 77 | /* Set pixel clock */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 78 | value = get_lcdc_clk_rate(0) / timing->pixelclock.typ; |
| 79 | if (get_lcdc_clk_rate(0) % timing->pixelclock.typ) |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 80 | value++; |
| 81 | value = (value / 2) - 1; |
| 82 | |
| 83 | if (!value) { |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 84 | lcdc_writel(reg, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 85 | } else |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 86 | lcdc_writel(reg, ATMEL_LCDC_LCDCON1, |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 87 | value << ATMEL_LCDC_CLKVAL_OFFSET); |
| 88 | |
| 89 | /* Initialize control register 2 */ |
| 90 | value = ATMEL_LCDC_MEMOR_LITTLE | ATMEL_LCDC_CLKMOD_ALWAYSACTIVE; |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 91 | if (tft) |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 92 | value |= ATMEL_LCDC_DISTYPE_TFT; |
| 93 | |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 94 | if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH)) |
| 95 | value |= ATMEL_LCDC_INVLINE_INVERTED; |
| 96 | if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH)) |
| 97 | value |= ATMEL_LCDC_INVFRAME_INVERTED; |
| 98 | value |= bpix << 5; |
| 99 | lcdc_writel(reg, ATMEL_LCDC_LCDCON2, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 100 | |
| 101 | /* Vertical timing */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 102 | value = (timing->vsync_len.typ - 1) << ATMEL_LCDC_VPW_OFFSET; |
| 103 | value |= timing->vback_porch.typ << ATMEL_LCDC_VBP_OFFSET; |
| 104 | value |= timing->vfront_porch.typ; |
| 105 | /* Magic! (Datasheet says "Bit 31 must be written to 1") */ |
| 106 | value |= 1U << 31; |
| 107 | lcdc_writel(reg, ATMEL_LCDC_TIM1, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 108 | |
| 109 | /* Horizontal timing */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 110 | value = (timing->hfront_porch.typ - 1) << ATMEL_LCDC_HFP_OFFSET; |
| 111 | value |= (timing->hsync_len.typ - 1) << ATMEL_LCDC_HPW_OFFSET; |
| 112 | value |= (timing->hback_porch.typ - 1); |
| 113 | lcdc_writel(reg, ATMEL_LCDC_TIM2, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 114 | |
| 115 | /* Display size */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 116 | value = (timing->hactive.typ - 1) << ATMEL_LCDC_HOZVAL_OFFSET; |
| 117 | value |= timing->vactive.typ - 1; |
| 118 | lcdc_writel(reg, ATMEL_LCDC_LCDFRMCFG, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 119 | |
| 120 | /* FIFO Threshold: Use formula from data sheet */ |
| 121 | value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3); |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 122 | lcdc_writel(reg, ATMEL_LCDC_FIFO, value); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 123 | |
| 124 | /* Toggle LCD_MODE every frame */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 125 | lcdc_writel(reg, ATMEL_LCDC_MVAL, 0); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 126 | |
| 127 | /* Disable all interrupts */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 128 | lcdc_writel(reg, ATMEL_LCDC_IDR, ~0UL); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 129 | |
| 130 | /* Set contrast */ |
| 131 | value = ATMEL_LCDC_PS_DIV8 | |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 132 | ATMEL_LCDC_ENA_PWMENABLE; |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 133 | if (!cont_pol_low) |
Alexander Stein | cdfcedb | 2010-07-20 08:55:40 +0200 | [diff] [blame] | 134 | value |= ATMEL_LCDC_POL_POSITIVE; |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 135 | lcdc_writel(reg, ATMEL_LCDC_CONTRAST_CTR, value); |
| 136 | lcdc_writel(reg, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 137 | |
| 138 | /* Set framebuffer DMA base address and pixel offset */ |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 139 | lcdc_writel(reg, ATMEL_LCDC_DMABADDR1, lcdbase); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 140 | |
Simon Glass | d63ec26 | 2016-05-05 07:28:19 -0600 | [diff] [blame] | 141 | lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMAEN); |
| 142 | lcdc_writel(reg, ATMEL_LCDC_PWRCON, |
Mark Jackson | 6bbced6 | 2009-06-29 15:59:10 +0100 | [diff] [blame] | 143 | (ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR); |
Stelian Pop | 39cf480 | 2008-05-09 21:57:18 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 146 | static int atmel_fb_lcd_probe(struct udevice *dev) |
| 147 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 148 | struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 149 | struct video_priv *uc_priv = dev_get_uclass_priv(dev); |
| 150 | struct atmel_fb_priv *priv = dev_get_priv(dev); |
| 151 | struct display_timing *timing = &priv->timing; |
| 152 | |
| 153 | /* |
| 154 | * For now some values are hard-coded. We could use the device tree |
| 155 | * bindings in simple-framebuffer.txt to specify the format/bpp and |
| 156 | * some Atmel-specific binding for tft and cont_pol_low. |
| 157 | */ |
| 158 | atmel_fb_init(ATMEL_BASE_LCDC, timing, VIDEO_BPP16, true, false, |
| 159 | uc_plat->base); |
| 160 | uc_priv->xsize = timing->hactive.typ; |
| 161 | uc_priv->ysize = timing->vactive.typ; |
| 162 | uc_priv->bpix = VIDEO_BPP16; |
| 163 | video_set_flush_dcache(dev, true); |
| 164 | debug("LCD frame buffer at %lx, size %x, %dx%d pixels\n", uc_plat->base, |
| 165 | uc_plat->size, uc_priv->xsize, uc_priv->ysize); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 170 | static int atmel_fb_of_to_plat(struct udevice *dev) |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 171 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 172 | struct atmel_lcd_plat *plat = dev_get_plat(dev); |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 173 | struct atmel_fb_priv *priv = dev_get_priv(dev); |
| 174 | struct display_timing *timing = &priv->timing; |
| 175 | const void *blob = gd->fdt_blob; |
| 176 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 177 | if (fdtdec_decode_display_timing(blob, dev_of_offset(dev), |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 178 | plat->timing_index, timing)) { |
| 179 | debug("%s: Failed to decode display timing\n", __func__); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int atmel_fb_lcd_bind(struct udevice *dev) |
| 187 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 188 | struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 189 | |
| 190 | uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT * |
| 191 | (1 << VIDEO_BPP16) / 8; |
| 192 | debug("%s: Frame buffer size %x\n", __func__, uc_plat->size); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static const struct udevice_id atmel_fb_lcd_ids[] = { |
| 198 | { .compatible = "atmel,at91sam9g45-lcdc" }, |
| 199 | { } |
| 200 | }; |
| 201 | |
| 202 | U_BOOT_DRIVER(atmel_fb) = { |
| 203 | .name = "atmel_fb", |
| 204 | .id = UCLASS_VIDEO, |
| 205 | .of_match = atmel_fb_lcd_ids, |
| 206 | .bind = atmel_fb_lcd_bind, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 207 | .of_to_plat = atmel_fb_of_to_plat, |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 208 | .probe = atmel_fb_lcd_probe, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 209 | .plat_auto = sizeof(struct atmel_lcd_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 210 | .priv_auto = sizeof(struct atmel_fb_priv), |
Simon Glass | 9dc89a0 | 2016-05-05 07:28:20 -0600 | [diff] [blame] | 211 | }; |