1 From e3152c690732fb141500379f32fbe2203fa47059 Mon Sep 17 00:00:00 2001
2 From: Samuel Holland <samuel@sholland.org>
3 Date: Sat, 6 Aug 2022 00:48:38 -0500
4 Subject: [PATCH 87/90] sunxi: Add temporary RISC-V version of board code
6 Signed-off-by: Samuel Holland <samuel@sholland.org>
8 board/sunxi/Makefile | 3 +-
9 board/sunxi/board-riscv.c | 687 +++++++++++++++++++++++++++++++++
10 include/configs/sunxi-common.h | 1 -
11 3 files changed, 689 insertions(+), 2 deletions(-)
12 create mode 100644 board/sunxi/board-riscv.c
14 --- a/board/sunxi/Makefile
15 +++ b/board/sunxi/Makefile
18 # (C) Copyright 2000-2003
19 # Wolfgang Denk, DENX Software Engineering, wd@denx.de.
21 +obj-$(CONFIG_ARM) += board.o
22 +obj-$(CONFIG_RISCV) += board-riscv.o
23 obj-$(CONFIG_SUN7I_GMAC) += gmac.o
24 obj-$(CONFIG_MACH_SUN4I) += dram_sun4i_auto.o
25 obj-$(CONFIG_MACH_SUN5I) += dram_sun5i_auto.o
27 +++ b/board/sunxi/board-riscv.c
29 +// SPDX-License-Identifier: GPL-2.0+
31 + * (C) Copyright 2012-2013 Henrik Nordstrom <henrik@henriknordstrom.net>
32 + * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
34 + * (C) Copyright 2007-2011
35 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
36 + * Tom Cubie <tangliang@allwinnertech.com>
38 + * Some board init for the Allwinner A10-evb board.
44 +#include <env_internal.h>
45 +#include <fdt_support.h>
46 +#include <generic-phy.h>
50 +#include <phy-sun4i-usb.h>
52 +#include <remoteproc.h>
54 +#include <status_led.h>
55 +#include <sunxi_image.h>
56 +#include <u-boot/crc.h>
58 +#include <asm/global_data.h>
64 + /* https://lore.kernel.org/u-boot/31587574-4cd1-02da-9761-0134ac82b94b@sholland.org/ */
65 + return cpu_probe_all();
68 +int sunxi_get_sid(unsigned int *sid)
73 +#define SPL_ADDR CONFIG_SUNXI_SRAM_ADDRESS
75 +/* The low 8-bits of the 'boot_media' field in the SPL header */
76 +#define SUNXI_BOOTED_FROM_MMC0 0
77 +#define SUNXI_BOOTED_FROM_NAND 1
78 +#define SUNXI_BOOTED_FROM_MMC2 2
79 +#define SUNXI_BOOTED_FROM_SPI 3
80 +#define SUNXI_BOOTED_FROM_MMC0_HIGH 0x10
81 +#define SUNXI_BOOTED_FROM_MMC2_HIGH 0x12
83 +#define SUNXI_INVALID_BOOT_SOURCE -1
85 +static int sunxi_egon_valid(struct boot_file_head *egon_head)
87 + return !memcmp(egon_head->magic, BOOT0_MAGIC, 8); /* eGON.BT0 */
90 +static int sunxi_toc0_valid(struct toc0_main_info *toc0_info)
92 + return !memcmp(toc0_info->name, TOC0_MAIN_INFO_NAME, 8); /* TOC0.GLH */
95 +static int sunxi_get_boot_source(void)
97 + struct boot_file_head *egon_head = (void *)SPL_ADDR;
98 + struct toc0_main_info *toc0_info = (void *)SPL_ADDR;
101 + * On the ARMv5 SoCs, the SPL header in SRAM is overwritten by the
102 + * exception vectors in U-Boot proper, so we won't find any
103 + * information there. Also the FEL stash is only valid in the SPL,
104 + * so we can't use that either. So if this is called from U-Boot
105 + * proper, just return MMC0 as a placeholder, for now.
107 + if (IS_ENABLED(CONFIG_MACH_SUNIV) &&
108 + !IS_ENABLED(CONFIG_SPL_BUILD))
109 + return SUNXI_BOOTED_FROM_MMC0;
111 + if (sunxi_egon_valid(egon_head))
112 + return readb(&egon_head->boot_media);
113 + if (sunxi_toc0_valid(toc0_info))
114 + return readb(&toc0_info->platform[0]);
116 + /* Not a valid image, so we must have been booted via FEL. */
117 + return SUNXI_INVALID_BOOT_SOURCE;
120 +/* The sunxi internal brom will try to loader external bootloader
121 + * from mmc0, nand flash, mmc2.
123 +uint32_t sunxi_get_boot_device(void)
125 + int boot_source = sunxi_get_boot_source();
128 + * When booting from the SD card or NAND memory, the "eGON.BT0"
129 + * signature is expected to be found in memory at the address 0x0004
130 + * (see the "mksunxiboot" tool, which generates this header).
132 + * When booting in the FEL mode over USB, this signature is patched in
133 + * memory and replaced with something else by the 'fel' tool. This other
134 + * signature is selected in such a way, that it can't be present in a
135 + * valid bootable SD card image (because the BROM would refuse to
136 + * execute the SPL in this case).
138 + * This checks for the signature and if it is not found returns to
139 + * the FEL code in the BROM to wait and receive the main u-boot
140 + * binary over USB. If it is found, it determines where SPL was
143 + switch (boot_source) {
144 + case SUNXI_INVALID_BOOT_SOURCE:
145 + return BOOT_DEVICE_BOARD;
146 + case SUNXI_BOOTED_FROM_MMC0:
147 + case SUNXI_BOOTED_FROM_MMC0_HIGH:
148 + return BOOT_DEVICE_MMC1;
149 + case SUNXI_BOOTED_FROM_NAND:
150 + return BOOT_DEVICE_NAND;
151 + case SUNXI_BOOTED_FROM_MMC2:
152 + case SUNXI_BOOTED_FROM_MMC2_HIGH:
153 + return BOOT_DEVICE_MMC2;
154 + case SUNXI_BOOTED_FROM_SPI:
155 + return BOOT_DEVICE_SPI;
158 + panic("Unknown boot source %d\n", boot_source);
159 + return -1; /* Never reached */
162 +uint32_t sunxi_get_spl_size(void)
164 + struct boot_file_head *egon_head = (void *)SPL_ADDR;
165 + struct toc0_main_info *toc0_info = (void *)SPL_ADDR;
167 + if (sunxi_egon_valid(egon_head))
168 + return readl(&egon_head->length);
169 + if (sunxi_toc0_valid(toc0_info))
170 + return readl(&toc0_info->length);
172 + /* Not a valid image, so use the default U-Boot offset. */
177 + * The eGON SPL image can be located at 8KB or at 128KB into an SD card or
178 + * an eMMC device. The boot source has bit 4 set in the latter case.
179 + * By adding 120KB to the normal offset when booting from a "high" location
180 + * we can support both cases.
181 + * Also U-Boot proper is located at least 32KB after the SPL, but will
182 + * immediately follow the SPL if that is bigger than that.
184 +unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
185 + unsigned long raw_sect)
187 + unsigned long spl_size = sunxi_get_spl_size();
188 + unsigned long sector;
190 + sector = max(raw_sect, spl_size / 512);
192 + switch (sunxi_get_boot_source()) {
193 + case SUNXI_BOOTED_FROM_MMC0_HIGH:
194 + case SUNXI_BOOTED_FROM_MMC2_HIGH:
195 + sector += (128 - 8) * 2;
199 + printf("SPL size = %lu, sector = %lu\n", spl_size, sector);
204 +u32 spl_boot_device(void)
206 + return sunxi_get_boot_device();
209 +#define CSR_MXSTATUS 0x7c0
210 +#define CSR_MHCR 0x7c1
211 +#define CSR_MCOR 0x7c2
212 +#define CSR_MHINT 0x7c5
214 +int spl_board_init_f(void)
217 + struct udevice *dev;
220 + ret = uclass_get_device(UCLASS_RAM, 0, &dev);
222 + debug("DRAM init failed: %d\n", ret);
226 + /* Initialize extension CSRs. */
227 + printf("mxstatus=0x%08lx mhcr=0x%08lx mcor=0x%08lx mhint=0x%08lx\n",
228 + csr_read(CSR_MXSTATUS),
229 + csr_read(CSR_MHCR),
230 + csr_read(CSR_MCOR),
231 + csr_read(CSR_MHINT));
233 + csr_set(CSR_MXSTATUS, 0x638000);
234 + csr_write(CSR_MCOR, 0x70013);
235 + csr_write(CSR_MHCR, 0x11ff);
236 + csr_write(CSR_MHINT, 0x16e30c);
241 +#ifdef CONFIG_SPL_BUILD
242 +void spl_perform_fixups(struct spl_image_info *spl_image)
244 + struct ram_info info;
245 + struct udevice *dev;
248 + ret = uclass_get_device(UCLASS_RAM, 0, &dev);
250 + panic("No RAM device");
252 + ret = ram_get_info(dev, &info);
254 + panic("No RAM info");
256 + ret = fdt_fixup_memory(spl_image->fdt_addr, info.base, info.size);
258 + panic("Failed to update DTB");
263 +DECLARE_GLOBAL_DATA_PTR;
266 + * Try to use the environment from the boot source first.
267 + * For MMC, this means a FAT partition on the boot device (SD or eMMC).
268 + * If the raw MMC environment is also enabled, this is tried next.
269 + * When booting from NAND we try UBI first, then NAND directly.
270 + * SPI flash falls back to FAT (on SD card).
272 +enum env_location env_get_location(enum env_operation op, int prio)
275 + return ENVL_UNKNOWN;
277 + /* NOWHERE is exclusive, no other option can be defined. */
278 + if (IS_ENABLED(CONFIG_ENV_IS_NOWHERE))
279 + return ENVL_NOWHERE;
281 + switch (sunxi_get_boot_device()) {
282 + case BOOT_DEVICE_MMC1:
283 + case BOOT_DEVICE_MMC2:
284 + if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
286 + if (IS_ENABLED(CONFIG_ENV_IS_IN_MMC))
289 + case BOOT_DEVICE_NAND:
290 + if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
292 + if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
295 + case BOOT_DEVICE_SPI:
296 + if (prio == 0 && IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
297 + return ENVL_SPI_FLASH;
298 + if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
301 + case BOOT_DEVICE_BOARD:
308 + * If we come here for the first time, we *must* return a valid
309 + * environment location other than ENVL_UNKNOWN, or the setup sequence
310 + * in board_f() will silently hang. This is arguably a bug in
311 + * env_init(), but for now pick one environment for which we know for
312 + * sure to have a driver for. For all defconfigs this is either FAT
313 + * or UBI, or NOWHERE, which is already handled above.
316 + if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
318 + if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
322 + return ENVL_UNKNOWN;
326 + * On older SoCs the SPL is actually at address zero, so using NULL as
327 + * an error value does not work.
329 +#define INVALID_SPL_HEADER ((void *)~0UL)
331 +static struct boot_file_head * get_spl_header(uint8_t req_version)
333 + struct boot_file_head *spl = (void *)(ulong)SPL_ADDR;
334 + uint8_t spl_header_version = spl->spl_signature[3];
336 + /* Is there really the SPL header (still) there? */
337 + if (memcmp(spl->spl_signature, SPL_SIGNATURE, 3) != 0)
338 + return INVALID_SPL_HEADER;
340 + if (spl_header_version < req_version) {
341 + printf("sunxi SPL version mismatch: expected %u, got %u\n",
342 + req_version, spl_header_version);
343 + return INVALID_SPL_HEADER;
349 +static const char *get_spl_dt_name(void)
351 + struct boot_file_head *spl = get_spl_header(SPL_DT_HEADER_VERSION);
353 + /* Check if there is a DT name stored in the SPL header. */
354 + if (spl != INVALID_SPL_HEADER && spl->dt_name_offset)
355 + return (char *)spl + spl->dt_name_offset;
360 +#if CONFIG_MMC_SUNXI_SLOT_EXTRA != -1
361 +int mmc_get_env_dev(void)
363 + switch (sunxi_get_boot_device()) {
364 + case BOOT_DEVICE_MMC1:
366 + case BOOT_DEVICE_MMC2:
369 + return CONFIG_SYS_MMC_ENV_DEV;
374 +#ifdef CONFIG_SPL_BUILD
375 +void sunxi_board_init(void)
377 +#ifdef CONFIG_LED_STATUS
378 + if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
384 +#ifdef CONFIG_USB_GADGET
385 +int g_dnl_board_usb_cable_connected(void)
387 + struct udevice *dev;
391 + ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, 0, &dev);
393 + pr_err("%s: Cannot find USB device\n", __func__);
397 + ret = generic_phy_get_by_name(dev, "usb", &phy);
399 + pr_err("failed to get %s USB PHY\n", dev->name);
403 + ret = generic_phy_init(&phy);
405 + pr_debug("failed to init %s USB PHY\n", dev->name);
409 + return sun4i_usb_phy_vbus_detect(&phy);
413 +#ifdef CONFIG_SERIAL_TAG
414 +void get_board_serial(struct tag_serialnr *serialnr)
416 + char *serial_string;
417 + unsigned long long serial;
419 + serial_string = env_get("serial#");
421 + if (serial_string) {
422 + serial = simple_strtoull(serial_string, NULL, 16);
424 + serialnr->high = (unsigned int) (serial >> 32);
425 + serialnr->low = (unsigned int) (serial & 0xffffffff);
427 + serialnr->high = 0;
434 + * Check the SPL header for the "sunxi" variant. If found: parse values
435 + * that might have been passed by the loader ("fel" utility), and update
436 + * the environment accordingly.
438 +static void parse_spl_header(const uint32_t spl_addr)
440 + struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
442 + if (spl == INVALID_SPL_HEADER)
445 + if (!spl->fel_script_address)
448 + if (spl->fel_uEnv_length != 0) {
450 + * data is expected in uEnv.txt compatible format, so "env
451 + * import -t" the string(s) at fel_script_address right away.
453 + himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
454 + spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
457 + /* otherwise assume .scr format (mkimage-type script) */
458 + env_set_hex("fel_scriptaddr", spl->fel_script_address);
461 +static bool get_unique_sid(unsigned int *sid)
463 + if (sunxi_get_sid(sid) != 0)
470 + * The single words 1 - 3 of the SID have quite a few bits
471 + * which are the same on many models, so we take a crc32
472 + * of all 3 words, to get a more unique value.
474 + * Note we only do this on newer SoCs as we cannot change
475 + * the algorithm on older SoCs since those have been using
476 + * fixed mac-addresses based on only using word 3 for a
477 + * long time and changing a fixed mac-address with an
478 + * u-boot update is not good.
480 +#if !defined(CONFIG_MACH_SUN4I) && !defined(CONFIG_MACH_SUN5I) && \
481 + !defined(CONFIG_MACH_SUN6I) && !defined(CONFIG_MACH_SUN7I) && \
482 + !defined(CONFIG_MACH_SUN8I_A23) && !defined(CONFIG_MACH_SUN8I_A33)
483 + sid[3] = crc32(0, (unsigned char *)&sid[1], 12);
486 + /* Ensure the NIC specific bytes of the mac are not all 0 */
487 + if ((sid[3] & 0xffffff) == 0)
488 + sid[3] |= 0x800000;
494 + * Note this function gets called multiple times.
495 + * It must not make any changes to env variables which already exist.
497 +static void setup_environment(const void *fdt)
499 + char serial_string[17] = { 0 };
500 + unsigned int sid[4];
501 + uint8_t mac_addr[6];
505 + if (!get_unique_sid(sid))
508 + for (i = 0; i < 4; i++) {
509 + sprintf(ethaddr, "ethernet%d", i);
510 + if (!fdt_get_alias(fdt, ethaddr))
514 + strcpy(ethaddr, "ethaddr");
516 + sprintf(ethaddr, "eth%daddr", i);
518 + if (env_get(ethaddr))
521 + /* Non OUI / registered MAC address */
522 + mac_addr[0] = (i << 4) | 0x02;
523 + mac_addr[1] = (sid[0] >> 0) & 0xff;
524 + mac_addr[2] = (sid[3] >> 24) & 0xff;
525 + mac_addr[3] = (sid[3] >> 16) & 0xff;
526 + mac_addr[4] = (sid[3] >> 8) & 0xff;
527 + mac_addr[5] = (sid[3] >> 0) & 0xff;
529 + eth_env_set_enetaddr(ethaddr, mac_addr);
532 + if (!env_get("serial#")) {
533 + snprintf(serial_string, sizeof(serial_string),
534 + "%08x%08x", sid[0], sid[3]);
536 + env_set("serial#", serial_string);
540 +int misc_init_r(void)
542 + const char *spl_dt_name;
545 + env_set("fel_booted", NULL);
546 + env_set("fel_scriptaddr", NULL);
547 + env_set("mmc_bootdev", NULL);
549 + boot = sunxi_get_boot_device();
550 + /* determine if we are running in FEL mode */
551 + if (boot == BOOT_DEVICE_BOARD) {
552 + env_set("fel_booted", "1");
553 + parse_spl_header(SPL_ADDR);
554 + /* or if we booted from MMC, and which one */
555 + } else if (boot == BOOT_DEVICE_MMC1) {
556 + env_set("mmc_bootdev", "0");
557 + } else if (boot == BOOT_DEVICE_MMC2) {
558 + env_set("mmc_bootdev", "1");
561 + /* Set fdtfile to match the FIT configuration chosen in SPL. */
562 + spl_dt_name = get_spl_dt_name();
564 + char *prefix = IS_ENABLED(CONFIG_ARM64) ? "allwinner/" : "";
567 + snprintf(str, sizeof(str), "%s%s.dtb", prefix, spl_dt_name);
568 + env_set("fdtfile", str);
571 + setup_environment(gd->fdt_blob);
576 +int board_late_init(void)
578 +#ifdef CONFIG_USB_ETHER
582 +#ifdef SUNXI_SCP_BASE
583 + if (!rproc_load(0, SUNXI_SCP_BASE, SUNXI_SCP_MAX_SIZE)) {
584 + puts("Starting SCP...\n");
592 +static void bluetooth_dt_fixup(void *blob)
594 + /* Some devices ship with a Bluetooth controller default address.
595 + * Set a valid address through the device tree.
597 + uchar tmp[ETH_ALEN], bdaddr[ETH_ALEN];
598 + unsigned int sid[4];
601 + if (!CONFIG_BLUETOOTH_DT_DEVICE_FIXUP[0])
604 + if (eth_env_get_enetaddr("bdaddr", tmp)) {
605 + /* Convert between the binary formats of the corresponding stacks */
606 + for (i = 0; i < ETH_ALEN; ++i)
607 + bdaddr[i] = tmp[ETH_ALEN - i - 1];
609 + if (!get_unique_sid(sid))
612 + bdaddr[0] = ((sid[3] >> 0) & 0xff) ^ 1;
613 + bdaddr[1] = (sid[3] >> 8) & 0xff;
614 + bdaddr[2] = (sid[3] >> 16) & 0xff;
615 + bdaddr[3] = (sid[3] >> 24) & 0xff;
616 + bdaddr[4] = (sid[0] >> 0) & 0xff;
620 + do_fixup_by_compat(blob, CONFIG_BLUETOOTH_DT_DEVICE_FIXUP,
621 + "local-bd-address", bdaddr, ETH_ALEN, 1);
624 +int ft_board_setup(void *blob, struct bd_info *bd)
626 + int __maybe_unused r;
629 + * Call setup_environment and fdt_fixup_ethernet again
630 + * in case the boot fdt has ethernet aliases the u-boot
631 + * copy does not have.
633 + setup_environment(blob);
634 + fdt_fixup_ethernet(blob);
636 + bluetooth_dt_fixup(blob);
638 +#ifdef CONFIG_VIDEO_DT_SIMPLEFB
639 + r = sunxi_simplefb_setup(blob);
646 +#ifdef CONFIG_SPL_LOAD_FIT
648 +static void set_spl_dt_name(const char *name)
650 + struct boot_file_head *spl = get_spl_header(SPL_ENV_HEADER_VERSION);
652 + if (spl == INVALID_SPL_HEADER)
655 + /* Promote the header version for U-Boot proper, if needed. */
656 + if (spl->spl_signature[3] < SPL_DT_HEADER_VERSION)
657 + spl->spl_signature[3] = SPL_DT_HEADER_VERSION;
659 + strcpy((char *)&spl->string_pool, name);
660 + spl->dt_name_offset = offsetof(struct boot_file_head, string_pool);
663 +int board_fit_config_name_match(const char *name)
665 + const char *best_dt_name = get_spl_dt_name();
668 +#ifdef CONFIG_DEFAULT_DEVICE_TREE
669 + if (best_dt_name == NULL)
670 + best_dt_name = CONFIG_DEFAULT_DEVICE_TREE;
673 + if (best_dt_name == NULL) {
674 + /* No DT name was provided, so accept the first config. */
677 +#ifdef CONFIG_PINE64_DT_SELECTION
678 + if (strstr(best_dt_name, "-pine64-plus")) {
679 + /* Differentiate the Pine A64 boards by their DRAM size. */
680 + if ((gd->ram_size == 512 * 1024 * 1024))
681 + best_dt_name = "sun50i-a64-pine64";
684 +#ifdef CONFIG_PINEPHONE_DT_SELECTION
685 + if (strstr(best_dt_name, "-pinephone")) {
686 + /* Differentiate the PinePhone revisions by GPIO inputs. */
687 + prcm_apb0_enable(PRCM_APB0_GATE_PIO);
688 + sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_UP);
689 + sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_INPUT);
692 + /* PL6 is pulled low by the modem on v1.2. */
693 + if (gpio_get_value(SUNXI_GPL(6)) == 0)
694 + best_dt_name = "sun50i-a64-pinephone-1.2";
696 + best_dt_name = "sun50i-a64-pinephone-1.1";
698 + sunxi_gpio_set_cfgpin(SUNXI_GPL(6), SUNXI_GPIO_DISABLE);
699 + sunxi_gpio_set_pull(SUNXI_GPL(6), SUNXI_GPIO_PULL_DISABLE);
700 + prcm_apb0_disable(PRCM_APB0_GATE_PIO);
704 + ret = strcmp(name, best_dt_name);
707 + * If one of the FIT configurations matches the most accurate DT name,
708 + * update the SPL header to provide that DT name to U-Boot proper.
711 + set_spl_dt_name(best_dt_name);
716 --- a/include/configs/sunxi-common.h
717 +++ b/include/configs/sunxi-common.h
719 #ifndef _SUNXI_COMMON_CONFIG_H
720 #define _SUNXI_COMMON_CONFIG_H
722 -#include <asm/arch/cpu.h>
723 #include <linux/stringify.h>
725 /* Serial & console */