2 * (C) Copyright 2012-2016 Stephen Warren
4 * SPDX-License-Identifier: GPL-2.0
11 #include <efi_loader.h>
12 #include <fdt_support.h>
13 #include <fdt_simplefb.h>
18 #include <asm/arch/mbox.h>
19 #include <asm/arch/msg.h>
20 #include <asm/arch/sdhci.h>
21 #include <asm/global_data.h>
22 #include <dm/platform_data/serial_bcm283x_mu.h>
24 #include <asm/armv8/mmu.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /* From lowlevel_init.S */
30 extern unsigned long fw_dtb_pointer;
33 struct msg_get_arm_mem {
34 struct bcm2835_mbox_hdr hdr;
35 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
39 struct msg_get_board_rev {
40 struct bcm2835_mbox_hdr hdr;
41 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
45 struct msg_get_board_serial {
46 struct bcm2835_mbox_hdr hdr;
47 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
51 struct msg_get_mac_address {
52 struct bcm2835_mbox_hdr hdr;
53 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
57 struct msg_get_clock_rate {
58 struct bcm2835_mbox_hdr hdr;
59 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
64 #define DTB_DIR "broadcom/"
70 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
71 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
72 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
74 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
75 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
76 * Foundation stated that the following source was accurate:
77 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
85 static const struct rpi_model rpi_model_unknown = {
87 DTB_DIR "bcm283x-rpi-other.dtb",
91 static const struct rpi_model rpi_models_new_scheme[] = {
94 DTB_DIR "bcm2836-rpi-2-b.dtb",
99 DTB_DIR "bcm2837-rpi-3-b.dtb",
104 DTB_DIR "bcm2835-rpi-zero.dtb",
109 static const struct rpi_model rpi_models_old_scheme[] = {
112 DTB_DIR "bcm2835-rpi-b.dtb",
117 DTB_DIR "bcm2835-rpi-b.dtb",
122 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
127 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
132 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
137 DTB_DIR "bcm2835-rpi-a.dtb",
142 DTB_DIR "bcm2835-rpi-a.dtb",
147 DTB_DIR "bcm2835-rpi-a.dtb",
152 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
157 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
162 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
167 DTB_DIR "bcm2835-rpi-b-plus.dtb",
172 DTB_DIR "bcm2835-rpi-cm.dtb",
177 DTB_DIR "bcm2835-rpi-a-plus.dtb",
182 DTB_DIR "bcm2835-rpi-b-plus.dtb",
187 DTB_DIR "bcm2835-rpi-cm.dtb",
192 DTB_DIR "bcm2835-rpi-a-plus.dtb",
197 static uint32_t revision;
198 static uint32_t rev_scheme;
199 static uint32_t rev_type;
200 static const struct rpi_model *model;
203 static struct mm_region bcm2837_mem_map[] = {
205 .virt = 0x00000000UL,
206 .phys = 0x00000000UL,
207 .size = 0x3f000000UL,
208 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
209 PTE_BLOCK_INNER_SHARE
211 .virt = 0x3f000000UL,
212 .phys = 0x3f000000UL,
213 .size = 0x01000000UL,
214 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
215 PTE_BLOCK_NON_SHARE |
216 PTE_BLOCK_PXN | PTE_BLOCK_UXN
218 /* List terminator */
223 struct mm_region *mem_map = bcm2837_mem_map;
228 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
231 BCM2835_MBOX_INIT_HDR(msg);
232 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
234 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
236 printf("bcm2835: Could not query ARM memory size\n");
240 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
245 static void set_fdtfile(void)
249 if (getenv("fdtfile"))
252 fdtfile = model->fdtfile;
253 setenv("fdtfile", fdtfile);
257 * If the firmware provided a valid FDT at boot time, let's expose it in
258 * ${fdt_addr} so it may be passed unmodified to the kernel.
260 static void set_fdt_addr(void)
262 if (getenv("fdt_addr"))
265 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
268 setenv_hex("fdt_addr", fw_dtb_pointer);
272 * Prevent relocation from stomping on a firmware provided FDT blob.
274 unsigned long board_get_usable_ram_top(unsigned long total_size)
276 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
278 return fw_dtb_pointer & ~0xffff;
281 static void set_usbethaddr(void)
283 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
286 if (!model->has_onboard_eth)
289 if (getenv("usbethaddr"))
292 BCM2835_MBOX_INIT_HDR(msg);
293 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
295 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
297 printf("bcm2835: Could not query MAC address\n");
298 /* Ignore error; not critical */
302 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
304 if (!getenv("ethaddr"))
305 setenv("ethaddr", getenv("usbethaddr"));
310 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
311 static void set_board_info(void)
315 snprintf(s, sizeof(s), "0x%X", revision);
316 setenv("board_revision", s);
317 snprintf(s, sizeof(s), "%d", rev_scheme);
318 setenv("board_rev_scheme", s);
319 /* Can't rename this to board_rev_type since it's an ABI for scripts */
320 snprintf(s, sizeof(s), "0x%X", rev_type);
321 setenv("board_rev", s);
322 setenv("board_name", model->name);
324 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
326 static void set_serial_number(void)
328 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
330 char serial_string[17] = { 0 };
332 if (getenv("serial#"))
335 BCM2835_MBOX_INIT_HDR(msg);
336 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
338 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
340 printf("bcm2835: Could not query board serial\n");
341 /* Ignore error; not critical */
345 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
346 msg->get_board_serial.body.resp.serial);
347 setenv("serial#", serial_string);
350 int misc_init_r(void)
355 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
363 static void get_board_rev(void)
365 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
367 const struct rpi_model *models;
368 uint32_t models_count;
370 BCM2835_MBOX_INIT_HDR(msg);
371 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
373 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
375 printf("bcm2835: Could not query board revision\n");
376 /* Ignore error; not critical */
381 * For details of old-vs-new scheme, see:
382 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
383 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
386 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
387 * lower byte to use as the board rev:
388 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
389 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
391 revision = msg->get_board_rev.body.resp.rev;
392 if (revision & 0x800000) {
394 rev_type = (revision >> 4) & 0xff;
395 models = rpi_models_new_scheme;
396 models_count = ARRAY_SIZE(rpi_models_new_scheme);
399 rev_type = revision & 0xff;
400 models = rpi_models_old_scheme;
401 models_count = ARRAY_SIZE(rpi_models_old_scheme);
403 if (rev_type >= models_count) {
404 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
405 model = &rpi_model_unknown;
406 } else if (!models[rev_type].name) {
407 printf("RPI: Board rev 0x%x unknown\n", rev_type);
408 model = &rpi_model_unknown;
410 model = &models[rev_type];
413 printf("RPI %s (0x%x)\n", model->name, revision);
416 #ifndef CONFIG_PL01X_SERIAL
417 static bool rpi_is_serial_active(void)
419 int serial_gpio = 15;
423 * The RPi3 disables the mini uart by default. The easiest way to find
424 * out whether it is available is to check if the RX pin is muxed.
427 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
430 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
436 /* Disable mini-UART I/O if it's not pinmuxed to our pins.
437 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
439 static void rpi_disable_inactive_uart(void)
442 struct bcm283x_mu_serial_platdata *plat;
444 if (uclass_get_device_by_driver(UCLASS_SERIAL,
445 DM_GET_DRIVER(serial_bcm283x_mu),
449 if (!rpi_is_serial_active()) {
450 plat = dev_get_platdata(dev);
451 plat->disabled = true;
458 #ifndef CONFIG_PL01X_SERIAL
459 rpi_disable_inactive_uart();
464 gd->bd->bi_boot_params = 0x100;
466 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
469 int ft_board_setup(void *blob, bd_t *bd)
472 * For now, we simply always add the simplefb DT node. Later, we
473 * should be more intelligent, and e.g. only do this if no enabled DT
474 * node exists for the "real" graphics driver.
476 lcd_dt_simplefb_add_node(blob);
478 #ifdef CONFIG_EFI_LOADER
479 /* Reserve the spin table */
480 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);