rpi.c 11 KB

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