rpi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. 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. #ifdef CONFIG_BCM2836
  78. "bcm2836-rpi-other.dtb",
  79. #else
  80. "bcm2835-rpi-other.dtb",
  81. #endif
  82. false,
  83. };
  84. static const struct rpi_model rpi_models_new_scheme[] = {
  85. [0x4] = {
  86. "2 Model B",
  87. "bcm2836-rpi-2-b.dtb",
  88. true,
  89. },
  90. };
  91. static const struct rpi_model rpi_models_old_scheme[] = {
  92. [0x2] = {
  93. "Model B (no P5)",
  94. "bcm2835-rpi-b-i2c0.dtb",
  95. true,
  96. },
  97. [0x3] = {
  98. "Model B (no P5)",
  99. "bcm2835-rpi-b-i2c0.dtb",
  100. true,
  101. },
  102. [0x4] = {
  103. "Model B",
  104. "bcm2835-rpi-b.dtb",
  105. true,
  106. },
  107. [0x5] = {
  108. "Model B",
  109. "bcm2835-rpi-b.dtb",
  110. true,
  111. },
  112. [0x6] = {
  113. "Model B",
  114. "bcm2835-rpi-b.dtb",
  115. true,
  116. },
  117. [0x7] = {
  118. "Model A",
  119. "bcm2835-rpi-a.dtb",
  120. false,
  121. },
  122. [0x8] = {
  123. "Model A",
  124. "bcm2835-rpi-a.dtb",
  125. false,
  126. },
  127. [0x9] = {
  128. "Model A",
  129. "bcm2835-rpi-a.dtb",
  130. false,
  131. },
  132. [0xd] = {
  133. "Model B rev2",
  134. "bcm2835-rpi-b-rev2.dtb",
  135. true,
  136. },
  137. [0xe] = {
  138. "Model B rev2",
  139. "bcm2835-rpi-b-rev2.dtb",
  140. true,
  141. },
  142. [0xf] = {
  143. "Model B rev2",
  144. "bcm2835-rpi-b-rev2.dtb",
  145. true,
  146. },
  147. [0x10] = {
  148. "Model B+",
  149. "bcm2835-rpi-b-plus.dtb",
  150. true,
  151. },
  152. [0x11] = {
  153. "Compute Module",
  154. "bcm2835-rpi-cm.dtb",
  155. false,
  156. },
  157. [0x12] = {
  158. "Model A+",
  159. "bcm2835-rpi-a-plus.dtb",
  160. false,
  161. },
  162. [0x13] = {
  163. "Model B+",
  164. "bcm2835-rpi-b-plus.dtb",
  165. true,
  166. },
  167. [0x14] = {
  168. "Compute Module",
  169. "bcm2835-rpi-cm.dtb",
  170. false,
  171. },
  172. [0x15] = {
  173. "Model A+",
  174. "bcm2835-rpi-a-plus.dtb",
  175. false,
  176. },
  177. };
  178. static uint32_t revision;
  179. static uint32_t rev_scheme;
  180. static uint32_t rev_type;
  181. static const struct rpi_model *model;
  182. int dram_init(void)
  183. {
  184. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
  185. int ret;
  186. BCM2835_MBOX_INIT_HDR(msg);
  187. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  188. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  189. if (ret) {
  190. printf("bcm2835: Could not query ARM memory size\n");
  191. return -1;
  192. }
  193. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  194. return 0;
  195. }
  196. static void set_fdtfile(void)
  197. {
  198. const char *fdtfile;
  199. if (getenv("fdtfile"))
  200. return;
  201. fdtfile = model->fdtfile;
  202. setenv("fdtfile", fdtfile);
  203. }
  204. static void set_usbethaddr(void)
  205. {
  206. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
  207. int ret;
  208. if (!model->has_onboard_eth)
  209. return;
  210. if (getenv("usbethaddr"))
  211. return;
  212. BCM2835_MBOX_INIT_HDR(msg);
  213. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  214. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  215. if (ret) {
  216. printf("bcm2835: Could not query MAC address\n");
  217. /* Ignore error; not critical */
  218. return;
  219. }
  220. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  221. return;
  222. }
  223. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  224. static void set_board_info(void)
  225. {
  226. char s[11];
  227. snprintf(s, sizeof(s), "0x%X", revision);
  228. setenv("board_revision", s);
  229. snprintf(s, sizeof(s), "%d", rev_scheme);
  230. setenv("board_rev_scheme", s);
  231. /* Can't rename this to board_rev_type since it's an ABI for scripts */
  232. snprintf(s, sizeof(s), "0x%X", rev_type);
  233. setenv("board_rev", s);
  234. setenv("board_name", model->name);
  235. }
  236. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  237. int misc_init_r(void)
  238. {
  239. set_fdtfile();
  240. set_usbethaddr();
  241. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  242. set_board_info();
  243. #endif
  244. return 0;
  245. }
  246. static int power_on_module(u32 module)
  247. {
  248. ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
  249. int ret;
  250. BCM2835_MBOX_INIT_HDR(msg_pwr);
  251. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  252. SET_POWER_STATE);
  253. msg_pwr->set_power_state.body.req.device_id = module;
  254. msg_pwr->set_power_state.body.req.state =
  255. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  256. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  257. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  258. &msg_pwr->hdr);
  259. if (ret) {
  260. printf("bcm2835: Could not set module %u power state\n",
  261. module);
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. static void get_board_rev(void)
  267. {
  268. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
  269. int ret;
  270. const struct rpi_model *models;
  271. uint32_t models_count;
  272. BCM2835_MBOX_INIT_HDR(msg);
  273. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  274. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  275. if (ret) {
  276. printf("bcm2835: Could not query board revision\n");
  277. /* Ignore error; not critical */
  278. return;
  279. }
  280. /*
  281. * For details of old-vs-new scheme, see:
  282. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  283. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  284. * (a few posts down)
  285. *
  286. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  287. * lower byte to use as the board rev:
  288. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  289. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  290. */
  291. revision = msg->get_board_rev.body.resp.rev;
  292. if (revision & 0x800000) {
  293. rev_scheme = 1;
  294. rev_type = (revision >> 4) & 0xff;
  295. models = rpi_models_new_scheme;
  296. models_count = ARRAY_SIZE(rpi_models_new_scheme);
  297. } else {
  298. rev_scheme = 0;
  299. rev_type = revision & 0xff;
  300. models = rpi_models_old_scheme;
  301. models_count = ARRAY_SIZE(rpi_models_old_scheme);
  302. }
  303. if (rev_type >= models_count) {
  304. printf("RPI: Board rev 0x%x outside known range\n", rev_type);
  305. model = &rpi_model_unknown;
  306. } else if (!models[rev_type].name) {
  307. printf("RPI: Board rev 0x%x unknown\n", rev_type);
  308. model = &rpi_model_unknown;
  309. } else {
  310. model = &models[rev_type];
  311. }
  312. printf("RPI %s (0x%x)\n", model->name, revision);
  313. }
  314. int board_init(void)
  315. {
  316. get_board_rev();
  317. gd->bd->bi_boot_params = 0x100;
  318. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  319. }
  320. int board_mmc_init(bd_t *bis)
  321. {
  322. ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
  323. int ret;
  324. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  325. BCM2835_MBOX_INIT_HDR(msg_clk);
  326. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  327. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  328. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  329. if (ret) {
  330. printf("bcm2835: Could not query eMMC clock rate\n");
  331. return -1;
  332. }
  333. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  334. msg_clk->get_clock_rate.body.resp.rate_hz);
  335. }
  336. int ft_board_setup(void *blob, bd_t *bd)
  337. {
  338. /*
  339. * For now, we simply always add the simplefb DT node. Later, we
  340. * should be more intelligent, and e.g. only do this if no enabled DT
  341. * node exists for the "real" graphics driver.
  342. */
  343. lcd_dt_simplefb_add_node(blob);
  344. return 0;
  345. }