rpi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. DECLARE_GLOBAL_DATA_PTR;
  27. static const struct bcm2835_gpio_platdata gpio_platdata = {
  28. .base = BCM2835_GPIO_BASE,
  29. };
  30. U_BOOT_DEVICE(bcm2835_gpios) = {
  31. .name = "gpio_bcm2835",
  32. .platdata = &gpio_platdata,
  33. };
  34. struct msg_get_arm_mem {
  35. struct bcm2835_mbox_hdr hdr;
  36. struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
  37. u32 end_tag;
  38. };
  39. struct msg_get_board_rev {
  40. struct bcm2835_mbox_hdr hdr;
  41. struct bcm2835_mbox_tag_get_board_rev get_board_rev;
  42. u32 end_tag;
  43. };
  44. struct msg_get_mac_address {
  45. struct bcm2835_mbox_hdr hdr;
  46. struct bcm2835_mbox_tag_get_mac_address get_mac_address;
  47. u32 end_tag;
  48. };
  49. struct msg_set_power_state {
  50. struct bcm2835_mbox_hdr hdr;
  51. struct bcm2835_mbox_tag_set_power_state set_power_state;
  52. u32 end_tag;
  53. };
  54. struct msg_get_clock_rate {
  55. struct bcm2835_mbox_hdr hdr;
  56. struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
  57. u32 end_tag;
  58. };
  59. /* See comments in mbox.h for data source */
  60. static const struct {
  61. const char *name;
  62. const char *fdtfile;
  63. } models[] = {
  64. [BCM2835_BOARD_REV_B_I2C0_2] = {
  65. "Model B (no P5)",
  66. "bcm2835-rpi-b-i2c0.dtb",
  67. },
  68. [BCM2835_BOARD_REV_B_I2C0_3] = {
  69. "Model B (no P5)",
  70. "bcm2835-rpi-b-i2c0.dtb",
  71. },
  72. [BCM2835_BOARD_REV_B_I2C1_4] = {
  73. "Model B",
  74. "bcm2835-rpi-b.dtb",
  75. },
  76. [BCM2835_BOARD_REV_B_I2C1_5] = {
  77. "Model B",
  78. "bcm2835-rpi-b.dtb",
  79. },
  80. [BCM2835_BOARD_REV_B_I2C1_6] = {
  81. "Model B",
  82. "bcm2835-rpi-b.dtb",
  83. },
  84. [BCM2835_BOARD_REV_A_7] = {
  85. "Model A",
  86. "bcm2835-rpi-a.dtb",
  87. },
  88. [BCM2835_BOARD_REV_A_8] = {
  89. "Model A",
  90. "bcm2835-rpi-a.dtb",
  91. },
  92. [BCM2835_BOARD_REV_A_9] = {
  93. "Model A",
  94. "bcm2835-rpi-a.dtb",
  95. },
  96. [BCM2835_BOARD_REV_B_REV2_d] = {
  97. "Model B rev2",
  98. "bcm2835-rpi-b-rev2.dtb",
  99. },
  100. [BCM2835_BOARD_REV_B_REV2_e] = {
  101. "Model B rev2",
  102. "bcm2835-rpi-b-rev2.dtb",
  103. },
  104. [BCM2835_BOARD_REV_B_REV2_f] = {
  105. "Model B rev2",
  106. "bcm2835-rpi-b-rev2.dtb",
  107. },
  108. [BCM2835_BOARD_REV_B_PLUS] = {
  109. "Model B+",
  110. "bcm2835-rpi-b-plus.dtb",
  111. },
  112. [BCM2835_BOARD_REV_CM] = {
  113. "Compute Module",
  114. "bcm2835-rpi-cm.dtb",
  115. },
  116. };
  117. u32 rpi_board_rev = 0;
  118. int dram_init(void)
  119. {
  120. ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
  121. int ret;
  122. BCM2835_MBOX_INIT_HDR(msg);
  123. BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
  124. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  125. if (ret) {
  126. printf("bcm2835: Could not query ARM memory size\n");
  127. return -1;
  128. }
  129. gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
  130. return 0;
  131. }
  132. static void set_fdtfile(void)
  133. {
  134. const char *fdtfile;
  135. if (getenv("fdtfile"))
  136. return;
  137. fdtfile = models[rpi_board_rev].fdtfile;
  138. if (!fdtfile)
  139. fdtfile = "bcm2835-rpi-other.dtb";
  140. setenv("fdtfile", fdtfile);
  141. }
  142. static void set_usbethaddr(void)
  143. {
  144. ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
  145. int ret;
  146. if (getenv("usbethaddr"))
  147. return;
  148. BCM2835_MBOX_INIT_HDR(msg);
  149. BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
  150. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  151. if (ret) {
  152. printf("bcm2835: Could not query MAC address\n");
  153. /* Ignore error; not critical */
  154. return;
  155. }
  156. eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
  157. return;
  158. }
  159. int misc_init_r(void)
  160. {
  161. set_fdtfile();
  162. set_usbethaddr();
  163. return 0;
  164. }
  165. static int power_on_module(u32 module)
  166. {
  167. ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
  168. int ret;
  169. BCM2835_MBOX_INIT_HDR(msg_pwr);
  170. BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
  171. SET_POWER_STATE);
  172. msg_pwr->set_power_state.body.req.device_id = module;
  173. msg_pwr->set_power_state.body.req.state =
  174. BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
  175. BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
  176. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
  177. &msg_pwr->hdr);
  178. if (ret) {
  179. printf("bcm2835: Could not set module %u power state\n",
  180. module);
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. static void get_board_rev(void)
  186. {
  187. ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
  188. int ret;
  189. const char *name;
  190. BCM2835_MBOX_INIT_HDR(msg);
  191. BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
  192. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
  193. if (ret) {
  194. printf("bcm2835: Could not query board revision\n");
  195. /* Ignore error; not critical */
  196. return;
  197. }
  198. rpi_board_rev = msg->get_board_rev.body.resp.rev;
  199. if (rpi_board_rev >= ARRAY_SIZE(models))
  200. rpi_board_rev = 0;
  201. name = models[rpi_board_rev].name;
  202. if (!name)
  203. name = "Unknown model";
  204. printf("RPI model: %s\n", name);
  205. }
  206. int board_init(void)
  207. {
  208. get_board_rev();
  209. gd->bd->bi_boot_params = 0x100;
  210. return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
  211. }
  212. int board_mmc_init(bd_t *bis)
  213. {
  214. ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
  215. int ret;
  216. power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
  217. BCM2835_MBOX_INIT_HDR(msg_clk);
  218. BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
  219. msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
  220. ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
  221. if (ret) {
  222. printf("bcm2835: Could not query eMMC clock rate\n");
  223. return -1;
  224. }
  225. return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
  226. msg_clk->get_clock_rate.body.resp.rate_hz);
  227. }
  228. int ft_board_setup(void *blob, bd_t *bd)
  229. {
  230. /*
  231. * For now, we simply always add the simplefb DT node. Later, we
  232. * should be more intelligent, and e.g. only do this if no enabled DT
  233. * node exists for the "real" graphics driver.
  234. */
  235. lcd_dt_simplefb_add_node(blob);
  236. return 0;
  237. }