rpi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * (C) Copyright 2012-2013 Stephen Warren
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <common.h>
  17. #include <config.h>
  18. #include <dm.h>
  19. #include <fdt_support.h>
  20. #include <lcd.h>
  21. #include <mmc.h>
  22. #include <asm/gpio.h>
  23. #include <asm/arch/mbox.h>
  24. #include <asm/arch/sdhci.h>
  25. #include <asm/global_data.h>
  26. #include <dm/platform_data/serial_pl01x.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. static const struct bcm2835_gpio_platdata gpio_platdata = {
  29. .base = BCM2835_GPIO_BASE,
  30. };
  31. U_BOOT_DEVICE(bcm2835_gpios) = {
  32. .name = "gpio_bcm2835",
  33. .platdata = &gpio_platdata,
  34. };
  35. static const struct pl01x_serial_platdata serial_platdata = {
  36. .base = 0x20201000,
  37. .type = TYPE_PL011,
  38. .clock = 3000000,
  39. };
  40. U_BOOT_DEVICE(bcm2835_serials) = {
  41. .name = "serial_pl01x",
  42. .platdata = &serial_platdata,
  43. };
  44. struct msg_get_arm_mem {
  45. struct bcm2835_mbox_hdr hdr;
  46. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  47. u32 end_tag;
  48. };
  49. struct msg_get_board_rev {
  50. struct bcm2835_mbox_hdr hdr;
  51. struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  52. u32 end_tag;
  53. };
  54. struct msg_get_mac_address {
  55. struct bcm2835_mbox_hdr hdr;
  56. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  57. u32 end_tag;
  58. };
  59. struct msg_set_power_state {
  60. struct bcm2835_mbox_hdr hdr;
  61. struct bcm2835_mbox_tag_set_power_state set_power_state;
  62. u32 end_tag;
  63. };
  64. struct msg_get_clock_rate {
  65. struct bcm2835_mbox_hdr hdr;
  66. struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  67. u32 end_tag;
  68. };
  69. /* See comments in mbox.h for data source */
  70. static const struct {
  71. const char *name;
  72. const char *fdtfile;
  73. bool has_onboard_eth;
  74. } models[] = {
  75. [BCM2835_BOARD_REV_B_I2C0_2] = {
  76. "Model B (no P5)",
  77. "bcm2835-rpi-b-i2c0.dtb",
  78. true,
  79. },
  80. [BCM2835_BOARD_REV_B_I2C0_3] = {
  81. "Model B (no P5)",
  82. "bcm2835-rpi-b-i2c0.dtb",
  83. true,
  84. },
  85. [BCM2835_BOARD_REV_B_I2C1_4] = {
  86. "Model B",
  87. "bcm2835-rpi-b.dtb",
  88. true,
  89. },
  90. [BCM2835_BOARD_REV_B_I2C1_5] = {
  91. "Model B",
  92. "bcm2835-rpi-b.dtb",
  93. true,
  94. },
  95. [BCM2835_BOARD_REV_B_I2C1_6] = {
  96. "Model B",
  97. "bcm2835-rpi-b.dtb",
  98. true,
  99. },
  100. [BCM2835_BOARD_REV_A_7] = {
  101. "Model A",
  102. "bcm2835-rpi-a.dtb",
  103. false,
  104. },
  105. [BCM2835_BOARD_REV_A_8] = {
  106. "Model A",
  107. "bcm2835-rpi-a.dtb",
  108. false,
  109. },
  110. [BCM2835_BOARD_REV_A_9] = {
  111. "Model A",
  112. "bcm2835-rpi-a.dtb",
  113. false,
  114. },
  115. [BCM2835_BOARD_REV_B_REV2_d] = {
  116. "Model B rev2",
  117. "bcm2835-rpi-b-rev2.dtb",
  118. true,
  119. },
  120. [BCM2835_BOARD_REV_B_REV2_e] = {
  121. "Model B rev2",
  122. "bcm2835-rpi-b-rev2.dtb",
  123. true,
  124. },
  125. [BCM2835_BOARD_REV_B_REV2_f] = {
  126. "Model B rev2",
  127. "bcm2835-rpi-b-rev2.dtb",
  128. true,
  129. },
  130. [BCM2835_BOARD_REV_B_PLUS] = {
  131. "Model B+",
  132. "bcm2835-rpi-b-plus.dtb",
  133. true,
  134. },
  135. [BCM2835_BOARD_REV_CM] = {
  136. "Compute Module",
  137. "bcm2835-rpi-cm.dtb",
  138. false,
  139. },
  140. [BCM2835_BOARD_REV_A_PLUS] = {
  141. "Model A+",
  142. "bcm2835-rpi-a-plus.dtb",
  143. false,
  144. },
  145. };
  146. u32 rpi_board_rev = 0;
  147. int dram_init(void)
  148. {
  149. ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
  150. int ret;
  151. BCM2835_MBOX_INIT_HDR(msg);
  152. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  153. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  154. if (ret) {
  155. printf("bcm2835: Could not query ARM memory size\n");
  156. return -1;
  157. }
  158. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  159. return 0;
  160. }
  161. static void set_fdtfile(void)
  162. {
  163. const char *fdtfile;
  164. if (getenv("fdtfile"))
  165. return;
  166. fdtfile = models[rpi_board_rev].fdtfile;
  167. if (!fdtfile)
  168. fdtfile = "bcm2835-rpi-other.dtb";
  169. setenv("fdtfile", fdtfile);
  170. }
  171. static void set_usbethaddr(void)
  172. {
  173. ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
  174. int ret;
  175. if (!models[rpi_board_rev].has_onboard_eth)
  176. return;
  177. if (getenv("usbethaddr"))
  178. return;
  179. BCM2835_MBOX_INIT_HDR(msg);
  180. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  181. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  182. if (ret) {
  183. printf("bcm2835: Could not query MAC address\n");
  184. /* Ignore error; not critical */
  185. return;
  186. }
  187. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  188. return;
  189. }
  190. int misc_init_r(void)
  191. {
  192. set_fdtfile();
  193. set_usbethaddr();
  194. return 0;
  195. }
  196. static int power_on_module(u32 module)
  197. {
  198. ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
  199. int ret;
  200. BCM2835_MBOX_INIT_HDR(msg_pwr);
  201. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  202. SET_POWER_STATE);
  203. msg_pwr->set_power_state.body.req.device_id = module;
  204. msg_pwr->set_power_state.body.req.state =
  205. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  206. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  207. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  208. &msg_pwr->hdr);
  209. if (ret) {
  210. printf("bcm2835: Could not set module %u power state\n",
  211. module);
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static void get_board_rev(void)
  217. {
  218. ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
  219. int ret;
  220. const char *name;
  221. BCM2835_MBOX_INIT_HDR(msg);
  222. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  223. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  224. if (ret) {
  225. printf("bcm2835: Could not query board revision\n");
  226. /* Ignore error; not critical */
  227. return;
  228. }
  229. rpi_board_rev = msg->get_board_rev.body.resp.rev;
  230. if (rpi_board_rev >= ARRAY_SIZE(models)) {
  231. printf("RPI: Board rev %u outside known range\n",
  232. rpi_board_rev);
  233. rpi_board_rev = 0;
  234. }
  235. name = models[rpi_board_rev].name;
  236. if (!name) {
  237. printf("RPI: Board rev %u unknown\n", rpi_board_rev);
  238. name = "Unknown model";
  239. }
  240. printf("RPI model: %s\n", name);
  241. }
  242. int board_init(void)
  243. {
  244. get_board_rev();
  245. gd->bd->bi_boot_params = 0x100;
  246. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  247. }
  248. int board_mmc_init(bd_t *bis)
  249. {
  250. ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
  251. int ret;
  252. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  253. BCM2835_MBOX_INIT_HDR(msg_clk);
  254. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  255. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  256. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  257. if (ret) {
  258. printf("bcm2835: Could not query eMMC clock rate\n");
  259. return -1;
  260. }
  261. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  262. msg_clk->get_clock_rate.body.resp.rate_hz);
  263. }
  264. int ft_board_setup(void *blob, bd_t *bd)
  265. {
  266. /*
  267. * For now, we simply always add the simplefb DT node. Later, we
  268. * should be more intelligent, and e.g. only do this if no enabled DT
  269. * node exists for the "real" graphics driver.
  270. */
  271. lcd_dt_simplefb_add_node(blob);
  272. return 0;
  273. }