rpi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. };
  149. static const struct rpi_model rpi_models_old_scheme[] = {
  150. [0x2] = {
  151. "Model B",
  152. DTB_DIR "bcm2835-rpi-b.dtb",
  153. true,
  154. },
  155. [0x3] = {
  156. "Model B",
  157. DTB_DIR "bcm2835-rpi-b.dtb",
  158. true,
  159. },
  160. [0x4] = {
  161. "Model B rev2",
  162. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  163. true,
  164. },
  165. [0x5] = {
  166. "Model B rev2",
  167. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  168. true,
  169. },
  170. [0x6] = {
  171. "Model B rev2",
  172. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  173. true,
  174. },
  175. [0x7] = {
  176. "Model A",
  177. DTB_DIR "bcm2835-rpi-a.dtb",
  178. false,
  179. },
  180. [0x8] = {
  181. "Model A",
  182. DTB_DIR "bcm2835-rpi-a.dtb",
  183. false,
  184. },
  185. [0x9] = {
  186. "Model A",
  187. DTB_DIR "bcm2835-rpi-a.dtb",
  188. false,
  189. },
  190. [0xd] = {
  191. "Model B rev2",
  192. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  193. true,
  194. },
  195. [0xe] = {
  196. "Model B rev2",
  197. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  198. true,
  199. },
  200. [0xf] = {
  201. "Model B rev2",
  202. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  203. true,
  204. },
  205. [0x10] = {
  206. "Model B+",
  207. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  208. true,
  209. },
  210. [0x11] = {
  211. "Compute Module",
  212. DTB_DIR "bcm2835-rpi-cm.dtb",
  213. false,
  214. },
  215. [0x12] = {
  216. "Model A+",
  217. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  218. false,
  219. },
  220. [0x13] = {
  221. "Model B+",
  222. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  223. true,
  224. },
  225. [0x14] = {
  226. "Compute Module",
  227. DTB_DIR "bcm2835-rpi-cm.dtb",
  228. false,
  229. },
  230. [0x15] = {
  231. "Model A+",
  232. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  233. false,
  234. },
  235. };
  236. static uint32_t revision;
  237. static uint32_t rev_scheme;
  238. static uint32_t rev_type;
  239. static const struct rpi_model *model;
  240. int dram_init(void)
  241. {
  242. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
  243. int ret;
  244. BCM2835_MBOX_INIT_HDR(msg);
  245. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  246. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  247. if (ret) {
  248. printf("bcm2835: Could not query ARM memory size\n");
  249. return -1;
  250. }
  251. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  252. return 0;
  253. }
  254. #ifdef CONFIG_OF_BOARD
  255. #ifdef CONFIG_BCM2711
  256. int dram_init_banksize(void)
  257. {
  258. return fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
  259. (phys_size_t *)&gd->ram_size, gd->bd);
  260. }
  261. #endif
  262. #endif
  263. static void set_fdtfile(void)
  264. {
  265. const char *fdtfile;
  266. if (env_get("fdtfile"))
  267. return;
  268. fdtfile = model->fdtfile;
  269. env_set("fdtfile", fdtfile);
  270. }
  271. /*
  272. * If the firmware provided a valid FDT at boot time, let's expose it in
  273. * ${fdt_addr} so it may be passed unmodified to the kernel.
  274. */
  275. static void set_fdt_addr(void)
  276. {
  277. if (env_get("fdt_addr"))
  278. return;
  279. if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
  280. return;
  281. env_set_hex("fdt_addr", fw_dtb_pointer);
  282. }
  283. /*
  284. * Prevent relocation from stomping on a firmware provided FDT blob.
  285. */
  286. unsigned long board_get_usable_ram_top(unsigned long total_size)
  287. {
  288. if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
  289. return gd->ram_top;
  290. return fw_dtb_pointer & ~0xffff;
  291. }
  292. static void set_usbethaddr(void)
  293. {
  294. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
  295. int ret;
  296. if (!model->has_onboard_eth)
  297. return;
  298. if (env_get("usbethaddr"))
  299. return;
  300. BCM2835_MBOX_INIT_HDR(msg);
  301. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  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. eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  309. if (!env_get("ethaddr"))
  310. env_set("ethaddr", env_get("usbethaddr"));
  311. return;
  312. }
  313. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  314. static void set_board_info(void)
  315. {
  316. char s[11];
  317. snprintf(s, sizeof(s), "0x%X", revision);
  318. env_set("board_revision", s);
  319. snprintf(s, sizeof(s), "%d", rev_scheme);
  320. env_set("board_rev_scheme", s);
  321. /* Can't rename this to board_rev_type since it's an ABI for scripts */
  322. snprintf(s, sizeof(s), "0x%X", rev_type);
  323. env_set("board_rev", s);
  324. env_set("board_name", model->name);
  325. }
  326. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  327. static void set_serial_number(void)
  328. {
  329. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
  330. int ret;
  331. char serial_string[17] = { 0 };
  332. if (env_get("serial#"))
  333. return;
  334. BCM2835_MBOX_INIT_HDR(msg);
  335. BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
  336. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  337. if (ret) {
  338. printf("bcm2835: Could not query board serial\n");
  339. /* Ignore error; not critical */
  340. return;
  341. }
  342. snprintf(serial_string, sizeof(serial_string), "%016llx",
  343. msg->get_board_serial.body.resp.serial);
  344. env_set("serial#", serial_string);
  345. }
  346. int misc_init_r(void)
  347. {
  348. set_fdt_addr();
  349. set_fdtfile();
  350. set_usbethaddr();
  351. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  352. set_board_info();
  353. #endif
  354. set_serial_number();
  355. return 0;
  356. }
  357. static void get_board_rev(void)
  358. {
  359. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
  360. int ret;
  361. const struct rpi_model *models;
  362. uint32_t models_count;
  363. BCM2835_MBOX_INIT_HDR(msg);
  364. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  365. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  366. if (ret) {
  367. printf("bcm2835: Could not query board revision\n");
  368. /* Ignore error; not critical */
  369. return;
  370. }
  371. /*
  372. * For details of old-vs-new scheme, see:
  373. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  374. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  375. * (a few posts down)
  376. *
  377. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  378. * lower byte to use as the board rev:
  379. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  380. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  381. */
  382. revision = msg->get_board_rev.body.resp.rev;
  383. if (revision & 0x800000) {
  384. rev_scheme = 1;
  385. rev_type = (revision >> 4) & 0xff;
  386. models = rpi_models_new_scheme;
  387. models_count = ARRAY_SIZE(rpi_models_new_scheme);
  388. } else {
  389. rev_scheme = 0;
  390. rev_type = revision & 0xff;
  391. models = rpi_models_old_scheme;
  392. models_count = ARRAY_SIZE(rpi_models_old_scheme);
  393. }
  394. if (rev_type >= models_count) {
  395. printf("RPI: Board rev 0x%x outside known range\n", rev_type);
  396. model = &rpi_model_unknown;
  397. } else if (!models[rev_type].name) {
  398. printf("RPI: Board rev 0x%x unknown\n", rev_type);
  399. model = &rpi_model_unknown;
  400. } else {
  401. model = &models[rev_type];
  402. }
  403. printf("RPI %s (0x%x)\n", model->name, revision);
  404. }
  405. int board_init(void)
  406. {
  407. #ifdef CONFIG_HW_WATCHDOG
  408. hw_watchdog_init();
  409. #endif
  410. get_board_rev();
  411. gd->bd->bi_boot_params = 0x100;
  412. return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  413. }
  414. /*
  415. * If the firmware passed a device tree use it for U-Boot.
  416. */
  417. void *board_fdt_blob_setup(void)
  418. {
  419. if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
  420. return NULL;
  421. return (void *)fw_dtb_pointer;
  422. }
  423. int ft_board_setup(void *blob, bd_t *bd)
  424. {
  425. /*
  426. * For now, we simply always add the simplefb DT node. Later, we
  427. * should be more intelligent, and e.g. only do this if no enabled DT
  428. * node exists for the "real" graphics driver.
  429. */
  430. lcd_dt_simplefb_add_node(blob);
  431. #ifdef CONFIG_EFI_LOADER
  432. /* Reserve the spin table */
  433. efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
  434. #endif
  435. return 0;
  436. }