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 /* From lowlevel_init.S */
31 extern unsigned long fw_dtb_pointer;
33 /* TODO(sjg@chromium.org): Move these to the msg.c file */
34 struct msg_get_arm_mem {
35 struct bcm2835_mbox_hdr hdr;
36 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
40 struct msg_get_board_rev {
41 struct bcm2835_mbox_hdr hdr;
42 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
46 struct msg_get_board_serial {
47 struct bcm2835_mbox_hdr hdr;
48 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
52 struct msg_get_mac_address {
53 struct bcm2835_mbox_hdr hdr;
54 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
58 struct msg_get_clock_rate {
59 struct bcm2835_mbox_hdr hdr;
60 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
65 #define DTB_DIR "broadcom/"
71 * https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
79 static const struct rpi_model rpi_model_unknown = {
81 DTB_DIR "bcm283x-rpi-other.dtb",
85 static const struct rpi_model rpi_models_new_scheme[] = {
88 DTB_DIR "bcm2835-rpi-a.dtb",
93 DTB_DIR "bcm2835-rpi-b.dtb",
98 DTB_DIR "bcm2835-rpi-a-plus.dtb",
103 DTB_DIR "bcm2835-rpi-b-plus.dtb",
108 DTB_DIR "bcm2836-rpi-2-b.dtb",
113 DTB_DIR "bcm2835-rpi-cm.dtb",
118 DTB_DIR "bcm2837-rpi-3-b.dtb",
123 DTB_DIR "bcm2835-rpi-zero.dtb",
128 DTB_DIR "bcm2837-rpi-cm3.dtb",
133 DTB_DIR "bcm2835-rpi-zero-w.dtb",
138 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
143 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
148 DTB_DIR "bcm2837-rpi-cm3.dtb",
153 DTB_DIR "bcm2711-rpi-4-b.dtb",
158 static const struct rpi_model rpi_models_old_scheme[] = {
161 DTB_DIR "bcm2835-rpi-b.dtb",
166 DTB_DIR "bcm2835-rpi-b.dtb",
171 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
176 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
181 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
186 DTB_DIR "bcm2835-rpi-a.dtb",
191 DTB_DIR "bcm2835-rpi-a.dtb",
196 DTB_DIR "bcm2835-rpi-a.dtb",
201 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
206 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
211 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
216 DTB_DIR "bcm2835-rpi-b-plus.dtb",
221 DTB_DIR "bcm2835-rpi-cm.dtb",
226 DTB_DIR "bcm2835-rpi-a-plus.dtb",
231 DTB_DIR "bcm2835-rpi-b-plus.dtb",
236 DTB_DIR "bcm2835-rpi-cm.dtb",
241 DTB_DIR "bcm2835-rpi-a-plus.dtb",
246 static uint32_t revision;
247 static uint32_t rev_scheme;
248 static uint32_t rev_type;
249 static const struct rpi_model *model;
252 #ifndef CONFIG_BCM2711
253 static struct mm_region bcm283x_mem_map[] = {
255 .virt = 0x00000000UL,
256 .phys = 0x00000000UL,
257 .size = 0x3f000000UL,
258 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
259 PTE_BLOCK_INNER_SHARE
261 .virt = 0x3f000000UL,
262 .phys = 0x3f000000UL,
263 .size = 0x01000000UL,
264 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
265 PTE_BLOCK_NON_SHARE |
266 PTE_BLOCK_PXN | PTE_BLOCK_UXN
268 /* List terminator */
273 static struct mm_region bcm283x_mem_map[] = {
275 .virt = 0x00000000UL,
276 .phys = 0x00000000UL,
277 .size = 0xfe000000UL,
278 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
279 PTE_BLOCK_INNER_SHARE
281 .virt = 0xfe000000UL,
282 .phys = 0xfe000000UL,
283 .size = 0x01800000UL,
284 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
285 PTE_BLOCK_NON_SHARE |
286 PTE_BLOCK_PXN | PTE_BLOCK_UXN
288 /* List terminator */
293 struct mm_region *mem_map = bcm283x_mem_map;
298 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
301 BCM2835_MBOX_INIT_HDR(msg);
302 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
304 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
306 printf("bcm2835: Could not query ARM memory size\n");
310 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
315 static void set_fdtfile(void)
319 if (env_get("fdtfile"))
322 fdtfile = model->fdtfile;
323 env_set("fdtfile", fdtfile);
327 * If the firmware provided a valid FDT at boot time, let's expose it in
328 * ${fdt_addr} so it may be passed unmodified to the kernel.
330 static void set_fdt_addr(void)
332 if (env_get("fdt_addr"))
335 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
338 env_set_hex("fdt_addr", fw_dtb_pointer);
342 * Prevent relocation from stomping on a firmware provided FDT blob.
344 unsigned long board_get_usable_ram_top(unsigned long total_size)
346 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
348 return fw_dtb_pointer & ~0xffff;
351 static void set_usbethaddr(void)
353 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
356 if (!model->has_onboard_eth)
359 if (env_get("usbethaddr"))
362 BCM2835_MBOX_INIT_HDR(msg);
363 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
365 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
367 printf("bcm2835: Could not query MAC address\n");
368 /* Ignore error; not critical */
372 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
374 if (!env_get("ethaddr"))
375 env_set("ethaddr", env_get("usbethaddr"));
380 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
381 static void set_board_info(void)
385 snprintf(s, sizeof(s), "0x%X", revision);
386 env_set("board_revision", s);
387 snprintf(s, sizeof(s), "%d", rev_scheme);
388 env_set("board_rev_scheme", s);
389 /* Can't rename this to board_rev_type since it's an ABI for scripts */
390 snprintf(s, sizeof(s), "0x%X", rev_type);
391 env_set("board_rev", s);
392 env_set("board_name", model->name);
394 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
396 static void set_serial_number(void)
398 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
400 char serial_string[17] = { 0 };
402 if (env_get("serial#"))
405 BCM2835_MBOX_INIT_HDR(msg);
406 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
408 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
410 printf("bcm2835: Could not query board serial\n");
411 /* Ignore error; not critical */
415 snprintf(serial_string, sizeof(serial_string), "%016llx",
416 msg->get_board_serial.body.resp.serial);
417 env_set("serial#", serial_string);
420 int misc_init_r(void)
425 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
433 static void get_board_rev(void)
435 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
437 const struct rpi_model *models;
438 uint32_t models_count;
440 BCM2835_MBOX_INIT_HDR(msg);
441 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
443 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
445 printf("bcm2835: Could not query board revision\n");
446 /* Ignore error; not critical */
451 * For details of old-vs-new scheme, see:
452 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
453 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
456 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
457 * lower byte to use as the board rev:
458 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
459 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
461 revision = msg->get_board_rev.body.resp.rev;
462 if (revision & 0x800000) {
464 rev_type = (revision >> 4) & 0xff;
465 models = rpi_models_new_scheme;
466 models_count = ARRAY_SIZE(rpi_models_new_scheme);
469 rev_type = revision & 0xff;
470 models = rpi_models_old_scheme;
471 models_count = ARRAY_SIZE(rpi_models_old_scheme);
473 if (rev_type >= models_count) {
474 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
475 model = &rpi_model_unknown;
476 } else if (!models[rev_type].name) {
477 printf("RPI: Board rev 0x%x unknown\n", rev_type);
478 model = &rpi_model_unknown;
480 model = &models[rev_type];
483 printf("RPI %s (0x%x)\n", model->name, revision);
488 #ifdef CONFIG_HW_WATCHDOG
494 gd->bd->bi_boot_params = 0x100;
496 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
500 * If the firmware passed a device tree use it for U-Boot.
502 void *board_fdt_blob_setup(void)
504 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
506 return (void *)fw_dtb_pointer;
509 int ft_board_setup(void *blob, bd_t *bd)
512 * For now, we simply always add the simplefb DT node. Later, we
513 * should be more intelligent, and e.g. only do this if no enabled DT
514 * node exists for the "real" graphics driver.
516 lcd_dt_simplefb_add_node(blob);
518 #ifdef CONFIG_EFI_LOADER
519 /* Reserve the spin table */
520 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);