rpi.c 10 KB

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