Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 4 | * |
| 5 | * Based on: |
| 6 | * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/ |
| 7 | * |
| 8 | * Based on da830evm.c. Original Copyrights follow: |
| 9 | * |
| 10 | * Copyright (C) 2009 Nick Thompson, GE Fanuc, Ltd. <nick.thompson@gefanuc.com> |
| 11 | * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> |
| 12 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 13 | * SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #include <command.h> |
| 18 | #include <environment.h> |
| 19 | #include <hwconfig.h> |
| 20 | #include <i2c.h> |
| 21 | #include <malloc.h> |
| 22 | #include <miiphy.h> |
| 23 | #include <mmc.h> |
| 24 | #include <net.h> |
| 25 | #include <netdev.h> |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 26 | #include <spi.h> |
| 27 | #include <linux/ctype.h> |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 28 | #include <asm/gpio.h> |
| 29 | #include <asm/io.h> |
| 30 | #include <asm/arch/da850_lowlevel.h> |
| 31 | #include <asm/arch/davinci_misc.h> |
Khoronzhuk, Ivan | 3e01ed0 | 2014-06-07 04:22:52 +0300 | [diff] [blame] | 32 | #include <asm/ti-common/davinci_nand.h> |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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> |
Tom Rini | 155d424 | 2013-08-28 09:00:28 -0400 | [diff] [blame] | 39 | #include <asm/davinci_rtc.h> |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 40 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
Sughosh Ganu | 6b873dc | 2012-02-02 00:44:41 +0000 | [diff] [blame] | 43 | const struct lpsc_resource lpsc[] = { |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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 }, |
| 57 | }; |
| 58 | |
Sughosh Ganu | 6b873dc | 2012-02-02 00:44:41 +0000 | [diff] [blame] | 59 | const int lpsc_size = ARRAY_SIZE(lpsc); |
| 60 | |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 61 | static const struct pinmux_config enbw_pins[] = { |
| 62 | { pinmux(0), 8, 0 }, |
| 63 | { pinmux(0), 8, 1 }, |
| 64 | { pinmux(0), 8, 2 }, |
| 65 | { pinmux(0), 8, 3 }, |
| 66 | { pinmux(0), 8, 4 }, |
| 67 | { pinmux(0), 8, 5 }, |
| 68 | { pinmux(1), 4, 0 }, |
| 69 | { pinmux(1), 8, 1 }, |
| 70 | { pinmux(1), 8, 2 }, |
| 71 | { pinmux(1), 8, 3 }, |
| 72 | { pinmux(1), 8, 4 }, |
| 73 | { pinmux(1), 8, 5 }, |
| 74 | { pinmux(1), 8, 6 }, |
| 75 | { pinmux(1), 4, 7 }, |
| 76 | { pinmux(2), 8, 0 }, |
| 77 | { pinmux(5), 1, 0 }, |
| 78 | { pinmux(5), 1, 3 }, |
| 79 | { pinmux(5), 1, 7 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 80 | { pinmux(5), 1, 5 }, |
| 81 | { pinmux(5), 1, 4 }, |
| 82 | { pinmux(5), 1, 3 }, |
| 83 | { pinmux(5), 1, 2 }, |
| 84 | { pinmux(5), 1, 1 }, |
| 85 | { pinmux(5), 1, 0 }, |
| 86 | { pinmux(6), 8, 0 }, |
| 87 | { pinmux(6), 8, 1 }, |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 88 | { pinmux(6), 8, 2 }, |
| 89 | { pinmux(6), 8, 3 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 90 | { pinmux(6), 8, 4 }, |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 91 | { pinmux(6), 8, 5 }, |
| 92 | { pinmux(6), 1, 7 }, |
| 93 | { pinmux(7), 8, 2 }, |
| 94 | { pinmux(7), 1, 3 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 95 | { pinmux(7), 8, 6 }, |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 96 | { pinmux(7), 1, 7 }, |
| 97 | { pinmux(13), 8, 2 }, |
| 98 | { pinmux(13), 8, 3 }, |
| 99 | { pinmux(13), 8, 4 }, |
| 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 }, |
| 134 | }; |
| 135 | |
| 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), |
| 151 | }; |
| 152 | |
| 153 | const int pinmuxes_size = ARRAY_SIZE(pinmuxes); |
| 154 | |
| 155 | struct gpio_config { |
| 156 | char name[GPIO_NAME_SIZE]; |
| 157 | unsigned char bank; |
| 158 | unsigned char gpio; |
| 159 | unsigned char out; |
| 160 | unsigned char value; |
| 161 | }; |
| 162 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 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 }, |
| 168 | }; |
| 169 | |
| 170 | static const struct gpio_config enbw_gpio_config_w[] = { |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static const struct gpio_config enbw_gpio_config[] = { |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 182 | { "PLC reset", 6, 13, 1, 0 }, |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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 }, |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 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 }, |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 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 }, |
| 205 | }; |
| 206 | |
| 207 | #define PHY_POWER 0x0800 |
| 208 | |
| 209 | static void enbw_cmc_switch(int port, int on) |
| 210 | { |
| 211 | const char *devname; |
| 212 | unsigned char phyaddr = 3; |
| 213 | unsigned char reg = 0; |
| 214 | unsigned short data; |
| 215 | |
| 216 | if (port == 1) |
| 217 | phyaddr = 2; |
| 218 | |
| 219 | devname = miiphy_get_current_dev(); |
| 220 | if (!devname) { |
| 221 | printf("Error: no mii device\n"); |
| 222 | return; |
| 223 | } |
| 224 | if (miiphy_read(devname, phyaddr, reg, &data) != 0) { |
| 225 | printf("Error reading from the PHY addr=%02x reg=%02x\n", |
| 226 | phyaddr, reg); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | if (on) |
| 231 | data &= ~PHY_POWER; |
| 232 | else |
| 233 | data |= PHY_POWER; |
| 234 | |
| 235 | if (miiphy_write(devname, phyaddr, reg, data) != 0) { |
| 236 | printf("Error writing to the PHY addr=%02x reg=%02x\n", |
| 237 | phyaddr, reg); |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 242 | static int enbw_cmc_init_gpio(const struct gpio_config *conf, int sz) |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 243 | { |
| 244 | int i, ret; |
| 245 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 246 | for (i = 0; i < sz; i++) { |
| 247 | int gpio = conf[i].bank * 16 + |
| 248 | conf[i].gpio; |
| 249 | |
| 250 | ret = gpio_request(gpio, conf[i].name); |
| 251 | if (ret) { |
| 252 | printf("%s: Could not get %s gpio\n", __func__, |
| 253 | conf[i].name); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | if (conf[i].out) |
| 258 | gpio_direction_output(gpio, |
| 259 | conf[i].value); |
| 260 | else |
| 261 | gpio_direction_input(gpio); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int board_init(void) |
| 268 | { |
| 269 | int board_type, hw_id; |
| 270 | |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 271 | #ifndef CONFIG_USE_IRQ |
| 272 | irq_init(); |
| 273 | #endif |
| 274 | /* address of boot parameters, not used as booting with DTT */ |
| 275 | gd->bd->bi_boot_params = 0; |
| 276 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 277 | enbw_cmc_init_gpio(enbw_gpio_config, ARRAY_SIZE(enbw_gpio_config)); |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 278 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 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"), |
| 285 | hw_id); |
| 286 | if (board_type) |
| 287 | enbw_cmc_init_gpio(enbw_gpio_config_w, |
| 288 | ARRAY_SIZE(enbw_gpio_config_w)); |
| 289 | else |
| 290 | enbw_cmc_init_gpio(enbw_gpio_config_hut, |
| 291 | ARRAY_SIZE(enbw_gpio_config_hut)); |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 292 | |
| 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)); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | #ifdef CONFIG_DRIVER_TI_EMAC |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 303 | |
| 304 | #define KSZ_CMD_READ 0x03 |
| 305 | #define KSZ_CMD_WRITE 0x02 |
| 306 | #define KSZ_ID 0x95 |
| 307 | |
| 308 | static int enbw_cmc_switch_read(struct spi_slave *spi, u8 reg, u8 *val) |
| 309 | { |
| 310 | unsigned long flags = SPI_XFER_BEGIN; |
| 311 | int ret; |
| 312 | int cmd_len; |
| 313 | u8 cmd[2]; |
| 314 | |
| 315 | cmd[0] = KSZ_CMD_READ; |
| 316 | cmd[1] = reg; |
| 317 | cmd_len = 2; |
| 318 | |
| 319 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
| 320 | if (ret) { |
| 321 | debug("Failed to send command (%zu bytes): %d\n", |
| 322 | cmd_len, ret); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | flags |= SPI_XFER_END; |
| 326 | *val = 0; |
| 327 | cmd_len = 1; |
| 328 | ret = spi_xfer(spi, cmd_len * 8, NULL, val, flags); |
| 329 | if (ret) { |
| 330 | debug("Failed to read (%zu bytes): %d\n", |
| 331 | cmd_len, ret); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int enbw_cmc_switch_read_ident(struct spi_slave *spi) |
| 339 | { |
| 340 | int ret; |
| 341 | u8 val; |
| 342 | |
| 343 | ret = enbw_cmc_switch_read(spi, 0, &val); |
| 344 | if (ret) { |
| 345 | debug("Failed to read\n"); |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | if (val != KSZ_ID) |
| 350 | return -EINVAL; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int enbw_cmc_switch_write(struct spi_slave *spi, unsigned long reg, |
| 356 | unsigned long val) |
| 357 | { |
| 358 | unsigned long flags = SPI_XFER_BEGIN; |
| 359 | int ret; |
| 360 | int cmd_len; |
| 361 | u8 cmd[3]; |
| 362 | |
| 363 | cmd[0] = KSZ_CMD_WRITE; |
| 364 | cmd[1] = reg; |
| 365 | cmd[2] = val; |
| 366 | cmd_len = 3; |
| 367 | flags |= SPI_XFER_END; |
| 368 | |
| 369 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
| 370 | if (ret) { |
| 371 | debug("Failed to send command (%zu bytes): %d\n", |
| 372 | cmd_len, ret); |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | udelay(1000); |
| 377 | ret = enbw_cmc_switch_read(spi, reg, &cmd[0]); |
| 378 | if (ret) { |
| 379 | debug("Failed to read\n"); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | if (val != cmd[0]) |
| 383 | debug("warning: reg: %lx va: %x soll: %lx\n", |
| 384 | reg, cmd[0], val); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int enbw_cmc_eof(unsigned char *ptr) |
| 390 | { |
| 391 | if (*ptr == 0xff) |
| 392 | return 1; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static char *enbw_cmc_getnewline(char *ptr) |
| 398 | { |
| 399 | while (*ptr != 0x0a) { |
| 400 | ptr++; |
| 401 | if (enbw_cmc_eof((unsigned char *)ptr)) |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | ptr++; |
| 406 | return ptr; |
| 407 | } |
| 408 | |
| 409 | static char *enbw_cmc_getvalue(char *ptr, int *value) |
| 410 | { |
| 411 | int end = 0; |
| 412 | |
| 413 | *value = -EINVAL; |
| 414 | |
| 415 | if (!isxdigit(*ptr)) |
| 416 | end = 1; |
| 417 | |
| 418 | while (end) { |
| 419 | if ((*ptr == '#') || (*ptr == ';')) { |
| 420 | ptr = enbw_cmc_getnewline(ptr); |
| 421 | return ptr; |
| 422 | } |
| 423 | if (ptr != NULL) { |
| 424 | if (isxdigit(*ptr)) { |
| 425 | end = 0; |
| 426 | } else if (*ptr == 0x0a) { |
| 427 | ptr++; |
| 428 | return ptr; |
| 429 | } else { |
| 430 | ptr++; |
| 431 | if (enbw_cmc_eof((unsigned char *)ptr)) |
| 432 | return NULL; |
| 433 | } |
| 434 | } else { |
| 435 | return NULL; |
| 436 | } |
| 437 | } |
| 438 | *value = (int)simple_strtoul((const char *)ptr, &ptr, 16); |
| 439 | ptr++; |
| 440 | return ptr; |
| 441 | } |
| 442 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 443 | static struct spi_slave *enbw_cmc_init_spi(void) |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 444 | { |
| 445 | struct spi_slave *spi; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 446 | int ret; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 447 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 448 | spi = spi_setup_slave(0, 0, 1000000, 0); |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 449 | if (!spi) { |
| 450 | printf("Failed to set up slave\n"); |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 451 | return NULL; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | ret = spi_claim_bus(spi); |
| 455 | if (ret) { |
| 456 | debug("Failed to claim SPI bus: %d\n", ret); |
| 457 | goto err_claim_bus; |
| 458 | } |
| 459 | |
| 460 | ret = enbw_cmc_switch_read_ident(spi); |
| 461 | if (ret) |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 462 | goto err_read; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 463 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 464 | return spi; |
| 465 | err_read: |
| 466 | spi_release_bus(spi); |
| 467 | err_claim_bus: |
| 468 | spi_free_slave(spi); |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | static int enbw_cmc_config_switch(unsigned long addr) |
| 473 | { |
| 474 | struct spi_slave *spi; |
| 475 | char *ptr = (char *)addr; |
| 476 | int value, reg; |
| 477 | int ret = 0; |
| 478 | |
| 479 | debug("configure switch with file on addr: 0x%lx\n", addr); |
| 480 | |
| 481 | spi = enbw_cmc_init_spi(); |
| 482 | if (!spi) |
| 483 | return -EINVAL; |
| 484 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 485 | while (ptr != NULL) { |
| 486 | ptr = enbw_cmc_getvalue(ptr, ®); |
| 487 | if (ptr != NULL) { |
| 488 | ptr = enbw_cmc_getvalue(ptr, &value); |
| 489 | if ((ptr != NULL) && (value >= 0)) |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 490 | if (enbw_cmc_switch_write(spi, reg, value)) { |
| 491 | /* error writing to switch */ |
| 492 | ptr = NULL; |
| 493 | ret = -EINVAL; |
| 494 | } |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 497 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 498 | spi_release_bus(spi); |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 499 | spi_free_slave(spi); |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 500 | return ret; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 504 | { |
| 505 | unsigned long addr; |
| 506 | |
| 507 | if (argc < 2) |
| 508 | return cmd_usage(cmdtp); |
| 509 | |
| 510 | addr = simple_strtoul(argv[1], NULL, 16); |
| 511 | enbw_cmc_config_switch(addr); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | U_BOOT_CMD(switch, 3, 1, do_switch, |
| 517 | "switch addr", |
| 518 | "[addr]" |
| 519 | ); |
| 520 | |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 521 | /* |
| 522 | * Initializes on-board ethernet controllers. |
| 523 | */ |
| 524 | int board_eth_init(bd_t *bis) |
| 525 | { |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 526 | struct spi_slave *spi; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 527 | const char *s; |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 528 | size_t len = 0; |
| 529 | int config = 1; |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 530 | |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 531 | davinci_emac_mii_mode_sel(0); |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 532 | |
| 533 | /* send a config file to the switch */ |
| 534 | s = hwconfig_subarg("switch", "config", &len); |
| 535 | if (len) { |
| 536 | unsigned long addr = simple_strtoul(s, NULL, 16); |
| 537 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 538 | config = enbw_cmc_config_switch(addr); |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 539 | } |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 540 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 541 | if (config) { |
| 542 | /* |
| 543 | * no valid config file -> do we have some args in |
| 544 | * hwconfig ? |
| 545 | */ |
| 546 | if ((hwconfig_subarg("switch", "lan", &len)) || |
| 547 | (hwconfig_subarg("switch", "lmn", &len))) { |
| 548 | /* If so start switch */ |
| 549 | spi = enbw_cmc_init_spi(); |
| 550 | if (spi) { |
| 551 | if (enbw_cmc_switch_write(spi, 1, 0)) |
| 552 | config = 0; |
| 553 | udelay(10000); |
| 554 | if (enbw_cmc_switch_write(spi, 1, 1)) |
| 555 | config = 0; |
| 556 | spi_release_bus(spi); |
| 557 | spi_free_slave(spi); |
| 558 | } |
| 559 | } else { |
| 560 | config = 0; |
| 561 | } |
| 562 | } |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 563 | if (!davinci_emac_initialize()) { |
| 564 | printf("Error: Ethernet init failed!\n"); |
| 565 | return -1; |
| 566 | } |
| 567 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 568 | if (config) { |
| 569 | if (hwconfig_subarg_cmp("switch", "lan", "on")) |
| 570 | /* Switch port lan on */ |
| 571 | enbw_cmc_switch(1, 1); |
| 572 | else |
| 573 | enbw_cmc_switch(1, 0); |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 574 | |
Heiko Schocher | 1bb0946 | 2012-08-13 23:51:04 +0000 | [diff] [blame] | 575 | if (hwconfig_subarg_cmp("switch", "lmn", "on")) |
| 576 | /* Switch port pwl on */ |
| 577 | enbw_cmc_switch(2, 1); |
| 578 | else |
| 579 | enbw_cmc_switch(2, 0); |
| 580 | } |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | #endif /* CONFIG_DRIVER_TI_EMAC */ |
| 585 | |
| 586 | #ifdef CONFIG_PREBOOT |
| 587 | static uchar kbd_magic_prefix[] = "key_magic_"; |
| 588 | static uchar kbd_command_prefix[] = "key_cmd_"; |
| 589 | |
| 590 | struct kbd_data_t { |
| 591 | char s1; |
| 592 | }; |
| 593 | |
| 594 | struct kbd_data_t *get_keys(struct kbd_data_t *kbd_data) |
| 595 | { |
| 596 | /* read SW1 + SW2 */ |
| 597 | kbd_data->s1 = gpio_get_value(12) + |
| 598 | (gpio_get_value(13) << 1); |
| 599 | return kbd_data; |
| 600 | } |
| 601 | |
| 602 | static int compare_magic(const struct kbd_data_t *kbd_data, char *str) |
| 603 | { |
| 604 | char s1 = str[0]; |
| 605 | |
| 606 | if (s1 >= '0' && s1 <= '9') |
| 607 | s1 -= '0'; |
| 608 | else if (s1 >= 'a' && s1 <= 'f') |
| 609 | s1 = s1 - 'a' + 10; |
| 610 | else if (s1 >= 'A' && s1 <= 'F') |
| 611 | s1 = s1 - 'A' + 10; |
| 612 | else |
| 613 | return -1; |
| 614 | |
| 615 | if (s1 != kbd_data->s1) |
| 616 | return -1; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static char *key_match(const struct kbd_data_t *kbd_data) |
| 622 | { |
| 623 | char magic[sizeof(kbd_magic_prefix) + 1]; |
| 624 | char *suffix; |
| 625 | char *kbd_magic_keys; |
| 626 | |
| 627 | /* |
| 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. |
| 634 | */ |
| 635 | kbd_magic_keys = getenv("magic_keys"); |
| 636 | if (kbd_magic_keys == NULL) |
| 637 | kbd_magic_keys = ""; |
| 638 | |
| 639 | /* |
| 640 | * loop over all magic keys; |
| 641 | * use '\0' suffix in case of empty string |
| 642 | */ |
| 643 | for (suffix = kbd_magic_keys; *suffix || |
| 644 | suffix == kbd_magic_keys; ++suffix) { |
| 645 | sprintf(magic, "%s%c", kbd_magic_prefix, *suffix); |
| 646 | |
| 647 | if (compare_magic(kbd_data, getenv(magic)) == 0) { |
| 648 | char cmd_name[sizeof(kbd_command_prefix) + 1]; |
| 649 | char *cmd; |
| 650 | |
| 651 | sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix); |
| 652 | cmd = getenv(cmd_name); |
| 653 | |
| 654 | return cmd; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return NULL; |
| 659 | } |
| 660 | #endif /* CONFIG_PREBOOT */ |
| 661 | |
| 662 | int misc_init_r(void) |
| 663 | { |
| 664 | char *s, buf[32]; |
| 665 | #ifdef CONFIG_PREBOOT |
| 666 | struct kbd_data_t kbd_data; |
| 667 | /* Decode keys */ |
| 668 | char *str = strdup(key_match(get_keys(&kbd_data))); |
| 669 | /* Set or delete definition */ |
| 670 | setenv("preboot", str); |
| 671 | free(str); |
| 672 | #endif /* CONFIG_PREBOOT */ |
| 673 | |
| 674 | /* count all restarts, and save this in an environment var */ |
| 675 | s = getenv("restartcount"); |
| 676 | |
| 677 | if (s) |
| 678 | sprintf(buf, "%ld", simple_strtoul(s, NULL, 10) + 1); |
| 679 | else |
| 680 | strcpy(buf, "1"); |
| 681 | |
| 682 | setenv("restartcount", buf); |
| 683 | saveenv(); |
| 684 | |
| 685 | #ifdef CONFIG_HW_WATCHDOG |
| 686 | davinci_hw_watchdog_enable(); |
| 687 | #endif |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | struct cmc_led { |
| 693 | char name[20]; |
| 694 | unsigned char bank; |
| 695 | unsigned char gpio; |
| 696 | }; |
| 697 | |
| 698 | struct cmc_led led_table[] = { |
| 699 | {"led1", 1, 15}, |
| 700 | {"led2", 0, 1}, |
| 701 | {"led3", 0, 2}, |
| 702 | {"led4", 0, 3}, |
| 703 | {"led5", 0, 4}, |
| 704 | {"led6", 0, 5}, |
| 705 | {"led7", 0, 6}, |
| 706 | {"led8", 0, 14}, |
| 707 | }; |
| 708 | |
| 709 | static int cmc_get_led_state(struct cmc_led *led) |
| 710 | { |
| 711 | int value; |
| 712 | int gpio = led->bank * 16 + led->gpio; |
| 713 | |
| 714 | value = gpio_get_value(gpio); |
| 715 | |
| 716 | return value; |
| 717 | } |
| 718 | |
| 719 | static int cmc_set_led_state(struct cmc_led *led, int state) |
| 720 | { |
| 721 | int gpio = led->bank * 16 + led->gpio; |
| 722 | |
| 723 | gpio_set_value(gpio, state); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 728 | { |
| 729 | struct cmc_led *led; |
| 730 | int found = 0; |
| 731 | int i = 0; |
| 732 | int only_print = 0; |
| 733 | int len = ARRAY_SIZE(led_table); |
| 734 | |
| 735 | if (argc < 2) |
| 736 | return cmd_usage(cmdtp); |
| 737 | |
| 738 | if (argc < 3) |
| 739 | only_print = 1; |
| 740 | |
| 741 | led = led_table; |
| 742 | while ((!found) && (i < len)) { |
| 743 | if (strcmp(argv[1], led->name) == 0) { |
| 744 | found = 1; |
| 745 | } else { |
| 746 | led++; |
| 747 | i++; |
| 748 | } |
| 749 | } |
| 750 | if (!found) |
| 751 | return cmd_usage(cmdtp); |
| 752 | |
| 753 | if (only_print) { |
| 754 | if (cmc_get_led_state(led)) |
| 755 | printf("on\n"); |
| 756 | else |
| 757 | printf("off\n"); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | if (strcmp(argv[2], "on") == 0) |
| 762 | cmc_set_led_state(led, 1); |
| 763 | else |
| 764 | cmc_set_led_state(led, 0); |
| 765 | |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | U_BOOT_CMD(led, 3, 1, do_led, |
| 770 | "switch on/off board led", |
| 771 | "[name] [on/off]" |
| 772 | ); |
| 773 | |
| 774 | #ifdef CONFIG_HW_WATCHDOG |
| 775 | void hw_watchdog_reset(void) |
| 776 | { |
| 777 | davinci_hw_watchdog_reset(); |
| 778 | } |
| 779 | #endif |
| 780 | |
| 781 | #if defined(CONFIG_POST) |
| 782 | void arch_memory_failure_handle(void) |
| 783 | { |
| 784 | struct davinci_gpio *gpio = davinci_gpio_bank01; |
| 785 | int state = 1; |
| 786 | |
| 787 | /* |
| 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 |
| 791 | * the gpio register. |
| 792 | */ |
| 793 | while (1) { |
| 794 | if (state) { |
| 795 | clrbits_le32(&gpio->out_data, 0x80000006); |
| 796 | state = 0; |
| 797 | } else { |
| 798 | setbits_le32(&gpio->out_data, 0x80000006); |
| 799 | state = 1; |
| 800 | } |
| 801 | udelay(500); |
| 802 | } |
| 803 | } |
| 804 | #endif |
| 805 | |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 806 | ulong post_word_load(void) |
| 807 | { |
| 808 | struct davinci_rtc *reg = |
| 809 | (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR; |
| 810 | |
| 811 | return in_be32(®->scratch2); |
| 812 | } |
| 813 | |
| 814 | void post_word_store(ulong value) |
| 815 | { |
| 816 | struct davinci_rtc *reg = |
| 817 | (struct davinci_rtc *)CONFIG_SYS_POST_WORD_ADDR; |
| 818 | |
| 819 | /* |
| 820 | * write RTC kick register to enable write |
| 821 | * for RTC Scratch registers. Cratch0 and 1 are |
| 822 | * used for bootcount values. |
| 823 | */ |
| 824 | writel(RTC_KICK0R_WE, ®->kick0r); |
| 825 | writel(RTC_KICK1R_WE, ®->kick1r); |
| 826 | out_be32(®->scratch2, value); |
| 827 | } |
| 828 | |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 829 | void board_gpio_init(void) |
| 830 | { |
| 831 | struct davinci_gpio *gpio = davinci_gpio_bank01; |
| 832 | |
| 833 | /* |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 834 | * set LED (gpio Interface not usable here) |
| 835 | * set LED pins to output and state 0 |
| 836 | */ |
| 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); |
Heiko Schocher | 14b9f16 | 2012-05-14 20:24:14 +0000 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | * set some gpio pins to low, this is needed early, |
| 844 | * so we have no gpio Interface here |
| 845 | * gpios: |
| 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 |
| 850 | */ |
| 851 | gpio = davinci_gpio_bank8; |
| 852 | clrbits_le32(&gpio->dir, 0x00000f00); |
| 853 | clrbits_le32(&gpio->out_data, 0x0f00); |
Heiko Schocher | f7264c3 | 2011-11-29 02:33:47 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | int board_late_init(void) |
| 857 | { |
| 858 | cmc_set_led_state(&led_table[4], 0); |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | void show_boot_progress(int val) |
| 864 | { |
| 865 | switch (val) { |
| 866 | case 1: |
| 867 | cmc_set_led_state(&led_table[4], 1); |
| 868 | break; |
| 869 | case 4: |
| 870 | cmc_set_led_state(&led_table[4], 0); |
| 871 | break; |
| 872 | case 15: |
| 873 | cmc_set_led_state(&led_table[4], 1); |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 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, |
| 885 | }; |
| 886 | |
| 887 | int board_mmc_init(bd_t *bis) |
| 888 | { |
| 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); |
| 892 | } |
| 893 | #endif |