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>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/msg.h>
19 #include <asm/arch/sdhci.h>
20 #include <asm/global_data.h>
21 #include <dm/platform_data/serial_bcm283x_mu.h>
23 #include <asm/armv8/mmu.h>
26 #include <dm/pinctrl.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 /* Assigned in lowlevel_init.S
31 * Push the variable into the .data section so that it
32 * does not get cleared later.
34 unsigned long __section(".data") fw_dtb_pointer;
36 /* TODO(sjg@chromium.org): Move these to the msg.c file */
37 struct msg_get_arm_mem {
38 struct bcm2835_mbox_hdr hdr;
39 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
43 struct msg_get_board_rev {
44 struct bcm2835_mbox_hdr hdr;
45 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
49 struct msg_get_board_serial {
50 struct bcm2835_mbox_hdr hdr;
51 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
55 struct msg_get_mac_address {
56 struct bcm2835_mbox_hdr hdr;
57 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
61 struct msg_get_clock_rate {
62 struct bcm2835_mbox_hdr hdr;
63 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
68 #define DTB_DIR "broadcom/"
74 * https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
82 static const struct rpi_model rpi_model_unknown = {
84 DTB_DIR "bcm283x-rpi-other.dtb",
88 static const struct rpi_model rpi_models_new_scheme[] = {
91 DTB_DIR "bcm2835-rpi-a.dtb",
96 DTB_DIR "bcm2835-rpi-b.dtb",
101 DTB_DIR "bcm2835-rpi-a-plus.dtb",
106 DTB_DIR "bcm2835-rpi-b-plus.dtb",
111 DTB_DIR "bcm2836-rpi-2-b.dtb",
116 DTB_DIR "bcm2835-rpi-cm.dtb",
121 DTB_DIR "bcm2837-rpi-3-b.dtb",
126 DTB_DIR "bcm2835-rpi-zero.dtb",
131 DTB_DIR "bcm2837-rpi-cm3.dtb",
136 DTB_DIR "bcm2835-rpi-zero-w.dtb",
141 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
146 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
151 DTB_DIR "bcm2837-rpi-cm3.dtb",
156 DTB_DIR "bcm2711-rpi-4-b.dtb",
161 static const struct rpi_model rpi_models_old_scheme[] = {
164 DTB_DIR "bcm2835-rpi-b.dtb",
169 DTB_DIR "bcm2835-rpi-b.dtb",
174 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
179 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
184 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
189 DTB_DIR "bcm2835-rpi-a.dtb",
194 DTB_DIR "bcm2835-rpi-a.dtb",
199 DTB_DIR "bcm2835-rpi-a.dtb",
204 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
209 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
214 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
219 DTB_DIR "bcm2835-rpi-b-plus.dtb",
224 DTB_DIR "bcm2835-rpi-cm.dtb",
229 DTB_DIR "bcm2835-rpi-a-plus.dtb",
234 DTB_DIR "bcm2835-rpi-b-plus.dtb",
239 DTB_DIR "bcm2835-rpi-cm.dtb",
244 DTB_DIR "bcm2835-rpi-a-plus.dtb",
249 static uint32_t revision;
250 static uint32_t rev_scheme;
251 static uint32_t rev_type;
252 static const struct rpi_model *model;
255 #ifndef CONFIG_BCM2711
256 static struct mm_region bcm283x_mem_map[] = {
258 .virt = 0x00000000UL,
259 .phys = 0x00000000UL,
260 .size = 0x3f000000UL,
261 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
262 PTE_BLOCK_INNER_SHARE
264 .virt = 0x3f000000UL,
265 .phys = 0x3f000000UL,
266 .size = 0x01000000UL,
267 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
268 PTE_BLOCK_NON_SHARE |
269 PTE_BLOCK_PXN | PTE_BLOCK_UXN
271 /* List terminator */
276 static struct mm_region bcm283x_mem_map[] = {
278 .virt = 0x00000000UL,
279 .phys = 0x00000000UL,
280 .size = 0xfe000000UL,
281 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
282 PTE_BLOCK_INNER_SHARE
284 .virt = 0xfe000000UL,
285 .phys = 0xfe000000UL,
286 .size = 0x01800000UL,
287 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
288 PTE_BLOCK_NON_SHARE |
289 PTE_BLOCK_PXN | PTE_BLOCK_UXN
291 /* List terminator */
296 struct mm_region *mem_map = bcm283x_mem_map;
301 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
304 BCM2835_MBOX_INIT_HDR(msg);
305 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
307 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
309 printf("bcm2835: Could not query ARM memory size\n");
313 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
318 #ifdef CONFIG_OF_BOARD
319 #ifdef CONFIG_BCM2711
320 int dram_init_banksize(void)
322 return fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
323 (phys_size_t *)&gd->ram_size, gd->bd);
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 set_usbethaddr(void)
366 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
369 if (!model->has_onboard_eth)
372 if (env_get("usbethaddr"))
375 BCM2835_MBOX_INIT_HDR(msg);
376 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
378 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
380 printf("bcm2835: Could not query MAC address\n");
381 /* Ignore error; not critical */
385 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
387 if (!env_get("ethaddr"))
388 env_set("ethaddr", env_get("usbethaddr"));
393 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
394 static void set_board_info(void)
398 snprintf(s, sizeof(s), "0x%X", revision);
399 env_set("board_revision", s);
400 snprintf(s, sizeof(s), "%d", rev_scheme);
401 env_set("board_rev_scheme", s);
402 /* Can't rename this to board_rev_type since it's an ABI for scripts */
403 snprintf(s, sizeof(s), "0x%X", rev_type);
404 env_set("board_rev", s);
405 env_set("board_name", model->name);
407 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
409 static void set_serial_number(void)
411 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
413 char serial_string[17] = { 0 };
415 if (env_get("serial#"))
418 BCM2835_MBOX_INIT_HDR(msg);
419 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
421 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
423 printf("bcm2835: Could not query board serial\n");
424 /* Ignore error; not critical */
428 snprintf(serial_string, sizeof(serial_string), "%016llx",
429 msg->get_board_serial.body.resp.serial);
430 env_set("serial#", serial_string);
433 int misc_init_r(void)
438 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
446 static void get_board_rev(void)
448 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
450 const struct rpi_model *models;
451 uint32_t models_count;
453 BCM2835_MBOX_INIT_HDR(msg);
454 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
456 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
458 printf("bcm2835: Could not query board revision\n");
459 /* Ignore error; not critical */
464 * For details of old-vs-new scheme, see:
465 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
466 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
469 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
470 * lower byte to use as the board rev:
471 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
472 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
474 revision = msg->get_board_rev.body.resp.rev;
475 if (revision & 0x800000) {
477 rev_type = (revision >> 4) & 0xff;
478 models = rpi_models_new_scheme;
479 models_count = ARRAY_SIZE(rpi_models_new_scheme);
482 rev_type = revision & 0xff;
483 models = rpi_models_old_scheme;
484 models_count = ARRAY_SIZE(rpi_models_old_scheme);
486 if (rev_type >= models_count) {
487 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
488 model = &rpi_model_unknown;
489 } else if (!models[rev_type].name) {
490 printf("RPI: Board rev 0x%x unknown\n", rev_type);
491 model = &rpi_model_unknown;
493 model = &models[rev_type];
496 printf("RPI %s (0x%x)\n", model->name, revision);
501 #ifdef CONFIG_HW_WATCHDOG
507 gd->bd->bi_boot_params = 0x100;
509 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
513 * If the firmware passed a device tree use it for U-Boot.
515 void *board_fdt_blob_setup(void)
517 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
519 return (void *)fw_dtb_pointer;
522 int ft_board_setup(void *blob, bd_t *bd)
525 * For now, we simply always add the simplefb DT node. Later, we
526 * should be more intelligent, and e.g. only do this if no enabled DT
527 * node exists for the "real" graphics driver.
529 lcd_dt_simplefb_add_node(blob);
531 #ifdef CONFIG_EFI_LOADER
532 /* Reserve the spin table */
533 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);