rpi.c 10 KB

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