rpi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. [BCM2835_BOARD_REV_B_PLUS_13] = {
  157. "Model B+",
  158. "bcm2835-rpi-b-plus.dtb",
  159. true,
  160. },
  161. [BCM2835_BOARD_REV_CM_14] = {
  162. "Compute Module",
  163. "bcm2835-rpi-cm.dtb",
  164. false,
  165. },
  166. #endif
  167. };
  168. u32 rpi_board_rev = 0;
  169. int dram_init(void)
  170. {
  171. ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
  172. int ret;
  173. BCM2835_MBOX_INIT_HDR(msg);
  174. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  175. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  176. if (ret) {
  177. printf("bcm2835: Could not query ARM memory size\n");
  178. return -1;
  179. }
  180. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  181. return 0;
  182. }
  183. static void set_fdtfile(void)
  184. {
  185. const char *fdtfile;
  186. if (getenv("fdtfile"))
  187. return;
  188. fdtfile = models[rpi_board_rev].fdtfile;
  189. setenv("fdtfile", fdtfile);
  190. }
  191. static void set_usbethaddr(void)
  192. {
  193. ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
  194. int ret;
  195. if (!models[rpi_board_rev].has_onboard_eth)
  196. return;
  197. if (getenv("usbethaddr"))
  198. return;
  199. BCM2835_MBOX_INIT_HDR(msg);
  200. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  201. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  202. if (ret) {
  203. printf("bcm2835: Could not query MAC address\n");
  204. /* Ignore error; not critical */
  205. return;
  206. }
  207. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  208. return;
  209. }
  210. int misc_init_r(void)
  211. {
  212. set_fdtfile();
  213. set_usbethaddr();
  214. return 0;
  215. }
  216. static int power_on_module(u32 module)
  217. {
  218. ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
  219. int ret;
  220. BCM2835_MBOX_INIT_HDR(msg_pwr);
  221. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  222. SET_POWER_STATE);
  223. msg_pwr->set_power_state.body.req.device_id = module;
  224. msg_pwr->set_power_state.body.req.state =
  225. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  226. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  227. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  228. &msg_pwr->hdr);
  229. if (ret) {
  230. printf("bcm2835: Could not set module %u power state\n",
  231. module);
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static void get_board_rev(void)
  237. {
  238. ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
  239. int ret;
  240. const char *name;
  241. BCM2835_MBOX_INIT_HDR(msg);
  242. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  243. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  244. if (ret) {
  245. printf("bcm2835: Could not query board revision\n");
  246. /* Ignore error; not critical */
  247. return;
  248. }
  249. /*
  250. * For details of old-vs-new scheme, see:
  251. * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
  252. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
  253. * (a few posts down)
  254. *
  255. * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
  256. * lower byte to use as the board rev:
  257. * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
  258. * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
  259. */
  260. rpi_board_rev = msg->get_board_rev.body.resp.rev;
  261. if (rpi_board_rev & 0x800000)
  262. rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
  263. else
  264. rpi_board_rev &= 0xff;
  265. if (rpi_board_rev >= ARRAY_SIZE(models)) {
  266. printf("RPI: Board rev %u outside known range\n",
  267. rpi_board_rev);
  268. rpi_board_rev = 0;
  269. }
  270. if (!models[rpi_board_rev].name) {
  271. printf("RPI: Board rev %u unknown\n", rpi_board_rev);
  272. rpi_board_rev = 0;
  273. }
  274. name = models[rpi_board_rev].name;
  275. printf("RPI %s\n", name);
  276. }
  277. int board_init(void)
  278. {
  279. get_board_rev();
  280. gd->bd->bi_boot_params = 0x100;
  281. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  282. }
  283. int board_mmc_init(bd_t *bis)
  284. {
  285. ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
  286. int ret;
  287. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  288. BCM2835_MBOX_INIT_HDR(msg_clk);
  289. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  290. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  291. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  292. if (ret) {
  293. printf("bcm2835: Could not query eMMC clock rate\n");
  294. return -1;
  295. }
  296. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  297. msg_clk->get_clock_rate.body.resp.rate_hz);
  298. }
  299. int ft_board_setup(void *blob, bd_t *bd)
  300. {
  301. /*
  302. * For now, we simply always add the simplefb DT node. Later, we
  303. * should be more intelligent, and e.g. only do this if no enabled DT
  304. * node exists for the "real" graphics driver.
  305. */
  306. lcd_dt_simplefb_add_node(blob);
  307. return 0;
  308. }