blob: 2653e107450849d371ed34bb255a84fdbe6725b5 [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 Kiszkae31f16c2023-02-28 19:19:23 +01004 * Copyright (c) Siemens AG, 2018-2022
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
150static bool board_is_m2(void)
151{
152 struct iot2050_info *info = IOT2050_INFO_DATA;
153
154 return IS_ENABLED(CONFIG_TARGET_IOT2050_A53_PG2) &&
155 info->magic == IOT2050_INFO_MAGIC &&
156 strcmp((char *)info->name, "IOT2050-ADVANCED-M2") == 0;
157}
158
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200159static bool board_is_advanced(void)
160{
161 struct iot2050_info *info = IOT2050_INFO_DATA;
162
163 return info->magic == IOT2050_INFO_MAGIC &&
164 strstr((char *)info->name, "IOT2050-ADVANCED") != NULL;
165}
166
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200167static void remove_mmc1_target(void)
168{
169 char *boot_targets = strdup(env_get("boot_targets"));
170 char *mmc1 = strstr(boot_targets, "mmc1");
171
172 if (mmc1) {
173 memmove(mmc1, mmc1 + 4, strlen(mmc1 + 4) + 1);
174 env_set("boot_targets", boot_targets);
175 }
176
177 free(boot_targets);
178}
179
180void set_board_info_env(void)
181{
182 struct iot2050_info *info = IOT2050_INFO_DATA;
183 u8 __maybe_unused mac_cnt;
184 const char *fdtfile;
185
186 if (info->magic != IOT2050_INFO_MAGIC) {
187 pr_err("IOT2050: Board info parsing error!\n");
188 return;
189 }
190
191 if (env_get("board_uuid"))
192 return;
193
194 env_set("board_name", info->name);
195 env_set("board_serial", info->serial);
196 env_set("mlfb", info->mlfb);
197 env_set("board_uuid", info->uuid);
198 env_set("board_a5e", info->a5e);
199 env_set("fw_version", PLAIN_VERSION);
200 env_set("seboot_version", info->seboot_version);
201
202 if (IS_ENABLED(CONFIG_NET)) {
203 /* set MAC addresses to ensure forwarding to the OS */
204 for (mac_cnt = 0; mac_cnt < info->mac_addr_cnt; mac_cnt++) {
205 if (is_valid_ethaddr(info->mac_addr[mac_cnt]))
206 eth_env_set_enetaddr_by_index("eth",
207 mac_cnt + 1,
208 info->mac_addr[mac_cnt]);
209 }
210 }
211
212 if (board_is_advanced()) {
Su Baocheng0dcd3372023-02-28 19:19:09 +0100213 if (IS_ENABLED(CONFIG_TARGET_IOT2050_A53_PG1))
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200214 fdtfile = "ti/k3-am6548-iot2050-advanced.dtb";
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100215 else if(board_is_m2())
216 fdtfile = "ti/k3-am6548-iot2050-advanced-m2.dtb";
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200217 else
218 fdtfile = "ti/k3-am6548-iot2050-advanced-pg2.dtb";
219 } else {
Su Baocheng0dcd3372023-02-28 19:19:09 +0100220 if (IS_ENABLED(CONFIG_TARGET_IOT2050_A53_PG1))
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200221 fdtfile = "ti/k3-am6528-iot2050-basic.dtb";
222 else
223 fdtfile = "ti/k3-am6528-iot2050-basic-pg2.dtb";
224 /* remove the unavailable eMMC (mmc1) from the list */
225 remove_mmc1_target();
226 }
227 env_set("fdtfile", fdtfile);
228
229 env_save();
230}
231
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100232static void m2_overlay_prepare(void)
233{
234#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
235 const char *overlay_path;
236 void *overlay;
237 u64 loadaddr;
238 ofnode node;
239 int ret;
240
241 if (connector_mode == BKEY_PCIEX2)
242 return;
243
244 if (connector_mode == BKEY_PCIE_EKEY_PCIE)
245 overlay_path = "/fit-images/bkey-ekey-pcie-overlay";
246 else
247 overlay_path = "/fit-images/bkey-usb3-overlay";
248
249 node = ofnode_path(overlay_path);
250 if (!ofnode_valid(node))
251 goto fit_error;
252
253 ret = ofnode_read_u64(node, "load", &loadaddr);
254 if (ret)
255 goto fit_error;
256
257 ret = ofnode_read_u32(node, "size", &connector_overlay_size);
258 if (ret)
259 goto fit_error;
260
261 overlay = map_sysmem(loadaddr, connector_overlay_size);
262
263 connector_overlay = malloc(connector_overlay_size);
264 if (!connector_overlay)
265 goto fit_error;
266
267 memcpy(connector_overlay, overlay, connector_overlay_size);
268 return;
269
270fit_error:
271 pr_err("M.2 device tree overlay %s not available,\n", overlay_path);
272#endif
273}
274
275static void m2_connector_setup(void)
276{
277 ulong m2_manual_config = env_get_ulong("m2_manual_config", 10,
278 CONNECTOR_MODE_INVALID);
279 const char *mode_info = "";
280 struct m2_config_pins config_pins;
281 unsigned int n;
282
283 /* enable M.2 connector power */
284 set_pinvalue("gpio@601000_17", "P3V3_M2_EN", 1);
285 udelay(4 * 100);
286
287 if (m2_manual_config < CONNECTOR_MODE_INVALID) {
288 mode_info = " [manual mode]";
289 connector_mode = m2_manual_config;
290 } else { /* auto detection */
291 for (n = 0; n < ARRAY_SIZE(config_pins.config); n++)
292 config_pins.config[n] =
293 get_pinvalue(m2_bkey_cfg_pin_info[n].gpio_name,
294 m2_bkey_cfg_pin_info[n].label);
295 connector_mode = CONNECTOR_MODE_INVALID;
296 for (n = 0; n < ARRAY_SIZE(m2_config_table); n++) {
297 if (!memcmp(config_pins.config,
298 m2_config_table[n].config_pins.config,
299 sizeof(config_pins.config))) {
300 connector_mode = m2_config_table[n].mode;
301 break;
302 }
303 }
304 if (connector_mode == CONNECTOR_MODE_INVALID) {
305 mode_info = " [fallback, card unknown/unsupported]";
306 connector_mode = BKEY_USB30_EKEY_PCIE;
307 }
308 }
309
310 printf("M.2: %s%s\n", m2_connector_mode_name[connector_mode],
311 mode_info);
312
313 /* configure serdes mux */
314 set_pinvalue(serdes_mux_ctl_pin_info[0].gpio_name,
315 serdes_mux_ctl_pin_info[0].label,
316 serdes_mux_ctrl[connector_mode].ctrl_usb30_pcie0_lane0);
317 set_pinvalue(serdes_mux_ctl_pin_info[1].gpio_name,
318 serdes_mux_ctl_pin_info[1].label,
319 serdes_mux_ctrl[connector_mode].ctrl_pcie1_pcie0);
320 set_pinvalue(serdes_mux_ctl_pin_info[2].gpio_name,
321 serdes_mux_ctl_pin_info[2].label,
322 serdes_mux_ctrl[connector_mode].ctrl_usb30_pcie0_lane1);
323
324 m2_overlay_prepare();
325}
326
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200327int board_init(void)
328{
329 return 0;
330}
331
332int dram_init(void)
333{
334 if (board_is_advanced())
335 gd->ram_size = SZ_2G;
336 else
337 gd->ram_size = SZ_1G;
338
339 return 0;
340}
341
342int dram_init_banksize(void)
343{
344 dram_init();
345
346 /* Bank 0 declares the memory available in the DDR low region */
Tom Rinibb4dd962022-11-16 13:10:37 -0500347 gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200348 gd->bd->bi_dram[0].size = gd->ram_size;
349
350 /* Bank 1 declares the memory available in the DDR high region */
351 gd->bd->bi_dram[1].start = 0;
352 gd->bd->bi_dram[1].size = 0;
353
354 return 0;
355}
356
357#ifdef CONFIG_SPL_LOAD_FIT
358int board_fit_config_name_match(const char *name)
359{
360 struct iot2050_info *info = IOT2050_INFO_DATA;
361 char upper_name[32];
362
Su Baocheng8999cc52023-02-28 19:19:10 +0100363 /* skip the prefix "k3-am65x8-" */
364 name += 10;
365
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200366 if (info->magic != IOT2050_INFO_MAGIC ||
367 strlen(name) >= sizeof(upper_name))
368 return -1;
369
370 str_to_upper(name, upper_name, sizeof(upper_name));
371 if (!strcmp(upper_name, (char *)info->name))
372 return 0;
373
374 return -1;
375}
376#endif
377
378int do_board_detect(void)
379{
380 return 0;
381}
382
383#ifdef CONFIG_IOT2050_BOOT_SWITCH
384static bool user_button_pressed(void)
385{
386 struct udevice *red_led = NULL;
387 unsigned long count = 0;
388 struct gpio_desc gpio;
389
390 memset(&gpio, 0, sizeof(gpio));
391
chao zeng7d933ad2023-02-28 19:19:20 +0100392 if (dm_gpio_lookup_name("gpio@42110000_25", &gpio) < 0 ||
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200393 dm_gpio_request(&gpio, "USER button") < 0 ||
394 dm_gpio_set_dir_flags(&gpio, GPIOD_IS_IN) < 0)
395 return false;
396
397 if (dm_gpio_get_value(&gpio) == 1)
398 return false;
399
400 printf("USER button pressed - booting from external media only\n");
401
402 led_get_by_label("status-led-red", &red_led);
403
404 if (red_led)
405 led_set_state(red_led, LEDST_ON);
406
407 while (dm_gpio_get_value(&gpio) == 0 && count++ < 10000)
408 mdelay(1);
409
410 if (red_led)
411 led_set_state(red_led, LEDST_OFF);
412
413 return true;
414}
415#endif
416
417#define SERDES0_LANE_SELECT 0x00104080
418
419int board_late_init(void)
420{
421 /* change CTRL_MMR register to let serdes0 not output USB3.0 signals. */
422 writel(0x3, SERDES0_LANE_SELECT);
423
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100424 if (board_is_m2())
425 m2_connector_setup();
426
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200427 set_board_info_env();
428
429 /* remove the eMMC if requested via button */
430 if (IS_ENABLED(CONFIG_IOT2050_BOOT_SWITCH) && board_is_advanced() &&
431 user_button_pressed())
432 remove_mmc1_target();
433
434 return 0;
435}
436
437#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100438static void m2_fdt_fixup(void *blob)
439{
440 void *overlay_copy = NULL;
441 void *fdt_copy = NULL;
442 u32 fdt_size;
443 int err;
444
445 if (!connector_overlay)
446 return;
447
448 /*
449 * We need to work with temporary copies here because fdt_overlay_apply
450 * is destructive to the overlay and also to the target blob, even if
451 * application fails.
452 */
453 fdt_size = fdt_totalsize(blob);
454 fdt_copy = malloc(fdt_size);
455 if (!fdt_copy)
456 goto fixup_error;
457
458 memcpy(fdt_copy, blob, fdt_size);
459
460 overlay_copy = malloc(connector_overlay_size);
461 if (!overlay_copy)
462 goto fixup_error;
463
464 memcpy(overlay_copy, connector_overlay, connector_overlay_size);
465
466 err = fdt_overlay_apply_verbose(fdt_copy, overlay_copy);
467 if (err)
468 goto fixup_error;
469
470 memcpy(blob, fdt_copy, fdt_size);
471
472cleanup:
473 free(fdt_copy);
474 free(overlay_copy);
475 return;
476
477fixup_error:
478 pr_err("Could not apply M.2 device tree overlay\n");
479 goto cleanup;
480}
481
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200482int ft_board_setup(void *blob, struct bd_info *bd)
483{
Jan Kiszkae31f16c2023-02-28 19:19:23 +0100484 if (board_is_m2())
485 m2_fdt_fixup(blob);
486
Andrew Davisb1c29792023-04-06 11:38:10 -0500487 return 0;
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200488}
489#endif
490
491void spl_board_init(void)
492{
493}
494
Marek Vasut98154342021-10-23 03:06:03 +0200495#if CONFIG_IS_ENABLED(LED) && CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200496/*
497 * Indicate any error or (accidental?) entering of CLI via the red status LED.
498 */
499void show_boot_progress(int progress)
500{
501 struct udevice *dev;
502 int ret;
503
Jan Kiszkaa8ea4f42021-11-03 15:12:30 +0100504 if ((progress < 0 && progress != -BOOTSTAGE_ID_NET_ETH_START) ||
505 progress == BOOTSTAGE_ID_ENTER_CLI_LOOP) {
Jan Kiszka8ff2ff82021-09-18 08:17:53 +0200506 ret = led_get_by_label("status-led-green", &dev);
507 if (ret == 0)
508 led_set_state(dev, LEDST_OFF);
509
510 ret = led_get_by_label("status-led-red", &dev);
511 if (ret == 0)
512 led_set_state(dev, LEDST_ON);
513 }
514}
515#endif