Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Driver for AT91/AT32 MULTI LAYER LCD Controller |
| 4 | * |
| 5 | * Copyright (C) 2012 Atmel Corporation |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame^] | 11 | #include <part.h> |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/arch/gpio.h> |
| 14 | #include <asm/arch/clk.h> |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 15 | #include <clk.h> |
| 16 | #include <dm.h> |
| 17 | #include <fdtdec.h> |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 18 | #include <lcd.h> |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 19 | #include <video.h> |
| 20 | #include <wait_bit.h> |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 21 | #include <atmel_hlcdc.h> |
| 22 | |
Nikita Kiryanov | 38b5508 | 2015-02-03 13:32:21 +0200 | [diff] [blame] | 23 | #if defined(CONFIG_LCD_LOGO) |
| 24 | #include <bmp_logo.h> |
| 25 | #endif |
| 26 | |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | #ifndef CONFIG_DM_VIDEO |
| 30 | |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 31 | /* configurable parameters */ |
| 32 | #define ATMEL_LCDC_CVAL_DEFAULT 0xc8 |
| 33 | #define ATMEL_LCDC_DMA_BURST_LEN 8 |
| 34 | #ifndef ATMEL_LCDC_GUARD_TIME |
| 35 | #define ATMEL_LCDC_GUARD_TIME 1 |
| 36 | #endif |
| 37 | |
| 38 | #define ATMEL_LCDC_FIFO_SIZE 512 |
| 39 | |
Bo Shen | cfcd1c0 | 2012-11-08 17:49:14 +0000 | [diff] [blame] | 40 | /* |
| 41 | * the CLUT register map as following |
| 42 | * RCLUT(24 ~ 16), GCLUT(15 ~ 8), BCLUT(7 ~ 0) |
| 43 | */ |
| 44 | void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue) |
| 45 | { |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 46 | writel(panel_info.mmio + ATMEL_LCDC_LUT(regno), |
| 47 | ((red << LCDC_BASECLUT_RCLUT_Pos) & LCDC_BASECLUT_RCLUT_Msk) |
| 48 | | ((green << LCDC_BASECLUT_GCLUT_Pos) & LCDC_BASECLUT_GCLUT_Msk) |
| 49 | | ((blue << LCDC_BASECLUT_BCLUT_Pos) & LCDC_BASECLUT_BCLUT_Msk)); |
Bo Shen | cfcd1c0 | 2012-11-08 17:49:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Nikita Kiryanov | 38b5508 | 2015-02-03 13:32:21 +0200 | [diff] [blame] | 52 | ushort *configuration_get_cmap(void) |
| 53 | { |
| 54 | #if defined(CONFIG_LCD_LOGO) |
| 55 | return bmp_logo_palette; |
| 56 | #else |
| 57 | return NULL; |
| 58 | #endif |
| 59 | } |
| 60 | |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 61 | void lcd_ctrl_init(void *lcdbase) |
| 62 | { |
| 63 | unsigned long value; |
| 64 | struct lcd_dma_desc *desc; |
| 65 | struct atmel_hlcd_regs *regs; |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 66 | int ret; |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 67 | |
| 68 | if (!has_lcdc()) |
| 69 | return; /* No lcdc */ |
| 70 | |
| 71 | regs = (struct atmel_hlcd_regs *)panel_info.mmio; |
| 72 | |
| 73 | /* Disable DISP signal */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 74 | writel(LCDC_LCDDIS_DISPDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 75 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS, |
| 76 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 77 | if (ret) |
| 78 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 79 | /* Disable synchronization */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 80 | writel(LCDC_LCDDIS_SYNCDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 81 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS, |
| 82 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 83 | if (ret) |
| 84 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 85 | /* Disable pixel clock */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 86 | writel(LCDC_LCDDIS_CLKDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 87 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS, |
| 88 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 89 | if (ret) |
| 90 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 91 | /* Disable PWM */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 92 | writel(LCDC_LCDDIS_PWMDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 93 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS, |
| 94 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 95 | if (ret) |
| 96 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 97 | |
| 98 | /* Set pixel clock */ |
| 99 | value = get_lcdc_clk_rate(0) / panel_info.vl_clk; |
| 100 | if (get_lcdc_clk_rate(0) % panel_info.vl_clk) |
| 101 | value++; |
| 102 | |
| 103 | if (value < 1) { |
| 104 | /* Using system clock as pixel clock */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 105 | writel(LCDC_LCDCFG0_CLKDIV(0) |
| 106 | | LCDC_LCDCFG0_CGDISHCR |
| 107 | | LCDC_LCDCFG0_CGDISHEO |
| 108 | | LCDC_LCDCFG0_CGDISOVR1 |
| 109 | | LCDC_LCDCFG0_CGDISBASE |
| 110 | | panel_info.vl_clk_pol |
| 111 | | LCDC_LCDCFG0_CLKSEL, |
| 112 | ®s->lcdc_lcdcfg0); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 113 | |
| 114 | } else { |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 115 | writel(LCDC_LCDCFG0_CLKDIV(value - 2) |
| 116 | | LCDC_LCDCFG0_CGDISHCR |
| 117 | | LCDC_LCDCFG0_CGDISHEO |
| 118 | | LCDC_LCDCFG0_CGDISOVR1 |
| 119 | | LCDC_LCDCFG0_CGDISBASE |
| 120 | | panel_info.vl_clk_pol, |
| 121 | ®s->lcdc_lcdcfg0); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Initialize control register 5 */ |
| 125 | value = 0; |
| 126 | |
| 127 | value |= panel_info.vl_sync; |
| 128 | |
| 129 | #ifndef LCD_OUTPUT_BPP |
| 130 | /* Output is 24bpp */ |
| 131 | value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP; |
| 132 | #else |
| 133 | switch (LCD_OUTPUT_BPP) { |
| 134 | case 12: |
| 135 | value |= LCDC_LCDCFG5_MODE_OUTPUT_12BPP; |
| 136 | break; |
| 137 | case 16: |
| 138 | value |= LCDC_LCDCFG5_MODE_OUTPUT_16BPP; |
| 139 | break; |
| 140 | case 18: |
| 141 | value |= LCDC_LCDCFG5_MODE_OUTPUT_18BPP; |
| 142 | break; |
| 143 | case 24: |
| 144 | value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP; |
| 145 | break; |
| 146 | default: |
| 147 | BUG(); |
| 148 | break; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | value |= LCDC_LCDCFG5_GUARDTIME(ATMEL_LCDC_GUARD_TIME); |
| 153 | value |= (LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 154 | writel(value, ®s->lcdc_lcdcfg5); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 155 | |
| 156 | /* Vertical & Horizontal Timing */ |
| 157 | value = LCDC_LCDCFG1_VSPW(panel_info.vl_vsync_len - 1); |
| 158 | value |= LCDC_LCDCFG1_HSPW(panel_info.vl_hsync_len - 1); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 159 | writel(value, ®s->lcdc_lcdcfg1); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 160 | |
Wu, Josh | 1161f98 | 2014-03-10 16:40:41 +0800 | [diff] [blame] | 161 | value = LCDC_LCDCFG2_VBPW(panel_info.vl_upper_margin); |
| 162 | value |= LCDC_LCDCFG2_VFPW(panel_info.vl_lower_margin - 1); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 163 | writel(value, ®s->lcdc_lcdcfg2); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 164 | |
Wu, Josh | 1161f98 | 2014-03-10 16:40:41 +0800 | [diff] [blame] | 165 | value = LCDC_LCDCFG3_HBPW(panel_info.vl_left_margin - 1); |
| 166 | value |= LCDC_LCDCFG3_HFPW(panel_info.vl_right_margin - 1); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 167 | writel(value, ®s->lcdc_lcdcfg3); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 168 | |
| 169 | /* Display size */ |
| 170 | value = LCDC_LCDCFG4_RPF(panel_info.vl_row - 1); |
| 171 | value |= LCDC_LCDCFG4_PPL(panel_info.vl_col - 1); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 172 | writel(value, ®s->lcdc_lcdcfg4); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 173 | |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 174 | writel(LCDC_BASECFG0_BLEN_AHB_INCR4 | LCDC_BASECFG0_DLBO, |
| 175 | ®s->lcdc_basecfg0); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 176 | |
| 177 | switch (NBITS(panel_info.vl_bpix)) { |
| 178 | case 16: |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 179 | writel(LCDC_BASECFG1_RGBMODE_16BPP_RGB_565, |
| 180 | ®s->lcdc_basecfg1); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 181 | break; |
Marek Vasut | 8c1b717 | 2015-10-23 22:55:40 +0200 | [diff] [blame] | 182 | case 32: |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 183 | writel(LCDC_BASECFG1_RGBMODE_24BPP_RGB_888, |
| 184 | ®s->lcdc_basecfg1); |
Marek Vasut | 8c1b717 | 2015-10-23 22:55:40 +0200 | [diff] [blame] | 185 | break; |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 186 | default: |
| 187 | BUG(); |
| 188 | break; |
| 189 | } |
| 190 | |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 191 | writel(LCDC_BASECFG2_XSTRIDE(0), ®s->lcdc_basecfg2); |
| 192 | writel(0, ®s->lcdc_basecfg3); |
| 193 | writel(LCDC_BASECFG4_DMA, ®s->lcdc_basecfg4); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 194 | |
| 195 | /* Disable all interrupts */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 196 | writel(~0UL, ®s->lcdc_lcdidr); |
| 197 | writel(~0UL, ®s->lcdc_baseidr); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 198 | |
| 199 | /* Setup the DMA descriptor, this descriptor will loop to itself */ |
| 200 | desc = (struct lcd_dma_desc *)(lcdbase - 16); |
| 201 | |
| 202 | desc->address = (u32)lcdbase; |
| 203 | /* Disable DMA transfer interrupt & descriptor loaded interrupt. */ |
| 204 | desc->control = LCDC_BASECTRL_ADDIEN | LCDC_BASECTRL_DSCRIEN |
| 205 | | LCDC_BASECTRL_DMAIEN | LCDC_BASECTRL_DFETCH; |
| 206 | desc->next = (u32)desc; |
| 207 | |
Wu, Josh | b137bd8 | 2014-05-19 19:51:27 +0800 | [diff] [blame] | 208 | /* Flush the DMA descriptor if we enabled dcache */ |
| 209 | flush_dcache_range((u32)desc, (u32)desc + sizeof(*desc)); |
| 210 | |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 211 | writel(desc->address, ®s->lcdc_baseaddr); |
| 212 | writel(desc->control, ®s->lcdc_basectrl); |
| 213 | writel(desc->next, ®s->lcdc_basenext); |
| 214 | writel(LCDC_BASECHER_CHEN | LCDC_BASECHER_UPDATEEN, |
| 215 | ®s->lcdc_basecher); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 216 | |
| 217 | /* Enable LCD */ |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 218 | value = readl(®s->lcdc_lcden); |
| 219 | writel(value | LCDC_LCDEN_CLKEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 220 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS, |
| 221 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 222 | if (ret) |
| 223 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 224 | value = readl(®s->lcdc_lcden); |
| 225 | writel(value | LCDC_LCDEN_SYNCEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 226 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS, |
| 227 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 228 | if (ret) |
| 229 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 230 | value = readl(®s->lcdc_lcden); |
| 231 | writel(value | LCDC_LCDEN_DISPEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 232 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS, |
| 233 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 234 | if (ret) |
| 235 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 236 | value = readl(®s->lcdc_lcden); |
| 237 | writel(value | LCDC_LCDEN_PWMEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 238 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS, |
| 239 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 240 | if (ret) |
| 241 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
Wu, Josh | b137bd8 | 2014-05-19 19:51:27 +0800 | [diff] [blame] | 242 | |
| 243 | /* Enable flushing if we enabled dcache */ |
| 244 | lcd_set_flush_dcache(1); |
Bo Shen | f6b690e | 2012-05-25 00:59:58 +0000 | [diff] [blame] | 245 | } |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 246 | |
| 247 | #else |
| 248 | |
| 249 | enum { |
| 250 | LCD_MAX_WIDTH = 1024, |
| 251 | LCD_MAX_HEIGHT = 768, |
| 252 | LCD_MAX_LOG2_BPP = VIDEO_BPP16, |
| 253 | }; |
| 254 | |
| 255 | struct atmel_hlcdc_priv { |
| 256 | struct atmel_hlcd_regs *regs; |
| 257 | struct display_timing timing; |
| 258 | unsigned int vl_bpix; |
| 259 | unsigned int output_mode; |
| 260 | unsigned int guard_time; |
| 261 | ulong clk_rate; |
| 262 | }; |
| 263 | |
| 264 | static int at91_hlcdc_enable_clk(struct udevice *dev) |
| 265 | { |
| 266 | struct atmel_hlcdc_priv *priv = dev_get_priv(dev); |
| 267 | struct clk clk; |
| 268 | ulong clk_rate; |
| 269 | int ret; |
| 270 | |
| 271 | ret = clk_get_by_index(dev, 0, &clk); |
| 272 | if (ret) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | ret = clk_enable(&clk); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | |
| 279 | clk_rate = clk_get_rate(&clk); |
| 280 | if (!clk_rate) { |
| 281 | clk_disable(&clk); |
| 282 | return -ENODEV; |
| 283 | } |
| 284 | |
| 285 | priv->clk_rate = clk_rate; |
| 286 | |
| 287 | clk_free(&clk); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void atmel_hlcdc_init(struct udevice *dev) |
| 293 | { |
| 294 | struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev); |
| 295 | struct atmel_hlcdc_priv *priv = dev_get_priv(dev); |
| 296 | struct atmel_hlcd_regs *regs = priv->regs; |
| 297 | struct display_timing *timing = &priv->timing; |
| 298 | struct lcd_dma_desc *desc; |
| 299 | unsigned long value, vl_clk_pol; |
| 300 | int ret; |
| 301 | |
| 302 | /* Disable DISP signal */ |
| 303 | writel(LCDC_LCDDIS_DISPDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 304 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS, |
| 305 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 306 | if (ret) |
| 307 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 308 | /* Disable synchronization */ |
| 309 | writel(LCDC_LCDDIS_SYNCDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 310 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS, |
| 311 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 312 | if (ret) |
| 313 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 314 | /* Disable pixel clock */ |
| 315 | writel(LCDC_LCDDIS_CLKDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 316 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS, |
| 317 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 318 | if (ret) |
| 319 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 320 | /* Disable PWM */ |
| 321 | writel(LCDC_LCDDIS_PWMDIS, ®s->lcdc_lcddis); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 322 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS, |
| 323 | false, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 324 | if (ret) |
| 325 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 326 | |
| 327 | /* Set pixel clock */ |
| 328 | value = priv->clk_rate / timing->pixelclock.typ; |
| 329 | if (priv->clk_rate % timing->pixelclock.typ) |
| 330 | value++; |
| 331 | |
| 332 | vl_clk_pol = 0; |
| 333 | if (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) |
| 334 | vl_clk_pol = LCDC_LCDCFG0_CLKPOL; |
| 335 | |
| 336 | if (value < 1) { |
| 337 | /* Using system clock as pixel clock */ |
| 338 | writel(LCDC_LCDCFG0_CLKDIV(0) |
| 339 | | LCDC_LCDCFG0_CGDISHCR |
| 340 | | LCDC_LCDCFG0_CGDISHEO |
| 341 | | LCDC_LCDCFG0_CGDISOVR1 |
| 342 | | LCDC_LCDCFG0_CGDISBASE |
| 343 | | vl_clk_pol |
| 344 | | LCDC_LCDCFG0_CLKSEL, |
| 345 | ®s->lcdc_lcdcfg0); |
| 346 | |
| 347 | } else { |
| 348 | writel(LCDC_LCDCFG0_CLKDIV(value - 2) |
| 349 | | LCDC_LCDCFG0_CGDISHCR |
| 350 | | LCDC_LCDCFG0_CGDISHEO |
| 351 | | LCDC_LCDCFG0_CGDISOVR1 |
| 352 | | LCDC_LCDCFG0_CGDISBASE |
| 353 | | vl_clk_pol, |
| 354 | ®s->lcdc_lcdcfg0); |
| 355 | } |
| 356 | |
| 357 | /* Initialize control register 5 */ |
| 358 | value = 0; |
| 359 | |
| 360 | if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH)) |
| 361 | value |= LCDC_LCDCFG5_HSPOL; |
| 362 | if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH)) |
| 363 | value |= LCDC_LCDCFG5_VSPOL; |
| 364 | |
| 365 | switch (priv->output_mode) { |
| 366 | case 12: |
| 367 | value |= LCDC_LCDCFG5_MODE_OUTPUT_12BPP; |
| 368 | break; |
| 369 | case 16: |
| 370 | value |= LCDC_LCDCFG5_MODE_OUTPUT_16BPP; |
| 371 | break; |
| 372 | case 18: |
| 373 | value |= LCDC_LCDCFG5_MODE_OUTPUT_18BPP; |
| 374 | break; |
| 375 | case 24: |
| 376 | value |= LCDC_LCDCFG5_MODE_OUTPUT_24BPP; |
| 377 | break; |
| 378 | default: |
| 379 | BUG(); |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | value |= LCDC_LCDCFG5_GUARDTIME(priv->guard_time); |
| 384 | value |= (LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS); |
| 385 | writel(value, ®s->lcdc_lcdcfg5); |
| 386 | |
| 387 | /* Vertical & Horizontal Timing */ |
| 388 | value = LCDC_LCDCFG1_VSPW(timing->vsync_len.typ - 1); |
| 389 | value |= LCDC_LCDCFG1_HSPW(timing->hsync_len.typ - 1); |
| 390 | writel(value, ®s->lcdc_lcdcfg1); |
| 391 | |
| 392 | value = LCDC_LCDCFG2_VBPW(timing->vback_porch.typ); |
| 393 | value |= LCDC_LCDCFG2_VFPW(timing->vfront_porch.typ - 1); |
| 394 | writel(value, ®s->lcdc_lcdcfg2); |
| 395 | |
| 396 | value = LCDC_LCDCFG3_HBPW(timing->hback_porch.typ - 1); |
| 397 | value |= LCDC_LCDCFG3_HFPW(timing->hfront_porch.typ - 1); |
| 398 | writel(value, ®s->lcdc_lcdcfg3); |
| 399 | |
| 400 | /* Display size */ |
| 401 | value = LCDC_LCDCFG4_RPF(timing->vactive.typ - 1); |
| 402 | value |= LCDC_LCDCFG4_PPL(timing->hactive.typ - 1); |
| 403 | writel(value, ®s->lcdc_lcdcfg4); |
| 404 | |
| 405 | writel(LCDC_BASECFG0_BLEN_AHB_INCR4 | LCDC_BASECFG0_DLBO, |
| 406 | ®s->lcdc_basecfg0); |
| 407 | |
| 408 | switch (VNBITS(priv->vl_bpix)) { |
| 409 | case 16: |
| 410 | writel(LCDC_BASECFG1_RGBMODE_16BPP_RGB_565, |
| 411 | ®s->lcdc_basecfg1); |
| 412 | break; |
| 413 | case 32: |
| 414 | writel(LCDC_BASECFG1_RGBMODE_24BPP_RGB_888, |
| 415 | ®s->lcdc_basecfg1); |
| 416 | break; |
| 417 | default: |
| 418 | BUG(); |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | writel(LCDC_BASECFG2_XSTRIDE(0), ®s->lcdc_basecfg2); |
| 423 | writel(0, ®s->lcdc_basecfg3); |
| 424 | writel(LCDC_BASECFG4_DMA, ®s->lcdc_basecfg4); |
| 425 | |
| 426 | /* Disable all interrupts */ |
| 427 | writel(~0UL, ®s->lcdc_lcdidr); |
| 428 | writel(~0UL, ®s->lcdc_baseidr); |
| 429 | |
| 430 | /* Setup the DMA descriptor, this descriptor will loop to itself */ |
Wenyou Yang | 31e5c89 | 2017-06-02 11:29:04 +0800 | [diff] [blame] | 431 | desc = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*desc)); |
| 432 | if (!desc) |
| 433 | return; |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 434 | |
| 435 | desc->address = (u32)uc_plat->base; |
| 436 | |
| 437 | /* Disable DMA transfer interrupt & descriptor loaded interrupt. */ |
| 438 | desc->control = LCDC_BASECTRL_ADDIEN | LCDC_BASECTRL_DSCRIEN |
| 439 | | LCDC_BASECTRL_DMAIEN | LCDC_BASECTRL_DFETCH; |
| 440 | desc->next = (u32)desc; |
| 441 | |
| 442 | /* Flush the DMA descriptor if we enabled dcache */ |
Wenyou Yang | 31e5c89 | 2017-06-02 11:29:04 +0800 | [diff] [blame] | 443 | flush_dcache_range((u32)desc, |
| 444 | ALIGN(((u32)desc + sizeof(*desc)), |
| 445 | CONFIG_SYS_CACHELINE_SIZE)); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 446 | |
| 447 | writel(desc->address, ®s->lcdc_baseaddr); |
| 448 | writel(desc->control, ®s->lcdc_basectrl); |
| 449 | writel(desc->next, ®s->lcdc_basenext); |
| 450 | writel(LCDC_BASECHER_CHEN | LCDC_BASECHER_UPDATEEN, |
| 451 | ®s->lcdc_basecher); |
| 452 | |
| 453 | /* Enable LCD */ |
| 454 | value = readl(®s->lcdc_lcden); |
| 455 | writel(value | LCDC_LCDEN_CLKEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 456 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_CLKSTS, |
| 457 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 458 | if (ret) |
| 459 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 460 | value = readl(®s->lcdc_lcden); |
| 461 | writel(value | LCDC_LCDEN_SYNCEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 462 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_LCDSTS, |
| 463 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 464 | if (ret) |
| 465 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 466 | value = readl(®s->lcdc_lcden); |
| 467 | writel(value | LCDC_LCDEN_DISPEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 468 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_DISPSTS, |
| 469 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 470 | if (ret) |
| 471 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 472 | value = readl(®s->lcdc_lcden); |
| 473 | writel(value | LCDC_LCDEN_PWMEN, ®s->lcdc_lcden); |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 474 | ret = wait_for_bit_le32(®s->lcdc_lcdsr, LCDC_LCDSR_PWMSTS, |
| 475 | true, 1000, false); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 476 | if (ret) |
| 477 | printf("%s: %d: Timeout!\n", __func__, __LINE__); |
| 478 | } |
| 479 | |
| 480 | static int atmel_hlcdc_probe(struct udevice *dev) |
| 481 | { |
| 482 | struct video_priv *uc_priv = dev_get_uclass_priv(dev); |
| 483 | struct atmel_hlcdc_priv *priv = dev_get_priv(dev); |
| 484 | int ret; |
| 485 | |
| 486 | ret = at91_hlcdc_enable_clk(dev); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | |
| 490 | atmel_hlcdc_init(dev); |
| 491 | |
| 492 | uc_priv->xsize = priv->timing.hactive.typ; |
| 493 | uc_priv->ysize = priv->timing.vactive.typ; |
| 494 | uc_priv->bpix = priv->vl_bpix; |
| 495 | |
| 496 | /* Enable flushing if we enabled dcache */ |
| 497 | video_set_flush_dcache(dev, true); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int atmel_hlcdc_ofdata_to_platdata(struct udevice *dev) |
| 503 | { |
| 504 | struct atmel_hlcdc_priv *priv = dev_get_priv(dev); |
| 505 | const void *blob = gd->fdt_blob; |
Simon Glass | da409cc | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 506 | int node = dev_of_offset(dev); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 507 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 508 | priv->regs = (struct atmel_hlcd_regs *)devfdt_get_addr(dev); |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 509 | if (!priv->regs) { |
| 510 | debug("%s: No display controller address\n", __func__); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
Simon Glass | da409cc | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 514 | if (fdtdec_decode_display_timing(blob, dev_of_offset(dev), |
Songjun Wu | 7927831 | 2017-04-11 16:33:30 +0800 | [diff] [blame] | 515 | 0, &priv->timing)) { |
| 516 | debug("%s: Failed to decode display timing\n", __func__); |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
| 520 | if (priv->timing.hactive.typ > LCD_MAX_WIDTH) |
| 521 | priv->timing.hactive.typ = LCD_MAX_WIDTH; |
| 522 | |
| 523 | if (priv->timing.vactive.typ > LCD_MAX_HEIGHT) |
| 524 | priv->timing.vactive.typ = LCD_MAX_HEIGHT; |
| 525 | |
| 526 | priv->vl_bpix = fdtdec_get_int(blob, node, "atmel,vl-bpix", 0); |
| 527 | if (!priv->vl_bpix) { |
| 528 | debug("%s: Failed to get bits per pixel\n", __func__); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | priv->output_mode = fdtdec_get_int(blob, node, "atmel,output-mode", 24); |
| 533 | priv->guard_time = fdtdec_get_int(blob, node, "atmel,guard-time", 1); |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int atmel_hlcdc_bind(struct udevice *dev) |
| 539 | { |
| 540 | struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev); |
| 541 | |
| 542 | uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT * |
| 543 | (1 << LCD_MAX_LOG2_BPP) / 8; |
| 544 | |
| 545 | debug("%s: Frame buffer size %x\n", __func__, uc_plat->size); |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static const struct udevice_id atmel_hlcdc_ids[] = { |
| 551 | { .compatible = "atmel,sama5d2-hlcdc" }, |
| 552 | { .compatible = "atmel,at91sam9x5-hlcdc" }, |
| 553 | { } |
| 554 | }; |
| 555 | |
| 556 | U_BOOT_DRIVER(atmel_hlcdfb) = { |
| 557 | .name = "atmel_hlcdfb", |
| 558 | .id = UCLASS_VIDEO, |
| 559 | .of_match = atmel_hlcdc_ids, |
| 560 | .bind = atmel_hlcdc_bind, |
| 561 | .probe = atmel_hlcdc_probe, |
| 562 | .ofdata_to_platdata = atmel_hlcdc_ofdata_to_platdata, |
| 563 | .priv_auto_alloc_size = sizeof(struct atmel_hlcdc_priv), |
| 564 | }; |
| 565 | |
| 566 | #endif |