rpi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <mmc.h>
  13. #include <asm/gpio.h>
  14. #include <asm/arch/mbox.h>
  15. #include <asm/arch/sdhci.h>
  16. #include <asm/global_data.h>
  17. #include <dm/platform_data/serial_pl01x.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. static const struct bcm2835_gpio_platdata gpio_platdata = {
  20. .base = BCM2835_GPIO_BASE,
  21. };
  22. U_BOOT_DEVICE(bcm2835_gpios) = {
  23. .name = "gpio_bcm2835",
  24. .platdata = &gpio_platdata,
  25. };
  26. static const struct pl01x_serial_platdata serial_platdata = {
  27. #ifdef CONFIG_BCM2836
  28. .base = 0x3f201000,
  29. #else
  30. .base = 0x20201000,
  31. #endif
  32. .type = TYPE_PL011,
  33. .clock = 3000000,
  34. };
  35. U_BOOT_DEVICE(bcm2835_serials) = {
  36. .name = "serial_pl01x",
  37. .platdata = &serial_platdata,
  38. };
  39. struct msg_get_arm_mem {
  40. struct bcm2835_mbox_hdr hdr;
  41. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  42. u32 end_tag;
  43. };
  44. struct msg_get_board_rev {
  45. struct bcm2835_mbox_hdr hdr;
  46. struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  47. u32 end_tag;
  48. };
  49. struct msg_get_mac_address {
  50. struct bcm2835_mbox_hdr hdr;
  51. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  52. u32 end_tag;
  53. };
  54. struct msg_set_power_state {
  55. struct bcm2835_mbox_hdr hdr;
  56. struct bcm2835_mbox_tag_set_power_state set_power_state;
  57. u32 end_tag;
  58. };
  59. struct msg_get_clock_rate {
  60. struct bcm2835_mbox_hdr hdr;
  61. struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  62. u32 end_tag;
  63. };
  64. /* See comments in mbox.h for data source */
  65. static const struct {
  66. const char *name;
  67. const char *fdtfile;
  68. bool has_onboard_eth;
  69. } models[] = {
  70. [0] = {
  71. "Unknown model",
  72. #ifdef CONFIG_BCM2836
  73. "bcm2836-rpi-other.dtb",
  74. #else
  75. "bcm2835-rpi-other.dtb",
  76. #endif
  77. false,
  78. },
  79. #ifdef CONFIG_BCM2836
  80. [BCM2836_BOARD_REV_2_B] = {
  81. "2 Model B",
  82. "bcm2836-rpi-2-b.dtb",
  83. true,
  84. },
  85. #else
  86. [BCM2835_BOARD_REV_B_I2C0_2] = {
  87. "Model B (no P5)",
  88. "bcm2835-rpi-b-i2c0.dtb",
  89. true,
  90. },
  91. [BCM2835_BOARD_REV_B_I2C0_3] = {
  92. "Model B (no P5)",
  93. "bcm2835-rpi-b-i2c0.dtb",
  94. true,
  95. },
  96. [BCM2835_BOARD_REV_B_I2C1_4] = {
  97. "Model B",
  98. "bcm2835-rpi-b.dtb",
  99. true,
  100. },
  101. [BCM2835_BOARD_REV_B_I2C1_5] = {
  102. "Model B",
  103. "bcm2835-rpi-b.dtb",
  104. true,
  105. },
  106. [BCM2835_BOARD_REV_B_I2C1_6] = {
  107. "Model B",
  108. "bcm2835-rpi-b.dtb",
  109. true,
  110. },
  111. [BCM2835_BOARD_REV_A_7] = {
  112. "Model A",
  113. "bcm2835-rpi-a.dtb",
  114. false,
  115. },
  116. [BCM2835_BOARD_REV_A_8] = {
  117. "Model A",
  118. "bcm2835-rpi-a.dtb",
  119. false,
  120. },
  121. [BCM2835_BOARD_REV_A_9] = {
  122. "Model A",
  123. "bcm2835-rpi-a.dtb",
  124. false,
  125. },
  126. [BCM2835_BOARD_REV_B_REV2_d] = {
  127. "Model B rev2",
  128. "bcm2835-rpi-b-rev2.dtb",
  129. true,
  130. },
  131. [BCM2835_BOARD_REV_B_REV2_e] = {
  132. "Model B rev2",
  133. "bcm2835-rpi-b-rev2.dtb",
  134. true,
  135. },
  136. [BCM2835_BOARD_REV_B_REV2_f] = {
  137. "Model B rev2",
  138. "bcm2835-rpi-b-rev2.dtb",
  139. true,
  140. },
  141. [BCM2835_BOARD_REV_B_PLUS] = {
  142. "Model B+",
  143. "bcm2835-rpi-b-plus.dtb",
  144. true,
  145. },
  146. [BCM2835_BOARD_REV_CM] = {
  147. "Compute Module",
  148. "bcm2835-rpi-cm.dtb",
  149. false,
  150. },
  151. [BCM2835_BOARD_REV_A_PLUS] = {
  152. "Model A+",
  153. "bcm2835-rpi-a-plus.dtb",
  154. false,
  155. },
  156. #endif
  157. };
  158. u32 rpi_board_rev = 0;
  159. int dram_init(void)
  160. {
  161. ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
  162. int ret;
  163. BCM2835_MBOX_INIT_HDR(msg);
  164. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  165. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  166. if (ret) {
  167. printf("bcm2835: Could not query ARM memory size\n");
  168. return -1;
  169. }
  170. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  171. return 0;
  172. }
  173. static void set_fdtfile(void)
  174. {
  175. const char *fdtfile;
  176. if (getenv("fdtfile"))
  177. return;
  178. fdtfile = models[rpi_board_rev].fdtfile;
  179. setenv("fdtfile", fdtfile);
  180. }
  181. static void set_usbethaddr(void)
  182. {
  183. ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
  184. int ret;
  185. if (!models[rpi_board_rev].has_onboard_eth)
  186. return;
  187. if (getenv("usbethaddr"))
  188. return;
  189. BCM2835_MBOX_INIT_HDR(msg);
  190. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  191. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  192. if (ret) {
  193. printf("bcm2835: Could not query MAC address\n");
  194. /* Ignore error; not critical */
  195. return;
  196. }
  197. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  198. return;
  199. }
  200. int misc_init_r(void)
  201. {
  202. set_fdtfile();
  203. set_usbethaddr();
  204. return 0;
  205. }
  206. static int power_on_module(u32 module)
  207. {
  208. ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
  209. int ret;
  210. BCM2835_MBOX_INIT_HDR(msg_pwr);
  211. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  212. SET_POWER_STATE);
  213. msg_pwr->set_power_state.body.req.device_id = module;
  214. msg_pwr->set_power_state.body.req.state =
  215. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  216. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  217. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  218. &msg_pwr->hdr);
  219. if (ret) {
  220. printf("bcm2835: Could not set module %u power state\n",
  221. module);
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. static void get_board_rev(void)
  227. {
  228. ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
  229. int ret;
  230. const char *name;
  231. BCM2835_MBOX_INIT_HDR(msg);
  232. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  233. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  234. if (ret) {
  235. printf("bcm2835: Could not query board revision\n");
  236. /* Ignore error; not critical */
  237. return;
  238. }
  239. /*
  240. * For details of old-vs-new scheme, see:
  241. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  242. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  243. * (a few posts down)
  244. *
  245. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  246. * lower byte to use as the board rev:
  247. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  248. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  249. */
  250. rpi_board_rev = msg->get_board_rev.body.resp.rev;
  251. if (rpi_board_rev & 0x800000)
  252. rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
  253. else
  254. rpi_board_rev &= 0xff;
  255. if (rpi_board_rev >= ARRAY_SIZE(models)) {
  256. printf("RPI: Board rev %u outside known range\n",
  257. rpi_board_rev);
  258. rpi_board_rev = 0;
  259. }
  260. if (!models[rpi_board_rev].name) {
  261. printf("RPI: Board rev %u unknown\n", rpi_board_rev);
  262. rpi_board_rev = 0;
  263. }
  264. name = models[rpi_board_rev].name;
  265. printf("RPI %s\n", name);
  266. }
  267. int board_init(void)
  268. {
  269. get_board_rev();
  270. gd->bd->bi_boot_params = 0x100;
  271. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  272. }
  273. int board_mmc_init(bd_t *bis)
  274. {
  275. ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
  276. int ret;
  277. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  278. BCM2835_MBOX_INIT_HDR(msg_clk);
  279. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  280. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  281. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  282. if (ret) {
  283. printf("bcm2835: Could not query eMMC clock rate\n");
  284. return -1;
  285. }
  286. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  287. msg_clk->get_clock_rate.body.resp.rate_hz);
  288. }
  289. int ft_board_setup(void *blob, bd_t *bd)
  290. {
  291. /*
  292. * For now, we simply always add the simplefb DT node. Later, we
  293. * should be more intelligent, and e.g. only do this if no enabled DT
  294. * node exists for the "real" graphics driver.
  295. */
  296. lcd_dt_simplefb_add_node(blob);
  297. return 0;
  298. }