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