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