2 * (C) Copyright 2012-2013,2015 Stephen Warren
4 * SPDX-License-Identifier: GPL-2.0
11 #include <fdt_support.h>
12 #include <fdt_simplefb.h>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_pl01x.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 static const struct bcm2835_gpio_platdata gpio_platdata = {
25 .base = BCM2835_GPIO_BASE,
28 U_BOOT_DEVICE(bcm2835_gpios) = {
29 .name = "gpio_bcm2835",
30 .platdata = &gpio_platdata,
33 static const struct pl01x_serial_platdata serial_platdata = {
34 #ifndef CONFIG_BCM2835
43 U_BOOT_DEVICE(bcm2835_serials) = {
44 .name = "serial_pl01x",
45 .platdata = &serial_platdata,
48 struct msg_get_arm_mem {
49 struct bcm2835_mbox_hdr hdr;
50 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
54 struct msg_get_board_rev {
55 struct bcm2835_mbox_hdr hdr;
56 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
60 struct msg_get_board_serial {
61 struct bcm2835_mbox_hdr hdr;
62 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
66 struct msg_get_mac_address {
67 struct bcm2835_mbox_hdr hdr;
68 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
72 struct msg_set_power_state {
73 struct bcm2835_mbox_hdr hdr;
74 struct bcm2835_mbox_tag_set_power_state set_power_state;
78 struct msg_get_clock_rate {
79 struct bcm2835_mbox_hdr hdr;
80 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
85 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
86 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
87 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
89 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
90 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
91 * Foundation stated that the following source was accurate:
92 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
100 static const struct rpi_model rpi_model_unknown = {
102 "bcm283x-rpi-other.dtb",
106 static const struct rpi_model rpi_models_new_scheme[] = {
109 "bcm2836-rpi-2-b.dtb",
114 "bcm2835-rpi-zero.dtb",
119 static const struct rpi_model rpi_models_old_scheme[] = {
132 "bcm2835-rpi-b-rev2.dtb",
137 "bcm2835-rpi-b-rev2.dtb",
142 "bcm2835-rpi-b-rev2.dtb",
162 "bcm2835-rpi-b-rev2.dtb",
167 "bcm2835-rpi-b-rev2.dtb",
172 "bcm2835-rpi-b-rev2.dtb",
177 "bcm2835-rpi-b-plus.dtb",
182 "bcm2835-rpi-cm.dtb",
187 "bcm2835-rpi-a-plus.dtb",
192 "bcm2835-rpi-b-plus.dtb",
197 "bcm2835-rpi-cm.dtb",
202 "bcm2835-rpi-a-plus.dtb",
207 static uint32_t revision;
208 static uint32_t rev_scheme;
209 static uint32_t rev_type;
210 static const struct rpi_model *model;
214 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
217 BCM2835_MBOX_INIT_HDR(msg);
218 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
220 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
222 printf("bcm2835: Could not query ARM memory size\n");
226 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
231 static void set_fdtfile(void)
235 if (getenv("fdtfile"))
238 fdtfile = model->fdtfile;
239 setenv("fdtfile", fdtfile);
242 static void set_usbethaddr(void)
244 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
247 if (!model->has_onboard_eth)
250 if (getenv("usbethaddr"))
253 BCM2835_MBOX_INIT_HDR(msg);
254 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
256 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
258 printf("bcm2835: Could not query MAC address\n");
259 /* Ignore error; not critical */
263 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
265 if (!getenv("ethaddr"))
266 setenv("ethaddr", getenv("usbethaddr"));
271 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
272 static void set_board_info(void)
276 snprintf(s, sizeof(s), "0x%X", revision);
277 setenv("board_revision", s);
278 snprintf(s, sizeof(s), "%d", rev_scheme);
279 setenv("board_rev_scheme", s);
280 /* Can't rename this to board_rev_type since it's an ABI for scripts */
281 snprintf(s, sizeof(s), "0x%X", rev_type);
282 setenv("board_rev", s);
283 setenv("board_name", model->name);
285 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
287 static void set_serial_number(void)
289 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
291 char serial_string[17] = { 0 };
293 if (getenv("serial#"))
296 BCM2835_MBOX_INIT_HDR(msg);
297 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
299 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
301 printf("bcm2835: Could not query board serial\n");
302 /* Ignore error; not critical */
306 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
307 msg->get_board_serial.body.resp.serial);
308 setenv("serial#", serial_string);
311 int misc_init_r(void)
315 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
323 static int power_on_module(u32 module)
325 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
328 BCM2835_MBOX_INIT_HDR(msg_pwr);
329 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
331 msg_pwr->set_power_state.body.req.device_id = module;
332 msg_pwr->set_power_state.body.req.state =
333 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
334 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
336 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
339 printf("bcm2835: Could not set module %u power state\n",
347 static void get_board_rev(void)
349 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
351 const struct rpi_model *models;
352 uint32_t models_count;
354 BCM2835_MBOX_INIT_HDR(msg);
355 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
357 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
359 printf("bcm2835: Could not query board revision\n");
360 /* Ignore error; not critical */
365 * For details of old-vs-new scheme, see:
366 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
367 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
370 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
371 * lower byte to use as the board rev:
372 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
373 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
375 revision = msg->get_board_rev.body.resp.rev;
376 if (revision & 0x800000) {
378 rev_type = (revision >> 4) & 0xff;
379 models = rpi_models_new_scheme;
380 models_count = ARRAY_SIZE(rpi_models_new_scheme);
383 rev_type = revision & 0xff;
384 models = rpi_models_old_scheme;
385 models_count = ARRAY_SIZE(rpi_models_old_scheme);
387 if (rev_type >= models_count) {
388 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
389 model = &rpi_model_unknown;
390 } else if (!models[rev_type].name) {
391 printf("RPI: Board rev 0x%x unknown\n", rev_type);
392 model = &rpi_model_unknown;
394 model = &models[rev_type];
397 printf("RPI %s (0x%x)\n", model->name, revision);
404 gd->bd->bi_boot_params = 0x100;
406 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
409 int board_mmc_init(bd_t *bis)
411 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
414 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
416 BCM2835_MBOX_INIT_HDR(msg_clk);
417 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
418 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
420 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
422 printf("bcm2835: Could not query eMMC clock rate\n");
426 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
427 msg_clk->get_clock_rate.body.resp.rate_hz);
430 int ft_board_setup(void *blob, bd_t *bd)
433 * For now, we simply always add the simplefb DT node. Later, we
434 * should be more intelligent, and e.g. only do this if no enabled DT
435 * node exists for the "real" graphics driver.
437 lcd_dt_simplefb_add_node(blob);