1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2012-2016 Stephen Warren
10 #include <efi_loader.h>
11 #include <fdt_support.h>
12 #include <fdt_simplefb.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>
25 #include <asm/armv8/mmu.h>
28 #include <dm/pinctrl.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 /* Assigned in lowlevel_init.S
33 * Push the variable into the .data section so that it
34 * does not get cleared later.
36 unsigned long __section(".data") fw_dtb_pointer;
38 /* TODO(sjg@chromium.org): Move these to the msg.c file */
39 struct msg_get_arm_mem {
40 struct bcm2835_mbox_hdr hdr;
41 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
45 struct msg_get_board_rev {
46 struct bcm2835_mbox_hdr hdr;
47 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
51 struct msg_get_board_serial {
52 struct bcm2835_mbox_hdr hdr;
53 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
57 struct msg_get_mac_address {
58 struct bcm2835_mbox_hdr hdr;
59 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
63 struct msg_get_clock_rate {
64 struct bcm2835_mbox_hdr hdr;
65 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
70 struct bcm2835_mbox_hdr hdr;
71 struct bcm2835_mbox_tag_get_rsts get_rsts;
76 #ifdef BCM2835_MBOX_DUMP_MESSAGES
77 #define BCM2835_MBOX_DUMP_MSG(msg) { \
80 printf("%s:%d", __func__, __LINE__); \
81 if (msg->hdr.code == 0) \
83 else if (msg->hdr.code == 0x80000000) \
85 else if (msg->hdr.code == 0x80000000) \
89 for (i = 0; i < sizeof(*(msg)) / sizeof(*p); i++) \
90 printf(" 0x%08X", p[i]); \
94 #define BCM2835_MBOX_DUMP_MSG(msg)
98 #define DTB_DIR "broadcom/"
104 * https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
109 bool has_onboard_eth;
112 static const struct rpi_model rpi_model_unknown = {
114 DTB_DIR "bcm283x-rpi-other.dtb",
118 static const struct rpi_model rpi_models_new_scheme[] = {
121 DTB_DIR "bcm2835-rpi-a.dtb",
126 DTB_DIR "bcm2835-rpi-b.dtb",
131 DTB_DIR "bcm2835-rpi-a-plus.dtb",
136 DTB_DIR "bcm2835-rpi-b-plus.dtb",
141 DTB_DIR "bcm2836-rpi-2-b.dtb",
146 DTB_DIR "bcm2835-rpi-cm.dtb",
151 DTB_DIR "bcm2837-rpi-3-b.dtb",
156 DTB_DIR "bcm2835-rpi-zero.dtb",
161 DTB_DIR "bcm2837-rpi-cm3.dtb",
166 DTB_DIR "bcm2835-rpi-zero-w.dtb",
171 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
176 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
181 DTB_DIR "bcm2837-rpi-cm3.dtb",
186 DTB_DIR "bcm2711-rpi-4-b.dtb",
191 DTB_DIR "bcm2711-rpi-400.dtb",
196 static const struct rpi_model rpi_models_old_scheme[] = {
199 DTB_DIR "bcm2835-rpi-b.dtb",
204 DTB_DIR "bcm2835-rpi-b.dtb",
209 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
214 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
219 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
224 DTB_DIR "bcm2835-rpi-a.dtb",
229 DTB_DIR "bcm2835-rpi-a.dtb",
234 DTB_DIR "bcm2835-rpi-a.dtb",
239 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
244 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
249 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
254 DTB_DIR "bcm2835-rpi-b-plus.dtb",
259 DTB_DIR "bcm2835-rpi-cm.dtb",
264 DTB_DIR "bcm2835-rpi-a-plus.dtb",
269 DTB_DIR "bcm2835-rpi-b-plus.dtb",
274 DTB_DIR "bcm2835-rpi-cm.dtb",
279 DTB_DIR "bcm2835-rpi-a-plus.dtb",
284 static uint32_t revision;
285 static uint32_t rev_scheme;
286 static uint32_t rev_type;
287 static const struct rpi_model *model;
291 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
294 BCM2835_MBOX_INIT_HDR(msg);
295 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
297 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
299 printf("bcm2835: Could not query ARM memory size\n");
303 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
306 * In some configurations the memory size returned by VideoCore
307 * is not aligned to the section size, what is mandatory for
308 * the u-boot's memory setup.
310 gd->ram_size &= ~MMU_SECTION_SIZE;
315 #ifdef CONFIG_OF_BOARD
316 int dram_init_banksize(void)
320 ret = fdtdec_setup_memory_banksize();
324 return fdtdec_setup_mem_size_base();
328 static void set_fdtfile(void)
332 if (env_get("fdtfile"))
335 fdtfile = model->fdtfile;
336 env_set("fdtfile", fdtfile);
340 * If the firmware provided a valid FDT at boot time, let's expose it in
341 * ${fdt_addr} so it may be passed unmodified to the kernel.
343 static void set_fdt_addr(void)
345 if (env_get("fdt_addr"))
348 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
351 env_set_hex("fdt_addr", fw_dtb_pointer);
355 * Prevent relocation from stomping on a firmware provided FDT blob.
357 unsigned long board_get_usable_ram_top(unsigned long total_size)
359 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
361 return fw_dtb_pointer & ~0xffff;
364 static void get_rsts(void)
366 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_rsts, msg, 1);
368 char rsts_string[11] = { 0 };
370 BCM2835_MBOX_INIT_HDR(msg);
371 BCM2835_MBOX_INIT_TAG(&msg->get_rsts, GET_RSTS);
373 BCM2835_MBOX_DUMP_MSG(msg);
375 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
377 printf("bcm2835: Could not query RSTS value\n");
378 /* Ignore the error, let's carry on. */
382 BCM2835_MBOX_DUMP_MSG(msg);
384 snprintf(rsts_string, sizeof(rsts_string), "0x%08X",
385 msg->get_rsts.body.resp.rsts);
386 env_set("reg_rsts", rsts_string);
391 static void set_usbethaddr(void)
393 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
396 if (!model->has_onboard_eth)
399 if (env_get("usbethaddr"))
402 BCM2835_MBOX_INIT_HDR(msg);
403 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
405 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
407 printf("bcm2835: Could not query MAC address\n");
408 /* Ignore error; not critical */
412 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
414 if (!env_get("ethaddr"))
415 env_set("ethaddr", env_get("usbethaddr"));
420 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
421 static void set_board_info(void)
425 snprintf(s, sizeof(s), "0x%X", revision);
426 env_set("board_revision", s);
427 snprintf(s, sizeof(s), "%d", rev_scheme);
428 env_set("board_rev_scheme", s);
429 /* Can't rename this to board_rev_type since it's an ABI for scripts */
430 snprintf(s, sizeof(s), "0x%X", rev_type);
431 env_set("board_rev", s);
432 env_set("board_name", model->name);
434 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
436 static void set_serial_number(void)
438 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
440 char serial_string[17] = { 0 };
442 if (env_get("serial#"))
445 BCM2835_MBOX_INIT_HDR(msg);
446 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
448 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
450 printf("bcm2835: Could not query board serial\n");
451 /* Ignore error; not critical */
455 snprintf(serial_string, sizeof(serial_string), "%016llx",
456 msg->get_board_serial.body.resp.serial);
457 env_set("serial#", serial_string);
460 #if defined(CONFIG_PCI) && defined(CONFIG_BOARD_EARLY_INIT_R)
461 static char *boot_interface;
462 int board_early_init_r(void)
465 * Currently there is not way to detect which device (SD card or
466 * USB Mass Storage) has been used by VideoCore to load the uboot,
467 * so just try MMC0 first, if not then use USB.
470 boot_interface = (blk_get_dev("mmc", 0)) ? "mmc" : "usb";
478 char *env_fat_get_interface(void)
480 return boot_interface;
484 int misc_init_r(void)
490 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
498 static void get_board_rev(void)
500 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
502 const struct rpi_model *models;
503 uint32_t models_count;
505 BCM2835_MBOX_INIT_HDR(msg);
506 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
508 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
510 printf("bcm2835: Could not query board revision\n");
511 /* Ignore error; not critical */
516 * For details of old-vs-new scheme, see:
517 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
518 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
521 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
522 * lower byte to use as the board rev:
523 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
524 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
526 revision = msg->get_board_rev.body.resp.rev;
527 if (revision & 0x800000) {
529 rev_type = (revision >> 4) & 0xff;
530 models = rpi_models_new_scheme;
531 models_count = ARRAY_SIZE(rpi_models_new_scheme);
534 rev_type = revision & 0xff;
535 models = rpi_models_old_scheme;
536 models_count = ARRAY_SIZE(rpi_models_old_scheme);
538 if (rev_type >= models_count) {
539 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
540 model = &rpi_model_unknown;
541 } else if (!models[rev_type].name) {
542 printf("RPI: Board rev 0x%x unknown\n", rev_type);
543 model = &rpi_model_unknown;
545 model = &models[rev_type];
548 printf("RPI %s (0x%x)\n", model->name, revision);
550 printf("CPU: %s\n", CONFIG_SYS_CPU);
555 #ifdef CONFIG_HW_WATCHDOG
561 gd->bd->bi_boot_params = 0x100;
563 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
567 * If the firmware passed a device tree use it for U-Boot.
569 void *board_fdt_blob_setup(void)
571 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
573 return (void *)fw_dtb_pointer;
576 int ft_board_setup(void *blob, struct bd_info *bd)
579 * For now, we simply always add the simplefb DT node. Later, we
580 * should be more intelligent, and e.g. only do this if no enabled DT
581 * node exists for the "real" graphics driver.
583 lcd_dt_simplefb_add_node(blob);
585 #ifdef CONFIG_EFI_LOADER
586 /* Reserve the spin table */
587 efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
588 EFI_RESERVED_MEMORY_TYPE);
594 void xhci_pci_fixup(struct udevice *dev)
596 static int done = false;
599 bcm2711_notify_vl805_reset();