rpi.c 10 KB

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