rpi.c 12 KB

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