blob: fdbf87d6fa7994c778f23cf68ee343fbc53ac778 [file] [log] [blame]
Stephen Warren0e012c32012-08-05 16:07:22 +00001/*
Stephen Warrenaa44d532013-05-27 18:31:18 +00002 * (C) Copyright 2012-2013 Stephen Warren
Stephen Warren0e012c32012-08-05 16:07:22 +00003 *
Stephen Warren29e494c2015-02-16 12:16:13 -07004 * SPDX-License-Identifier: GPL-2.0
Stephen Warren0e012c32012-08-05 16:07:22 +00005 */
6
7#include <common.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +00008#include <config.h>
Simon Glass74807622014-09-22 17:30:56 -06009#include <dm.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020010#include <fdt_support.h>
Nikita Kiryanovb071e4c2015-02-03 13:32:31 +020011#include <fdt_simplefb.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +000012#include <lcd.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020013#include <mmc.h>
Simon Glass74807622014-09-22 17:30:56 -060014#include <asm/gpio.h>
Stephen Warrenacc8bf42013-01-29 16:37:37 +000015#include <asm/arch/mbox.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000016#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000017#include <asm/global_data.h>
Simon Glassf52cada2014-11-24 21:36:34 -070018#include <dm/platform_data/serial_pl01x.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass74807622014-09-22 17:30:56 -060022static const struct bcm2835_gpio_platdata gpio_platdata = {
23 .base = BCM2835_GPIO_BASE,
24};
25
26U_BOOT_DEVICE(bcm2835_gpios) = {
27 .name = "gpio_bcm2835",
28 .platdata = &gpio_platdata,
29};
30
Simon Glassf52cada2014-11-24 21:36:34 -070031static const struct pl01x_serial_platdata serial_platdata = {
32 .base = 0x20201000,
33 .type = TYPE_PL011,
34 .clock = 3000000,
35};
36
37U_BOOT_DEVICE(bcm2835_serials) = {
38 .name = "serial_pl01x",
39 .platdata = &serial_platdata,
40};
41
Stephen Warrenacc8bf42013-01-29 16:37:37 +000042struct msg_get_arm_mem {
43 struct bcm2835_mbox_hdr hdr;
44 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
45 u32 end_tag;
46};
47
Stephen Warrencd210c12014-11-18 21:40:21 -070048struct msg_get_board_rev {
49 struct bcm2835_mbox_hdr hdr;
50 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
51 u32 end_tag;
52};
53
Stephen Warrenaf6e20d2014-09-26 20:51:39 -060054struct 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
Stephen Warren8672d202014-01-13 19:50:11 -070060struct msg_set_power_state {
61 struct bcm2835_mbox_hdr hdr;
62 struct bcm2835_mbox_tag_set_power_state set_power_state;
63 u32 end_tag;
64};
65
Stephen Warrenc4ab9712013-01-29 16:37:42 +000066struct msg_get_clock_rate {
67 struct bcm2835_mbox_hdr hdr;
68 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
69 u32 end_tag;
70};
71
Stephen Warrencd210c12014-11-18 21:40:21 -070072/* See comments in mbox.h for data source */
73static const struct {
74 const char *name;
75 const char *fdtfile;
Stephen Warrenb2fa38362014-12-05 20:56:46 -070076 bool has_onboard_eth;
Stephen Warrencd210c12014-11-18 21:40:21 -070077} models[] = {
Stephen Warrenc61dfd12014-12-23 20:01:44 -070078 [0] = {
79 "Unknown model",
80 "bcm2835-rpi-other.dtb",
81 false,
82 },
Stephen Warrencd210c12014-11-18 21:40:21 -070083 [BCM2835_BOARD_REV_B_I2C0_2] = {
84 "Model B (no P5)",
85 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -070086 true,
Stephen Warrencd210c12014-11-18 21:40:21 -070087 },
88 [BCM2835_BOARD_REV_B_I2C0_3] = {
89 "Model B (no P5)",
90 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -070091 true,
Stephen Warrencd210c12014-11-18 21:40:21 -070092 },
93 [BCM2835_BOARD_REV_B_I2C1_4] = {
94 "Model B",
95 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -070096 true,
Stephen Warrencd210c12014-11-18 21:40:21 -070097 },
98 [BCM2835_BOARD_REV_B_I2C1_5] = {
99 "Model B",
100 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700101 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700102 },
103 [BCM2835_BOARD_REV_B_I2C1_6] = {
104 "Model B",
105 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700106 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700107 },
108 [BCM2835_BOARD_REV_A_7] = {
109 "Model A",
110 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700111 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700112 },
113 [BCM2835_BOARD_REV_A_8] = {
114 "Model A",
115 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700116 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700117 },
118 [BCM2835_BOARD_REV_A_9] = {
119 "Model A",
120 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700121 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700122 },
123 [BCM2835_BOARD_REV_B_REV2_d] = {
124 "Model B rev2",
125 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700126 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700127 },
128 [BCM2835_BOARD_REV_B_REV2_e] = {
129 "Model B rev2",
130 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700131 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700132 },
133 [BCM2835_BOARD_REV_B_REV2_f] = {
134 "Model B rev2",
135 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700136 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700137 },
138 [BCM2835_BOARD_REV_B_PLUS] = {
139 "Model B+",
140 "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700141 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700142 },
143 [BCM2835_BOARD_REV_CM] = {
144 "Compute Module",
145 "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700146 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700147 },
Stephen Warren18a5fc62014-12-23 20:01:43 -0700148 [BCM2835_BOARD_REV_A_PLUS] = {
149 "Model A+",
150 "bcm2835-rpi-a-plus.dtb",
151 false,
152 },
Stephen Warrencd210c12014-11-18 21:40:21 -0700153};
154
155u32 rpi_board_rev = 0;
156
Stephen Warren0e012c32012-08-05 16:07:22 +0000157int dram_init(void)
158{
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000159 ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
160 int ret;
161
162 BCM2835_MBOX_INIT_HDR(msg);
163 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
164
165 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
166 if (ret) {
167 printf("bcm2835: Could not query ARM memory size\n");
168 return -1;
169 }
170
171 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000172
173 return 0;
174}
175
Stephen Warrencd210c12014-11-18 21:40:21 -0700176static void set_fdtfile(void)
177{
178 const char *fdtfile;
179
180 if (getenv("fdtfile"))
181 return;
182
183 fdtfile = models[rpi_board_rev].fdtfile;
Stephen Warrencd210c12014-11-18 21:40:21 -0700184 setenv("fdtfile", fdtfile);
185}
186
187static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600188{
189 ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
190 int ret;
191
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700192 if (!models[rpi_board_rev].has_onboard_eth)
193 return;
194
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600195 if (getenv("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700196 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600197
198 BCM2835_MBOX_INIT_HDR(msg);
199 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
200
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 */
Stephen Warrencd210c12014-11-18 21:40:21 -0700205 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600206 }
207
208 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
209
Stephen Warrencd210c12014-11-18 21:40:21 -0700210 return;
211}
212
213int misc_init_r(void)
214{
215 set_fdtfile();
216 set_usbethaddr();
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600217 return 0;
218}
219
Stephen Warren8672d202014-01-13 19:50:11 -0700220static int power_on_module(u32 module)
221{
222 ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
223 int ret;
224
225 BCM2835_MBOX_INIT_HDR(msg_pwr);
226 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
227 SET_POWER_STATE);
228 msg_pwr->set_power_state.body.req.device_id = module;
229 msg_pwr->set_power_state.body.req.state =
230 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
231 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
232
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
241 return 0;
242}
243
Stephen Warrencd210c12014-11-18 21:40:21 -0700244static void get_board_rev(void)
245{
246 ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
247 int ret;
248 const char *name;
249
250 BCM2835_MBOX_INIT_HDR(msg);
251 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
252
253 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
254 if (ret) {
255 printf("bcm2835: Could not query board revision\n");
256 /* Ignore error; not critical */
257 return;
258 }
259
260 rpi_board_rev = msg->get_board_rev.body.resp.rev;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700261 if (rpi_board_rev >= ARRAY_SIZE(models)) {
262 printf("RPI: Board rev %u outside known range\n",
263 rpi_board_rev);
Stephen Warrencd210c12014-11-18 21:40:21 -0700264 rpi_board_rev = 0;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700265 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700266 if (!models[rpi_board_rev].name) {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700267 printf("RPI: Board rev %u unknown\n", rpi_board_rev);
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700268 rpi_board_rev = 0;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700269 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700270
271 name = models[rpi_board_rev].name;
Stephen Warrencd210c12014-11-18 21:40:21 -0700272 printf("RPI model: %s\n", name);
273}
274
Stephen Warren0e012c32012-08-05 16:07:22 +0000275int board_init(void)
276{
Stephen Warrencd210c12014-11-18 21:40:21 -0700277 get_board_rev();
278
Stephen Warren0e012c32012-08-05 16:07:22 +0000279 gd->bd->bi_boot_params = 0x100;
280
Stephen Warren8672d202014-01-13 19:50:11 -0700281 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Stephen Warren0e012c32012-08-05 16:07:22 +0000282}
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000283
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +0200284int board_mmc_init(bd_t *bis)
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000285{
286 ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
287 int ret;
288
Stephen Warren8672d202014-01-13 19:50:11 -0700289 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
290
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000291 BCM2835_MBOX_INIT_HDR(msg_clk);
292 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
293 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
294
295 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
296 if (ret) {
297 printf("bcm2835: Could not query eMMC clock rate\n");
298 return -1;
299 }
300
301 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
302 msg_clk->get_clock_rate.body.resp.rate_hz);
303}
Stephen Warrenaa44d532013-05-27 18:31:18 +0000304
Simon Glass2aec3cc2014-10-23 18:58:47 -0600305int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000306{
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);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600313
314 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000315}