board: sama5d4ek: update to support DM/DT
[oweals/u-boot.git] / board / atmel / sama5d4ek / sama5d4ek.c
1 /*
2  * Copyright (C) 2014 Atmel
3  *                    Bo Shen <voice.shen@atmel.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <asm/io.h>
10 #include <asm/arch/at91_common.h>
11 #include <asm/arch/at91_rstc.h>
12 #include <asm/arch/atmel_mpddrc.h>
13 #include <asm/arch/atmel_usba_udc.h>
14 #include <asm/arch/gpio.h>
15 #include <asm/arch/clk.h>
16 #include <asm/arch/sama5d3_smc.h>
17 #include <asm/arch/sama5d4.h>
18 #include <atmel_hlcdc.h>
19 #include <atmel_mci.h>
20 #include <lcd.h>
21 #include <mmc.h>
22 #include <net.h>
23 #include <netdev.h>
24 #include <nand.h>
25 #include <spi.h>
26 #include <version.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 #ifdef CONFIG_ATMEL_SPI
31 #ifndef CONFIG_DM_SPI
32 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
33 {
34         return bus == 0 && cs == 0;
35 }
36 #endif
37
38 void spi_cs_activate(struct spi_slave *slave)
39 {
40         at91_set_pio_output(AT91_PIO_PORTC, 3, 0);
41 }
42
43 void spi_cs_deactivate(struct spi_slave *slave)
44 {
45         at91_set_pio_output(AT91_PIO_PORTC, 3, 1);
46 }
47
48 static void sama5d4ek_spi0_hw_init(void)
49 {
50         at91_pio3_set_a_periph(AT91_PIO_PORTC, 0, 0);   /* SPI0_MISO */
51         at91_pio3_set_a_periph(AT91_PIO_PORTC, 1, 0);   /* SPI0_MOSI */
52         at91_pio3_set_a_periph(AT91_PIO_PORTC, 2, 0);   /* SPI0_SPCK */
53
54         at91_set_pio_output(AT91_PIO_PORTC, 3, 1);      /* SPI0_CS0 */
55
56         /* Enable clock */
57         at91_periph_clk_enable(ATMEL_ID_SPI0);
58 }
59 #endif /* CONFIG_ATMEL_SPI */
60
61 #ifdef CONFIG_NAND_ATMEL
62 static void sama5d4ek_nand_hw_init(void)
63 {
64         struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
65
66         at91_periph_clk_enable(ATMEL_ID_SMC);
67
68         /* Configure SMC CS3 for NAND */
69         writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(1) |
70                AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(1),
71                &smc->cs[3].setup);
72         writel(AT91_SMC_PULSE_NWE(2) | AT91_SMC_PULSE_NCS_WR(3) |
73                AT91_SMC_PULSE_NRD(2) | AT91_SMC_PULSE_NCS_RD(3),
74                &smc->cs[3].pulse);
75         writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
76                &smc->cs[3].cycle);
77         writel(AT91_SMC_TIMINGS_TCLR(2) | AT91_SMC_TIMINGS_TADL(7) |
78                AT91_SMC_TIMINGS_TAR(2)  | AT91_SMC_TIMINGS_TRR(3)   |
79                AT91_SMC_TIMINGS_TWB(7)  | AT91_SMC_TIMINGS_RBNSEL(3)|
80                AT91_SMC_TIMINGS_NFSEL(1), &smc->cs[3].timings);
81         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
82                AT91_SMC_MODE_EXNW_DISABLE |
83                AT91_SMC_MODE_DBW_8 |
84                AT91_SMC_MODE_TDF_CYCLE(3),
85                &smc->cs[3].mode);
86
87         at91_pio3_set_a_periph(AT91_PIO_PORTC, 5, 0);   /* D0 */
88         at91_pio3_set_a_periph(AT91_PIO_PORTC, 6, 0);   /* D1 */
89         at91_pio3_set_a_periph(AT91_PIO_PORTC, 7, 0);   /* D2 */
90         at91_pio3_set_a_periph(AT91_PIO_PORTC, 8, 0);   /* D3 */
91         at91_pio3_set_a_periph(AT91_PIO_PORTC, 9, 0);   /* D4 */
92         at91_pio3_set_a_periph(AT91_PIO_PORTC, 10, 0);  /* D5 */
93         at91_pio3_set_a_periph(AT91_PIO_PORTC, 11, 0);  /* D6 */
94         at91_pio3_set_a_periph(AT91_PIO_PORTC, 12, 0);  /* D7 */
95         at91_pio3_set_a_periph(AT91_PIO_PORTC, 13, 0);  /* RE */
96         at91_pio3_set_a_periph(AT91_PIO_PORTC, 14, 0);  /* WE */
97         at91_pio3_set_a_periph(AT91_PIO_PORTC, 15, 1);  /* NCS */
98         at91_pio3_set_a_periph(AT91_PIO_PORTC, 16, 1);  /* RDY */
99         at91_pio3_set_a_periph(AT91_PIO_PORTC, 17, 1);  /* ALE */
100         at91_pio3_set_a_periph(AT91_PIO_PORTC, 18, 1);  /* CLE */
101 }
102 #endif
103
104 #ifdef CONFIG_CMD_USB
105 static void sama5d4ek_usb_hw_init(void)
106 {
107         at91_set_pio_output(AT91_PIO_PORTE, 11, 0);
108         at91_set_pio_output(AT91_PIO_PORTE, 12, 0);
109         at91_set_pio_output(AT91_PIO_PORTE, 10, 0);
110 }
111 #endif
112
113 #ifdef CONFIG_LCD
114 vidinfo_t panel_info = {
115         .vl_col = 800,
116         .vl_row = 480,
117         .vl_clk = 33260000,
118         .vl_bpix = LCD_BPP,
119         .vl_tft = 1,
120         .vl_hsync_len = 5,
121         .vl_left_margin = 128,
122         .vl_right_margin = 0,
123         .vl_vsync_len = 5,
124         .vl_upper_margin = 23,
125         .vl_lower_margin = 22,
126         .mmio = ATMEL_BASE_LCDC,
127 };
128
129 /* No power up/down pin for the LCD pannel */
130 void lcd_enable(void)   { /* Empty! */ }
131 void lcd_disable(void)  { /* Empty! */ }
132
133 unsigned int has_lcdc(void)
134 {
135         return 1;
136 }
137
138 static void sama5d4ek_lcd_hw_init(void)
139 {
140         at91_pio3_set_a_periph(AT91_PIO_PORTA, 24, 0);  /* LCDPWM */
141         at91_pio3_set_a_periph(AT91_PIO_PORTA, 25, 0);  /* LCDDISP */
142         at91_pio3_set_a_periph(AT91_PIO_PORTA, 26, 0);  /* LCDVSYNC */
143         at91_pio3_set_a_periph(AT91_PIO_PORTA, 27, 0);  /* LCDHSYNC */
144         at91_pio3_set_a_periph(AT91_PIO_PORTA, 28, 0);  /* LCDDOTCK */
145         at91_pio3_set_a_periph(AT91_PIO_PORTA, 29, 0);  /* LCDDEN */
146
147         at91_pio3_set_a_periph(AT91_PIO_PORTA,  2, 0);  /* LCDD2 */
148         at91_pio3_set_a_periph(AT91_PIO_PORTA,  3, 0);  /* LCDD3 */
149         at91_pio3_set_a_periph(AT91_PIO_PORTA,  4, 0);  /* LCDD4 */
150         at91_pio3_set_a_periph(AT91_PIO_PORTA,  5, 0);  /* LCDD5 */
151         at91_pio3_set_a_periph(AT91_PIO_PORTA,  6, 0);  /* LCDD6 */
152         at91_pio3_set_a_periph(AT91_PIO_PORTA,  7, 0);  /* LCDD7 */
153
154         at91_pio3_set_a_periph(AT91_PIO_PORTA, 10, 0);  /* LCDD10 */
155         at91_pio3_set_a_periph(AT91_PIO_PORTA, 11, 0);  /* LCDD11 */
156         at91_pio3_set_a_periph(AT91_PIO_PORTA, 12, 0);  /* LCDD12 */
157         at91_pio3_set_a_periph(AT91_PIO_PORTA, 13, 0);  /* LCDD13 */
158         at91_pio3_set_a_periph(AT91_PIO_PORTA, 14, 0);  /* LCDD14 */
159         at91_pio3_set_a_periph(AT91_PIO_PORTA, 15, 0);  /* LCDD15 */
160
161         at91_pio3_set_a_periph(AT91_PIO_PORTA, 18, 0);  /* LCDD18 */
162         at91_pio3_set_a_periph(AT91_PIO_PORTA, 19, 0);  /* LCDD19 */
163         at91_pio3_set_a_periph(AT91_PIO_PORTA, 20, 0);  /* LCDD20 */
164         at91_pio3_set_a_periph(AT91_PIO_PORTA, 21, 0);  /* LCDD21 */
165         at91_pio3_set_a_periph(AT91_PIO_PORTA, 22, 0);  /* LCDD22 */
166         at91_pio3_set_a_periph(AT91_PIO_PORTA, 23, 0);  /* LCDD23 */
167
168         /* Enable clock */
169         at91_periph_clk_enable(ATMEL_ID_LCDC);
170 }
171
172 #ifdef CONFIG_LCD_INFO
173 void lcd_show_board_info(void)
174 {
175         ulong dram_size, nand_size;
176         int i;
177         char temp[32];
178
179         lcd_printf("%s\n", U_BOOT_VERSION);
180         lcd_printf("2014 ATMEL Corp\n");
181         lcd_printf("at91@atmel.com\n");
182         lcd_printf("%s CPU at %s MHz\n", get_cpu_name(),
183                    strmhz(temp, get_cpu_clk_rate()));
184
185         dram_size = 0;
186         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
187                 dram_size += gd->bd->bi_dram[i].size;
188
189         nand_size = 0;
190 #ifdef CONFIG_NAND_ATMEL
191         for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
192                 nand_size += nand_info[i]->size;
193 #endif
194         lcd_printf("%ld MB SDRAM, %ld MB NAND\n",
195                    dram_size >> 20, nand_size >> 20);
196 }
197 #endif /* CONFIG_LCD_INFO */
198
199 #endif /* CONFIG_LCD */
200
201 #ifdef CONFIG_GENERIC_ATMEL_MCI
202 void sama5d4ek_mci1_hw_init(void)
203 {
204         at91_pio3_set_c_periph(AT91_PIO_PORTE, 19, 1);  /* MCI1 CDA */
205         at91_pio3_set_c_periph(AT91_PIO_PORTE, 20, 1);  /* MCI1 DA0 */
206         at91_pio3_set_c_periph(AT91_PIO_PORTE, 21, 1);  /* MCI1 DA1 */
207         at91_pio3_set_c_periph(AT91_PIO_PORTE, 22, 1);  /* MCI1 DA2 */
208         at91_pio3_set_c_periph(AT91_PIO_PORTE, 23, 1);  /* MCI1 DA3 */
209         at91_pio3_set_c_periph(AT91_PIO_PORTE, 18, 0);  /* MCI1 CLK */
210
211         /*
212          * As the mci io internal pull down is too strong, so if the io needs
213          * external pull up, the pull up resistor will be very small, if so
214          * the power consumption will increase, so disable the interanl pull
215          * down to save the power.
216          */
217         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 18, 0);
218         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 19, 0);
219         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 20, 0);
220         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 21, 0);
221         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 22, 0);
222         at91_pio3_set_pio_pulldown(AT91_PIO_PORTE, 23, 0);
223
224         /* Enable clock */
225         at91_periph_clk_enable(ATMEL_ID_MCI1);
226 }
227
228 int board_mmc_init(bd_t *bis)
229 {
230         /* Enable power for MCI1 interface */
231         at91_set_pio_output(AT91_PIO_PORTE, 15, 0);
232
233         return atmel_mci_init((void *)ATMEL_BASE_MCI1);
234 }
235 #endif /* CONFIG_GENERIC_ATMEL_MCI */
236
237 #ifdef CONFIG_MACB
238 void sama5d4ek_macb0_hw_init(void)
239 {
240         at91_pio3_set_a_periph(AT91_PIO_PORTB, 0, 0);   /* ETXCK_EREFCK */
241         at91_pio3_set_a_periph(AT91_PIO_PORTB, 6, 0);   /* ERXDV */
242         at91_pio3_set_a_periph(AT91_PIO_PORTB, 8, 0);   /* ERX0 */
243         at91_pio3_set_a_periph(AT91_PIO_PORTB, 9, 0);   /* ERX1 */
244         at91_pio3_set_a_periph(AT91_PIO_PORTB, 7, 0);   /* ERXER */
245         at91_pio3_set_a_periph(AT91_PIO_PORTB, 2, 0);   /* ETXEN */
246         at91_pio3_set_a_periph(AT91_PIO_PORTB, 12, 0);  /* ETX0 */
247         at91_pio3_set_a_periph(AT91_PIO_PORTB, 13, 0);  /* ETX1 */
248         at91_pio3_set_a_periph(AT91_PIO_PORTB, 17, 0);  /* EMDIO */
249         at91_pio3_set_a_periph(AT91_PIO_PORTB, 16, 0);  /* EMDC */
250
251         /* Enable clock */
252         at91_periph_clk_enable(ATMEL_ID_GMAC0);
253 }
254 #endif
255
256 static void sama5d4ek_serial3_hw_init(void)
257 {
258         at91_pio3_set_b_periph(AT91_PIO_PORTE, 17, 1);  /* TXD3 */
259         at91_pio3_set_b_periph(AT91_PIO_PORTE, 16, 0);  /* RXD3 */
260
261         /* Enable clock */
262         at91_periph_clk_enable(ATMEL_ID_USART3);
263 }
264
265 int board_early_init_f(void)
266 {
267         at91_periph_clk_enable(ATMEL_ID_PIOA);
268         at91_periph_clk_enable(ATMEL_ID_PIOB);
269         at91_periph_clk_enable(ATMEL_ID_PIOC);
270         at91_periph_clk_enable(ATMEL_ID_PIOD);
271         at91_periph_clk_enable(ATMEL_ID_PIOE);
272
273         sama5d4ek_serial3_hw_init();
274
275         return 0;
276 }
277
278 int board_init(void)
279 {
280         /* adress of boot parameters */
281         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
282
283 #ifdef CONFIG_ATMEL_SPI
284         sama5d4ek_spi0_hw_init();
285 #endif
286 #ifdef CONFIG_NAND_ATMEL
287         sama5d4ek_nand_hw_init();
288 #endif
289 #ifdef CONFIG_GENERIC_ATMEL_MCI
290         sama5d4ek_mci1_hw_init();
291 #endif
292 #ifdef CONFIG_MACB
293         sama5d4ek_macb0_hw_init();
294 #endif
295 #ifdef CONFIG_LCD
296         sama5d4ek_lcd_hw_init();
297 #endif
298 #ifdef CONFIG_CMD_USB
299         sama5d4ek_usb_hw_init();
300 #endif
301 #ifdef CONFIG_USB_GADGET_ATMEL_USBA
302         at91_udp_hw_init();
303 #endif
304
305         return 0;
306 }
307
308 int dram_init(void)
309 {
310         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
311                                     CONFIG_SYS_SDRAM_SIZE);
312         return 0;
313 }
314
315 int board_eth_init(bd_t *bis)
316 {
317         int rc = 0;
318
319 #ifdef CONFIG_MACB
320         rc = macb_eth_initialize(0, (void *)ATMEL_BASE_GMAC0, 0x00);
321 #endif
322
323 #ifdef CONFIG_USB_GADGET_ATMEL_USBA
324         usba_udc_probe(&pdata);
325 #ifdef CONFIG_USB_ETH_RNDIS
326         usb_eth_initialize(bis);
327 #endif
328 #endif
329
330         return rc;
331 }
332
333 /* SPL */
334 #ifdef CONFIG_SPL_BUILD
335 void spl_board_init(void)
336 {
337 #ifdef CONFIG_SYS_USE_MMC
338         sama5d4ek_mci1_hw_init();
339 #elif CONFIG_SYS_USE_NANDFLASH
340         sama5d4ek_nand_hw_init();
341 #elif CONFIG_SYS_USE_SERIALFLASH
342         sama5d4ek_spi0_hw_init();
343 #endif
344 }
345
346 static void ddr2_conf(struct atmel_mpddrc_config *ddr2)
347 {
348         ddr2->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR2_SDRAM);
349
350         ddr2->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
351                     ATMEL_MPDDRC_CR_NR_ROW_14 |
352                     ATMEL_MPDDRC_CR_CAS_DDR_CAS3 |
353                     ATMEL_MPDDRC_CR_NB_8BANKS |
354                     ATMEL_MPDDRC_CR_NDQS_DISABLED |
355                     ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
356                     ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
357
358         ddr2->rtr = 0x2b0;
359
360         ddr2->tpr0 = (8 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
361                       3 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
362                       3 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
363                       10 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
364                       3 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
365                       2 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
366                       2 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
367                       2 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
368
369         ddr2->tpr1 = (2 << ATMEL_MPDDRC_TPR1_TXP_OFFSET |
370                       200 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
371                       25 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
372                       23 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET);
373
374         ddr2->tpr2 = (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET |
375                       2 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
376                       3 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
377                       2 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
378                       8 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET);
379 }
380
381 void mem_init(void)
382 {
383         struct atmel_mpddrc_config ddr2;
384
385         ddr2_conf(&ddr2);
386
387         /* Enable MPDDR clock */
388         at91_periph_clk_enable(ATMEL_ID_MPDDRC);
389         at91_system_clk_enable(AT91_PMC_DDR);
390
391         /* DDRAM2 Controller initialize */
392         ddr2_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddr2);
393 }
394
395 void at91_pmc_init(void)
396 {
397         u32 tmp;
398
399         tmp = AT91_PMC_PLLAR_29 |
400               AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
401               AT91_PMC_PLLXR_MUL(87) |
402               AT91_PMC_PLLXR_DIV(1);
403         at91_plla_init(tmp);
404
405         at91_pllicpr_init(AT91_PMC_IPLL_PLLA(0x0));
406
407         tmp = AT91_PMC_MCKR_H32MXDIV |
408               AT91_PMC_MCKR_PLLADIV_2 |
409               AT91_PMC_MCKR_MDIV_3 |
410               AT91_PMC_MCKR_CSS_PLLA;
411         at91_mck_init(tmp);
412 }
413 #endif