rpi.c 11 KB

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