xref: /openbmc/u-boot/board/raspberrypi/rpi/rpi.c (revision 730d2544)
1 /*
2  * (C) Copyright 2012-2016 Stephen Warren
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 
7 #include <common.h>
8 #include <inttypes.h>
9 #include <config.h>
10 #include <dm.h>
11 #include <fdt_support.h>
12 #include <fdt_simplefb.h>
13 #include <lcd.h>
14 #include <memalign.h>
15 #include <mmc.h>
16 #include <asm/gpio.h>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_pl01x.h>
21 #include <dm/platform_data/serial_bcm283x_mu.h>
22 #ifdef CONFIG_ARM64
23 #include <asm/armv8/mmu.h>
24 #endif
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 /* From lowlevel_init.S */
29 extern unsigned long fw_dtb_pointer;
30 
31 static const struct bcm2835_gpio_platdata gpio_platdata = {
32 	.base = BCM2835_GPIO_BASE,
33 };
34 
35 U_BOOT_DEVICE(bcm2835_gpios) = {
36 	.name = "gpio_bcm2835",
37 	.platdata = &gpio_platdata,
38 };
39 
40 #ifdef CONFIG_PL01X_SERIAL
41 static const struct pl01x_serial_platdata serial_platdata = {
42 #ifndef CONFIG_BCM2835
43 	.base = 0x3f201000,
44 #else
45 	.base = 0x20201000,
46 #endif
47 	.type = TYPE_PL011,
48 	.skip_init = true,
49 };
50 
51 U_BOOT_DEVICE(bcm2835_serials) = {
52 	.name = "serial_pl01x",
53 	.platdata = &serial_platdata,
54 };
55 #else
56 static struct bcm283x_mu_serial_platdata serial_platdata = {
57 	.base = 0x3f215040,
58 	.clock = 250000000,
59 	.skip_init = true,
60 };
61 
62 U_BOOT_DEVICE(bcm2837_serials) = {
63 	.name = "serial_bcm283x_mu",
64 	.platdata = &serial_platdata,
65 };
66 #endif
67 
68 struct msg_get_arm_mem {
69 	struct bcm2835_mbox_hdr hdr;
70 	struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
71 	u32 end_tag;
72 };
73 
74 struct msg_get_board_rev {
75 	struct bcm2835_mbox_hdr hdr;
76 	struct bcm2835_mbox_tag_get_board_rev get_board_rev;
77 	u32 end_tag;
78 };
79 
80 struct msg_get_board_serial {
81 	struct bcm2835_mbox_hdr hdr;
82 	struct bcm2835_mbox_tag_get_board_serial get_board_serial;
83 	u32 end_tag;
84 };
85 
86 struct msg_get_mac_address {
87 	struct bcm2835_mbox_hdr hdr;
88 	struct bcm2835_mbox_tag_get_mac_address get_mac_address;
89 	u32 end_tag;
90 };
91 
92 struct msg_set_power_state {
93 	struct bcm2835_mbox_hdr hdr;
94 	struct bcm2835_mbox_tag_set_power_state set_power_state;
95 	u32 end_tag;
96 };
97 
98 struct msg_get_clock_rate {
99 	struct bcm2835_mbox_hdr hdr;
100 	struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
101 	u32 end_tag;
102 };
103 
104 /*
105  * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
106  * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
107  * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
108  *
109  * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
110  * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
111  * Foundation stated that the following source was accurate:
112  * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
113  */
114 struct rpi_model {
115 	const char *name;
116 	const char *fdtfile;
117 	bool has_onboard_eth;
118 };
119 
120 static const struct rpi_model rpi_model_unknown = {
121 	"Unknown model",
122 	"bcm283x-rpi-other.dtb",
123 	false,
124 };
125 
126 static const struct rpi_model rpi_models_new_scheme[] = {
127 	[0x4] = {
128 		"2 Model B",
129 		"bcm2836-rpi-2-b.dtb",
130 		true,
131 	},
132 	[0x8] = {
133 		"3 Model B",
134 		"bcm2837-rpi-3-b.dtb",
135 		true,
136 	},
137 	[0x9] = {
138 		"Zero",
139 		"bcm2835-rpi-zero.dtb",
140 		false,
141 	},
142 };
143 
144 static const struct rpi_model rpi_models_old_scheme[] = {
145 	[0x2] = {
146 		"Model B",
147 		"bcm2835-rpi-b.dtb",
148 		true,
149 	},
150 	[0x3] = {
151 		"Model B",
152 		"bcm2835-rpi-b.dtb",
153 		true,
154 	},
155 	[0x4] = {
156 		"Model B rev2",
157 		"bcm2835-rpi-b-rev2.dtb",
158 		true,
159 	},
160 	[0x5] = {
161 		"Model B rev2",
162 		"bcm2835-rpi-b-rev2.dtb",
163 		true,
164 	},
165 	[0x6] = {
166 		"Model B rev2",
167 		"bcm2835-rpi-b-rev2.dtb",
168 		true,
169 	},
170 	[0x7] = {
171 		"Model A",
172 		"bcm2835-rpi-a.dtb",
173 		false,
174 	},
175 	[0x8] = {
176 		"Model A",
177 		"bcm2835-rpi-a.dtb",
178 		false,
179 	},
180 	[0x9] = {
181 		"Model A",
182 		"bcm2835-rpi-a.dtb",
183 		false,
184 	},
185 	[0xd] = {
186 		"Model B rev2",
187 		"bcm2835-rpi-b-rev2.dtb",
188 		true,
189 	},
190 	[0xe] = {
191 		"Model B rev2",
192 		"bcm2835-rpi-b-rev2.dtb",
193 		true,
194 	},
195 	[0xf] = {
196 		"Model B rev2",
197 		"bcm2835-rpi-b-rev2.dtb",
198 		true,
199 	},
200 	[0x10] = {
201 		"Model B+",
202 		"bcm2835-rpi-b-plus.dtb",
203 		true,
204 	},
205 	[0x11] = {
206 		"Compute Module",
207 		"bcm2835-rpi-cm.dtb",
208 		false,
209 	},
210 	[0x12] = {
211 		"Model A+",
212 		"bcm2835-rpi-a-plus.dtb",
213 		false,
214 	},
215 	[0x13] = {
216 		"Model B+",
217 		"bcm2835-rpi-b-plus.dtb",
218 		true,
219 	},
220 	[0x14] = {
221 		"Compute Module",
222 		"bcm2835-rpi-cm.dtb",
223 		false,
224 	},
225 	[0x15] = {
226 		"Model A+",
227 		"bcm2835-rpi-a-plus.dtb",
228 		false,
229 	},
230 };
231 
232 static uint32_t revision;
233 static uint32_t rev_scheme;
234 static uint32_t rev_type;
235 static const struct rpi_model *model;
236 
237 #ifdef CONFIG_ARM64
238 static struct mm_region bcm2837_mem_map[] = {
239 	{
240 		.virt = 0x00000000UL,
241 		.phys = 0x00000000UL,
242 		.size = 0x3f000000UL,
243 		.attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
244 			 PTE_BLOCK_INNER_SHARE
245 	}, {
246 		.virt = 0x3f000000UL,
247 		.phys = 0x3f000000UL,
248 		.size = 0x01000000UL,
249 		.attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
250 			 PTE_BLOCK_NON_SHARE |
251 			 PTE_BLOCK_PXN | PTE_BLOCK_UXN
252 	}, {
253 		/* List terminator */
254 		0,
255 	}
256 };
257 
258 struct mm_region *mem_map = bcm2837_mem_map;
259 #endif
260 
261 int dram_init(void)
262 {
263 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
264 	int ret;
265 
266 	BCM2835_MBOX_INIT_HDR(msg);
267 	BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
268 
269 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
270 	if (ret) {
271 		printf("bcm2835: Could not query ARM memory size\n");
272 		return -1;
273 	}
274 
275 	gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
276 
277 	return 0;
278 }
279 
280 static void set_fdtfile(void)
281 {
282 	const char *fdtfile;
283 
284 	if (getenv("fdtfile"))
285 		return;
286 
287 	fdtfile = model->fdtfile;
288 	setenv("fdtfile", fdtfile);
289 }
290 
291 /*
292  * If the firmware provided a valid FDT at boot time, let's expose it in
293  * ${fdt_addr} so it may be passed unmodified to the kernel.
294  */
295 static void set_fdt_addr(void)
296 {
297 	if (getenv("fdt_addr"))
298 		return;
299 
300 	if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
301 		return;
302 
303 	setenv_hex("fdt_addr", fw_dtb_pointer);
304 }
305 
306 /*
307  * Prevent relocation from stomping on a firmware provided FDT blob.
308  */
309 unsigned long board_get_usable_ram_top(unsigned long total_size)
310 {
311 	if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
312 		return gd->ram_top;
313 	return fw_dtb_pointer & ~0xffff;
314 }
315 
316 static void set_usbethaddr(void)
317 {
318 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
319 	int ret;
320 
321 	if (!model->has_onboard_eth)
322 		return;
323 
324 	if (getenv("usbethaddr"))
325 		return;
326 
327 	BCM2835_MBOX_INIT_HDR(msg);
328 	BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
329 
330 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
331 	if (ret) {
332 		printf("bcm2835: Could not query MAC address\n");
333 		/* Ignore error; not critical */
334 		return;
335 	}
336 
337 	eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
338 
339 	if (!getenv("ethaddr"))
340 		setenv("ethaddr", getenv("usbethaddr"));
341 
342 	return;
343 }
344 
345 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
346 static void set_board_info(void)
347 {
348 	char s[11];
349 
350 	snprintf(s, sizeof(s), "0x%X", revision);
351 	setenv("board_revision", s);
352 	snprintf(s, sizeof(s), "%d", rev_scheme);
353 	setenv("board_rev_scheme", s);
354 	/* Can't rename this to board_rev_type since it's an ABI for scripts */
355 	snprintf(s, sizeof(s), "0x%X", rev_type);
356 	setenv("board_rev", s);
357 	setenv("board_name", model->name);
358 }
359 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
360 
361 static void set_serial_number(void)
362 {
363 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
364 	int ret;
365 	char serial_string[17] = { 0 };
366 
367 	if (getenv("serial#"))
368 		return;
369 
370 	BCM2835_MBOX_INIT_HDR(msg);
371 	BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
372 
373 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
374 	if (ret) {
375 		printf("bcm2835: Could not query board serial\n");
376 		/* Ignore error; not critical */
377 		return;
378 	}
379 
380 	snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
381 		 msg->get_board_serial.body.resp.serial);
382 	setenv("serial#", serial_string);
383 }
384 
385 int misc_init_r(void)
386 {
387 	set_fdt_addr();
388 	set_fdtfile();
389 	set_usbethaddr();
390 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
391 	set_board_info();
392 #endif
393 	set_serial_number();
394 
395 	return 0;
396 }
397 
398 static int power_on_module(u32 module)
399 {
400 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
401 	int ret;
402 
403 	BCM2835_MBOX_INIT_HDR(msg_pwr);
404 	BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
405 			      SET_POWER_STATE);
406 	msg_pwr->set_power_state.body.req.device_id = module;
407 	msg_pwr->set_power_state.body.req.state =
408 		BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
409 		BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
410 
411 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
412 				     &msg_pwr->hdr);
413 	if (ret) {
414 		printf("bcm2835: Could not set module %u power state\n",
415 		       module);
416 		return -1;
417 	}
418 
419 	return 0;
420 }
421 
422 static void get_board_rev(void)
423 {
424 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
425 	int ret;
426 	const struct rpi_model *models;
427 	uint32_t models_count;
428 
429 	BCM2835_MBOX_INIT_HDR(msg);
430 	BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
431 
432 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
433 	if (ret) {
434 		printf("bcm2835: Could not query board revision\n");
435 		/* Ignore error; not critical */
436 		return;
437 	}
438 
439 	/*
440 	 * For details of old-vs-new scheme, see:
441 	 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
442 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
443 	 * (a few posts down)
444 	 *
445 	 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
446 	 * lower byte to use as the board rev:
447 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
448 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
449 	 */
450 	revision = msg->get_board_rev.body.resp.rev;
451 	if (revision & 0x800000) {
452 		rev_scheme = 1;
453 		rev_type = (revision >> 4) & 0xff;
454 		models = rpi_models_new_scheme;
455 		models_count = ARRAY_SIZE(rpi_models_new_scheme);
456 	} else {
457 		rev_scheme = 0;
458 		rev_type = revision & 0xff;
459 		models = rpi_models_old_scheme;
460 		models_count = ARRAY_SIZE(rpi_models_old_scheme);
461 	}
462 	if (rev_type >= models_count) {
463 		printf("RPI: Board rev 0x%x outside known range\n", rev_type);
464 		model = &rpi_model_unknown;
465 	} else if (!models[rev_type].name) {
466 		printf("RPI: Board rev 0x%x unknown\n", rev_type);
467 		model = &rpi_model_unknown;
468 	} else {
469 		model = &models[rev_type];
470 	}
471 
472 	printf("RPI %s (0x%x)\n", model->name, revision);
473 }
474 
475 int board_init(void)
476 {
477 	get_board_rev();
478 
479 	gd->bd->bi_boot_params = 0x100;
480 
481 	return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
482 }
483 
484 #ifndef CONFIG_PL01X_SERIAL
485 static bool rpi_is_serial_active(void)
486 {
487 	int serial_gpio = 15;
488 	struct udevice *dev;
489 
490 	/*
491 	 * The RPi3 disables the mini uart by default. The easiest way to find
492 	 * out whether it is available is to check if the RX pin is muxed.
493 	 */
494 
495 	if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
496 		return true;
497 
498 	if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
499 		return false;
500 
501 	return true;
502 }
503 #endif
504 
505 int board_early_init_f(void)
506 {
507 #ifndef CONFIG_PL01X_SERIAL
508 	/* Disable mini-UART I/O if it's not pinmuxed to our pins */
509 	if (!rpi_is_serial_active())
510 		serial_platdata.disabled = true;
511 #endif
512 
513 	return 0;
514 }
515 
516 int board_mmc_init(bd_t *bis)
517 {
518 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
519 	int ret;
520 
521 	power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
522 
523 	BCM2835_MBOX_INIT_HDR(msg_clk);
524 	BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
525 	msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
526 
527 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
528 	if (ret) {
529 		printf("bcm2835: Could not query eMMC clock rate\n");
530 		return -1;
531 	}
532 
533 	return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
534 				  msg_clk->get_clock_rate.body.resp.rate_hz);
535 }
536 
537 int ft_board_setup(void *blob, bd_t *bd)
538 {
539 	/*
540 	 * For now, we simply always add the simplefb DT node. Later, we
541 	 * should be more intelligent, and e.g. only do this if no enabled DT
542 	 * node exists for the "real" graphics driver.
543 	 */
544 	lcd_dt_simplefb_add_node(blob);
545 
546 	return 0;
547 }
548