blob: 15f5310c7bf3850083bc69d771462f20044f1632 [file] [log] [blame]
Jan Kiszka8ff2ff82021-09-18 08:17:53 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Board specific initialization for IOT2050
Jan Kiszkab2d24f92023-07-27 06:34:54 +02004 * Copyright (c) Siemens AG, 2018-2023
Jan Kiszka8ff2ff82021-09-18 08:17:53 +02005 *
6 * Authors:
7 * Le Jin <le.jin@siemens.com>
8 * Jan Kiszka <jan.kiszka@siemens.com>
9 */
10
11#include <common.h>
12#include <bootstage.h>
13#include <dm.h>
Jan Kiszkae31f16c2023-02-28 19:19:23 +010014#include <fdt_support.h>
Jan Kiszka8ff2ff82021-09-18 08:17:53 +020015#include <i2c.h>
16#include <led.h>
17#include <malloc.h>
Jan Kiszkae31f16c2023-02-28 19:19:23 +010018#include <mapmem.h>
Jan Kiszka8ff2ff82021-09-18 08:17:53 +020019#include <net.h>
20#include <phy.h>
21#include <spl.h>
22#include <version.h>
23#include <linux/delay.h>
Jan Kiszka8ff2ff82021-09-18 08:17:53 +020024#include <asm/arch/hardware.h>
25#include <asm/gpio.h>
26#include <asm/io.h>
27
28#define IOT2050_INFO_MAGIC 0x20502050
29
30struct iot2050_info {
31 u32 magic;
32 u16 size;
33 char name[20 + 1];
34 char serial[16 + 1];
35 char mlfb[18 + 1];
36 char uuid[32 + 1];
37 char a5e[18 + 1];
38 u8 mac_addr_cnt;
39 u8 mac_addr[8][ARP_HLEN];
40 char seboot_version[40 + 1];
41} __packed;
42
43/*
44 * Scratch SRAM (available before DDR RAM) contains extracted EEPROM data.
45 */
46#define IOT2050_INFO_DATA ((struct iot2050_info *) \
47 TI_SRAM_SCRATCH_BOARD_EEPROM_START)
48
49DECLARE_GLOBAL_DATA_PTR;
50
Jan Kiszkae31f16c2023-02-28 19:19:23 +010051struct gpio_config {
52 const char *gpio_name;
53 const char *label;
54};
55
56enum m2_connector_mode {
57 BKEY_PCIEX2 = 0,
58 BKEY_PCIE_EKEY_PCIE,
59 BKEY_USB30_EKEY_PCIE,
60 CONNECTOR_MODE_INVALID
61};
62
63struct m2_config_pins {
64 int config[4];
65};
66
67struct serdes_mux_control {
68 int ctrl_usb30_pcie0_lane0;
69 int ctrl_pcie1_pcie0;
70 int ctrl_usb30_pcie0_lane1;
71};
72
73struct m2_config_table {
74 struct m2_config_pins config_pins;
75 enum m2_connector_mode mode;
76};
77
78static const struct gpio_config serdes_mux_ctl_pin_info[] = {
79 {"gpio@600000_88", "CTRL_USB30_PCIE0_LANE0"},
80 {"gpio@600000_82", "CTRL_PCIE1_PCIE0"},
81 {"gpio@600000_89", "CTRL_USB30_PCIE0_LANE1"},
82};
83
84static const struct gpio_config m2_bkey_cfg_pin_info[] = {
85 {"gpio@601000_18", "KEY_CONFIG_0"},
86 {"gpio@601000_19", "KEY_CONFIG_1"},
87 {"gpio@601000_88", "KEY_CONFIG_2"},
88 {"gpio@601000_89", "KEY_CONFIG_3"},
89};
90
91static const struct m2_config_table m2_config_table[] = {
92 {{{0, 1, 0, 0}}, BKEY_PCIEX2},
93 {{{0, 0, 1, 0}}, BKEY_PCIE_EKEY_PCIE},
94 {{{0, 1, 1, 0}}, BKEY_PCIE_EKEY_PCIE},
95 {{{1, 0, 0, 1}}, BKEY_PCIE_EKEY_PCIE},
96 {{{1, 1, 0, 1}}, BKEY_PCIE_EKEY_PCIE},
97 {{{0, 0, 0, 1}}, BKEY_USB30_EKEY_PCIE},
98 {{{0, 1, 0, 1}}, BKEY_USB30_EKEY_PCIE},
99 {{{0, 0, 1, 1}}, BKEY_USB30_EKEY_PCIE},
100 {{{0, 1, 1, 1}}, BKEY_USB30_EKEY_PCIE},
101 {{{1, 0, 1, 1}}, BKEY_USB30_EKEY_PCIE},
102};
103
104static const struct serdes_mux_control serdes_mux_ctrl[] = {
105 [BKEY_PCIEX2] = {0, 0, 1},
106 [BKEY_PCIE_EKEY_PCIE] = {0, 1, 0},
107 [BKEY_USB30_EKEY_PCIE] = {1, 1, 0},
108};
109
110static const char *m2_connector_mode_name[] = {
111 [BKEY_PCIEX2] = "PCIe x2 (key B)",
112 [BKEY_PCIE_EKEY_PCIE] = "PCIe (key B) / PCIe (key E)",
113 [BKEY_USB30_EKEY_PCIE] = "USB 3.0 (key B) / PCIe (key E)",
114};
115
116static enum m2_connector_mode connector_mode;
117
118#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
119static void *connector_overlay;
120static u32 connector_overlay_size;
121#endif
122
123static int get_pinvalue(const char *gpio_name, const char *label)
124{
125 struct gpio_desc gpio;
126
127 if (dm_gpio_lookup_name(gpio_name, &gpio) < 0 ||
128 dm_gpio_request(&gpio, label) < 0 ||
129 dm_gpio_set_dir_flags(&gpio, GPIOD_IS_IN) < 0) {
130 pr_err("Cannot get pin %s for M.2 configuration\n", gpio_name);
131 return 0;
132 }
133
134 return dm_gpio_get_value(&gpio);
135}
136
137static void set_pinvalue(const char *gpio_name, const char *label, int value)
138{
139 struct gpio_desc gpio;
140
141 if (dm_gpio_lookup_name(gpio_name, &gpio) < 0 ||
142 dm_gpio_request(&gpio, label) < 0 ||
143 dm_gpio_set_dir_flags(&gpio, GPIOD_IS_OUT) < 0) {
144 pr_err("Cannot set pin %s for M.2 configuration\n", gpio_name);
145 return;
146 }
147 dm_gpio_set_value(&gpio, value);
148}
149
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200150static bool board_is_advanced(void)
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100151{
152 struct iot2050_info *info = IOT2050_INFO_DATA;
153
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200154 return info->magic == IOT2050_INFO_MAGIC &&
155 strstr((char *)info->name, "IOT2050-ADVANCED") != NULL;
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100156}
157
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200158static bool board_is_sr1(void)
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200159{
160 struct iot2050_info *info = IOT2050_INFO_DATA;
161
162 return info->magic == IOT2050_INFO_MAGIC &&
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200163 strstr((char *)info->name, "-PG2") != NULL;
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200164}
165
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200166static bool board_is_m2(void)
167{
168 struct iot2050_info *info = IOT2050_INFO_DATA;
169
170 return !board_is_sr1() && info->magic == IOT2050_INFO_MAGIC &&
171 strcmp((char *)info->name, "IOT2050-ADVANCED-M2") == 0;
172}
173
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200174static void remove_mmc1_target(void)
175{
176 char *boot_targets = strdup(env_get("boot_targets"));
177 char *mmc1 = strstr(boot_targets, "mmc1");
178
179 if (mmc1) {
180 memmove(mmc1, mmc1 + 4, strlen(mmc1 + 4) + 1);
181 env_set("boot_targets", boot_targets);
182 }
183
184 free(boot_targets);
185}
186
187void set_board_info_env(void)
188{
189 struct iot2050_info *info = IOT2050_INFO_DATA;
190 u8 __maybe_unused mac_cnt;
191 const char *fdtfile;
192
193 if (info->magic != IOT2050_INFO_MAGIC) {
194 pr_err("IOT2050: Board info parsing error!\n");
195 return;
196 }
197
198 if (env_get("board_uuid"))
199 return;
200
201 env_set("board_name", info->name);
202 env_set("board_serial", info->serial);
203 env_set("mlfb", info->mlfb);
204 env_set("board_uuid", info->uuid);
205 env_set("board_a5e", info->a5e);
206 env_set("fw_version", PLAIN_VERSION);
207 env_set("seboot_version", info->seboot_version);
208
209 if (IS_ENABLED(CONFIG_NET)) {
210 /* set MAC addresses to ensure forwarding to the OS */
211 for (mac_cnt = 0; mac_cnt < info->mac_addr_cnt; mac_cnt++) {
212 if (is_valid_ethaddr(info->mac_addr[mac_cnt]))
213 eth_env_set_enetaddr_by_index("eth",
214 mac_cnt + 1,
215 info->mac_addr[mac_cnt]);
216 }
217 }
218
219 if (board_is_advanced()) {
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200220 if (board_is_sr1())
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200221 fdtfile = "ti/k3-am6548-iot2050-advanced.dtb";
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100222 else if(board_is_m2())
223 fdtfile = "ti/k3-am6548-iot2050-advanced-m2.dtb";
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200224 else
225 fdtfile = "ti/k3-am6548-iot2050-advanced-pg2.dtb";
226 } else {
Jan Kiszkab2d24f92023-07-27 06:34:54 +0200227 if (board_is_sr1())
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200228 fdtfile = "ti/k3-am6528-iot2050-basic.dtb";
229 else
230 fdtfile = "ti/k3-am6528-iot2050-basic-pg2.dtb";
231 /* remove the unavailable eMMC (mmc1) from the list */
232 remove_mmc1_target();
233 }
234 env_set("fdtfile", fdtfile);
235
236 env_save();
237}
238
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100239static void m2_overlay_prepare(void)
240{
241#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
242 const char *overlay_path;
243 void *overlay;
244 u64 loadaddr;
245 ofnode node;
246 int ret;
247
248 if (connector_mode == BKEY_PCIEX2)
249 return;
250
251 if (connector_mode == BKEY_PCIE_EKEY_PCIE)
252 overlay_path = "/fit-images/bkey-ekey-pcie-overlay";
253 else
254 overlay_path = "/fit-images/bkey-usb3-overlay";
255
256 node = ofnode_path(overlay_path);
257 if (!ofnode_valid(node))
258 goto fit_error;
259
260 ret = ofnode_read_u64(node, "load", &loadaddr);
261 if (ret)
262 goto fit_error;
263
264 ret = ofnode_read_u32(node, "size", &connector_overlay_size);
265 if (ret)
266 goto fit_error;
267
268 overlay = map_sysmem(loadaddr, connector_overlay_size);
269
270 connector_overlay = malloc(connector_overlay_size);
271 if (!connector_overlay)
272 goto fit_error;
273
274 memcpy(connector_overlay, overlay, connector_overlay_size);
275 return;
276
277fit_error:
278 pr_err("M.2 device tree overlay %s not available,\n", overlay_path);
279#endif
280}
281
282static void m2_connector_setup(void)
283{
284 ulong m2_manual_config = env_get_ulong("m2_manual_config", 10,
285 CONNECTOR_MODE_INVALID);
286 const char *mode_info = "";
287 struct m2_config_pins config_pins;
288 unsigned int n;
289
290 /* enable M.2 connector power */
291 set_pinvalue("gpio@601000_17", "P3V3_M2_EN", 1);
292 udelay(4 * 100);
293
294 if (m2_manual_config < CONNECTOR_MODE_INVALID) {
295 mode_info = " [manual mode]";
296 connector_mode = m2_manual_config;
297 } else { /* auto detection */
298 for (n = 0; n < ARRAY_SIZE(config_pins.config); n++)
299 config_pins.config[n] =
300 get_pinvalue(m2_bkey_cfg_pin_info[n].gpio_name,
301 m2_bkey_cfg_pin_info[n].label);
302 connector_mode = CONNECTOR_MODE_INVALID;
303 for (n = 0; n < ARRAY_SIZE(m2_config_table); n++) {
304 if (!memcmp(config_pins.config,
305 m2_config_table[n].config_pins.config,
306 sizeof(config_pins.config))) {
307 connector_mode = m2_config_table[n].mode;
308 break;
309 }
310 }
311 if (connector_mode == CONNECTOR_MODE_INVALID) {
312 mode_info = " [fallback, card unknown/unsupported]";
313 connector_mode = BKEY_USB30_EKEY_PCIE;
314 }
315 }
316
317 printf("M.2: %s%s\n", m2_connector_mode_name[connector_mode],
318 mode_info);
319
320 /* configure serdes mux */
321 set_pinvalue(serdes_mux_ctl_pin_info[0].gpio_name,
322 serdes_mux_ctl_pin_info[0].label,
323 serdes_mux_ctrl[connector_mode].ctrl_usb30_pcie0_lane0);
324 set_pinvalue(serdes_mux_ctl_pin_info[1].gpio_name,
325 serdes_mux_ctl_pin_info[1].label,
326 serdes_mux_ctrl[connector_mode].ctrl_pcie1_pcie0);
327 set_pinvalue(serdes_mux_ctl_pin_info[2].gpio_name,
328 serdes_mux_ctl_pin_info[2].label,
329 serdes_mux_ctrl[connector_mode].ctrl_usb30_pcie0_lane1);
330
331 m2_overlay_prepare();
332}
333
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200334int board_init(void)
335{
336 return 0;
337}
338
339int dram_init(void)
340{
341 if (board_is_advanced())
342 gd->ram_size = SZ_2G;
343 else
344 gd->ram_size = SZ_1G;
345
346 return 0;
347}
348
349int dram_init_banksize(void)
350{
351 dram_init();
352
353 /* Bank 0 declares the memory available in the DDR low region */
Tom Rinibb4dd962022-11-16 13:10:37 -0500354 gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200355 gd->bd->bi_dram[0].size = gd->ram_size;
356
357 /* Bank 1 declares the memory available in the DDR high region */
358 gd->bd->bi_dram[1].start = 0;
359 gd->bd->bi_dram[1].size = 0;
360
361 return 0;
362}
363
364#ifdef CONFIG_SPL_LOAD_FIT
365int board_fit_config_name_match(const char *name)
366{
367 struct iot2050_info *info = IOT2050_INFO_DATA;
368 char upper_name[32];
369
Su Baocheng8999cc52023-02-28 19:19:10 +0100370 /* skip the prefix "k3-am65x8-" */
371 name += 10;
372
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200373 if (info->magic != IOT2050_INFO_MAGIC ||
374 strlen(name) >= sizeof(upper_name))
375 return -1;
376
377 str_to_upper(name, upper_name, sizeof(upper_name));
378 if (!strcmp(upper_name, (char *)info->name))
379 return 0;
380
381 return -1;
382}
383#endif
384
385int do_board_detect(void)
386{
387 return 0;
388}
389
390#ifdef CONFIG_IOT2050_BOOT_SWITCH
391static bool user_button_pressed(void)
392{
393 struct udevice *red_led = NULL;
394 unsigned long count = 0;
395 struct gpio_desc gpio;
396
397 memset(&gpio, 0, sizeof(gpio));
398
chao zeng7d933ad2023-02-28 19:19:20 +0100399 if (dm_gpio_lookup_name("gpio@42110000_25", &gpio) < 0 ||
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200400 dm_gpio_request(&gpio, "USER button") < 0 ||
401 dm_gpio_set_dir_flags(&gpio, GPIOD_IS_IN) < 0)
402 return false;
403
404 if (dm_gpio_get_value(&gpio) == 1)
405 return false;
406
407 printf("USER button pressed - booting from external media only\n");
408
409 led_get_by_label("status-led-red", &red_led);
410
411 if (red_led)
412 led_set_state(red_led, LEDST_ON);
413
414 while (dm_gpio_get_value(&gpio) == 0 && count++ < 10000)
415 mdelay(1);
416
417 if (red_led)
418 led_set_state(red_led, LEDST_OFF);
419
420 return true;
421}
422#endif
423
424#define SERDES0_LANE_SELECT 0x00104080
425
426int board_late_init(void)
427{
428 /* change CTRL_MMR register to let serdes0 not output USB3.0 signals. */
429 writel(0x3, SERDES0_LANE_SELECT);
430
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100431 if (board_is_m2())
432 m2_connector_setup();
433
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200434 set_board_info_env();
435
436 /* remove the eMMC if requested via button */
437 if (IS_ENABLED(CONFIG_IOT2050_BOOT_SWITCH) && board_is_advanced() &&
438 user_button_pressed())
439 remove_mmc1_target();
440
441 return 0;
442}
443
444#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100445static void m2_fdt_fixup(void *blob)
446{
447 void *overlay_copy = NULL;
448 void *fdt_copy = NULL;
449 u32 fdt_size;
450 int err;
451
452 if (!connector_overlay)
453 return;
454
455 /*
456 * We need to work with temporary copies here because fdt_overlay_apply
457 * is destructive to the overlay and also to the target blob, even if
458 * application fails.
459 */
460 fdt_size = fdt_totalsize(blob);
461 fdt_copy = malloc(fdt_size);
462 if (!fdt_copy)
463 goto fixup_error;
464
465 memcpy(fdt_copy, blob, fdt_size);
466
467 overlay_copy = malloc(connector_overlay_size);
468 if (!overlay_copy)
469 goto fixup_error;
470
471 memcpy(overlay_copy, connector_overlay, connector_overlay_size);
472
473 err = fdt_overlay_apply_verbose(fdt_copy, overlay_copy);
474 if (err)
475 goto fixup_error;
476
477 memcpy(blob, fdt_copy, fdt_size);
478
479cleanup:
480 free(fdt_copy);
481 free(overlay_copy);
482 return;
483
484fixup_error:
485 pr_err("Could not apply M.2 device tree overlay\n");
486 goto cleanup;
487}
488
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200489int ft_board_setup(void *blob, struct bd_info *bd)
490{
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100491 if (board_is_m2())
492 m2_fdt_fixup(blob);
493
Andrew Davisb1c29792023-04-06 11:38:10 -0500494 return 0;
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200495}
496#endif
497
498void spl_board_init(void)
499{
500}
501
Marek Vasut98154342021-10-23 03:06:03 +0200502#if CONFIG_IS_ENABLED(LED) && CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200503/*
504 * Indicate any error or (accidental?) entering of CLI via the red status LED.
505 */
506void show_boot_progress(int progress)
507{
508 struct udevice *dev;
509 int ret;
510
Jan Kiszkaa8ea4f42021-11-03 15:12:30 +0100511 if ((progress < 0 && progress != -BOOTSTAGE_ID_NET_ETH_START) ||
512 progress == BOOTSTAGE_ID_ENTER_CLI_LOOP) {
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200513 ret = led_get_by_label("status-led-green", &dev);
514 if (ret == 0)
515 led_set_state(dev, LEDST_OFF);
516
517 ret = led_get_by_label("status-led-red", &dev);
518 if (ret == 0)
519 led_set_state(dev, LEDST_ON);
520 }
521}
522#endif