rpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2012-2016 Stephen Warren
  4. */
  5. #include <common.h>
  6. #include <config.h>
  7. #include <dm.h>
  8. #include <env.h>
  9. #include <efi_loader.h>
  10. #include <fdt_support.h>
  11. #include <fdt_simplefb.h>
  12. #include <init.h>
  13. #include <memalign.h>
  14. #include <mmc.h>
  15. #include <asm/gpio.h>
  16. #include <asm/arch/mbox.h>
  17. #include <asm/arch/msg.h>
  18. #include <asm/arch/sdhci.h>
  19. #include <asm/global_data.h>
  20. #include <dm/platform_data/serial_bcm283x_mu.h>
  21. #ifdef CONFIG_ARM64
  22. #include <asm/armv8/mmu.h>
  23. #endif
  24. #include <watchdog.h>
  25. #include <dm/pinctrl.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. /* Assigned in lowlevel_init.S
  28. * Push the variable into the .data section so that it
  29. * does not get cleared later.
  30. */
  31. unsigned long __section(".data") fw_dtb_pointer;
  32. /* TODO(sjg@chromium.org): Move these to the msg.c file */
  33. struct msg_get_arm_mem {
  34. struct bcm2835_mbox_hdr hdr;
  35. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  36. u32 end_tag;
  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. struct msg_get_board_serial {
  44. struct bcm2835_mbox_hdr hdr;
  45. struct bcm2835_mbox_tag_get_board_serial get_board_serial;
  46. u32 end_tag;
  47. };
  48. struct msg_get_mac_address {
  49. struct bcm2835_mbox_hdr hdr;
  50. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  51. u32 end_tag;
  52. };
  53. struct msg_get_clock_rate {
  54. struct bcm2835_mbox_hdr hdr;
  55. struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  56. u32 end_tag;
  57. };
  58. #ifdef CONFIG_ARM64
  59. #define DTB_DIR "broadcom/"
  60. #else
  61. #define DTB_DIR ""
  62. #endif
  63. /*
  64. * https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
  65. */
  66. struct rpi_model {
  67. const char *name;
  68. const char *fdtfile;
  69. bool has_onboard_eth;
  70. };
  71. static const struct rpi_model rpi_model_unknown = {
  72. "Unknown model",
  73. DTB_DIR "bcm283x-rpi-other.dtb",
  74. false,
  75. };
  76. static const struct rpi_model rpi_models_new_scheme[] = {
  77. [0x0] = {
  78. "Model A",
  79. DTB_DIR "bcm2835-rpi-a.dtb",
  80. false,
  81. },
  82. [0x1] = {
  83. "Model B",
  84. DTB_DIR "bcm2835-rpi-b.dtb",
  85. true,
  86. },
  87. [0x2] = {
  88. "Model A+",
  89. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  90. false,
  91. },
  92. [0x3] = {
  93. "Model B+",
  94. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  95. true,
  96. },
  97. [0x4] = {
  98. "2 Model B",
  99. DTB_DIR "bcm2836-rpi-2-b.dtb",
  100. true,
  101. },
  102. [0x6] = {
  103. "Compute Module",
  104. DTB_DIR "bcm2835-rpi-cm.dtb",
  105. false,
  106. },
  107. [0x8] = {
  108. "3 Model B",
  109. DTB_DIR "bcm2837-rpi-3-b.dtb",
  110. true,
  111. },
  112. [0x9] = {
  113. "Zero",
  114. DTB_DIR "bcm2835-rpi-zero.dtb",
  115. false,
  116. },
  117. [0xA] = {
  118. "Compute Module 3",
  119. DTB_DIR "bcm2837-rpi-cm3.dtb",
  120. false,
  121. },
  122. [0xC] = {
  123. "Zero W",
  124. DTB_DIR "bcm2835-rpi-zero-w.dtb",
  125. false,
  126. },
  127. [0xD] = {
  128. "3 Model B+",
  129. DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
  130. true,
  131. },
  132. [0xE] = {
  133. "3 Model A+",
  134. DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
  135. false,
  136. },
  137. [0x10] = {
  138. "Compute Module 3+",
  139. DTB_DIR "bcm2837-rpi-cm3.dtb",
  140. false,
  141. },
  142. [0x11] = {
  143. "4 Model B",
  144. DTB_DIR "bcm2711-rpi-4-b.dtb",
  145. true,
  146. },
  147. [0x12] = {
  148. "Zero 2 W",
  149. DTB_DIR "bcm2837-rpi-zero-2-w.dtb",
  150. false,
  151. },
  152. [0x13] = {
  153. "400",
  154. DTB_DIR "bcm2711-rpi-400.dtb",
  155. true,
  156. },
  157. [0x14] = {
  158. "Compute Module 4",
  159. DTB_DIR "bcm2711-rpi-cm4.dtb",
  160. true,
  161. },
  162. };
  163. static const struct rpi_model rpi_models_old_scheme[] = {
  164. [0x2] = {
  165. "Model B",
  166. DTB_DIR "bcm2835-rpi-b.dtb",
  167. true,
  168. },
  169. [0x3] = {
  170. "Model B",
  171. DTB_DIR "bcm2835-rpi-b.dtb",
  172. true,
  173. },
  174. [0x4] = {
  175. "Model B rev2",
  176. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  177. true,
  178. },
  179. [0x5] = {
  180. "Model B rev2",
  181. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  182. true,
  183. },
  184. [0x6] = {
  185. "Model B rev2",
  186. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  187. true,
  188. },
  189. [0x7] = {
  190. "Model A",
  191. DTB_DIR "bcm2835-rpi-a.dtb",
  192. false,
  193. },
  194. [0x8] = {
  195. "Model A",
  196. DTB_DIR "bcm2835-rpi-a.dtb",
  197. false,
  198. },
  199. [0x9] = {
  200. "Model A",
  201. DTB_DIR "bcm2835-rpi-a.dtb",
  202. false,
  203. },
  204. [0xd] = {
  205. "Model B rev2",
  206. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  207. true,
  208. },
  209. [0xe] = {
  210. "Model B rev2",
  211. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  212. true,
  213. },
  214. [0xf] = {
  215. "Model B rev2",
  216. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  217. true,
  218. },
  219. [0x10] = {
  220. "Model B+",
  221. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  222. true,
  223. },
  224. [0x11] = {
  225. "Compute Module",
  226. DTB_DIR "bcm2835-rpi-cm.dtb",
  227. false,
  228. },
  229. [0x12] = {
  230. "Model A+",
  231. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  232. false,
  233. },
  234. [0x13] = {
  235. "Model B+",
  236. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  237. true,
  238. },
  239. [0x14] = {
  240. "Compute Module",
  241. DTB_DIR "bcm2835-rpi-cm.dtb",
  242. false,
  243. },
  244. [0x15] = {
  245. "Model A+",
  246. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  247. false,
  248. },
  249. };
  250. static uint32_t revision;
  251. static uint32_t rev_scheme;
  252. static uint32_t rev_type;
  253. static const struct rpi_model *model;
  254. int dram_init(void)
  255. {
  256. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
  257. int ret;
  258. BCM2835_MBOX_INIT_HDR(msg);
  259. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  260. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  261. if (ret) {
  262. printf("bcm2835: Could not query ARM memory size\n");
  263. return -1;
  264. }
  265. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  266. /*
  267. * In some configurations the memory size returned by VideoCore
  268. * is not aligned to the section size, what is mandatory for
  269. * the u-boot's memory setup.
  270. */
  271. gd->ram_size &= ~MMU_SECTION_SIZE;
  272. return 0;
  273. }
  274. #ifdef CONFIG_OF_BOARD
  275. int dram_init_banksize(void)
  276. {
  277. int ret;
  278. ret = fdtdec_setup_memory_banksize();
  279. if (ret)
  280. return ret;
  281. return fdtdec_setup_mem_size_base();
  282. }
  283. #endif
  284. static void set_fdtfile(void)
  285. {
  286. const char *fdtfile;
  287. if (env_get("fdtfile"))
  288. return;
  289. fdtfile = model->fdtfile;
  290. env_set("fdtfile", fdtfile);
  291. }
  292. /*
  293. * If the firmware provided a valid FDT at boot time, let's expose it in
  294. * ${fdt_addr} so it may be passed unmodified to the kernel.
  295. */
  296. static void set_fdt_addr(void)
  297. {
  298. if (env_get("fdt_addr"))
  299. return;
  300. if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
  301. return;
  302. env_set_hex("fdt_addr", fw_dtb_pointer);
  303. }
  304. /*
  305. * Prevent relocation from stomping on a firmware provided FDT blob.
  306. */
  307. phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
  308. {
  309. if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
  310. return gd->ram_top;
  311. return fw_dtb_pointer & ~0xffff;
  312. }
  313. static void set_usbethaddr(void)
  314. {
  315. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
  316. int ret;
  317. if (!model->has_onboard_eth)
  318. return;
  319. if (env_get("usbethaddr"))
  320. return;
  321. BCM2835_MBOX_INIT_HDR(msg);
  322. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  323. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  324. if (ret) {
  325. printf("bcm2835: Could not query MAC address\n");
  326. /* Ignore error; not critical */
  327. return;
  328. }
  329. eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  330. if (!env_get("ethaddr"))
  331. env_set("ethaddr", env_get("usbethaddr"));
  332. return;
  333. }
  334. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  335. static void set_board_info(void)
  336. {
  337. char s[11];
  338. snprintf(s, sizeof(s), "0x%X", revision);
  339. env_set("board_revision", s);
  340. snprintf(s, sizeof(s), "%d", rev_scheme);
  341. env_set("board_rev_scheme", s);
  342. /* Can't rename this to board_rev_type since it's an ABI for scripts */
  343. snprintf(s, sizeof(s), "0x%X", rev_type);
  344. env_set("board_rev", s);
  345. env_set("board_name", model->name);
  346. }
  347. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  348. static void set_serial_number(void)
  349. {
  350. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
  351. int ret;
  352. char serial_string[17] = { 0 };
  353. if (env_get("serial#"))
  354. return;
  355. BCM2835_MBOX_INIT_HDR(msg);
  356. BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
  357. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  358. if (ret) {
  359. printf("bcm2835: Could not query board serial\n");
  360. /* Ignore error; not critical */
  361. return;
  362. }
  363. snprintf(serial_string, sizeof(serial_string), "%016llx",
  364. msg->get_board_serial.body.resp.serial);
  365. env_set("serial#", serial_string);
  366. }
  367. int misc_init_r(void)
  368. {
  369. set_fdt_addr();
  370. set_fdtfile();
  371. set_usbethaddr();
  372. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  373. set_board_info();
  374. #endif
  375. set_serial_number();
  376. return 0;
  377. }
  378. static void get_board_revision(void)
  379. {
  380. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
  381. int ret;
  382. const struct rpi_model *models;
  383. uint32_t models_count;
  384. BCM2835_MBOX_INIT_HDR(msg);
  385. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  386. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  387. if (ret) {
  388. printf("bcm2835: Could not query board revision\n");
  389. /* Ignore error; not critical */
  390. return;
  391. }
  392. /*
  393. * For details of old-vs-new scheme, see:
  394. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  395. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  396. * (a few posts down)
  397. *
  398. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  399. * lower byte to use as the board rev:
  400. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  401. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  402. */
  403. revision = msg->get_board_rev.body.resp.rev;
  404. if (revision & 0x800000) {
  405. rev_scheme = 1;
  406. rev_type = (revision >> 4) & 0xff;
  407. models = rpi_models_new_scheme;
  408. models_count = ARRAY_SIZE(rpi_models_new_scheme);
  409. } else {
  410. rev_scheme = 0;
  411. rev_type = revision & 0xff;
  412. models = rpi_models_old_scheme;
  413. models_count = ARRAY_SIZE(rpi_models_old_scheme);
  414. }
  415. if (rev_type >= models_count) {
  416. printf("RPI: Board rev 0x%x outside known range\n", rev_type);
  417. model = &rpi_model_unknown;
  418. } else if (!models[rev_type].name) {
  419. printf("RPI: Board rev 0x%x unknown\n", rev_type);
  420. model = &rpi_model_unknown;
  421. } else {
  422. model = &models[rev_type];
  423. }
  424. printf("RPI %s (0x%x)\n", model->name, revision);
  425. }
  426. int board_init(void)
  427. {
  428. #ifdef CONFIG_HW_WATCHDOG
  429. hw_watchdog_init();
  430. #endif
  431. get_board_revision();
  432. gd->bd->bi_boot_params = 0x100;
  433. return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  434. }
  435. /*
  436. * If the firmware passed a device tree use it for U-Boot.
  437. */
  438. void *board_fdt_blob_setup(int *err)
  439. {
  440. *err = 0;
  441. if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC) {
  442. *err = -ENXIO;
  443. return NULL;
  444. }
  445. return (void *)fw_dtb_pointer;
  446. }
  447. int copy_property(void *dst, void *src, char *path, char *property)
  448. {
  449. int dst_offset, src_offset;
  450. const fdt32_t *prop;
  451. int len;
  452. src_offset = fdt_path_offset(src, path);
  453. dst_offset = fdt_path_offset(dst, path);
  454. if (src_offset < 0 || dst_offset < 0)
  455. return -1;
  456. prop = fdt_getprop(src, src_offset, property, &len);
  457. if (!prop)
  458. return -1;
  459. return fdt_setprop(dst, dst_offset, property, prop, len);
  460. }
  461. /* Copy tweaks from the firmware dtb to the loaded dtb */
  462. void update_fdt_from_fw(void *fdt, void *fw_fdt)
  463. {
  464. /* Using dtb from firmware directly; leave it alone */
  465. if (fdt == fw_fdt)
  466. return;
  467. /* The firmware provides a more precie model; so copy that */
  468. copy_property(fdt, fw_fdt, "/", "model");
  469. /* memory reserve as suggested by the firmware */
  470. copy_property(fdt, fw_fdt, "/", "memreserve");
  471. /* Adjust dma-ranges for the SD card and PCI bus as they can depend on
  472. * the SoC revision
  473. */
  474. copy_property(fdt, fw_fdt, "emmc2bus", "dma-ranges");
  475. copy_property(fdt, fw_fdt, "pcie0", "dma-ranges");
  476. /* Bootloader configuration template exposes as nvmem */
  477. if (copy_property(fdt, fw_fdt, "blconfig", "reg") == 0)
  478. copy_property(fdt, fw_fdt, "blconfig", "status");
  479. /* kernel address randomisation seed as provided by the firmware */
  480. copy_property(fdt, fw_fdt, "/chosen", "kaslr-seed");
  481. /* address of the PHY device as provided by the firmware */
  482. copy_property(fdt, fw_fdt, "ethernet0/mdio@e14/ethernet-phy@1", "reg");
  483. }
  484. int ft_board_setup(void *blob, struct bd_info *bd)
  485. {
  486. int node;
  487. update_fdt_from_fw(blob, (void *)fw_dtb_pointer);
  488. node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
  489. if (node < 0)
  490. fdt_simplefb_add_node(blob);
  491. else
  492. fdt_simplefb_enable_and_mem_rsv(blob);
  493. #ifdef CONFIG_EFI_LOADER
  494. /* Reserve the spin table */
  495. efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
  496. EFI_RESERVED_MEMORY_TYPE);
  497. #endif
  498. return 0;
  499. }