xref: /openbmc/u-boot/board/renesas/lager/lager_spl.c (revision b25f8e21)
1 /*
2  * board/renesas/lager/lager_spl.c
3  *
4  * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
5  *
6  * SPDX-License-Identifier: GPL-2.0
7  */
8 
9 #include <common.h>
10 #include <malloc.h>
11 #include <dm/platform_data/serial_sh.h>
12 #include <asm/processor.h>
13 #include <asm/mach-types.h>
14 #include <asm/io.h>
15 #include <linux/errno.h>
16 #include <asm/arch/sys_proto.h>
17 #include <asm/gpio.h>
18 #include <asm/arch/rmobile.h>
19 #include <asm/arch/rcar-mstp.h>
20 
21 #include <spl.h>
22 
23 #define TMU0_MSTP125	BIT(25)
24 #define SCIF0_MSTP721	BIT(21)
25 #define QSPI_MSTP917	BIT(17)
26 
27 #define SD2CKCR		0xE615026C
28 #define SD_97500KHZ	0x7
29 
30 struct reg_config {
31 	u16	off;
32 	u32	val;
33 };
34 
35 static void dbsc_wait(u16 reg)
36 {
37 	static const u32 dbsc3_0_base = DBSC3_0_BASE;
38 
39 	while (!(readl(dbsc3_0_base + reg) & BIT(0)))
40 		;
41 }
42 
43 static void spl_init_sys(void)
44 {
45 	u32 r0 = 0;
46 
47 	writel(0xa5a5a500, 0xe6020004);
48 	writel(0xa5a5a500, 0xe6030004);
49 
50 	asm volatile(
51 		/* ICIALLU - Invalidate I$ to PoU */
52 		"mcr	15, 0, %0, cr7, cr5, 0	\n"
53 		/* BPIALL - Invalidate branch predictors */
54 		"mcr	15, 0, %0, cr7, cr5, 6	\n"
55 		/* Set SCTLR[IZ] */
56 		"mrc	15, 0, %0, cr1, cr0, 0	\n"
57 		"orr	%0, #0x1800		\n"
58 		"mcr	15, 0, %0, cr1, cr0, 0	\n"
59 		"isb	sy			\n"
60 		:"=r"(r0));
61 }
62 
63 static void spl_init_pfc(void)
64 {
65 	static const struct reg_config pfc_with_unlock[] = {
66 		{ 0x0090, 0x00000000 },
67 		{ 0x0094, 0x00000000 },
68 		{ 0x0098, 0xc0000000 },
69 		{ 0x0020, 0x00000000 },
70 		{ 0x0024, 0x00000000 },
71 		{ 0x0028, 0x00000000 },
72 		{ 0x002c, 0x20000000 },
73 		{ 0x0030, 0x00001249 },
74 		{ 0x0034, 0x00000278 },
75 		{ 0x0038, 0x00000841 },
76 		{ 0x003c, 0x00000000 },
77 		{ 0x0040, 0x00000000 },
78 		{ 0x0044, 0x10000000 },
79 		{ 0x0048, 0x00000001 },
80 		{ 0x004c, 0x0004aab0 },
81 		{ 0x0050, 0x37301b00 },
82 		{ 0x0054, 0x00048da3 },
83 		{ 0x0058, 0x089044a1 },
84 		{ 0x005c, 0x2a3a55b4 },
85 		{ 0x0160, 0x00000003 },
86 		{ 0x0004, 0xffffffff },
87 		{ 0x0008, 0x2aef3fff },
88 		{ 0x000c, 0x3fffffff },
89 		{ 0x0010, 0xff7fc07f },
90 		{ 0x0014, 0x7f3ff3f8 },
91 		{ 0x0018, 0x1cfdfff7 },
92 	};
93 
94 	static const struct reg_config pfc_without_unlock[] = {
95 		{ 0x0100, 0x1fffffff },
96 		{ 0x0104, 0xffff0318 },
97 		{ 0x0108, 0x387fffe1 },
98 		{ 0x010c, 0x00803f80 },
99 		{ 0x0110, 0x1520009f },
100 		{ 0x0114, 0x00000000 },
101 		{ 0x0118, 0x00000000 },
102 	};
103 
104 	static const u32 pfc_base = 0xe6060000;
105 
106 	unsigned int i;
107 
108 	for (i = 0; i < ARRAY_SIZE(pfc_with_unlock); i++) {
109 		writel(~pfc_with_unlock[i].val, pfc_base);
110 		writel(pfc_with_unlock[i].val,
111 		       pfc_base | pfc_with_unlock[i].off);
112 	}
113 
114 	for (i = 0; i < ARRAY_SIZE(pfc_without_unlock); i++)
115 		writel(pfc_without_unlock[i].val,
116 		       pfc_base | pfc_without_unlock[i].off);
117 }
118 
119 static void spl_init_gpio(void)
120 {
121 	static const u16 gpio_offs[] = {
122 		0x1000, 0x3000, 0x4000, 0x5000
123 	};
124 
125 	static const struct reg_config gpio_set[] = {
126 		{ 0x4000, 0x00c00000 },
127 		{ 0x5000, 0x63020000 },
128 	};
129 
130 	static const struct reg_config gpio_clr[] = {
131 		{ 0x1000, 0x00000000 },
132 		{ 0x3000, 0x00000000 },
133 		{ 0x4000, 0x00c00000 },
134 		{ 0x5000, 0xe3020000 },
135 	};
136 
137 	static const u32 gpio_base = 0xe6050000;
138 
139 	unsigned int i;
140 
141 	for (i = 0; i < ARRAY_SIZE(gpio_offs); i++)
142 		writel(0, gpio_base | 0x20 | gpio_offs[i]);
143 
144 	for (i = 0; i < ARRAY_SIZE(gpio_offs); i++)
145 		writel(0, gpio_base | 0x00 | gpio_offs[i]);
146 
147 	for (i = 0; i < ARRAY_SIZE(gpio_set); i++)
148 		writel(gpio_set[i].val, gpio_base | 0x08 | gpio_set[i].off);
149 
150 	for (i = 0; i < ARRAY_SIZE(gpio_clr); i++)
151 		writel(gpio_clr[i].val, gpio_base | 0x04 | gpio_clr[i].off);
152 }
153 
154 static void spl_init_lbsc(void)
155 {
156 	static const struct reg_config lbsc_config[] = {
157 		{ 0x00, 0x00000020 },
158 		{ 0x08, 0x00002020 },
159 		{ 0x30, 0x02150326 },
160 		{ 0x38, 0x077f077f },
161 	};
162 
163 	static const u16 lbsc_offs[] = {
164 		0x80, 0x84, 0x88, 0x8c, 0xa0, 0xc0, 0xc4, 0xc8, 0x180
165 	};
166 
167 	static const u32 lbsc_base = 0xfec00200;
168 
169 	unsigned int i;
170 
171 	for (i = 0; i < ARRAY_SIZE(lbsc_config); i++) {
172 		writel(lbsc_config[i].val,
173 		       lbsc_base | lbsc_config[i].off);
174 		writel(lbsc_config[i].val,
175 		       lbsc_base | (lbsc_config[i].off + 4));
176 	}
177 
178 	for (i = 0; i < ARRAY_SIZE(lbsc_offs); i++)
179 		writel(0, lbsc_base | lbsc_offs[i]);
180 }
181 
182 static void spl_init_dbsc(void)
183 {
184 	static const struct reg_config dbsc_config1[] = {
185 		{ 0x0018, 0x21000000 },
186 		{ 0x0018, 0x11000000 },
187 		{ 0x0018, 0x10000000 },
188 		{ 0x0280, 0x0000a55a },
189 		{ 0x0290, 0x00000001 },
190 		{ 0x02a0, 0x80000000 },
191 		{ 0x0290, 0x00000004 },
192 	};
193 
194 	static const struct reg_config dbsc_config4[] = {
195 		{ 0x0290, 0x00000010 },
196 		{ 0x02a0, 0xf004649b },
197 		{ 0x0290, 0x0000000f },
198 		{ 0x02a0, 0x00181ee4 },
199 		{ 0x0290, 0x00000060 },
200 		{ 0x02a0, 0x330657b2 },
201 		{ 0x0290, 0x00000001 },
202 		{ 0x02a0, 0x00000071 },
203 		{ 0x0020, 0x00000007 },
204 		{ 0x0024, 0x10030a02 },
205 		{ 0x0030, 0x00000001 },
206 		{ 0x00b0, 0x00000000 },
207 		{ 0x0040, 0x0000000b },
208 		{ 0x0044, 0x00000008 },
209 		{ 0x0048, 0x00000000 },
210 		{ 0x0050, 0x0000000b },
211 		{ 0x0054, 0x000c000b },
212 		{ 0x0058, 0x00000027 },
213 		{ 0x005c, 0x0000001c },
214 		{ 0x0060, 0x00000005 },
215 		{ 0x0064, 0x00000018 },
216 		{ 0x0068, 0x00000008 },
217 		{ 0x006c, 0x0000000c },
218 		{ 0x0070, 0x00000009 },
219 		{ 0x0074, 0x00000012 },
220 		{ 0x0078, 0x000000d0 },
221 		{ 0x007c, 0x00140005 },
222 		{ 0x0080, 0x00050004 },
223 		{ 0x0084, 0x70233005 },
224 		{ 0x0088, 0x000c0000 },
225 		{ 0x008c, 0x00000300 },
226 		{ 0x0090, 0x00000040 },
227 		{ 0x0100, 0x00000001 },
228 		{ 0x00c0, 0x00020001 },
229 		{ 0x00c8, 0x20082008 },
230 		{ 0x0380, 0x00020002 },
231 		{ 0x0390, 0x0000000f },
232 	};
233 
234 	static const struct reg_config dbsc_config5[] = {
235 		{ 0x0244, 0x00000011 },
236 		{ 0x0290, 0x00000006 },
237 		{ 0x02a0, 0x0005c000 },
238 		{ 0x0290, 0x00000003 },
239 		{ 0x02a0, 0x0300c481 },
240 		{ 0x0290, 0x00000023 },
241 		{ 0x02a0, 0x00fdb6c0 },
242 		{ 0x0290, 0x00000011 },
243 		{ 0x02a0, 0x1000040b },
244 		{ 0x0290, 0x00000012 },
245 		{ 0x02a0, 0x9d5cbb66 },
246 		{ 0x0290, 0x00000013 },
247 		{ 0x02a0, 0x1a868300 },
248 		{ 0x0290, 0x00000014 },
249 		{ 0x02a0, 0x300214d8 },
250 		{ 0x0290, 0x00000015 },
251 		{ 0x02a0, 0x00000d70 },
252 		{ 0x0290, 0x00000016 },
253 		{ 0x02a0, 0x00000006 },
254 		{ 0x0290, 0x00000017 },
255 		{ 0x02a0, 0x00000018 },
256 		{ 0x0290, 0x0000001a },
257 		{ 0x02a0, 0x910035c7 },
258 		{ 0x0290, 0x00000004 },
259 	};
260 
261 	static const struct reg_config dbsc_config6[] = {
262 		{ 0x0290, 0x00000001 },
263 		{ 0x02a0, 0x00000181 },
264 		{ 0x0018, 0x11000000 },
265 		{ 0x0290, 0x00000004 },
266 	};
267 
268 	static const struct reg_config dbsc_config7[] = {
269 		{ 0x0290, 0x00000001 },
270 		{ 0x02a0, 0x0000fe01 },
271 		{ 0x0290, 0x00000004 },
272 	};
273 
274 	static const struct reg_config dbsc_config8[] = {
275 		{ 0x0304, 0x00000000 },
276 		{ 0x00f4, 0x01004c20 },
277 		{ 0x00f8, 0x014000aa },
278 		{ 0x00e0, 0x00000140 },
279 		{ 0x00e4, 0x00081860 },
280 		{ 0x00e8, 0x00010000 },
281 		{ 0x0014, 0x00000001 },
282 		{ 0x0010, 0x00000001 },
283 		{ 0x0280, 0x00000000 },
284 	};
285 
286 	static const u32 dbsc3_0_base = DBSC3_0_BASE;
287 	unsigned int i;
288 
289 	for (i = 0; i < ARRAY_SIZE(dbsc_config1); i++)
290 		writel(dbsc_config1[i].val, dbsc3_0_base | dbsc_config1[i].off);
291 
292 	dbsc_wait(0x2a0);
293 
294 	for (i = 0; i < ARRAY_SIZE(dbsc_config4); i++)
295 		writel(dbsc_config4[i].val, dbsc3_0_base | dbsc_config4[i].off);
296 
297 	dbsc_wait(0x240);
298 
299 	for (i = 0; i < ARRAY_SIZE(dbsc_config5); i++)
300 		writel(dbsc_config5[i].val, dbsc3_0_base | dbsc_config5[i].off);
301 
302 	dbsc_wait(0x2a0);
303 
304 	for (i = 0; i < ARRAY_SIZE(dbsc_config6); i++)
305 		writel(dbsc_config6[i].val, dbsc3_0_base | dbsc_config6[i].off);
306 
307 	dbsc_wait(0x2a0);
308 
309 	for (i = 0; i < ARRAY_SIZE(dbsc_config7); i++)
310 		writel(dbsc_config7[i].val, dbsc3_0_base | dbsc_config7[i].off);
311 
312 	dbsc_wait(0x2a0);
313 
314 	for (i = 0; i < ARRAY_SIZE(dbsc_config8); i++)
315 		writel(dbsc_config8[i].val, dbsc3_0_base | dbsc_config8[i].off);
316 
317 }
318 
319 static void spl_init_qspi(void)
320 {
321 	mstp_clrbits_le32(MSTPSR9, SMSTPCR9, QSPI_MSTP917);
322 
323 	static const u32 qspi_base = 0xe6b10000;
324 
325 	writeb(0x08, qspi_base + 0x00);
326 	writeb(0x00, qspi_base + 0x01);
327 	writeb(0x06, qspi_base + 0x02);
328 	writeb(0x01, qspi_base + 0x0a);
329 	writeb(0x00, qspi_base + 0x0b);
330 	writeb(0x00, qspi_base + 0x0c);
331 	writeb(0x00, qspi_base + 0x0d);
332 	writeb(0x00, qspi_base + 0x0e);
333 
334 	writew(0xe080, qspi_base + 0x10);
335 
336 	writeb(0xc0, qspi_base + 0x18);
337 	writeb(0x00, qspi_base + 0x18);
338 	writeb(0x00, qspi_base + 0x08);
339 	writeb(0x48, qspi_base + 0x00);
340 }
341 
342 void board_init_f(ulong dummy)
343 {
344 	mstp_clrbits_le32(MSTPSR1, SMSTPCR1, TMU0_MSTP125);
345 	mstp_clrbits_le32(MSTPSR7, SMSTPCR7, SCIF0_MSTP721);
346 
347 	/*
348 	 * SD0 clock is set to 97.5MHz by default.
349 	 * Set SD2 to the 97.5MHz as well.
350 	 */
351 	writel(SD_97500KHZ, SD2CKCR);
352 
353 	spl_init_sys();
354 	spl_init_pfc();
355 	spl_init_gpio();
356 	spl_init_lbsc();
357 	spl_init_dbsc();
358 	spl_init_qspi();
359 }
360 
361 void spl_board_init(void)
362 {
363 	/* UART clocks enabled and gd valid - init serial console */
364 	preloader_console_init();
365 }
366 
367 void board_boot_order(u32 *spl_boot_list)
368 {
369 	const u32 jtag_magic = 0x1337c0de;
370 	const u32 load_magic = 0xb33fc0de;
371 
372 	/*
373 	 * If JTAG probe sets special word at 0xe6300020, then it must
374 	 * put U-Boot into RAM and SPL will start it from RAM.
375 	 */
376 	if (readl(CONFIG_SPL_TEXT_BASE + 0x20) == jtag_magic) {
377 		printf("JTAG boot detected!\n");
378 
379 		while (readl(CONFIG_SPL_TEXT_BASE + 0x24) != load_magic)
380 			;
381 
382 		spl_boot_list[0] = BOOT_DEVICE_RAM;
383 		spl_boot_list[1] = BOOT_DEVICE_NONE;
384 
385 		return;
386 	}
387 
388 	/* Boot from SPI NOR with YMODEM UART fallback. */
389 	spl_boot_list[0] = BOOT_DEVICE_SPI;
390 	spl_boot_list[1] = BOOT_DEVICE_UART;
391 	spl_boot_list[2] = BOOT_DEVICE_NONE;
392 }
393 
394 void reset_cpu(ulong addr)
395 {
396 }
397