blob: c8924d43627cc364dae9e51f752af05d18282008 [file] [log] [blame]
Stephen Warren0d04f342012-08-05 16:07:22 +00001/*
Stephen Warrenf031f502016-03-24 22:15:20 -06002 * (C) Copyright 2012-2016 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>
Lubomir Rintel757cd142016-02-22 22:06:47 +01008#include <inttypes.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +00009#include <config.h>
Simon Glass41e98e02014-09-22 17:30:56 -060010#include <dm.h>
Alexander Graf1bcf7a32016-11-02 10:36:20 +010011#include <efi_loader.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020012#include <fdt_support.h>
Nikita Kiryanov033167c2015-02-03 13:32:31 +020013#include <fdt_simplefb.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +000014#include <lcd.h>
Simon Glasscf92e052015-09-02 17:24:58 -060015#include <memalign.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020016#include <mmc.h>
Simon Glass41e98e02014-09-22 17:30:56 -060017#include <asm/gpio.h>
Stephen Warren3f397782013-01-29 16:37:37 +000018#include <asm/arch/mbox.h>
Simon Glass70997d82017-04-05 16:23:36 -060019#include <asm/arch/msg.h>
Stephen Warren131a1e62013-01-29 16:37:42 +000020#include <asm/arch/sdhci.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000021#include <asm/global_data.h>
Stephen Warrenf031f502016-03-24 22:15:20 -060022#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warrend22a7652016-04-01 21:14:15 -060023#ifdef CONFIG_ARM64
24#include <asm/armv8/mmu.h>
25#endif
Paolo Pisati45a6d232017-02-10 17:28:05 +010026#include <watchdog.h>
Alexander Grafcaf22332018-01-23 18:05:21 +010027#include <dm/pinctrl.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000028
29DECLARE_GLOBAL_DATA_PTR;
30
Cédric Schieliade243a2016-11-11 11:59:07 +010031/* From lowlevel_init.S */
32extern unsigned long fw_dtb_pointer;
33
Simon Glass3e167052017-04-05 16:23:45 -060034/* TODO(sjg@chromium.org): Move these to the msg.c file */
Stephen Warren3f397782013-01-29 16:37:37 +000035struct msg_get_arm_mem {
36 struct bcm2835_mbox_hdr hdr;
37 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
38 u32 end_tag;
39};
40
Stephen Warren6fe78452014-11-18 21:40:21 -070041struct msg_get_board_rev {
42 struct bcm2835_mbox_hdr hdr;
43 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
44 u32 end_tag;
45};
46
Lubomir Rintel757cd142016-02-22 22:06:47 +010047struct msg_get_board_serial {
48 struct bcm2835_mbox_hdr hdr;
49 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
50 u32 end_tag;
51};
52
Stephen Warren4f80a062014-09-26 20:51:39 -060053struct msg_get_mac_address {
54 struct bcm2835_mbox_hdr hdr;
55 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
56 u32 end_tag;
57};
58
Stephen Warren131a1e62013-01-29 16:37:42 +000059struct msg_get_clock_rate {
60 struct bcm2835_mbox_hdr hdr;
61 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
62 u32 end_tag;
63};
64
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020065#ifdef CONFIG_ARM64
66#define DTB_DIR "broadcom/"
67#else
68#define DTB_DIR ""
69#endif
70
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070071/*
72 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
73 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070074 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
Stephen Warrendba060c2016-01-28 22:24:44 -070075 *
76 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
77 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
78 * Foundation stated that the following source was accurate:
79 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070080 */
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070081struct rpi_model {
Stephen Warren6fe78452014-11-18 21:40:21 -070082 const char *name;
83 const char *fdtfile;
Stephen Warren3207d8f2014-12-05 20:56:46 -070084 bool has_onboard_eth;
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070085};
86
87static const struct rpi_model rpi_model_unknown = {
88 "Unknown model",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020089 DTB_DIR "bcm283x-rpi-other.dtb",
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070090 false,
91};
92
93static const struct rpi_model rpi_models_new_scheme[] = {
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070094 [0x4] = {
Stephen Warren46414292015-02-16 12:16:15 -070095 "2 Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020096 DTB_DIR "bcm2836-rpi-2-b.dtb",
Stephen Warren46414292015-02-16 12:16:15 -070097 true,
98 },
Stephen Warren7233fb32016-03-24 22:15:18 -060099 [0x8] = {
100 "3 Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200101 DTB_DIR "bcm2837-rpi-3-b.dtb",
Stephen Warren7233fb32016-03-24 22:15:18 -0600102 true,
103 },
Stephen Warrenaf7c03e2015-12-04 22:07:46 -0700104 [0x9] = {
105 "Zero",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200106 DTB_DIR "bcm2835-rpi-zero.dtb",
Stephen Warrenaf7c03e2015-12-04 22:07:46 -0700107 false,
108 },
Dmitry Korunov89930562017-11-26 13:38:53 +0400109 [0xC] = {
110 "Zero W",
111 DTB_DIR "bcm2835-rpi-zero-w.dtb",
112 false,
113 },
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700114};
115
116static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700117 [0x2] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100118 "Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200119 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700120 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700121 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700122 [0x3] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100123 "Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200124 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700125 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700126 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700127 [0x4] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100128 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200129 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700130 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700131 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700132 [0x5] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100133 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200134 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700135 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700136 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700137 [0x6] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100138 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200139 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700140 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700141 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700142 [0x7] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700143 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200144 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700145 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700146 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700147 [0x8] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700148 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200149 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700150 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700151 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700152 [0x9] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700153 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200154 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700155 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700156 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700157 [0xd] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700158 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200159 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700160 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700161 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700162 [0xe] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700163 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200164 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700165 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700166 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700167 [0xf] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700168 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200169 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700170 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700171 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700172 [0x10] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700173 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200174 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700175 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700176 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700177 [0x11] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700178 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200179 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700180 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700181 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700182 [0x12] = {
Stephen Warren47705ef2014-12-23 20:01:43 -0700183 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200184 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren47705ef2014-12-23 20:01:43 -0700185 false,
186 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700187 [0x13] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600188 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200189 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600190 true,
191 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700192 [0x14] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600193 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200194 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600195 false,
196 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700197 [0x15] = {
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200198 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200199 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200200 false,
201 },
Stephen Warren6fe78452014-11-18 21:40:21 -0700202};
203
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700204static uint32_t revision;
205static uint32_t rev_scheme;
206static uint32_t rev_type;
207static const struct rpi_model *model;
Stephen Warren6fe78452014-11-18 21:40:21 -0700208
Stephen Warrend22a7652016-04-01 21:14:15 -0600209#ifdef CONFIG_ARM64
210static struct mm_region bcm2837_mem_map[] = {
211 {
York Suncd4b0c52016-06-24 16:46:22 -0700212 .virt = 0x00000000UL,
213 .phys = 0x00000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600214 .size = 0x3f000000UL,
215 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
216 PTE_BLOCK_INNER_SHARE
217 }, {
York Suncd4b0c52016-06-24 16:46:22 -0700218 .virt = 0x3f000000UL,
219 .phys = 0x3f000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600220 .size = 0x01000000UL,
221 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
222 PTE_BLOCK_NON_SHARE |
223 PTE_BLOCK_PXN | PTE_BLOCK_UXN
224 }, {
225 /* List terminator */
226 0,
227 }
228};
229
230struct mm_region *mem_map = bcm2837_mem_map;
231#endif
232
Stephen Warren0d04f342012-08-05 16:07:22 +0000233int dram_init(void)
234{
Alexander Stein927753a2015-07-24 09:22:12 +0200235 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warren3f397782013-01-29 16:37:37 +0000236 int ret;
237
238 BCM2835_MBOX_INIT_HDR(msg);
239 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
240
241 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
242 if (ret) {
243 printf("bcm2835: Could not query ARM memory size\n");
244 return -1;
245 }
246
247 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0d04f342012-08-05 16:07:22 +0000248
249 return 0;
250}
251
Stephen Warren6fe78452014-11-18 21:40:21 -0700252static void set_fdtfile(void)
253{
254 const char *fdtfile;
255
Simon Glass00caae62017-08-03 12:22:12 -0600256 if (env_get("fdtfile"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700257 return;
258
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700259 fdtfile = model->fdtfile;
Simon Glass382bee52017-08-03 12:22:09 -0600260 env_set("fdtfile", fdtfile);
Stephen Warren6fe78452014-11-18 21:40:21 -0700261}
262
Cédric Schieliade243a2016-11-11 11:59:07 +0100263/*
264 * If the firmware provided a valid FDT at boot time, let's expose it in
265 * ${fdt_addr} so it may be passed unmodified to the kernel.
266 */
267static void set_fdt_addr(void)
268{
Simon Glass00caae62017-08-03 12:22:12 -0600269 if (env_get("fdt_addr"))
Cédric Schieliade243a2016-11-11 11:59:07 +0100270 return;
271
272 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
273 return;
274
Simon Glass018f5302017-08-03 12:22:10 -0600275 env_set_hex("fdt_addr", fw_dtb_pointer);
Cédric Schieliade243a2016-11-11 11:59:07 +0100276}
277
278/*
279 * Prevent relocation from stomping on a firmware provided FDT blob.
280 */
281unsigned long board_get_usable_ram_top(unsigned long total_size)
282{
283 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
284 return gd->ram_top;
285 return fw_dtb_pointer & ~0xffff;
286}
287
Stephen Warren6fe78452014-11-18 21:40:21 -0700288static void set_usbethaddr(void)
Stephen Warren4f80a062014-09-26 20:51:39 -0600289{
Alexander Stein927753a2015-07-24 09:22:12 +0200290 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warren4f80a062014-09-26 20:51:39 -0600291 int ret;
292
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700293 if (!model->has_onboard_eth)
Stephen Warren3207d8f2014-12-05 20:56:46 -0700294 return;
295
Simon Glass00caae62017-08-03 12:22:12 -0600296 if (env_get("usbethaddr"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700297 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600298
299 BCM2835_MBOX_INIT_HDR(msg);
300 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
301
302 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
303 if (ret) {
304 printf("bcm2835: Could not query MAC address\n");
305 /* Ignore error; not critical */
Stephen Warren6fe78452014-11-18 21:40:21 -0700306 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600307 }
308
Simon Glassfd1e9592017-08-03 12:22:11 -0600309 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
Stephen Warren4f80a062014-09-26 20:51:39 -0600310
Simon Glass00caae62017-08-03 12:22:12 -0600311 if (!env_get("ethaddr"))
312 env_set("ethaddr", env_get("usbethaddr"));
Lubomir Rintel859f1432016-02-03 16:08:09 +0100313
Stephen Warren6fe78452014-11-18 21:40:21 -0700314 return;
315}
316
Guillaume GARDETbff78562015-08-25 15:10:26 +0200317#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
318static void set_board_info(void)
319{
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700320 char s[11];
321
322 snprintf(s, sizeof(s), "0x%X", revision);
Simon Glass382bee52017-08-03 12:22:09 -0600323 env_set("board_revision", s);
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700324 snprintf(s, sizeof(s), "%d", rev_scheme);
Simon Glass382bee52017-08-03 12:22:09 -0600325 env_set("board_rev_scheme", s);
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700326 /* Can't rename this to board_rev_type since it's an ABI for scripts */
327 snprintf(s, sizeof(s), "0x%X", rev_type);
Simon Glass382bee52017-08-03 12:22:09 -0600328 env_set("board_rev", s);
329 env_set("board_name", model->name);
Guillaume GARDETbff78562015-08-25 15:10:26 +0200330}
331#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
332
Lubomir Rintel757cd142016-02-22 22:06:47 +0100333static void set_serial_number(void)
334{
335 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
336 int ret;
337 char serial_string[17] = { 0 };
338
Simon Glass00caae62017-08-03 12:22:12 -0600339 if (env_get("serial#"))
Lubomir Rintel757cd142016-02-22 22:06:47 +0100340 return;
341
342 BCM2835_MBOX_INIT_HDR(msg);
343 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
344
345 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
346 if (ret) {
347 printf("bcm2835: Could not query board serial\n");
348 /* Ignore error; not critical */
349 return;
350 }
351
352 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
353 msg->get_board_serial.body.resp.serial);
Simon Glass382bee52017-08-03 12:22:09 -0600354 env_set("serial#", serial_string);
Lubomir Rintel757cd142016-02-22 22:06:47 +0100355}
356
Stephen Warren6fe78452014-11-18 21:40:21 -0700357int misc_init_r(void)
358{
Cédric Schieliade243a2016-11-11 11:59:07 +0100359 set_fdt_addr();
Stephen Warren6fe78452014-11-18 21:40:21 -0700360 set_fdtfile();
361 set_usbethaddr();
Guillaume GARDETbff78562015-08-25 15:10:26 +0200362#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
363 set_board_info();
364#endif
Lubomir Rintel757cd142016-02-22 22:06:47 +0100365 set_serial_number();
366
Stephen Warren4f80a062014-09-26 20:51:39 -0600367 return 0;
368}
369
Stephen Warren6fe78452014-11-18 21:40:21 -0700370static void get_board_rev(void)
371{
Alexander Stein927753a2015-07-24 09:22:12 +0200372 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warren6fe78452014-11-18 21:40:21 -0700373 int ret;
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700374 const struct rpi_model *models;
375 uint32_t models_count;
Stephen Warren6fe78452014-11-18 21:40:21 -0700376
377 BCM2835_MBOX_INIT_HDR(msg);
378 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
379
380 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
381 if (ret) {
382 printf("bcm2835: Could not query board revision\n");
383 /* Ignore error; not critical */
384 return;
385 }
386
Stephen Warren46414292015-02-16 12:16:15 -0700387 /*
388 * For details of old-vs-new scheme, see:
389 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
390 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
391 * (a few posts down)
Stephen Warren95b4f112015-03-23 23:00:25 -0600392 *
393 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
394 * lower byte to use as the board rev:
395 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
396 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warren46414292015-02-16 12:16:15 -0700397 */
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700398 revision = msg->get_board_rev.body.resp.rev;
399 if (revision & 0x800000) {
400 rev_scheme = 1;
401 rev_type = (revision >> 4) & 0xff;
402 models = rpi_models_new_scheme;
403 models_count = ARRAY_SIZE(rpi_models_new_scheme);
404 } else {
405 rev_scheme = 0;
406 rev_type = revision & 0xff;
407 models = rpi_models_old_scheme;
408 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren47705ef2014-12-23 20:01:43 -0700409 }
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700410 if (rev_type >= models_count) {
411 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
412 model = &rpi_model_unknown;
413 } else if (!models[rev_type].name) {
414 printf("RPI: Board rev 0x%x unknown\n", rev_type);
415 model = &rpi_model_unknown;
416 } else {
417 model = &models[rev_type];
Stephen Warren914627f2014-12-23 20:01:44 -0700418 }
Stephen Warren6fe78452014-11-18 21:40:21 -0700419
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700420 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warren6fe78452014-11-18 21:40:21 -0700421}
422
Alexander Graf601147b2016-08-15 17:48:51 +0200423#ifndef CONFIG_PL01X_SERIAL
424static bool rpi_is_serial_active(void)
425{
426 int serial_gpio = 15;
427 struct udevice *dev;
428
429 /*
430 * The RPi3 disables the mini uart by default. The easiest way to find
431 * out whether it is available is to check if the RX pin is muxed.
432 */
433
Alexander Grafcaf22332018-01-23 18:05:21 +0100434 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
Alexander Graf601147b2016-08-15 17:48:51 +0200435 return true;
436
Alexander Grafcaf22332018-01-23 18:05:21 +0100437 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
Alexander Graf601147b2016-08-15 17:48:51 +0200438 return false;
439
440 return true;
441}
Fabian Vogtd8396a32016-09-26 14:26:50 +0200442
443/* Disable mini-UART I/O if it's not pinmuxed to our pins.
444 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
445 */
446static void rpi_disable_inactive_uart(void)
447{
448 struct udevice *dev;
449 struct bcm283x_mu_serial_platdata *plat;
450
451 if (uclass_get_device_by_driver(UCLASS_SERIAL,
452 DM_GET_DRIVER(serial_bcm283x_mu),
453 &dev) || !dev)
454 return;
455
456 if (!rpi_is_serial_active()) {
457 plat = dev_get_platdata(dev);
458 plat->disabled = true;
459 }
460}
Alexander Graf601147b2016-08-15 17:48:51 +0200461#endif
462
Fabian Vogtd8396a32016-09-26 14:26:50 +0200463int board_init(void)
Alexander Graf601147b2016-08-15 17:48:51 +0200464{
Paolo Pisati45a6d232017-02-10 17:28:05 +0100465#ifdef CONFIG_HW_WATCHDOG
466 hw_watchdog_init();
467#endif
Alexander Graf601147b2016-08-15 17:48:51 +0200468#ifndef CONFIG_PL01X_SERIAL
Fabian Vogtd8396a32016-09-26 14:26:50 +0200469 rpi_disable_inactive_uart();
Alexander Graf601147b2016-08-15 17:48:51 +0200470#endif
471
Fabian Vogtd8396a32016-09-26 14:26:50 +0200472 get_board_rev();
473
474 gd->bd->bi_boot_params = 0x100;
475
Simon Glass70997d82017-04-05 16:23:36 -0600476 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Graf601147b2016-08-15 17:48:51 +0200477}
478
Alex Deymo82f766d2017-04-02 01:25:20 -0700479/*
480 * If the firmware passed a device tree use it for U-Boot.
481 */
482void *board_fdt_blob_setup(void)
483{
484 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
485 return NULL;
486 return (void *)fw_dtb_pointer;
487}
488
Simon Glasse895a4b2014-10-23 18:58:47 -0600489int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenea697ae2013-05-27 18:31:18 +0000490{
491 /*
492 * For now, we simply always add the simplefb DT node. Later, we
493 * should be more intelligent, and e.g. only do this if no enabled DT
494 * node exists for the "real" graphics driver.
495 */
496 lcd_dt_simplefb_add_node(blob);
Simon Glasse895a4b2014-10-23 18:58:47 -0600497
Alexander Graf1bcf7a32016-11-02 10:36:20 +0100498#ifdef CONFIG_EFI_LOADER
499 /* Reserve the spin table */
500 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
501#endif
502
Simon Glasse895a4b2014-10-23 18:58:47 -0600503 return 0;
Stephen Warrenea697ae2013-05-27 18:31:18 +0000504}