uboot/board/raspberrypi/rpi/rpi.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2012-2016 Stephen Warren
   3 *
   4 * SPDX-License-Identifier:     GPL-2.0
   5 */
   6
   7#include <common.h>
   8#include <inttypes.h>
   9#include <config.h>
  10#include <dm.h>
  11#include <efi_loader.h>
  12#include <fdt_support.h>
  13#include <fdt_simplefb.h>
  14#include <lcd.h>
  15#include <memalign.h>
  16#include <mmc.h>
  17#include <asm/gpio.h>
  18#include <asm/arch/mbox.h>
  19#include <asm/arch/msg.h>
  20#include <asm/arch/sdhci.h>
  21#include <asm/global_data.h>
  22#include <dm/platform_data/serial_bcm283x_mu.h>
  23#ifdef CONFIG_ARM64
  24#include <asm/armv8/mmu.h>
  25#endif
  26#include <watchdog.h>
  27#include <dm/pinctrl.h>
  28
  29DECLARE_GLOBAL_DATA_PTR;
  30
  31/* From lowlevel_init.S */
  32extern unsigned long fw_dtb_pointer;
  33
  34/* TODO(sjg@chromium.org): Move these to the msg.c file */
  35struct msg_get_arm_mem {
  36        struct bcm2835_mbox_hdr hdr;
  37        struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  38        u32 end_tag;
  39};
  40
  41struct msg_get_board_rev {
  42        struct bcm2835_mbox_hdr hdr;
  43        struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  44        u32 end_tag;
  45};
  46
  47struct msg_get_board_serial {
  48        struct bcm2835_mbox_hdr hdr;
  49        struct bcm2835_mbox_tag_get_board_serial get_board_serial;
  50        u32 end_tag;
  51};
  52
  53struct msg_get_mac_address {
  54        struct bcm2835_mbox_hdr hdr;
  55        struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  56        u32 end_tag;
  57};
  58
  59struct msg_get_clock_rate {
  60        struct bcm2835_mbox_hdr hdr;
  61        struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  62        u32 end_tag;
  63};
  64
  65#ifdef CONFIG_ARM64
  66#define DTB_DIR "broadcom/"
  67#else
  68#define DTB_DIR ""
  69#endif
  70
  71/*
  72 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
  73 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
  74 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
  75 *
  76 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
  77 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
  78 * Foundation stated that the following source was accurate:
  79 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
  80 */
  81struct rpi_model {
  82        const char *name;
  83        const char *fdtfile;
  84        bool has_onboard_eth;
  85};
  86
  87static const struct rpi_model rpi_model_unknown = {
  88        "Unknown model",
  89        DTB_DIR "bcm283x-rpi-other.dtb",
  90        false,
  91};
  92
  93static const struct rpi_model rpi_models_new_scheme[] = {
  94        [0x4] = {
  95                "2 Model B",
  96                DTB_DIR "bcm2836-rpi-2-b.dtb",
  97                true,
  98        },
  99        [0x8] = {
 100                "3 Model B",
 101                DTB_DIR "bcm2837-rpi-3-b.dtb",
 102                true,
 103        },
 104        [0x9] = {
 105                "Zero",
 106                DTB_DIR "bcm2835-rpi-zero.dtb",
 107                false,
 108        },
 109        [0xC] = {
 110                "Zero W",
 111                DTB_DIR "bcm2835-rpi-zero-w.dtb",
 112                false,
 113        },
 114};
 115
 116static const struct rpi_model rpi_models_old_scheme[] = {
 117        [0x2] = {
 118                "Model B",
 119                DTB_DIR "bcm2835-rpi-b.dtb",
 120                true,
 121        },
 122        [0x3] = {
 123                "Model B",
 124                DTB_DIR "bcm2835-rpi-b.dtb",
 125                true,
 126        },
 127        [0x4] = {
 128                "Model B rev2",
 129                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 130                true,
 131        },
 132        [0x5] = {
 133                "Model B rev2",
 134                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 135                true,
 136        },
 137        [0x6] = {
 138                "Model B rev2",
 139                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 140                true,
 141        },
 142        [0x7] = {
 143                "Model A",
 144                DTB_DIR "bcm2835-rpi-a.dtb",
 145                false,
 146        },
 147        [0x8] = {
 148                "Model A",
 149                DTB_DIR "bcm2835-rpi-a.dtb",
 150                false,
 151        },
 152        [0x9] = {
 153                "Model A",
 154                DTB_DIR "bcm2835-rpi-a.dtb",
 155                false,
 156        },
 157        [0xd] = {
 158                "Model B rev2",
 159                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 160                true,
 161        },
 162        [0xe] = {
 163                "Model B rev2",
 164                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 165                true,
 166        },
 167        [0xf] = {
 168                "Model B rev2",
 169                DTB_DIR "bcm2835-rpi-b-rev2.dtb",
 170                true,
 171        },
 172        [0x10] = {
 173                "Model B+",
 174                DTB_DIR "bcm2835-rpi-b-plus.dtb",
 175                true,
 176        },
 177        [0x11] = {
 178                "Compute Module",
 179                DTB_DIR "bcm2835-rpi-cm.dtb",
 180                false,
 181        },
 182        [0x12] = {
 183                "Model A+",
 184                DTB_DIR "bcm2835-rpi-a-plus.dtb",
 185                false,
 186        },
 187        [0x13] = {
 188                "Model B+",
 189                DTB_DIR "bcm2835-rpi-b-plus.dtb",
 190                true,
 191        },
 192        [0x14] = {
 193                "Compute Module",
 194                DTB_DIR "bcm2835-rpi-cm.dtb",
 195                false,
 196        },
 197        [0x15] = {
 198                "Model A+",
 199                DTB_DIR "bcm2835-rpi-a-plus.dtb",
 200                false,
 201        },
 202};
 203
 204static uint32_t revision;
 205static uint32_t rev_scheme;
 206static uint32_t rev_type;
 207static const struct rpi_model *model;
 208
 209#ifdef CONFIG_ARM64
 210static struct mm_region bcm2837_mem_map[] = {
 211        {
 212                .virt = 0x00000000UL,
 213                .phys = 0x00000000UL,
 214                .size = 0x3f000000UL,
 215                .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
 216                         PTE_BLOCK_INNER_SHARE
 217        }, {
 218                .virt = 0x3f000000UL,
 219                .phys = 0x3f000000UL,
 220                .size = 0x01000000UL,
 221                .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
 222                         PTE_BLOCK_NON_SHARE |
 223                         PTE_BLOCK_PXN | PTE_BLOCK_UXN
 224        }, {
 225                /* List terminator */
 226                0,
 227        }
 228};
 229
 230struct mm_region *mem_map = bcm2837_mem_map;
 231#endif
 232
 233int dram_init(void)
 234{
 235        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
 236        int ret;
 237
 238        BCM2835_MBOX_INIT_HDR(msg);
 239        BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
 240
 241        ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
 242        if (ret) {
 243                printf("bcm2835: Could not query ARM memory size\n");
 244                return -1;
 245        }
 246
 247        gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
 248
 249        return 0;
 250}
 251
 252static void set_fdtfile(void)
 253{
 254        const char *fdtfile;
 255
 256        if (env_get("fdtfile"))
 257                return;
 258
 259        fdtfile = model->fdtfile;
 260        env_set("fdtfile", fdtfile);
 261}
 262
 263/*
 264 * If the firmware provided a valid FDT at boot time, let's expose it in
 265 * ${fdt_addr} so it may be passed unmodified to the kernel.
 266 */
 267static void set_fdt_addr(void)
 268{
 269        if (env_get("fdt_addr"))
 270                return;
 271
 272        if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
 273                return;
 274
 275        env_set_hex("fdt_addr", fw_dtb_pointer);
 276}
 277
 278/*
 279 * Prevent relocation from stomping on a firmware provided FDT blob.
 280 */
 281unsigned long board_get_usable_ram_top(unsigned long total_size)
 282{
 283        if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
 284                return gd->ram_top;
 285        return fw_dtb_pointer & ~0xffff;
 286}
 287
 288static void set_usbethaddr(void)
 289{
 290        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
 291        int ret;
 292
 293        if (!model->has_onboard_eth)
 294                return;
 295
 296        if (env_get("usbethaddr"))
 297                return;
 298
 299        BCM2835_MBOX_INIT_HDR(msg);
 300        BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
 301
 302        ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
 303        if (ret) {
 304                printf("bcm2835: Could not query MAC address\n");
 305                /* Ignore error; not critical */
 306                return;
 307        }
 308
 309        eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
 310
 311        if (!env_get("ethaddr"))
 312                env_set("ethaddr", env_get("usbethaddr"));
 313
 314        return;
 315}
 316
 317#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
 318static void set_board_info(void)
 319{
 320        char s[11];
 321
 322        snprintf(s, sizeof(s), "0x%X", revision);
 323        env_set("board_revision", s);
 324        snprintf(s, sizeof(s), "%d", rev_scheme);
 325        env_set("board_rev_scheme", s);
 326        /* Can't rename this to board_rev_type since it's an ABI for scripts */
 327        snprintf(s, sizeof(s), "0x%X", rev_type);
 328        env_set("board_rev", s);
 329        env_set("board_name", model->name);
 330}
 331#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
 332
 333static void set_serial_number(void)
 334{
 335        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
 336        int ret;
 337        char serial_string[17] = { 0 };
 338
 339        if (env_get("serial#"))
 340                return;
 341
 342        BCM2835_MBOX_INIT_HDR(msg);
 343        BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
 344
 345        ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
 346        if (ret) {
 347                printf("bcm2835: Could not query board serial\n");
 348                /* Ignore error; not critical */
 349                return;
 350        }
 351
 352        snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
 353                 msg->get_board_serial.body.resp.serial);
 354        env_set("serial#", serial_string);
 355}
 356
 357int misc_init_r(void)
 358{
 359        set_fdt_addr();
 360        set_fdtfile();
 361        set_usbethaddr();
 362#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
 363        set_board_info();
 364#endif
 365        set_serial_number();
 366
 367        return 0;
 368}
 369
 370static void get_board_rev(void)
 371{
 372        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
 373        int ret;
 374        const struct rpi_model *models;
 375        uint32_t models_count;
 376
 377        BCM2835_MBOX_INIT_HDR(msg);
 378        BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
 379
 380        ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
 381        if (ret) {
 382                printf("bcm2835: Could not query board revision\n");
 383                /* Ignore error; not critical */
 384                return;
 385        }
 386
 387        /*
 388         * For details of old-vs-new scheme, see:
 389         * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
 390         * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
 391         * (a few posts down)
 392         *
 393         * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
 394         * lower byte to use as the board rev:
 395         * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
 396         * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
 397         */
 398        revision = msg->get_board_rev.body.resp.rev;
 399        if (revision & 0x800000) {
 400                rev_scheme = 1;
 401                rev_type = (revision >> 4) & 0xff;
 402                models = rpi_models_new_scheme;
 403                models_count = ARRAY_SIZE(rpi_models_new_scheme);
 404        } else {
 405                rev_scheme = 0;
 406                rev_type = revision & 0xff;
 407                models = rpi_models_old_scheme;
 408                models_count = ARRAY_SIZE(rpi_models_old_scheme);
 409        }
 410        if (rev_type >= models_count) {
 411                printf("RPI: Board rev 0x%x outside known range\n", rev_type);
 412                model = &rpi_model_unknown;
 413        } else if (!models[rev_type].name) {
 414                printf("RPI: Board rev 0x%x unknown\n", rev_type);
 415                model = &rpi_model_unknown;
 416        } else {
 417                model = &models[rev_type];
 418        }
 419
 420        printf("RPI %s (0x%x)\n", model->name, revision);
 421}
 422
 423int board_init(void)
 424{
 425#ifdef CONFIG_HW_WATCHDOG
 426        hw_watchdog_init();
 427#endif
 428
 429        get_board_rev();
 430
 431        gd->bd->bi_boot_params = 0x100;
 432
 433        return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 434}
 435
 436/*
 437 * If the firmware passed a device tree use it for U-Boot.
 438 */
 439void *board_fdt_blob_setup(void)
 440{
 441        if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
 442                return NULL;
 443        return (void *)fw_dtb_pointer;
 444}
 445
 446int ft_board_setup(void *blob, bd_t *bd)
 447{
 448        /*
 449         * For now, we simply always add the simplefb DT node. Later, we
 450         * should be more intelligent, and e.g. only do this if no enabled DT
 451         * node exists for the "real" graphics driver.
 452         */
 453        lcd_dt_simplefb_add_node(blob);
 454
 455#ifdef CONFIG_EFI_LOADER
 456        /* Reserve the spin table */
 457        efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
 458#endif
 459
 460        return 0;
 461}
 462