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>
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>
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 DTB_DIR "bcm2711-rpi-cm4.dtb",
201 static const struct rpi_model rpi_models_old_scheme[] = {
204 DTB_DIR "bcm2835-rpi-b.dtb",
209 DTB_DIR "bcm2835-rpi-b.dtb",
214 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
219 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
224 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
229 DTB_DIR "bcm2835-rpi-a.dtb",
234 DTB_DIR "bcm2835-rpi-a.dtb",
239 DTB_DIR "bcm2835-rpi-a.dtb",
244 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
249 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
254 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
259 DTB_DIR "bcm2835-rpi-b-plus.dtb",
264 DTB_DIR "bcm2835-rpi-cm.dtb",
269 DTB_DIR "bcm2835-rpi-a-plus.dtb",
274 DTB_DIR "bcm2835-rpi-b-plus.dtb",
279 DTB_DIR "bcm2835-rpi-cm.dtb",
284 DTB_DIR "bcm2835-rpi-a-plus.dtb",
289 static uint32_t revision;
290 static uint32_t rev_scheme;
291 static uint32_t rev_type;
292 static const struct rpi_model *model;
296 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
299 BCM2835_MBOX_INIT_HDR(msg);
300 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
302 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
304 printf("bcm2835: Could not query ARM memory size\n");
308 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
311 * In some configurations the memory size returned by VideoCore
312 * is not aligned to the section size, what is mandatory for
313 * the u-boot's memory setup.
315 gd->ram_size &= ~MMU_SECTION_SIZE;
320 #ifdef CONFIG_OF_BOARD
321 int dram_init_banksize(void)
325 ret = fdtdec_setup_memory_banksize();
329 return fdtdec_setup_mem_size_base();
333 static void set_fdtfile(void)
337 if (env_get("fdtfile"))
340 fdtfile = model->fdtfile;
341 env_set("fdtfile", fdtfile);
345 * If the firmware provided a valid FDT at boot time, let's expose it in
346 * ${fdt_addr} so it may be passed unmodified to the kernel.
348 static void set_fdt_addr(void)
350 if (env_get("fdt_addr"))
353 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
356 env_set_hex("fdt_addr", fw_dtb_pointer);
360 * Prevent relocation from stomping on a firmware provided FDT blob.
362 unsigned long board_get_usable_ram_top(unsigned long total_size)
364 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
366 return fw_dtb_pointer & ~0xffff;
369 static void get_rsts(void)
371 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_rsts, msg, 1);
373 char rsts_string[11] = { 0 };
375 BCM2835_MBOX_INIT_HDR(msg);
376 BCM2835_MBOX_INIT_TAG(&msg->get_rsts, GET_RSTS);
378 BCM2835_MBOX_DUMP_MSG(msg);
380 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
382 printf("bcm2835: Could not query RSTS value\n");
383 /* Ignore the error, let's carry on. */
387 BCM2835_MBOX_DUMP_MSG(msg);
389 snprintf(rsts_string, sizeof(rsts_string), "0x%08X",
390 msg->get_rsts.body.resp.rsts);
391 env_set("reg_rsts", rsts_string);
396 static void set_usbethaddr(void)
398 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
401 if (!model->has_onboard_eth)
404 if (env_get("usbethaddr"))
407 BCM2835_MBOX_INIT_HDR(msg);
408 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
410 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
412 printf("bcm2835: Could not query MAC address\n");
413 /* Ignore error; not critical */
417 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
419 if (!env_get("ethaddr"))
420 env_set("ethaddr", env_get("usbethaddr"));
425 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
426 static void set_board_info(void)
430 snprintf(s, sizeof(s), "0x%X", revision);
431 env_set("board_revision", s);
432 snprintf(s, sizeof(s), "%d", rev_scheme);
433 env_set("board_rev_scheme", s);
434 /* Can't rename this to board_rev_type since it's an ABI for scripts */
435 snprintf(s, sizeof(s), "0x%X", rev_type);
436 env_set("board_rev", s);
437 env_set("board_name", model->name);
439 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
441 static void set_serial_number(void)
443 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
445 char serial_string[17] = { 0 };
447 if (env_get("serial#"))
450 BCM2835_MBOX_INIT_HDR(msg);
451 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
453 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
455 printf("bcm2835: Could not query board serial\n");
456 /* Ignore error; not critical */
460 snprintf(serial_string, sizeof(serial_string), "%016llx",
461 msg->get_board_serial.body.resp.serial);
462 env_set("serial#", serial_string);
465 #if defined(CONFIG_PCI) && defined(CONFIG_BOARD_EARLY_INIT_R)
466 static char *boot_interface;
467 int board_early_init_r(void)
470 * Currently there is not way to detect which device (SD card or
471 * USB Mass Storage) has been used by VideoCore to load the uboot,
472 * so just try MMC0 first, if not then use USB.
475 boot_interface = (blk_get_dev("mmc", 0)) ? "mmc" : "usb";
483 char *env_fat_get_interface(void)
485 return boot_interface;
489 int misc_init_r(void)
495 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
503 static void get_board_rev(void)
505 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
507 const struct rpi_model *models;
508 uint32_t models_count;
510 BCM2835_MBOX_INIT_HDR(msg);
511 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
513 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
515 printf("bcm2835: Could not query board revision\n");
516 /* Ignore error; not critical */
521 * For details of old-vs-new scheme, see:
522 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
523 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
526 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
527 * lower byte to use as the board rev:
528 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
529 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
531 revision = msg->get_board_rev.body.resp.rev;
532 if (revision & 0x800000) {
534 rev_type = (revision >> 4) & 0xff;
535 models = rpi_models_new_scheme;
536 models_count = ARRAY_SIZE(rpi_models_new_scheme);
539 rev_type = revision & 0xff;
540 models = rpi_models_old_scheme;
541 models_count = ARRAY_SIZE(rpi_models_old_scheme);
543 if (rev_type >= models_count) {
544 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
545 model = &rpi_model_unknown;
546 } else if (!models[rev_type].name) {
547 printf("RPI: Board rev 0x%x unknown\n", rev_type);
548 model = &rpi_model_unknown;
550 model = &models[rev_type];
553 printf("RPI %s (0x%x)\n", model->name, revision);
555 printf("CPU: %s\n", CONFIG_SYS_CPU);
560 #ifdef CONFIG_HW_WATCHDOG
566 gd->bd->bi_boot_params = 0x100;
568 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
572 * If the firmware passed a device tree use it for U-Boot.
574 void *board_fdt_blob_setup(void)
576 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
578 return (void *)fw_dtb_pointer;
581 int ft_board_setup(void *blob, struct bd_info *bd)
585 node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
587 lcd_dt_simplefb_add_node(blob);
589 #ifdef CONFIG_EFI_LOADER
590 /* Reserve the spin table */
591 efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
592 EFI_RESERVED_MEMORY_TYPE);