rpi.c 9.7 KB

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