66c64f8056a207a003d3c6a86d68dcc02dc1e703
[oweals/u-boot.git] / board / imgtec / ci20 / ci20.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * CI20 setup code
4  *
5  * Copyright (c) 2013 Imagination Technologies
6  * Author: Paul Burton <paul.burton@imgtec.com>
7  */
8
9 #include <common.h>
10 #include <env.h>
11 #include <init.h>
12 #include <net.h>
13 #include <netdev.h>
14 #include <asm/io.h>
15 #include <asm/gpio.h>
16 #include <mach/jz4780.h>
17 #include <mach/jz4780_dram.h>
18 #include <mach/jz4780_gpio.h>
19
20 struct ci20_otp {
21         u32     serial_number;
22         u32     date;
23         u8      manufacturer[2];
24         u8      mac[6];
25 } __packed;
26
27 static void ci20_mux_mmc(void)
28 {
29         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
30
31         /* setup MSC1 pins */
32         writel(0x30f00000, gpio_regs + GPIO_PXINTC(4));
33         writel(0x30f00000, gpio_regs + GPIO_PXMASKC(4));
34         writel(0x30f00000, gpio_regs + GPIO_PXPAT1C(4));
35         writel(0x30f00000, gpio_regs + GPIO_PXPAT0C(4));
36         writel(0x30f00000, gpio_regs + GPIO_PXPENC(4));
37         jz4780_clk_ungate_mmc();
38 }
39
40 #ifndef CONFIG_SPL_BUILD
41
42 static void ci20_mux_eth(void)
43 {
44         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
45
46 #ifdef CONFIG_MTD_RAW_NAND
47         /* setup pins (some already setup for NAND) */
48         writel(0x04030000, gpio_regs + GPIO_PXINTC(0));
49         writel(0x04030000, gpio_regs + GPIO_PXMASKC(0));
50         writel(0x04030000, gpio_regs + GPIO_PXPAT1C(0));
51         writel(0x04030000, gpio_regs + GPIO_PXPAT0C(0));
52         writel(0x04030000, gpio_regs + GPIO_PXPENS(0));
53 #else
54         /* setup pins (as above +NAND CS +RD/WE +SDx +SAx) */
55         writel(0x0dff00ff, gpio_regs + GPIO_PXINTC(0));
56         writel(0x0dff00ff, gpio_regs + GPIO_PXMASKC(0));
57         writel(0x0dff00ff, gpio_regs + GPIO_PXPAT1C(0));
58         writel(0x0dff00ff, gpio_regs + GPIO_PXPAT0C(0));
59         writel(0x0dff00ff, gpio_regs + GPIO_PXPENS(0));
60         writel(0x00000003, gpio_regs + GPIO_PXINTC(1));
61         writel(0x00000003, gpio_regs + GPIO_PXMASKC(1));
62         writel(0x00000003, gpio_regs + GPIO_PXPAT1C(1));
63         writel(0x00000003, gpio_regs + GPIO_PXPAT0C(1));
64         writel(0x00000003, gpio_regs + GPIO_PXPENS(1));
65 #endif
66 }
67
68 static void ci20_mux_jtag(void)
69 {
70 #ifdef CONFIG_JTAG
71         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
72
73         /* enable JTAG */
74         writel(3 << 30, gpio_regs + GPIO_PXINTC(0));
75         writel(3 << 30, gpio_regs + GPIO_PXMASKC(0));
76         writel(3 << 30, gpio_regs + GPIO_PXPAT1C(0));
77         writel(3 << 30, gpio_regs + GPIO_PXPAT0C(0));
78 #endif
79 }
80
81 static void ci20_mux_nand(void)
82 {
83         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
84
85         /* setup pins */
86         writel(0x002c00ff, gpio_regs + GPIO_PXINTC(0));
87         writel(0x002c00ff, gpio_regs + GPIO_PXMASKC(0));
88         writel(0x002c00ff, gpio_regs + GPIO_PXPAT1C(0));
89         writel(0x002c00ff, gpio_regs + GPIO_PXPAT0C(0));
90         writel(0x002c00ff, gpio_regs + GPIO_PXPENS(0));
91         writel(0x00000003, gpio_regs + GPIO_PXINTC(1));
92         writel(0x00000003, gpio_regs + GPIO_PXMASKC(1));
93         writel(0x00000003, gpio_regs + GPIO_PXPAT1C(1));
94         writel(0x00000003, gpio_regs + GPIO_PXPAT0C(1));
95         writel(0x00000003, gpio_regs + GPIO_PXPENS(1));
96
97         /* FRB0_N */
98         jz47xx_gpio_direction_input(JZ_GPIO(0, 20));
99         writel(20, gpio_regs + GPIO_PXPENS(0));
100
101         /* disable write protect */
102         jz47xx_gpio_direction_output(JZ_GPIO(5, 22), 1);
103 }
104
105 static void ci20_mux_uart(void)
106 {
107         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
108
109         /* UART0 */
110         writel(0x9, gpio_regs + GPIO_PXINTC(5));
111         writel(0x9, gpio_regs + GPIO_PXMASKC(5));
112         writel(0x9, gpio_regs + GPIO_PXPAT1C(5));
113         writel(0x9, gpio_regs + GPIO_PXPAT0C(5));
114         writel(0x9, gpio_regs + GPIO_PXPENC(5));
115         jz4780_clk_ungate_uart(0);
116
117         /* UART 1 and 2 */
118         jz4780_clk_ungate_uart(1);
119         jz4780_clk_ungate_uart(2);
120
121 #ifndef CONFIG_JTAG
122         /* UART3 */
123         writel(1 << 12, gpio_regs + GPIO_PXINTC(3));
124         writel(1 << 12, gpio_regs + GPIO_PXMASKS(3));
125         writel(1 << 12, gpio_regs + GPIO_PXPAT1S(3));
126         writel(1 << 12, gpio_regs + GPIO_PXPAT0C(3));
127         writel(3 << 30, gpio_regs + GPIO_PXINTC(0));
128         writel(3 << 30, gpio_regs + GPIO_PXMASKC(0));
129         writel(3 << 30, gpio_regs + GPIO_PXPAT1C(0));
130         writel(1 << 30, gpio_regs + GPIO_PXPAT0C(0));
131         writel(1 << 31, gpio_regs + GPIO_PXPAT0S(0));
132         jz4780_clk_ungate_uart(3);
133 #endif
134
135         /* UART4 */
136         writel(0x100400, gpio_regs + GPIO_PXINTC(2));
137         writel(0x100400, gpio_regs + GPIO_PXMASKC(2));
138         writel(0x100400, gpio_regs + GPIO_PXPAT1S(2));
139         writel(0x100400, gpio_regs + GPIO_PXPAT0C(2));
140         writel(0x100400, gpio_regs + GPIO_PXPENC(2));
141         jz4780_clk_ungate_uart(4);
142 }
143
144 int board_early_init_f(void)
145 {
146         ci20_mux_jtag();
147         ci20_mux_uart();
148
149         ci20_mux_eth();
150         ci20_mux_mmc();
151         ci20_mux_nand();
152
153         /* SYS_POWER_IND high (LED blue, VBUS off) */
154         jz47xx_gpio_direction_output(JZ_GPIO(5, 15), 0);
155
156         /* LEDs off */
157         jz47xx_gpio_direction_output(JZ_GPIO(2, 0), 0);
158         jz47xx_gpio_direction_output(JZ_GPIO(2, 1), 0);
159         jz47xx_gpio_direction_output(JZ_GPIO(2, 2), 0);
160         jz47xx_gpio_direction_output(JZ_GPIO(2, 3), 0);
161
162         return 0;
163 }
164
165 int misc_init_r(void)
166 {
167         const u32 efuse_clk = jz4780_clk_get_efuse_clk();
168         struct ci20_otp otp;
169         char manufacturer[3];
170
171         /* Read the board OTP data */
172         jz4780_efuse_init(efuse_clk);
173         jz4780_efuse_read(0x18, 16, (u8 *)&otp);
174
175         /* Set MAC address */
176         if (!is_valid_ethaddr(otp.mac)) {
177                 /* no MAC assigned, generate one from the unique chip ID */
178                 jz4780_efuse_read(0x8, 4, &otp.mac[0]);
179                 jz4780_efuse_read(0x12, 2, &otp.mac[4]);
180                 otp.mac[0] = (otp.mac[0] | 0x02) & ~0x01;
181         }
182         eth_env_set_enetaddr("ethaddr", otp.mac);
183
184         /* Put other board information into the environment */
185         env_set_ulong("serial#", otp.serial_number);
186         env_set_ulong("board_date", otp.date);
187         manufacturer[0] = otp.manufacturer[0];
188         manufacturer[1] = otp.manufacturer[1];
189         manufacturer[2] = 0;
190         env_set("board_mfr", manufacturer);
191
192         return 0;
193 }
194
195 #ifdef CONFIG_DRIVER_DM9000
196 int board_eth_init(bd_t *bis)
197 {
198         /* Enable clock */
199         jz4780_clk_ungate_ethernet();
200
201         /* Enable power (PB25) */
202         jz47xx_gpio_direction_output(JZ_GPIO(1, 25), 1);
203
204         /* Reset (PF12) */
205         mdelay(10);
206         jz47xx_gpio_direction_output(JZ_GPIO(5, 12), 0);
207         mdelay(10);
208         jz47xx_gpio_direction_output(JZ_GPIO(5, 12), 1);
209         mdelay(10);
210
211         return dm9000_initialize(bis);
212 }
213 #endif /* CONFIG_DRIVER_DM9000 */
214 #endif
215
216 static u8 ci20_revision(void)
217 {
218         void __iomem *gpio_regs = (void __iomem *)GPIO_BASE;
219         int val;
220
221         jz47xx_gpio_direction_input(JZ_GPIO(2, 18));
222         jz47xx_gpio_direction_input(JZ_GPIO(2, 19));
223
224         /* Enable pullups */
225         writel(BIT(18) | BIT(19), gpio_regs + GPIO_PXPENC(2));
226
227         /* Read PC18/19 for version */
228         val = (!!jz47xx_gpio_get_value(JZ_GPIO(2, 18))) |
229              ((!!jz47xx_gpio_get_value(JZ_GPIO(2, 19))) << 1);
230
231         if (val == 3)   /* Rev 1 boards had no pulldowns - giving 3 */
232                 return 1;
233         if (val == 1)   /* Rev 2 boards pulldown port C bit 18 giving 1 */
234                 return 2;
235
236         return 0;
237 }
238
239 int dram_init(void)
240 {
241         gd->ram_size = sdram_size(0) + sdram_size(1);
242         return 0;
243 }
244
245 /* U-Boot common routines */
246 int checkboard(void)
247 {
248         printf("Board: Creator CI20 (rev.%d)\n", ci20_revision());
249         return 0;
250 }
251
252 #ifdef CONFIG_SPL_BUILD
253
254 #if defined(CONFIG_SPL_MMC_SUPPORT)
255 int board_mmc_init(bd_t *bd)
256 {
257         ci20_mux_mmc();
258         return jz_mmc_init((void __iomem *)MSC0_BASE);
259 }
260 #endif
261
262 static const struct jz4780_ddr_config K4B2G0846Q_48_config = {
263         .timing = {
264                 (4 << DDRC_TIMING1_TRTP_BIT) | (13 << DDRC_TIMING1_TWTR_BIT) |
265                 (6 << DDRC_TIMING1_TWR_BIT) | (5 << DDRC_TIMING1_TWL_BIT),
266
267                 (4 << DDRC_TIMING2_TCCD_BIT) | (15 << DDRC_TIMING2_TRAS_BIT) |
268                 (6 << DDRC_TIMING2_TRCD_BIT) | (6 << DDRC_TIMING2_TRL_BIT),
269
270                 (4 << DDRC_TIMING3_ONUM) | (7 << DDRC_TIMING3_TCKSRE_BIT) |
271                 (6 << DDRC_TIMING3_TRP_BIT) | (4 << DDRC_TIMING3_TRRD_BIT) |
272                 (21 << DDRC_TIMING3_TRC_BIT),
273
274                 (31 << DDRC_TIMING4_TRFC_BIT) | (1 << DDRC_TIMING4_TRWCOV_BIT) |
275                 (4 << DDRC_TIMING4_TCKE_BIT) | (9 << DDRC_TIMING4_TMINSR_BIT) |
276                 (8 << DDRC_TIMING4_TXP_BIT) | (3 << DDRC_TIMING4_TMRD_BIT),
277
278                 (8 << DDRC_TIMING5_TRTW_BIT) | (4 << DDRC_TIMING5_TRDLAT_BIT) |
279                 (4 << DDRC_TIMING5_TWDLAT_BIT),
280
281                 (25 << DDRC_TIMING6_TXSRD_BIT) | (12 << DDRC_TIMING6_TFAW_BIT) |
282                 (2 << DDRC_TIMING6_TCFGW_BIT) | (2 << DDRC_TIMING6_TCFGR_BIT),
283         },
284
285         /* PHY */
286         /* Mode Register 0 */
287         .mr0 = 0x420,
288 #ifdef SDRAM_DISABLE_DLL
289         .mr1 = (DDR3_MR1_DIC_7 | DDR3_MR1_RTT_DIS | DDR3_MR1_DLL_DISABLE),
290 #else
291         .mr1 = (DDR3_MR1_DIC_7 | DDR3_MR1_RTT_DIS),
292 #endif
293
294         .ptr0 = 0x002000d4,
295         .ptr1 = 0x02230d40,
296         .ptr2 = 0x04013880,
297
298         .dtpr0 = 0x2a8f6690,
299         .dtpr1 = 0x00400860,
300         .dtpr2 = 0x10042a00,
301
302         .pullup = 0x0b,
303         .pulldn = 0x0b,
304 };
305
306 static const struct jz4780_ddr_config H5TQ2G83CFR_48_config = {
307         .timing = {
308                 (4 << DDRC_TIMING1_TRTP_BIT) | (13 << DDRC_TIMING1_TWTR_BIT) |
309                 (6 << DDRC_TIMING1_TWR_BIT) | (5 << DDRC_TIMING1_TWL_BIT),
310
311                 (4 << DDRC_TIMING2_TCCD_BIT) | (16 << DDRC_TIMING2_TRAS_BIT) |
312                 (6 << DDRC_TIMING2_TRCD_BIT) | (6 << DDRC_TIMING2_TRL_BIT),
313
314                 (4 << DDRC_TIMING3_ONUM) | (7 << DDRC_TIMING3_TCKSRE_BIT) |
315                 (6 << DDRC_TIMING3_TRP_BIT) | (4 << DDRC_TIMING3_TRRD_BIT) |
316                 (22 << DDRC_TIMING3_TRC_BIT),
317
318                 (42 << DDRC_TIMING4_TRFC_BIT) | (1 << DDRC_TIMING4_TRWCOV_BIT) |
319                 (4 << DDRC_TIMING4_TCKE_BIT) | (7 << DDRC_TIMING4_TMINSR_BIT) |
320                 (3 << DDRC_TIMING4_TXP_BIT) | (3 << DDRC_TIMING4_TMRD_BIT),
321
322                 (8 << DDRC_TIMING5_TRTW_BIT) | (4 << DDRC_TIMING5_TRDLAT_BIT) |
323                 (4 << DDRC_TIMING5_TWDLAT_BIT),
324
325                 (25 << DDRC_TIMING6_TXSRD_BIT) | (20 << DDRC_TIMING6_TFAW_BIT) |
326                 (2 << DDRC_TIMING6_TCFGW_BIT) | (2 << DDRC_TIMING6_TCFGR_BIT),
327         },
328
329         /* PHY */
330         /* Mode Register 0 */
331         .mr0 = 0x420,
332 #ifdef SDRAM_DISABLE_DLL
333         .mr1 = (DDR3_MR1_DIC_7 | DDR3_MR1_RTT_DIS | DDR3_MR1_DLL_DISABLE),
334 #else
335         .mr1 = (DDR3_MR1_DIC_7 | DDR3_MR1_RTT_DIS),
336 #endif
337
338         .ptr0 = 0x002000d4,
339         .ptr1 = 0x02d30d40,
340         .ptr2 = 0x04013880,
341
342         .dtpr0 = 0x2c906690,
343         .dtpr1 = 0x005608a0,
344         .dtpr2 = 0x10042a00,
345
346         .pullup = 0x0e,
347         .pulldn = 0x0e,
348 };
349
350 #if (CONFIG_SYS_MHZ != 1200)
351 #error No DDR configuration for CPU speed
352 #endif
353
354 const struct jz4780_ddr_config *jz4780_get_ddr_config(void)
355 {
356         const int board_revision = ci20_revision();
357
358         if (board_revision == 2)
359                 return &K4B2G0846Q_48_config;
360         else /* Fall back to H5TQ2G83CFR RAM */
361                 return &H5TQ2G83CFR_48_config;
362 }
363 #endif