rpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/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. DECLARE_GLOBAL_DATA_PTR;
  25. /* From lowlevel_init.S */
  26. extern unsigned long fw_dtb_pointer;
  27. struct msg_get_arm_mem {
  28. struct bcm2835_mbox_hdr hdr;
  29. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  30. u32 end_tag;
  31. };
  32. struct msg_get_board_rev {
  33. struct bcm2835_mbox_hdr hdr;
  34. struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  35. u32 end_tag;
  36. };
  37. struct msg_get_board_serial {
  38. struct bcm2835_mbox_hdr hdr;
  39. struct bcm2835_mbox_tag_get_board_serial get_board_serial;
  40. u32 end_tag;
  41. };
  42. struct msg_get_mac_address {
  43. struct bcm2835_mbox_hdr hdr;
  44. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  45. u32 end_tag;
  46. };
  47. struct msg_set_power_state {
  48. struct bcm2835_mbox_hdr hdr;
  49. struct bcm2835_mbox_tag_set_power_state set_power_state;
  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. [0x4] = {
  84. "2 Model B",
  85. DTB_DIR "bcm2836-rpi-2-b.dtb",
  86. true,
  87. },
  88. [0x8] = {
  89. "3 Model B",
  90. DTB_DIR "bcm2837-rpi-3-b.dtb",
  91. true,
  92. },
  93. [0x9] = {
  94. "Zero",
  95. DTB_DIR "bcm2835-rpi-zero.dtb",
  96. false,
  97. },
  98. };
  99. static const struct rpi_model rpi_models_old_scheme[] = {
  100. [0x2] = {
  101. "Model B",
  102. DTB_DIR "bcm2835-rpi-b.dtb",
  103. true,
  104. },
  105. [0x3] = {
  106. "Model B",
  107. DTB_DIR "bcm2835-rpi-b.dtb",
  108. true,
  109. },
  110. [0x4] = {
  111. "Model B rev2",
  112. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  113. true,
  114. },
  115. [0x5] = {
  116. "Model B rev2",
  117. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  118. true,
  119. },
  120. [0x6] = {
  121. "Model B rev2",
  122. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  123. true,
  124. },
  125. [0x7] = {
  126. "Model A",
  127. DTB_DIR "bcm2835-rpi-a.dtb",
  128. false,
  129. },
  130. [0x8] = {
  131. "Model A",
  132. DTB_DIR "bcm2835-rpi-a.dtb",
  133. false,
  134. },
  135. [0x9] = {
  136. "Model A",
  137. DTB_DIR "bcm2835-rpi-a.dtb",
  138. false,
  139. },
  140. [0xd] = {
  141. "Model B rev2",
  142. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  143. true,
  144. },
  145. [0xe] = {
  146. "Model B rev2",
  147. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  148. true,
  149. },
  150. [0xf] = {
  151. "Model B rev2",
  152. DTB_DIR "bcm2835-rpi-b-rev2.dtb",
  153. true,
  154. },
  155. [0x10] = {
  156. "Model B+",
  157. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  158. true,
  159. },
  160. [0x11] = {
  161. "Compute Module",
  162. DTB_DIR "bcm2835-rpi-cm.dtb",
  163. false,
  164. },
  165. [0x12] = {
  166. "Model A+",
  167. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  168. false,
  169. },
  170. [0x13] = {
  171. "Model B+",
  172. DTB_DIR "bcm2835-rpi-b-plus.dtb",
  173. true,
  174. },
  175. [0x14] = {
  176. "Compute Module",
  177. DTB_DIR "bcm2835-rpi-cm.dtb",
  178. false,
  179. },
  180. [0x15] = {
  181. "Model A+",
  182. DTB_DIR "bcm2835-rpi-a-plus.dtb",
  183. false,
  184. },
  185. };
  186. static uint32_t revision;
  187. static uint32_t rev_scheme;
  188. static uint32_t rev_type;
  189. static const struct rpi_model *model;
  190. #ifdef CONFIG_ARM64
  191. static struct mm_region bcm2837_mem_map[] = {
  192. {
  193. .virt = 0x00000000UL,
  194. .phys = 0x00000000UL,
  195. .size = 0x3f000000UL,
  196. .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  197. PTE_BLOCK_INNER_SHARE
  198. }, {
  199. .virt = 0x3f000000UL,
  200. .phys = 0x3f000000UL,
  201. .size = 0x01000000UL,
  202. .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
  203. PTE_BLOCK_NON_SHARE |
  204. PTE_BLOCK_PXN | PTE_BLOCK_UXN
  205. }, {
  206. /* List terminator */
  207. 0,
  208. }
  209. };
  210. struct mm_region *mem_map = bcm2837_mem_map;
  211. #endif
  212. int dram_init(void)
  213. {
  214. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
  215. int ret;
  216. BCM2835_MBOX_INIT_HDR(msg);
  217. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  218. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  219. if (ret) {
  220. printf("bcm2835: Could not query ARM memory size\n");
  221. return -1;
  222. }
  223. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  224. return 0;
  225. }
  226. static void set_fdtfile(void)
  227. {
  228. const char *fdtfile;
  229. if (getenv("fdtfile"))
  230. return;
  231. fdtfile = model->fdtfile;
  232. setenv("fdtfile", fdtfile);
  233. }
  234. /*
  235. * If the firmware provided a valid FDT at boot time, let's expose it in
  236. * ${fdt_addr} so it may be passed unmodified to the kernel.
  237. */
  238. static void set_fdt_addr(void)
  239. {
  240. if (getenv("fdt_addr"))
  241. return;
  242. if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
  243. return;
  244. setenv_hex("fdt_addr", fw_dtb_pointer);
  245. }
  246. /*
  247. * Prevent relocation from stomping on a firmware provided FDT blob.
  248. */
  249. unsigned long board_get_usable_ram_top(unsigned long total_size)
  250. {
  251. if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
  252. return gd->ram_top;
  253. return fw_dtb_pointer & ~0xffff;
  254. }
  255. static void set_usbethaddr(void)
  256. {
  257. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
  258. int ret;
  259. if (!model->has_onboard_eth)
  260. return;
  261. if (getenv("usbethaddr"))
  262. return;
  263. BCM2835_MBOX_INIT_HDR(msg);
  264. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  265. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  266. if (ret) {
  267. printf("bcm2835: Could not query MAC address\n");
  268. /* Ignore error; not critical */
  269. return;
  270. }
  271. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  272. if (!getenv("ethaddr"))
  273. setenv("ethaddr", getenv("usbethaddr"));
  274. return;
  275. }
  276. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  277. static void set_board_info(void)
  278. {
  279. char s[11];
  280. snprintf(s, sizeof(s), "0x%X", revision);
  281. setenv("board_revision", s);
  282. snprintf(s, sizeof(s), "%d", rev_scheme);
  283. setenv("board_rev_scheme", s);
  284. /* Can't rename this to board_rev_type since it's an ABI for scripts */
  285. snprintf(s, sizeof(s), "0x%X", rev_type);
  286. setenv("board_rev", s);
  287. setenv("board_name", model->name);
  288. }
  289. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  290. static void set_serial_number(void)
  291. {
  292. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
  293. int ret;
  294. char serial_string[17] = { 0 };
  295. if (getenv("serial#"))
  296. return;
  297. BCM2835_MBOX_INIT_HDR(msg);
  298. BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
  299. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  300. if (ret) {
  301. printf("bcm2835: Could not query board serial\n");
  302. /* Ignore error; not critical */
  303. return;
  304. }
  305. snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
  306. msg->get_board_serial.body.resp.serial);
  307. setenv("serial#", serial_string);
  308. }
  309. int misc_init_r(void)
  310. {
  311. set_fdt_addr();
  312. set_fdtfile();
  313. set_usbethaddr();
  314. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  315. set_board_info();
  316. #endif
  317. set_serial_number();
  318. return 0;
  319. }
  320. static int power_on_module(u32 module)
  321. {
  322. ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
  323. int ret;
  324. BCM2835_MBOX_INIT_HDR(msg_pwr);
  325. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  326. SET_POWER_STATE);
  327. msg_pwr->set_power_state.body.req.device_id = module;
  328. msg_pwr->set_power_state.body.req.state =
  329. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  330. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  331. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  332. &msg_pwr->hdr);
  333. if (ret) {
  334. printf("bcm2835: Could not set module %u power state\n",
  335. module);
  336. return -1;
  337. }
  338. return 0;
  339. }
  340. static void get_board_rev(void)
  341. {
  342. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
  343. int ret;
  344. const struct rpi_model *models;
  345. uint32_t models_count;
  346. BCM2835_MBOX_INIT_HDR(msg);
  347. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  348. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  349. if (ret) {
  350. printf("bcm2835: Could not query board revision\n");
  351. /* Ignore error; not critical */
  352. return;
  353. }
  354. /*
  355. * For details of old-vs-new scheme, see:
  356. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  357. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  358. * (a few posts down)
  359. *
  360. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  361. * lower byte to use as the board rev:
  362. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  363. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  364. */
  365. revision = msg->get_board_rev.body.resp.rev;
  366. if (revision & 0x800000) {
  367. rev_scheme = 1;
  368. rev_type = (revision >> 4) & 0xff;
  369. models = rpi_models_new_scheme;
  370. models_count = ARRAY_SIZE(rpi_models_new_scheme);
  371. } else {
  372. rev_scheme = 0;
  373. rev_type = revision & 0xff;
  374. models = rpi_models_old_scheme;
  375. models_count = ARRAY_SIZE(rpi_models_old_scheme);
  376. }
  377. if (rev_type >= models_count) {
  378. printf("RPI: Board rev 0x%x outside known range\n", rev_type);
  379. model = &rpi_model_unknown;
  380. } else if (!models[rev_type].name) {
  381. printf("RPI: Board rev 0x%x unknown\n", rev_type);
  382. model = &rpi_model_unknown;
  383. } else {
  384. model = &models[rev_type];
  385. }
  386. printf("RPI %s (0x%x)\n", model->name, revision);
  387. }
  388. #ifndef CONFIG_PL01X_SERIAL
  389. static bool rpi_is_serial_active(void)
  390. {
  391. int serial_gpio = 15;
  392. struct udevice *dev;
  393. /*
  394. * The RPi3 disables the mini uart by default. The easiest way to find
  395. * out whether it is available is to check if the RX pin is muxed.
  396. */
  397. if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
  398. return true;
  399. if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
  400. return false;
  401. return true;
  402. }
  403. /* Disable mini-UART I/O if it's not pinmuxed to our pins.
  404. * The firmware only enables it if explicitly done in config.txt: enable_uart=1
  405. */
  406. static void rpi_disable_inactive_uart(void)
  407. {
  408. struct udevice *dev;
  409. struct bcm283x_mu_serial_platdata *plat;
  410. if (uclass_get_device_by_driver(UCLASS_SERIAL,
  411. DM_GET_DRIVER(serial_bcm283x_mu),
  412. &dev) || !dev)
  413. return;
  414. if (!rpi_is_serial_active()) {
  415. plat = dev_get_platdata(dev);
  416. plat->disabled = true;
  417. }
  418. }
  419. #endif
  420. int board_init(void)
  421. {
  422. #ifndef CONFIG_PL01X_SERIAL
  423. rpi_disable_inactive_uart();
  424. #endif
  425. get_board_rev();
  426. gd->bd->bi_boot_params = 0x100;
  427. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  428. }
  429. int board_mmc_init(bd_t *bis)
  430. {
  431. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
  432. int ret;
  433. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  434. BCM2835_MBOX_INIT_HDR(msg_clk);
  435. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  436. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  437. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  438. if (ret) {
  439. printf("bcm2835: Could not query eMMC clock rate\n");
  440. return -1;
  441. }
  442. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  443. msg_clk->get_clock_rate.body.resp.rate_hz);
  444. }
  445. int ft_board_setup(void *blob, bd_t *bd)
  446. {
  447. /*
  448. * For now, we simply always add the simplefb DT node. Later, we
  449. * should be more intelligent, and e.g. only do this if no enabled DT
  450. * node exists for the "real" graphics driver.
  451. */
  452. lcd_dt_simplefb_add_node(blob);
  453. #ifdef CONFIG_EFI_LOADER
  454. /* Reserve the spin table */
  455. efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
  456. #endif
  457. return 0;
  458. }