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