Prepare v2024.10
[platform/kernel/u-boot.git] / board / raspberrypi / rpi / rpi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2012-2016 Stephen Warren
4  */
5
6 #include <config.h>
7 #include <dm.h>
8 #include <env.h>
9 #include <efi_loader.h>
10 #include <fdt_support.h>
11 #include <fdt_simplefb.h>
12 #include <init.h>
13 #include <memalign.h>
14 #include <mmc.h>
15 #include <asm/gpio.h>
16 #include <asm/arch/mbox.h>
17 #include <asm/arch/msg.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_bcm283x_mu.h>
21 #ifdef CONFIG_ARM64
22 #include <asm/armv8/mmu.h>
23 #endif
24 #include <watchdog.h>
25 #include <dm/pinctrl.h>
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /* Assigned in lowlevel_init.S
30  * Push the variable into the .data section so that it
31  * does not get cleared later.
32  */
33 unsigned long __section(".data") fw_dtb_pointer;
34
35 /* TODO(sjg@chromium.org): Move these to the msg.c file */
36 struct 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
42 struct 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
48 struct 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
54 struct 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
60 struct 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
66 #ifdef CONFIG_ARM64
67 #define DTB_DIR "broadcom/"
68 #else
69 #define DTB_DIR ""
70 #endif
71
72 /*
73  * https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
74  */
75 struct rpi_model {
76         const char *name;
77         const char *fdtfile;
78         bool has_onboard_eth;
79 };
80
81 static const struct rpi_model rpi_model_unknown = {
82         "Unknown model",
83         DTB_DIR "bcm283x-rpi-other.dtb",
84         false,
85 };
86
87 static const struct rpi_model rpi_models_new_scheme[] = {
88         [0x0] = {
89                 "Model A",
90                 DTB_DIR "bcm2835-rpi-a.dtb",
91                 false,
92         },
93         [0x1] = {
94                 "Model B",
95                 DTB_DIR "bcm2835-rpi-b.dtb",
96                 true,
97         },
98         [0x2] = {
99                 "Model A+",
100                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
101                 false,
102         },
103         [0x3] = {
104                 "Model B+",
105                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
106                 true,
107         },
108         [0x4] = {
109                 "2 Model B",
110                 DTB_DIR "bcm2836-rpi-2-b.dtb",
111                 true,
112         },
113         [0x6] = {
114                 "Compute Module",
115                 DTB_DIR "bcm2835-rpi-cm.dtb",
116                 false,
117         },
118         [0x8] = {
119                 "3 Model B",
120                 DTB_DIR "bcm2837-rpi-3-b.dtb",
121                 true,
122         },
123         [0x9] = {
124                 "Zero",
125                 DTB_DIR "bcm2835-rpi-zero.dtb",
126                 false,
127         },
128         [0xA] = {
129                 "Compute Module 3",
130                 DTB_DIR "bcm2837-rpi-cm3.dtb",
131                 false,
132         },
133         [0xC] = {
134                 "Zero W",
135                 DTB_DIR "bcm2835-rpi-zero-w.dtb",
136                 false,
137         },
138         [0xD] = {
139                 "3 Model B+",
140                 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
141                 true,
142         },
143         [0xE] = {
144                 "3 Model A+",
145                 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
146                 false,
147         },
148         [0x10] = {
149                 "Compute Module 3+",
150                 DTB_DIR "bcm2837-rpi-cm3.dtb",
151                 false,
152         },
153         [0x11] = {
154                 "4 Model B",
155                 DTB_DIR "bcm2711-rpi-4-b.dtb",
156                 true,
157         },
158         [0x12] = {
159                 "Zero 2 W",
160                 DTB_DIR "bcm2837-rpi-zero-2-w.dtb",
161                 false,
162         },
163         [0x13] = {
164                 "400",
165                 DTB_DIR "bcm2711-rpi-400.dtb",
166                 true,
167         },
168         [0x14] = {
169                 "Compute Module 4",
170                 DTB_DIR "bcm2711-rpi-cm4.dtb",
171                 true,
172         },
173         [0x17] = {
174                 "5 Model B",
175                 DTB_DIR "bcm2712-rpi-5-b.dtb",
176                 true,
177         },
178 };
179
180 static const struct rpi_model rpi_models_old_scheme[] = {
181         [0x2] = {
182                 "Model B",
183                 DTB_DIR "bcm2835-rpi-b.dtb",
184                 true,
185         },
186         [0x3] = {
187                 "Model B",
188                 DTB_DIR "bcm2835-rpi-b.dtb",
189                 true,
190         },
191         [0x4] = {
192                 "Model B rev2",
193                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
194                 true,
195         },
196         [0x5] = {
197                 "Model B rev2",
198                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
199                 true,
200         },
201         [0x6] = {
202                 "Model B rev2",
203                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
204                 true,
205         },
206         [0x7] = {
207                 "Model A",
208                 DTB_DIR "bcm2835-rpi-a.dtb",
209                 false,
210         },
211         [0x8] = {
212                 "Model A",
213                 DTB_DIR "bcm2835-rpi-a.dtb",
214                 false,
215         },
216         [0x9] = {
217                 "Model A",
218                 DTB_DIR "bcm2835-rpi-a.dtb",
219                 false,
220         },
221         [0xd] = {
222                 "Model B rev2",
223                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
224                 true,
225         },
226         [0xe] = {
227                 "Model B rev2",
228                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
229                 true,
230         },
231         [0xf] = {
232                 "Model B rev2",
233                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
234                 true,
235         },
236         [0x10] = {
237                 "Model B+",
238                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
239                 true,
240         },
241         [0x11] = {
242                 "Compute Module",
243                 DTB_DIR "bcm2835-rpi-cm.dtb",
244                 false,
245         },
246         [0x12] = {
247                 "Model A+",
248                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
249                 false,
250         },
251         [0x13] = {
252                 "Model B+",
253                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
254                 true,
255         },
256         [0x14] = {
257                 "Compute Module",
258                 DTB_DIR "bcm2835-rpi-cm.dtb",
259                 false,
260         },
261         [0x15] = {
262                 "Model A+",
263                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
264                 false,
265         },
266 };
267
268 static uint32_t revision;
269 static uint32_t rev_scheme;
270 static uint32_t rev_type;
271 static const struct rpi_model *model;
272
273 int dram_init(void)
274 {
275         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
276         int ret;
277
278         BCM2835_MBOX_INIT_HDR(msg);
279         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
280
281         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
282         if (ret) {
283                 printf("bcm2835: Could not query ARM memory size\n");
284                 return -1;
285         }
286
287         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
288
289         /*
290          * In some configurations the memory size returned by VideoCore
291          * is not aligned to the section size, what is mandatory for
292          * the u-boot's memory setup.
293          */
294         gd->ram_size &= ~MMU_SECTION_SIZE;
295
296         return 0;
297 }
298
299 #ifdef CONFIG_OF_BOARD
300 int dram_init_banksize(void)
301 {
302         int ret;
303
304         ret = fdtdec_setup_memory_banksize();
305         if (ret)
306                 return ret;
307
308         return fdtdec_setup_mem_size_base();
309 }
310 #endif
311
312 static void set_fdtfile(void)
313 {
314         const char *fdtfile;
315
316         if (env_get("fdtfile"))
317                 return;
318
319         fdtfile = model->fdtfile;
320         env_set("fdtfile", fdtfile);
321 }
322
323 /*
324  * If the firmware provided a valid FDT at boot time, let's expose it in
325  * ${fdt_addr} so it may be passed unmodified to the kernel.
326  */
327 static void set_fdt_addr(void)
328 {
329         if (env_get("fdt_addr"))
330                 return;
331
332         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
333                 return;
334
335         env_set_hex("fdt_addr", fw_dtb_pointer);
336 }
337
338 /*
339  * Prevent relocation from stomping on a firmware provided FDT blob.
340  */
341 phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
342 {
343         if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
344                 return gd->ram_top;
345         return fw_dtb_pointer & ~0xffff;
346 }
347
348 static void set_usbethaddr(void)
349 {
350         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
351         int ret;
352
353         if (!model->has_onboard_eth)
354                 return;
355
356         if (env_get("usbethaddr"))
357                 return;
358
359         BCM2835_MBOX_INIT_HDR(msg);
360         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
361
362         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
363         if (ret) {
364                 printf("bcm2835: Could not query MAC address\n");
365                 /* Ignore error; not critical */
366                 return;
367         }
368
369         eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
370
371         if (!env_get("ethaddr"))
372                 env_set("ethaddr", env_get("usbethaddr"));
373
374         return;
375 }
376
377 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
378 static void set_board_info(void)
379 {
380         char s[11];
381
382         snprintf(s, sizeof(s), "0x%X", revision);
383         env_set("board_revision", s);
384         snprintf(s, sizeof(s), "%u", rev_scheme);
385         env_set("board_rev_scheme", s);
386         /* Can't rename this to board_rev_type since it's an ABI for scripts */
387         snprintf(s, sizeof(s), "0x%X", rev_type);
388         env_set("board_rev", s);
389         env_set("board_name", model->name);
390 }
391 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
392
393 static void set_serial_number(void)
394 {
395         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
396         int ret;
397         char serial_string[17] = { 0 };
398
399         if (env_get("serial#"))
400                 return;
401
402         BCM2835_MBOX_INIT_HDR(msg);
403         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
404
405         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
406         if (ret) {
407                 printf("bcm2835: Could not query board serial\n");
408                 /* Ignore error; not critical */
409                 return;
410         }
411
412         snprintf(serial_string, sizeof(serial_string), "%016llx",
413                  msg->get_board_serial.body.resp.serial);
414         env_set("serial#", serial_string);
415 }
416
417 int misc_init_r(void)
418 {
419         set_fdt_addr();
420         set_fdtfile();
421         set_usbethaddr();
422 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
423         set_board_info();
424 #endif
425         set_serial_number();
426
427         return 0;
428 }
429
430 static void get_board_revision(void)
431 {
432         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
433         int ret;
434         const struct rpi_model *models;
435         uint32_t models_count;
436         ofnode node;
437
438         BCM2835_MBOX_INIT_HDR(msg);
439         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
440
441         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
442         if (ret) {
443                 /* Ignore error; not critical */
444                 node = ofnode_path("/system");
445                 if (!ofnode_valid(node)) {
446                         printf("bcm2835: Could not find /system node\n");
447                         return;
448                 }
449
450                 ret = ofnode_read_u32(node, "linux,revision", &revision);
451                 if (ret) {
452                         printf("bcm2835: Could not find linux,revision\n");
453                         return;
454                 }
455         } else {
456                 revision = msg->get_board_rev.body.resp.rev;
457         }
458
459         /*
460          * For details of old-vs-new scheme, see:
461          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
462          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
463          * (a few posts down)
464          *
465          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
466          * lower byte to use as the board rev:
467          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
468          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
469          */
470         if (revision & 0x800000) {
471                 rev_scheme = 1;
472                 rev_type = (revision >> 4) & 0xff;
473                 models = rpi_models_new_scheme;
474                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
475         } else {
476                 rev_scheme = 0;
477                 rev_type = revision & 0xff;
478                 models = rpi_models_old_scheme;
479                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
480         }
481         if (rev_type >= models_count) {
482                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
483                 model = &rpi_model_unknown;
484         } else if (!models[rev_type].name) {
485                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
486                 model = &rpi_model_unknown;
487         } else {
488                 model = &models[rev_type];
489         }
490
491         printf("RPI %s (0x%x)\n", model->name, revision);
492 }
493
494 int board_init(void)
495 {
496         get_board_revision();
497
498         gd->bd->bi_boot_params = 0x100;
499
500         return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
501 }
502
503 /*
504  * If the firmware passed a device tree use it for U-Boot.
505  */
506 void *board_fdt_blob_setup(int *err)
507 {
508         *err = 0;
509         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC) {
510                 *err = -ENXIO;
511                 return NULL;
512         }
513
514         return (void *)fw_dtb_pointer;
515 }
516
517 int copy_property(void *dst, void *src, char *path, char *property)
518 {
519         int dst_offset, src_offset;
520         const fdt32_t *prop;
521         int len;
522
523         src_offset = fdt_path_offset(src, path);
524         dst_offset = fdt_path_offset(dst, path);
525
526         if (src_offset < 0 || dst_offset < 0)
527                 return -1;
528
529         prop = fdt_getprop(src, src_offset, property, &len);
530         if (!prop)
531                 return -1;
532
533         return fdt_setprop(dst, dst_offset, property, prop, len);
534 }
535
536 /* Copy tweaks from the firmware dtb to the loaded dtb */
537 void  update_fdt_from_fw(void *fdt, void *fw_fdt)
538 {
539         /* Using dtb from firmware directly; leave it alone */
540         if (fdt == fw_fdt)
541                 return;
542
543         /* The firmware provides a more precie model; so copy that */
544         copy_property(fdt, fw_fdt, "/", "model");
545
546         /* memory reserve as suggested by the firmware */
547         copy_property(fdt, fw_fdt, "/", "memreserve");
548
549         /* Adjust dma-ranges for the SD card and PCI bus as they can depend on
550          * the SoC revision
551          */
552         copy_property(fdt, fw_fdt, "emmc2bus", "dma-ranges");
553         copy_property(fdt, fw_fdt, "pcie0", "dma-ranges");
554
555         /* Bootloader configuration template exposes as nvmem */
556         if (copy_property(fdt, fw_fdt, "blconfig", "reg") == 0)
557                 copy_property(fdt, fw_fdt, "blconfig", "status");
558
559         /* kernel address randomisation seed as provided by the firmware */
560         copy_property(fdt, fw_fdt, "/chosen", "kaslr-seed");
561
562         /* address of the PHY device as provided by the firmware  */
563         copy_property(fdt, fw_fdt, "ethernet0/mdio@e14/ethernet-phy@1", "reg");
564 }
565
566 int ft_board_setup(void *blob, struct bd_info *bd)
567 {
568         int node;
569
570         update_fdt_from_fw(blob, (void *)fw_dtb_pointer);
571
572         node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
573         if (node < 0)
574                 fdt_simplefb_add_node(blob);
575         else
576                 fdt_simplefb_enable_and_mem_rsv(blob);
577
578 #ifdef CONFIG_EFI_LOADER
579         /* Reserve the spin table */
580         efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
581                            EFI_RESERVED_MEMORY_TYPE);
582 #endif
583
584         return 0;
585 }