1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2012-2016 Stephen Warren
9 #include <efi_loader.h>
10 #include <fdt_support.h>
11 #include <fdt_simplefb.h>
16 #include <asm/arch/mbox.h>
17 #include <asm/arch/msg.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_bcm283x_mu.h>
22 #include <asm/armv8/mmu.h>
25 #include <dm/pinctrl.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /* Assigned in lowlevel_init.S
30 * Push the variable into the .data section so that it
31 * does not get cleared later.
33 unsigned long __section(".data") fw_dtb_pointer;
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;
42 struct msg_get_board_rev {
43 struct bcm2835_mbox_hdr hdr;
44 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
48 struct msg_get_board_serial {
49 struct bcm2835_mbox_hdr hdr;
50 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
54 struct msg_get_mac_address {
55 struct bcm2835_mbox_hdr hdr;
56 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
60 struct msg_get_clock_rate {
61 struct bcm2835_mbox_hdr hdr;
62 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
67 #define DTB_DIR "broadcom/"
73 * https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
81 static const struct rpi_model rpi_model_unknown = {
83 DTB_DIR "bcm283x-rpi-other.dtb",
87 static const struct rpi_model rpi_models_new_scheme[] = {
90 DTB_DIR "bcm2835-rpi-a.dtb",
95 DTB_DIR "bcm2835-rpi-b.dtb",
100 DTB_DIR "bcm2835-rpi-a-plus.dtb",
105 DTB_DIR "bcm2835-rpi-b-plus.dtb",
110 DTB_DIR "bcm2836-rpi-2-b.dtb",
115 DTB_DIR "bcm2835-rpi-cm.dtb",
120 DTB_DIR "bcm2837-rpi-3-b.dtb",
125 DTB_DIR "bcm2835-rpi-zero.dtb",
130 DTB_DIR "bcm2837-rpi-cm3.dtb",
135 DTB_DIR "bcm2835-rpi-zero-w.dtb",
140 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
145 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
150 DTB_DIR "bcm2837-rpi-cm3.dtb",
155 DTB_DIR "bcm2711-rpi-4-b.dtb",
160 DTB_DIR "bcm2837-rpi-zero-2-w.dtb",
165 DTB_DIR "bcm2711-rpi-400.dtb",
170 DTB_DIR "bcm2711-rpi-cm4.dtb",
175 DTB_DIR "bcm2712-rpi-5-b.dtb",
180 static const struct rpi_model rpi_models_old_scheme[] = {
183 DTB_DIR "bcm2835-rpi-b.dtb",
188 DTB_DIR "bcm2835-rpi-b.dtb",
193 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
198 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
203 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
208 DTB_DIR "bcm2835-rpi-a.dtb",
213 DTB_DIR "bcm2835-rpi-a.dtb",
218 DTB_DIR "bcm2835-rpi-a.dtb",
223 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
228 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
233 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
238 DTB_DIR "bcm2835-rpi-b-plus.dtb",
243 DTB_DIR "bcm2835-rpi-cm.dtb",
248 DTB_DIR "bcm2835-rpi-a-plus.dtb",
253 DTB_DIR "bcm2835-rpi-b-plus.dtb",
258 DTB_DIR "bcm2835-rpi-cm.dtb",
263 DTB_DIR "bcm2835-rpi-a-plus.dtb",
268 static uint32_t revision;
269 static uint32_t rev_scheme;
270 static uint32_t rev_type;
271 static const struct rpi_model *model;
275 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
278 BCM2835_MBOX_INIT_HDR(msg);
279 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
281 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
283 printf("bcm2835: Could not query ARM memory size\n");
287 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
290 * In some configurations the memory size returned by VideoCore
291 * is not aligned to the section size, what is mandatory for
292 * the u-boot's memory setup.
294 gd->ram_size &= ~MMU_SECTION_SIZE;
299 #ifdef CONFIG_OF_BOARD
300 int dram_init_banksize(void)
304 ret = fdtdec_setup_memory_banksize();
308 return fdtdec_setup_mem_size_base();
312 static void set_fdtfile(void)
316 if (env_get("fdtfile"))
319 fdtfile = model->fdtfile;
320 env_set("fdtfile", fdtfile);
324 * If the firmware provided a valid FDT at boot time, let's expose it in
325 * ${fdt_addr} so it may be passed unmodified to the kernel.
327 static void set_fdt_addr(void)
329 if (env_get("fdt_addr"))
332 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
335 env_set_hex("fdt_addr", fw_dtb_pointer);
339 * Prevent relocation from stomping on a firmware provided FDT blob.
341 phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
343 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
345 return fw_dtb_pointer & ~0xffff;
348 static void set_usbethaddr(void)
350 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
353 if (!model->has_onboard_eth)
356 if (env_get("usbethaddr"))
359 BCM2835_MBOX_INIT_HDR(msg);
360 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
362 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
364 printf("bcm2835: Could not query MAC address\n");
365 /* Ignore error; not critical */
369 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
371 if (!env_get("ethaddr"))
372 env_set("ethaddr", env_get("usbethaddr"));
377 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
378 static void set_board_info(void)
382 snprintf(s, sizeof(s), "0x%X", revision);
383 env_set("board_revision", s);
384 snprintf(s, sizeof(s), "%u", rev_scheme);
385 env_set("board_rev_scheme", s);
386 /* Can't rename this to board_rev_type since it's an ABI for scripts */
387 snprintf(s, sizeof(s), "0x%X", rev_type);
388 env_set("board_rev", s);
389 env_set("board_name", model->name);
391 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
393 static void set_serial_number(void)
395 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
397 char serial_string[17] = { 0 };
399 if (env_get("serial#"))
402 BCM2835_MBOX_INIT_HDR(msg);
403 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
405 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
407 printf("bcm2835: Could not query board serial\n");
408 /* Ignore error; not critical */
412 snprintf(serial_string, sizeof(serial_string), "%016llx",
413 msg->get_board_serial.body.resp.serial);
414 env_set("serial#", serial_string);
417 int misc_init_r(void)
422 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
430 static void get_board_revision(void)
432 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
434 const struct rpi_model *models;
435 uint32_t models_count;
438 BCM2835_MBOX_INIT_HDR(msg);
439 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
441 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
443 /* Ignore error; not critical */
444 node = ofnode_path("/system");
445 if (!ofnode_valid(node)) {
446 printf("bcm2835: Could not find /system node\n");
450 ret = ofnode_read_u32(node, "linux,revision", &revision);
452 printf("bcm2835: Could not find linux,revision\n");
456 revision = msg->get_board_rev.body.resp.rev;
460 * For details of old-vs-new scheme, see:
461 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
462 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
465 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
466 * lower byte to use as the board rev:
467 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
468 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
470 if (revision & 0x800000) {
472 rev_type = (revision >> 4) & 0xff;
473 models = rpi_models_new_scheme;
474 models_count = ARRAY_SIZE(rpi_models_new_scheme);
477 rev_type = revision & 0xff;
478 models = rpi_models_old_scheme;
479 models_count = ARRAY_SIZE(rpi_models_old_scheme);
481 if (rev_type >= models_count) {
482 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
483 model = &rpi_model_unknown;
484 } else if (!models[rev_type].name) {
485 printf("RPI: Board rev 0x%x unknown\n", rev_type);
486 model = &rpi_model_unknown;
488 model = &models[rev_type];
491 printf("RPI %s (0x%x)\n", model->name, revision);
496 get_board_revision();
498 gd->bd->bi_boot_params = 0x100;
500 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
504 * If the firmware passed a device tree use it for U-Boot.
506 void *board_fdt_blob_setup(int *err)
509 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC) {
514 return (void *)fw_dtb_pointer;
517 int copy_property(void *dst, void *src, char *path, char *property)
519 int dst_offset, src_offset;
523 src_offset = fdt_path_offset(src, path);
524 dst_offset = fdt_path_offset(dst, path);
526 if (src_offset < 0 || dst_offset < 0)
529 prop = fdt_getprop(src, src_offset, property, &len);
533 return fdt_setprop(dst, dst_offset, property, prop, len);
536 /* Copy tweaks from the firmware dtb to the loaded dtb */
537 void update_fdt_from_fw(void *fdt, void *fw_fdt)
539 /* Using dtb from firmware directly; leave it alone */
543 /* The firmware provides a more precie model; so copy that */
544 copy_property(fdt, fw_fdt, "/", "model");
546 /* memory reserve as suggested by the firmware */
547 copy_property(fdt, fw_fdt, "/", "memreserve");
549 /* Adjust dma-ranges for the SD card and PCI bus as they can depend on
552 copy_property(fdt, fw_fdt, "emmc2bus", "dma-ranges");
553 copy_property(fdt, fw_fdt, "pcie0", "dma-ranges");
555 /* Bootloader configuration template exposes as nvmem */
556 if (copy_property(fdt, fw_fdt, "blconfig", "reg") == 0)
557 copy_property(fdt, fw_fdt, "blconfig", "status");
559 /* kernel address randomisation seed as provided by the firmware */
560 copy_property(fdt, fw_fdt, "/chosen", "kaslr-seed");
562 /* address of the PHY device as provided by the firmware */
563 copy_property(fdt, fw_fdt, "ethernet0/mdio@e14/ethernet-phy@1", "reg");
566 int ft_board_setup(void *blob, struct bd_info *bd)
570 update_fdt_from_fw(blob, (void *)fw_dtb_pointer);
572 node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
574 fdt_simplefb_add_node(blob);
576 fdt_simplefb_enable_and_mem_rsv(blob);
578 #ifdef CONFIG_EFI_LOADER
579 /* Reserve the spin table */
580 efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
581 EFI_RESERVED_MEMORY_TYPE);