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/ti-common/davinci_nand.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>
39 #include <asm/davinci_rtc.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 const struct lpsc_resource lpsc[] = {
44 { DAVINCI_LPSC_AEMIF },
45 { DAVINCI_LPSC_SPI1 },
46 { DAVINCI_LPSC_ARM_RAM_ROM },
47 { DAVINCI_LPSC_UART0 },
48 { DAVINCI_LPSC_EMAC },
49 { DAVINCI_LPSC_UART0 },
50 { DAVINCI_LPSC_GPIO },
51 { DAVINCI_LPSC_DDR_EMIF },
52 { DAVINCI_LPSC_UART1 },
53 { DAVINCI_LPSC_UART2 },
54 { DAVINCI_LPSC_MMC_SD1 },
55 { DAVINCI_LPSC_USB20 },
56 { DAVINCI_LPSC_USB11 },
59 const int lpsc_size = ARRAY_SIZE(lpsc);
61 static const struct pinmux_config enbw_pins[] = {
100 { pinmux(13), 8, 5 },
101 { pinmux(13), 8, 6 },
102 { pinmux(13), 8, 7 },
103 { pinmux(14), 8, 0 },
104 { pinmux(14), 8, 1 },
105 { pinmux(16), 8, 1 },
106 { pinmux(16), 8, 2 },
107 { pinmux(16), 8, 3 },
108 { pinmux(16), 8, 4 },
109 { pinmux(16), 8, 5 },
110 { pinmux(16), 8, 6 },
111 { pinmux(16), 8, 7 },
112 { pinmux(17), 1, 0 },
113 { pinmux(17), 1, 1 },
114 { pinmux(17), 1, 2 },
115 { pinmux(17), 8, 3 },
116 { pinmux(17), 8, 4 },
117 { pinmux(17), 8, 5 },
118 { pinmux(17), 8, 6 },
119 { pinmux(17), 8, 7 },
120 { pinmux(18), 8, 0 },
121 { pinmux(18), 8, 1 },
122 { pinmux(18), 2, 2 },
123 { pinmux(18), 2, 3 },
124 { pinmux(18), 2, 4 },
125 { pinmux(18), 8, 6 },
126 { pinmux(18), 8, 7 },
127 { pinmux(19), 8, 0 },
128 { pinmux(19), 2, 1 },
129 { pinmux(19), 2, 2 },
130 { pinmux(19), 2, 3 },
131 { pinmux(19), 2, 4 },
132 { pinmux(19), 8, 5 },
133 { pinmux(19), 8, 6 },
136 const struct pinmux_resource pinmuxes[] = {
137 PINMUX_ITEM(emac_pins_mii),
138 PINMUX_ITEM(emac_pins_mdio),
139 PINMUX_ITEM(i2c0_pins),
140 PINMUX_ITEM(emifa_pins_cs2),
141 PINMUX_ITEM(emifa_pins_cs3),
142 PINMUX_ITEM(emifa_pins_cs4),
143 PINMUX_ITEM(emifa_pins_nand),
144 PINMUX_ITEM(emifa_pins_nor),
145 PINMUX_ITEM(spi1_pins_base),
146 PINMUX_ITEM(spi1_pins_scs0),
147 PINMUX_ITEM(uart1_pins_txrx),
148 PINMUX_ITEM(uart2_pins_txrx),
149 PINMUX_ITEM(uart2_pins_rtscts),
150 PINMUX_ITEM(enbw_pins),
153 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
156 char name[GPIO_NAME_SIZE];
163 static const struct gpio_config enbw_gpio_config_hut[] = {
164 { "RS485 enable", 8, 11, 1, 0 },
165 { "RS485 iso", 8, 10, 1, 1 },
166 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
167 { "W2HUT RS485 iso", 8, 8, 1, 1 },
170 static const struct gpio_config enbw_gpio_config_w[] = {
171 { "RS485 enable", 8, 11, 1, 0 },
172 { "RS485 iso", 8, 10, 1, 0 },
173 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
174 { "W2HUT RS485 iso", 8, 8, 1, 0 },
177 static const struct gpio_config enbw_gpio_config[] = {
178 { "LAN reset", 7, 15, 1, 1 },
179 { "ena 11V PLC", 7, 14, 1, 0 },
180 { "ena 1.5V PLC", 7, 13, 1, 0 },
181 { "disable VBUS", 7, 12, 1, 1 },
182 { "PLC reset", 6, 13, 1, 0 },
183 { "LCM RS", 6, 12, 1, 0 },
184 { "LCM R/W", 6, 11, 1, 0 },
185 { "PLC pairing", 6, 10, 1, 1 },
186 { "PLC MDIO CLK", 6, 9, 1, 0 },
187 { "HK218", 6, 8, 1, 0 },
188 { "HK218 Rx", 6, 1, 1, 1 },
189 { "TPM reset", 6, 0, 1, 0 },
190 { "Board-Type", 3, 9, 0, 0 },
191 { "HW-ID0", 2, 7, 0, 0 },
192 { "HW-ID1", 2, 6, 0, 0 },
193 { "HW-ID2", 2, 3, 0, 0 },
194 { "PV-IF RxD ena", 0, 15, 1, 1 },
195 { "LED1", 1, 15, 1, 1 },
196 { "LED2", 0, 1, 1, 1 },
197 { "LED3", 0, 2, 1, 1 },
198 { "LED4", 0, 3, 1, 1 },
199 { "LED5", 0, 4, 1, 1 },
200 { "LED6", 0, 5, 1, 0 },
201 { "LED7", 0, 6, 1, 0 },
202 { "LED8", 0, 14, 1, 0 },
203 { "USER1", 0, 12, 0, 0 },
204 { "USER2", 0, 13, 0, 0 },
207 #define PHY_POWER 0x0800
209 static void enbw_cmc_switch(int port, int on)
212 unsigned char phyaddr = 3;
213 unsigned char reg = 0;
219 devname = miiphy_get_current_dev();
221 printf("Error: no mii device\n");
224 if (miiphy_read(devname, phyaddr, reg, &data) != 0) {
225 printf("Error reading from the PHY addr=%02x reg=%02x\n",
235 if (miiphy_write(devname, phyaddr, reg, data) != 0) {
236 printf("Error writing to the PHY addr=%02x reg=%02x\n",
242 static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz)
246 for (i = 0; i < sz; i++) {
247 int gpio = conf[i].bank * 16 +
250 ret = gpio_request(gpio, conf[i].name);
252 printf("%s: Could not get %s gpio\n", __func__,
258 gpio_direction_output(gpio,
261 gpio_direction_input(gpio);
269 int board_type, hw_id;
271 #ifndef CONFIG_USE_IRQ
274 /* address of boot parameters, not used as booting with DTT */
275 gd->bd->bi_boot_params = 0;
277 enbw_cmc_init_gpio(enbw_gpio_config, ARRAY_SIZE(enbw_gpio_config));
279 /* detect HW version */
280 board_type = gpio_get_value(CONFIG_ENBW_CMC_BOARD_TYPE);
281 hw_id = gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT0) +
282 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT1) << 1) +
283 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT2) << 2);
284 printf("BOARD: CMC-%s hw id: %d\n", (board_type ? "w2" : "hut"),
287 enbw_cmc_init_gpio(enbw_gpio_config_w,
288 ARRAY_SIZE(enbw_gpio_config_w));
290 enbw_cmc_init_gpio(enbw_gpio_config_hut,
291 ARRAY_SIZE(enbw_gpio_config_hut));
293 /* setup the SUSPSRC for ARM to control emulation suspend */
294 clrbits_le32(&davinci_syscfg_regs->suspsrc,
295 (DAVINCI_SYSCFG_SUSPSRC_EMAC | DAVINCI_SYSCFG_SUSPSRC_I2C |
296 DAVINCI_SYSCFG_SUSPSRC_SPI1 | DAVINCI_SYSCFG_SUSPSRC_TIMER0 |
297 DAVINCI_SYSCFG_SUSPSRC_UART2));
302 #ifdef CONFIG_DRIVER_TI_EMAC
304 #define KSZ_CMD_READ 0x03
305 #define KSZ_CMD_WRITE 0x02
308 static int enbw_cmc_switch_read(struct spi_slave *spi, u8 reg, u8 *val)
310 unsigned long flags = SPI_XFER_BEGIN;
315 cmd[0] = KSZ_CMD_READ;
319 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
321 debug("Failed to send command (%zu bytes): %d\n",
325 flags |= SPI_XFER_END;
328 ret = spi_xfer(spi, cmd_len * 8, NULL, val, flags);
330 debug("Failed to read (%zu bytes): %d\n",
338 static int enbw_cmc_switch_read_ident(struct spi_slave *spi)
343 ret = enbw_cmc_switch_read(spi, 0, &val);
345 debug("Failed to read\n");
355 static int enbw_cmc_switch_write(struct spi_slave *spi, unsigned long reg,
358 unsigned long flags = SPI_XFER_BEGIN;
363 cmd[0] = KSZ_CMD_WRITE;
367 flags |= SPI_XFER_END;
369 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
371 debug("Failed to send command (%zu bytes): %d\n",
377 ret = enbw_cmc_switch_read(spi, reg, &cmd[0]);
379 debug("Failed to read\n");
383 debug("warning: reg: %lx va: %x soll: %lx\n",
389 static int enbw_cmc_eof(unsigned char *ptr)
397 static char *enbw_cmc_getnewline(char *ptr)
399 while (*ptr != 0x0a) {
401 if (enbw_cmc_eof((unsigned char *)ptr))
409 static char *enbw_cmc_getvalue(char *ptr, int *value)
419 if ((*ptr == '#') || (*ptr == ';')) {
420 ptr = enbw_cmc_getnewline(ptr);
424 if (isxdigit(*ptr)) {
426 } else if (*ptr == 0x0a) {
431 if (enbw_cmc_eof((unsigned char *)ptr))
438 *value = (int)simple_strtoul((const char *)ptr, &ptr, 16);
443 static struct spi_slave *enbw_cmc_init_spi(void)
445 struct spi_slave *spi;
448 spi = spi_setup_slave(0, 0, 1000000, 0);
450 printf("Failed to set up slave\n");
454 ret = spi_claim_bus(spi);
456 debug("Failed to claim SPI bus: %d\n", ret);
460 ret = enbw_cmc_switch_read_ident(spi);
466 spi_release_bus(spi);
472 static int enbw_cmc_config_switch(unsigned long addr)
474 struct spi_slave *spi;
475 char *ptr = (char *)addr;
479 debug("configure switch with file on addr: 0x%lx\n", addr);
481 spi = enbw_cmc_init_spi();
485 while (ptr != NULL) {
486 ptr = enbw_cmc_getvalue(ptr, ®);
488 ptr = enbw_cmc_getvalue(ptr, &value);
489 if ((ptr != NULL) && (value >= 0))
490 if (enbw_cmc_switch_write(spi, reg, value)) {
491 /* error writing to switch */
498 spi_release_bus(spi);
503 static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
508 return cmd_usage(cmdtp);
510 addr = simple_strtoul(argv[1], NULL, 16);
511 enbw_cmc_config_switch(addr);
516 U_BOOT_CMD(switch, 3, 1, do_switch,
522 * Initializes on-board ethernet controllers.
524 int board_eth_init(bd_t *bis)
526 struct spi_slave *spi;
531 davinci_emac_mii_mode_sel(0);
533 /* send a config file to the switch */
534 s = hwconfig_subarg("switch", "config", &len);
536 unsigned long addr = simple_strtoul(s, NULL, 16);
538 config = enbw_cmc_config_switch(addr);
543 * no valid config file -> do we have some args in
546 if ((hwconfig_subarg("switch", "lan", &len)) ||
547 (hwconfig_subarg("switch", "lmn", &len))) {
548 /* If so start switch */
549 spi = enbw_cmc_init_spi();
551 if (enbw_cmc_switch_write(spi, 1, 0))
554 if (enbw_cmc_switch_write(spi, 1, 1))
556 spi_release_bus(spi);
563 if (!davinci_emac_initialize()) {
564 printf("Error: Ethernet init failed!\n");
569 if (hwconfig_subarg_cmp("switch", "lan", "on"))
570 /* Switch port lan on */
571 enbw_cmc_switch(1, 1);
573 enbw_cmc_switch(1, 0);
575 if (hwconfig_subarg_cmp("switch", "lmn", "on"))
576 /* Switch port pwl on */
577 enbw_cmc_switch(2, 1);
579 enbw_cmc_switch(2, 0);
584 #endif /* CONFIG_DRIVER_TI_EMAC */
586 #ifdef CONFIG_PREBOOT
587 static uchar kbd_magic_prefix[] = "key_magic_";
588 static uchar kbd_command_prefix[] = "key_cmd_";
594 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
597 kbd_data->s1 = gpio_get_value(12) +
598 (gpio_get_value(13) << 1);
602 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
606 if (s1 >= '0' && s1 <= '9')
608 else if (s1 >= 'a' && s1 <= 'f')
610 else if (s1 >= 'A' && s1 <= 'F')
615 if (s1 != kbd_data->s1)
621 static char *key_match(const struct kbd_data_t *kbd_data)
623 char magic[sizeof(kbd_magic_prefix) + 1];
625 char *kbd_magic_keys;
628 * The following string defines the characters that can be appended
629 * to "key_magic" to form the names of environment variables that
630 * hold "magic" key codes, i. e. such key codes that can cause
631 * pre-boot actions. If the string is empty (""), then only
632 * "key_magic" is checked (old behaviour); the string "125" causes
633 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
635 kbd_magic_keys = getenv("magic_keys");
636 if (kbd_magic_keys == NULL)
640 * loop over all magic keys;
641 * use '\0' suffix in case of empty string
643 for (suffix = kbd_magic_keys; *suffix ||
644 suffix == kbd_magic_keys; ++suffix) {
645 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
647 if (compare_magic(kbd_data, getenv(magic)) == 0) {
648 char cmd_name[sizeof(kbd_command_prefix) + 1];
651 sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
652 cmd = getenv(cmd_name);
660 #endif /* CONFIG_PREBOOT */
662 int misc_init_r(void)
665 #ifdef CONFIG_PREBOOT
666 struct kbd_data_t kbd_data;
668 char *str = strdup(key_match(get_keys(&kbd_data)));
669 /* Set or delete definition */
670 setenv("preboot", str);
672 #endif /* CONFIG_PREBOOT */
674 /* count all restarts, and save this in an environment var */
675 s = getenv("restartcount");
678 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
682 setenv("restartcount", buf);
685 #ifdef CONFIG_HW_WATCHDOG
686 davinci_hw_watchdog_enable();
698 struct cmc_led led_table[] = {
709 static int cmc_get_led_state(struct cmc_led *led)
712 int gpio = led->bank * 16 + led->gpio;
714 value = gpio_get_value(gpio);
719 static int cmc_set_led_state(struct cmc_led *led, int state)
721 int gpio = led->bank * 16 + led->gpio;
723 gpio_set_value(gpio, state);
727 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
733 int len = ARRAY_SIZE(led_table);
736 return cmd_usage(cmdtp);
742 while ((!found) && (i < len)) {
743 if (strcmp(argv[1], led->name) == 0) {
751 return cmd_usage(cmdtp);
754 if (cmc_get_led_state(led))
761 if (strcmp(argv[2], "on") == 0)
762 cmc_set_led_state(led, 1);
764 cmc_set_led_state(led, 0);
769 U_BOOT_CMD(led, 3, 1, do_led,
770 "switch on/off board led",
774 #ifdef CONFIG_HW_WATCHDOG
775 void hw_watchdog_reset(void)
777 davinci_hw_watchdog_reset();
781 #if defined(CONFIG_POST)
782 void arch_memory_failure_handle(void)
784 struct davinci_gpio *gpio = davinci_gpio_bank01;
788 * if memor< failure blink with the LED 1,2 and 3
789 * as we running from flash, we cannot use the gpio
790 * api here, so access the gpio pin direct through
795 clrbits_le32(&gpio->out_data, 0x80000006);
798 setbits_le32(&gpio->out_data, 0x80000006);
806 ulong post_word_load(void)
808 struct davinci_rtc *reg =
809 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
811 return in_be32(®->scratch2);
814 void post_word_store(ulong value)
816 struct davinci_rtc *reg =
817 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
820 * write RTC kick register to enable write
821 * for RTC Scratch registers. Cratch0 and 1 are
822 * used for bootcount values.
824 writel(RTC_KICK0R_WE, ®->kick0r);
825 writel(RTC_KICK1R_WE, ®->kick1r);
826 out_be32(®->scratch2, value);
829 void board_gpio_init(void)
831 struct davinci_gpio *gpio = davinci_gpio_bank01;
834 * set LED (gpio Interface not usable here)
835 * set LED pins to output and state 0
837 clrbits_le32(&gpio->dir, 0x8000407e);
838 clrbits_le32(&gpio->out_data, 0x8000407e);
839 /* set LED 1 - 5 to state on */
840 setbits_le32(&gpio->out_data, 0x8000001e);
843 * set some gpio pins to low, this is needed early,
844 * so we have no gpio Interface here
846 * 8[8] Mode PV select low
847 * 8[9] Debug Rx Enable low
848 * 8[10] Mode Select PV low
849 * 8[11] Counter Interface RS485 Rx-Enable low
851 gpio = davinci_gpio_bank8;
852 clrbits_le32(&gpio->dir, 0x00000f00);
853 clrbits_le32(&gpio->out_data, 0x0f00);
856 int board_late_init(void)
858 cmc_set_led_state(&led_table[4], 0);
863 void show_boot_progress(int val)
867 cmc_set_led_state(&led_table[4], 1);
870 cmc_set_led_state(&led_table[4], 0);
873 cmc_set_led_state(&led_table[4], 1);
878 #ifdef CONFIG_DAVINCI_MMC
879 static struct davinci_mmc mmc_sd1 = {
880 .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
881 .input_clk = 228000000,
882 .host_caps = MMC_MODE_4BIT,
883 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
884 .version = MMC_CTLR_VERSION_2,
887 int board_mmc_init(bd_t *bis)
889 mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
890 /* Add slot-0 to mmc subsystem */
891 return davinci_mmc_init(bis, &mmc_sd1);