rpi.c 8.6 KB

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