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