blob: a105953541bec89ee9759edfc7966af4327a34de [file] [log] [blame]
Stephen Warren0d04f342012-08-05 16:07:22 +00001/*
Stephen Warren46414292015-02-16 12:16:15 -07002 * (C) Copyright 2012-2013,2015 Stephen Warren
Stephen Warren0d04f342012-08-05 16:07:22 +00003 *
Stephen Warrena0331712015-02-16 12:16:13 -07004 * SPDX-License-Identifier: GPL-2.0
Stephen Warren0d04f342012-08-05 16:07:22 +00005 */
6
7#include <common.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +00008#include <config.h>
Simon Glass41e98e02014-09-22 17:30:56 -06009#include <dm.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020010#include <fdt_support.h>
Nikita Kiryanov033167c2015-02-03 13:32:31 +020011#include <fdt_simplefb.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +000012#include <lcd.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020013#include <mmc.h>
Simon Glass41e98e02014-09-22 17:30:56 -060014#include <asm/gpio.h>
Stephen Warren3f397782013-01-29 16:37:37 +000015#include <asm/arch/mbox.h>
Stephen Warren131a1e62013-01-29 16:37:42 +000016#include <asm/arch/sdhci.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000017#include <asm/global_data.h>
Simon Glass11506662014-11-24 21:36:34 -070018#include <dm/platform_data/serial_pl01x.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass41e98e02014-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 Glass11506662014-11-24 21:36:34 -070031static const struct pl01x_serial_platdata serial_platdata = {
Stephen Warren46414292015-02-16 12:16:15 -070032#ifdef CONFIG_BCM2836
33 .base = 0x3f201000,
34#else
Simon Glass11506662014-11-24 21:36:34 -070035 .base = 0x20201000,
Stephen Warren46414292015-02-16 12:16:15 -070036#endif
Simon Glass11506662014-11-24 21:36:34 -070037 .type = TYPE_PL011,
38 .clock = 3000000,
39};
40
41U_BOOT_DEVICE(bcm2835_serials) = {
42 .name = "serial_pl01x",
43 .platdata = &serial_platdata,
44};
45
Stephen Warren3f397782013-01-29 16:37:37 +000046struct msg_get_arm_mem {
47 struct bcm2835_mbox_hdr hdr;
48 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
49 u32 end_tag;
50};
51
Stephen Warren6fe78452014-11-18 21:40:21 -070052struct msg_get_board_rev {
53 struct bcm2835_mbox_hdr hdr;
54 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
55 u32 end_tag;
56};
57
Stephen Warren4f80a062014-09-26 20:51:39 -060058struct msg_get_mac_address {
59 struct bcm2835_mbox_hdr hdr;
60 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
61 u32 end_tag;
62};
63
Stephen Warrenf66f2aa2014-01-13 19:50:11 -070064struct msg_set_power_state {
65 struct bcm2835_mbox_hdr hdr;
66 struct bcm2835_mbox_tag_set_power_state set_power_state;
67 u32 end_tag;
68};
69
Stephen Warren131a1e62013-01-29 16:37:42 +000070struct msg_get_clock_rate {
71 struct bcm2835_mbox_hdr hdr;
72 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
73 u32 end_tag;
74};
75
Stephen Warren6fe78452014-11-18 21:40:21 -070076/* See comments in mbox.h for data source */
77static const struct {
78 const char *name;
79 const char *fdtfile;
Stephen Warren3207d8f2014-12-05 20:56:46 -070080 bool has_onboard_eth;
Stephen Warren6fe78452014-11-18 21:40:21 -070081} models[] = {
Stephen Warren914627f2014-12-23 20:01:44 -070082 [0] = {
83 "Unknown model",
Stephen Warren46414292015-02-16 12:16:15 -070084#ifdef CONFIG_BCM2836
85 "bcm2836-rpi-other.dtb",
86#else
Stephen Warren914627f2014-12-23 20:01:44 -070087 "bcm2835-rpi-other.dtb",
Stephen Warren46414292015-02-16 12:16:15 -070088#endif
Stephen Warren914627f2014-12-23 20:01:44 -070089 false,
90 },
Stephen Warren46414292015-02-16 12:16:15 -070091#ifdef CONFIG_BCM2836
92 [BCM2836_BOARD_REV_2_B] = {
93 "2 Model B",
94 "bcm2836-rpi-2-b.dtb",
95 true,
96 },
97#else
Stephen Warren6fe78452014-11-18 21:40:21 -070098 [BCM2835_BOARD_REV_B_I2C0_2] = {
99 "Model B (no P5)",
100 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700101 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700102 },
103 [BCM2835_BOARD_REV_B_I2C0_3] = {
104 "Model B (no P5)",
105 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700106 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700107 },
108 [BCM2835_BOARD_REV_B_I2C1_4] = {
109 "Model B",
110 "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700111 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700112 },
113 [BCM2835_BOARD_REV_B_I2C1_5] = {
114 "Model B",
115 "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700116 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700117 },
118 [BCM2835_BOARD_REV_B_I2C1_6] = {
119 "Model B",
120 "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700121 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700122 },
123 [BCM2835_BOARD_REV_A_7] = {
124 "Model A",
125 "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700126 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700127 },
128 [BCM2835_BOARD_REV_A_8] = {
129 "Model A",
130 "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700131 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700132 },
133 [BCM2835_BOARD_REV_A_9] = {
134 "Model A",
135 "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700136 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700137 },
138 [BCM2835_BOARD_REV_B_REV2_d] = {
139 "Model B rev2",
140 "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700141 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700142 },
143 [BCM2835_BOARD_REV_B_REV2_e] = {
144 "Model B rev2",
145 "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700146 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700147 },
148 [BCM2835_BOARD_REV_B_REV2_f] = {
149 "Model B rev2",
150 "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700151 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700152 },
153 [BCM2835_BOARD_REV_B_PLUS] = {
154 "Model B+",
155 "bcm2835-rpi-b-plus.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700156 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700157 },
158 [BCM2835_BOARD_REV_CM] = {
159 "Compute Module",
160 "bcm2835-rpi-cm.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700161 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700162 },
Stephen Warren47705ef2014-12-23 20:01:43 -0700163 [BCM2835_BOARD_REV_A_PLUS] = {
164 "Model A+",
165 "bcm2835-rpi-a-plus.dtb",
166 false,
167 },
Stephen Warren46414292015-02-16 12:16:15 -0700168#endif
Stephen Warren6fe78452014-11-18 21:40:21 -0700169};
170
171u32 rpi_board_rev = 0;
172
Stephen Warren0d04f342012-08-05 16:07:22 +0000173int dram_init(void)
174{
Stephen Warren3f397782013-01-29 16:37:37 +0000175 ALLOC_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1, 16);
176 int ret;
177
178 BCM2835_MBOX_INIT_HDR(msg);
179 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
180
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
187 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0d04f342012-08-05 16:07:22 +0000188
189 return 0;
190}
191
Stephen Warren6fe78452014-11-18 21:40:21 -0700192static void set_fdtfile(void)
193{
194 const char *fdtfile;
195
196 if (getenv("fdtfile"))
197 return;
198
199 fdtfile = models[rpi_board_rev].fdtfile;
Stephen Warren6fe78452014-11-18 21:40:21 -0700200 setenv("fdtfile", fdtfile);
201}
202
203static void set_usbethaddr(void)
Stephen Warren4f80a062014-09-26 20:51:39 -0600204{
205 ALLOC_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1, 16);
206 int ret;
207
Stephen Warren3207d8f2014-12-05 20:56:46 -0700208 if (!models[rpi_board_rev].has_onboard_eth)
209 return;
210
Stephen Warren4f80a062014-09-26 20:51:39 -0600211 if (getenv("usbethaddr"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700212 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600213
214 BCM2835_MBOX_INIT_HDR(msg);
215 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
216
217 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
218 if (ret) {
219 printf("bcm2835: Could not query MAC address\n");
220 /* Ignore error; not critical */
Stephen Warren6fe78452014-11-18 21:40:21 -0700221 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600222 }
223
224 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
225
Stephen Warren6fe78452014-11-18 21:40:21 -0700226 return;
227}
228
229int misc_init_r(void)
230{
231 set_fdtfile();
232 set_usbethaddr();
Stephen Warren4f80a062014-09-26 20:51:39 -0600233 return 0;
234}
235
Stephen Warrenf66f2aa2014-01-13 19:50:11 -0700236static int power_on_module(u32 module)
237{
238 ALLOC_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1, 16);
239 int ret;
240
241 BCM2835_MBOX_INIT_HDR(msg_pwr);
242 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
243 SET_POWER_STATE);
244 msg_pwr->set_power_state.body.req.device_id = module;
245 msg_pwr->set_power_state.body.req.state =
246 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
247 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
248
249 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
250 &msg_pwr->hdr);
251 if (ret) {
252 printf("bcm2835: Could not set module %u power state\n",
253 module);
254 return -1;
255 }
256
257 return 0;
258}
259
Stephen Warren6fe78452014-11-18 21:40:21 -0700260static void get_board_rev(void)
261{
262 ALLOC_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1, 16);
263 int ret;
264 const char *name;
265
266 BCM2835_MBOX_INIT_HDR(msg);
267 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
268
269 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
270 if (ret) {
271 printf("bcm2835: Could not query board revision\n");
272 /* Ignore error; not critical */
273 return;
274 }
275
Stephen Warren46414292015-02-16 12:16:15 -0700276 /*
277 * For details of old-vs-new scheme, see:
278 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
279 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
280 * (a few posts down)
Stephen Warren95b4f112015-03-23 23:00:25 -0600281 *
282 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
283 * lower byte to use as the board rev:
284 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
285 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warren46414292015-02-16 12:16:15 -0700286 */
Stephen Warren6fe78452014-11-18 21:40:21 -0700287 rpi_board_rev = msg->get_board_rev.body.resp.rev;
Stephen Warren46414292015-02-16 12:16:15 -0700288 if (rpi_board_rev & 0x800000)
289 rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
Stephen Warren95b4f112015-03-23 23:00:25 -0600290 else
291 rpi_board_rev &= 0xff;
Stephen Warren47705ef2014-12-23 20:01:43 -0700292 if (rpi_board_rev >= ARRAY_SIZE(models)) {
293 printf("RPI: Board rev %u outside known range\n",
294 rpi_board_rev);
Stephen Warren6fe78452014-11-18 21:40:21 -0700295 rpi_board_rev = 0;
Stephen Warren47705ef2014-12-23 20:01:43 -0700296 }
Stephen Warren914627f2014-12-23 20:01:44 -0700297 if (!models[rpi_board_rev].name) {
298 printf("RPI: Board rev %u unknown\n", rpi_board_rev);
299 rpi_board_rev = 0;
300 }
Stephen Warren6fe78452014-11-18 21:40:21 -0700301
302 name = models[rpi_board_rev].name;
Stephen Warren46414292015-02-16 12:16:15 -0700303 printf("RPI %s\n", name);
Stephen Warren6fe78452014-11-18 21:40:21 -0700304}
305
Stephen Warren0d04f342012-08-05 16:07:22 +0000306int board_init(void)
307{
Stephen Warren6fe78452014-11-18 21:40:21 -0700308 get_board_rev();
309
Stephen Warren0d04f342012-08-05 16:07:22 +0000310 gd->bd->bi_boot_params = 0x100;
311
Stephen Warrenf66f2aa2014-01-13 19:50:11 -0700312 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Stephen Warren0d04f342012-08-05 16:07:22 +0000313}
Stephen Warren131a1e62013-01-29 16:37:42 +0000314
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +0200315int board_mmc_init(bd_t *bis)
Stephen Warren131a1e62013-01-29 16:37:42 +0000316{
317 ALLOC_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1, 16);
318 int ret;
319
Stephen Warrenf66f2aa2014-01-13 19:50:11 -0700320 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
321
Stephen Warren131a1e62013-01-29 16:37:42 +0000322 BCM2835_MBOX_INIT_HDR(msg_clk);
323 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
324 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
325
326 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
327 if (ret) {
328 printf("bcm2835: Could not query eMMC clock rate\n");
329 return -1;
330 }
331
332 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
333 msg_clk->get_clock_rate.body.resp.rate_hz);
334}
Stephen Warrenea697ae2013-05-27 18:31:18 +0000335
Simon Glasse895a4b2014-10-23 18:58:47 -0600336int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenea697ae2013-05-27 18:31:18 +0000337{
338 /*
339 * For now, we simply always add the simplefb DT node. Later, we
340 * should be more intelligent, and e.g. only do this if no enabled DT
341 * node exists for the "real" graphics driver.
342 */
343 lcd_dt_simplefb_add_node(blob);
Simon Glasse895a4b2014-10-23 18:58:47 -0600344
345 return 0;
Stephen Warrenea697ae2013-05-27 18:31:18 +0000346}