2 * (C) Copyright 2012-2016 Stephen Warren
4 * SPDX-License-Identifier: GPL-2.0
11 #include <fdt_support.h>
12 #include <fdt_simplefb.h>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_pl01x.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;
31 static const struct bcm2835_gpio_platdata gpio_platdata = {
32 .base = BCM2835_GPIO_BASE,
35 U_BOOT_DEVICE(bcm2835_gpios) = {
36 .name = "gpio_bcm2835",
37 .platdata = &gpio_platdata,
40 #ifdef CONFIG_PL01X_SERIAL
41 static const struct pl01x_serial_platdata serial_platdata = {
42 #ifndef CONFIG_BCM2835
51 U_BOOT_DEVICE(bcm2835_serials) = {
52 .name = "serial_pl01x",
53 .platdata = &serial_platdata,
56 static struct bcm283x_mu_serial_platdata serial_platdata = {
62 U_BOOT_DEVICE(bcm2837_serials) = {
63 .name = "serial_bcm283x_mu",
64 .platdata = &serial_platdata,
68 struct msg_get_arm_mem {
69 struct bcm2835_mbox_hdr hdr;
70 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
74 struct msg_get_board_rev {
75 struct bcm2835_mbox_hdr hdr;
76 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
80 struct msg_get_board_serial {
81 struct bcm2835_mbox_hdr hdr;
82 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
86 struct msg_get_mac_address {
87 struct bcm2835_mbox_hdr hdr;
88 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
92 struct msg_set_power_state {
93 struct bcm2835_mbox_hdr hdr;
94 struct bcm2835_mbox_tag_set_power_state set_power_state;
98 struct msg_get_clock_rate {
99 struct bcm2835_mbox_hdr hdr;
100 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
105 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
106 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
107 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
109 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
110 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
111 * Foundation stated that the following source was accurate:
112 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
117 bool has_onboard_eth;
120 static const struct rpi_model rpi_model_unknown = {
122 "bcm283x-rpi-other.dtb",
126 static const struct rpi_model rpi_models_new_scheme[] = {
129 "bcm2836-rpi-2-b.dtb",
134 "bcm2837-rpi-3-b.dtb",
139 "bcm2835-rpi-zero.dtb",
144 static const struct rpi_model rpi_models_old_scheme[] = {
157 "bcm2835-rpi-b-rev2.dtb",
162 "bcm2835-rpi-b-rev2.dtb",
167 "bcm2835-rpi-b-rev2.dtb",
187 "bcm2835-rpi-b-rev2.dtb",
192 "bcm2835-rpi-b-rev2.dtb",
197 "bcm2835-rpi-b-rev2.dtb",
202 "bcm2835-rpi-b-plus.dtb",
207 "bcm2835-rpi-cm.dtb",
212 "bcm2835-rpi-a-plus.dtb",
217 "bcm2835-rpi-b-plus.dtb",
222 "bcm2835-rpi-cm.dtb",
227 "bcm2835-rpi-a-plus.dtb",
232 static uint32_t revision;
233 static uint32_t rev_scheme;
234 static uint32_t rev_type;
235 static const struct rpi_model *model;
238 static struct mm_region bcm2837_mem_map[] = {
240 .virt = 0x00000000UL,
241 .phys = 0x00000000UL,
242 .size = 0x3f000000UL,
243 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
244 PTE_BLOCK_INNER_SHARE
246 .virt = 0x3f000000UL,
247 .phys = 0x3f000000UL,
248 .size = 0x01000000UL,
249 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
250 PTE_BLOCK_NON_SHARE |
251 PTE_BLOCK_PXN | PTE_BLOCK_UXN
253 /* List terminator */
258 struct mm_region *mem_map = bcm2837_mem_map;
263 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
266 BCM2835_MBOX_INIT_HDR(msg);
267 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
269 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
271 printf("bcm2835: Could not query ARM memory size\n");
275 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
280 static void set_fdtfile(void)
284 if (getenv("fdtfile"))
287 fdtfile = model->fdtfile;
288 setenv("fdtfile", fdtfile);
292 * If the firmware provided a valid FDT at boot time, let's expose it in
293 * ${fdt_addr} so it may be passed unmodified to the kernel.
295 static void set_fdt_addr(void)
297 if (getenv("fdt_addr"))
300 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
303 setenv_hex("fdt_addr", fw_dtb_pointer);
307 * Prevent relocation from stomping on a firmware provided FDT blob.
309 unsigned long board_get_usable_ram_top(unsigned long total_size)
311 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
313 return fw_dtb_pointer & ~0xffff;
316 static void set_usbethaddr(void)
318 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
321 if (!model->has_onboard_eth)
324 if (getenv("usbethaddr"))
327 BCM2835_MBOX_INIT_HDR(msg);
328 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
330 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
332 printf("bcm2835: Could not query MAC address\n");
333 /* Ignore error; not critical */
337 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
339 if (!getenv("ethaddr"))
340 setenv("ethaddr", getenv("usbethaddr"));
345 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
346 static void set_board_info(void)
350 snprintf(s, sizeof(s), "0x%X", revision);
351 setenv("board_revision", s);
352 snprintf(s, sizeof(s), "%d", rev_scheme);
353 setenv("board_rev_scheme", s);
354 /* Can't rename this to board_rev_type since it's an ABI for scripts */
355 snprintf(s, sizeof(s), "0x%X", rev_type);
356 setenv("board_rev", s);
357 setenv("board_name", model->name);
359 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
361 static void set_serial_number(void)
363 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
365 char serial_string[17] = { 0 };
367 if (getenv("serial#"))
370 BCM2835_MBOX_INIT_HDR(msg);
371 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
373 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
375 printf("bcm2835: Could not query board serial\n");
376 /* Ignore error; not critical */
380 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
381 msg->get_board_serial.body.resp.serial);
382 setenv("serial#", serial_string);
385 int misc_init_r(void)
390 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
398 static int power_on_module(u32 module)
400 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
403 BCM2835_MBOX_INIT_HDR(msg_pwr);
404 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
406 msg_pwr->set_power_state.body.req.device_id = module;
407 msg_pwr->set_power_state.body.req.state =
408 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
409 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
411 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
414 printf("bcm2835: Could not set module %u power state\n",
422 static void get_board_rev(void)
424 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
426 const struct rpi_model *models;
427 uint32_t models_count;
429 BCM2835_MBOX_INIT_HDR(msg);
430 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
432 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
434 printf("bcm2835: Could not query board revision\n");
435 /* Ignore error; not critical */
440 * For details of old-vs-new scheme, see:
441 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
442 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
445 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
446 * lower byte to use as the board rev:
447 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
448 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
450 revision = msg->get_board_rev.body.resp.rev;
451 if (revision & 0x800000) {
453 rev_type = (revision >> 4) & 0xff;
454 models = rpi_models_new_scheme;
455 models_count = ARRAY_SIZE(rpi_models_new_scheme);
458 rev_type = revision & 0xff;
459 models = rpi_models_old_scheme;
460 models_count = ARRAY_SIZE(rpi_models_old_scheme);
462 if (rev_type >= models_count) {
463 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
464 model = &rpi_model_unknown;
465 } else if (!models[rev_type].name) {
466 printf("RPI: Board rev 0x%x unknown\n", rev_type);
467 model = &rpi_model_unknown;
469 model = &models[rev_type];
472 printf("RPI %s (0x%x)\n", model->name, revision);
475 #ifndef CONFIG_PL01X_SERIAL
476 static bool rpi_is_serial_active(void)
478 int serial_gpio = 15;
482 * The RPi3 disables the mini uart by default. The easiest way to find
483 * out whether it is available is to check if the RX pin is muxed.
486 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
489 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
495 /* Disable mini-UART I/O if it's not pinmuxed to our pins.
496 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
498 static void rpi_disable_inactive_uart(void)
501 struct bcm283x_mu_serial_platdata *plat;
503 if (uclass_get_device_by_driver(UCLASS_SERIAL,
504 DM_GET_DRIVER(serial_bcm283x_mu),
508 if (!rpi_is_serial_active()) {
509 plat = dev_get_platdata(dev);
510 plat->disabled = true;
517 #ifndef CONFIG_PL01X_SERIAL
518 rpi_disable_inactive_uart();
523 gd->bd->bi_boot_params = 0x100;
525 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
528 int board_mmc_init(bd_t *bis)
530 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
533 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
535 BCM2835_MBOX_INIT_HDR(msg_clk);
536 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
537 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
539 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
541 printf("bcm2835: Could not query eMMC clock rate\n");
545 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
546 msg_clk->get_clock_rate.body.resp.rate_hz);
549 int ft_board_setup(void *blob, bd_t *bd)
552 * For now, we simply always add the simplefb DT node. Later, we
553 * should be more intelligent, and e.g. only do this if no enabled DT
554 * node exists for the "real" graphics driver.
556 lcd_dt_simplefb_add_node(blob);