rpi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. };
  141. u32 rpi_board_rev = 0;
  142. int dram_init(void)
  143. {
  144. ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
  145. int ret;
  146. BCM2835_MBOX_INIT_HDR(msg);
  147. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  148. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  149. if (ret) {
  150. printf("bcm2835: Could not query ARM memory size\n");
  151. return -1;
  152. }
  153. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  154. return 0;
  155. }
  156. static void set_fdtfile(void)
  157. {
  158. const char *fdtfile;
  159. if (getenv("fdtfile"))
  160. return;
  161. fdtfile = models[rpi_board_rev].fdtfile;
  162. if (!fdtfile)
  163. fdtfile = "bcm2835-rpi-other.dtb";
  164. setenv("fdtfile", fdtfile);
  165. }
  166. static void set_usbethaddr(void)
  167. {
  168. ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
  169. int ret;
  170. if (!models[rpi_board_rev].has_onboard_eth)
  171. return;
  172. if (getenv("usbethaddr"))
  173. return;
  174. BCM2835_MBOX_INIT_HDR(msg);
  175. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  176. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  177. if (ret) {
  178. printf("bcm2835: Could not query MAC address\n");
  179. /* Ignore error; not critical */
  180. return;
  181. }
  182. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  183. return;
  184. }
  185. int misc_init_r(void)
  186. {
  187. set_fdtfile();
  188. set_usbethaddr();
  189. return 0;
  190. }
  191. static int power_on_module(u32 module)
  192. {
  193. ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
  194. int ret;
  195. BCM2835_MBOX_INIT_HDR(msg_pwr);
  196. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  197. SET_POWER_STATE);
  198. msg_pwr->set_power_state.body.req.device_id = module;
  199. msg_pwr->set_power_state.body.req.state =
  200. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  201. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  202. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  203. &msg_pwr->hdr);
  204. if (ret) {
  205. printf("bcm2835: Could not set module %u power state\n",
  206. module);
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. static void get_board_rev(void)
  212. {
  213. ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
  214. int ret;
  215. const char *name;
  216. BCM2835_MBOX_INIT_HDR(msg);
  217. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  218. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  219. if (ret) {
  220. printf("bcm2835: Could not query board revision\n");
  221. /* Ignore error; not critical */
  222. return;
  223. }
  224. rpi_board_rev = msg->get_board_rev.body.resp.rev;
  225. if (rpi_board_rev >= ARRAY_SIZE(models))
  226. rpi_board_rev = 0;
  227. name = models[rpi_board_rev].name;
  228. if (!name)
  229. name = "Unknown model";
  230. printf("RPI model: %s\n", name);
  231. }
  232. int board_init(void)
  233. {
  234. get_board_rev();
  235. gd->bd->bi_boot_params = 0x100;
  236. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  237. }
  238. int board_mmc_init(bd_t *bis)
  239. {
  240. ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
  241. int ret;
  242. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  243. BCM2835_MBOX_INIT_HDR(msg_clk);
  244. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  245. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  246. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  247. if (ret) {
  248. printf("bcm2835: Could not query eMMC clock rate\n");
  249. return -1;
  250. }
  251. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  252. msg_clk->get_clock_rate.body.resp.rate_hz);
  253. }
  254. int ft_board_setup(void *blob, bd_t *bd)
  255. {
  256. /*
  257. * For now, we simply always add the simplefb DT node. Later, we
  258. * should be more intelligent, and e.g. only do this if no enabled DT
  259. * node exists for the "real" graphics driver.
  260. */
  261. lcd_dt_simplefb_add_node(blob);
  262. return 0;
  263. }