2 * (C) Copyright 2012-2016 Stephen Warren
4 * SPDX-License-Identifier: GPL-2.0
11 #include <efi_loader.h>
12 #include <fdt_support.h>
13 #include <fdt_simplefb.h>
18 #include <asm/arch/mbox.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 DECLARE_GLOBAL_DATA_PTR;
28 /* From lowlevel_init.S */
29 extern unsigned long fw_dtb_pointer;
32 struct msg_get_arm_mem {
33 struct bcm2835_mbox_hdr hdr;
34 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
38 struct msg_get_board_rev {
39 struct bcm2835_mbox_hdr hdr;
40 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
44 struct msg_get_board_serial {
45 struct bcm2835_mbox_hdr hdr;
46 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
50 struct msg_get_mac_address {
51 struct bcm2835_mbox_hdr hdr;
52 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
56 struct msg_set_power_state {
57 struct bcm2835_mbox_hdr hdr;
58 struct bcm2835_mbox_tag_set_power_state set_power_state;
62 struct msg_get_clock_rate {
63 struct bcm2835_mbox_hdr hdr;
64 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
69 #define DTB_DIR "broadcom/"
75 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
76 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
77 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
79 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
80 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
81 * Foundation stated that the following source was accurate:
82 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
90 static const struct rpi_model rpi_model_unknown = {
92 DTB_DIR "bcm283x-rpi-other.dtb",
96 static const struct rpi_model rpi_models_new_scheme[] = {
99 DTB_DIR "bcm2836-rpi-2-b.dtb",
104 DTB_DIR "bcm2837-rpi-3-b.dtb",
109 DTB_DIR "bcm2835-rpi-zero.dtb",
114 static const struct rpi_model rpi_models_old_scheme[] = {
117 DTB_DIR "bcm2835-rpi-b.dtb",
122 DTB_DIR "bcm2835-rpi-b.dtb",
127 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
132 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
137 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
142 DTB_DIR "bcm2835-rpi-a.dtb",
147 DTB_DIR "bcm2835-rpi-a.dtb",
152 DTB_DIR "bcm2835-rpi-a.dtb",
157 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
162 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
167 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
172 DTB_DIR "bcm2835-rpi-b-plus.dtb",
177 DTB_DIR "bcm2835-rpi-cm.dtb",
182 DTB_DIR "bcm2835-rpi-a-plus.dtb",
187 DTB_DIR "bcm2835-rpi-b-plus.dtb",
192 DTB_DIR "bcm2835-rpi-cm.dtb",
197 DTB_DIR "bcm2835-rpi-a-plus.dtb",
202 static uint32_t revision;
203 static uint32_t rev_scheme;
204 static uint32_t rev_type;
205 static const struct rpi_model *model;
208 static struct mm_region bcm2837_mem_map[] = {
210 .virt = 0x00000000UL,
211 .phys = 0x00000000UL,
212 .size = 0x3f000000UL,
213 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
214 PTE_BLOCK_INNER_SHARE
216 .virt = 0x3f000000UL,
217 .phys = 0x3f000000UL,
218 .size = 0x01000000UL,
219 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
220 PTE_BLOCK_NON_SHARE |
221 PTE_BLOCK_PXN | PTE_BLOCK_UXN
223 /* List terminator */
228 struct mm_region *mem_map = bcm2837_mem_map;
233 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
236 BCM2835_MBOX_INIT_HDR(msg);
237 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
239 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
241 printf("bcm2835: Could not query ARM memory size\n");
245 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
250 static void set_fdtfile(void)
254 if (getenv("fdtfile"))
257 fdtfile = model->fdtfile;
258 setenv("fdtfile", fdtfile);
262 * If the firmware provided a valid FDT at boot time, let's expose it in
263 * ${fdt_addr} so it may be passed unmodified to the kernel.
265 static void set_fdt_addr(void)
267 if (getenv("fdt_addr"))
270 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
273 setenv_hex("fdt_addr", fw_dtb_pointer);
277 * Prevent relocation from stomping on a firmware provided FDT blob.
279 unsigned long board_get_usable_ram_top(unsigned long total_size)
281 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
283 return fw_dtb_pointer & ~0xffff;
286 static void set_usbethaddr(void)
288 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
291 if (!model->has_onboard_eth)
294 if (getenv("usbethaddr"))
297 BCM2835_MBOX_INIT_HDR(msg);
298 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
300 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
302 printf("bcm2835: Could not query MAC address\n");
303 /* Ignore error; not critical */
307 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
309 if (!getenv("ethaddr"))
310 setenv("ethaddr", getenv("usbethaddr"));
315 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
316 static void set_board_info(void)
320 snprintf(s, sizeof(s), "0x%X", revision);
321 setenv("board_revision", s);
322 snprintf(s, sizeof(s), "%d", rev_scheme);
323 setenv("board_rev_scheme", s);
324 /* Can't rename this to board_rev_type since it's an ABI for scripts */
325 snprintf(s, sizeof(s), "0x%X", rev_type);
326 setenv("board_rev", s);
327 setenv("board_name", model->name);
329 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
331 static void set_serial_number(void)
333 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
335 char serial_string[17] = { 0 };
337 if (getenv("serial#"))
340 BCM2835_MBOX_INIT_HDR(msg);
341 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
343 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
345 printf("bcm2835: Could not query board serial\n");
346 /* Ignore error; not critical */
350 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
351 msg->get_board_serial.body.resp.serial);
352 setenv("serial#", serial_string);
355 int misc_init_r(void)
360 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
368 static int power_on_module(u32 module)
370 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
373 BCM2835_MBOX_INIT_HDR(msg_pwr);
374 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
376 msg_pwr->set_power_state.body.req.device_id = module;
377 msg_pwr->set_power_state.body.req.state =
378 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
379 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
381 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
384 printf("bcm2835: Could not set module %u power state\n",
392 static void get_board_rev(void)
394 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
396 const struct rpi_model *models;
397 uint32_t models_count;
399 BCM2835_MBOX_INIT_HDR(msg);
400 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
402 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
404 printf("bcm2835: Could not query board revision\n");
405 /* Ignore error; not critical */
410 * For details of old-vs-new scheme, see:
411 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
412 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
415 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
416 * lower byte to use as the board rev:
417 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
418 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
420 revision = msg->get_board_rev.body.resp.rev;
421 if (revision & 0x800000) {
423 rev_type = (revision >> 4) & 0xff;
424 models = rpi_models_new_scheme;
425 models_count = ARRAY_SIZE(rpi_models_new_scheme);
428 rev_type = revision & 0xff;
429 models = rpi_models_old_scheme;
430 models_count = ARRAY_SIZE(rpi_models_old_scheme);
432 if (rev_type >= models_count) {
433 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
434 model = &rpi_model_unknown;
435 } else if (!models[rev_type].name) {
436 printf("RPI: Board rev 0x%x unknown\n", rev_type);
437 model = &rpi_model_unknown;
439 model = &models[rev_type];
442 printf("RPI %s (0x%x)\n", model->name, revision);
445 #ifndef CONFIG_PL01X_SERIAL
446 static bool rpi_is_serial_active(void)
448 int serial_gpio = 15;
452 * The RPi3 disables the mini uart by default. The easiest way to find
453 * out whether it is available is to check if the RX pin is muxed.
456 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
459 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
465 /* Disable mini-UART I/O if it's not pinmuxed to our pins.
466 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
468 static void rpi_disable_inactive_uart(void)
471 struct bcm283x_mu_serial_platdata *plat;
473 if (uclass_get_device_by_driver(UCLASS_SERIAL,
474 DM_GET_DRIVER(serial_bcm283x_mu),
478 if (!rpi_is_serial_active()) {
479 plat = dev_get_platdata(dev);
480 plat->disabled = true;
487 #ifndef CONFIG_PL01X_SERIAL
488 rpi_disable_inactive_uart();
493 gd->bd->bi_boot_params = 0x100;
495 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
498 int board_mmc_init(bd_t *bis)
500 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
503 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
505 BCM2835_MBOX_INIT_HDR(msg_clk);
506 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
507 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
509 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
511 printf("bcm2835: Could not query eMMC clock rate\n");
515 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
516 msg_clk->get_clock_rate.body.resp.rate_hz);
519 int ft_board_setup(void *blob, bd_t *bd)
522 * For now, we simply always add the simplefb DT node. Later, we
523 * should be more intelligent, and e.g. only do this if no enabled DT
524 * node exists for the "real" graphics driver.
526 lcd_dt_simplefb_add_node(blob);
528 #ifdef CONFIG_EFI_LOADER
529 /* Reserve the spin table */
530 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);