board: sama5d3xek: Clean up code
[oweals/u-boot.git] / board / atmel / sama5d3xek / sama5d3xek.c
1 /*
2  * Copyright (C) 2012 - 2013 Atmel Corporation
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/sama5d3_smc.h>
11 #include <asm/arch/at91_common.h>
12 #include <asm/arch/at91_rstc.h>
13 #include <asm/arch/gpio.h>
14 #include <asm/arch/clk.h>
15 #include <lcd.h>
16 #include <linux/ctype.h>
17 #include <atmel_hlcdc.h>
18 #include <phy.h>
19 #include <micrel.h>
20 #include <spl.h>
21 #include <asm/arch/atmel_mpddrc.h>
22 #include <asm/arch/at91_wdt.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 /* ------------------------------------------------------------------------- */
27 /*
28  * Miscelaneous platform dependent initialisations
29  */
30
31 #ifdef CONFIG_NAND_ATMEL
32 void sama5d3xek_nand_hw_init(void)
33 {
34         struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
35
36         at91_periph_clk_enable(ATMEL_ID_SMC);
37
38         /* Configure SMC CS3 for NAND/SmartMedia */
39         writel(AT91_SMC_SETUP_NWE(2) | AT91_SMC_SETUP_NCS_WR(1) |
40                AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(1),
41                &smc->cs[3].setup);
42         writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(5) |
43                AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(5),
44                &smc->cs[3].pulse);
45         writel(AT91_SMC_CYCLE_NWE(8) | AT91_SMC_CYCLE_NRD(8),
46                &smc->cs[3].cycle);
47         writel(AT91_SMC_TIMINGS_TCLR(3) | AT91_SMC_TIMINGS_TADL(10) |
48                AT91_SMC_TIMINGS_TAR(3)  | AT91_SMC_TIMINGS_TRR(4)   |
49                AT91_SMC_TIMINGS_TWB(5)  | AT91_SMC_TIMINGS_RBNSEL(3)|
50                AT91_SMC_TIMINGS_NFSEL(1), &smc->cs[3].timings);
51         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
52                AT91_SMC_MODE_EXNW_DISABLE |
53 #ifdef CONFIG_SYS_NAND_DBW_16
54                AT91_SMC_MODE_DBW_16 |
55 #else /* CONFIG_SYS_NAND_DBW_8 */
56                AT91_SMC_MODE_DBW_8 |
57 #endif
58                AT91_SMC_MODE_TDF_CYCLE(3),
59                &smc->cs[3].mode);
60 }
61 #endif
62
63 #ifdef CONFIG_MTD_NOR_FLASH
64 static void sama5d3xek_nor_hw_init(void)
65 {
66         struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
67
68         at91_periph_clk_enable(ATMEL_ID_SMC);
69
70         /* Configure SMC CS0 for NOR flash */
71         writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
72                AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(0),
73                &smc->cs[0].setup);
74         writel(AT91_SMC_PULSE_NWE(10) | AT91_SMC_PULSE_NCS_WR(11) |
75                AT91_SMC_PULSE_NRD(10) | AT91_SMC_PULSE_NCS_RD(11),
76                &smc->cs[0].pulse);
77         writel(AT91_SMC_CYCLE_NWE(11) | AT91_SMC_CYCLE_NRD(14),
78                &smc->cs[0].cycle);
79         writel(AT91_SMC_TIMINGS_TCLR(0) | AT91_SMC_TIMINGS_TADL(0)  |
80                AT91_SMC_TIMINGS_TAR(0)  | AT91_SMC_TIMINGS_TRR(0)   |
81                AT91_SMC_TIMINGS_TWB(0)  | AT91_SMC_TIMINGS_RBNSEL(0)|
82                AT91_SMC_TIMINGS_NFSEL(0), &smc->cs[0].timings);
83         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
84                AT91_SMC_MODE_EXNW_DISABLE |
85                AT91_SMC_MODE_DBW_16 |
86                AT91_SMC_MODE_TDF_CYCLE(1),
87                &smc->cs[0].mode);
88
89         /* Address pin (A1 ~ A23) configuration */
90         at91_pio3_set_a_periph(AT91_PIO_PORTE, 1, 0);
91         at91_pio3_set_a_periph(AT91_PIO_PORTE, 2, 0);
92         at91_pio3_set_a_periph(AT91_PIO_PORTE, 3, 0);
93         at91_pio3_set_a_periph(AT91_PIO_PORTE, 4, 0);
94         at91_pio3_set_a_periph(AT91_PIO_PORTE, 5, 0);
95         at91_pio3_set_a_periph(AT91_PIO_PORTE, 6, 0);
96         at91_pio3_set_a_periph(AT91_PIO_PORTE, 7, 0);
97         at91_pio3_set_a_periph(AT91_PIO_PORTE, 8, 0);
98         at91_pio3_set_a_periph(AT91_PIO_PORTE, 9, 0);
99         at91_pio3_set_a_periph(AT91_PIO_PORTE, 10, 0);
100         at91_pio3_set_a_periph(AT91_PIO_PORTE, 11, 0);
101         at91_pio3_set_a_periph(AT91_PIO_PORTE, 12, 0);
102         at91_pio3_set_a_periph(AT91_PIO_PORTE, 13, 0);
103         at91_pio3_set_a_periph(AT91_PIO_PORTE, 14, 0);
104         at91_pio3_set_a_periph(AT91_PIO_PORTE, 15, 0);
105         at91_pio3_set_a_periph(AT91_PIO_PORTE, 16, 0);
106         at91_pio3_set_a_periph(AT91_PIO_PORTE, 17, 0);
107         at91_pio3_set_a_periph(AT91_PIO_PORTE, 18, 0);
108         at91_pio3_set_a_periph(AT91_PIO_PORTE, 19, 0);
109         at91_pio3_set_a_periph(AT91_PIO_PORTE, 20, 0);
110         at91_pio3_set_a_periph(AT91_PIO_PORTE, 21, 0);
111         at91_pio3_set_a_periph(AT91_PIO_PORTE, 22, 0);
112         at91_pio3_set_a_periph(AT91_PIO_PORTE, 23, 0);
113         /* CS0 pin configuration */
114         at91_pio3_set_a_periph(AT91_PIO_PORTE, 26, 0);
115 }
116 #endif
117
118 #ifdef CONFIG_CMD_USB
119 static void sama5d3xek_usb_hw_init(void)
120 {
121         at91_set_pio_output(AT91_PIO_PORTD, 25, 0);
122         at91_set_pio_output(AT91_PIO_PORTD, 26, 0);
123         at91_set_pio_output(AT91_PIO_PORTD, 27, 0);
124 }
125 #endif
126
127 #ifdef CONFIG_GENERIC_ATMEL_MCI
128 static void sama5d3xek_mci_hw_init(void)
129 {
130         at91_set_pio_output(AT91_PIO_PORTB, 10, 0);     /* MCI0 Power */
131 }
132 #endif
133
134 #ifdef CONFIG_LCD
135 vidinfo_t panel_info = {
136         .vl_col = 800,
137         .vl_row = 480,
138         .vl_clk = 24000000,
139         .vl_bpix = LCD_BPP,
140         .vl_tft = 1,
141         .vl_hsync_len = 128,
142         .vl_left_margin = 64,
143         .vl_right_margin = 64,
144         .vl_vsync_len = 2,
145         .vl_upper_margin = 22,
146         .vl_lower_margin = 21,
147         .mmio = ATMEL_BASE_LCDC,
148 };
149
150 void lcd_enable(void)
151 {
152 }
153
154 void lcd_disable(void)
155 {
156 }
157
158 static void sama5d3xek_lcd_hw_init(void)
159 {
160         gd->fb_base = CONFIG_SAMA5D3_LCD_BASE;
161
162         /* The higher 8 bit of LCD is board related */
163         at91_pio3_set_c_periph(AT91_PIO_PORTC, 14, 0);  /* LCDD16 */
164         at91_pio3_set_c_periph(AT91_PIO_PORTC, 13, 0);  /* LCDD17 */
165         at91_pio3_set_c_periph(AT91_PIO_PORTC, 12, 0);  /* LCDD18 */
166         at91_pio3_set_c_periph(AT91_PIO_PORTC, 11, 0);  /* LCDD19 */
167         at91_pio3_set_c_periph(AT91_PIO_PORTC, 10, 0);  /* LCDD20 */
168         at91_pio3_set_c_periph(AT91_PIO_PORTC, 15, 0);  /* LCDD21 */
169         at91_pio3_set_c_periph(AT91_PIO_PORTE, 27, 0);  /* LCDD22 */
170         at91_pio3_set_c_periph(AT91_PIO_PORTE, 28, 0);  /* LCDD23 */
171
172         /* Configure lower 16 bit of LCD and enable clock */
173         at91_lcd_hw_init();
174 }
175
176 #ifdef CONFIG_LCD_INFO
177 #include <nand.h>
178 #include <version.h>
179
180 void lcd_show_board_info(void)
181 {
182         ulong dram_size;
183         uint64_t nand_size;
184         int i;
185         char temp[32];
186
187         lcd_printf("%s\n", U_BOOT_VERSION);
188         lcd_printf("(C) 2013 ATMEL Corp\n");
189         lcd_printf("at91@atmel.com\n");
190         lcd_printf("%s CPU at %s MHz\n", get_cpu_name(),
191                    strmhz(temp, get_cpu_clk_rate()));
192
193         dram_size = 0;
194         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
195                 dram_size += gd->bd->bi_dram[i].size;
196
197         nand_size = 0;
198 #ifdef CONFIG_NAND_ATMEL
199         for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
200                 nand_size += nand_info[i]->size;
201 #endif
202         lcd_printf("%ld MB SDRAM, %lld MB NAND\n",
203                    dram_size >> 20, nand_size >> 20);
204 }
205 #endif /* CONFIG_LCD_INFO */
206 #endif /* CONFIG_LCD */
207
208 int board_early_init_f(void)
209 {
210         at91_seriald_hw_init();
211
212         return 0;
213 }
214
215 int board_init(void)
216 {
217         /* adress of boot parameters */
218         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
219
220 #ifdef CONFIG_NAND_ATMEL
221         sama5d3xek_nand_hw_init();
222 #endif
223 #ifdef CONFIG_MTD_NOR_FLASH
224         sama5d3xek_nor_hw_init();
225 #endif
226 #ifdef CONFIG_CMD_USB
227         sama5d3xek_usb_hw_init();
228 #endif
229 #ifdef CONFIG_GENERIC_ATMEL_MCI
230         sama5d3xek_mci_hw_init();
231 #endif
232 #ifdef CONFIG_LCD
233         if (has_lcdc())
234                 sama5d3xek_lcd_hw_init();
235 #endif
236         return 0;
237 }
238
239 int dram_init(void)
240 {
241         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
242                                     CONFIG_SYS_SDRAM_SIZE);
243         return 0;
244 }
245
246 #ifdef CONFIG_BOARD_LATE_INIT
247 int board_late_init(void)
248 {
249 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
250         const int MAX_STR_LEN = 32;
251         char name[MAX_STR_LEN], *p;
252         int i;
253
254         strncpy(name, get_cpu_name(), MAX_STR_LEN);
255         for (i = 0, p = name; (*p) && (i < MAX_STR_LEN); p++, i++)
256                 *p = tolower(*p);
257
258         strcat(name, "ek.dtb");
259         setenv("dtb_name", name);
260 #endif
261         return 0;
262 }
263 #endif
264
265 /* SPL */
266 #ifdef CONFIG_SPL_BUILD
267 void spl_board_init(void)
268 {
269 #if CONFIG_SYS_USE_NANDFLASH
270         sama5d3xek_nand_hw_init();
271 #endif
272 }
273
274 static void ddr2_conf(struct atmel_mpddrc_config *ddr2)
275 {
276         ddr2->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR2_SDRAM);
277
278         ddr2->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
279                     ATMEL_MPDDRC_CR_NR_ROW_14 |
280                     ATMEL_MPDDRC_CR_CAS_DDR_CAS3 |
281                     ATMEL_MPDDRC_CR_ENRDM_ON |
282                     ATMEL_MPDDRC_CR_NB_8BANKS |
283                     ATMEL_MPDDRC_CR_NDQS_DISABLED |
284                     ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
285                     ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
286         /*
287          * As the DDR2-SDRAm device requires a refresh time is 7.8125us
288          * when DDR run at 133MHz, so it needs (7.8125us * 133MHz / 10^9) clocks
289          */
290         ddr2->rtr = 0x411;
291
292         ddr2->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
293                       2 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
294                       2 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
295                       8 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
296                       2 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
297                       2 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
298                       2 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
299                       2 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
300
301         ddr2->tpr1 = (2 << ATMEL_MPDDRC_TPR1_TXP_OFFSET |
302                       200 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
303                       28 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
304                       26 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET);
305
306         ddr2->tpr2 = (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET |
307                       2 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
308                       2 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
309                       7 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
310                       8 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET);
311 }
312
313 void mem_init(void)
314 {
315         struct atmel_mpddrc_config ddr2;
316
317         ddr2_conf(&ddr2);
318
319         /* Enable MPDDR clock */
320         at91_periph_clk_enable(ATMEL_ID_MPDDRC);
321         at91_system_clk_enable(AT91_PMC_DDR);
322
323         /* DDRAM2 Controller initialize */
324         ddr2_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddr2);
325 }
326
327 void at91_pmc_init(void)
328 {
329         u32 tmp;
330
331         tmp = AT91_PMC_PLLAR_29 |
332               AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
333               AT91_PMC_PLLXR_MUL(43) |
334               AT91_PMC_PLLXR_DIV(1);
335         at91_plla_init(tmp);
336
337         at91_pllicpr_init(AT91_PMC_IPLL_PLLA(0x3));
338
339         tmp = AT91_PMC_MCKR_MDIV_4 |
340               AT91_PMC_MCKR_CSS_PLLA;
341         at91_mck_init(tmp);
342 }
343 #endif