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 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <environment.h>
39 #include <linux/ctype.h>
42 #include <asm/arch/da850_lowlevel.h>
43 #include <asm/arch/davinci_misc.h>
44 #include <asm/arch/emif_defs.h>
45 #include <asm/arch/emac_defs.h>
46 #include <asm/arch/gpio.h>
47 #include <asm/arch/pinmux_defs.h>
48 #include <asm/arch/hardware.h>
49 #include <asm/arch/sdmmc_defs.h>
50 #include <asm/arch/timer_defs.h>
52 DECLARE_GLOBAL_DATA_PTR;
54 const struct lpsc_resource lpsc[] = {
55 { DAVINCI_LPSC_AEMIF },
56 { DAVINCI_LPSC_SPI1 },
57 { DAVINCI_LPSC_ARM_RAM_ROM },
58 { DAVINCI_LPSC_UART0 },
59 { DAVINCI_LPSC_EMAC },
60 { DAVINCI_LPSC_UART0 },
61 { DAVINCI_LPSC_GPIO },
62 { DAVINCI_LPSC_DDR_EMIF },
63 { DAVINCI_LPSC_UART1 },
64 { DAVINCI_LPSC_UART2 },
65 { DAVINCI_LPSC_MMC_SD1 },
66 { DAVINCI_LPSC_USB20 },
67 { DAVINCI_LPSC_USB11 },
70 const int lpsc_size = ARRAY_SIZE(lpsc);
72 static const struct pinmux_config enbw_pins[] = {
108 { pinmux(13), 8, 2 },
109 { pinmux(13), 8, 3 },
110 { pinmux(13), 8, 4 },
111 { pinmux(13), 8, 5 },
112 { pinmux(13), 8, 6 },
113 { pinmux(13), 8, 7 },
114 { pinmux(14), 8, 0 },
115 { pinmux(14), 8, 1 },
116 { pinmux(16), 8, 1 },
117 { pinmux(16), 8, 2 },
118 { pinmux(16), 8, 3 },
119 { pinmux(16), 8, 4 },
120 { pinmux(16), 8, 5 },
121 { pinmux(16), 8, 6 },
122 { pinmux(16), 8, 7 },
123 { pinmux(17), 1, 0 },
124 { pinmux(17), 1, 1 },
125 { pinmux(17), 1, 2 },
126 { pinmux(17), 8, 3 },
127 { pinmux(17), 8, 4 },
128 { pinmux(17), 8, 5 },
129 { pinmux(17), 8, 6 },
130 { pinmux(17), 8, 7 },
131 { pinmux(18), 8, 0 },
132 { pinmux(18), 8, 1 },
133 { pinmux(18), 2, 2 },
134 { pinmux(18), 2, 3 },
135 { pinmux(18), 2, 4 },
136 { pinmux(18), 8, 6 },
137 { pinmux(18), 8, 7 },
138 { pinmux(19), 8, 0 },
139 { pinmux(19), 2, 1 },
140 { pinmux(19), 2, 2 },
141 { pinmux(19), 2, 3 },
142 { pinmux(19), 2, 4 },
143 { pinmux(19), 8, 5 },
144 { pinmux(19), 8, 6 },
147 const struct pinmux_resource pinmuxes[] = {
148 PINMUX_ITEM(emac_pins_mii),
149 PINMUX_ITEM(emac_pins_mdio),
150 PINMUX_ITEM(i2c0_pins),
151 PINMUX_ITEM(emifa_pins_cs2),
152 PINMUX_ITEM(emifa_pins_cs3),
153 PINMUX_ITEM(emifa_pins_cs4),
154 PINMUX_ITEM(emifa_pins_nand),
155 PINMUX_ITEM(emifa_pins_nor),
156 PINMUX_ITEM(spi1_pins_base),
157 PINMUX_ITEM(spi1_pins_scs0),
158 PINMUX_ITEM(uart1_pins_txrx),
159 PINMUX_ITEM(uart2_pins_txrx),
160 PINMUX_ITEM(uart2_pins_rtscts),
161 PINMUX_ITEM(enbw_pins),
164 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
167 char name[GPIO_NAME_SIZE];
174 static const struct gpio_config enbw_gpio_config_hut[] = {
175 { "RS485 enable", 8, 11, 1, 0 },
176 { "RS485 iso", 8, 10, 1, 1 },
177 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
178 { "W2HUT RS485 iso", 8, 8, 1, 1 },
181 static const struct gpio_config enbw_gpio_config_w[] = {
182 { "RS485 enable", 8, 11, 1, 0 },
183 { "RS485 iso", 8, 10, 1, 0 },
184 { "W2HUT RS485 Rx ena", 8, 9, 1, 0 },
185 { "W2HUT RS485 iso", 8, 8, 1, 0 },
188 static const struct gpio_config enbw_gpio_config[] = {
189 { "LAN reset", 7, 15, 1, 1 },
190 { "ena 11V PLC", 7, 14, 1, 0 },
191 { "ena 1.5V PLC", 7, 13, 1, 0 },
192 { "disable VBUS", 7, 12, 1, 1 },
193 { "PLC reset", 6, 13, 1, 0 },
194 { "LCM RS", 6, 12, 1, 0 },
195 { "LCM R/W", 6, 11, 1, 0 },
196 { "PLC pairing", 6, 10, 1, 1 },
197 { "PLC MDIO CLK", 6, 9, 1, 0 },
198 { "HK218", 6, 8, 1, 0 },
199 { "HK218 Rx", 6, 1, 1, 1 },
200 { "TPM reset", 6, 0, 1, 0 },
201 { "Board-Type", 3, 9, 0, 0 },
202 { "HW-ID0", 2, 7, 0, 0 },
203 { "HW-ID1", 2, 6, 0, 0 },
204 { "HW-ID2", 2, 3, 0, 0 },
205 { "PV-IF RxD ena", 0, 15, 1, 1 },
206 { "LED1", 1, 15, 1, 1 },
207 { "LED2", 0, 1, 1, 1 },
208 { "LED3", 0, 2, 1, 1 },
209 { "LED4", 0, 3, 1, 1 },
210 { "LED5", 0, 4, 1, 1 },
211 { "LED6", 0, 5, 1, 0 },
212 { "LED7", 0, 6, 1, 0 },
213 { "LED8", 0, 14, 1, 0 },
214 { "USER1", 0, 12, 0, 0 },
215 { "USER2", 0, 13, 0, 0 },
218 #define PHY_POWER 0x0800
220 static void enbw_cmc_switch(int port, int on)
223 unsigned char phyaddr = 3;
224 unsigned char reg = 0;
230 devname = miiphy_get_current_dev();
232 printf("Error: no mii device\n");
235 if (miiphy_read(devname, phyaddr, reg, &data) != 0) {
236 printf("Error reading from the PHY addr=%02x reg=%02x\n",
246 if (miiphy_write(devname, phyaddr, reg, data) != 0) {
247 printf("Error writing to the PHY addr=%02x reg=%02x\n",
253 static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz)
257 for (i = 0; i < sz; i++) {
258 int gpio = conf[i].bank * 16 +
261 ret = gpio_request(gpio, conf[i].name);
263 printf("%s: Could not get %s gpio\n", __func__,
269 gpio_direction_output(gpio,
272 gpio_direction_input(gpio);
280 int board_type, hw_id;
282 #ifndef CONFIG_USE_IRQ
285 /* address of boot parameters, not used as booting with DTT */
286 gd->bd->bi_boot_params = 0;
288 enbw_cmc_init_gpio(enbw_gpio_config, ARRAY_SIZE(enbw_gpio_config));
290 /* detect HW version */
291 board_type = gpio_get_value(CONFIG_ENBW_CMC_BOARD_TYPE);
292 hw_id = gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT0) +
293 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT1) << 1) +
294 (gpio_get_value(CONFIG_ENBW_CMC_HW_ID_BIT2) << 2);
295 printf("BOARD: CMC-%s hw id: %d\n", (board_type ? "w2" : "hut"),
298 enbw_cmc_init_gpio(enbw_gpio_config_w,
299 ARRAY_SIZE(enbw_gpio_config_w));
301 enbw_cmc_init_gpio(enbw_gpio_config_hut,
302 ARRAY_SIZE(enbw_gpio_config_hut));
304 /* setup the SUSPSRC for ARM to control emulation suspend */
305 clrbits_le32(&davinci_syscfg_regs->suspsrc,
306 (DAVINCI_SYSCFG_SUSPSRC_EMAC | DAVINCI_SYSCFG_SUSPSRC_I2C |
307 DAVINCI_SYSCFG_SUSPSRC_SPI1 | DAVINCI_SYSCFG_SUSPSRC_TIMER0 |
308 DAVINCI_SYSCFG_SUSPSRC_UART2));
313 #ifdef CONFIG_DRIVER_TI_EMAC
315 #define KSZ_CMD_READ 0x03
316 #define KSZ_CMD_WRITE 0x02
319 static int enbw_cmc_switch_read(struct spi_slave *spi, u8 reg, u8 *val)
321 unsigned long flags = SPI_XFER_BEGIN;
326 cmd[0] = KSZ_CMD_READ;
330 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
332 debug("Failed to send command (%zu bytes): %d\n",
336 flags |= SPI_XFER_END;
339 ret = spi_xfer(spi, cmd_len * 8, NULL, val, flags);
341 debug("Failed to read (%zu bytes): %d\n",
349 static int enbw_cmc_switch_read_ident(struct spi_slave *spi)
354 ret = enbw_cmc_switch_read(spi, 0, &val);
356 debug("Failed to read\n");
366 static int enbw_cmc_switch_write(struct spi_slave *spi, unsigned long reg,
369 unsigned long flags = SPI_XFER_BEGIN;
374 cmd[0] = KSZ_CMD_WRITE;
378 flags |= SPI_XFER_END;
380 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
382 debug("Failed to send command (%zu bytes): %d\n",
388 ret = enbw_cmc_switch_read(spi, reg, &cmd[0]);
390 debug("Failed to read\n");
394 debug("warning: reg: %lx va: %x soll: %lx\n",
400 static int enbw_cmc_eof(unsigned char *ptr)
408 static char *enbw_cmc_getnewline(char *ptr)
410 while (*ptr != 0x0a) {
412 if (enbw_cmc_eof((unsigned char *)ptr))
420 static char *enbw_cmc_getvalue(char *ptr, int *value)
430 if ((*ptr == '#') || (*ptr == ';')) {
431 ptr = enbw_cmc_getnewline(ptr);
435 if (isxdigit(*ptr)) {
437 } else if (*ptr == 0x0a) {
442 if (enbw_cmc_eof((unsigned char *)ptr))
449 *value = (int)simple_strtoul((const char *)ptr, &ptr, 16);
454 static struct spi_slave *enbw_cmc_init_spi(void)
456 struct spi_slave *spi;
459 spi = spi_setup_slave(0, 0, 1000000, 0);
461 printf("Failed to set up slave\n");
465 ret = spi_claim_bus(spi);
467 debug("Failed to claim SPI bus: %d\n", ret);
471 ret = enbw_cmc_switch_read_ident(spi);
477 spi_release_bus(spi);
483 static int enbw_cmc_config_switch(unsigned long addr)
485 struct spi_slave *spi;
486 char *ptr = (char *)addr;
490 debug("configure switch with file on addr: 0x%lx\n", addr);
492 spi = enbw_cmc_init_spi();
496 while (ptr != NULL) {
497 ptr = enbw_cmc_getvalue(ptr, ®);
499 ptr = enbw_cmc_getvalue(ptr, &value);
500 if ((ptr != NULL) && (value >= 0))
501 if (enbw_cmc_switch_write(spi, reg, value)) {
502 /* error writing to switch */
509 spi_release_bus(spi);
514 static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
519 return cmd_usage(cmdtp);
521 addr = simple_strtoul(argv[1], NULL, 16);
522 enbw_cmc_config_switch(addr);
527 U_BOOT_CMD(switch, 3, 1, do_switch,
533 * Initializes on-board ethernet controllers.
535 int board_eth_init(bd_t *bis)
537 struct spi_slave *spi;
542 davinci_emac_mii_mode_sel(0);
544 /* send a config file to the switch */
545 s = hwconfig_subarg("switch", "config", &len);
547 unsigned long addr = simple_strtoul(s, NULL, 16);
549 config = enbw_cmc_config_switch(addr);
554 * no valid config file -> do we have some args in
557 if ((hwconfig_subarg("switch", "lan", &len)) ||
558 (hwconfig_subarg("switch", "lmn", &len))) {
559 /* If so start switch */
560 spi = enbw_cmc_init_spi();
562 if (enbw_cmc_switch_write(spi, 1, 0))
565 if (enbw_cmc_switch_write(spi, 1, 1))
567 spi_release_bus(spi);
574 if (!davinci_emac_initialize()) {
575 printf("Error: Ethernet init failed!\n");
580 if (hwconfig_subarg_cmp("switch", "lan", "on"))
581 /* Switch port lan on */
582 enbw_cmc_switch(1, 1);
584 enbw_cmc_switch(1, 0);
586 if (hwconfig_subarg_cmp("switch", "lmn", "on"))
587 /* Switch port pwl on */
588 enbw_cmc_switch(2, 1);
590 enbw_cmc_switch(2, 0);
595 #endif /* CONFIG_DRIVER_TI_EMAC */
597 #ifdef CONFIG_PREBOOT
598 static uchar kbd_magic_prefix[] = "key_magic_";
599 static uchar kbd_command_prefix[] = "key_cmd_";
605 struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data)
608 kbd_data->s1 = gpio_get_value(12) +
609 (gpio_get_value(13) << 1);
613 static int compare_magic(const struct kbd_data_t *kbd_data, char *str)
617 if (s1 >= '0' && s1 <= '9')
619 else if (s1 >= 'a' && s1 <= 'f')
621 else if (s1 >= 'A' && s1 <= 'F')
626 if (s1 != kbd_data->s1)
632 static char *key_match(const struct kbd_data_t *kbd_data)
634 char magic[sizeof(kbd_magic_prefix) + 1];
636 char *kbd_magic_keys;
639 * The following string defines the characters that can be appended
640 * to "key_magic" to form the names of environment variables that
641 * hold "magic" key codes, i. e. such key codes that can cause
642 * pre-boot actions. If the string is empty (""), then only
643 * "key_magic" is checked (old behaviour); the string "125" causes
644 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
646 kbd_magic_keys = getenv("magic_keys");
647 if (kbd_magic_keys == NULL)
651 * loop over all magic keys;
652 * use '\0' suffix in case of empty string
654 for (suffix = kbd_magic_keys; *suffix ||
655 suffix == kbd_magic_keys; ++suffix) {
656 sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
658 if (compare_magic(kbd_data, getenv(magic)) == 0) {
659 char cmd_name[sizeof(kbd_command_prefix) + 1];
662 sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
663 cmd = getenv(cmd_name);
671 #endif /* CONFIG_PREBOOT */
673 int misc_init_r(void)
676 #ifdef CONFIG_PREBOOT
677 struct kbd_data_t kbd_data;
679 char *str = strdup(key_match(get_keys(&kbd_data)));
680 /* Set or delete definition */
681 setenv("preboot", str);
683 #endif /* CONFIG_PREBOOT */
685 /* count all restarts, and save this in an environment var */
686 s = getenv("restartcount");
689 sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1);
693 setenv("restartcount", buf);
696 #ifdef CONFIG_HW_WATCHDOG
697 davinci_hw_watchdog_enable();
709 struct cmc_led led_table[] = {
720 static int cmc_get_led_state(struct cmc_led *led)
723 int gpio = led->bank * 16 + led->gpio;
725 value = gpio_get_value(gpio);
730 static int cmc_set_led_state(struct cmc_led *led, int state)
732 int gpio = led->bank * 16 + led->gpio;
734 gpio_set_value(gpio, state);
738 static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
744 int len = ARRAY_SIZE(led_table);
747 return cmd_usage(cmdtp);
753 while ((!found) && (i < len)) {
754 if (strcmp(argv[1], led->name) == 0) {
762 return cmd_usage(cmdtp);
765 if (cmc_get_led_state(led))
772 if (strcmp(argv[2], "on") == 0)
773 cmc_set_led_state(led, 1);
775 cmc_set_led_state(led, 0);
780 U_BOOT_CMD(led, 3, 1, do_led,
781 "switch on/off board led",
785 #ifdef CONFIG_HW_WATCHDOG
786 void hw_watchdog_reset(void)
788 davinci_hw_watchdog_reset();
792 #if defined(CONFIG_POST)
793 void arch_memory_failure_handle(void)
795 struct davinci_gpio *gpio = davinci_gpio_bank01;
799 * if memor< failure blink with the LED 1,2 and 3
800 * as we running from flash, we cannot use the gpio
801 * api here, so access the gpio pin direct through
806 clrbits_le32(&gpio->out_data, 0x80000006);
809 setbits_le32(&gpio->out_data, 0x80000006);
817 ulong post_word_load(void)
819 struct davinci_rtc *reg =
820 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
822 return in_be32(®->scratch2);
825 void post_word_store(ulong value)
827 struct davinci_rtc *reg =
828 (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR;
831 * write RTC kick register to enable write
832 * for RTC Scratch registers. Cratch0 and 1 are
833 * used for bootcount values.
835 writel(RTC_KICK0R_WE, ®->kick0r);
836 writel(RTC_KICK1R_WE, ®->kick1r);
837 out_be32(®->scratch2, value);
840 void board_gpio_init(void)
842 struct davinci_gpio *gpio = davinci_gpio_bank01;
845 * set LED (gpio Interface not usable here)
846 * set LED pins to output and state 0
848 clrbits_le32(&gpio->dir, 0x8000407e);
849 clrbits_le32(&gpio->out_data, 0x8000407e);
850 /* set LED 1 - 5 to state on */
851 setbits_le32(&gpio->out_data, 0x8000001e);
854 * set some gpio pins to low, this is needed early,
855 * so we have no gpio Interface here
857 * 8[8] Mode PV select low
858 * 8[9] Debug Rx Enable low
859 * 8[10] Mode Select PV low
860 * 8[11] Counter Interface RS485 Rx-Enable low
862 gpio = davinci_gpio_bank8;
863 clrbits_le32(&gpio->dir, 0x00000f00);
864 clrbits_le32(&gpio->out_data, 0x0f00);
867 int board_late_init(void)
869 cmc_set_led_state(&led_table[4], 0);
874 void show_boot_progress(int val)
878 cmc_set_led_state(&led_table[4], 1);
881 cmc_set_led_state(&led_table[4], 0);
884 cmc_set_led_state(&led_table[4], 1);
889 #ifdef CONFIG_DAVINCI_MMC
890 static struct davinci_mmc mmc_sd1 = {
891 .reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD1_BASE,
892 .input_clk = 228000000,
893 .host_caps = MMC_MODE_4BIT,
894 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
895 .version = MMC_CTLR_VERSION_2,
898 int board_mmc_init(bd_t *bis)
900 mmc_sd1.input_clk = clk_get(DAVINCI_MMC_CLKID);
901 /* Add slot-0 to mmc subsystem */
902 return davinci_mmc_init(bis, &mmc_sd1);