3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
6 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
8 * Based on da830evm.c. Original Copyrights follow:
10 * Copyright (C) 2009 Nick Thompson, GE Fanuc, Ltd. <nick.thompson@gefanuc.com>
11 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
13 * SPDX-License-Identifier: GPL-2.0+
18 #include <environment.h>
27 #include <linux/ctype.h>
30 #include <asm/arch/da850_lowlevel.h>
31 #include <asm/arch/davinci_misc.h>
32 #include <asm/arch/emif_defs.h>
33 #include <asm/arch/emac_defs.h>
34 #include <asm/arch/gpio.h>
35 #include <asm/arch/pinmux_defs.h>
36 #include <asm/arch/hardware.h>
37 #include <asm/arch/sdmmc_defs.h>
38 #include <asm/arch/timer_defs.h>
40 DECLARE_GLOBAL_DATA_PTR;
42 const struct lpsc_resource lpsc[] = {
43 { DAVINCI_LPSC_AEMIF },
44 { DAVINCI_LPSC_SPI1 },
45 { DAVINCI_LPSC_ARM_RAM_ROM },
46 { DAVINCI_LPSC_UART0 },
47 { DAVINCI_LPSC_EMAC },
48 { DAVINCI_LPSC_UART0 },
49 { DAVINCI_LPSC_GPIO },
50 { DAVINCI_LPSC_DDR_EMIF },
51 { DAVINCI_LPSC_UART1 },
52 { DAVINCI_LPSC_UART2 },
53 { DAVINCI_LPSC_MMC_SD1 },
54 { DAVINCI_LPSC_USB20 },
55 { DAVINCI_LPSC_USB11 },
58 const int lpsc_size = ARRAY_SIZE(lpsc);
60 static const struct pinmux_config enbw_pins[] = {
100 { pinmux(13), 8, 6 },
101 { pinmux(13), 8, 7 },
102 { pinmux(14), 8, 0 },
103 { pinmux(14), 8, 1 },
104 { pinmux(16), 8, 1 },
105 { pinmux(16), 8, 2 },
106 { pinmux(16), 8, 3 },
107 { pinmux(16), 8, 4 },
108 { pinmux(16), 8, 5 },
109 { pinmux(16), 8, 6 },
110 { pinmux(16), 8, 7 },
111 { pinmux(17), 1, 0 },
112 { pinmux(17), 1, 1 },
113 { pinmux(17), 1, 2 },
114 { pinmux(17), 8, 3 },
115 { pinmux(17), 8, 4 },
116 { pinmux(17), 8, 5 },
117 { pinmux(17), 8, 6 },
118 { pinmux(17), 8, 7 },
119 { pinmux(18), 8, 0 },
120 { pinmux(18), 8, 1 },
121 { pinmux(18), 2, 2 },
122 { pinmux(18), 2, 3 },
123 { pinmux(18), 2, 4 },
124 { pinmux(18), 8, 6 },
125 { pinmux(18), 8, 7 },
126 { pinmux(19), 8, 0 },
127 { pinmux(19), 2, 1 },
128 { pinmux(19), 2, 2 },
129 { pinmux(19), 2, 3 },
130 { pinmux(19), 2, 4 },
131 { pinmux(19), 8, 5 },
132 { pinmux(19), 8, 6 },
135 const struct pinmux_resource pinmuxes[] = {
136 PINMUX_ITEM(emac_pins_mii),
137 PINMUX_ITEM(emac_pins_mdio),
138 PINMUX_ITEM(i2c0_pins),
139 PINMUX_ITEM(emifa_pins_cs2),
140 PINMUX_ITEM(emifa_pins_cs3),
141 PINMUX_ITEM(emifa_pins_cs4),
142 PINMUX_ITEM(emifa_pins_nand),
143 PINMUX_ITEM(emifa_pins_nor),
144 PINMUX_ITEM(spi1_pins_base),
145 PINMUX_ITEM(spi1_pins_scs0),
146 PINMUX_ITEM(uart1_pins_txrx),
147 PINMUX_ITEM(uart2_pins_txrx),
148 PINMUX_ITEM(uart2_pins_rtscts),
149 PINMUX_ITEM(enbw_pins),
152 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
155 char name[GPIO_NAME_SIZE];
162 static const struct gpio_config enbw_gpio_config_hut[] = {
163 { "RS485 enable", 8, 11, 1, 0 },
164 { "RS485 iso", 8, 10, 1, 1 },
165 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
166 { "W2HUT RS485 iso", 8, 8, 1, 1 },
169 static const struct gpio_config enbw_gpio_config_w[] = {
170 { "RS485 enable", 8, 11, 1, 0 },
171 { "RS485 iso", 8, 10, 1, 0 },
172 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
173 { "W2HUT RS485 iso", 8, 8, 1, 0 },
176 static const struct gpio_config enbw_gpio_config[] = {
177 { "LAN reset", 7, 15, 1, 1 },
178 { "ena 11V PLC", 7, 14, 1, 0 },
179 { "ena 1.5V PLC", 7, 13, 1, 0 },
180 { "disable VBUS", 7, 12, 1, 1 },
181 { "PLC reset", 6, 13, 1, 0 },
182 { "LCM RS", 6, 12, 1, 0 },
183 { "LCM R/W", 6, 11, 1, 0 },
184 { "PLC pairing", 6, 10, 1, 1 },
185 { "PLC MDIO CLK", 6, 9, 1, 0 },
186 { "HK218", 6, 8, 1, 0 },
187 { "HK218 Rx", 6, 1, 1, 1 },
188 { "TPM reset", 6, 0, 1, 0 },
189 { "Board-Type", 3, 9, 0, 0 },
190 { "HW-ID0", 2, 7, 0, 0 },
191 { "HW-ID1", 2, 6, 0, 0 },
192 { "HW-ID2", 2, 3, 0, 0 },
193 { "PV-IF RxD ena", 0, 15, 1, 1 },
194 { "LED1", 1, 15, 1, 1 },
195 { "LED2", 0, 1, 1, 1 },
196 { "LED3", 0, 2, 1, 1 },
197 { "LED4", 0, 3, 1, 1 },
198 { "LED5", 0, 4, 1, 1 },
199 { "LED6", 0, 5, 1, 0 },
200 { "LED7", 0, 6, 1, 0 },
201 { "LED8", 0, 14, 1, 0 },
202 { "USER1", 0, 12, 0, 0 },
203 { "USER2", 0, 13, 0, 0 },
206 #define PHY_POWER 0x0800
208 static void enbw_cmc_switch(int port, int on)
211 unsigned char phyaddr = 3;
212 unsigned char reg = 0;
218 devname = miiphy_get_current_dev();
220 printf("Error: no mii device\n");
223 if (miiphy_read(devname, phyaddr, reg, &data) != 0) {
224 printf("Error reading from the PHY addr=%02x reg=%02x\n",
234 if (miiphy_write(devname, phyaddr, reg, data) != 0) {
235 printf("Error writing to the PHY addr=%02x reg=%02x\n",
241 static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz)
245 for (i = 0; i < sz; i++) {
246 int gpio = conf[i].bank * 16 +
249 ret = gpio_request(gpio, conf[i].name);
251 printf("%s: Could not get %s gpio\n", __func__,
257 gpio_direction_output(gpio,
260 gpio_direction_input(gpio);
268 int board_type, hw_id;
270 #ifndef CONFIG_USE_IRQ
273 /* address of boot parameters, not used as booting with DTT */
274 gd->bd->bi_boot_params = 0;
276 enbw_cmc_init_gpio(enbw_gpio_config, ARRAY_SIZE(enbw_gpio_config));
278 /* detect HW version */
279 board_type = gpio_get_value(CONFIG_ENBW_CMC_BOARD_TYPE);
280 hw_id = gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT0) +
281 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT1) << 1) +
282 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT2) << 2);
283 printf("BOARD: CMC-%s hw id: %d\n", (board_type ? "w2" : "hut"),
286 enbw_cmc_init_gpio(enbw_gpio_config_w,
287 ARRAY_SIZE(enbw_gpio_config_w));
289 enbw_cmc_init_gpio(enbw_gpio_config_hut,
290 ARRAY_SIZE(enbw_gpio_config_hut));
292 /* setup the SUSPSRC for ARM to control emulation suspend */
293 clrbits_le32(&davinci_syscfg_regs->suspsrc,
294 (DAVINCI_SYSCFG_SUSPSRC_EMAC | DAVINCI_SYSCFG_SUSPSRC_I2C |
295 DAVINCI_SYSCFG_SUSPSRC_SPI1 | DAVINCI_SYSCFG_SUSPSRC_TIMER0 |
296 DAVINCI_SYSCFG_SUSPSRC_UART2));
301 #ifdef CONFIG_DRIVER_TI_EMAC
303 #define KSZ_CMD_READ 0x03
304 #define KSZ_CMD_WRITE 0x02
307 static int enbw_cmc_switch_read(struct spi_slave *spi, u8 reg, u8 *val)
309 unsigned long flags = SPI_XFER_BEGIN;
314 cmd[0] = KSZ_CMD_READ;
318 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
320 debug("Failed to send command (%zu bytes): %d\n",
324 flags |= SPI_XFER_END;
327 ret = spi_xfer(spi, cmd_len * 8, NULL, val, flags);
329 debug("Failed to read (%zu bytes): %d\n",
337 static int enbw_cmc_switch_read_ident(struct spi_slave *spi)
342 ret = enbw_cmc_switch_read(spi, 0, &val);
344 debug("Failed to read\n");
354 static int enbw_cmc_switch_write(struct spi_slave *spi, unsigned long reg,
357 unsigned long flags = SPI_XFER_BEGIN;
362 cmd[0] = KSZ_CMD_WRITE;
366 flags |= SPI_XFER_END;
368 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
370 debug("Failed to send command (%zu bytes): %d\n",
376 ret = enbw_cmc_switch_read(spi, reg, &cmd[0]);
378 debug("Failed to read\n");
382 debug("warning: reg: %lx va: %x soll: %lx\n",
388 static int enbw_cmc_eof(unsigned char *ptr)
396 static char *enbw_cmc_getnewline(char *ptr)
398 while (*ptr != 0x0a) {
400 if (enbw_cmc_eof((unsigned char *)ptr))
408 static char *enbw_cmc_getvalue(char *ptr, int *value)
418 if ((*ptr == '#') || (*ptr == ';')) {
419 ptr = enbw_cmc_getnewline(ptr);
423 if (isxdigit(*ptr)) {
425 } else if (*ptr == 0x0a) {
430 if (enbw_cmc_eof((unsigned char *)ptr))
437 *value = (int)simple_strtoul((const char *)ptr, &ptr, 16);
442 static struct spi_slave *enbw_cmc_init_spi(void)
444 struct spi_slave *spi;
447 spi = spi_setup_slave(0, 0, 1000000, 0);
449 printf("Failed to set up slave\n");
453 ret = spi_claim_bus(spi);
455 debug("Failed to claim SPI bus: %d\n", ret);
459 ret = enbw_cmc_switch_read_ident(spi);
465 spi_release_bus(spi);
471 static int enbw_cmc_config_switch(unsigned long addr)
473 struct spi_slave *spi;
474 char *ptr = (char *)addr;
478 debug("configure switch with file on addr: 0x%lx\n", addr);
480 spi = enbw_cmc_init_spi();
484 while (ptr != NULL) {
485 ptr = enbw_cmc_getvalue(ptr, ®);
487 ptr = enbw_cmc_getvalue(ptr, &value);
488 if ((ptr != NULL) && (value >= 0))
489 if (enbw_cmc_switch_write(spi, reg, value)) {
490 /* error writing to switch */
497 spi_release_bus(spi);
502 static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
507 return cmd_usage(cmdtp);
509 addr = simple_strtoul(argv[1], NULL, 16);
510 enbw_cmc_config_switch(addr);
515 U_BOOT_CMD(switch, 3, 1, do_switch,
521 * Initializes on-board ethernet controllers.
523 int board_eth_init(bd_t *bis)
525 struct spi_slave *spi;
530 davinci_emac_mii_mode_sel(0);
532 /* send a config file to the switch */
533 s = hwconfig_subarg("switch", "config", &len);
535 unsigned long addr = simple_strtoul(s, NULL, 16);
537 config = enbw_cmc_config_switch(addr);
542 * no valid config file -> do we have some args in
545 if ((hwconfig_subarg("switch", "lan", &len)) ||
546 (hwconfig_subarg("switch", "lmn", &len))) {
547 /* If so start switch */
548 spi = enbw_cmc_init_spi();
550 if (enbw_cmc_switch_write(spi, 1, 0))
553 if (enbw_cmc_switch_write(spi, 1, 1))
555 spi_release_bus(spi);
562 if (!davinci_emac_initialize()) {
563 printf("Error: Ethernet init failed!\n");
568 if (hwconfig_subarg_cmp("switch", "lan", "on"))
569 /* Switch port lan on */
570 enbw_cmc_switch(1, 1);
572 enbw_cmc_switch(1, 0);
574 if (hwconfig_subarg_cmp("switch", "lmn", "on"))
575 /* Switch port pwl on */
576 enbw_cmc_switch(2, 1);
578 enbw_cmc_switch(2, 0);
583 #endif /* CONFIG_DRIVER_TI_EMAC */
585 #ifdef CONFIG_PREBOOT
586 static uchar kbd_magic_prefix[] = "key_magic_";
587 static uchar kbd_command_prefix[] = "key_cmd_";
593 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
596 kbd_data->s1 = gpio_get_value(12) +
597 (gpio_get_value(13) << 1);
601 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
605 if (s1 >= '0' && s1 <= '9')
607 else if (s1 >= 'a' && s1 <= 'f')
609 else if (s1 >= 'A' && s1 <= 'F')
614 if (s1 != kbd_data->s1)
620 static char *key_match(const struct kbd_data_t *kbd_data)
622 char magic[sizeof(kbd_magic_prefix) + 1];
624 char *kbd_magic_keys;
627 * The following string defines the characters that can be appended
628 * to "key_magic" to form the names of environment variables that
629 * hold "magic" key codes, i. e. such key codes that can cause
630 * pre-boot actions. If the string is empty (""), then only
631 * "key_magic" is checked (old behaviour); the string "125" causes
632 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
634 kbd_magic_keys = getenv("magic_keys");
635 if (kbd_magic_keys == NULL)
639 * loop over all magic keys;
640 * use '\0' suffix in case of empty string
642 for (suffix = kbd_magic_keys; *suffix ||
643 suffix == kbd_magic_keys; ++suffix) {
644 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
646 if (compare_magic(kbd_data, getenv(magic)) == 0) {
647 char cmd_name[sizeof(kbd_command_prefix) + 1];
650 sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
651 cmd = getenv(cmd_name);
659 #endif /* CONFIG_PREBOOT */
661 int misc_init_r(void)
664 #ifdef CONFIG_PREBOOT
665 struct kbd_data_t kbd_data;
667 char *str = strdup(key_match(get_keys(&kbd_data)));
668 /* Set or delete definition */
669 setenv("preboot", str);
671 #endif /* CONFIG_PREBOOT */
673 /* count all restarts, and save this in an environment var */
674 s = getenv("restartcount");
677 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
681 setenv("restartcount", buf);
684 #ifdef CONFIG_HW_WATCHDOG
685 davinci_hw_watchdog_enable();
697 struct cmc_led led_table[] = {
708 static int cmc_get_led_state(struct cmc_led *led)
711 int gpio = led->bank * 16 + led->gpio;
713 value = gpio_get_value(gpio);
718 static int cmc_set_led_state(struct cmc_led *led, int state)
720 int gpio = led->bank * 16 + led->gpio;
722 gpio_set_value(gpio, state);
726 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
732 int len = ARRAY_SIZE(led_table);
735 return cmd_usage(cmdtp);
741 while ((!found) && (i < len)) {
742 if (strcmp(argv[1], led->name) == 0) {
750 return cmd_usage(cmdtp);
753 if (cmc_get_led_state(led))
760 if (strcmp(argv[2], "on") == 0)
761 cmc_set_led_state(led, 1);
763 cmc_set_led_state(led, 0);
768 U_BOOT_CMD(led, 3, 1, do_led,
769 "switch on/off board led",
773 #ifdef CONFIG_HW_WATCHDOG
774 void hw_watchdog_reset(void)
776 davinci_hw_watchdog_reset();
780 #if defined(CONFIG_POST)
781 void arch_memory_failure_handle(void)
783 struct davinci_gpio *gpio = davinci_gpio_bank01;
787 * if memor< failure blink with the LED 1,2 and 3
788 * as we running from flash, we cannot use the gpio
789 * api here, so access the gpio pin direct through
794 clrbits_le32(&gpio->out_data, 0x80000006);
797 setbits_le32(&gpio->out_data, 0x80000006);
805 ulong post_word_load(void)
807 struct davinci_rtc *reg =
808 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
810 return in_be32(®->scratch2);
813 void post_word_store(ulong value)
815 struct davinci_rtc *reg =
816 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
819 * write RTC kick register to enable write
820 * for RTC Scratch registers. Cratch0 and 1 are
821 * used for bootcount values.
823 writel(RTC_KICK0R_WE, ®->kick0r);
824 writel(RTC_KICK1R_WE, ®->kick1r);
825 out_be32(®->scratch2, value);
828 void board_gpio_init(void)
830 struct davinci_gpio *gpio = davinci_gpio_bank01;
833 * set LED (gpio Interface not usable here)
834 * set LED pins to output and state 0
836 clrbits_le32(&gpio->dir, 0x8000407e);
837 clrbits_le32(&gpio->out_data, 0x8000407e);
838 /* set LED 1 - 5 to state on */
839 setbits_le32(&gpio->out_data, 0x8000001e);
842 * set some gpio pins to low, this is needed early,
843 * so we have no gpio Interface here
845 * 8[8] Mode PV select low
846 * 8[9] Debug Rx Enable low
847 * 8[10] Mode Select PV low
848 * 8[11] Counter Interface RS485 Rx-Enable low
850 gpio = davinci_gpio_bank8;
851 clrbits_le32(&gpio->dir, 0x00000f00);
852 clrbits_le32(&gpio->out_data, 0x0f00);
855 int board_late_init(void)
857 cmc_set_led_state(&led_table[4], 0);
862 void show_boot_progress(int val)
866 cmc_set_led_state(&led_table[4], 1);
869 cmc_set_led_state(&led_table[4], 0);
872 cmc_set_led_state(&led_table[4], 1);
877 #ifdef CONFIG_DAVINCI_MMC
878 static struct davinci_mmc mmc_sd1 = {
879 .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
880 .input_clk = 228000000,
881 .host_caps = MMC_MODE_4BIT,
882 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
883 .version = MMC_CTLR_VERSION_2,
886 int board_mmc_init(bd_t *bis)
888 mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
889 /* Add slot-0 to mmc subsystem */
890 return davinci_mmc_init(bis, &mmc_sd1);