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