rpi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * (C) Copyright 2012-2013,2015 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. DECLARE_GLOBAL_DATA_PTR;
  21. static const struct bcm2835_gpio_platdata gpio_platdata = {
  22. .base = BCM2835_GPIO_BASE,
  23. };
  24. U_BOOT_DEVICE(bcm2835_gpios) = {
  25. .name = "gpio_bcm2835",
  26. .platdata = &gpio_platdata,
  27. };
  28. static const struct pl01x_serial_platdata serial_platdata = {
  29. #ifndef CONFIG_BCM2835
  30. .base = 0x3f201000,
  31. #else
  32. .base = 0x20201000,
  33. #endif
  34. .type = TYPE_PL011,
  35. .skip_init = true,
  36. };
  37. U_BOOT_DEVICE(bcm2835_serials) = {
  38. .name = "serial_pl01x",
  39. .platdata = &serial_platdata,
  40. };
  41. struct msg_get_arm_mem {
  42. struct bcm2835_mbox_hdr hdr;
  43. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  44. u32 end_tag;
  45. };
  46. struct msg_get_board_rev {
  47. struct bcm2835_mbox_hdr hdr;
  48. struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  49. u32 end_tag;
  50. };
  51. struct msg_get_board_serial {
  52. struct bcm2835_mbox_hdr hdr;
  53. struct bcm2835_mbox_tag_get_board_serial get_board_serial;
  54. u32 end_tag;
  55. };
  56. struct msg_get_mac_address {
  57. struct bcm2835_mbox_hdr hdr;
  58. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  59. u32 end_tag;
  60. };
  61. struct msg_set_power_state {
  62. struct bcm2835_mbox_hdr hdr;
  63. struct bcm2835_mbox_tag_set_power_state set_power_state;
  64. u32 end_tag;
  65. };
  66. struct msg_get_clock_rate {
  67. struct bcm2835_mbox_hdr hdr;
  68. struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  69. u32 end_tag;
  70. };
  71. /*
  72. * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
  73. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
  74. * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
  75. *
  76. * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
  77. * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
  78. * Foundation stated that the following source was accurate:
  79. * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
  80. */
  81. struct rpi_model {
  82. const char *name;
  83. const char *fdtfile;
  84. bool has_onboard_eth;
  85. };
  86. static const struct rpi_model rpi_model_unknown = {
  87. "Unknown model",
  88. #ifdef CONFIG_BCM2836
  89. "bcm2836-rpi-other.dtb",
  90. #else
  91. "bcm2835-rpi-other.dtb",
  92. #endif
  93. false,
  94. };
  95. static const struct rpi_model rpi_models_new_scheme[] = {
  96. [0x4] = {
  97. "2 Model B",
  98. "bcm2836-rpi-2-b.dtb",
  99. true,
  100. },
  101. [0x9] = {
  102. "Zero",
  103. "bcm2835-rpi-zero.dtb",
  104. false,
  105. },
  106. };
  107. static const struct rpi_model rpi_models_old_scheme[] = {
  108. [0x2] = {
  109. "Model B",
  110. "bcm2835-rpi-b.dtb",
  111. true,
  112. },
  113. [0x3] = {
  114. "Model B",
  115. "bcm2835-rpi-b.dtb",
  116. true,
  117. },
  118. [0x4] = {
  119. "Model B rev2",
  120. "bcm2835-rpi-b-rev2.dtb",
  121. true,
  122. },
  123. [0x5] = {
  124. "Model B rev2",
  125. "bcm2835-rpi-b-rev2.dtb",
  126. true,
  127. },
  128. [0x6] = {
  129. "Model B rev2",
  130. "bcm2835-rpi-b-rev2.dtb",
  131. true,
  132. },
  133. [0x7] = {
  134. "Model A",
  135. "bcm2835-rpi-a.dtb",
  136. false,
  137. },
  138. [0x8] = {
  139. "Model A",
  140. "bcm2835-rpi-a.dtb",
  141. false,
  142. },
  143. [0x9] = {
  144. "Model A",
  145. "bcm2835-rpi-a.dtb",
  146. false,
  147. },
  148. [0xd] = {
  149. "Model B rev2",
  150. "bcm2835-rpi-b-rev2.dtb",
  151. true,
  152. },
  153. [0xe] = {
  154. "Model B rev2",
  155. "bcm2835-rpi-b-rev2.dtb",
  156. true,
  157. },
  158. [0xf] = {
  159. "Model B rev2",
  160. "bcm2835-rpi-b-rev2.dtb",
  161. true,
  162. },
  163. [0x10] = {
  164. "Model B+",
  165. "bcm2835-rpi-b-plus.dtb",
  166. true,
  167. },
  168. [0x11] = {
  169. "Compute Module",
  170. "bcm2835-rpi-cm.dtb",
  171. false,
  172. },
  173. [0x12] = {
  174. "Model A+",
  175. "bcm2835-rpi-a-plus.dtb",
  176. false,
  177. },
  178. [0x13] = {
  179. "Model B+",
  180. "bcm2835-rpi-b-plus.dtb",
  181. true,
  182. },
  183. [0x14] = {
  184. "Compute Module",
  185. "bcm2835-rpi-cm.dtb",
  186. false,
  187. },
  188. [0x15] = {
  189. "Model A+",
  190. "bcm2835-rpi-a-plus.dtb",
  191. false,
  192. },
  193. };
  194. static uint32_t revision;
  195. static uint32_t rev_scheme;
  196. static uint32_t rev_type;
  197. static const struct rpi_model *model;
  198. int dram_init(void)
  199. {
  200. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
  201. int ret;
  202. BCM2835_MBOX_INIT_HDR(msg);
  203. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  204. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  205. if (ret) {
  206. printf("bcm2835: Could not query ARM memory size\n");
  207. return -1;
  208. }
  209. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  210. return 0;
  211. }
  212. static void set_fdtfile(void)
  213. {
  214. const char *fdtfile;
  215. if (getenv("fdtfile"))
  216. return;
  217. fdtfile = model->fdtfile;
  218. setenv("fdtfile", fdtfile);
  219. }
  220. static void set_usbethaddr(void)
  221. {
  222. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
  223. int ret;
  224. if (!model->has_onboard_eth)
  225. return;
  226. if (getenv("usbethaddr"))
  227. return;
  228. BCM2835_MBOX_INIT_HDR(msg);
  229. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  230. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  231. if (ret) {
  232. printf("bcm2835: Could not query MAC address\n");
  233. /* Ignore error; not critical */
  234. return;
  235. }
  236. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  237. if (!getenv("ethaddr"))
  238. setenv("ethaddr", getenv("usbethaddr"));
  239. return;
  240. }
  241. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  242. static void set_board_info(void)
  243. {
  244. char s[11];
  245. snprintf(s, sizeof(s), "0x%X", revision);
  246. setenv("board_revision", s);
  247. snprintf(s, sizeof(s), "%d", rev_scheme);
  248. setenv("board_rev_scheme", s);
  249. /* Can't rename this to board_rev_type since it's an ABI for scripts */
  250. snprintf(s, sizeof(s), "0x%X", rev_type);
  251. setenv("board_rev", s);
  252. setenv("board_name", model->name);
  253. }
  254. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  255. static void set_serial_number(void)
  256. {
  257. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
  258. int ret;
  259. char serial_string[17] = { 0 };
  260. if (getenv("serial#"))
  261. return;
  262. BCM2835_MBOX_INIT_HDR(msg);
  263. BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
  264. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  265. if (ret) {
  266. printf("bcm2835: Could not query board serial\n");
  267. /* Ignore error; not critical */
  268. return;
  269. }
  270. snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
  271. msg->get_board_serial.body.resp.serial);
  272. setenv("serial#", serial_string);
  273. }
  274. int misc_init_r(void)
  275. {
  276. set_fdtfile();
  277. set_usbethaddr();
  278. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  279. set_board_info();
  280. #endif
  281. set_serial_number();
  282. return 0;
  283. }
  284. static int power_on_module(u32 module)
  285. {
  286. ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
  287. int ret;
  288. BCM2835_MBOX_INIT_HDR(msg_pwr);
  289. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  290. SET_POWER_STATE);
  291. msg_pwr->set_power_state.body.req.device_id = module;
  292. msg_pwr->set_power_state.body.req.state =
  293. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  294. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  295. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  296. &msg_pwr->hdr);
  297. if (ret) {
  298. printf("bcm2835: Could not set module %u power state\n",
  299. module);
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. static void get_board_rev(void)
  305. {
  306. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
  307. int ret;
  308. const struct rpi_model *models;
  309. uint32_t models_count;
  310. BCM2835_MBOX_INIT_HDR(msg);
  311. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  312. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  313. if (ret) {
  314. printf("bcm2835: Could not query board revision\n");
  315. /* Ignore error; not critical */
  316. return;
  317. }
  318. /*
  319. * For details of old-vs-new scheme, see:
  320. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  321. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  322. * (a few posts down)
  323. *
  324. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  325. * lower byte to use as the board rev:
  326. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  327. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  328. */
  329. revision = msg->get_board_rev.body.resp.rev;
  330. if (revision & 0x800000) {
  331. rev_scheme = 1;
  332. rev_type = (revision >> 4) & 0xff;
  333. models = rpi_models_new_scheme;
  334. models_count = ARRAY_SIZE(rpi_models_new_scheme);
  335. } else {
  336. rev_scheme = 0;
  337. rev_type = revision & 0xff;
  338. models = rpi_models_old_scheme;
  339. models_count = ARRAY_SIZE(rpi_models_old_scheme);
  340. }
  341. if (rev_type >= models_count) {
  342. printf("RPI: Board rev 0x%x outside known range\n", rev_type);
  343. model = &rpi_model_unknown;
  344. } else if (!models[rev_type].name) {
  345. printf("RPI: Board rev 0x%x unknown\n", rev_type);
  346. model = &rpi_model_unknown;
  347. } else {
  348. model = &models[rev_type];
  349. }
  350. printf("RPI %s (0x%x)\n", model->name, revision);
  351. }
  352. int board_init(void)
  353. {
  354. get_board_rev();
  355. gd->bd->bi_boot_params = 0x100;
  356. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  357. }
  358. int board_mmc_init(bd_t *bis)
  359. {
  360. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
  361. int ret;
  362. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  363. BCM2835_MBOX_INIT_HDR(msg_clk);
  364. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  365. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  366. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  367. if (ret) {
  368. printf("bcm2835: Could not query eMMC clock rate\n");
  369. return -1;
  370. }
  371. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  372. msg_clk->get_clock_rate.body.resp.rate_hz);
  373. }
  374. int ft_board_setup(void *blob, bd_t *bd)
  375. {
  376. /*
  377. * For now, we simply always add the simplefb DT node. Later, we
  378. * should be more intelligent, and e.g. only do this if no enabled DT
  379. * node exists for the "real" graphics driver.
  380. */
  381. lcd_dt_simplefb_add_node(blob);
  382. return 0;
  383. }