blob: 30495057d1759e295a54996bddf0a0f35f448283 [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>
Alex Kiernan9c215492018-04-01 09:22:38 +000011#include <environment.h>
Alexander Graf013e9ae2016-11-02 10:36:20 +010012#include <efi_loader.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020013#include <fdt_support.h>
Nikita Kiryanovb071e4c2015-02-03 13:32:31 +020014#include <fdt_simplefb.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +000015#include <lcd.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060016#include <memalign.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020017#include <mmc.h>
Simon Glass74807622014-09-22 17:30:56 -060018#include <asm/gpio.h>
Stephen Warrenacc8bf42013-01-29 16:37:37 +000019#include <asm/arch/mbox.h>
Simon Glass558ec672017-04-05 16:23:36 -060020#include <asm/arch/msg.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000021#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000022#include <asm/global_data.h>
Stephen Warrend6f47c82016-03-24 22:15:20 -060023#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warren93ea5262016-04-01 21:14:15 -060024#ifdef CONFIG_ARM64
25#include <asm/armv8/mmu.h>
26#endif
Paolo Pisati6213c552017-02-10 17:28:05 +010027#include <watchdog.h>
Alexander Graffce8e5c2018-01-23 18:05:21 +010028#include <dm/pinctrl.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000029
30DECLARE_GLOBAL_DATA_PTR;
31
Cédric Schieli9473f532016-11-11 11:59:07 +010032/* From lowlevel_init.S */
33extern unsigned long fw_dtb_pointer;
34
Simon Glass56991cd2017-04-05 16:23:45 -060035/* TODO(sjg@chromium.org): Move these to the msg.c file */
Stephen Warrenacc8bf42013-01-29 16:37:37 +000036struct msg_get_arm_mem {
37 struct bcm2835_mbox_hdr hdr;
38 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
39 u32 end_tag;
40};
41
Stephen Warrencd210c12014-11-18 21:40:21 -070042struct msg_get_board_rev {
43 struct bcm2835_mbox_hdr hdr;
44 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
45 u32 end_tag;
46};
47
Lubomir Rintel7d33bb62016-02-22 22:06:47 +010048struct msg_get_board_serial {
49 struct bcm2835_mbox_hdr hdr;
50 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
51 u32 end_tag;
52};
53
Stephen Warrenaf6e20d2014-09-26 20:51:39 -060054struct msg_get_mac_address {
55 struct bcm2835_mbox_hdr hdr;
56 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
57 u32 end_tag;
58};
59
Stephen Warrenc4ab9712013-01-29 16:37:42 +000060struct msg_get_clock_rate {
61 struct bcm2835_mbox_hdr hdr;
62 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
63 u32 end_tag;
64};
65
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020066#ifdef CONFIG_ARM64
67#define DTB_DIR "broadcom/"
68#else
69#define DTB_DIR ""
70#endif
71
Stephen Warrenbe8efec2015-12-04 22:07:44 -070072/*
73 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
74 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
Stephen Warren26683852015-12-04 22:07:45 -070075 * 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 -070076 *
77 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
78 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
79 * Foundation stated that the following source was accurate:
80 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
Stephen Warrenbe8efec2015-12-04 22:07:44 -070081 */
Stephen Warren26683852015-12-04 22:07:45 -070082struct rpi_model {
Stephen Warrencd210c12014-11-18 21:40:21 -070083 const char *name;
84 const char *fdtfile;
Stephen Warrenb2fa38362014-12-05 20:56:46 -070085 bool has_onboard_eth;
Stephen Warren26683852015-12-04 22:07:45 -070086};
87
88static const struct rpi_model rpi_model_unknown = {
89 "Unknown model",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020090 DTB_DIR "bcm283x-rpi-other.dtb",
Stephen Warren26683852015-12-04 22:07:45 -070091 false,
92};
93
94static const struct rpi_model rpi_models_new_scheme[] = {
Jonathan Grayde4524b2018-04-06 18:45:49 +100095 [0x0] = {
96 "Model A",
97 DTB_DIR "bcm2835-rpi-a.dtb",
98 false,
99 },
100 [0x1] = {
101 "Model B",
102 DTB_DIR "bcm2835-rpi-b.dtb",
103 true,
104 },
105 [0x2] = {
106 "Model A+",
107 DTB_DIR "bcm2835-rpi-a-plus.dtb",
108 false,
109 },
110 [0x3] = {
111 "Model B+",
112 DTB_DIR "bcm2835-rpi-b-plus.dtb",
113 true,
114 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700115 [0x4] = {
Stephen Warrendc7ea682015-02-16 12:16:15 -0700116 "2 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200117 DTB_DIR "bcm2836-rpi-2-b.dtb",
Stephen Warrendc7ea682015-02-16 12:16:15 -0700118 true,
119 },
Jonathan Grayde4524b2018-04-06 18:45:49 +1000120 [0x6] = {
121 "Compute Module",
122 DTB_DIR "bcm2835-rpi-cm.dtb",
123 false,
124 },
Stephen Warren303244a2016-03-24 22:15:18 -0600125 [0x8] = {
126 "3 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200127 DTB_DIR "bcm2837-rpi-3-b.dtb",
Stephen Warren303244a2016-03-24 22:15:18 -0600128 true,
129 },
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700130 [0x9] = {
131 "Zero",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200132 DTB_DIR "bcm2835-rpi-zero.dtb",
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700133 false,
134 },
Jonathan Grayde4524b2018-04-06 18:45:49 +1000135 [0xA] = {
136 "Compute Module 3",
137 DTB_DIR "bcm2837-rpi-cm3.dtb",
138 false,
139 },
Dmitry Korunov3f887412017-11-26 13:38:53 +0400140 [0xC] = {
141 "Zero W",
142 DTB_DIR "bcm2835-rpi-zero-w.dtb",
143 false,
144 },
Alexander Grafd0043a02018-03-15 15:05:37 +0100145 [0xD] = {
146 "3 Model B+",
147 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
148 true,
149 },
Stephen Warren26683852015-12-04 22:07:45 -0700150};
151
152static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700153 [0x2] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100154 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200155 DTB_DIR "bcm2835-rpi-b.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 [0x3] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100159 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200160 DTB_DIR "bcm2835-rpi-b.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 [0x4] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100164 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200165 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700166 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700167 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700168 [0x5] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100169 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200170 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700171 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700172 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700173 [0x6] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100174 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200175 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700176 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700177 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700178 [0x7] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700179 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200180 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700181 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700182 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700183 [0x8] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700184 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200185 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700186 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700187 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700188 [0x9] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700189 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200190 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700191 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700192 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700193 [0xd] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700194 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200195 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700196 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700197 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700198 [0xe] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700199 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200200 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700201 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700202 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700203 [0xf] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700204 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200205 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700206 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700207 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700208 [0x10] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700209 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200210 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700211 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700212 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700213 [0x11] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700214 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200215 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700216 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700217 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700218 [0x12] = {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700219 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200220 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren18a5fc62014-12-23 20:01:43 -0700221 false,
222 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700223 [0x13] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600224 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200225 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600226 true,
227 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700228 [0x14] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600229 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200230 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600231 false,
232 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700233 [0x15] = {
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200234 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200235 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200236 false,
237 },
Stephen Warrencd210c12014-11-18 21:40:21 -0700238};
239
Stephen Warren26683852015-12-04 22:07:45 -0700240static uint32_t revision;
241static uint32_t rev_scheme;
242static uint32_t rev_type;
243static const struct rpi_model *model;
Stephen Warrencd210c12014-11-18 21:40:21 -0700244
Stephen Warren93ea5262016-04-01 21:14:15 -0600245#ifdef CONFIG_ARM64
246static struct mm_region bcm2837_mem_map[] = {
247 {
York Sunc7104e52016-06-24 16:46:22 -0700248 .virt = 0x00000000UL,
249 .phys = 0x00000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600250 .size = 0x3f000000UL,
251 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
252 PTE_BLOCK_INNER_SHARE
253 }, {
York Sunc7104e52016-06-24 16:46:22 -0700254 .virt = 0x3f000000UL,
255 .phys = 0x3f000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600256 .size = 0x01000000UL,
257 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
258 PTE_BLOCK_NON_SHARE |
259 PTE_BLOCK_PXN | PTE_BLOCK_UXN
260 }, {
261 /* List terminator */
262 0,
263 }
264};
265
266struct mm_region *mem_map = bcm2837_mem_map;
267#endif
268
Stephen Warren0e012c32012-08-05 16:07:22 +0000269int dram_init(void)
270{
Alexander Steinc56c9472015-07-24 09:22:12 +0200271 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000272 int ret;
273
274 BCM2835_MBOX_INIT_HDR(msg);
275 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
276
277 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
278 if (ret) {
279 printf("bcm2835: Could not query ARM memory size\n");
280 return -1;
281 }
282
283 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000284
285 return 0;
286}
287
Stephen Warrencd210c12014-11-18 21:40:21 -0700288static void set_fdtfile(void)
289{
290 const char *fdtfile;
291
Simon Glass64b723f2017-08-03 12:22:12 -0600292 if (env_get("fdtfile"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700293 return;
294
Stephen Warren26683852015-12-04 22:07:45 -0700295 fdtfile = model->fdtfile;
Simon Glass6a38e412017-08-03 12:22:09 -0600296 env_set("fdtfile", fdtfile);
Stephen Warrencd210c12014-11-18 21:40:21 -0700297}
298
Cédric Schieli9473f532016-11-11 11:59:07 +0100299/*
300 * If the firmware provided a valid FDT at boot time, let's expose it in
301 * ${fdt_addr} so it may be passed unmodified to the kernel.
302 */
303static void set_fdt_addr(void)
304{
Simon Glass64b723f2017-08-03 12:22:12 -0600305 if (env_get("fdt_addr"))
Cédric Schieli9473f532016-11-11 11:59:07 +0100306 return;
307
308 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
309 return;
310
Simon Glass4d949a22017-08-03 12:22:10 -0600311 env_set_hex("fdt_addr", fw_dtb_pointer);
Cédric Schieli9473f532016-11-11 11:59:07 +0100312}
313
314/*
315 * Prevent relocation from stomping on a firmware provided FDT blob.
316 */
317unsigned long board_get_usable_ram_top(unsigned long total_size)
318{
319 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
320 return gd->ram_top;
321 return fw_dtb_pointer & ~0xffff;
322}
323
Stephen Warrencd210c12014-11-18 21:40:21 -0700324static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600325{
Alexander Steinc56c9472015-07-24 09:22:12 +0200326 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600327 int ret;
328
Stephen Warren26683852015-12-04 22:07:45 -0700329 if (!model->has_onboard_eth)
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700330 return;
331
Simon Glass64b723f2017-08-03 12:22:12 -0600332 if (env_get("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700333 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600334
335 BCM2835_MBOX_INIT_HDR(msg);
336 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
337
338 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
339 if (ret) {
340 printf("bcm2835: Could not query MAC address\n");
341 /* Ignore error; not critical */
Stephen Warrencd210c12014-11-18 21:40:21 -0700342 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600343 }
344
Simon Glass8551d552017-08-03 12:22:11 -0600345 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600346
Simon Glass64b723f2017-08-03 12:22:12 -0600347 if (!env_get("ethaddr"))
348 env_set("ethaddr", env_get("usbethaddr"));
Lubomir Rintel3ac09f82016-02-03 16:08:09 +0100349
Stephen Warrencd210c12014-11-18 21:40:21 -0700350 return;
351}
352
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200353#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
354static void set_board_info(void)
355{
Stephen Warren26683852015-12-04 22:07:45 -0700356 char s[11];
357
358 snprintf(s, sizeof(s), "0x%X", revision);
Simon Glass6a38e412017-08-03 12:22:09 -0600359 env_set("board_revision", s);
Stephen Warren26683852015-12-04 22:07:45 -0700360 snprintf(s, sizeof(s), "%d", rev_scheme);
Simon Glass6a38e412017-08-03 12:22:09 -0600361 env_set("board_rev_scheme", s);
Stephen Warren26683852015-12-04 22:07:45 -0700362 /* Can't rename this to board_rev_type since it's an ABI for scripts */
363 snprintf(s, sizeof(s), "0x%X", rev_type);
Simon Glass6a38e412017-08-03 12:22:09 -0600364 env_set("board_rev", s);
365 env_set("board_name", model->name);
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200366}
367#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
368
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100369static void set_serial_number(void)
370{
371 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
372 int ret;
373 char serial_string[17] = { 0 };
374
Simon Glass64b723f2017-08-03 12:22:12 -0600375 if (env_get("serial#"))
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100376 return;
377
378 BCM2835_MBOX_INIT_HDR(msg);
379 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
380
381 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
382 if (ret) {
383 printf("bcm2835: Could not query board serial\n");
384 /* Ignore error; not critical */
385 return;
386 }
387
388 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
389 msg->get_board_serial.body.resp.serial);
Simon Glass6a38e412017-08-03 12:22:09 -0600390 env_set("serial#", serial_string);
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100391}
392
Stephen Warrencd210c12014-11-18 21:40:21 -0700393int misc_init_r(void)
394{
Cédric Schieli9473f532016-11-11 11:59:07 +0100395 set_fdt_addr();
Stephen Warrencd210c12014-11-18 21:40:21 -0700396 set_fdtfile();
397 set_usbethaddr();
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200398#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
399 set_board_info();
400#endif
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100401 set_serial_number();
402
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600403 return 0;
404}
405
Stephen Warrencd210c12014-11-18 21:40:21 -0700406static void get_board_rev(void)
407{
Alexander Steinc56c9472015-07-24 09:22:12 +0200408 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warrencd210c12014-11-18 21:40:21 -0700409 int ret;
Stephen Warren26683852015-12-04 22:07:45 -0700410 const struct rpi_model *models;
411 uint32_t models_count;
Stephen Warrencd210c12014-11-18 21:40:21 -0700412
413 BCM2835_MBOX_INIT_HDR(msg);
414 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
415
416 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
417 if (ret) {
418 printf("bcm2835: Could not query board revision\n");
419 /* Ignore error; not critical */
420 return;
421 }
422
Stephen Warrendc7ea682015-02-16 12:16:15 -0700423 /*
424 * For details of old-vs-new scheme, see:
425 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
426 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
427 * (a few posts down)
Stephen Warrenb834af82015-03-23 23:00:25 -0600428 *
429 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
430 * lower byte to use as the board rev:
431 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
432 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warrendc7ea682015-02-16 12:16:15 -0700433 */
Stephen Warren26683852015-12-04 22:07:45 -0700434 revision = msg->get_board_rev.body.resp.rev;
435 if (revision & 0x800000) {
436 rev_scheme = 1;
437 rev_type = (revision >> 4) & 0xff;
438 models = rpi_models_new_scheme;
439 models_count = ARRAY_SIZE(rpi_models_new_scheme);
440 } else {
441 rev_scheme = 0;
442 rev_type = revision & 0xff;
443 models = rpi_models_old_scheme;
444 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren18a5fc62014-12-23 20:01:43 -0700445 }
Stephen Warren26683852015-12-04 22:07:45 -0700446 if (rev_type >= models_count) {
447 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
448 model = &rpi_model_unknown;
449 } else if (!models[rev_type].name) {
450 printf("RPI: Board rev 0x%x unknown\n", rev_type);
451 model = &rpi_model_unknown;
452 } else {
453 model = &models[rev_type];
Stephen Warren18a5fc62014-12-23 20:01:43 -0700454 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700455
Stephen Warren26683852015-12-04 22:07:45 -0700456 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warrencd210c12014-11-18 21:40:21 -0700457}
458
Fabian Vogt1d317b72016-09-26 14:26:50 +0200459int board_init(void)
Alexander Grafafb99072016-08-15 17:48:51 +0200460{
Paolo Pisati6213c552017-02-10 17:28:05 +0100461#ifdef CONFIG_HW_WATCHDOG
462 hw_watchdog_init();
463#endif
Alexander Grafafb99072016-08-15 17:48:51 +0200464
Fabian Vogt1d317b72016-09-26 14:26:50 +0200465 get_board_rev();
466
467 gd->bd->bi_boot_params = 0x100;
468
Simon Glass558ec672017-04-05 16:23:36 -0600469 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Grafafb99072016-08-15 17:48:51 +0200470}
471
Alex Deymo5b661ec2017-04-02 01:25:20 -0700472/*
473 * If the firmware passed a device tree use it for U-Boot.
474 */
475void *board_fdt_blob_setup(void)
476{
477 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
478 return NULL;
479 return (void *)fw_dtb_pointer;
480}
481
Simon Glass2aec3cc2014-10-23 18:58:47 -0600482int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000483{
484 /*
485 * For now, we simply always add the simplefb DT node. Later, we
486 * should be more intelligent, and e.g. only do this if no enabled DT
487 * node exists for the "real" graphics driver.
488 */
489 lcd_dt_simplefb_add_node(blob);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600490
Alexander Graf013e9ae2016-11-02 10:36:20 +0100491#ifdef CONFIG_EFI_LOADER
492 /* Reserve the spin table */
493 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
494#endif
495
Simon Glass2aec3cc2014-10-23 18:58:47 -0600496 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000497}