blob: 07b1a63db7dc263446765697bf9c5e9c2514a9bc [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01004 */
Patrick Delaunayf7b2a842020-11-06 19:01:59 +01005
6#define LOG_CATEGORY LOGC_BOARD
7
Patrice Chotard879cde52019-02-12 16:50:40 +01008#include <common.h>
9#include <adc.h>
Patrick Delaunay500401f2019-06-21 15:26:40 +020010#include <bootm.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020011#include <clk.h>
Patrick Delaunay266bf102019-07-30 19:16:44 +020012#include <config.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020013#include <dm.h>
Simon Glassdb229612019-08-01 09:46:42 -060014#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060015#include <env_internal.h>
Patrick Delaunay028fddd2021-11-15 16:32:23 +010016#include <fdt_simplefb.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060017#include <fdt_support.h>
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +010018#include <g_dnl.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020019#include <generic-phy.h>
Simon Glassf11478f2019-12-28 10:45:07 -070020#include <hang.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010021#include <i2c.h>
Simon Glassa7b51302019-11-14 12:57:46 -070022#include <init.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010023#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <malloc.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010026#include <misc.h>
Patrick Delaunayde98cbf2019-07-02 13:26:07 +020027#include <mtd_node.h>
Simon Glass274e0b02020-05-10 11:39:56 -060028#include <net.h>
Patrick Delaunaybff66f92019-08-01 11:29:03 +020029#include <netdev.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020030#include <phy.h>
Patrick Delaunayc17d7252019-08-02 15:07:20 +020031#include <remoteproc.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020032#include <reset.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010033#include <syscon.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010034#include <usb.h>
Patrick Delaunayae0931d02019-07-30 19:16:39 +020035#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060036#include <asm/global_data.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020037#include <asm/io.h>
Patrick Delaunayf2a7b872019-02-27 17:01:18 +010038#include <asm/gpio.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010039#include <asm/arch/stm32.h>
Patrice Chotarddad97bf2019-05-02 18:36:01 +020040#include <asm/arch/sys_proto.h>
Simon Glass0034d962021-08-07 07:24:01 -060041#include <dm/ofnode.h>
Patrick Delaunayde98cbf2019-07-02 13:26:07 +020042#include <jffs2/load_kernel.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060043#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060044#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070045#include <linux/err.h>
Patrick Delaunay181298e2020-04-22 14:29:16 +020046#include <linux/iopoll.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020047#include <power/regulator.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010048#include <usb/dwc2_udc.h>
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010049
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +020050#include "../../st/common/stusb160x.h"
51
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010052/* SYSCFG registers */
53#define SYSCFG_BOOTR 0x00
54#define SYSCFG_PMCSETR 0x04
55#define SYSCFG_IOCTRLSETR 0x18
56#define SYSCFG_ICNR 0x1C
57#define SYSCFG_CMPCR 0x20
58#define SYSCFG_CMPENSETR 0x24
59#define SYSCFG_PMCCLRR 0x44
60
61#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
62#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
63
64#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
65#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
66#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
67#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
68#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
69
70#define SYSCFG_CMPCR_SW_CTRL BIT(1)
71#define SYSCFG_CMPCR_READY BIT(8)
72
73#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
74
75#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
76#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
77
78#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
79
80#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Christophe Roullier69ac3f52019-05-17 15:08:43 +020081#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
82#define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
83#define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010084
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010085/*
86 * Get a global data pointer
87 */
88DECLARE_GLOBAL_DATA_PTR;
89
Patrice Chotardcded32f2019-04-30 18:09:38 +020090#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard879cde52019-02-12 16:50:40 +010091#define USB_WARNING_LOW_THRESHOLD_UV 660000
92#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotardcded32f2019-04-30 18:09:38 +020093#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard879cde52019-02-12 16:50:40 +010094
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020095int board_early_init_f(void)
96{
97 /* nothing to do, only used in SPL */
98 return 0;
99}
100
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100101int checkboard(void)
102{
103 int ret;
104 char *mode;
105 u32 otp;
106 struct udevice *dev;
107 const char *fdt_compat;
108 int fdt_compat_len;
109
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200110 if (IS_ENABLED(CONFIG_TFABOOT)) {
111 if (IS_ENABLED(CONFIG_STM32MP15x_STM32IMAGE))
112 mode = "trusted - stm32image";
113 else
114 mode = "trusted";
115 } else {
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100116 mode = "basic";
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200117 }
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100118
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100119 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
120 &fdt_compat_len);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100121
122 log_info("Board: stm32mp1 in %s mode (%s)\n", mode,
123 fdt_compat && fdt_compat_len ? fdt_compat : "");
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100124
Patrick Delaunay52163d92020-03-24 09:05:00 +0100125 /* display the STMicroelectronics board identification */
Patrick Delaunay0885c232020-02-12 19:37:42 +0100126 if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
127 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700128 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay0885c232020-02-12 19:37:42 +0100129 &dev);
130 if (!ret)
131 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
132 &otp, sizeof(otp));
133 if (ret > 0 && otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100134 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
135 otp >> 16,
136 (otp >> 12) & 0xF,
137 (otp >> 4) & 0xF,
138 ((otp >> 8) & 0xF) - 1 + 'A',
139 otp & 0xF);
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100140 }
141
142 return 0;
143}
144
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100145static void board_key_check(void)
146{
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100147 ofnode node;
148 struct gpio_desc gpio;
149 enum forced_boot_mode boot_mode = BOOT_NORMAL;
150
Patrick Delaunayf6604852020-07-31 16:31:42 +0200151 if (!IS_ENABLED(CONFIG_FASTBOOT) && !IS_ENABLED(CONFIG_CMD_STM32PROG))
152 return;
153
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100154 node = ofnode_path("/config");
155 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100156 log_debug("no /config node?\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100157 return;
158 }
Patrick Delaunayf6604852020-07-31 16:31:42 +0200159 if (IS_ENABLED(CONFIG_FASTBOOT)) {
160 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
161 &gpio, GPIOD_IS_IN)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100162 log_debug("could not find a /config/st,fastboot-gpios\n");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200163 } else {
Patrick Delaunay466d3af2021-07-09 09:53:37 +0200164 udelay(20);
Patrick Delaunayf6604852020-07-31 16:31:42 +0200165 if (dm_gpio_get_value(&gpio)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100166 log_notice("Fastboot key pressed, ");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200167 boot_mode = BOOT_FASTBOOT;
168 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100169
Patrick Delaunayf6604852020-07-31 16:31:42 +0200170 dm_gpio_free(NULL, &gpio);
171 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100172 }
Patrick Delaunayf6604852020-07-31 16:31:42 +0200173 if (IS_ENABLED(CONFIG_CMD_STM32PROG)) {
174 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
175 &gpio, GPIOD_IS_IN)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100176 log_debug("could not find a /config/st,stm32prog-gpios\n");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200177 } else {
Patrick Delaunay466d3af2021-07-09 09:53:37 +0200178 udelay(20);
Patrick Delaunayf6604852020-07-31 16:31:42 +0200179 if (dm_gpio_get_value(&gpio)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100180 log_notice("STM32Programmer key pressed, ");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200181 boot_mode = BOOT_STM32PROG;
182 }
183 dm_gpio_free(NULL, &gpio);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100184 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100185 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100186 if (boot_mode != BOOT_NORMAL) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100187 log_notice("entering download mode...\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100188 clrsetbits_le32(TAMP_BOOT_CONTEXT,
189 TAMP_BOOT_FORCED_MASK,
190 boot_mode);
191 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100192}
193
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100194int g_dnl_board_usb_cable_connected(void)
Patrice Chotard204079b2018-08-10 17:12:14 +0200195{
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100196 struct udevice *dwc2_udc_otg;
Patrice Chotard204079b2018-08-10 17:12:14 +0200197 int ret;
198
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200199 if (!IS_ENABLED(CONFIG_USB_GADGET_DWC2_OTG))
200 return -ENODEV;
201
Patrice Chotardaef7c182022-04-22 09:39:18 +0200202 /*
203 * In case of USB boot device is detected, consider USB cable is
204 * connected
205 */
206 if ((get_bootmode() & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_USB)
207 return true;
208
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +0200209 /* if typec stusb160x is present, means DK1 or DK2 board */
210 ret = stusb160x_cable_connected();
211 if (ret >= 0)
212 return ret;
Patrick Delaunay7f3384d2019-03-29 15:42:24 +0100213
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100214 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700215 DM_DRIVER_GET(dwc2_udc_otg),
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100216 &dwc2_udc_otg);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100217 if (ret) {
218 log_debug("dwc2_udc_otg init failed\n");
219 return ret;
220 }
Patrice Chotard204079b2018-08-10 17:12:14 +0200221
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100222 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard879cde52019-02-12 16:50:40 +0100223}
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200224
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200225#ifdef CONFIG_USB_GADGET_DOWNLOAD
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200226#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
227#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
228
229int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
230{
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200231 if (IS_ENABLED(CONFIG_DFU_OVER_USB) &&
232 !strcmp(name, "usb_dnl_dfu"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200233 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200234 else if (IS_ENABLED(CONFIG_FASTBOOT) &&
235 !strcmp(name, "usb_dnl_fastboot"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200236 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
237 &dev->idProduct);
238 else
239 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
240
241 return 0;
242}
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200243#endif /* CONFIG_USB_GADGET_DOWNLOAD */
Patrice Chotard879cde52019-02-12 16:50:40 +0100244
245static int get_led(struct udevice **dev, char *led_string)
246{
Simon Glass0034d962021-08-07 07:24:01 -0600247 const char *led_name;
Patrice Chotard879cde52019-02-12 16:50:40 +0100248 int ret;
249
Simon Glass0034d962021-08-07 07:24:01 -0600250 led_name = ofnode_conf_read_str(led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100251 if (!led_name) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100252 log_debug("could not find %s config string\n", led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100253 return -ENOENT;
254 }
255 ret = led_get_by_label(led_name, dev);
256 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100257 log_debug("get=%d\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100258 return ret;
259 }
260
261 return 0;
262}
263
264static int setup_led(enum led_state_t cmd)
265{
266 struct udevice *dev;
267 int ret;
268
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200269 if (!CONFIG_IS_ENABLED(LED))
270 return 0;
271
Patrice Chotard879cde52019-02-12 16:50:40 +0100272 ret = get_led(&dev, "u-boot,boot-led");
273 if (ret)
274 return ret;
275
276 ret = led_set_state(dev, cmd);
Patrice Chotard204079b2018-08-10 17:12:14 +0200277 return ret;
278}
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200279
280static void __maybe_unused led_error_blink(u32 nb_blink)
281{
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200282 int ret;
283 struct udevice *led;
284 u32 i;
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200285
286 if (!nb_blink)
287 return;
288
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200289 if (CONFIG_IS_ENABLED(LED)) {
290 ret = get_led(&led, "u-boot,error-led");
291 if (!ret) {
292 /* make u-boot,error-led blinking */
293 /* if U32_MAX and 125ms interval, for 17.02 years */
294 for (i = 0; i < 2 * nb_blink; i++) {
295 led_set_state(led, LEDST_TOGGLE);
296 mdelay(125);
297 WATCHDOG_RESET();
298 }
299 led_set_state(led, LEDST_ON);
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200300 }
301 }
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200302
303 /* infinite: the boot process must be stopped */
304 if (nb_blink == U32_MAX)
305 hang();
306}
Patrice Chotard204079b2018-08-10 17:12:14 +0200307
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200308static int adc_measurement(ofnode node, int adc_count, int *min_uV, int *max_uV)
Patrice Chotard879cde52019-02-12 16:50:40 +0100309{
310 struct ofnode_phandle_args adc_args;
311 struct udevice *adc;
Patrice Chotard879cde52019-02-12 16:50:40 +0100312 unsigned int raw;
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200313 int ret, uV;
314 int i;
Patrice Chotard879cde52019-02-12 16:50:40 +0100315
316 for (i = 0; i < adc_count; i++) {
317 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
318 "#io-channel-cells", 0, i,
319 &adc_args)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100320 log_debug("can't find /config/st,adc_usb_pd\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100321 return 0;
322 }
323
324 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
325 &adc);
326
327 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100328 log_err("Can't get adc device(%d)\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100329 return ret;
330 }
331
332 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
333 &raw);
334 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100335 log_err("single shot failed for %s[%d]!\n",
336 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100337 return ret;
338 }
339 /* Convert to uV */
340 if (!adc_raw_to_uV(adc, raw, &uV)) {
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200341 if (uV > *max_uV)
342 *max_uV = uV;
343 if (uV < *min_uV)
344 *min_uV = uV;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100345 log_debug("%s[%02d] = %u, %d uV\n",
346 adc->name, adc_args.args[0], raw, uV);
Patrice Chotard879cde52019-02-12 16:50:40 +0100347 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100348 log_err("Can't get uV value for %s[%d]\n",
349 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100350 }
351 }
352
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200353 return 0;
354}
355
356static int board_check_usb_power(void)
357{
358 ofnode node;
359 int max_uV = 0;
360 int min_uV = USB_START_HIGH_THRESHOLD_UV;
361 int adc_count, ret;
362 u32 nb_blink;
363 u8 i;
364
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200365 if (!IS_ENABLED(CONFIG_ADC))
366 return -ENODEV;
367
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200368 node = ofnode_path("/config");
369 if (!ofnode_valid(node)) {
370 log_debug("no /config node?\n");
371 return -ENOENT;
372 }
373
Patrice Chotard879cde52019-02-12 16:50:40 +0100374 /*
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200375 * Retrieve the ADC channels devices and get measurement
376 * for each of them
Patrice Chotard879cde52019-02-12 16:50:40 +0100377 */
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200378 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
379 "#io-channel-cells", 0);
380 if (adc_count < 0) {
381 if (adc_count == -ENOENT)
382 return 0;
Patrice Chotard879cde52019-02-12 16:50:40 +0100383
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200384 log_err("Can't find adc channel (%d)\n", adc_count);
385
386 return adc_count;
387 }
Patrice Chotardcded32f2019-04-30 18:09:38 +0200388
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200389 /* perform maximum of 2 ADC measurements to detect power supply current */
390 for (i = 0; i < 2; i++) {
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200391 ret = adc_measurement(node, adc_count, &min_uV, &max_uV);
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200392 if (ret)
393 return ret;
394
395 /*
396 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
397 * board is plugged on an USB-C 3A power supply and boot process can
398 * continue.
399 */
400 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
401 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
402 min_uV <= USB_LOW_THRESHOLD_UV)
403 return 0;
404
405 if (i == 0) {
406 log_err("Previous ADC measurements was not the one expected, retry in 20ms\n");
407 mdelay(20); /* equal to max tPDDebounce duration (min 10ms - max 20ms) */
408 }
409 }
410
411 log_notice("****************************************************\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200412 /*
413 * If highest and lowest value are either both below
414 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
415 * means USB TYPE-C is in unattached mode, this is an issue, make
416 * u-boot,error-led blinking and stop boot process.
417 */
418 if ((max_uV > USB_LOW_THRESHOLD_UV &&
419 min_uV > USB_LOW_THRESHOLD_UV) ||
420 (max_uV <= USB_LOW_THRESHOLD_UV &&
421 min_uV <= USB_LOW_THRESHOLD_UV)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100422 log_notice("* ERROR USB TYPE-C connection in unattached mode *\n");
423 log_notice("* Check that USB TYPE-C cable is correctly plugged *\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200424 /* with 125ms interval, led will blink for 17.02 years ....*/
425 nb_blink = U32_MAX;
426 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100427
Patrice Chotardcded32f2019-04-30 18:09:38 +0200428 if (max_uV > USB_LOW_THRESHOLD_UV &&
429 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
430 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100431 log_notice("* WARNING 500mA power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100432 nb_blink = 2;
Patrice Chotardcded32f2019-04-30 18:09:38 +0200433 }
434
435 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
436 max_uV <= USB_START_LOW_THRESHOLD_UV &&
437 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100438 log_notice("* WARNING 1.5A power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100439 nb_blink = 3;
440 }
441
Patrice Chotardcded32f2019-04-30 18:09:38 +0200442 /*
443 * If highest value is above 2.15 Volts that means that the USB TypeC
444 * supplies more than 3 Amp, this is not compliant with TypeC specification
445 */
446 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100447 log_notice("* USB TYPE-C charger not compliant with *\n");
448 log_notice("* specification *\n");
449 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200450 /* with 125ms interval, led will blink for 17.02 years ....*/
451 nb_blink = U32_MAX;
452 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100453 log_notice("* Current too low, use a 3A power supply! *\n");
454 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200455 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100456
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200457 led_error_blink(nb_blink);
Patrice Chotard879cde52019-02-12 16:50:40 +0100458
459 return 0;
460}
461
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100462static void sysconf_init(void)
463{
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100464 u8 *syscfg;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100465 struct udevice *pwr_dev;
466 struct udevice *pwr_reg;
467 struct udevice *dev;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100468 u32 otp = 0;
Patrick Delaunay181298e2020-04-22 14:29:16 +0200469 int ret;
470 u32 bootr, val;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100471
472 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
473
474 /* interconnect update : select master using the port 1 */
475 /* LTDC = AXI_M9 */
476 /* GPU = AXI_M8 */
477 /* today information is hardcoded in U-Boot */
478 writel(BIT(9), syscfg + SYSCFG_ICNR);
479
480 /* disable Pull-Down for boot pin connected to VDD */
481 bootr = readl(syscfg + SYSCFG_BOOTR);
482 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
483 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
484 writel(bootr, syscfg + SYSCFG_BOOTR);
485
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100486 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
487 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
488 * The customer will have to disable this for low frequencies
489 * or if AFMUX is selected but the function not used, typically for
490 * TRACE. Otherwise, impact on power consumption.
491 *
492 * WARNING:
493 * enabling High Speed mode while VDD>2.7V
494 * with the OTP product_below_2v5 (OTP 18, BIT 13)
495 * erroneously set to 1 can damage the IC!
496 * => U-Boot set the register only if VDD < 2.7V (in DT)
497 * but this value need to be consistent with board design
498 */
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200499 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700500 DM_DRIVER_GET(stm32mp_pwr_pmic),
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200501 &pwr_dev);
Patrick Delaunay4b2bfd62020-07-31 16:31:45 +0200502 if (!ret && IS_ENABLED(CONFIG_DM_REGULATOR)) {
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100503 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700504 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100505 &dev);
506 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100507 log_err("Can't find stm32mp_bsec driver\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100508 return;
509 }
510
511 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
Patrick Delaunayceb82e32019-08-02 13:08:06 +0200512 if (ret > 0)
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100513 otp = otp & BIT(13);
514
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200515 /* get VDD = vdd-supply */
516 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100517 &pwr_reg);
518
519 /* check if VDD is Low Voltage */
520 if (!ret) {
521 if (regulator_get_value(pwr_reg) < 2700000) {
522 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
523 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
524 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
525 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
526 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
527 syscfg + SYSCFG_IOCTRLSETR);
528
529 if (!otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100530 log_err("product_below_2v5=0: HSLVEN protected by HW\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100531 } else {
532 if (otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100533 log_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100534 }
535 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100536 log_debug("VDD unknown");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100537 }
538 }
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100539
540 /* activate automatic I/O compensation
541 * warning: need to ensure CSI enabled and ready in clock driver
542 */
543 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
544
Patrick Delaunay181298e2020-04-22 14:29:16 +0200545 /* poll until ready (1s timeout) */
546 ret = readl_poll_timeout(syscfg + SYSCFG_CMPCR, val,
547 val & SYSCFG_CMPCR_READY,
548 1000000);
549 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100550 log_err("SYSCFG: I/O compensation failed, timeout.\n");
Patrick Delaunay181298e2020-04-22 14:29:16 +0200551 led_error_blink(10);
552 }
553
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100554 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100555}
556
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200557/* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
558static int dk2_i2c1_fix(void)
559{
560 ofnode node;
561 struct gpio_desc hdmi, audio;
562 int ret = 0;
563
Patrick Delaunayc75bc6a2020-07-31 16:31:47 +0200564 if (!IS_ENABLED(CONFIG_DM_REGULATOR))
565 return -ENODEV;
566
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200567 node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
568 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100569 log_debug("no hdmi-transmitter@39 ?\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200570 return -ENOENT;
571 }
572
573 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
574 &hdmi, GPIOD_IS_OUT)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100575 log_debug("could not find reset-gpios\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200576 return -ENOENT;
577 }
578
579 node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
580 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100581 log_debug("no cs42l51@4a ?\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200582 return -ENOENT;
583 }
584
585 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
586 &audio, GPIOD_IS_OUT)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100587 log_debug("could not find reset-gpios\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200588 return -ENOENT;
589 }
590
591 /* before power up, insure that HDMI and AUDIO IC is under reset */
592 ret = dm_gpio_set_value(&hdmi, 1);
593 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100594 log_err("can't set_value for hdmi_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200595 goto error;
596 }
597 ret = dm_gpio_set_value(&audio, 1);
598 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100599 log_err("can't set_value for audio_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200600 goto error;
601 }
602
603 /* power-up audio IC */
604 regulator_autoset_by_name("v1v8_audio", NULL);
605
606 /* power-up HDMI IC */
607 regulator_autoset_by_name("v1v2_hdmi", NULL);
608 regulator_autoset_by_name("v3v3_hdmi", NULL);
609
610error:
611 return ret;
612}
613
614static bool board_is_dk2(void)
615{
Patrick Delaunay310aa8a2020-01-13 15:17:42 +0100616 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15x) &&
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200617 of_machine_is_compatible("st,stm32mp157c-dk2"))
618 return true;
619
620 return false;
621}
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200622
Patrick Delaunay486ae962020-04-22 14:29:13 +0200623static bool board_is_ev1(void)
624{
625 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15x) &&
626 (of_machine_is_compatible("st,stm32mp157a-ev1") ||
627 of_machine_is_compatible("st,stm32mp157c-ev1") ||
628 of_machine_is_compatible("st,stm32mp157d-ev1") ||
629 of_machine_is_compatible("st,stm32mp157f-ev1")))
630 return true;
631
632 return false;
633}
634
635/* touchscreen driver: only used for pincontrol configuration */
636static const struct udevice_id goodix_ids[] = {
637 { .compatible = "goodix,gt9147", },
638 { }
639};
640
641U_BOOT_DRIVER(goodix) = {
642 .name = "goodix",
643 .id = UCLASS_NOP,
644 .of_match = goodix_ids,
645};
646
647static void board_ev1_init(void)
648{
649 struct udevice *dev;
650
651 /* configure IRQ line on EV1 for touchscreen before LCD reset */
Simon Glass65130cd2020-12-28 20:34:56 -0700652 uclass_get_device_by_driver(UCLASS_NOP, DM_DRIVER_GET(goodix), &dev);
Patrick Delaunay486ae962020-04-22 14:29:13 +0200653}
654
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100655/* board dependent setup after realloc */
656int board_init(void)
657{
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100658 board_key_check();
659
Patrick Delaunay486ae962020-04-22 14:29:13 +0200660 if (board_is_ev1())
661 board_ev1_init();
662
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200663 if (board_is_dk2())
664 dk2_i2c1_fix();
665
Patrick Delaunayc75bc6a2020-07-31 16:31:47 +0200666 if (IS_ENABLED(CONFIG_DM_REGULATOR))
667 regulators_enable_boot_on(_DEBUG);
Patrick Delaunay6519e442019-07-05 17:20:09 +0200668
Patrick Delaunay72a57622021-10-11 09:52:50 +0200669 /*
670 * sysconf initialisation done only when U-Boot is running in secure
671 * done in TF-A for TFABOOT.
672 */
673 if (IS_ENABLED(CONFIG_ARMV7_NONSEC))
Patrick Delaunay4b2bfd62020-07-31 16:31:45 +0200674 sysconf_init();
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100675
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200676 setup_led(LEDST_ON);
677
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100678 return 0;
679}
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100680
681int board_late_init(void)
682{
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100683 const void *fdt_compat;
684 int fdt_compat_len;
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200685 int ret;
686 u32 otp;
687 struct udevice *dev;
688 char buf[10];
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200689 char dtb_name[256];
690 int buf_len;
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100691
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200692 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
693 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
694 &fdt_compat_len);
695 if (fdt_compat && fdt_compat_len) {
696 if (strncmp(fdt_compat, "st,", 3) != 0) {
697 env_set("board_name", fdt_compat);
698 } else {
699 env_set("board_name", fdt_compat + 3);
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200700
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200701 buf_len = sizeof(dtb_name);
702 strncpy(dtb_name, fdt_compat + 3, buf_len);
703 buf_len -= strlen(fdt_compat + 3);
704 strncat(dtb_name, ".dtb", buf_len);
705 env_set("fdtfile", dtb_name);
706 }
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200707 }
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200708 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700709 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200710 &dev);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200711
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200712 if (!ret)
713 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
714 &otp, sizeof(otp));
715 if (ret > 0 && otp) {
716 snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
717 env_set("board_id", buf);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200718
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200719 snprintf(buf, sizeof(buf), "0x%04x",
720 ((otp >> 8) & 0xF) - 1 + 0xA);
721 env_set("board_rev", buf);
722 }
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200723 }
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100724
Patrice Chotard879cde52019-02-12 16:50:40 +0100725 /* for DK1/DK2 boards */
726 board_check_usb_power();
727
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100728 return 0;
729}
Patrice Chotard879cde52019-02-12 16:50:40 +0100730
731void board_quiesce_devices(void)
732{
733 setup_led(LEDST_OFF);
734}
Patrice Chotard41443cf2019-05-02 18:07:14 +0200735
Patrick Delaunaybff66f92019-08-01 11:29:03 +0200736/* eth init function : weak called in eqos driver */
737int board_interface_eth_init(struct udevice *dev,
738 phy_interface_t interface_type)
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200739{
740 u8 *syscfg;
741 u32 value;
Patrick Delaunaybff66f92019-08-01 11:29:03 +0200742 bool eth_clk_sel_reg = false;
743 bool eth_ref_clk_sel_reg = false;
744
745 /* Gigabit Ethernet 125MHz clock selection. */
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200746 eth_clk_sel_reg = dev_read_bool(dev, "st,eth-clk-sel");
Patrick Delaunaybff66f92019-08-01 11:29:03 +0200747
748 /* Ethernet 50Mhz RMII clock selection */
749 eth_ref_clk_sel_reg =
Patrick Delaunay00cf4a82021-06-04 18:25:55 +0200750 dev_read_bool(dev, "st,eth-ref-clk-sel");
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200751
752 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
753
754 if (!syscfg)
755 return -ENODEV;
756
757 switch (interface_type) {
758 case PHY_INTERFACE_MODE_MII:
759 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
760 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100761 log_debug("PHY_INTERFACE_MODE_MII\n");
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200762 break;
763 case PHY_INTERFACE_MODE_GMII:
764 if (eth_clk_sel_reg)
765 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
766 SYSCFG_PMCSETR_ETH_CLK_SEL;
767 else
768 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100769 log_debug("PHY_INTERFACE_MODE_GMII\n");
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200770 break;
771 case PHY_INTERFACE_MODE_RMII:
772 if (eth_ref_clk_sel_reg)
773 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
774 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
775 else
776 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100777 log_debug("PHY_INTERFACE_MODE_RMII\n");
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200778 break;
779 case PHY_INTERFACE_MODE_RGMII:
780 case PHY_INTERFACE_MODE_RGMII_ID:
781 case PHY_INTERFACE_MODE_RGMII_RXID:
782 case PHY_INTERFACE_MODE_RGMII_TXID:
783 if (eth_clk_sel_reg)
784 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
785 SYSCFG_PMCSETR_ETH_CLK_SEL;
786 else
787 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100788 log_debug("PHY_INTERFACE_MODE_RGMII\n");
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200789 break;
790 default:
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100791 log_debug("Do not manage %d interface\n",
792 interface_type);
Christophe Roullier69ac3f52019-05-17 15:08:43 +0200793 /* Do not manage others interfaces */
794 return -EINVAL;
795 }
796
797 /* clear and set ETH configuration bits */
798 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
799 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
800 syscfg + SYSCFG_PMCCLRR);
801 writel(value, syscfg + SYSCFG_PMCSETR);
802
803 return 0;
804}
805
Patrice Chotard34320372019-05-02 18:28:05 +0200806enum env_location env_get_location(enum env_operation op, int prio)
807{
808 u32 bootmode = get_bootmode();
809
810 if (prio)
811 return ENVL_UNKNOWN;
812
813 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
Patrick Delaunay455b0652020-06-15 11:18:22 +0200814 case BOOT_FLASH_SD:
815 case BOOT_FLASH_EMMC:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200816 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
817 return ENVL_MMC;
818 else if (CONFIG_IS_ENABLED(ENV_IS_IN_EXT4))
819 return ENVL_EXT4;
820 else
821 return ENVL_NOWHERE;
822
Patrice Chotard34320372019-05-02 18:28:05 +0200823 case BOOT_FLASH_NAND:
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100824 case BOOT_FLASH_SPINAND:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200825 if (CONFIG_IS_ENABLED(ENV_IS_IN_UBI))
826 return ENVL_UBI;
827 else
828 return ENVL_NOWHERE;
829
Patrice Chotard2c461ec2019-05-09 14:25:36 +0200830 case BOOT_FLASH_NOR:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200831 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
832 return ENVL_SPI_FLASH;
833 else
834 return ENVL_NOWHERE;
835
Patrice Chotard34320372019-05-02 18:28:05 +0200836 default:
837 return ENVL_NOWHERE;
838 }
839}
840
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200841const char *env_ext4_get_intf(void)
842{
843 u32 bootmode = get_bootmode();
844
845 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
846 case BOOT_FLASH_SD:
847 case BOOT_FLASH_EMMC:
848 return "mmc";
849 default:
850 return "";
851 }
852}
853
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200854int mmc_get_boot(void)
855{
856 struct udevice *dev;
857 u32 boot_mode = get_bootmode();
858 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
859 char cmd[20];
860 const u32 sdmmc_addr[] = {
861 STM32_SDMMC1_BASE,
862 STM32_SDMMC2_BASE,
863 STM32_SDMMC3_BASE
864 };
865
866 if (instance > ARRAY_SIZE(sdmmc_addr))
867 return 0;
868
869 /* search associated sdmmc node in devicetree */
870 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
871 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
872 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
873 return 0;
874 }
875
876 return dev_seq(dev);
877};
878
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200879const char *env_ext4_get_dev_part(void)
880{
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000881 static char *const env_dev_part =
882#ifdef CONFIG_ENV_EXT4_DEVICE_AND_PART
883 CONFIG_ENV_EXT4_DEVICE_AND_PART;
884#else
885 "";
886#endif
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200887 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000888
889 if (strlen(env_dev_part) > 0)
890 return env_dev_part;
891
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200892 return dev_part[mmc_get_boot()];
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200893}
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000894
Patrick Delaunay455b0652020-06-15 11:18:22 +0200895int mmc_get_env_dev(void)
896{
Patrick Delaunay520e2992022-01-11 16:37:21 +0100897 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
898
899 if (mmc_env_dev >= 0)
900 return mmc_env_dev;
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100901
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100902 /* use boot instance to select the correct mmc device identifier */
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200903 return mmc_get_boot();
Patrick Delaunay455b0652020-06-15 11:18:22 +0200904}
Patrick Delaunay455b0652020-06-15 11:18:22 +0200905
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200906#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900907int ft_board_setup(void *blob, struct bd_info *bd)
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200908{
Patrick Delaunaya8a67ed2020-07-29 13:24:52 +0200909 static const struct node_info nodes[] = {
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200910 { "st,stm32f469-qspi", MTD_DEV_TYPE_NOR, },
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100911 { "st,stm32f469-qspi", MTD_DEV_TYPE_SPINAND},
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200912 { "st,stm32mp15-fmc2", MTD_DEV_TYPE_NAND, },
Christophe Kerello15359e42020-07-31 09:53:42 +0200913 { "st,stm32mp1-fmc2-nfc", MTD_DEV_TYPE_NAND, },
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200914 };
Patrick Delaunay1b5cd0b2020-10-15 14:52:30 +0200915 char *boot_device;
Patrick Delaunay9d96e902020-07-31 16:31:50 +0200916
Patrick Delaunay1b5cd0b2020-10-15 14:52:30 +0200917 /* Check the boot-source and don't update MTD for serial or usb boot */
918 boot_device = env_get("boot_device");
919 if (!boot_device ||
920 (strcmp(boot_device, "serial") && strcmp(boot_device, "usb")))
921 if (IS_ENABLED(CONFIG_FDT_FIXUP_PARTITIONS))
922 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200923
Patrick Delaunay028fddd2021-11-15 16:32:23 +0100924 if (CONFIG_IS_ENABLED(FDT_SIMPLEFB))
925 fdt_simplefb_enable_and_mem_rsv(blob);
926
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200927 return 0;
928}
929#endif
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200930
931static void board_copro_image_process(ulong fw_image, size_t fw_size)
932{
933 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
934
935 if (!rproc_is_initialized())
936 if (rproc_init()) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100937 log_err("Remote Processor %d initialization failed\n",
938 id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200939 return;
940 }
941
942 ret = rproc_load(id, fw_image, fw_size);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100943 log_err("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
944 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200945
Fabien Dessennead6cc942019-10-30 14:38:32 +0100946 if (!ret)
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200947 rproc_start(id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200948}
949
950U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);