blob: b8f1b56502e6ba97c65bfcb732f1c615899cabab [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 *
11 * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
Heiko Schocherf1163962016-06-07 08:31:25 +020012 */
13
14#include <common.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>
32#include <asm/io.h>
33#include <asm/emif.h>
34#include <asm/gpio.h>
35#include <i2c.h>
36#include <miiphy.h>
37#include <cpsw.h>
Simon Glassdbd79542020-05-10 11:40:11 -060038#include <linux/delay.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020039#include <power/tps65217.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060040#include <env_internal.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020041#include <watchdog.h>
Heiko Schocherf1163962016-06-07 08:31:25 +020042#include "mmc.h"
43#include "board.h"
44
45DECLARE_GLOBAL_DATA_PTR;
46
Heiko Schocherf1163962016-06-07 08:31:25 +020047static struct shc_eeprom __attribute__((section(".data"))) header;
48static int shc_eeprom_valid;
49
50/*
51 * Read header information from EEPROM into global structure.
52 */
53static int read_eeprom(void)
54{
55 /* Check if baseboard eeprom is available */
56 if (i2c_probe(CONFIG_SYS_I2C_EEPROM_ADDR)) {
57 puts("Could not probe the EEPROM; something fundamentally wrong on the I2C bus.\n");
58 return -ENODEV;
59 }
60
61 /* read the eeprom using i2c */
62 if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)&header,
63 sizeof(header))) {
64 puts("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
65 return -EIO;
66 }
67
68 if (header.magic != HDR_MAGIC) {
69 printf("Incorrect magic number (0x%x) in EEPROM\n",
70 header.magic);
71 return -EIO;
72 }
73
74 shc_eeprom_valid = 1;
75
76 return 0;
77}
78
79static void shc_request_gpio(void)
80{
81 gpio_request(LED_PWR_BL_GPIO, "LED PWR BL");
82 gpio_request(LED_PWR_RD_GPIO, "LED PWR RD");
83 gpio_request(RESET_GPIO, "reset");
84 gpio_request(WIFI_REGEN_GPIO, "WIFI REGEN");
85 gpio_request(WIFI_RST_GPIO, "WIFI rst");
86 gpio_request(ZIGBEE_RST_GPIO, "ZigBee rst");
87 gpio_request(BIDCOS_RST_GPIO, "BIDCOS rst");
88 gpio_request(ENOC_RST_GPIO, "ENOC rst");
89#if defined CONFIG_B_SAMPLE
90 gpio_request(LED_PWR_GN_GPIO, "LED PWR GN");
91 gpio_request(LED_CONN_BL_GPIO, "LED CONN BL");
92 gpio_request(LED_CONN_RD_GPIO, "LED CONN RD");
93 gpio_request(LED_CONN_GN_GPIO, "LED CONN GN");
94#else
95 gpio_request(LED_LAN_BL_GPIO, "LED LAN BL");
96 gpio_request(LED_LAN_RD_GPIO, "LED LAN RD");
97 gpio_request(LED_CLOUD_BL_GPIO, "LED CLOUD BL");
98 gpio_request(LED_CLOUD_RD_GPIO, "LED CLOUD RD");
99 gpio_request(LED_PWM_GPIO, "LED PWM");
100 gpio_request(Z_WAVE_RST_GPIO, "Z WAVE rst");
101#endif
102 gpio_request(BACK_BUTTON_GPIO, "Back button");
103 gpio_request(FRONT_BUTTON_GPIO, "Front button");
104}
105
106/*
107 * Function which forces all installed modules into running state for ICT
108 * testing. Called by SPL.
109 */
110static void __maybe_unused force_modules_running(void)
111{
112 /* Wi-Fi power regulator enable - high = enabled */
113 gpio_direction_output(WIFI_REGEN_GPIO, 1);
114 /*
115 * Wait for Wi-Fi power regulator to reach a stable voltage
116 * (soft-start time, max. 350 µs)
117 */
118 __udelay(350);
119
120 /* Wi-Fi module reset - high = running */
121 gpio_direction_output(WIFI_RST_GPIO, 1);
122
123 /* ZigBee reset - high = running */
124 gpio_direction_output(ZIGBEE_RST_GPIO, 1);
125
126 /* BidCos reset - high = running */
127 gpio_direction_output(BIDCOS_RST_GPIO, 1);
128
129#if !defined(CONFIG_B_SAMPLE)
130 /* Z-Wave reset - high = running */
131 gpio_direction_output(Z_WAVE_RST_GPIO, 1);
132#endif
133
134 /* EnOcean reset - low = running */
135 gpio_direction_output(ENOC_RST_GPIO, 0);
136}
137
138/*
139 * Function which forces all installed modules into reset - to be released by
140 * the OS, called by SPL
141 */
142static void __maybe_unused force_modules_reset(void)
143{
144 /* Wi-Fi module reset - low = reset */
145 gpio_direction_output(WIFI_RST_GPIO, 0);
146
147 /* Wi-Fi power regulator enable - low = disabled */
148 gpio_direction_output(WIFI_REGEN_GPIO, 0);
149
150 /* ZigBee reset - low = reset */
151 gpio_direction_output(ZIGBEE_RST_GPIO, 0);
152
153 /* BidCos reset - low = reset */
154 /*gpio_direction_output(BIDCOS_RST_GPIO, 0);*/
155
156#if !defined(CONFIG_B_SAMPLE)
157 /* Z-Wave reset - low = reset */
158 gpio_direction_output(Z_WAVE_RST_GPIO, 0);
159#endif
160
161 /* EnOcean reset - high = reset*/
162 gpio_direction_output(ENOC_RST_GPIO, 1);
163}
164
165/*
166 * Function to set the LEDs in the state "Bootloader booting"
167 */
168static void __maybe_unused leds_set_booting(void)
169{
170#if defined(CONFIG_B_SAMPLE)
171
172 /* Turn all red LEDs on */
173 gpio_direction_output(LED_PWR_RD_GPIO, 1);
174 gpio_direction_output(LED_CONN_RD_GPIO, 1);
175
176#else /* All other SHCs starting with B2-Sample */
177 /* Set the PWM GPIO */
178 gpio_direction_output(LED_PWM_GPIO, 1);
179 /* Turn all red LEDs on */
180 gpio_direction_output(LED_PWR_RD_GPIO, 1);
181 gpio_direction_output(LED_LAN_RD_GPIO, 1);
182 gpio_direction_output(LED_CLOUD_RD_GPIO, 1);
183
184#endif
185}
186
187/*
188 * Function to set the LEDs in the state "Bootloader error"
189 */
190static void leds_set_failure(int state)
191{
192#if defined(CONFIG_B_SAMPLE)
193 /* Turn all blue and green LEDs off */
194 gpio_set_value(LED_PWR_BL_GPIO, 0);
195 gpio_set_value(LED_PWR_GN_GPIO, 0);
196 gpio_set_value(LED_CONN_BL_GPIO, 0);
197 gpio_set_value(LED_CONN_GN_GPIO, 0);
198
199 /* Turn all red LEDs to 'state' */
200 gpio_set_value(LED_PWR_RD_GPIO, state);
201 gpio_set_value(LED_CONN_RD_GPIO, state);
202
203#else /* All other SHCs starting with B2-Sample */
204 /* Set the PWM GPIO */
205 gpio_direction_output(LED_PWM_GPIO, 1);
206
207 /* Turn all blue LEDs off */
208 gpio_set_value(LED_PWR_BL_GPIO, 0);
209 gpio_set_value(LED_LAN_BL_GPIO, 0);
210 gpio_set_value(LED_CLOUD_BL_GPIO, 0);
211
212 /* Turn all red LEDs to 'state' */
213 gpio_set_value(LED_PWR_RD_GPIO, state);
214 gpio_set_value(LED_LAN_RD_GPIO, state);
215 gpio_set_value(LED_CLOUD_RD_GPIO, state);
216#endif
217}
218
219/*
220 * Function to set the LEDs in the state "Bootloader finished"
221 */
222static void leds_set_finish(void)
223{
224#if defined(CONFIG_B_SAMPLE)
225 /* Turn all LEDs off */
226 gpio_set_value(LED_PWR_BL_GPIO, 0);
227 gpio_set_value(LED_PWR_RD_GPIO, 0);
228 gpio_set_value(LED_PWR_GN_GPIO, 0);
229 gpio_set_value(LED_CONN_BL_GPIO, 0);
230 gpio_set_value(LED_CONN_RD_GPIO, 0);
231 gpio_set_value(LED_CONN_GN_GPIO, 0);
232#else /* All other SHCs starting with B2-Sample */
233 /* Turn all LEDs off */
234 gpio_set_value(LED_PWR_BL_GPIO, 0);
235 gpio_set_value(LED_PWR_RD_GPIO, 0);
236 gpio_set_value(LED_LAN_BL_GPIO, 0);
237 gpio_set_value(LED_LAN_RD_GPIO, 0);
238 gpio_set_value(LED_CLOUD_BL_GPIO, 0);
239 gpio_set_value(LED_CLOUD_RD_GPIO, 0);
240
241 /* Turn off the PWM GPIO and mux it to EHRPWM */
242 gpio_set_value(LED_PWM_GPIO, 0);
243 enable_shc_board_pwm_pin_mux();
244#endif
245}
246
247static void check_button_status(void)
248{
249 ulong value;
250 gpio_direction_input(FRONT_BUTTON_GPIO);
251 value = gpio_get_value(FRONT_BUTTON_GPIO);
252
253 if (value == 0) {
254 printf("front button activated !\n");
Simon Glass6a38e412017-08-03 12:22:09 -0600255 env_set("harakiri", "1");
Heiko Schocherf1163962016-06-07 08:31:25 +0200256 }
257}
258
Heiko Schocher5c944142019-01-21 06:16:28 +0100259#if defined(CONFIG_SPL_BUILD)
Heiko Schocherf1163962016-06-07 08:31:25 +0200260#ifdef CONFIG_SPL_OS_BOOT
261int spl_start_uboot(void)
262{
263 return 1;
264}
265#endif
266
267static void shc_board_early_init(void)
268{
269 shc_request_gpio();
270# ifdef CONFIG_SHC_ICT
271 /* Force all modules into enabled state for ICT testing */
272 force_modules_running();
273# else
274 /* Force all modules to enter Reset state until released by the OS */
275 force_modules_reset();
276# endif
277 leds_set_booting();
278}
279
Heiko Schocher5c944142019-01-21 06:16:28 +0100280static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
281
Heiko Schocherf1163962016-06-07 08:31:25 +0200282#define MPU_SPREADING_PERMILLE 18 /* Spread 1.8 percent */
283#define OSC (V_OSCK/1000000)
284/* Bosch: Predivider must be fixed to 4, so N = 4-1 */
285#define MPUPLL_N (4-1)
286/* Bosch: Fref = 24 MHz / (N+1) = 24 MHz / 4 = 6 MHz */
287#define MPUPLL_FREF (OSC / (MPUPLL_N + 1))
288
289const struct dpll_params dpll_ddr_shc = {
290 400, OSC-1, 1, -1, -1, -1, -1};
291
292const struct dpll_params *get_dpll_ddr_params(void)
293{
294 return &dpll_ddr_shc;
295}
296
297/*
298 * As we enabled downspread SSC with 1.8%, the values needed to be corrected
299 * such that the 20% overshoot will not lead to too high frequencies.
300 * In all cases, this is achieved by subtracting one from M (6 MHz less).
301 * Example: 600 MHz CPU
302 * Step size: 24 MHz OSC, N = 4 (fix) --> Fref = 6 MHz
303 * 600 MHz - 6 MHz (1x Fref) = 594 MHz
304 * SSC: 594 MHz * 1.8% = 10.7 MHz SSC
305 * Overshoot: 10.7 MHz * 20 % = 2.2 MHz
306 * --> Fmax = 594 MHz + 2.2 MHz = 596.2 MHz, lower than 600 MHz --> OK!
307 */
308const struct dpll_params dpll_mpu_shc_opp100 = {
309 99, MPUPLL_N, 1, -1, -1, -1, -1};
310
311void am33xx_spl_board_init(void)
312{
313 int sil_rev;
314 int mpu_vdd;
315
316 puts(BOARD_ID_STR);
317
318 /*
319 * Set CORE Frequency to OPP100
320 * Hint: DCDC3 (CORE) defaults to 1.100V (for OPP100)
321 */
322 do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
323
324 sil_rev = readl(&cdev->deviceid) >> 28;
325 if (sil_rev < 2) {
326 puts("We do not support Silicon Revisions below 2.0!\n");
327 return;
328 }
329
330 dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
331 if (i2c_probe(TPS65217_CHIP_PM))
332 return;
333
334 /*
335 * Retrieve the CPU max frequency by reading the efuse
336 * SHC-Default: 600 MHz
337 */
338 switch (dpll_mpu_opp100.m) {
339 case MPUPLL_M_1000:
340 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
341 break;
342 case MPUPLL_M_800:
343 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
344 break;
345 case MPUPLL_M_720:
346 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1200MV;
347 break;
348 case MPUPLL_M_600:
349 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1100MV;
350 break;
351 case MPUPLL_M_300:
352 mpu_vdd = TPS65217_DCDC_VOLT_SEL_950MV;
353 break;
354 default:
355 puts("Cannot determine the frequency, failing!\n");
356 return;
357 }
358
359 if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
360 puts("tps65217_voltage_update failure\n");
361 return;
362 }
363
364 /* Set MPU Frequency to what we detected */
365 printf("MPU reference clock runs at %d MHz\n", MPUPLL_FREF);
366 printf("Setting MPU clock to %d MHz\n", MPUPLL_FREF *
367 dpll_mpu_shc_opp100.m);
368 do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_shc_opp100);
369
370 /* Enable Spread Spectrum for this freq to be clean on EMI side */
371 set_mpu_spreadspectrum(MPU_SPREADING_PERMILLE);
372
373 /*
374 * Using the default voltages for the PMIC (TPS65217D)
375 * LS1 = 1.8V (VDD_1V8)
376 * LS2 = 3.3V (VDD_3V3A)
377 * LDO1 = 1.8V (VIO and VRTC)
378 * LDO2 = 3.3V (VDD_3V3AUX)
379 */
380 shc_board_early_init();
381}
382
383void set_uart_mux_conf(void)
384{
385 enable_uart0_pin_mux();
386}
387
388void set_mux_conf_regs(void)
389{
390 enable_shc_board_pin_mux();
391}
392
393const struct ctrl_ioregs ioregs_evmsk = {
394 .cm0ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
395 .cm1ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
396 .cm2ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
397 .dt0ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
398 .dt1ioctl = MT41K256M16HA125E_IOCTRL_VALUE,
399};
400
401static const struct ddr_data ddr3_shc_data = {
402 .datardsratio0 = MT41K256M16HA125E_RD_DQS,
403 .datawdsratio0 = MT41K256M16HA125E_WR_DQS,
404 .datafwsratio0 = MT41K256M16HA125E_PHY_FIFO_WE,
405 .datawrsratio0 = MT41K256M16HA125E_PHY_WR_DATA,
406};
407
408static const struct cmd_control ddr3_shc_cmd_ctrl_data = {
409 .cmd0csratio = MT41K256M16HA125E_RATIO,
410 .cmd0iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
411
412 .cmd1csratio = MT41K256M16HA125E_RATIO,
413 .cmd1iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
414
415 .cmd2csratio = MT41K256M16HA125E_RATIO,
416 .cmd2iclkout = MT41K256M16HA125E_INVERT_CLKOUT,
417};
418
419static struct emif_regs ddr3_shc_emif_reg_data = {
420 .sdram_config = MT41K256M16HA125E_EMIF_SDCFG,
421 .ref_ctrl = MT41K256M16HA125E_EMIF_SDREF,
422 .sdram_tim1 = MT41K256M16HA125E_EMIF_TIM1,
423 .sdram_tim2 = MT41K256M16HA125E_EMIF_TIM2,
424 .sdram_tim3 = MT41K256M16HA125E_EMIF_TIM3,
425 .zq_config = MT41K256M16HA125E_ZQ_CFG,
426 .emif_ddr_phy_ctlr_1 = MT41K256M16HA125E_EMIF_READ_LATENCY |
427 PHY_EN_DYN_PWRDN,
428};
429
430void sdram_init(void)
431{
432 /* Configure the DDR3 RAM */
433 config_ddr(400, &ioregs_evmsk, &ddr3_shc_data,
434 &ddr3_shc_cmd_ctrl_data, &ddr3_shc_emif_reg_data, 0);
435}
436#endif
437
438/*
439 * Basic board specific setup. Pinmux has been handled already.
440 */
441int board_init(void)
442{
443#if defined(CONFIG_HW_WATCHDOG)
444 hw_watchdog_init();
445#endif
446 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
447 if (read_eeprom() < 0)
448 puts("EEPROM Content Invalid.\n");
449
450 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
Miquel Raynald0935362019-10-03 19:50:03 +0200451#if defined(CONFIG_NOR) || defined(CONFIG_MTD_RAW_NAND)
Heiko Schocherf1163962016-06-07 08:31:25 +0200452 gpmc_init();
453#endif
454 shc_request_gpio();
455
456 return 0;
457}
458
459#ifdef CONFIG_BOARD_LATE_INIT
460int board_late_init(void)
461{
462 check_button_status();
463#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
464 if (shc_eeprom_valid)
465 if (is_valid_ethaddr(header.mac_addr))
Simon Glass8551d552017-08-03 12:22:11 -0600466 eth_env_set_enetaddr("ethaddr", header.mac_addr);
Heiko Schocherf1163962016-06-07 08:31:25 +0200467#endif
468
469 return 0;
470}
Heiko Schocherf1163962016-06-07 08:31:25 +0200471#endif
472
Heiko Schocherf1163962016-06-07 08:31:25 +0200473#if defined(CONFIG_USB_ETHER) && \
Faiz Abbasc01553b2018-02-16 21:17:44 +0530474 (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_USB_ETHER))
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900475int board_eth_init(struct bd_info *bis)
Heiko Schocher5c944142019-01-21 06:16:28 +0100476{
477 return usb_eth_initialize(bis);
Heiko Schocherf1163962016-06-07 08:31:25 +0200478}
479#endif
480
Heiko Schocherf1163962016-06-07 08:31:25 +0200481#ifdef CONFIG_SHOW_BOOT_PROGRESS
482static void bosch_check_reset_pin(void)
483{
484 if (readl(GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0) & RESET_MASK) {
485 printf("Resetting ...\n");
486 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0);
487 disable_interrupts();
488 reset_cpu(0);
489 /*NOTREACHED*/
490 }
491}
492
493static void hang_bosch(const char *cause, int code)
494{
495 int lv;
496
497 gpio_direction_input(RESET_GPIO);
498
499 /* Enable reset pin interrupt on falling edge */
500 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_IRQSTATUS_SET_0);
501 writel(RESET_MASK, GPIO1_BASE + OMAP_GPIO_FALLINGDETECT);
502 enable_interrupts();
503
504 puts(cause);
505 for (;;) {
506 for (lv = 0; lv < code; lv++) {
507 bosch_check_reset_pin();
508 leds_set_failure(1);
509 __udelay(150 * 1000);
510 leds_set_failure(0);
511 __udelay(150 * 1000);
512 }
513#if defined(BLINK_CODE)
514 __udelay(300 * 1000);
515#endif
516 }
517}
518
519void show_boot_progress(int val)
520{
521 switch (val) {
522 case BOOTSTAGE_ID_NEED_RESET:
523 hang_bosch("need reset", 4);
524 break;
525 }
526}
Heiko Schocherf1163962016-06-07 08:31:25 +0200527
528void arch_preboot_os(void)
529{
530 leds_set_finish();
531}
Heiko Schocherf1163962016-06-07 08:31:25 +0200532#endif