rpi.c 10 KB

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