Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2020-2021 SiFive, Inc |
| 4 | * |
| 5 | * Authors: |
| 6 | * Pragnesh Patel <pragnesh.patel@sifive.com> |
| 7 | */ |
| 8 | |
| 9 | #include <init.h> |
| 10 | #include <spl.h> |
| 11 | #include <misc.h> |
| 12 | #include <log.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <asm/gpio.h> |
| 16 | #include <asm/arch/gpio.h> |
| 17 | #include <asm/arch/spl.h> |
Vincent Chen | 6a84277 | 2024-01-16 14:35:57 +0800 | [diff] [blame] | 18 | #include <linux/io.h> |
| 19 | #include <asm/arch/eeprom.h> |
| 20 | |
| 21 | struct pwm_sifive_regs { |
| 22 | unsigned int cfg; /* PWM configuration register */ |
| 23 | unsigned int pad0; /* Reserved */ |
| 24 | unsigned int cnt; /* PWM count register */ |
| 25 | unsigned int pad1; /* Reserved */ |
| 26 | unsigned int pwms; /* Scaled PWM count register */ |
| 27 | unsigned int pad2; /* Reserved */ |
| 28 | unsigned int pad3; /* Reserved */ |
| 29 | unsigned int pad4; /* Reserved */ |
| 30 | unsigned int cmp0; /* PWM 0 compare register */ |
| 31 | unsigned int cmp1; /* PWM 1 compare register */ |
| 32 | unsigned int cmp2; /* PWM 2 compare register */ |
| 33 | unsigned int cmp3; /* PWM 3 compare register */ |
| 34 | }; |
| 35 | |
| 36 | #define PWM0_BASE 0x10020000 |
| 37 | #define PWM1_BASE 0x10021000 |
| 38 | #define PWM_CFG_INIT 0x1000 |
| 39 | #define PWM_CMP_ENABLE_VAL 0x0 |
| 40 | #define PWM_CMP_DISABLE_VAL 0xffff |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 41 | |
Vincent Chen | caeb511 | 2021-07-08 09:08:21 +0800 | [diff] [blame] | 42 | #define UBRDG_RESET SIFIVE_GENERIC_GPIO_NR(0, 7) |
| 43 | #define ULPI_RESET SIFIVE_GENERIC_GPIO_NR(0, 9) |
| 44 | #define UHUB_RESET SIFIVE_GENERIC_GPIO_NR(0, 11) |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 45 | #define GEM_PHY_RESET SIFIVE_GENERIC_GPIO_NR(0, 12) |
| 46 | |
| 47 | #define MODE_SELECT_REG 0x1000 |
Thomas Skibo | f50fad6 | 2021-11-24 14:32:09 -0800 | [diff] [blame] | 48 | #define MODE_SELECT_SPI 0x6 |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 49 | #define MODE_SELECT_SD 0xb |
| 50 | #define MODE_SELECT_MASK GENMASK(3, 0) |
| 51 | |
Vincent Chen | 6a84277 | 2024-01-16 14:35:57 +0800 | [diff] [blame] | 52 | void spl_pwm_device_init(void) |
| 53 | { |
| 54 | struct pwm_sifive_regs *pwm0, *pwm1; |
| 55 | |
| 56 | pwm0 = (struct pwm_sifive_regs *)PWM0_BASE; |
| 57 | pwm1 = (struct pwm_sifive_regs *)PWM1_BASE; |
| 58 | writel(PWM_CMP_DISABLE_VAL, (void *)&pwm0->cmp0); |
| 59 | |
| 60 | /* Set the 3-color PWM LEDs to yellow in SPL */ |
| 61 | writel(PWM_CMP_ENABLE_VAL, (void *)&pwm0->cmp1); |
| 62 | writel(PWM_CMP_ENABLE_VAL, (void *)&pwm0->cmp2); |
| 63 | writel(PWM_CMP_DISABLE_VAL, (void *)&pwm0->cmp3); |
| 64 | writel(PWM_CFG_INIT, (void *)&pwm0->cfg); |
| 65 | |
| 66 | /* Turn on all the fans, (J21), (J23) and (J24), on the unmatched board */ |
| 67 | /* The SoC fan(J21) on the rev3 board cannot be controlled by PWM_COMP0, |
| 68 | * so here sets the initial value of PWM_COMP0 as DISABLE */ |
| 69 | if (get_pcb_revision_from_eeprom() == PCB_REVISION_REV3) |
| 70 | writel(PWM_CMP_DISABLE_VAL, (void *)&pwm1->cmp1); |
| 71 | else |
| 72 | writel(PWM_CMP_ENABLE_VAL, (void *)&pwm1->cmp1); |
| 73 | |
| 74 | writel(PWM_CMP_ENABLE_VAL, (void *)&pwm1->cmp2); |
| 75 | writel(PWM_CMP_ENABLE_VAL, (void *)&pwm1->cmp3); |
| 76 | writel(PWM_CFG_INIT, (void *)&pwm1->cfg); |
| 77 | } |
| 78 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 79 | static inline int spl_reset_device_by_gpio(const char *label, int pin, int low_width) |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 80 | { |
| 81 | int ret; |
| 82 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 83 | ret = gpio_request(pin, label); |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 84 | if (ret) { |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 85 | debug("%s gpio request failed: %d\n", label, ret); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | ret = gpio_direction_output(pin, 1); |
| 90 | if (ret) { |
| 91 | debug("%s gpio direction set failed: %d\n", label, ret); |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 92 | return ret; |
| 93 | } |
| 94 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 95 | udelay(1); |
| 96 | |
| 97 | gpio_set_value(pin, 0); |
| 98 | udelay(low_width); |
| 99 | gpio_set_value(pin, 1); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static inline int spl_gemgxl_init(void) |
| 105 | { |
| 106 | int ret; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 107 | /* |
| 108 | * GEMGXL init VSC8541 PHY reset sequence; |
| 109 | * leave pull-down active for 2ms |
| 110 | */ |
| 111 | udelay(2000); |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 112 | ret = spl_reset_device_by_gpio("gem_phy_reset", GEM_PHY_RESET, 1); |
| 113 | mdelay(15); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Vincent Chen | caeb511 | 2021-07-08 09:08:21 +0800 | [diff] [blame] | 118 | static inline int spl_usb_pcie_bridge_init(void) |
| 119 | { |
| 120 | return spl_reset_device_by_gpio("usb_pcie_bridge_reset", UBRDG_RESET, 3000); |
| 121 | } |
| 122 | |
| 123 | static inline int spl_usb_hub_init(void) |
| 124 | { |
| 125 | return spl_reset_device_by_gpio("usb_hub_reset", UHUB_RESET, 100); |
| 126 | } |
| 127 | |
| 128 | static inline int spl_ulpi_init(void) |
| 129 | { |
| 130 | return spl_reset_device_by_gpio("ulpi_reset", ULPI_RESET, 1); |
| 131 | } |
| 132 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 133 | int spl_board_init_f(void) |
| 134 | { |
| 135 | int ret; |
| 136 | |
Lukas Funke | 2e71a9e | 2024-04-24 09:43:38 +0200 | [diff] [blame] | 137 | ret = spl_dram_init(); |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 138 | if (ret) { |
Lukas Funke | 2e71a9e | 2024-04-24 09:43:38 +0200 | [diff] [blame] | 139 | debug("HiFive Unmatched FU740 DRAM init failed: %d\n", ret); |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 140 | goto end; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Vincent Chen | 6a84277 | 2024-01-16 14:35:57 +0800 | [diff] [blame] | 143 | spl_pwm_device_init(); |
| 144 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 145 | ret = spl_gemgxl_init(); |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 146 | if (ret) { |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 147 | debug("Gigabit ethernet PHY (VSC8541) init failed: %d\n", ret); |
| 148 | goto end; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Vincent Chen | caeb511 | 2021-07-08 09:08:21 +0800 | [diff] [blame] | 151 | ret = spl_usb_pcie_bridge_init(); |
| 152 | if (ret) { |
| 153 | debug("USB Bridge (ASM1042A) init failed: %d\n", ret); |
| 154 | goto end; |
| 155 | } |
| 156 | |
| 157 | ret = spl_usb_hub_init(); |
| 158 | if (ret) { |
| 159 | debug("USB Hub (ASM1074) init failed: %d\n", ret); |
| 160 | goto end; |
| 161 | } |
| 162 | |
| 163 | ret = spl_ulpi_init(); |
| 164 | if (ret) { |
| 165 | debug("USB 2.0 PHY (USB3320C) init failed: %d\n", ret); |
| 166 | goto end; |
| 167 | } |
| 168 | |
Vincent Chen | 5a6def9 | 2021-07-08 09:08:20 +0800 | [diff] [blame] | 169 | end: |
| 170 | return ret; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | u32 spl_boot_device(void) |
| 174 | { |
| 175 | u32 mode_select = readl((void *)MODE_SELECT_REG); |
| 176 | u32 boot_device = mode_select & MODE_SELECT_MASK; |
| 177 | |
| 178 | switch (boot_device) { |
Thomas Skibo | f50fad6 | 2021-11-24 14:32:09 -0800 | [diff] [blame] | 179 | case MODE_SELECT_SPI: |
| 180 | return BOOT_DEVICE_SPI; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 181 | case MODE_SELECT_SD: |
| 182 | return BOOT_DEVICE_MMC1; |
| 183 | default: |
| 184 | debug("Unsupported boot device 0x%x but trying MMC1\n", |
| 185 | boot_device); |
| 186 | return BOOT_DEVICE_MMC1; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #ifdef CONFIG_SPL_LOAD_FIT |
| 191 | int board_fit_config_name_match(const char *name) |
| 192 | { |
Zong Li | 288ad1f | 2021-07-20 14:26:08 +0800 | [diff] [blame] | 193 | /* boot using first FIT config */ |
| 194 | return 0; |
Green Wan | 2e5da52 | 2021-05-27 06:52:13 -0700 | [diff] [blame] | 195 | } |
| 196 | #endif |