rpi.c 7.4 KB

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