]> git.sur5r.net Git - u-boot/blobdiff - board/raspberrypi/rpi/rpi.c
SPDX: Convert all of our single license tags to Linux Kernel style
[u-boot] / board / raspberrypi / rpi / rpi.c
index 89d78cc6c407a36189c9b24f4fe7c844917f848f..35f59395520731b304ef12df5919b7b85ac890bb 100644 (file)
@@ -1,12 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
- * (C) Copyright 2012-2013,2015 Stephen Warren
- *
- * SPDX-License-Identifier:    GPL-2.0
+ * (C) Copyright 2012-2016 Stephen Warren
  */
 
 #include <common.h>
+#include <inttypes.h>
 #include <config.h>
 #include <dm.h>
+#include <environment.h>
+#include <efi_loader.h>
 #include <fdt_support.h>
 #include <fdt_simplefb.h>
 #include <lcd.h>
 #include <mmc.h>
 #include <asm/gpio.h>
 #include <asm/arch/mbox.h>
+#include <asm/arch/msg.h>
 #include <asm/arch/sdhci.h>
 #include <asm/global_data.h>
-#include <dm/platform_data/serial_pl01x.h>
+#include <dm/platform_data/serial_bcm283x_mu.h>
+#ifdef CONFIG_ARM64
+#include <asm/armv8/mmu.h>
+#endif
+#include <watchdog.h>
+#include <dm/pinctrl.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static const struct bcm2835_gpio_platdata gpio_platdata = {
-       .base = BCM2835_GPIO_BASE,
-};
-
-U_BOOT_DEVICE(bcm2835_gpios) = {
-       .name = "gpio_bcm2835",
-       .platdata = &gpio_platdata,
-};
-
-static const struct pl01x_serial_platdata serial_platdata = {
-#ifdef CONFIG_BCM2836
-       .base = 0x3f201000,
-#else
-       .base = 0x20201000,
-#endif
-       .type = TYPE_PL011,
-       .clock = 3000000,
-};
-
-U_BOOT_DEVICE(bcm2835_serials) = {
-       .name = "serial_pl01x",
-       .platdata = &serial_platdata,
-};
+/* From lowlevel_init.S */
+extern unsigned long fw_dtb_pointer;
 
+/* TODO(sjg@chromium.org): Move these to the msg.c file */
 struct msg_get_arm_mem {
        struct bcm2835_mbox_hdr hdr;
        struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
@@ -56,15 +44,15 @@ struct msg_get_board_rev {
        u32 end_tag;
 };
 
-struct msg_get_mac_address {
+struct msg_get_board_serial {
        struct bcm2835_mbox_hdr hdr;
-       struct bcm2835_mbox_tag_get_mac_address get_mac_address;
+       struct bcm2835_mbox_tag_get_board_serial get_board_serial;
        u32 end_tag;
 };
 
-struct msg_set_power_state {
+struct msg_get_mac_address {
        struct bcm2835_mbox_hdr hdr;
-       struct bcm2835_mbox_tag_set_power_state set_power_state;
+       struct bcm2835_mbox_tag_get_mac_address get_mac_address;
        u32 end_tag;
 };
 
@@ -74,6 +62,12 @@ struct msg_get_clock_rate {
        u32 end_tag;
 };
 
+#ifdef CONFIG_ARM64
+#define DTB_DIR "broadcom/"
+#else
+#define DTB_DIR ""
+#endif
+
 /*
  * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
  * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
@@ -92,111 +86,152 @@ struct rpi_model {
 
 static const struct rpi_model rpi_model_unknown = {
        "Unknown model",
-#ifdef CONFIG_BCM2836
-       "bcm2836-rpi-other.dtb",
-#else
-       "bcm2835-rpi-other.dtb",
-#endif
+       DTB_DIR "bcm283x-rpi-other.dtb",
        false,
 };
 
 static const struct rpi_model rpi_models_new_scheme[] = {
+       [0x0] = {
+               "Model A",
+               DTB_DIR "bcm2835-rpi-a.dtb",
+               false,
+       },
+       [0x1] = {
+               "Model B",
+               DTB_DIR "bcm2835-rpi-b.dtb",
+               true,
+       },
+       [0x2] = {
+               "Model A+",
+               DTB_DIR "bcm2835-rpi-a-plus.dtb",
+               false,
+       },
+       [0x3] = {
+               "Model B+",
+               DTB_DIR "bcm2835-rpi-b-plus.dtb",
+               true,
+       },
        [0x4] = {
                "2 Model B",
-               "bcm2836-rpi-2-b.dtb",
+               DTB_DIR "bcm2836-rpi-2-b.dtb",
+               true,
+       },
+       [0x6] = {
+               "Compute Module",
+               DTB_DIR "bcm2835-rpi-cm.dtb",
+               false,
+       },
+       [0x8] = {
+               "3 Model B",
+               DTB_DIR "bcm2837-rpi-3-b.dtb",
                true,
        },
        [0x9] = {
                "Zero",
-               "bcm2835-rpi-zero.dtb",
+               DTB_DIR "bcm2835-rpi-zero.dtb",
+               false,
+       },
+       [0xA] = {
+               "Compute Module 3",
+               DTB_DIR "bcm2837-rpi-cm3.dtb",
                false,
        },
+       [0xC] = {
+               "Zero W",
+               DTB_DIR "bcm2835-rpi-zero-w.dtb",
+               false,
+       },
+       [0xD] = {
+               "3 Model B+",
+               DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
+               true,
+       },
 };
 
 static const struct rpi_model rpi_models_old_scheme[] = {
        [0x2] = {
                "Model B",
-               "bcm2835-rpi-b.dtb",
+               DTB_DIR "bcm2835-rpi-b.dtb",
                true,
        },
        [0x3] = {
                "Model B",
-               "bcm2835-rpi-b.dtb",
+               DTB_DIR "bcm2835-rpi-b.dtb",
                true,
        },
        [0x4] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0x5] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0x6] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0x7] = {
                "Model A",
-               "bcm2835-rpi-a.dtb",
+               DTB_DIR "bcm2835-rpi-a.dtb",
                false,
        },
        [0x8] = {
                "Model A",
-               "bcm2835-rpi-a.dtb",
+               DTB_DIR "bcm2835-rpi-a.dtb",
                false,
        },
        [0x9] = {
                "Model A",
-               "bcm2835-rpi-a.dtb",
+               DTB_DIR "bcm2835-rpi-a.dtb",
                false,
        },
        [0xd] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0xe] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0xf] = {
                "Model B rev2",
-               "bcm2835-rpi-b-rev2.dtb",
+               DTB_DIR "bcm2835-rpi-b-rev2.dtb",
                true,
        },
        [0x10] = {
                "Model B+",
-               "bcm2835-rpi-b-plus.dtb",
+               DTB_DIR "bcm2835-rpi-b-plus.dtb",
                true,
        },
        [0x11] = {
                "Compute Module",
-               "bcm2835-rpi-cm.dtb",
+               DTB_DIR "bcm2835-rpi-cm.dtb",
                false,
        },
        [0x12] = {
                "Model A+",
-               "bcm2835-rpi-a-plus.dtb",
+               DTB_DIR "bcm2835-rpi-a-plus.dtb",
                false,
        },
        [0x13] = {
                "Model B+",
-               "bcm2835-rpi-b-plus.dtb",
+               DTB_DIR "bcm2835-rpi-b-plus.dtb",
                true,
        },
        [0x14] = {
                "Compute Module",
-               "bcm2835-rpi-cm.dtb",
+               DTB_DIR "bcm2835-rpi-cm.dtb",
                false,
        },
        [0x15] = {
                "Model A+",
-               "bcm2835-rpi-a-plus.dtb",
+               DTB_DIR "bcm2835-rpi-a-plus.dtb",
                false,
        },
 };
@@ -206,6 +241,30 @@ static uint32_t rev_scheme;
 static uint32_t rev_type;
 static const struct rpi_model *model;
 
+#ifdef CONFIG_ARM64
+static struct mm_region bcm2837_mem_map[] = {
+       {
+               .virt = 0x00000000UL,
+               .phys = 0x00000000UL,
+               .size = 0x3f000000UL,
+               .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
+                        PTE_BLOCK_INNER_SHARE
+       }, {
+               .virt = 0x3f000000UL,
+               .phys = 0x3f000000UL,
+               .size = 0x01000000UL,
+               .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
+                        PTE_BLOCK_NON_SHARE |
+                        PTE_BLOCK_PXN | PTE_BLOCK_UXN
+       }, {
+               /* List terminator */
+               0,
+       }
+};
+
+struct mm_region *mem_map = bcm2837_mem_map;
+#endif
+
 int dram_init(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
@@ -229,11 +288,36 @@ static void set_fdtfile(void)
 {
        const char *fdtfile;
 
-       if (getenv("fdtfile"))
+       if (env_get("fdtfile"))
                return;
 
        fdtfile = model->fdtfile;
-       setenv("fdtfile", fdtfile);
+       env_set("fdtfile", fdtfile);
+}
+
+/*
+ * If the firmware provided a valid FDT at boot time, let's expose it in
+ * ${fdt_addr} so it may be passed unmodified to the kernel.
+ */
+static void set_fdt_addr(void)
+{
+       if (env_get("fdt_addr"))
+               return;
+
+       if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
+               return;
+
+       env_set_hex("fdt_addr", fw_dtb_pointer);
+}
+
+/*
+ * Prevent relocation from stomping on a firmware provided FDT blob.
+ */
+unsigned long board_get_usable_ram_top(unsigned long total_size)
+{
+       if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
+               return gd->ram_top;
+       return fw_dtb_pointer & ~0xffff;
 }
 
 static void set_usbethaddr(void)
@@ -244,7 +328,7 @@ static void set_usbethaddr(void)
        if (!model->has_onboard_eth)
                return;
 
-       if (getenv("usbethaddr"))
+       if (env_get("usbethaddr"))
                return;
 
        BCM2835_MBOX_INIT_HDR(msg);
@@ -257,7 +341,10 @@ static void set_usbethaddr(void)
                return;
        }
 
-       eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
+       eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
+
+       if (!env_get("ethaddr"))
+               env_set("ethaddr", env_get("usbethaddr"));
 
        return;
 }
@@ -268,46 +355,49 @@ static void set_board_info(void)
        char s[11];
 
        snprintf(s, sizeof(s), "0x%X", revision);
-       setenv("board_revision", s);
+       env_set("board_revision", s);
        snprintf(s, sizeof(s), "%d", rev_scheme);
-       setenv("board_rev_scheme", s);
+       env_set("board_rev_scheme", s);
        /* Can't rename this to board_rev_type since it's an ABI for scripts */
        snprintf(s, sizeof(s), "0x%X", rev_type);
-       setenv("board_rev", s);
-       setenv("board_name", model->name);
+       env_set("board_rev", s);
+       env_set("board_name", model->name);
 }
 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
 
+static void set_serial_number(void)
+{
+       ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
+       int ret;
+       char serial_string[17] = { 0 };
+
+       if (env_get("serial#"))
+               return;
+
+       BCM2835_MBOX_INIT_HDR(msg);
+       BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
+
+       ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
+       if (ret) {
+               printf("bcm2835: Could not query board serial\n");
+               /* Ignore error; not critical */
+               return;
+       }
+
+       snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
+                msg->get_board_serial.body.resp.serial);
+       env_set("serial#", serial_string);
+}
+
 int misc_init_r(void)
 {
+       set_fdt_addr();
        set_fdtfile();
        set_usbethaddr();
 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
        set_board_info();
 #endif
-       return 0;
-}
-
-static int power_on_module(u32 module)
-{
-       ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
-       int ret;
-
-       BCM2835_MBOX_INIT_HDR(msg_pwr);
-       BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
-                             SET_POWER_STATE);
-       msg_pwr->set_power_state.body.req.device_id = module;
-       msg_pwr->set_power_state.body.req.state =
-               BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
-               BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
-
-       ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
-                                    &msg_pwr->hdr);
-       if (ret) {
-               printf("bcm2835: Could not set module %u power state\n",
-                      module);
-               return -1;
-       }
+       set_serial_number();
 
        return 0;
 }
@@ -367,32 +457,25 @@ static void get_board_rev(void)
 
 int board_init(void)
 {
+#ifdef CONFIG_HW_WATCHDOG
+       hw_watchdog_init();
+#endif
+
        get_board_rev();
 
        gd->bd->bi_boot_params = 0x100;
 
-       return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
+       return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 }
 
-int board_mmc_init(bd_t *bis)
+/*
+ * If the firmware passed a device tree use it for U-Boot.
+ */
+void *board_fdt_blob_setup(void)
 {
-       ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
-       int ret;
-
-       power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
-
-       BCM2835_MBOX_INIT_HDR(msg_clk);
-       BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
-       msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
-
-       ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
-       if (ret) {
-               printf("bcm2835: Could not query eMMC clock rate\n");
-               return -1;
-       }
-
-       return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
-                                 msg_clk->get_clock_rate.body.resp.rate_hz);
+       if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
+               return NULL;
+       return (void *)fw_dtb_pointer;
 }
 
 int ft_board_setup(void *blob, bd_t *bd)
@@ -404,5 +487,10 @@ int ft_board_setup(void *blob, bd_t *bd)
         */
        lcd_dt_simplefb_add_node(blob);
 
+#ifdef CONFIG_EFI_LOADER
+       /* Reserve the spin table */
+       efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
+#endif
+
        return 0;
 }