blob: 43ed60ae9811c425fdfacd4523483cd0002a43b6 [file] [log] [blame]
Stephen Warren0e012c32012-08-05 16:07:22 +00001/*
Stephen Warrendc7ea682015-02-16 12:16:15 -07002 * (C) Copyright 2012-2013,2015 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>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020014#include <mmc.h>
Simon Glass74807622014-09-22 17:30:56 -060015#include <asm/gpio.h>
Stephen Warrenacc8bf42013-01-29 16:37:37 +000016#include <asm/arch/mbox.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000017#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000018#include <asm/global_data.h>
Simon Glassf52cada2014-11-24 21:36:34 -070019#include <dm/platform_data/serial_pl01x.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000020
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass74807622014-09-22 17:30:56 -060023static const struct bcm2835_gpio_platdata gpio_platdata = {
24 .base = BCM2835_GPIO_BASE,
25};
26
27U_BOOT_DEVICE(bcm2835_gpios) = {
28 .name = "gpio_bcm2835",
29 .platdata = &gpio_platdata,
30};
31
Simon Glassf52cada2014-11-24 21:36:34 -070032static const struct pl01x_serial_platdata serial_platdata = {
Stephen Warrendc7ea682015-02-16 12:16:15 -070033#ifdef CONFIG_BCM2836
34 .base = 0x3f201000,
35#else
Simon Glassf52cada2014-11-24 21:36:34 -070036 .base = 0x20201000,
Stephen Warrendc7ea682015-02-16 12:16:15 -070037#endif
Simon Glassf52cada2014-11-24 21:36:34 -070038 .type = TYPE_PL011,
39 .clock = 3000000,
40};
41
42U_BOOT_DEVICE(bcm2835_serials) = {
43 .name = "serial_pl01x",
44 .platdata = &serial_platdata,
45};
46
Stephen Warrenacc8bf42013-01-29 16:37:37 +000047struct msg_get_arm_mem {
48 struct bcm2835_mbox_hdr hdr;
49 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
50 u32 end_tag;
51};
52
Stephen Warrencd210c12014-11-18 21:40:21 -070053struct msg_get_board_rev {
54 struct bcm2835_mbox_hdr hdr;
55 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
56 u32 end_tag;
57};
58
Stephen Warrenaf6e20d2014-09-26 20:51:39 -060059struct msg_get_mac_address {
60 struct bcm2835_mbox_hdr hdr;
61 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
62 u32 end_tag;
63};
64
Stephen Warren8672d202014-01-13 19:50:11 -070065struct msg_set_power_state {
66 struct bcm2835_mbox_hdr hdr;
67 struct bcm2835_mbox_tag_set_power_state set_power_state;
68 u32 end_tag;
69};
70
Stephen Warrenc4ab9712013-01-29 16:37:42 +000071struct msg_get_clock_rate {
72 struct bcm2835_mbox_hdr hdr;
73 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
74 u32 end_tag;
75};
76
Stephen Warrenbe8efec2015-12-04 22:07:44 -070077/*
78 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
79 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
80 * http://git.drogon.net/?p=wiringPi;a=blob_plain;f=wiringPi/wiringPi.c;hb=5edd177112c99416f68ba3e8c6c4db6ed942e796
81 */
Stephen Warrencd210c12014-11-18 21:40:21 -070082static const struct {
83 const char *name;
84 const char *fdtfile;
Stephen Warrenb2fa38362014-12-05 20:56:46 -070085 bool has_onboard_eth;
Stephen Warrencd210c12014-11-18 21:40:21 -070086} models[] = {
Stephen Warrenc61dfd12014-12-23 20:01:44 -070087 [0] = {
88 "Unknown model",
Stephen Warrendc7ea682015-02-16 12:16:15 -070089#ifdef CONFIG_BCM2836
90 "bcm2836-rpi-other.dtb",
91#else
Stephen Warrenc61dfd12014-12-23 20:01:44 -070092 "bcm2835-rpi-other.dtb",
Stephen Warrendc7ea682015-02-16 12:16:15 -070093#endif
Stephen Warrenc61dfd12014-12-23 20:01:44 -070094 false,
95 },
Stephen Warrendc7ea682015-02-16 12:16:15 -070096#ifdef CONFIG_BCM2836
Stephen Warrenbe8efec2015-12-04 22:07:44 -070097 [0x4] = {
Stephen Warrendc7ea682015-02-16 12:16:15 -070098 "2 Model B",
99 "bcm2836-rpi-2-b.dtb",
100 true,
101 },
102#else
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700103 [0x2] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700104 "Model B (no P5)",
105 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700106 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700107 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700108 [0x3] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700109 "Model B (no P5)",
110 "bcm2835-rpi-b-i2c0.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700111 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700112 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700113 [0x4] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700114 "Model B",
115 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700116 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700117 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700118 [0x5] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700119 "Model B",
120 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700121 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700122 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700123 [0x6] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700124 "Model B",
125 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700126 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700127 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700128 [0x7] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700129 "Model A",
130 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700131 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700132 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700133 [0x8] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700134 "Model A",
135 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700136 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700137 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700138 [0x9] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700139 "Model A",
140 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700141 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700142 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700143 [0xd] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700144 "Model B rev2",
145 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700146 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700147 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700148 [0xe] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700149 "Model B rev2",
150 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700151 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700152 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700153 [0xf] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700154 "Model B rev2",
155 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700156 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700157 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700158 [0x10] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700159 "Model B+",
160 "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700161 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700162 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700163 [0x11] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700164 "Compute Module",
165 "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700166 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700167 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700168 [0x12] = {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700169 "Model A+",
170 "bcm2835-rpi-a-plus.dtb",
171 false,
172 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700173 [0x13] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600174 "Model B+",
175 "bcm2835-rpi-b-plus.dtb",
176 true,
177 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700178 [0x14] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600179 "Compute Module",
180 "bcm2835-rpi-cm.dtb",
181 false,
182 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700183 [0x15] = {
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200184 "Model A+",
185 "bcm2835-rpi-a-plus.dtb",
186 false,
187 },
Stephen Warrendc7ea682015-02-16 12:16:15 -0700188#endif
Stephen Warrencd210c12014-11-18 21:40:21 -0700189};
190
191u32 rpi_board_rev = 0;
192
Stephen Warren0e012c32012-08-05 16:07:22 +0000193int dram_init(void)
194{
Alexander Steinc56c9472015-07-24 09:22:12 +0200195 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000196 int ret;
197
198 BCM2835_MBOX_INIT_HDR(msg);
199 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
200
201 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
202 if (ret) {
203 printf("bcm2835: Could not query ARM memory size\n");
204 return -1;
205 }
206
207 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000208
209 return 0;
210}
211
Stephen Warrencd210c12014-11-18 21:40:21 -0700212static void set_fdtfile(void)
213{
214 const char *fdtfile;
215
216 if (getenv("fdtfile"))
217 return;
218
219 fdtfile = models[rpi_board_rev].fdtfile;
Stephen Warrencd210c12014-11-18 21:40:21 -0700220 setenv("fdtfile", fdtfile);
221}
222
223static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600224{
Alexander Steinc56c9472015-07-24 09:22:12 +0200225 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600226 int ret;
227
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700228 if (!models[rpi_board_rev].has_onboard_eth)
229 return;
230
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600231 if (getenv("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700232 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600233
234 BCM2835_MBOX_INIT_HDR(msg);
235 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
236
237 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
238 if (ret) {
239 printf("bcm2835: Could not query MAC address\n");
240 /* Ignore error; not critical */
Stephen Warrencd210c12014-11-18 21:40:21 -0700241 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600242 }
243
244 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
245
Stephen Warrencd210c12014-11-18 21:40:21 -0700246 return;
247}
248
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200249#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
250static void set_board_info(void)
251{
252 char str_rev[11];
253 sprintf(str_rev, "0x%X", rpi_board_rev);
254 setenv("board_rev", str_rev);
255 setenv("board_name", models[rpi_board_rev].name);
256}
257#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
258
Stephen Warrencd210c12014-11-18 21:40:21 -0700259int misc_init_r(void)
260{
261 set_fdtfile();
262 set_usbethaddr();
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200263#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
264 set_board_info();
265#endif
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600266 return 0;
267}
268
Stephen Warren8672d202014-01-13 19:50:11 -0700269static int power_on_module(u32 module)
270{
Alexander Steinc56c9472015-07-24 09:22:12 +0200271 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
Stephen Warren8672d202014-01-13 19:50:11 -0700272 int ret;
273
274 BCM2835_MBOX_INIT_HDR(msg_pwr);
275 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
276 SET_POWER_STATE);
277 msg_pwr->set_power_state.body.req.device_id = module;
278 msg_pwr->set_power_state.body.req.state =
279 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
280 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
281
282 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
283 &msg_pwr->hdr);
284 if (ret) {
285 printf("bcm2835: Could not set module %u power state\n",
286 module);
287 return -1;
288 }
289
290 return 0;
291}
292
Stephen Warrencd210c12014-11-18 21:40:21 -0700293static void get_board_rev(void)
294{
Alexander Steinc56c9472015-07-24 09:22:12 +0200295 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warrencd210c12014-11-18 21:40:21 -0700296 int ret;
297 const char *name;
298
299 BCM2835_MBOX_INIT_HDR(msg);
300 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
301
302 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
303 if (ret) {
304 printf("bcm2835: Could not query board revision\n");
305 /* Ignore error; not critical */
306 return;
307 }
308
Stephen Warrendc7ea682015-02-16 12:16:15 -0700309 /*
310 * For details of old-vs-new scheme, see:
311 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
312 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
313 * (a few posts down)
Stephen Warrenb834af82015-03-23 23:00:25 -0600314 *
315 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
316 * lower byte to use as the board rev:
317 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
318 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warrendc7ea682015-02-16 12:16:15 -0700319 */
Stephen Warrencd210c12014-11-18 21:40:21 -0700320 rpi_board_rev = msg->get_board_rev.body.resp.rev;
Stephen Warrendc7ea682015-02-16 12:16:15 -0700321 if (rpi_board_rev & 0x800000)
322 rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
Stephen Warrenb834af82015-03-23 23:00:25 -0600323 else
324 rpi_board_rev &= 0xff;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700325 if (rpi_board_rev >= ARRAY_SIZE(models)) {
326 printf("RPI: Board rev %u outside known range\n",
327 rpi_board_rev);
Stephen Warrencd210c12014-11-18 21:40:21 -0700328 rpi_board_rev = 0;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700329 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700330 if (!models[rpi_board_rev].name) {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700331 printf("RPI: Board rev %u unknown\n", rpi_board_rev);
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700332 rpi_board_rev = 0;
Stephen Warren18a5fc62014-12-23 20:01:43 -0700333 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700334
335 name = models[rpi_board_rev].name;
Stephen Warrendc7ea682015-02-16 12:16:15 -0700336 printf("RPI %s\n", name);
Stephen Warrencd210c12014-11-18 21:40:21 -0700337}
338
Stephen Warren0e012c32012-08-05 16:07:22 +0000339int board_init(void)
340{
Stephen Warrencd210c12014-11-18 21:40:21 -0700341 get_board_rev();
342
Stephen Warren0e012c32012-08-05 16:07:22 +0000343 gd->bd->bi_boot_params = 0x100;
344
Stephen Warren8672d202014-01-13 19:50:11 -0700345 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Stephen Warren0e012c32012-08-05 16:07:22 +0000346}
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000347
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +0200348int board_mmc_init(bd_t *bis)
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000349{
Alexander Steinc56c9472015-07-24 09:22:12 +0200350 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000351 int ret;
352
Stephen Warren8672d202014-01-13 19:50:11 -0700353 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
354
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000355 BCM2835_MBOX_INIT_HDR(msg_clk);
356 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
357 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
358
359 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
360 if (ret) {
361 printf("bcm2835: Could not query eMMC clock rate\n");
362 return -1;
363 }
364
365 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
366 msg_clk->get_clock_rate.body.resp.rate_hz);
367}
Stephen Warrenaa44d532013-05-27 18:31:18 +0000368
Simon Glass2aec3cc2014-10-23 18:58:47 -0600369int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000370{
371 /*
372 * For now, we simply always add the simplefb DT node. Later, we
373 * should be more intelligent, and e.g. only do this if no enabled DT
374 * node exists for the "real" graphics driver.
375 */
376 lcd_dt_simplefb_add_node(blob);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600377
378 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000379}