Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 6 | #include <adc.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 8 | #include <net.h> |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 9 | #include <asm/arch/stm32.h> |
| 10 | #include <asm/arch/sys_proto.h> |
| 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <bootm.h> |
| 14 | #include <clk.h> |
| 15 | #include <config.h> |
| 16 | #include <dm.h> |
| 17 | #include <dm/device.h> |
| 18 | #include <dm/uclass.h> |
| 19 | #include <env.h> |
| 20 | #include <env_internal.h> |
| 21 | #include <g_dnl.h> |
| 22 | #include <generic-phy.h> |
| 23 | #include <hang.h> |
| 24 | #include <i2c.h> |
| 25 | #include <i2c_eeprom.h> |
| 26 | #include <init.h> |
| 27 | #include <led.h> |
| 28 | #include <memalign.h> |
| 29 | #include <misc.h> |
| 30 | #include <mtd.h> |
| 31 | #include <mtd_node.h> |
| 32 | #include <netdev.h> |
| 33 | #include <phy.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 34 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 35 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 36 | #include <linux/printk.h> |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 37 | #include <power/regulator.h> |
| 38 | #include <remoteproc.h> |
| 39 | #include <reset.h> |
| 40 | #include <syscon.h> |
| 41 | #include <usb.h> |
| 42 | #include <usb/dwc2_udc.h> |
| 43 | #include <watchdog.h> |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 44 | #include <dm/ofnode.h> |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 45 | #include "../common/dh_common.h" |
Patrick Delaunay | f2f25c3 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 46 | #include "../../st/common/stpmic1.h" |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 47 | |
| 48 | /* SYSCFG registers */ |
| 49 | #define SYSCFG_BOOTR 0x00 |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 50 | #define SYSCFG_IOCTRLSETR 0x18 |
| 51 | #define SYSCFG_ICNR 0x1C |
| 52 | #define SYSCFG_CMPCR 0x20 |
| 53 | #define SYSCFG_CMPENSETR 0x24 |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 54 | |
| 55 | #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0) |
| 56 | #define SYSCFG_BOOTR_BOOTPD_SHIFT 4 |
| 57 | |
| 58 | #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0) |
| 59 | #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1) |
| 60 | #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2) |
| 61 | #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3) |
| 62 | #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4) |
| 63 | |
| 64 | #define SYSCFG_CMPCR_SW_CTRL BIT(1) |
| 65 | #define SYSCFG_CMPCR_READY BIT(8) |
| 66 | |
| 67 | #define SYSCFG_CMPENSETR_MPU_EN BIT(0) |
| 68 | |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 69 | #define KS_CCR 0x08 |
| 70 | #define KS_CCR_EEPROM BIT(9) |
| 71 | #define KS_BE0 BIT(12) |
| 72 | #define KS_BE1 BIT(13) |
Marek Vasut | b2b31c1 | 2021-05-03 13:31:39 +0200 | [diff] [blame] | 73 | #define KS_CIDER 0xC0 |
| 74 | #define CIDER_ID 0x8870 |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 75 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 76 | static bool dh_stm32_mac_is_in_ks8851(void) |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 77 | { |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 78 | ofnode node; |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 79 | u32 reg, cider, ccr; |
Marek Vasut | b0a2a49 | 2020-07-31 01:34:50 +0200 | [diff] [blame] | 80 | |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 81 | node = ofnode_path("ethernet1"); |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 82 | if (!ofnode_valid(node)) |
| 83 | return false; |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 84 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 85 | if (ofnode_device_is_compatible(node, "micrel,ks8851-mll")) |
| 86 | return false; |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * KS8851 with EEPROM may use custom MAC from EEPROM, read |
| 90 | * out the KS8851 CCR register to determine whether EEPROM |
| 91 | * is present. If EEPROM is present, it must contain valid |
| 92 | * MAC address. |
| 93 | */ |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 94 | reg = ofnode_get_addr(node); |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 95 | if (!reg) |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 96 | return false; |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 97 | |
Marek Vasut | b2b31c1 | 2021-05-03 13:31:39 +0200 | [diff] [blame] | 98 | writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2); |
| 99 | cider = readw(reg); |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 100 | if ((cider & 0xfff0) != CIDER_ID) |
| 101 | return true; |
Marek Vasut | b2b31c1 | 2021-05-03 13:31:39 +0200 | [diff] [blame] | 102 | |
Marek Vasut | 145a876 | 2020-10-08 15:14:58 +0200 | [diff] [blame] | 103 | writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2); |
| 104 | ccr = readw(reg); |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 105 | if (ccr & KS_CCR_EEPROM) |
| 106 | return true; |
| 107 | |
| 108 | return false; |
| 109 | } |
Marek Vasut | b0a2a49 | 2020-07-31 01:34:50 +0200 | [diff] [blame] | 110 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 111 | static int dh_stm32_setup_ethaddr(void) |
| 112 | { |
| 113 | unsigned char enetaddr[6]; |
| 114 | |
| 115 | if (dh_mac_is_in_env("ethaddr")) |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 116 | return 0; |
| 117 | |
Marek Vasut | 29ab1a9 | 2024-03-12 22:15:58 +0100 | [diff] [blame] | 118 | if (dh_get_mac_is_enabled("ethernet0")) |
| 119 | return 0; |
| 120 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 121 | if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) |
| 122 | return eth_env_set_enetaddr("ethaddr", enetaddr); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 123 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 124 | return -ENXIO; |
| 125 | } |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 126 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 127 | static int dh_stm32_setup_eth1addr(void) |
| 128 | { |
| 129 | unsigned char enetaddr[6]; |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 130 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 131 | if (dh_mac_is_in_env("eth1addr")) |
| 132 | return 0; |
Marek Vasut | b0a2a49 | 2020-07-31 01:34:50 +0200 | [diff] [blame] | 133 | |
Marek Vasut | 29ab1a9 | 2024-03-12 22:15:58 +0100 | [diff] [blame] | 134 | if (dh_get_mac_is_enabled("ethernet1")) |
| 135 | return 0; |
| 136 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 137 | if (dh_stm32_mac_is_in_ks8851()) |
| 138 | return 0; |
| 139 | |
| 140 | if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) { |
Marek Vasut | b0a2a49 | 2020-07-31 01:34:50 +0200 | [diff] [blame] | 141 | enetaddr[5]++; |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 142 | return eth_env_set_enetaddr("eth1addr", enetaddr); |
Marek Vasut | b0a2a49 | 2020-07-31 01:34:50 +0200 | [diff] [blame] | 143 | } |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 144 | |
Philip Oberfichtner | 78fa6b9 | 2022-07-26 15:04:53 +0200 | [diff] [blame] | 145 | return -ENXIO; |
| 146 | } |
| 147 | |
| 148 | int setup_mac_address(void) |
| 149 | { |
| 150 | if (dh_stm32_setup_ethaddr()) |
| 151 | log_err("%s: Unable to setup ethaddr!\n", __func__); |
| 152 | |
| 153 | if (dh_stm32_setup_eth1addr()) |
| 154 | log_err("%s: Unable to setup eth1addr!\n", __func__); |
| 155 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int checkboard(void) |
| 160 | { |
| 161 | char *mode; |
| 162 | const char *fdt_compat; |
| 163 | int fdt_compat_len; |
| 164 | |
Patrick Delaunay | 472407a | 2020-03-18 09:22:49 +0100 | [diff] [blame] | 165 | if (IS_ENABLED(CONFIG_TFABOOT)) |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 166 | mode = "trusted"; |
| 167 | else |
| 168 | mode = "basic"; |
| 169 | |
| 170 | printf("Board: stm32mp1 in %s mode", mode); |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 171 | fdt_compat = ofnode_get_property(ofnode_root(), "compatible", |
| 172 | &fdt_compat_len); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 173 | if (fdt_compat && fdt_compat_len) |
| 174 | printf(" (%s)", fdt_compat); |
| 175 | puts("\n"); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 180 | #ifdef CONFIG_BOARD_EARLY_INIT_F |
Marek Vasut | e5905ee | 2023-05-04 21:52:08 +0200 | [diff] [blame] | 181 | static u8 brdcode __section(".data"); |
| 182 | static u8 ddr3code __section(".data"); |
| 183 | static u8 somcode __section(".data"); |
Patrick Delaunay | 08c891a | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 184 | static u32 opp_voltage_mv __section(".data"); |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 185 | |
| 186 | static void board_get_coding_straps(void) |
| 187 | { |
| 188 | struct gpio_desc gpio[4]; |
| 189 | ofnode node; |
| 190 | int i, ret; |
| 191 | |
Marek Vasut | 4bd7a5a | 2021-11-13 03:26:39 +0100 | [diff] [blame] | 192 | brdcode = 0; |
| 193 | ddr3code = 0; |
| 194 | somcode = 0; |
| 195 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 196 | node = ofnode_path("/config"); |
| 197 | if (!ofnode_valid(node)) { |
| 198 | printf("%s: no /config node?\n", __func__); |
| 199 | return; |
| 200 | } |
| 201 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 202 | ret = gpio_request_list_by_name_nodev(node, "dh,som-coding-gpios", |
| 203 | gpio, ARRAY_SIZE(gpio), |
| 204 | GPIOD_IS_IN); |
| 205 | for (i = 0; i < ret; i++) |
| 206 | somcode |= !!dm_gpio_get_value(&(gpio[i])) << i; |
| 207 | |
Marek Vasut | 4bd7a5a | 2021-11-13 03:26:39 +0100 | [diff] [blame] | 208 | gpio_free_list_nodev(gpio, ret); |
| 209 | |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 210 | ret = gpio_request_list_by_name_nodev(node, "dh,ddr3-coding-gpios", |
| 211 | gpio, ARRAY_SIZE(gpio), |
| 212 | GPIOD_IS_IN); |
| 213 | for (i = 0; i < ret; i++) |
| 214 | ddr3code |= !!dm_gpio_get_value(&(gpio[i])) << i; |
| 215 | |
Marek Vasut | 4bd7a5a | 2021-11-13 03:26:39 +0100 | [diff] [blame] | 216 | gpio_free_list_nodev(gpio, ret); |
| 217 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 218 | ret = gpio_request_list_by_name_nodev(node, "dh,board-coding-gpios", |
| 219 | gpio, ARRAY_SIZE(gpio), |
| 220 | GPIOD_IS_IN); |
| 221 | for (i = 0; i < ret; i++) |
| 222 | brdcode |= !!dm_gpio_get_value(&(gpio[i])) << i; |
| 223 | |
Marek Vasut | 4bd7a5a | 2021-11-13 03:26:39 +0100 | [diff] [blame] | 224 | gpio_free_list_nodev(gpio, ret); |
| 225 | |
Harald Seiler | 1768f5d | 2023-09-27 14:46:25 +0200 | [diff] [blame] | 226 | if (CONFIG_IS_ENABLED(DISPLAY_PRINT)) |
| 227 | printf("Code: SoM:rev=%d,ddr3=%d Board:rev=%d\n", |
| 228 | somcode, ddr3code, brdcode); |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int board_stm32mp1_ddr_config_name_match(struct udevice *dev, |
| 232 | const char *name) |
| 233 | { |
Marek Vasut | 272198e | 2020-04-29 15:08:38 +0200 | [diff] [blame] | 234 | if (ddr3code == 1 && |
| 235 | !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x1gb-533mhz")) |
| 236 | return 0; |
| 237 | |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 238 | if (ddr3code == 2 && |
Marek Vasut | 272198e | 2020-04-29 15:08:38 +0200 | [diff] [blame] | 239 | !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x2gb-533mhz")) |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 240 | return 0; |
| 241 | |
| 242 | if (ddr3code == 3 && |
Marek Vasut | 272198e | 2020-04-29 15:08:38 +0200 | [diff] [blame] | 243 | !strcmp(name, "st,ddr3l-dhsom-1066-888-bin-g-2x4gb-533mhz")) |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 244 | return 0; |
| 245 | |
| 246 | return -EINVAL; |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Patrick Delaunay | 08c891a | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 249 | void board_vddcore_init(u32 voltage_mv) |
| 250 | { |
| 251 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
| 252 | opp_voltage_mv = voltage_mv; |
| 253 | } |
| 254 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 255 | int board_early_init_f(void) |
| 256 | { |
Patrick Delaunay | f2f25c3 | 2020-05-25 12:19:46 +0200 | [diff] [blame] | 257 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 08c891a | 2020-05-25 12:19:47 +0200 | [diff] [blame] | 258 | stpmic1_init(opp_voltage_mv); |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 259 | board_get_coding_straps(); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | #ifdef CONFIG_SPL_LOAD_FIT |
| 265 | int board_fit_config_name_match(const char *name) |
| 266 | { |
Marek Vasut | 060cb12 | 2020-07-31 01:35:33 +0200 | [diff] [blame] | 267 | const char *compat; |
| 268 | char test[128]; |
| 269 | |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 270 | compat = ofnode_get_property(ofnode_root(), "compatible", NULL); |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 271 | |
Marek Vasut | 060cb12 | 2020-07-31 01:35:33 +0200 | [diff] [blame] | 272 | snprintf(test, sizeof(test), "%s_somrev%d_boardrev%d", |
| 273 | compat, somcode, brdcode); |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 274 | |
| 275 | if (!strcmp(name, test)) |
| 276 | return 0; |
| 277 | |
| 278 | return -EINVAL; |
| 279 | } |
| 280 | #endif |
| 281 | #endif |
| 282 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 283 | static void board_key_check(void) |
| 284 | { |
| 285 | #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG) |
| 286 | ofnode node; |
| 287 | struct gpio_desc gpio; |
| 288 | enum forced_boot_mode boot_mode = BOOT_NORMAL; |
| 289 | |
| 290 | node = ofnode_path("/config"); |
| 291 | if (!ofnode_valid(node)) { |
| 292 | debug("%s: no /config node?\n", __func__); |
| 293 | return; |
| 294 | } |
| 295 | #ifdef CONFIG_FASTBOOT |
| 296 | if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0, |
| 297 | &gpio, GPIOD_IS_IN)) { |
| 298 | debug("%s: could not find a /config/st,fastboot-gpios\n", |
| 299 | __func__); |
| 300 | } else { |
| 301 | if (dm_gpio_get_value(&gpio)) { |
| 302 | puts("Fastboot key pressed, "); |
| 303 | boot_mode = BOOT_FASTBOOT; |
| 304 | } |
| 305 | |
| 306 | dm_gpio_free(NULL, &gpio); |
| 307 | } |
| 308 | #endif |
| 309 | #ifdef CONFIG_CMD_STM32PROG |
| 310 | if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0, |
| 311 | &gpio, GPIOD_IS_IN)) { |
| 312 | debug("%s: could not find a /config/st,stm32prog-gpios\n", |
| 313 | __func__); |
| 314 | } else { |
| 315 | if (dm_gpio_get_value(&gpio)) { |
| 316 | puts("STM32Programmer key pressed, "); |
| 317 | boot_mode = BOOT_STM32PROG; |
| 318 | } |
| 319 | dm_gpio_free(NULL, &gpio); |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | if (boot_mode != BOOT_NORMAL) { |
| 324 | puts("entering download mode...\n"); |
| 325 | clrsetbits_le32(TAMP_BOOT_CONTEXT, |
| 326 | TAMP_BOOT_FORCED_MASK, |
| 327 | boot_mode); |
| 328 | } |
| 329 | #endif |
| 330 | } |
| 331 | |
| 332 | #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG) |
| 333 | |
| 334 | #include <usb/dwc2_udc.h> |
| 335 | int g_dnl_board_usb_cable_connected(void) |
| 336 | { |
| 337 | struct udevice *dwc2_udc_otg; |
| 338 | int ret; |
| 339 | |
| 340 | ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 341 | DM_DRIVER_GET(dwc2_udc_otg), |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 342 | &dwc2_udc_otg); |
| 343 | if (!ret) |
| 344 | debug("dwc2_udc_otg init failed\n"); |
| 345 | |
| 346 | return dwc2_udc_B_session_valid(dwc2_udc_otg); |
| 347 | } |
| 348 | |
| 349 | #define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11 |
| 350 | #define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb |
| 351 | |
| 352 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
| 353 | { |
| 354 | if (!strcmp(name, "usb_dnl_dfu")) |
| 355 | put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct); |
| 356 | else if (!strcmp(name, "usb_dnl_fastboot")) |
| 357 | put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM, |
| 358 | &dev->idProduct); |
| 359 | else |
| 360 | put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | #endif /* CONFIG_USB_GADGET */ |
| 366 | |
| 367 | #ifdef CONFIG_LED |
| 368 | static int get_led(struct udevice **dev, char *led_string) |
| 369 | { |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 370 | const char *led_name; |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 371 | int ret; |
| 372 | |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 373 | led_name = ofnode_conf_read_str(led_string); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 374 | if (!led_name) { |
| 375 | pr_debug("%s: could not find %s config string\n", |
| 376 | __func__, led_string); |
| 377 | return -ENOENT; |
| 378 | } |
| 379 | ret = led_get_by_label(led_name, dev); |
| 380 | if (ret) { |
| 381 | debug("%s: get=%d\n", __func__, ret); |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int setup_led(enum led_state_t cmd) |
| 389 | { |
| 390 | struct udevice *dev; |
| 391 | int ret; |
| 392 | |
| 393 | ret = get_led(&dev, "u-boot,boot-led"); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | |
| 397 | ret = led_set_state(dev, cmd); |
| 398 | return ret; |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | static void __maybe_unused led_error_blink(u32 nb_blink) |
| 403 | { |
| 404 | #ifdef CONFIG_LED |
| 405 | int ret; |
| 406 | struct udevice *led; |
| 407 | u32 i; |
| 408 | #endif |
| 409 | |
| 410 | if (!nb_blink) |
| 411 | return; |
| 412 | |
| 413 | #ifdef CONFIG_LED |
| 414 | ret = get_led(&led, "u-boot,error-led"); |
| 415 | if (!ret) { |
| 416 | /* make u-boot,error-led blinking */ |
| 417 | /* if U32_MAX and 125ms interval, for 17.02 years */ |
| 418 | for (i = 0; i < 2 * nb_blink; i++) { |
| 419 | led_set_state(led, LEDST_TOGGLE); |
| 420 | mdelay(125); |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 421 | schedule(); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | #endif |
| 425 | |
| 426 | /* infinite: the boot process must be stopped */ |
| 427 | if (nb_blink == U32_MAX) |
| 428 | hang(); |
| 429 | } |
| 430 | |
| 431 | static void sysconf_init(void) |
| 432 | { |
Patrick Delaunay | f8fe21d | 2020-04-01 09:07:33 +0200 | [diff] [blame] | 433 | #ifndef CONFIG_TFABOOT |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 434 | u8 *syscfg; |
| 435 | #ifdef CONFIG_DM_REGULATOR |
| 436 | struct udevice *pwr_dev; |
| 437 | struct udevice *pwr_reg; |
| 438 | struct udevice *dev; |
| 439 | int ret; |
| 440 | u32 otp = 0; |
| 441 | #endif |
| 442 | u32 bootr; |
| 443 | |
| 444 | syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG); |
| 445 | |
| 446 | /* interconnect update : select master using the port 1 */ |
| 447 | /* LTDC = AXI_M9 */ |
| 448 | /* GPU = AXI_M8 */ |
| 449 | /* today information is hardcoded in U-Boot */ |
| 450 | writel(BIT(9), syscfg + SYSCFG_ICNR); |
| 451 | |
| 452 | /* disable Pull-Down for boot pin connected to VDD */ |
| 453 | bootr = readl(syscfg + SYSCFG_BOOTR); |
| 454 | bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT); |
| 455 | bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT; |
| 456 | writel(bootr, syscfg + SYSCFG_BOOTR); |
| 457 | |
| 458 | #ifdef CONFIG_DM_REGULATOR |
| 459 | /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI |
| 460 | * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection. |
| 461 | * The customer will have to disable this for low frequencies |
| 462 | * or if AFMUX is selected but the function not used, typically for |
| 463 | * TRACE. Otherwise, impact on power consumption. |
| 464 | * |
| 465 | * WARNING: |
| 466 | * enabling High Speed mode while VDD>2.7V |
| 467 | * with the OTP product_below_2v5 (OTP 18, BIT 13) |
| 468 | * erroneously set to 1 can damage the IC! |
| 469 | * => U-Boot set the register only if VDD < 2.7V (in DT) |
| 470 | * but this value need to be consistent with board design |
| 471 | */ |
| 472 | ret = uclass_get_device_by_driver(UCLASS_PMIC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 473 | DM_DRIVER_GET(stm32mp_pwr_pmic), |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 474 | &pwr_dev); |
| 475 | if (!ret) { |
| 476 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 477 | DM_DRIVER_GET(stm32mp_bsec), |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 478 | &dev); |
| 479 | if (ret) { |
| 480 | pr_err("Can't find stm32mp_bsec driver\n"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4); |
| 485 | if (ret > 0) |
| 486 | otp = otp & BIT(13); |
| 487 | |
| 488 | /* get VDD = vdd-supply */ |
| 489 | ret = device_get_supply_regulator(pwr_dev, "vdd-supply", |
| 490 | &pwr_reg); |
| 491 | |
| 492 | /* check if VDD is Low Voltage */ |
| 493 | if (!ret) { |
| 494 | if (regulator_get_value(pwr_reg) < 2700000) { |
| 495 | writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE | |
| 496 | SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI | |
| 497 | SYSCFG_IOCTRLSETR_HSLVEN_ETH | |
| 498 | SYSCFG_IOCTRLSETR_HSLVEN_SDMMC | |
| 499 | SYSCFG_IOCTRLSETR_HSLVEN_SPI, |
| 500 | syscfg + SYSCFG_IOCTRLSETR); |
| 501 | |
| 502 | if (!otp) |
| 503 | pr_err("product_below_2v5=0: HSLVEN protected by HW\n"); |
| 504 | } else { |
| 505 | if (otp) |
| 506 | pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n"); |
| 507 | } |
| 508 | } else { |
| 509 | debug("VDD unknown"); |
| 510 | } |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | /* activate automatic I/O compensation |
| 515 | * warning: need to ensure CSI enabled and ready in clock driver |
| 516 | */ |
| 517 | writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR); |
| 518 | |
| 519 | while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY)) |
| 520 | ; |
| 521 | clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL); |
| 522 | #endif |
| 523 | } |
| 524 | |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 525 | #ifdef CONFIG_DM_REGULATOR |
| 526 | #define STPMIC_NVM_BUCKS_VOUT_SHR 0xfc |
| 527 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 0 |
| 528 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8 1 |
| 529 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0 2 |
| 530 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3 3 |
| 531 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK GENMASK(1, 0) |
| 532 | #define STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(n) ((((n) - 1) & 3) * 2) |
| 533 | static int board_get_regulator_buck3_nvm_uv_av96(int *uv) |
| 534 | { |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 535 | struct udevice *dev; |
| 536 | u8 bucks_vout = 0; |
| 537 | const char *prop; |
| 538 | int len, ret; |
| 539 | |
| 540 | /* Check whether this is Avenger96 board. */ |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 541 | prop = ofnode_get_property(ofnode_root(), "compatible", &len); |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 542 | if (!prop || !len) |
| 543 | return -ENODEV; |
| 544 | |
Marek Vasut | 5278494 | 2022-09-26 18:50:00 +0200 | [diff] [blame] | 545 | if (!strstr(prop, "avenger96") && !strstr(prop, "dhcor-testbench")) |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 546 | return -EINVAL; |
| 547 | |
| 548 | /* Read out STPMIC1 NVM and determine default Buck3 voltage. */ |
| 549 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 550 | DM_DRIVER_GET(stpmic1_nvm), |
| 551 | &dev); |
| 552 | if (ret) |
| 553 | return ret; |
| 554 | |
| 555 | ret = misc_read(dev, STPMIC_NVM_BUCKS_VOUT_SHR, &bucks_vout, 1); |
| 556 | if (ret != 1) |
| 557 | return -EINVAL; |
| 558 | |
| 559 | bucks_vout >>= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_OFFSET(3); |
| 560 | bucks_vout &= STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_MASK; |
| 561 | |
Marek Vasut | 5278494 | 2022-09-26 18:50:00 +0200 | [diff] [blame] | 562 | if (strstr(prop, "avenger96")) { |
| 563 | /* |
| 564 | * Avenger96 board comes in multiple regulator configurations: |
| 565 | * - rev.100 or rev.200 have Buck3 preconfigured to |
| 566 | * 3V3 operation on boot and contains extra Enpirion |
| 567 | * EP53A8LQI DCDC converter which supplies the IO. |
| 568 | * Reduce Buck3 voltage to 2V9 to not waste power. |
| 569 | * - rev.200L have Buck3 preconfigured to 1V8 operation |
| 570 | * and have no Enpirion EP53A8LQI DCDC anymore, the |
| 571 | * IO is supplied from Buck3. |
| 572 | */ |
| 573 | if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3) |
| 574 | *uv = 2900000; |
| 575 | else |
| 576 | *uv = 1800000; |
| 577 | } else { |
| 578 | /* Testbench always respects Buck3 NVM settings */ |
| 579 | if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V3) |
| 580 | *uv = 3300000; |
| 581 | else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_3V0) |
| 582 | *uv = 3000000; |
| 583 | else if (bucks_vout == STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V8) |
| 584 | *uv = 1800000; |
| 585 | else /* STPMIC_NVM_BUCKS_VOUT_SHR_BUCK_1V2 */ |
| 586 | *uv = 1200000; |
| 587 | } |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static void board_init_regulator_av96(void) |
| 593 | { |
| 594 | struct udevice *rdev; |
| 595 | int ret, uv; |
| 596 | |
| 597 | ret = board_get_regulator_buck3_nvm_uv_av96(&uv); |
| 598 | if (ret) /* Not Avenger96 board. */ |
| 599 | return; |
| 600 | |
| 601 | ret = regulator_get_by_devname("buck3", &rdev); |
| 602 | if (ret) |
| 603 | return; |
| 604 | |
| 605 | /* Adjust Buck3 per preconfigured PMIC voltage from NVM. */ |
| 606 | regulator_set_value(rdev, uv); |
Marek Vasut | 69e8995 | 2022-09-23 03:31:22 +0200 | [diff] [blame] | 607 | regulator_set_enable(rdev, true); |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void board_init_regulator(void) |
| 611 | { |
| 612 | board_init_regulator_av96(); |
| 613 | |
| 614 | regulators_enable_boot_on(_DEBUG); |
| 615 | } |
| 616 | #else |
| 617 | static inline int board_get_regulator_buck3_nvm_uv_av96(int *uv) |
| 618 | { |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
| 622 | static inline void board_init_regulator(void) {} |
| 623 | #endif |
| 624 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 625 | /* board dependent setup after realloc */ |
| 626 | int board_init(void) |
| 627 | { |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 628 | board_key_check(); |
| 629 | |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 630 | board_init_regulator(); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 631 | |
| 632 | sysconf_init(); |
| 633 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int board_late_init(void) |
| 638 | { |
| 639 | char *boot_device; |
| 640 | #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG |
| 641 | const void *fdt_compat; |
| 642 | int fdt_compat_len; |
| 643 | |
Patrick Delaunay | 280949c | 2022-06-06 16:04:15 +0200 | [diff] [blame] | 644 | fdt_compat = ofnode_get_property(ofnode_root(), "compatible", |
| 645 | &fdt_compat_len); |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 646 | if (fdt_compat && fdt_compat_len) { |
| 647 | if (strncmp(fdt_compat, "st,", 3) != 0) |
| 648 | env_set("board_name", fdt_compat); |
| 649 | else |
| 650 | env_set("board_name", fdt_compat + 3); |
| 651 | } |
| 652 | #endif |
| 653 | |
| 654 | /* Check the boot-source to disable bootdelay */ |
| 655 | boot_device = env_get("boot_device"); |
| 656 | if (!strcmp(boot_device, "serial") || !strcmp(boot_device, "usb")) |
| 657 | env_set("bootdelay", "0"); |
| 658 | |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 659 | #ifdef CONFIG_BOARD_EARLY_INIT_F |
| 660 | env_set_ulong("dh_som_rev", somcode); |
| 661 | env_set_ulong("dh_board_rev", brdcode); |
Marek Vasut | 39221b5 | 2020-04-22 13:18:14 +0200 | [diff] [blame] | 662 | env_set_ulong("dh_ddr3_code", ddr3code); |
Marek Vasut | 47b98ba | 2020-04-22 13:18:11 +0200 | [diff] [blame] | 663 | #endif |
| 664 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | void board_quiesce_devices(void) |
| 669 | { |
| 670 | #ifdef CONFIG_LED |
| 671 | setup_led(LEDST_OFF); |
| 672 | #endif |
| 673 | } |
| 674 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 675 | #if defined(CONFIG_OF_BOARD_SETUP) |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 676 | int ft_board_setup(void *blob, struct bd_info *bd) |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 677 | { |
Marek Vasut | 7f809fe | 2022-05-11 23:09:33 +0200 | [diff] [blame] | 678 | const char *buck3path = "/soc/i2c@5c002000/stpmic@33/regulators/buck3"; |
| 679 | int buck3off, ret, uv; |
| 680 | |
| 681 | ret = board_get_regulator_buck3_nvm_uv_av96(&uv); |
| 682 | if (ret) /* Not Avenger96 board, do not patch Buck3 in DT. */ |
| 683 | return 0; |
| 684 | |
| 685 | buck3off = fdt_path_offset(blob, buck3path); |
| 686 | if (buck3off < 0) /* No Buck3 regulator found. */ |
| 687 | return 0; |
| 688 | |
| 689 | ret = fdt_setprop_u32(blob, buck3off, "regulator-min-microvolt", uv); |
| 690 | if (ret < 0) |
| 691 | return ret; |
| 692 | |
| 693 | ret = fdt_setprop_u32(blob, buck3off, "regulator-max-microvolt", uv); |
| 694 | if (ret < 0) |
| 695 | return ret; |
| 696 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | #endif |
| 700 | |
Marek Vasut | 5ff0529 | 2020-01-24 18:39:16 +0100 | [diff] [blame] | 701 | static void board_copro_image_process(ulong fw_image, size_t fw_size) |
| 702 | { |
| 703 | int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */ |
| 704 | |
| 705 | if (!rproc_is_initialized()) |
| 706 | if (rproc_init()) { |
| 707 | printf("Remote Processor %d initialization failed\n", |
| 708 | id); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | ret = rproc_load(id, fw_image, fw_size); |
| 713 | printf("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n", |
| 714 | id, fw_image, fw_size, ret ? " Failed!" : " Success!"); |
| 715 | |
| 716 | if (!ret) { |
| 717 | rproc_start(id); |
| 718 | env_set("copro_state", "booted"); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process); |