blob: 02f51ca2ca8ce8d306d8543271b360e09bcc62ab [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherf1163962016-06-07 08:31:25 +02002/*
3 * board.c
4 *
5 * (C) Copyright 2016
6 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
7 *
8 * Based on:
9 * Board functions for TI AM335X based boards
10 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -050011 * Copyright (C) 2011, Texas Instruments, Incorporated - https://www.ti.com/
Heiko Schocherf1163962016-06-07 08:31:25 +020012 */
13
Tom Rinidec7ea02024-05-20 13:35:03 -060014#include <config.h>
Simon Glass1ea97892020-05-10 11:40:00 -060015#include <bootstage.h>
Simon Glassafb02152019-12-28 10:45:01 -070016#include <cpu_func.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060017#include <env.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020018#include <errno.h>
Simon Glassa7b51302019-11-14 12:57:46 -070019#include <init.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070020#include <irq_func.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <net.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020022#include <spl.h>
23#include <asm/arch/cpu.h>
24#include <asm/arch/hardware.h>
25#include <asm/arch/omap.h>
26#include <asm/arch/ddr_defs.h>
27#include <asm/arch/clock.h>
28#include <asm/arch/gpio.h>
29#include <asm/arch/mmc_host_def.h>
30#include <asm/arch/sys_proto.h>
31#include <asm/arch/mem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060032#include <asm/global_data.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020033#include <asm/io.h>
34#include <asm/emif.h>
35#include <asm/gpio.h>
36#include <i2c.h>
Anatolij Gustschinb37d9892024-08-03 18:15:03 +020037#include <i2c_eeprom.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020038#include <miiphy.h>
39#include <cpsw.h>
Simon Glassdbd79542020-05-10 11:40:11 -060040#include <linux/delay.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020041#include <power/tps65217.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060042#include <env_internal.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020043#include <watchdog.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020044#include "mmc.h"
45#include "board.h"
46
47DECLARE_GLOBAL_DATA_PTR;
48
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020049static struct shc_eeprom __section(".data") header;
Heiko Schocherf1163962016-06-07 08:31:25 +020050static int shc_eeprom_valid;
51
52/*
53 * Read header information from EEPROM into global structure.
54 */
55static int read_eeprom(void)
56{
Anatolij Gustschinb37d9892024-08-03 18:15:03 +020057 struct udevice *dev;
58 int ret;
59
Heiko Schocherf1163962016-06-07 08:31:25 +020060 /* Check if baseboard eeprom is available */
Anatolij Gustschinb37d9892024-08-03 18:15:03 +020061 ret = uclass_first_device_err(UCLASS_I2C_EEPROM, &dev);
62 if (ret) {
63 puts("Could not find EEPROM.\n");
64 return ret;
Heiko Schocherf1163962016-06-07 08:31:25 +020065 }
66
Anatolij Gustschinb37d9892024-08-03 18:15:03 +020067 ret = i2c_eeprom_read(dev, 0, (uint8_t *)&header, sizeof(header));
68 if (ret)
69 return ret;
Heiko Schocherf1163962016-06-07 08:31:25 +020070
71 if (header.magic != HDR_MAGIC) {
72 printf("Incorrect magic number (0x%x) in EEPROM\n",
73 header.magic);
74 return -EIO;
75 }
76
77 shc_eeprom_valid = 1;
78
79 return 0;
80}
81
82static void shc_request_gpio(void)
83{
84 gpio_request(LED_PWR_BL_GPIO, "LED PWR BL");
85 gpio_request(LED_PWR_RD_GPIO, "LED PWR RD");
86 gpio_request(RESET_GPIO, "reset");
87 gpio_request(WIFI_REGEN_GPIO, "WIFI REGEN");
88 gpio_request(WIFI_RST_GPIO, "WIFI rst");
89 gpio_request(ZIGBEE_RST_GPIO, "ZigBee rst");
90 gpio_request(BIDCOS_RST_GPIO, "BIDCOS rst");
91 gpio_request(ENOC_RST_GPIO, "ENOC rst");
92#if defined CONFIG_B_SAMPLE
93 gpio_request(LED_PWR_GN_GPIO, "LED PWR GN");
94 gpio_request(LED_CONN_BL_GPIO, "LED CONN BL");
95 gpio_request(LED_CONN_RD_GPIO, "LED CONN RD");
96 gpio_request(LED_CONN_GN_GPIO, "LED CONN GN");
97#else
98 gpio_request(LED_LAN_BL_GPIO, "LED LAN BL");
99 gpio_request(LED_LAN_RD_GPIO, "LED LAN RD");
100 gpio_request(LED_CLOUD_BL_GPIO, "LED CLOUD BL");
101 gpio_request(LED_CLOUD_RD_GPIO, "LED CLOUD RD");
102 gpio_request(LED_PWM_GPIO, "LED PWM");
103 gpio_request(Z_WAVE_RST_GPIO, "Z WAVE rst");
104#endif
105 gpio_request(BACK_BUTTON_GPIO, "Back button");
106 gpio_request(FRONT_BUTTON_GPIO, "Front button");
107}
108
109/*
110 * Function which forces all installed modules into running state for ICT
111 * testing. Called by SPL.
112 */
113static void __maybe_unused force_modules_running(void)
114{
115 /* Wi-Fi power regulator enable - high = enabled */
116 gpio_direction_output(WIFI_REGEN_GPIO, 1);
117 /*
118 * Wait for Wi-Fi power regulator to reach a stable voltage
Michal Simekcc046dc2024-04-16 08:55:19 +0200119 * (soft-start time, max. 350 us)
Heiko Schocherf1163962016-06-07 08:31:25 +0200120 */
121 __udelay(350);
122
123 /* Wi-Fi module reset - high = running */
124 gpio_direction_output(WIFI_RST_GPIO, 1);
125
126 /* ZigBee reset - high = running */
127 gpio_direction_output(ZIGBEE_RST_GPIO, 1);
128
129 /* BidCos reset - high = running */
130 gpio_direction_output(BIDCOS_RST_GPIO, 1);
131
132#if !defined(CONFIG_B_SAMPLE)
133 /* Z-Wave reset - high = running */
134 gpio_direction_output(Z_WAVE_RST_GPIO, 1);
135#endif
136
137 /* EnOcean reset - low = running */
138 gpio_direction_output(ENOC_RST_GPIO, 0);
139}
140
141/*
142 * Function which forces all installed modules into reset - to be released by
143 * the OS, called by SPL
144 */
145static void __maybe_unused force_modules_reset(void)
146{
147 /* Wi-Fi module reset - low = reset */
148 gpio_direction_output(WIFI_RST_GPIO, 0);
149
150 /* Wi-Fi power regulator enable - low = disabled */
151 gpio_direction_output(WIFI_REGEN_GPIO, 0);
152
153 /* ZigBee reset - low = reset */
154 gpio_direction_output(ZIGBEE_RST_GPIO, 0);
155
156 /* BidCos reset - low = reset */
157 /*gpio_direction_output(BIDCOS_RST_GPIO, 0);*/
158
159#if !defined(CONFIG_B_SAMPLE)
160 /* Z-Wave reset - low = reset */
161 gpio_direction_output(Z_WAVE_RST_GPIO, 0);
162#endif
163
164 /* EnOcean reset - high = reset*/
165 gpio_direction_output(ENOC_RST_GPIO, 1);
166}
167
168/*
169 * Function to set the LEDs in the state "Bootloader booting"
170 */
171static void __maybe_unused leds_set_booting(void)
172{
173#if defined(CONFIG_B_SAMPLE)
174
175 /* Turn all red LEDs on */
176 gpio_direction_output(LED_PWR_RD_GPIO, 1);
177 gpio_direction_output(LED_CONN_RD_GPIO, 1);
178
179#else /* All other SHCs starting with B2-Sample */
180 /* Set the PWM GPIO */
181 gpio_direction_output(LED_PWM_GPIO, 1);
182 /* Turn all red LEDs on */
183 gpio_direction_output(LED_PWR_RD_GPIO, 1);
184 gpio_direction_output(LED_LAN_RD_GPIO, 1);
185 gpio_direction_output(LED_CLOUD_RD_GPIO, 1);
186
187#endif
188}
189
190/*
191 * Function to set the LEDs in the state "Bootloader error"
192 */
Tom Rinia9765d02021-05-03 16:48:58 -0400193static void __maybe_unused leds_set_failure(int state)
Heiko Schocherf1163962016-06-07 08:31:25 +0200194{
195#if defined(CONFIG_B_SAMPLE)
196 /* Turn all blue and green LEDs off */
197 gpio_set_value(LED_PWR_BL_GPIO, 0);
198 gpio_set_value(LED_PWR_GN_GPIO, 0);
199 gpio_set_value(LED_CONN_BL_GPIO, 0);
200 gpio_set_value(LED_CONN_GN_GPIO, 0);
201
202 /* Turn all red LEDs to 'state' */
203 gpio_set_value(LED_PWR_RD_GPIO, state);
204 gpio_set_value(LED_CONN_RD_GPIO, state);
205
206#else /* All other SHCs starting with B2-Sample */
207 /* Set the PWM GPIO */
208 gpio_direction_output(LED_PWM_GPIO, 1);
209
210 /* Turn all blue LEDs off */
211 gpio_set_value(LED_PWR_BL_GPIO, 0);
212 gpio_set_value(LED_LAN_BL_GPIO, 0);
213 gpio_set_value(LED_CLOUD_BL_GPIO, 0);
214
215 /* Turn all red LEDs to 'state' */
216 gpio_set_value(LED_PWR_RD_GPIO, state);
217 gpio_set_value(LED_LAN_RD_GPIO, state);
218 gpio_set_value(LED_CLOUD_RD_GPIO, state);
219#endif
220}
221
222/*
223 * Function to set the LEDs in the state "Bootloader finished"
224 */
225static void leds_set_finish(void)
226{
227#if defined(CONFIG_B_SAMPLE)
228 /* Turn all LEDs off */
229 gpio_set_value(LED_PWR_BL_GPIO, 0);
230 gpio_set_value(LED_PWR_RD_GPIO, 0);
231 gpio_set_value(LED_PWR_GN_GPIO, 0);
232 gpio_set_value(LED_CONN_BL_GPIO, 0);
233 gpio_set_value(LED_CONN_RD_GPIO, 0);
234 gpio_set_value(LED_CONN_GN_GPIO, 0);
235#else /* All other SHCs starting with B2-Sample */
236 /* Turn all LEDs off */
237 gpio_set_value(LED_PWR_BL_GPIO, 0);
238 gpio_set_value(LED_PWR_RD_GPIO, 0);
239 gpio_set_value(LED_LAN_BL_GPIO, 0);
240 gpio_set_value(LED_LAN_RD_GPIO, 0);
241 gpio_set_value(LED_CLOUD_BL_GPIO, 0);
242 gpio_set_value(LED_CLOUD_RD_GPIO, 0);
243
244 /* Turn off the PWM GPIO and mux it to EHRPWM */
245 gpio_set_value(LED_PWM_GPIO, 0);
246 enable_shc_board_pwm_pin_mux();
247#endif
248}
249
250static void check_button_status(void)
251{
252 ulong value;
253 gpio_direction_input(FRONT_BUTTON_GPIO);
254 value = gpio_get_value(FRONT_BUTTON_GPIO);
255
256 if (value == 0) {
257 printf("front button activated !\n");
Simon Glass6a38e412017-08-03 12:22:09 -0600258 env_set("harakiri", "1");
Heiko Schocherf1163962016-06-07 08:31:25 +0200259 }
260}
261
Simon Glass49c24a82024-09-29 19:49:47 -0600262#if defined(CONFIG_XPL_BUILD)
Heiko Schocherf1163962016-06-07 08:31:25 +0200263#ifdef CONFIG_SPL_OS_BOOT
264int spl_start_uboot(void)
265{
266 return 1;
267}
268#endif
269
270static void shc_board_early_init(void)
271{
272 shc_request_gpio();
273# ifdef CONFIG_SHC_ICT
274 /* Force all modules into enabled state for ICT testing */
275 force_modules_running();
276# else
277 /* Force all modules to enter Reset state until released by the OS */
278 force_modules_reset();
279# endif
280 leds_set_booting();
281}
282
Heiko Schocher5c944142019-01-21 06:16:28 +0100283static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
284
Heiko Schocherf1163962016-06-07 08:31:25 +0200285#define MPU_SPREADING_PERMILLE 18 /* Spread 1.8 percent */
286#define OSC (V_OSCK/1000000)
287/* Bosch: Predivider must be fixed to 4, so N = 4-1 */
288#define MPUPLL_N (4-1)
289/* Bosch: Fref = 24 MHz / (N+1) = 24 MHz / 4 = 6 MHz */
290#define MPUPLL_FREF (OSC / (MPUPLL_N + 1))
291
292const struct dpll_params dpll_ddr_shc = {
293 400, OSC-1, 1, -1, -1, -1, -1};
294
295const struct dpll_params *get_dpll_ddr_params(void)
296{
297 return &dpll_ddr_shc;
298}
299
300/*
301 * As we enabled downspread SSC with 1.8%, the values needed to be corrected
302 * such that the 20% overshoot will not lead to too high frequencies.
303 * In all cases, this is achieved by subtracting one from M (6 MHz less).
304 * Example: 600 MHz CPU
305 * Step size: 24 MHz OSC, N = 4 (fix) --> Fref = 6 MHz
306 * 600 MHz - 6 MHz (1x Fref) = 594 MHz
307 * SSC: 594 MHz * 1.8% = 10.7 MHz SSC
308 * Overshoot: 10.7 MHz * 20 % = 2.2 MHz
309 * --> Fmax = 594 MHz + 2.2 MHz = 596.2 MHz, lower than 600 MHz --> OK!
310 */
311const struct dpll_params dpll_mpu_shc_opp100 = {
312 99, MPUPLL_N, 1, -1, -1, -1, -1};
313
Paul Kocialkowski35839c62024-07-29 22:44:38 +0200314void spl_board_init(void)
Heiko Schocherf1163962016-06-07 08:31:25 +0200315{
316 int sil_rev;
317 int mpu_vdd;
318
319 puts(BOARD_ID_STR);
320
321 /*
322 * Set CORE Frequency to OPP100
323 * Hint: DCDC3 (CORE) defaults to 1.100V (for OPP100)
324 */
325 do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
326
327 sil_rev = readl(&cdev->deviceid) >> 28;
328 if (sil_rev < 2) {
329 puts("We do not support Silicon Revisions below 2.0!\n");
330 return;
331 }
332
333 dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
334 if (i2c_probe(TPS65217_CHIP_PM))
335 return;
336
337 /*
338 * Retrieve the CPU max frequency by reading the efuse
339 * SHC-Default: 600 MHz
340 */
341 switch (dpll_mpu_opp100.m) {
342 case MPUPLL_M_1000:
343 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
344 break;
345 case MPUPLL_M_800:
346 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
347 break;
348 case MPUPLL_M_720:
349 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1200MV;
350 break;
351 case MPUPLL_M_600:
352 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1100MV;
353 break;
354 case MPUPLL_M_300:
355 mpu_vdd = TPS65217_DCDC_VOLT_SEL_950MV;
356 break;
357 default:
358 puts("Cannot determine the frequency, failing!\n");
359 return;
360 }
361
362 if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
363 puts("tps65217_voltage_update failure\n");
364 return;
365 }
366
367 /* Set MPU Frequency to what we detected */
368 printf("MPU reference clock runs at %d MHz\n", MPUPLL_FREF);
369 printf("Setting MPU clock to %d MHz\n", MPUPLL_FREF *
370 dpll_mpu_shc_opp100.m);
371 do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_shc_opp100);
372
373 /* Enable Spread Spectrum for this freq to be clean on EMI side */
374 set_mpu_spreadspectrum(MPU_SPREADING_PERMILLE);
375
376 /*
377 * Using the default voltages for the PMIC (TPS65217D)
378 * LS1 = 1.8V (VDD_1V8)
379 * LS2 = 3.3V (VDD_3V3A)
380 * LDO1 = 1.8V (VIO and VRTC)
381 * LDO2 = 3.3V (VDD_3V3AUX)
382 */
383 shc_board_early_init();
384}
385
386void set_uart_mux_conf(void)
387{
388 enable_uart0_pin_mux();
389}
390
391void set_mux_conf_regs(void)
392{
393 enable_shc_board_pin_mux();
394}
395
396const struct ctrl_ioregs ioregs_evmsk = {
397 .cm0ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
398 .cm1ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
399 .cm2ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
400 .dt0ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
401 .dt1ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
402};
403
404static const struct ddr_data ddr3_shc_data = {
405 .datardsratio0 = MT41K256M16HA125E_RD_DQS,
406 .datawdsratio0 = MT41K256M16HA125E_WR_DQS,
407 .datafwsratio0 = MT41K256M16HA125E_PHY_FIFO_WE,
408 .datawrsratio0 = MT41K256M16HA125E_PHY_WR_DATA,
409};
410
411static const struct cmd_control ddr3_shc_cmd_ctrl_data = {
412 .cmd0csratio = MT41K256M16HA125E_RATIO,
413 .cmd0iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
414
415 .cmd1csratio = MT41K256M16HA125E_RATIO,
416 .cmd1iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
417
418 .cmd2csratio = MT41K256M16HA125E_RATIO,
419 .cmd2iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
420};
421
422static struct emif_regs ddr3_shc_emif_reg_data = {
423 .sdram_config = MT41K256M16HA125E_EMIF_SDCFG,
424 .ref_ctrl = MT41K256M16HA125E_EMIF_SDREF,
425 .sdram_tim1 = MT41K256M16HA125E_EMIF_TIM1,
426 .sdram_tim2 = MT41K256M16HA125E_EMIF_TIM2,
427 .sdram_tim3 = MT41K256M16HA125E_EMIF_TIM3,
428 .zq_config = MT41K256M16HA125E_ZQ_CFG,
429 .emif_ddr_phy_ctlr_1 = MT41K256M16HA125E_EMIF_READ_LATENCY |
430 PHY_EN_DYN_PWRDN,
431};
432
433void sdram_init(void)
434{
435 /* Configure the DDR3 RAM */
436 config_ddr(400, &ioregs_evmsk, &ddr3_shc_data,
437 &ddr3_shc_cmd_ctrl_data, &ddr3_shc_emif_reg_data, 0);
438}
439#endif
440
441/*
442 * Basic board specific setup. Pinmux has been handled already.
443 */
444int board_init(void)
445{
446#if defined(CONFIG_HW_WATCHDOG)
447 hw_watchdog_init();
448#endif
Heiko Schocherf1163962016-06-07 08:31:25 +0200449 if (read_eeprom() < 0)
450 puts("EEPROM Content Invalid.\n");
451
Tom Rinibb4dd962022-11-16 13:10:37 -0500452 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Miquel Raynald0935362019-10-03 19:50:03 +0200453#if defined(CONFIG_NOR) || defined(CONFIG_MTD_RAW_NAND)
Heiko Schocherf1163962016-06-07 08:31:25 +0200454 gpmc_init();
455#endif
456 shc_request_gpio();
457
458 return 0;
459}
460
461#ifdef CONFIG_BOARD_LATE_INIT
462int board_late_init(void)
463{
464 check_button_status();
465#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
466 if (shc_eeprom_valid)
467 if (is_valid_ethaddr(header.mac_addr))
Simon Glass8551d552017-08-03 12:22:11 -0600468 eth_env_set_enetaddr("ethaddr", header.mac_addr);
Heiko Schocherf1163962016-06-07 08:31:25 +0200469#endif
470
471 return 0;
472}
Heiko Schocherf1163962016-06-07 08:31:25 +0200473#endif
474
Heiko Schocherf1163962016-06-07 08:31:25 +0200475#if defined(CONFIG_USB_ETHER) && \
Simon Glass49c24a82024-09-29 19:49:47 -0600476 (!defined(CONFIG_XPL_BUILD) || defined(CONFIG_SPL_USB_ETHER))
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900477int board_eth_init(struct bd_info *bis)
Heiko Schocher5c944142019-01-21 06:16:28 +0100478{
479 return usb_eth_initialize(bis);
Heiko Schocherf1163962016-06-07 08:31:25 +0200480}
481#endif
482
Tom Rinia9765d02021-05-03 16:48:58 -0400483#if CONFIG_IS_ENABLED(BOOTSTAGE)
Heiko Schocherf1163962016-06-07 08:31:25 +0200484static void bosch_check_reset_pin(void)
485{
486 if (readl(GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0) & RESET_MASK) {
487 printf("Resetting ...\n");
488 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0);
489 disable_interrupts();
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100490 reset_cpu();
Heiko Schocherf1163962016-06-07 08:31:25 +0200491 /*NOTREACHED*/
492 }
493}
494
495static void hang_bosch(const char *cause, int code)
496{
497 int lv;
498
499 gpio_direction_input(RESET_GPIO);
500
501 /* Enable reset pin interrupt on falling edge */
502 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0);
503 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_FALLINGDETECT);
504 enable_interrupts();
505
506 puts(cause);
507 for (;;) {
508 for (lv = 0; lv < code; lv++) {
509 bosch_check_reset_pin();
510 leds_set_failure(1);
511 __udelay(150 * 1000);
512 leds_set_failure(0);
513 __udelay(150 * 1000);
514 }
515#if defined(BLINK_CODE)
516 __udelay(300 * 1000);
517#endif
518 }
519}
520
521void show_boot_progress(int val)
522{
523 switch (val) {
524 case BOOTSTAGE_ID_NEED_RESET:
525 hang_bosch("need reset", 4);
526 break;
527 }
528}
Tom Rinia9765d02021-05-03 16:48:58 -0400529#endif
Heiko Schocherf1163962016-06-07 08:31:25 +0200530
531void arch_preboot_os(void)
532{
533 leds_set_finish();
534}