blob: 530f1496171b406875b2d31d6c08f30aeadf306b [file] [log] [blame]
Stephen Warren0e012c32012-08-05 16:07:22 +00001/*
Stephen Warrend6f47c82016-03-24 22:15:20 -06002 * (C) Copyright 2012-2016 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>
Lubomir Rintel7d33bb62016-02-22 22:06:47 +01008#include <inttypes.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +00009#include <config.h>
Simon Glass74807622014-09-22 17:30:56 -060010#include <dm.h>
Alexander Graf013e9ae2016-11-02 10:36:20 +010011#include <efi_loader.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020012#include <fdt_support.h>
Nikita Kiryanovb071e4c2015-02-03 13:32:31 +020013#include <fdt_simplefb.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +000014#include <lcd.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020016#include <mmc.h>
Simon Glass74807622014-09-22 17:30:56 -060017#include <asm/gpio.h>
Stephen Warrenacc8bf42013-01-29 16:37:37 +000018#include <asm/arch/mbox.h>
Simon Glass558ec672017-04-05 16:23:36 -060019#include <asm/arch/msg.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000020#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000021#include <asm/global_data.h>
Stephen Warrend6f47c82016-03-24 22:15:20 -060022#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warren93ea5262016-04-01 21:14:15 -060023#ifdef CONFIG_ARM64
24#include <asm/armv8/mmu.h>
25#endif
Paolo Pisati6213c552017-02-10 17:28:05 +010026#include <watchdog.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000027
28DECLARE_GLOBAL_DATA_PTR;
29
Cédric Schieli9473f532016-11-11 11:59:07 +010030/* From lowlevel_init.S */
31extern unsigned long fw_dtb_pointer;
32
Simon Glass56991cd2017-04-05 16:23:45 -060033/* TODO(sjg@chromium.org): Move these to the msg.c file */
Stephen Warrenacc8bf42013-01-29 16:37:37 +000034struct 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
Stephen Warrencd210c12014-11-18 21:40:21 -070040struct msg_get_board_rev {
41 struct bcm2835_mbox_hdr hdr;
42 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
43 u32 end_tag;
44};
45
Lubomir Rintel7d33bb62016-02-22 22:06:47 +010046struct msg_get_board_serial {
47 struct bcm2835_mbox_hdr hdr;
48 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
49 u32 end_tag;
50};
51
Stephen Warrenaf6e20d2014-09-26 20:51:39 -060052struct msg_get_mac_address {
53 struct bcm2835_mbox_hdr hdr;
54 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
55 u32 end_tag;
56};
57
Stephen Warrenc4ab9712013-01-29 16:37:42 +000058struct msg_get_clock_rate {
59 struct bcm2835_mbox_hdr hdr;
60 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
61 u32 end_tag;
62};
63
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020064#ifdef CONFIG_ARM64
65#define DTB_DIR "broadcom/"
66#else
67#define DTB_DIR ""
68#endif
69
Stephen Warrenbe8efec2015-12-04 22:07:44 -070070/*
71 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
72 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
Stephen Warren26683852015-12-04 22:07:45 -070073 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
Stephen Warrena33dc8b2016-01-28 22:24:44 -070074 *
75 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
76 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
77 * Foundation stated that the following source was accurate:
78 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
Stephen Warrenbe8efec2015-12-04 22:07:44 -070079 */
Stephen Warren26683852015-12-04 22:07:45 -070080struct rpi_model {
Stephen Warrencd210c12014-11-18 21:40:21 -070081 const char *name;
82 const char *fdtfile;
Stephen Warrenb2fa38362014-12-05 20:56:46 -070083 bool has_onboard_eth;
Stephen Warren26683852015-12-04 22:07:45 -070084};
85
86static const struct rpi_model rpi_model_unknown = {
87 "Unknown model",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020088 DTB_DIR "bcm283x-rpi-other.dtb",
Stephen Warren26683852015-12-04 22:07:45 -070089 false,
90};
91
92static const struct rpi_model rpi_models_new_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -070093 [0x4] = {
Stephen Warrendc7ea682015-02-16 12:16:15 -070094 "2 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020095 DTB_DIR "bcm2836-rpi-2-b.dtb",
Stephen Warrendc7ea682015-02-16 12:16:15 -070096 true,
97 },
Stephen Warren303244a2016-03-24 22:15:18 -060098 [0x8] = {
99 "3 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200100 DTB_DIR "bcm2837-rpi-3-b.dtb",
Stephen Warren303244a2016-03-24 22:15:18 -0600101 true,
102 },
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700103 [0x9] = {
104 "Zero",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200105 DTB_DIR "bcm2835-rpi-zero.dtb",
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700106 false,
107 },
Stephen Warren26683852015-12-04 22:07:45 -0700108};
109
110static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700111 [0x2] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100112 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200113 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700114 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700115 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700116 [0x3] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100117 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200118 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700119 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700120 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700121 [0x4] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100122 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200123 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700124 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700125 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700126 [0x5] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100127 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200128 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700129 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700130 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700131 [0x6] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100132 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200133 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700134 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700135 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700136 [0x7] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700137 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200138 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700139 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700140 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700141 [0x8] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700142 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200143 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700144 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700145 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700146 [0x9] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700147 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200148 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700149 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700150 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700151 [0xd] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700152 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200153 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700154 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700155 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700156 [0xe] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700157 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200158 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700159 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700160 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700161 [0xf] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700162 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200163 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700164 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700165 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700166 [0x10] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700167 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200168 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700169 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700170 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700171 [0x11] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700172 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200173 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700174 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700175 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700176 [0x12] = {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700177 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200178 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren18a5fc62014-12-23 20:01:43 -0700179 false,
180 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700181 [0x13] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600182 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200183 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600184 true,
185 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700186 [0x14] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600187 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200188 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600189 false,
190 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700191 [0x15] = {
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200192 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200193 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200194 false,
195 },
Stephen Warrencd210c12014-11-18 21:40:21 -0700196};
197
Stephen Warren26683852015-12-04 22:07:45 -0700198static uint32_t revision;
199static uint32_t rev_scheme;
200static uint32_t rev_type;
201static const struct rpi_model *model;
Stephen Warrencd210c12014-11-18 21:40:21 -0700202
Stephen Warren93ea5262016-04-01 21:14:15 -0600203#ifdef CONFIG_ARM64
204static struct mm_region bcm2837_mem_map[] = {
205 {
York Sunc7104e52016-06-24 16:46:22 -0700206 .virt = 0x00000000UL,
207 .phys = 0x00000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600208 .size = 0x3f000000UL,
209 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
210 PTE_BLOCK_INNER_SHARE
211 }, {
York Sunc7104e52016-06-24 16:46:22 -0700212 .virt = 0x3f000000UL,
213 .phys = 0x3f000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600214 .size = 0x01000000UL,
215 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
216 PTE_BLOCK_NON_SHARE |
217 PTE_BLOCK_PXN | PTE_BLOCK_UXN
218 }, {
219 /* List terminator */
220 0,
221 }
222};
223
224struct mm_region *mem_map = bcm2837_mem_map;
225#endif
226
Stephen Warren0e012c32012-08-05 16:07:22 +0000227int dram_init(void)
228{
Alexander Steinc56c9472015-07-24 09:22:12 +0200229 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000230 int ret;
231
232 BCM2835_MBOX_INIT_HDR(msg);
233 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
234
235 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
236 if (ret) {
237 printf("bcm2835: Could not query ARM memory size\n");
238 return -1;
239 }
240
241 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000242
243 return 0;
244}
245
Stephen Warrencd210c12014-11-18 21:40:21 -0700246static void set_fdtfile(void)
247{
248 const char *fdtfile;
249
Simon Glass64b723f2017-08-03 12:22:12 -0600250 if (env_get("fdtfile"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700251 return;
252
Stephen Warren26683852015-12-04 22:07:45 -0700253 fdtfile = model->fdtfile;
Simon Glass6a38e412017-08-03 12:22:09 -0600254 env_set("fdtfile", fdtfile);
Stephen Warrencd210c12014-11-18 21:40:21 -0700255}
256
Cédric Schieli9473f532016-11-11 11:59:07 +0100257/*
258 * If the firmware provided a valid FDT at boot time, let's expose it in
259 * ${fdt_addr} so it may be passed unmodified to the kernel.
260 */
261static void set_fdt_addr(void)
262{
Simon Glass64b723f2017-08-03 12:22:12 -0600263 if (env_get("fdt_addr"))
Cédric Schieli9473f532016-11-11 11:59:07 +0100264 return;
265
266 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
267 return;
268
Simon Glass4d949a22017-08-03 12:22:10 -0600269 env_set_hex("fdt_addr", fw_dtb_pointer);
Cédric Schieli9473f532016-11-11 11:59:07 +0100270}
271
272/*
273 * Prevent relocation from stomping on a firmware provided FDT blob.
274 */
275unsigned long board_get_usable_ram_top(unsigned long total_size)
276{
277 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
278 return gd->ram_top;
279 return fw_dtb_pointer & ~0xffff;
280}
281
Stephen Warrencd210c12014-11-18 21:40:21 -0700282static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600283{
Alexander Steinc56c9472015-07-24 09:22:12 +0200284 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600285 int ret;
286
Stephen Warren26683852015-12-04 22:07:45 -0700287 if (!model->has_onboard_eth)
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700288 return;
289
Simon Glass64b723f2017-08-03 12:22:12 -0600290 if (env_get("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700291 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600292
293 BCM2835_MBOX_INIT_HDR(msg);
294 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
295
296 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
297 if (ret) {
298 printf("bcm2835: Could not query MAC address\n");
299 /* Ignore error; not critical */
Stephen Warrencd210c12014-11-18 21:40:21 -0700300 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600301 }
302
Simon Glass8551d552017-08-03 12:22:11 -0600303 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600304
Simon Glass64b723f2017-08-03 12:22:12 -0600305 if (!env_get("ethaddr"))
306 env_set("ethaddr", env_get("usbethaddr"));
Lubomir Rintel3ac09f82016-02-03 16:08:09 +0100307
Stephen Warrencd210c12014-11-18 21:40:21 -0700308 return;
309}
310
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200311#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
312static void set_board_info(void)
313{
Stephen Warren26683852015-12-04 22:07:45 -0700314 char s[11];
315
316 snprintf(s, sizeof(s), "0x%X", revision);
Simon Glass6a38e412017-08-03 12:22:09 -0600317 env_set("board_revision", s);
Stephen Warren26683852015-12-04 22:07:45 -0700318 snprintf(s, sizeof(s), "%d", rev_scheme);
Simon Glass6a38e412017-08-03 12:22:09 -0600319 env_set("board_rev_scheme", s);
Stephen Warren26683852015-12-04 22:07:45 -0700320 /* Can't rename this to board_rev_type since it's an ABI for scripts */
321 snprintf(s, sizeof(s), "0x%X", rev_type);
Simon Glass6a38e412017-08-03 12:22:09 -0600322 env_set("board_rev", s);
323 env_set("board_name", model->name);
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200324}
325#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
326
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100327static void set_serial_number(void)
328{
329 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
330 int ret;
331 char serial_string[17] = { 0 };
332
Simon Glass64b723f2017-08-03 12:22:12 -0600333 if (env_get("serial#"))
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100334 return;
335
336 BCM2835_MBOX_INIT_HDR(msg);
337 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
338
339 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
340 if (ret) {
341 printf("bcm2835: Could not query board serial\n");
342 /* Ignore error; not critical */
343 return;
344 }
345
346 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
347 msg->get_board_serial.body.resp.serial);
Simon Glass6a38e412017-08-03 12:22:09 -0600348 env_set("serial#", serial_string);
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100349}
350
Stephen Warrencd210c12014-11-18 21:40:21 -0700351int misc_init_r(void)
352{
Cédric Schieli9473f532016-11-11 11:59:07 +0100353 set_fdt_addr();
Stephen Warrencd210c12014-11-18 21:40:21 -0700354 set_fdtfile();
355 set_usbethaddr();
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200356#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
357 set_board_info();
358#endif
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100359 set_serial_number();
360
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600361 return 0;
362}
363
Stephen Warrencd210c12014-11-18 21:40:21 -0700364static void get_board_rev(void)
365{
Alexander Steinc56c9472015-07-24 09:22:12 +0200366 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warrencd210c12014-11-18 21:40:21 -0700367 int ret;
Stephen Warren26683852015-12-04 22:07:45 -0700368 const struct rpi_model *models;
369 uint32_t models_count;
Stephen Warrencd210c12014-11-18 21:40:21 -0700370
371 BCM2835_MBOX_INIT_HDR(msg);
372 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
373
374 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
375 if (ret) {
376 printf("bcm2835: Could not query board revision\n");
377 /* Ignore error; not critical */
378 return;
379 }
380
Stephen Warrendc7ea682015-02-16 12:16:15 -0700381 /*
382 * For details of old-vs-new scheme, see:
383 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
384 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
385 * (a few posts down)
Stephen Warrenb834af82015-03-23 23:00:25 -0600386 *
387 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
388 * lower byte to use as the board rev:
389 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
390 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warrendc7ea682015-02-16 12:16:15 -0700391 */
Stephen Warren26683852015-12-04 22:07:45 -0700392 revision = msg->get_board_rev.body.resp.rev;
393 if (revision & 0x800000) {
394 rev_scheme = 1;
395 rev_type = (revision >> 4) & 0xff;
396 models = rpi_models_new_scheme;
397 models_count = ARRAY_SIZE(rpi_models_new_scheme);
398 } else {
399 rev_scheme = 0;
400 rev_type = revision & 0xff;
401 models = rpi_models_old_scheme;
402 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren18a5fc62014-12-23 20:01:43 -0700403 }
Stephen Warren26683852015-12-04 22:07:45 -0700404 if (rev_type >= models_count) {
405 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
406 model = &rpi_model_unknown;
407 } else if (!models[rev_type].name) {
408 printf("RPI: Board rev 0x%x unknown\n", rev_type);
409 model = &rpi_model_unknown;
410 } else {
411 model = &models[rev_type];
Stephen Warren18a5fc62014-12-23 20:01:43 -0700412 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700413
Stephen Warren26683852015-12-04 22:07:45 -0700414 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warrencd210c12014-11-18 21:40:21 -0700415}
416
Alexander Grafafb99072016-08-15 17:48:51 +0200417#ifndef CONFIG_PL01X_SERIAL
418static bool rpi_is_serial_active(void)
419{
420 int serial_gpio = 15;
421 struct udevice *dev;
422
423 /*
424 * The RPi3 disables the mini uart by default. The easiest way to find
425 * out whether it is available is to check if the RX pin is muxed.
426 */
427
428 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
429 return true;
430
431 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
432 return false;
433
434 return true;
435}
Fabian Vogt1d317b72016-09-26 14:26:50 +0200436
437/* Disable mini-UART I/O if it's not pinmuxed to our pins.
438 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
439 */
440static void rpi_disable_inactive_uart(void)
441{
442 struct udevice *dev;
443 struct bcm283x_mu_serial_platdata *plat;
444
445 if (uclass_get_device_by_driver(UCLASS_SERIAL,
446 DM_GET_DRIVER(serial_bcm283x_mu),
447 &dev) || !dev)
448 return;
449
450 if (!rpi_is_serial_active()) {
451 plat = dev_get_platdata(dev);
452 plat->disabled = true;
453 }
454}
Alexander Grafafb99072016-08-15 17:48:51 +0200455#endif
456
Fabian Vogt1d317b72016-09-26 14:26:50 +0200457int board_init(void)
Alexander Grafafb99072016-08-15 17:48:51 +0200458{
Paolo Pisati6213c552017-02-10 17:28:05 +0100459#ifdef CONFIG_HW_WATCHDOG
460 hw_watchdog_init();
461#endif
Alexander Grafafb99072016-08-15 17:48:51 +0200462#ifndef CONFIG_PL01X_SERIAL
Fabian Vogt1d317b72016-09-26 14:26:50 +0200463 rpi_disable_inactive_uart();
Alexander Grafafb99072016-08-15 17:48:51 +0200464#endif
465
Fabian Vogt1d317b72016-09-26 14:26:50 +0200466 get_board_rev();
467
468 gd->bd->bi_boot_params = 0x100;
469
Simon Glass558ec672017-04-05 16:23:36 -0600470 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Grafafb99072016-08-15 17:48:51 +0200471}
472
Alex Deymo5b661ec2017-04-02 01:25:20 -0700473/*
474 * If the firmware passed a device tree use it for U-Boot.
475 */
476void *board_fdt_blob_setup(void)
477{
478 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
479 return NULL;
480 return (void *)fw_dtb_pointer;
481}
482
Simon Glass2aec3cc2014-10-23 18:58:47 -0600483int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000484{
485 /*
486 * For now, we simply always add the simplefb DT node. Later, we
487 * should be more intelligent, and e.g. only do this if no enabled DT
488 * node exists for the "real" graphics driver.
489 */
490 lcd_dt_simplefb_add_node(blob);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600491
Alexander Graf013e9ae2016-11-02 10:36:20 +0100492#ifdef CONFIG_EFI_LOADER
493 /* Reserve the spin table */
494 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
495#endif
496
Simon Glass2aec3cc2014-10-23 18:58:47 -0600497 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000498}