rpi.c 9.9 KB

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