env: Move env_set() to env.h
[oweals/u-boot.git] / board / renesas / blanche / blanche.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * board/renesas/blanche/blanche.c
4  *     This file is blanche board support.
5  *
6  * Copyright (C) 2016 Renesas Electronics Corporation
7  */
8
9 #include <common.h>
10 #include <asm/arch/mmc.h>
11 #include <asm/arch/rcar-mstp.h>
12 #include <asm/arch/rmobile.h>
13 #include <asm/arch/sh_sdhi.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/gpio.h>
16 #include <asm/io.h>
17 #include <asm/mach-types.h>
18 #include <asm/processor.h>
19 #include <dm.h>
20 #include <dm/platform_data/serial_sh.h>
21 #include <env.h>
22 #include <environment.h>
23 #include <i2c.h>
24 #include <linux/errno.h>
25 #include <malloc.h>
26 #include <miiphy.h>
27 #include <mmc.h>
28 #include <netdev.h>
29 #include "qos.h"
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #define CPG_PLL1CR      0xE6150028
34 #define CPG_PLL3CR      0xE61500DC
35
36 #define TMU0_MSTP125    BIT(25)
37 #define QSPI_MSTP917    BIT(17)
38
39 struct reg_config {
40         u16     off;
41         u32     val;
42 };
43
44 static void blanche_init_sys(void)
45 {
46         struct rcar_rwdt *rwdt = (struct rcar_rwdt *)RWDT_BASE;
47         struct rcar_swdt *swdt = (struct rcar_swdt *)SWDT_BASE;
48         u32 cpu_type;
49
50         cpu_type = rmobile_get_cpu_type();
51         if (cpu_type == 0x4A) {
52                 writel(0x4D000000, CPG_PLL1CR);
53                 writel(0x4F000000, CPG_PLL3CR);
54         }
55
56         /* Watchdog init */
57         writel(0xA5A5A500, &rwdt->rwtcsra);
58         writel(0xA5A5A500, &swdt->swtcsra);
59 }
60
61 static void blanche_init_pfc(void)
62 {
63         static const struct reg_config pfc_with_unlock[] = {
64                 { 0x0004, 0x0bffffff },
65                 { 0x0008, 0x002fffff },
66                 { 0x0014, 0x00000fff },
67                 { 0x0018, 0x00010fff },
68                 { 0x001c, 0x00010fff },
69                 { 0x0020, 0x00010fff },
70                 { 0x0024, 0x00010fff },
71                 { 0x0028, 0x00010fff },
72                 { 0x002c, 0x04006000 },
73                 { 0x0030, 0x303fefe0 },
74                 { 0x0058, 0x0002000e },
75         };
76
77         static const struct reg_config pfc_without_unlock[] = {
78                 { 0x0108, 0x00000000 },
79                 { 0x010c, 0x0803FF40 },
80                 { 0x0110, 0x0000FFFF },
81                 { 0x0114, 0x00010FFF },
82                 { 0x011c, 0x0001AFFF },
83                 { 0x0124, 0x0001CFFF },
84                 { 0x0128, 0xC0438001 },
85                 { 0x012c, 0x0FC00007 },
86         };
87
88         static const u32 pfc_base = 0xe6060000;
89
90         unsigned int i;
91
92         for (i = 0; i < ARRAY_SIZE(pfc_with_unlock); i++) {
93                 writel(~pfc_with_unlock[i].val, pfc_base);
94                 writel(pfc_with_unlock[i].val,
95                        pfc_base | pfc_with_unlock[i].off);
96         }
97
98         for (i = 0; i < ARRAY_SIZE(pfc_without_unlock); i++)
99                 writel(pfc_without_unlock[i].val,
100                        pfc_base | pfc_without_unlock[i].off);
101 }
102
103 static void blanche_init_lbsc(void)
104 {
105         static const struct reg_config lbsc_config[] = {
106                 { 0x00, 0x00000020 },
107                 { 0x08, 0x00002020 },
108                 { 0x30, 0x2a103320 },
109                 { 0x38, 0x19102110 },
110         };
111
112         static const u32 lbsc_base = 0xfec00200;
113
114         unsigned int i;
115
116         for (i = 0; i < ARRAY_SIZE(lbsc_config); i++) {
117                 writel(lbsc_config[i].val,
118                        lbsc_base | lbsc_config[i].off);
119                 writel(lbsc_config[i].val,
120                        lbsc_base | (lbsc_config[i].off + 4));
121         }
122 }
123
124 #if defined(CONFIG_MTD_NOR_FLASH)
125 static void dbsc_wait(u16 reg)
126 {
127         static const u32 dbsc3_0_base = DBSC3_0_BASE;
128
129         while (!(readl(dbsc3_0_base + reg) & BIT(0)))
130                 ;
131 }
132
133 static void blanche_init_dbsc(void)
134 {
135         static const struct reg_config dbsc_config1[] = {
136                 { 0x0280, 0x0000a55a },
137                 { 0x0018, 0x21000000 },
138                 { 0x0018, 0x11000000 },
139                 { 0x0018, 0x10000000 },
140                 { 0x0290, 0x00000001 },
141                 { 0x02a0, 0x80000000 },
142                 { 0x0290, 0x00000004 },
143         };
144
145         static const struct reg_config dbsc_config2[] = {
146                 { 0x0290, 0x00000006 },
147                 { 0x02a0, 0x0001c000 },
148         };
149
150         static const struct reg_config dbsc_config4[] = {
151                 { 0x0290, 0x0000000f },
152                 { 0x02a0, 0x00181ee4 },
153                 { 0x0290, 0x00000010 },
154                 { 0x02a0, 0xf00464db },
155                 { 0x0290, 0x00000061 },
156                 { 0x02a0, 0x0000008d },
157                 { 0x0290, 0x00000001 },
158                 { 0x02a0, 0x00000073 },
159                 { 0x0020, 0x00000007 },
160                 { 0x0024, 0x0f030a02 },
161                 { 0x0030, 0x00000001 },
162                 { 0x00b0, 0x00000000 },
163                 { 0x0040, 0x0000000b },
164                 { 0x0044, 0x00000008 },
165                 { 0x0048, 0x00000000 },
166                 { 0x0050, 0x0000000b },
167                 { 0x0054, 0x000c000b },
168                 { 0x0058, 0x00000027 },
169                 { 0x005c, 0x0000001c },
170                 { 0x0060, 0x00000006 },
171                 { 0x0064, 0x00000020 },
172                 { 0x0068, 0x00000008 },
173                 { 0x006c, 0x0000000c },
174                 { 0x0070, 0x00000009 },
175                 { 0x0074, 0x00000012 },
176                 { 0x0078, 0x000000d0 },
177                 { 0x007c, 0x00140005 },
178                 { 0x0080, 0x00050004 },
179                 { 0x0084, 0x70233005 },
180                 { 0x0088, 0x000c0000 },
181                 { 0x008c, 0x00000300 },
182                 { 0x0090, 0x00000040 },
183                 { 0x0100, 0x00000001 },
184                 { 0x00c0, 0x00020001 },
185                 { 0x00c8, 0x20082004 },
186                 { 0x0380, 0x00020002 },
187                 { 0x0390, 0x0000001f },
188         };
189
190         static const struct reg_config dbsc_config5[] = {
191                 { 0x0244, 0x00000011 },
192                 { 0x0290, 0x00000003 },
193                 { 0x02a0, 0x0300c4e1 },
194                 { 0x0290, 0x00000023 },
195                 { 0x02a0, 0x00fcdb60 },
196                 { 0x0290, 0x00000011 },
197                 { 0x02a0, 0x1000040b },
198                 { 0x0290, 0x00000012 },
199                 { 0x02a0, 0x9d9cbb66 },
200                 { 0x0290, 0x00000013 },
201                 { 0x02a0, 0x1a868400 },
202                 { 0x0290, 0x00000014 },
203                 { 0x02a0, 0x300214d8 },
204                 { 0x0290, 0x00000015 },
205                 { 0x02a0, 0x00000d70 },
206                 { 0x0290, 0x00000016 },
207                 { 0x02a0, 0x00000004 },
208                 { 0x0290, 0x00000017 },
209                 { 0x02a0, 0x00000018 },
210                 { 0x0290, 0x0000001a },
211                 { 0x02a0, 0x910035c7 },
212                 { 0x0290, 0x00000004 },
213         };
214
215         static const struct reg_config dbsc_config6[] = {
216                 { 0x0290, 0x00000001 },
217                 { 0x02a0, 0x00000181 },
218                 { 0x0018, 0x11000000 },
219                 { 0x0290, 0x00000004 },
220         };
221
222         static const struct reg_config dbsc_config7[] = {
223                 { 0x0290, 0x00000001 },
224                 { 0x02a0, 0x0000fe01 },
225                 { 0x0304, 0x00000000 },
226                 { 0x00f4, 0x01004c20 },
227                 { 0x00f8, 0x014000aa },
228                 { 0x00e0, 0x00000140 },
229                 { 0x00e4, 0x00081860 },
230                 { 0x00e8, 0x00010000 },
231                 { 0x0290, 0x00000004 },
232         };
233
234         static const struct reg_config dbsc_config8[] = {
235                 { 0x0014, 0x00000001 },
236                 { 0x0010, 0x00000001 },
237                 { 0x0280, 0x00000000 },
238         };
239
240         static const u32 dbsc3_0_base = DBSC3_0_BASE;
241         unsigned int i;
242
243         for (i = 0; i < ARRAY_SIZE(dbsc_config1); i++)
244                 writel(dbsc_config1[i].val, dbsc3_0_base | dbsc_config1[i].off);
245
246         dbsc_wait(0x2a0);
247
248         for (i = 0; i < ARRAY_SIZE(dbsc_config2); i++)
249                 writel(dbsc_config2[i].val, dbsc3_0_base | dbsc_config2[i].off);
250
251         for (i = 0; i < ARRAY_SIZE(dbsc_config4); i++)
252                 writel(dbsc_config4[i].val, dbsc3_0_base | dbsc_config4[i].off);
253
254         dbsc_wait(0x240);
255
256         for (i = 0; i < ARRAY_SIZE(dbsc_config5); i++)
257                 writel(dbsc_config5[i].val, dbsc3_0_base | dbsc_config5[i].off);
258
259         dbsc_wait(0x2a0);
260
261         for (i = 0; i < ARRAY_SIZE(dbsc_config6); i++)
262                 writel(dbsc_config6[i].val, dbsc3_0_base | dbsc_config6[i].off);
263
264         dbsc_wait(0x2a0);
265
266         for (i = 0; i < ARRAY_SIZE(dbsc_config7); i++)
267                 writel(dbsc_config7[i].val, dbsc3_0_base | dbsc_config7[i].off);
268
269         dbsc_wait(0x2a0);
270
271         for (i = 0; i < ARRAY_SIZE(dbsc_config8); i++)
272                 writel(dbsc_config8[i].val, dbsc3_0_base | dbsc_config8[i].off);
273
274 }
275
276 static void s_init_wait(volatile unsigned int cnt)
277 {
278         volatile u32 i = cnt * 0x10000;
279
280         while (i-- > 0)
281                 ;
282 }
283 #endif
284
285 void s_init(void)
286 {
287         blanche_init_sys();
288         qos_init();
289         blanche_init_pfc();
290         blanche_init_lbsc();
291 #if defined(CONFIG_MTD_NOR_FLASH)
292         s_init_wait(10);
293         blanche_init_dbsc();
294 #endif /* CONFIG_MTD_NOR_FLASH */
295 }
296
297 int board_early_init_f(void)
298 {
299         /* TMU0 */
300         mstp_clrbits_le32(MSTPSR1, SMSTPCR1, TMU0_MSTP125);
301         /* QSPI */
302         mstp_clrbits_le32(MSTPSR9, SMSTPCR9, QSPI_MSTP917);
303
304         return 0;
305 }
306
307 int board_init(void)
308 {
309         /* adress of boot parameters */
310         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
311
312         return 0;
313 }
314
315 /* Added for BLANCHE(R-CarV2H board) */
316 int board_eth_init(bd_t *bis)
317 {
318         int rc = 0;
319
320 #ifdef CONFIG_SMC911X
321         struct eth_device *dev;
322         uchar eth_addr[6];
323
324         rc = smc911x_initialize(0, CONFIG_SMC911X_BASE);
325
326         if (!eth_env_get_enetaddr("ethaddr", eth_addr)) {
327                 dev = eth_get_dev_by_index(0);
328                 if (dev) {
329                         eth_env_set_enetaddr("ethaddr", dev->enetaddr);
330                 } else {
331                         printf("blanche: Couldn't get eth device\n");
332                         rc = -1;
333                 }
334         }
335
336 #endif
337
338         return rc;
339 }
340
341 int dram_init(void)
342 {
343         if (fdtdec_setup_mem_size_base() != 0)
344                 return -EINVAL;
345
346         return 0;
347 }
348
349 int dram_init_banksize(void)
350 {
351         fdtdec_setup_memory_banksize();
352
353         return 0;
354 }
355
356 void reset_cpu(ulong addr)
357 {
358 }