blob: 9122f33d88d04591c9a3356272735ca8900b868a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren0e012c32012-08-05 16:07:22 +00002/*
Stephen Warrend6f47c82016-03-24 22:15:20 -06003 * (C) Copyright 2012-2016 Stephen Warren
Stephen Warren0e012c32012-08-05 16:07:22 +00004 */
5
Stephen Warrenaa44d532013-05-27 18:31:18 +00006#include <config.h>
Simon Glass74807622014-09-22 17:30:56 -06007#include <dm.h>
Simon Glass313112a2019-08-01 09:46:46 -06008#include <env.h>
Alexander Graf013e9ae2016-11-02 10:36:20 +01009#include <efi_loader.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>
Simon Glass6980b6b2019-11-14 12:57:45 -070012#include <init.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>
Simon Glass558ec672017-04-05 16:23:36 -060017#include <asm/arch/msg.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000018#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000019#include <asm/global_data.h>
Stephen Warrend6f47c82016-03-24 22:15:20 -060020#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warren93ea5262016-04-01 21:14:15 -060021#ifdef CONFIG_ARM64
22#include <asm/armv8/mmu.h>
23#endif
Paolo Pisati6213c552017-02-10 17:28:05 +010024#include <watchdog.h>
Alexander Graffce8e5c2018-01-23 18:05:21 +010025#include <dm/pinctrl.h>
Simon Glass18d4a112024-10-23 15:20:12 +020026#include <dm/ofnode.h>
27#include <acpi/acpi_table.h>
28#include <acpi/acpigen.h>
29#include <dm/lists.h>
30#include <tables_csum.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000031
32DECLARE_GLOBAL_DATA_PTR;
33
Matthias Bruggerd75403d2019-11-19 16:01:02 +010034/* Assigned in lowlevel_init.S
35 * Push the variable into the .data section so that it
36 * does not get cleared later.
37 */
38unsigned long __section(".data") fw_dtb_pointer;
Cédric Schieli9473f532016-11-11 11:59:07 +010039
Simon Glass56991cd2017-04-05 16:23:45 -060040/* TODO(sjg@chromium.org): Move these to the msg.c file */
Stephen Warrenacc8bf42013-01-29 16:37:37 +000041struct msg_get_arm_mem {
42 struct bcm2835_mbox_hdr hdr;
43 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
44 u32 end_tag;
45};
46
Stephen Warrencd210c12014-11-18 21:40:21 -070047struct msg_get_board_rev {
48 struct bcm2835_mbox_hdr hdr;
49 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
50 u32 end_tag;
51};
52
Lubomir Rintel7d33bb62016-02-22 22:06:47 +010053struct msg_get_board_serial {
54 struct bcm2835_mbox_hdr hdr;
55 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
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 Warrenc4ab9712013-01-29 16:37:42 +000065struct msg_get_clock_rate {
66 struct bcm2835_mbox_hdr hdr;
67 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
68 u32 end_tag;
69};
70
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +020071#ifdef CONFIG_ARM64
72#define DTB_DIR "broadcom/"
73#else
74#define DTB_DIR ""
75#endif
76
Stephen Warrenbe8efec2015-12-04 22:07:44 -070077/*
Peter Robinson52e33462021-11-21 17:03:45 +000078 * https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
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[] = {
Jonathan Grayde4524b2018-04-06 18:45:49 +100093 [0x0] = {
94 "Model A",
95 DTB_DIR "bcm2835-rpi-a.dtb",
96 false,
97 },
98 [0x1] = {
99 "Model B",
100 DTB_DIR "bcm2835-rpi-b.dtb",
101 true,
102 },
103 [0x2] = {
104 "Model A+",
105 DTB_DIR "bcm2835-rpi-a-plus.dtb",
106 false,
107 },
108 [0x3] = {
109 "Model B+",
110 DTB_DIR "bcm2835-rpi-b-plus.dtb",
111 true,
112 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700113 [0x4] = {
Stephen Warrendc7ea682015-02-16 12:16:15 -0700114 "2 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200115 DTB_DIR "bcm2836-rpi-2-b.dtb",
Stephen Warrendc7ea682015-02-16 12:16:15 -0700116 true,
117 },
Jonathan Grayde4524b2018-04-06 18:45:49 +1000118 [0x6] = {
119 "Compute Module",
120 DTB_DIR "bcm2835-rpi-cm.dtb",
121 false,
122 },
Stephen Warren303244a2016-03-24 22:15:18 -0600123 [0x8] = {
124 "3 Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200125 DTB_DIR "bcm2837-rpi-3-b.dtb",
Stephen Warren303244a2016-03-24 22:15:18 -0600126 true,
127 },
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700128 [0x9] = {
129 "Zero",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200130 DTB_DIR "bcm2835-rpi-zero.dtb",
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700131 false,
132 },
Jonathan Grayde4524b2018-04-06 18:45:49 +1000133 [0xA] = {
134 "Compute Module 3",
135 DTB_DIR "bcm2837-rpi-cm3.dtb",
136 false,
137 },
Dmitry Korunov3f887412017-11-26 13:38:53 +0400138 [0xC] = {
139 "Zero W",
140 DTB_DIR "bcm2835-rpi-zero-w.dtb",
141 false,
142 },
Alexander Grafd0043a02018-03-15 15:05:37 +0100143 [0xD] = {
144 "3 Model B+",
145 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
146 true,
147 },
Jonathan Grayed102552018-11-16 23:07:39 +1100148 [0xE] = {
149 "3 Model A+",
150 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
151 false,
152 },
Jonathan Gray765d34d2019-01-31 09:24:44 +1100153 [0x10] = {
154 "Compute Module 3+",
155 DTB_DIR "bcm2837-rpi-cm3.dtb",
156 false,
157 },
Andrei Gherzan2633ec82019-07-24 15:39:07 +0100158 [0x11] = {
159 "4 Model B",
160 DTB_DIR "bcm2711-rpi-4-b.dtb",
161 true,
162 },
Peter Robinson400260c2021-11-21 17:03:46 +0000163 [0x12] = {
164 "Zero 2 W",
Peter Robinson78c7e462023-02-28 10:17:26 +0000165 DTB_DIR "bcm2837-rpi-zero-2-w.dtb",
Peter Robinson400260c2021-11-21 17:03:46 +0000166 false,
167 },
Nicolas Saenz Julienne12969612021-01-12 13:55:19 +0100168 [0x13] = {
169 "400",
170 DTB_DIR "bcm2711-rpi-400.dtb",
171 true,
172 },
Nicolas Saenz Julienne0c291862021-01-12 13:55:20 +0100173 [0x14] = {
174 "Compute Module 4",
175 DTB_DIR "bcm2711-rpi-cm4.dtb",
176 true,
177 },
Ivan T. Ivanov14fb9522024-01-23 10:07:55 +0200178 [0x17] = {
179 "5 Model B",
180 DTB_DIR "bcm2712-rpi-5-b.dtb",
181 true,
182 },
Stephen Warren26683852015-12-04 22:07:45 -0700183};
184
185static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700186 [0x2] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100187 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200188 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700189 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700190 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700191 [0x3] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100192 "Model B",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200193 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700194 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700195 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700196 [0x4] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100197 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200198 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700199 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700200 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700201 [0x5] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100202 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200203 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700204 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700205 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700206 [0x6] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100207 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200208 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700209 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700210 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700211 [0x7] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700212 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200213 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700214 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700215 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700216 [0x8] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700217 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200218 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700219 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700220 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700221 [0x9] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700222 "Model A",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200223 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700224 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700225 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700226 [0xd] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700227 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200228 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700229 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700230 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700231 [0xe] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700232 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200233 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700234 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700235 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700236 [0xf] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700237 "Model B rev2",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200238 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700239 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700240 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700241 [0x10] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700242 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200243 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700244 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700245 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700246 [0x11] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700247 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200248 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700249 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700250 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700251 [0x12] = {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700252 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200253 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren18a5fc62014-12-23 20:01:43 -0700254 false,
255 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700256 [0x13] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600257 "Model B+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200258 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600259 true,
260 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700261 [0x14] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600262 "Compute Module",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200263 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3648a852015-04-12 21:43:25 -0600264 false,
265 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700266 [0x15] = {
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200267 "Model A+",
Tuomas Tynkkynen9266d1d2017-01-23 01:34:39 +0200268 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200269 false,
270 },
Stephen Warrencd210c12014-11-18 21:40:21 -0700271};
272
Stephen Warren26683852015-12-04 22:07:45 -0700273static uint32_t revision;
274static uint32_t rev_scheme;
275static uint32_t rev_type;
276static const struct rpi_model *model;
Stephen Warrencd210c12014-11-18 21:40:21 -0700277
Stephen Warren0e012c32012-08-05 16:07:22 +0000278int dram_init(void)
279{
Alexander Steinc56c9472015-07-24 09:22:12 +0200280 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000281 int ret;
282
283 BCM2835_MBOX_INIT_HDR(msg);
284 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
285
286 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
287 if (ret) {
288 printf("bcm2835: Could not query ARM memory size\n");
289 return -1;
290 }
291
292 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000293
Marek Szyprowski8c5ca982021-02-01 12:16:33 +0100294 /*
295 * In some configurations the memory size returned by VideoCore
296 * is not aligned to the section size, what is mandatory for
297 * the u-boot's memory setup.
298 */
299 gd->ram_size &= ~MMU_SECTION_SIZE;
300
Stephen Warren0e012c32012-08-05 16:07:22 +0000301 return 0;
302}
303
Matthias Bruggerd3dcfe22019-09-09 18:31:56 +0200304#ifdef CONFIG_OF_BOARD
Matthias Bruggerd3dcfe22019-09-09 18:31:56 +0200305int dram_init_banksize(void)
306{
Matthias Bruggerf9788372019-12-05 18:53:13 +0100307 int ret;
308
309 ret = fdtdec_setup_memory_banksize();
310 if (ret)
311 return ret;
312
313 return fdtdec_setup_mem_size_base();
Matthias Bruggerd3dcfe22019-09-09 18:31:56 +0200314}
315#endif
Matthias Bruggerd3dcfe22019-09-09 18:31:56 +0200316
Stephen Warrencd210c12014-11-18 21:40:21 -0700317static void set_fdtfile(void)
318{
319 const char *fdtfile;
320
Simon Glass64b723f2017-08-03 12:22:12 -0600321 if (env_get("fdtfile"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700322 return;
323
Stephen Warren26683852015-12-04 22:07:45 -0700324 fdtfile = model->fdtfile;
Simon Glass6a38e412017-08-03 12:22:09 -0600325 env_set("fdtfile", fdtfile);
Stephen Warrencd210c12014-11-18 21:40:21 -0700326}
327
Cédric Schieli9473f532016-11-11 11:59:07 +0100328/*
329 * If the firmware provided a valid FDT at boot time, let's expose it in
330 * ${fdt_addr} so it may be passed unmodified to the kernel.
331 */
332static void set_fdt_addr(void)
333{
Simon Glass64b723f2017-08-03 12:22:12 -0600334 if (env_get("fdt_addr"))
Cédric Schieli9473f532016-11-11 11:59:07 +0100335 return;
336
337 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
338 return;
339
Simon Glass4d949a22017-08-03 12:22:10 -0600340 env_set_hex("fdt_addr", fw_dtb_pointer);
Cédric Schieli9473f532016-11-11 11:59:07 +0100341}
342
343/*
344 * Prevent relocation from stomping on a firmware provided FDT blob.
345 */
Heinrich Schuchardt51a9aac2023-08-12 20:16:58 +0200346phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Cédric Schieli9473f532016-11-11 11:59:07 +0100347{
348 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
349 return gd->ram_top;
350 return fw_dtb_pointer & ~0xffff;
351}
352
Stephen Warrencd210c12014-11-18 21:40:21 -0700353static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600354{
Alexander Steinc56c9472015-07-24 09:22:12 +0200355 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600356 int ret;
357
Stephen Warren26683852015-12-04 22:07:45 -0700358 if (!model->has_onboard_eth)
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700359 return;
360
Simon Glass64b723f2017-08-03 12:22:12 -0600361 if (env_get("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700362 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600363
364 BCM2835_MBOX_INIT_HDR(msg);
365 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
366
367 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
368 if (ret) {
369 printf("bcm2835: Could not query MAC address\n");
370 /* Ignore error; not critical */
Stephen Warrencd210c12014-11-18 21:40:21 -0700371 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600372 }
373
Simon Glass8551d552017-08-03 12:22:11 -0600374 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600375
Simon Glass64b723f2017-08-03 12:22:12 -0600376 if (!env_get("ethaddr"))
377 env_set("ethaddr", env_get("usbethaddr"));
Lubomir Rintel3ac09f82016-02-03 16:08:09 +0100378
Stephen Warrencd210c12014-11-18 21:40:21 -0700379 return;
380}
381
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200382#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
383static void set_board_info(void)
384{
Stephen Warren26683852015-12-04 22:07:45 -0700385 char s[11];
386
387 snprintf(s, sizeof(s), "0x%X", revision);
Simon Glass6a38e412017-08-03 12:22:09 -0600388 env_set("board_revision", s);
Francois Berdere4403102024-07-09 14:20:48 +0200389 snprintf(s, sizeof(s), "%u", rev_scheme);
Simon Glass6a38e412017-08-03 12:22:09 -0600390 env_set("board_rev_scheme", s);
Stephen Warren26683852015-12-04 22:07:45 -0700391 /* Can't rename this to board_rev_type since it's an ABI for scripts */
392 snprintf(s, sizeof(s), "0x%X", rev_type);
Simon Glass6a38e412017-08-03 12:22:09 -0600393 env_set("board_rev", s);
394 env_set("board_name", model->name);
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200395}
396#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
397
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100398static void set_serial_number(void)
399{
400 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
401 int ret;
402 char serial_string[17] = { 0 };
403
Simon Glass64b723f2017-08-03 12:22:12 -0600404 if (env_get("serial#"))
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100405 return;
406
407 BCM2835_MBOX_INIT_HDR(msg);
408 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
409
410 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
411 if (ret) {
412 printf("bcm2835: Could not query board serial\n");
413 /* Ignore error; not critical */
414 return;
415 }
416
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900417 snprintf(serial_string, sizeof(serial_string), "%016llx",
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100418 msg->get_board_serial.body.resp.serial);
Simon Glass6a38e412017-08-03 12:22:09 -0600419 env_set("serial#", serial_string);
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100420}
421
Stephen Warrencd210c12014-11-18 21:40:21 -0700422int misc_init_r(void)
423{
Cédric Schieli9473f532016-11-11 11:59:07 +0100424 set_fdt_addr();
Stephen Warrencd210c12014-11-18 21:40:21 -0700425 set_fdtfile();
426 set_usbethaddr();
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200427#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
428 set_board_info();
429#endif
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100430 set_serial_number();
431
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600432 return 0;
433}
434
Tom Rini4cc38852021-08-30 09:16:30 -0400435static void get_board_revision(void)
Stephen Warrencd210c12014-11-18 21:40:21 -0700436{
Alexander Steinc56c9472015-07-24 09:22:12 +0200437 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warrencd210c12014-11-18 21:40:21 -0700438 int ret;
Stephen Warren26683852015-12-04 22:07:45 -0700439 const struct rpi_model *models;
440 uint32_t models_count;
Ivan T. Ivanov14fb9522024-01-23 10:07:55 +0200441 ofnode node;
Stephen Warrencd210c12014-11-18 21:40:21 -0700442
443 BCM2835_MBOX_INIT_HDR(msg);
444 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
445
446 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
447 if (ret) {
Stephen Warrencd210c12014-11-18 21:40:21 -0700448 /* Ignore error; not critical */
Ivan T. Ivanov14fb9522024-01-23 10:07:55 +0200449 node = ofnode_path("/system");
450 if (!ofnode_valid(node)) {
451 printf("bcm2835: Could not find /system node\n");
452 return;
453 }
454
455 ret = ofnode_read_u32(node, "linux,revision", &revision);
456 if (ret) {
457 printf("bcm2835: Could not find linux,revision\n");
458 return;
459 }
460 } else {
461 revision = msg->get_board_rev.body.resp.rev;
Stephen Warrencd210c12014-11-18 21:40:21 -0700462 }
463
Stephen Warrendc7ea682015-02-16 12:16:15 -0700464 /*
465 * For details of old-vs-new scheme, see:
466 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
467 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
468 * (a few posts down)
Stephen Warrenb834af82015-03-23 23:00:25 -0600469 *
470 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
471 * lower byte to use as the board rev:
472 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
473 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warrendc7ea682015-02-16 12:16:15 -0700474 */
Stephen Warren26683852015-12-04 22:07:45 -0700475 if (revision & 0x800000) {
476 rev_scheme = 1;
477 rev_type = (revision >> 4) & 0xff;
478 models = rpi_models_new_scheme;
479 models_count = ARRAY_SIZE(rpi_models_new_scheme);
480 } else {
481 rev_scheme = 0;
482 rev_type = revision & 0xff;
483 models = rpi_models_old_scheme;
484 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren18a5fc62014-12-23 20:01:43 -0700485 }
Stephen Warren26683852015-12-04 22:07:45 -0700486 if (rev_type >= models_count) {
487 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
488 model = &rpi_model_unknown;
489 } else if (!models[rev_type].name) {
490 printf("RPI: Board rev 0x%x unknown\n", rev_type);
491 model = &rpi_model_unknown;
492 } else {
493 model = &models[rev_type];
Stephen Warren18a5fc62014-12-23 20:01:43 -0700494 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700495
Stephen Warren26683852015-12-04 22:07:45 -0700496 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warrencd210c12014-11-18 21:40:21 -0700497}
498
Fabian Vogt1d317b72016-09-26 14:26:50 +0200499int board_init(void)
Alexander Grafafb99072016-08-15 17:48:51 +0200500{
Tom Rini4cc38852021-08-30 09:16:30 -0400501 get_board_revision();
Fabian Vogt1d317b72016-09-26 14:26:50 +0200502
503 gd->bd->bi_boot_params = 0x100;
504
Simon Glass558ec672017-04-05 16:23:36 -0600505 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Grafafb99072016-08-15 17:48:51 +0200506}
507
Alex Deymo5b661ec2017-04-02 01:25:20 -0700508/*
509 * If the firmware passed a device tree use it for U-Boot.
510 */
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300511void *board_fdt_blob_setup(int *err)
Alex Deymo5b661ec2017-04-02 01:25:20 -0700512{
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300513 *err = 0;
514 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC) {
515 *err = -ENXIO;
Alex Deymo5b661ec2017-04-02 01:25:20 -0700516 return NULL;
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300517 }
518
Alex Deymo5b661ec2017-04-02 01:25:20 -0700519 return (void *)fw_dtb_pointer;
520}
521
Antoine Mazeasfb784822022-08-19 10:56:45 +0200522int copy_property(void *dst, void *src, char *path, char *property)
523{
524 int dst_offset, src_offset;
525 const fdt32_t *prop;
526 int len;
527
528 src_offset = fdt_path_offset(src, path);
529 dst_offset = fdt_path_offset(dst, path);
530
531 if (src_offset < 0 || dst_offset < 0)
532 return -1;
533
534 prop = fdt_getprop(src, src_offset, property, &len);
535 if (!prop)
536 return -1;
537
538 return fdt_setprop(dst, dst_offset, property, prop, len);
539}
540
541/* Copy tweaks from the firmware dtb to the loaded dtb */
542void update_fdt_from_fw(void *fdt, void *fw_fdt)
543{
544 /* Using dtb from firmware directly; leave it alone */
545 if (fdt == fw_fdt)
546 return;
547
548 /* The firmware provides a more precie model; so copy that */
549 copy_property(fdt, fw_fdt, "/", "model");
550
551 /* memory reserve as suggested by the firmware */
552 copy_property(fdt, fw_fdt, "/", "memreserve");
553
554 /* Adjust dma-ranges for the SD card and PCI bus as they can depend on
555 * the SoC revision
556 */
557 copy_property(fdt, fw_fdt, "emmc2bus", "dma-ranges");
558 copy_property(fdt, fw_fdt, "pcie0", "dma-ranges");
559
560 /* Bootloader configuration template exposes as nvmem */
561 if (copy_property(fdt, fw_fdt, "blconfig", "reg") == 0)
562 copy_property(fdt, fw_fdt, "blconfig", "status");
563
564 /* kernel address randomisation seed as provided by the firmware */
565 copy_property(fdt, fw_fdt, "/chosen", "kaslr-seed");
Antoine Mazeas533e5842022-08-19 10:56:46 +0200566
567 /* address of the PHY device as provided by the firmware */
568 copy_property(fdt, fw_fdt, "ethernet0/mdio@e14/ethernet-phy@1", "reg");
Antoine Mazeasfb784822022-08-19 10:56:45 +0200569}
570
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900571int ft_board_setup(void *blob, struct bd_info *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000572{
Ivan T. Ivanov6436ecf2021-08-10 16:31:14 +0200573 int node;
574
Antoine Mazeasfb784822022-08-19 10:56:45 +0200575 update_fdt_from_fw(blob, (void *)fw_dtb_pointer);
576
Ivan T. Ivanov6436ecf2021-08-10 16:31:14 +0200577 node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
578 if (node < 0)
Patrick Delaunaye7e69a52021-11-15 16:32:19 +0100579 fdt_simplefb_add_node(blob);
Meng Lic5eaa072023-07-26 10:42:35 +0800580 else
581 fdt_simplefb_enable_and_mem_rsv(blob);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600582
Alexander Graf013e9ae2016-11-02 10:36:20 +0100583#ifdef CONFIG_EFI_LOADER
584 /* Reserve the spin table */
Michael Walle282d3862020-05-17 12:29:19 +0200585 efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
586 EFI_RESERVED_MEMORY_TYPE);
Alexander Graf013e9ae2016-11-02 10:36:20 +0100587#endif
588
Simon Glass2aec3cc2014-10-23 18:58:47 -0600589 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000590}
Simon Glass18d4a112024-10-23 15:20:12 +0200591
592#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
593static bool is_rpi4(void)
594{
595 return of_machine_is_compatible("brcm,bcm2711") ||
596 of_machine_is_compatible("brcm,bcm2712");
597}
598
599static bool is_rpi3(void)
600{
601 return of_machine_is_compatible("brcm,bcm2837");
602}
603
604static int acpi_rpi_board_fill_ssdt(struct acpi_ctx *ctx)
605{
606 int node, ret, uart_in_use, mini_clock_rate;
607 bool enabled;
608 struct udevice *dev;
609 struct {
610 const char *fdt_compatible;
611 const char *acpi_scope;
612 bool on_rpi4;
613 bool on_rpi3;
614 u32 mmio_address;
615 } map[] = {
616 {"brcm,bcm2711-pcie", "\\_SB.PCI0", true, false},
617 {"brcm,bcm2711-emmc2", "\\_SB.GDV1.SDC3", true, false},
618 {"brcm,bcm2835-pwm", "\\_SB.GDV0.PWM0", true, true},
619 {"brcm,bcm2711-genet-v5", "\\_SB.ETH0", true, false},
620 {"brcm,bcm2711-thermal", "\\_SB.EC00", true, true},
621 {"brcm,bcm2835-sdhci", "\\_SB.SDC1", true, true},
622 {"brcm,bcm2835-sdhost", "\\_SB.SDC2", false, true},
623 {"brcm,bcm2835-mbox", "\\_SB.GDV0.RPIQ", true, true},
624 {"brcm,bcm2835-i2c", "\\_SB.GDV0.I2C1", true, true, 0xfe205000},
625 {"brcm,bcm2835-i2c", "\\_SB.GDV0.I2C2", true, true, 0xfe804000},
626 {"brcm,bcm2835-spi", "\\_SB.GDV0.SPI0", true, true},
627 {"brcm,bcm2835-aux-spi", "\\_SB.GDV0.SPI1", true, true, 0xfe215080},
628 {"arm,pl011", "\\_SB.URT0", true, true},
629 {"brcm,bcm2835-aux-uart", "\\_SB.URTM", true, true},
630 { /* Sentinel */ }
631 };
632
633 /* Device enable */
634 for (int i = 0; map[i].fdt_compatible; i++) {
635 if ((is_rpi4() && !map[i].on_rpi4) ||
636 (is_rpi3() && !map[i].on_rpi3)) {
637 enabled = false;
638 } else {
639 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
640 map[i].fdt_compatible);
641 while (node != -FDT_ERR_NOTFOUND && map[i].mmio_address) {
642 struct fdt_resource r;
643
644 ret = fdt_get_resource(gd->fdt_blob, node, "reg", 0, &r);
645 if (ret) {
646 node = -FDT_ERR_NOTFOUND;
647 break;
648 }
649
650 if (r.start == map[i].mmio_address)
651 break;
652
653 node = fdt_node_offset_by_compatible(gd->fdt_blob, node,
654 map[i].fdt_compatible);
655 }
656
657 enabled = (node > 0) ? fdtdec_get_is_enabled(gd->fdt_blob, node) : 0;
658 }
659 acpigen_write_scope(ctx, map[i].acpi_scope);
660 acpigen_write_name_integer(ctx, "_STA", enabled ? 0xf : 0);
661 acpigen_pop_len(ctx);
662 }
663
664 /* GPIO quirks */
665 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2835-gpio");
666 if (node <= 0)
667 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2711-gpio");
668
669 acpigen_write_scope(ctx, "\\_SB.GDV0.GPI0");
670 enabled = (node > 0) ? fdtdec_get_is_enabled(gd->fdt_blob, node) : 0;
671 acpigen_write_name_integer(ctx, "_STA", enabled ? 0xf : 0);
672 acpigen_pop_len(ctx);
673
674 if (is_rpi4()) {
675 /* eMMC quirks */
676 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2711-emmc2");
677 if (node) {
678 phys_addr_t cpu;
679 dma_addr_t bus;
680 u64 size;
681
682 ret = fdt_get_dma_range(gd->fdt_blob, node, &cpu, &bus, &size);
683
684 acpigen_write_scope(ctx, "\\_SB.GDV1");
685 acpigen_write_method_serialized(ctx, "_DMA", 0);
686 acpigen_emit_byte(ctx, RETURN_OP);
687
688 if (!ret && bus != cpu) /* Translated DMA range */
689 acpigen_emit_namestring(ctx, "\\_SB.GDV1.DMTR");
690 else if (!ret && bus == cpu) /* Non translated DMA */
691 acpigen_emit_namestring(ctx, "\\_SB.GDV1.DMNT");
692 else /* Silicon revisions older than C0: Translated DMA range */
693 acpigen_emit_namestring(ctx, "\\_SB.GDV1.DMTR");
694 acpigen_pop_len(ctx);
695 }
696 }
697
698 /* Serial */
699 uart_in_use = ~0;
700 mini_clock_rate = 0x1000000;
701
702 ret = uclass_get_device_by_driver(UCLASS_SERIAL,
703 DM_DRIVER_GET(bcm283x_pl011_uart),
704 &dev);
705 if (!ret)
706 uart_in_use = 0;
707
708 ret = uclass_get_device_by_driver(UCLASS_SERIAL,
709 DM_DRIVER_GET(serial_bcm283x_mu),
710 &dev);
711 if (!ret) {
712 if (uart_in_use == 0)
713 log_err("Invalid config: PL011 and MiniUART are both enabled.");
714 else
715 uart_in_use = 1;
716
717 mini_clock_rate = dev_read_u32_default(dev, "clock", 0x1000000);
718 }
719 if (uart_in_use > 1)
720 log_err("No working serial: PL011 and MiniUART are both disabled.");
721
722 acpigen_write_scope(ctx, "\\_SB.BTH0");
723 acpigen_write_name_integer(ctx, "URIU", uart_in_use);
724 acpigen_pop_len(ctx);
725
726 acpigen_write_scope(ctx, "\\_SB.URTM");
727 acpigen_write_name_integer(ctx, "MUCR", mini_clock_rate);
728 acpigen_pop_len(ctx);
729
730 return 0;
731}
732
733static int rpi_acpi_write_ssdt(struct acpi_ctx *ctx, const struct acpi_writer *entry)
734{
735 struct acpi_table_header *ssdt;
736 int ret;
737
738 ssdt = ctx->current;
739 memset(ssdt, '\0', sizeof(struct acpi_table_header));
740
741 acpi_fill_header(ssdt, "SSDT");
742 ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
743 ssdt->creator_revision = 1;
744 ssdt->length = sizeof(struct acpi_table_header);
745
746 acpi_inc(ctx, sizeof(struct acpi_table_header));
747
748 ret = acpi_rpi_board_fill_ssdt(ctx);
749 if (ret) {
750 ctx->current = ssdt;
751 return log_msg_ret("fill", ret);
752 }
753
754 /* (Re)calculate length and checksum */
755 ssdt->length = ctx->current - (void *)ssdt;
756 ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
757 log_debug("SSDT at %p, length %x\n", ssdt, ssdt->length);
758
759 /* Drop the table if it is empty */
760 if (ssdt->length == sizeof(struct acpi_table_header))
761 return log_msg_ret("fill", -ENOENT);
762 acpi_add_table(ctx, ssdt);
763
764 return 0;
765}
766
767ACPI_WRITER(5ssdt, "SSDT", rpi_acpi_write_ssdt, 0);
768#endif