rpi.c 11 KB

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