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/sdhci.h> 20 #include <asm/global_data.h> 21 #include <dm/platform_data/serial_bcm283x_mu.h> 22 #ifdef CONFIG_ARM64 23 #include <asm/armv8/mmu.h> 24 #endif 25 26 DECLARE_GLOBAL_DATA_PTR; 27 28 /* From lowlevel_init.S */ 29 extern unsigned long fw_dtb_pointer; 30 31 32 struct msg_get_arm_mem { 33 struct bcm2835_mbox_hdr hdr; 34 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem; 35 u32 end_tag; 36 }; 37 38 struct msg_get_board_rev { 39 struct bcm2835_mbox_hdr hdr; 40 struct bcm2835_mbox_tag_get_board_rev get_board_rev; 41 u32 end_tag; 42 }; 43 44 struct msg_get_board_serial { 45 struct bcm2835_mbox_hdr hdr; 46 struct bcm2835_mbox_tag_get_board_serial get_board_serial; 47 u32 end_tag; 48 }; 49 50 struct msg_get_mac_address { 51 struct bcm2835_mbox_hdr hdr; 52 struct bcm2835_mbox_tag_get_mac_address get_mac_address; 53 u32 end_tag; 54 }; 55 56 struct msg_set_power_state { 57 struct bcm2835_mbox_hdr hdr; 58 struct bcm2835_mbox_tag_set_power_state set_power_state; 59 u32 end_tag; 60 }; 61 62 struct msg_get_clock_rate { 63 struct bcm2835_mbox_hdr hdr; 64 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate; 65 u32 end_tag; 66 }; 67 68 #ifdef CONFIG_ARM64 69 #define DTB_DIR "broadcom/" 70 #else 71 #define DTB_DIR "" 72 #endif 73 74 /* 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 78 * 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 83 */ 84 struct rpi_model { 85 const char *name; 86 const char *fdtfile; 87 bool has_onboard_eth; 88 }; 89 90 static const struct rpi_model rpi_model_unknown = { 91 "Unknown model", 92 DTB_DIR "bcm283x-rpi-other.dtb", 93 false, 94 }; 95 96 static const struct rpi_model rpi_models_new_scheme[] = { 97 [0x4] = { 98 "2 Model B", 99 DTB_DIR "bcm2836-rpi-2-b.dtb", 100 true, 101 }, 102 [0x8] = { 103 "3 Model B", 104 DTB_DIR "bcm2837-rpi-3-b.dtb", 105 true, 106 }, 107 [0x9] = { 108 "Zero", 109 DTB_DIR "bcm2835-rpi-zero.dtb", 110 false, 111 }, 112 }; 113 114 static const struct rpi_model rpi_models_old_scheme[] = { 115 [0x2] = { 116 "Model B", 117 DTB_DIR "bcm2835-rpi-b.dtb", 118 true, 119 }, 120 [0x3] = { 121 "Model B", 122 DTB_DIR "bcm2835-rpi-b.dtb", 123 true, 124 }, 125 [0x4] = { 126 "Model B rev2", 127 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 128 true, 129 }, 130 [0x5] = { 131 "Model B rev2", 132 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 133 true, 134 }, 135 [0x6] = { 136 "Model B rev2", 137 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 138 true, 139 }, 140 [0x7] = { 141 "Model A", 142 DTB_DIR "bcm2835-rpi-a.dtb", 143 false, 144 }, 145 [0x8] = { 146 "Model A", 147 DTB_DIR "bcm2835-rpi-a.dtb", 148 false, 149 }, 150 [0x9] = { 151 "Model A", 152 DTB_DIR "bcm2835-rpi-a.dtb", 153 false, 154 }, 155 [0xd] = { 156 "Model B rev2", 157 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 158 true, 159 }, 160 [0xe] = { 161 "Model B rev2", 162 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 163 true, 164 }, 165 [0xf] = { 166 "Model B rev2", 167 DTB_DIR "bcm2835-rpi-b-rev2.dtb", 168 true, 169 }, 170 [0x10] = { 171 "Model B+", 172 DTB_DIR "bcm2835-rpi-b-plus.dtb", 173 true, 174 }, 175 [0x11] = { 176 "Compute Module", 177 DTB_DIR "bcm2835-rpi-cm.dtb", 178 false, 179 }, 180 [0x12] = { 181 "Model A+", 182 DTB_DIR "bcm2835-rpi-a-plus.dtb", 183 false, 184 }, 185 [0x13] = { 186 "Model B+", 187 DTB_DIR "bcm2835-rpi-b-plus.dtb", 188 true, 189 }, 190 [0x14] = { 191 "Compute Module", 192 DTB_DIR "bcm2835-rpi-cm.dtb", 193 false, 194 }, 195 [0x15] = { 196 "Model A+", 197 DTB_DIR "bcm2835-rpi-a-plus.dtb", 198 false, 199 }, 200 }; 201 202 static uint32_t revision; 203 static uint32_t rev_scheme; 204 static uint32_t rev_type; 205 static const struct rpi_model *model; 206 207 #ifdef CONFIG_ARM64 208 static struct mm_region bcm2837_mem_map[] = { 209 { 210 .virt = 0x00000000UL, 211 .phys = 0x00000000UL, 212 .size = 0x3f000000UL, 213 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | 214 PTE_BLOCK_INNER_SHARE 215 }, { 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 222 }, { 223 /* List terminator */ 224 0, 225 } 226 }; 227 228 struct mm_region *mem_map = bcm2837_mem_map; 229 #endif 230 231 int dram_init(void) 232 { 233 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1); 234 int ret; 235 236 BCM2835_MBOX_INIT_HDR(msg); 237 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY); 238 239 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr); 240 if (ret) { 241 printf("bcm2835: Could not query ARM memory size\n"); 242 return -1; 243 } 244 245 gd->ram_size = msg->get_arm_mem.body.resp.mem_size; 246 247 return 0; 248 } 249 250 static void set_fdtfile(void) 251 { 252 const char *fdtfile; 253 254 if (getenv("fdtfile")) 255 return; 256 257 fdtfile = model->fdtfile; 258 setenv("fdtfile", fdtfile); 259 } 260 261 /* 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. 264 */ 265 static void set_fdt_addr(void) 266 { 267 if (getenv("fdt_addr")) 268 return; 269 270 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC) 271 return; 272 273 setenv_hex("fdt_addr", fw_dtb_pointer); 274 } 275 276 /* 277 * Prevent relocation from stomping on a firmware provided FDT blob. 278 */ 279 unsigned long board_get_usable_ram_top(unsigned long total_size) 280 { 281 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M) 282 return gd->ram_top; 283 return fw_dtb_pointer & ~0xffff; 284 } 285 286 static void set_usbethaddr(void) 287 { 288 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1); 289 int ret; 290 291 if (!model->has_onboard_eth) 292 return; 293 294 if (getenv("usbethaddr")) 295 return; 296 297 BCM2835_MBOX_INIT_HDR(msg); 298 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS); 299 300 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr); 301 if (ret) { 302 printf("bcm2835: Could not query MAC address\n"); 303 /* Ignore error; not critical */ 304 return; 305 } 306 307 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac); 308 309 if (!getenv("ethaddr")) 310 setenv("ethaddr", getenv("usbethaddr")); 311 312 return; 313 } 314 315 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG 316 static void set_board_info(void) 317 { 318 char s[11]; 319 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); 328 } 329 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */ 330 331 static void set_serial_number(void) 332 { 333 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1); 334 int ret; 335 char serial_string[17] = { 0 }; 336 337 if (getenv("serial#")) 338 return; 339 340 BCM2835_MBOX_INIT_HDR(msg); 341 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL); 342 343 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr); 344 if (ret) { 345 printf("bcm2835: Could not query board serial\n"); 346 /* Ignore error; not critical */ 347 return; 348 } 349 350 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64, 351 msg->get_board_serial.body.resp.serial); 352 setenv("serial#", serial_string); 353 } 354 355 int misc_init_r(void) 356 { 357 set_fdt_addr(); 358 set_fdtfile(); 359 set_usbethaddr(); 360 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG 361 set_board_info(); 362 #endif 363 set_serial_number(); 364 365 return 0; 366 } 367 368 static int power_on_module(u32 module) 369 { 370 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1); 371 int ret; 372 373 BCM2835_MBOX_INIT_HDR(msg_pwr); 374 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state, 375 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; 380 381 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, 382 &msg_pwr->hdr); 383 if (ret) { 384 printf("bcm2835: Could not set module %u power state\n", 385 module); 386 return -1; 387 } 388 389 return 0; 390 } 391 392 static void get_board_rev(void) 393 { 394 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1); 395 int ret; 396 const struct rpi_model *models; 397 uint32_t models_count; 398 399 BCM2835_MBOX_INIT_HDR(msg); 400 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV); 401 402 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr); 403 if (ret) { 404 printf("bcm2835: Could not query board revision\n"); 405 /* Ignore error; not critical */ 406 return; 407 } 408 409 /* 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 413 * (a few posts down) 414 * 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 419 */ 420 revision = msg->get_board_rev.body.resp.rev; 421 if (revision & 0x800000) { 422 rev_scheme = 1; 423 rev_type = (revision >> 4) & 0xff; 424 models = rpi_models_new_scheme; 425 models_count = ARRAY_SIZE(rpi_models_new_scheme); 426 } else { 427 rev_scheme = 0; 428 rev_type = revision & 0xff; 429 models = rpi_models_old_scheme; 430 models_count = ARRAY_SIZE(rpi_models_old_scheme); 431 } 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; 438 } else { 439 model = &models[rev_type]; 440 } 441 442 printf("RPI %s (0x%x)\n", model->name, revision); 443 } 444 445 #ifndef CONFIG_PL01X_SERIAL 446 static bool rpi_is_serial_active(void) 447 { 448 int serial_gpio = 15; 449 struct udevice *dev; 450 451 /* 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. 454 */ 455 456 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev) 457 return true; 458 459 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5) 460 return false; 461 462 return true; 463 } 464 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 467 */ 468 static void rpi_disable_inactive_uart(void) 469 { 470 struct udevice *dev; 471 struct bcm283x_mu_serial_platdata *plat; 472 473 if (uclass_get_device_by_driver(UCLASS_SERIAL, 474 DM_GET_DRIVER(serial_bcm283x_mu), 475 &dev) || !dev) 476 return; 477 478 if (!rpi_is_serial_active()) { 479 plat = dev_get_platdata(dev); 480 plat->disabled = true; 481 } 482 } 483 #endif 484 485 int board_init(void) 486 { 487 #ifndef CONFIG_PL01X_SERIAL 488 rpi_disable_inactive_uart(); 489 #endif 490 491 get_board_rev(); 492 493 gd->bd->bi_boot_params = 0x100; 494 495 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD); 496 } 497 498 int board_mmc_init(bd_t *bis) 499 { 500 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1); 501 int ret; 502 503 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI); 504 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; 508 509 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr); 510 if (ret) { 511 printf("bcm2835: Could not query eMMC clock rate\n"); 512 return -1; 513 } 514 515 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE, 516 msg_clk->get_clock_rate.body.resp.rate_hz); 517 } 518 519 int ft_board_setup(void *blob, bd_t *bd) 520 { 521 /* 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. 525 */ 526 lcd_dt_simplefb_add_node(blob); 527 528 #ifdef CONFIG_EFI_LOADER 529 /* Reserve the spin table */ 530 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0); 531 #endif 532 533 return 0; 534 } 535