Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Ethernet driver for TI K2HK EVM. |
| 3 | * |
| 4 | * (C) Copyright 2012-2014 |
| 5 | * Texas Instruments Incorporated, <www.ti.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | |
| 12 | #include <net.h> |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 13 | #include <phy.h> |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 14 | #include <errno.h> |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 15 | #include <miiphy.h> |
| 16 | #include <malloc.h> |
Khoronzhuk, Ivan | ef45471 | 2014-09-05 19:02:47 +0300 | [diff] [blame] | 17 | #include <asm/ti-common/keystone_nav.h> |
Khoronzhuk, Ivan | 0935cac | 2014-09-29 22:17:22 +0300 | [diff] [blame] | 18 | #include <asm/ti-common/keystone_net.h> |
Khoronzhuk, Ivan | a43febd | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 19 | #include <asm/ti-common/keystone_serdes.h> |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 20 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 21 | unsigned int emac_open; |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 22 | static struct mii_dev *mdio_bus; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 23 | static unsigned int sys_has_mdio = 1; |
| 24 | |
| 25 | #ifdef KEYSTONE2_EMAC_GIG_ENABLE |
| 26 | #define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x) |
| 27 | #else |
| 28 | #define emac_gigabit_enable(x) /* no gigabit to enable */ |
| 29 | #endif |
| 30 | |
| 31 | #define RX_BUFF_NUMS 24 |
| 32 | #define RX_BUFF_LEN 1520 |
| 33 | #define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 34 | #define SGMII_ANEG_TIMEOUT 4000 |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 35 | |
| 36 | static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16); |
| 37 | |
| 38 | struct rx_buff_desc net_rx_buffs = { |
| 39 | .buff_ptr = rx_buffs, |
| 40 | .num_buffs = RX_BUFF_NUMS, |
| 41 | .buff_len = RX_BUFF_LEN, |
| 42 | .rx_flow = 22, |
| 43 | }; |
| 44 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 45 | #ifndef CONFIG_SOC_K2G |
Khoronzhuk, Ivan | a43febd | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 46 | static void keystone2_net_serdes_setup(void); |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 47 | #endif |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 48 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 49 | int keystone2_eth_read_mac_addr(struct eth_device *dev) |
| 50 | { |
| 51 | struct eth_priv_t *eth_priv; |
| 52 | u32 maca = 0; |
| 53 | u32 macb = 0; |
| 54 | |
| 55 | eth_priv = (struct eth_priv_t *)dev->priv; |
| 56 | |
| 57 | /* Read the e-fuse mac address */ |
| 58 | if (eth_priv->slave_port == 1) { |
| 59 | maca = __raw_readl(MAC_ID_BASE_ADDR); |
| 60 | macb = __raw_readl(MAC_ID_BASE_ADDR + 4); |
| 61 | } |
| 62 | |
| 63 | dev->enetaddr[0] = (macb >> 8) & 0xff; |
| 64 | dev->enetaddr[1] = (macb >> 0) & 0xff; |
| 65 | dev->enetaddr[2] = (maca >> 24) & 0xff; |
| 66 | dev->enetaddr[3] = (maca >> 16) & 0xff; |
| 67 | dev->enetaddr[4] = (maca >> 8) & 0xff; |
| 68 | dev->enetaddr[5] = (maca >> 0) & 0xff; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 73 | /* MDIO */ |
| 74 | |
| 75 | static int keystone2_mdio_reset(struct mii_dev *bus) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 76 | { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 77 | u_int32_t clkdiv; |
| 78 | struct mdio_regs *adap_mdio = bus->priv; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 79 | |
| 80 | clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1; |
| 81 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 82 | writel((clkdiv & 0xffff) | MDIO_CONTROL_ENABLE | |
| 83 | MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE, |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 84 | &adap_mdio->control); |
| 85 | |
| 86 | while (readl(&adap_mdio->control) & MDIO_CONTROL_IDLE) |
| 87 | ; |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 88 | |
| 89 | return 0; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 92 | /** |
| 93 | * keystone2_mdio_read - read a PHY register via MDIO interface. |
| 94 | * Blocks until operation is complete. |
| 95 | */ |
| 96 | static int keystone2_mdio_read(struct mii_dev *bus, |
| 97 | int addr, int devad, int reg) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 98 | { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 99 | int tmp; |
| 100 | struct mdio_regs *adap_mdio = bus->priv; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 101 | |
| 102 | while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO) |
| 103 | ; |
| 104 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 105 | writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ | |
| 106 | ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16), |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 107 | &adap_mdio->useraccess0); |
| 108 | |
| 109 | /* Wait for command to complete */ |
| 110 | while ((tmp = readl(&adap_mdio->useraccess0)) & MDIO_USERACCESS0_GO) |
| 111 | ; |
| 112 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 113 | if (tmp & MDIO_USERACCESS0_ACK) |
| 114 | return tmp & 0xffff; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 115 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 119 | /** |
| 120 | * keystone2_mdio_write - write to a PHY register via MDIO interface. |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 121 | * Blocks until operation is complete. |
| 122 | */ |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 123 | static int keystone2_mdio_write(struct mii_dev *bus, |
| 124 | int addr, int devad, int reg, u16 val) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 125 | { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 126 | struct mdio_regs *adap_mdio = bus->priv; |
| 127 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 128 | while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO) |
| 129 | ; |
| 130 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 131 | writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE | |
| 132 | ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16) | |
| 133 | (val & 0xffff), &adap_mdio->useraccess0); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 134 | |
| 135 | /* Wait for command to complete */ |
| 136 | while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO) |
| 137 | ; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 142 | static void __attribute__((unused)) |
| 143 | keystone2_eth_gigabit_enable(struct eth_device *dev) |
| 144 | { |
| 145 | u_int16_t data; |
| 146 | struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv; |
| 147 | |
| 148 | if (sys_has_mdio) { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 149 | data = keystone2_mdio_read(mdio_bus, eth_priv->phy_addr, |
| 150 | MDIO_DEVAD_NONE, 0); |
| 151 | /* speed selection MSB */ |
| 152 | if (!(data & (1 << 6))) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Check if link detected is giga-bit |
| 158 | * If Gigabit mode detected, enable gigbit in MAC |
| 159 | */ |
Hao Zhang | b2cfe32 | 2014-09-29 22:17:20 +0300 | [diff] [blame] | 160 | writel(readl(DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + |
| 161 | CPGMACSL_REG_CTL) | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 162 | EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE, |
Hao Zhang | b2cfe32 | 2014-09-29 22:17:20 +0300 | [diff] [blame] | 163 | DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + CPGMACSL_REG_CTL); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 166 | #ifdef CONFIG_SOC_K2G |
| 167 | int keystone_rgmii_config(struct phy_device *phy_dev) |
| 168 | { |
| 169 | unsigned int i, status; |
| 170 | |
| 171 | i = 0; |
| 172 | do { |
| 173 | if (i > SGMII_ANEG_TIMEOUT) { |
| 174 | puts(" TIMEOUT !\n"); |
| 175 | phy_dev->link = 0; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | if (ctrlc()) { |
| 180 | puts("user interrupt!\n"); |
| 181 | phy_dev->link = 0; |
| 182 | return -EINTR; |
| 183 | } |
| 184 | |
| 185 | if ((i++ % 500) == 0) |
| 186 | printf("."); |
| 187 | |
| 188 | udelay(1000); /* 1 ms */ |
| 189 | status = readl(RGMII_STATUS_REG); |
| 190 | } while (!(status & RGMII_REG_STATUS_LINK)); |
| 191 | |
| 192 | puts(" done\n"); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | #else |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 197 | int keystone_sgmii_config(struct phy_device *phy_dev, int port, int interface) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 198 | { |
| 199 | unsigned int i, status, mask; |
| 200 | unsigned int mr_adv_ability, control; |
| 201 | |
| 202 | switch (interface) { |
| 203 | case SGMII_LINK_MAC_MAC_AUTONEG: |
| 204 | mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE | |
| 205 | SGMII_REG_MR_ADV_LINK | |
| 206 | SGMII_REG_MR_ADV_FULL_DUPLEX | |
| 207 | SGMII_REG_MR_ADV_GIG_MODE); |
| 208 | control = (SGMII_REG_CONTROL_MASTER | |
| 209 | SGMII_REG_CONTROL_AUTONEG); |
| 210 | |
| 211 | break; |
| 212 | case SGMII_LINK_MAC_PHY: |
| 213 | case SGMII_LINK_MAC_PHY_FORCED: |
| 214 | mr_adv_ability = SGMII_REG_MR_ADV_ENABLE; |
| 215 | control = SGMII_REG_CONTROL_AUTONEG; |
| 216 | |
| 217 | break; |
| 218 | case SGMII_LINK_MAC_MAC_FORCED: |
| 219 | mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE | |
| 220 | SGMII_REG_MR_ADV_LINK | |
| 221 | SGMII_REG_MR_ADV_FULL_DUPLEX | |
| 222 | SGMII_REG_MR_ADV_GIG_MODE); |
| 223 | control = SGMII_REG_CONTROL_MASTER; |
| 224 | |
| 225 | break; |
| 226 | case SGMII_LINK_MAC_FIBER: |
| 227 | mr_adv_ability = 0x20; |
| 228 | control = SGMII_REG_CONTROL_AUTONEG; |
| 229 | |
| 230 | break; |
| 231 | default: |
| 232 | mr_adv_ability = SGMII_REG_MR_ADV_ENABLE; |
| 233 | control = SGMII_REG_CONTROL_AUTONEG; |
| 234 | } |
| 235 | |
| 236 | __raw_writel(0, SGMII_CTL_REG(port)); |
| 237 | |
| 238 | /* |
| 239 | * Wait for the SerDes pll to lock, |
| 240 | * but don't trap if lock is never read |
| 241 | */ |
| 242 | for (i = 0; i < 1000; i++) { |
| 243 | udelay(2000); |
| 244 | status = __raw_readl(SGMII_STATUS_REG(port)); |
| 245 | if ((status & SGMII_REG_STATUS_LOCK) != 0) |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port)); |
| 250 | __raw_writel(control, SGMII_CTL_REG(port)); |
| 251 | |
| 252 | |
| 253 | mask = SGMII_REG_STATUS_LINK; |
| 254 | |
| 255 | if (control & SGMII_REG_CONTROL_AUTONEG) |
| 256 | mask |= SGMII_REG_STATUS_AUTONEG; |
| 257 | |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 258 | status = __raw_readl(SGMII_STATUS_REG(port)); |
| 259 | if ((status & mask) == mask) |
| 260 | return 0; |
| 261 | |
| 262 | printf("\n%s Waiting for SGMII auto negotiation to complete", |
| 263 | phy_dev->dev->name); |
| 264 | while ((status & mask) != mask) { |
| 265 | /* |
| 266 | * Timeout reached ? |
| 267 | */ |
| 268 | if (i > SGMII_ANEG_TIMEOUT) { |
| 269 | puts(" TIMEOUT !\n"); |
| 270 | phy_dev->link = 0; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | if (ctrlc()) { |
| 275 | puts("user interrupt!\n"); |
| 276 | phy_dev->link = 0; |
| 277 | return -EINTR; |
| 278 | } |
| 279 | |
| 280 | if ((i++ % 500) == 0) |
| 281 | printf("."); |
| 282 | |
| 283 | udelay(1000); /* 1 ms */ |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 284 | status = __raw_readl(SGMII_STATUS_REG(port)); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 285 | } |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 286 | puts(" done\n"); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 290 | #endif |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 291 | |
| 292 | int mac_sl_reset(u32 port) |
| 293 | { |
| 294 | u32 i, v; |
| 295 | |
| 296 | if (port >= DEVICE_N_GMACSL_PORTS) |
| 297 | return GMACSL_RET_INVALID_PORT; |
| 298 | |
| 299 | /* Set the soft reset bit */ |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 300 | writel(CPGMAC_REG_RESET_VAL_RESET, |
| 301 | DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 302 | |
| 303 | /* Wait for the bit to clear */ |
| 304 | for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) { |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 305 | v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 306 | if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) != |
| 307 | CPGMAC_REG_RESET_VAL_RESET) |
| 308 | return GMACSL_RET_OK; |
| 309 | } |
| 310 | |
| 311 | /* Timeout on the reset */ |
| 312 | return GMACSL_RET_WARN_RESET_INCOMPLETE; |
| 313 | } |
| 314 | |
| 315 | int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg) |
| 316 | { |
| 317 | u32 v, i; |
| 318 | int ret = GMACSL_RET_OK; |
| 319 | |
| 320 | if (port >= DEVICE_N_GMACSL_PORTS) |
| 321 | return GMACSL_RET_INVALID_PORT; |
| 322 | |
| 323 | if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) { |
| 324 | cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN; |
| 325 | ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG; |
| 326 | } |
| 327 | |
| 328 | /* Must wait if the device is undergoing reset */ |
| 329 | for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) { |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 330 | v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 331 | if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) != |
| 332 | CPGMAC_REG_RESET_VAL_RESET) |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (i == DEVICE_EMACSL_RESET_POLL_COUNT) |
| 337 | return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE; |
| 338 | |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 339 | writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN); |
| 340 | writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 341 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 342 | #ifndef CONFIG_SOC_K2HK |
Khoronzhuk, Ivan | ff11c76 | 2014-10-17 21:01:14 +0300 | [diff] [blame] | 343 | /* Map RX packet flow priority to 0 */ |
| 344 | writel(0, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RX_PRI_MAP); |
| 345 | #endif |
| 346 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | int ethss_config(u32 ctl, u32 max_pkt_size) |
| 351 | { |
| 352 | u32 i; |
| 353 | |
| 354 | /* Max length register */ |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 355 | writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 356 | |
| 357 | /* Control register */ |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 358 | writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 359 | |
| 360 | /* All statistics enabled by default */ |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 361 | writel(CPSW_REG_VAL_STAT_ENABLE_ALL, |
| 362 | DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 363 | |
| 364 | /* Reset and enable the ALE */ |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 365 | writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE | |
| 366 | CPSW_REG_VAL_ALE_CTL_BYPASS, |
| 367 | DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 368 | |
| 369 | /* All ports put into forward mode */ |
| 370 | for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++) |
Khoronzhuk, Ivan | e6c9428 | 2014-08-28 16:07:45 +0300 | [diff] [blame] | 371 | writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE, |
| 372 | DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i)); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int ethss_start(void) |
| 378 | { |
| 379 | int i; |
| 380 | struct mac_sl_cfg cfg; |
| 381 | |
| 382 | cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER; |
| 383 | cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL; |
| 384 | |
| 385 | for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) { |
| 386 | mac_sl_reset(i); |
| 387 | mac_sl_config(i, &cfg); |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int ethss_stop(void) |
| 394 | { |
| 395 | int i; |
| 396 | |
| 397 | for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) |
| 398 | mac_sl_reset(i); |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | int32_t cpmac_drv_send(u32 *buffer, int num_bytes, int slave_port_num) |
| 404 | { |
| 405 | if (num_bytes < EMAC_MIN_ETHERNET_PKT_SIZE) |
| 406 | num_bytes = EMAC_MIN_ETHERNET_PKT_SIZE; |
| 407 | |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 408 | return ksnav_send(&netcp_pktdma, buffer, |
| 409 | num_bytes, (slave_port_num) << 16); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* Eth device open */ |
| 413 | static int keystone2_eth_open(struct eth_device *dev, bd_t *bis) |
| 414 | { |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 415 | struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv; |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 416 | struct phy_device *phy_dev = eth_priv->phy_dev; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 417 | |
| 418 | debug("+ emac_open\n"); |
| 419 | |
| 420 | net_rx_buffs.rx_flow = eth_priv->rx_flow; |
| 421 | |
| 422 | sys_has_mdio = |
| 423 | (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY) ? 1 : 0; |
| 424 | |
Khoronzhuk, Ivan | 6c0fb41 | 2014-10-29 13:09:33 +0200 | [diff] [blame] | 425 | if (sys_has_mdio) |
| 426 | keystone2_mdio_reset(mdio_bus); |
| 427 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 428 | #ifdef CONFIG_SOC_K2G |
| 429 | keystone_rgmii_config(phy_dev); |
| 430 | #else |
Khoronzhuk, Ivan | c05d05e | 2014-10-17 21:01:15 +0300 | [diff] [blame] | 431 | keystone_sgmii_config(phy_dev, eth_priv->slave_port - 1, |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 432 | eth_priv->sgmii_link_type); |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 433 | #endif |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 434 | |
| 435 | udelay(10000); |
| 436 | |
| 437 | /* On chip switch configuration */ |
| 438 | ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE); |
| 439 | |
| 440 | /* TODO: add error handling code */ |
| 441 | if (qm_init()) { |
| 442 | printf("ERROR: qm_init()\n"); |
| 443 | return -1; |
| 444 | } |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 445 | if (ksnav_init(&netcp_pktdma, &net_rx_buffs)) { |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 446 | qm_close(); |
| 447 | printf("ERROR: netcp_init()\n"); |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Streaming switch configuration. If not present this |
| 453 | * statement is defined to void in target.h. |
| 454 | * If present this is usually defined to a series of register writes |
| 455 | */ |
| 456 | hw_config_streaming_switch(); |
| 457 | |
| 458 | if (sys_has_mdio) { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 459 | keystone2_mdio_reset(mdio_bus); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 460 | |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 461 | phy_startup(phy_dev); |
| 462 | if (phy_dev->link == 0) { |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 463 | ksnav_close(&netcp_pktdma); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 464 | qm_close(); |
| 465 | return -1; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | emac_gigabit_enable(dev); |
| 470 | |
| 471 | ethss_start(); |
| 472 | |
| 473 | debug("- emac_open\n"); |
| 474 | |
| 475 | emac_open = 1; |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* Eth device close */ |
| 481 | void keystone2_eth_close(struct eth_device *dev) |
| 482 | { |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 483 | struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv; |
| 484 | struct phy_device *phy_dev = eth_priv->phy_dev; |
| 485 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 486 | debug("+ emac_close\n"); |
| 487 | |
| 488 | if (!emac_open) |
| 489 | return; |
| 490 | |
| 491 | ethss_stop(); |
| 492 | |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 493 | ksnav_close(&netcp_pktdma); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 494 | qm_close(); |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 495 | phy_shutdown(phy_dev); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 496 | |
| 497 | emac_open = 0; |
| 498 | |
| 499 | debug("- emac_close\n"); |
| 500 | } |
| 501 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 502 | /* |
| 503 | * This function sends a single packet on the network and returns |
| 504 | * positive number (number of bytes transmitted) or negative for error |
| 505 | */ |
| 506 | static int keystone2_eth_send_packet(struct eth_device *dev, |
| 507 | void *packet, int length) |
| 508 | { |
| 509 | int ret_status = -1; |
| 510 | struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv; |
Khoronzhuk, Ivan | a4d2ade | 2014-10-17 20:44:36 +0300 | [diff] [blame] | 511 | struct phy_device *phy_dev = eth_priv->phy_dev; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 512 | |
Khoronzhuk, Ivan | a4d2ade | 2014-10-17 20:44:36 +0300 | [diff] [blame] | 513 | genphy_update_link(phy_dev); |
| 514 | if (phy_dev->link == 0) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 515 | return -1; |
| 516 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 517 | if (cpmac_drv_send((u32 *)packet, length, eth_priv->slave_port) != 0) |
| 518 | return ret_status; |
| 519 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 520 | return length; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * This function handles receipt of a packet from the network |
| 525 | */ |
| 526 | static int keystone2_eth_rcv_packet(struct eth_device *dev) |
| 527 | { |
| 528 | void *hd; |
| 529 | int pkt_size; |
| 530 | u32 *pkt; |
| 531 | |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 532 | hd = ksnav_recv(&netcp_pktdma, &pkt, &pkt_size); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 533 | if (hd == NULL) |
| 534 | return 0; |
| 535 | |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 536 | net_process_received_packet((uchar *)pkt, pkt_size); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 537 | |
Khoronzhuk, Ivan | 9ea9021 | 2014-09-05 19:02:48 +0300 | [diff] [blame] | 538 | ksnav_release_rxhd(&netcp_pktdma, hd); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 539 | |
| 540 | return pkt_size; |
| 541 | } |
| 542 | |
Vitaly Andrianov | 5031ca5 | 2015-07-08 11:56:01 -0400 | [diff] [blame] | 543 | #ifdef CONFIG_MCAST_TFTP |
| 544 | static int keystone2_eth_bcast_addr(struct eth_device *dev, u32 ip, u8 set) |
| 545 | { |
| 546 | return 0; |
| 547 | } |
| 548 | #endif |
| 549 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 550 | /* |
| 551 | * This function initializes the EMAC hardware. |
| 552 | */ |
| 553 | int keystone2_emac_initialize(struct eth_priv_t *eth_priv) |
| 554 | { |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 555 | int res; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 556 | struct eth_device *dev; |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 557 | struct phy_device *phy_dev; |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 558 | |
| 559 | dev = malloc(sizeof(struct eth_device)); |
| 560 | if (dev == NULL) |
| 561 | return -1; |
| 562 | |
| 563 | memset(dev, 0, sizeof(struct eth_device)); |
| 564 | |
| 565 | strcpy(dev->name, eth_priv->int_name); |
| 566 | dev->priv = eth_priv; |
| 567 | |
| 568 | keystone2_eth_read_mac_addr(dev); |
| 569 | |
| 570 | dev->iobase = 0; |
| 571 | dev->init = keystone2_eth_open; |
| 572 | dev->halt = keystone2_eth_close; |
| 573 | dev->send = keystone2_eth_send_packet; |
| 574 | dev->recv = keystone2_eth_rcv_packet; |
Vitaly Andrianov | 5031ca5 | 2015-07-08 11:56:01 -0400 | [diff] [blame] | 575 | #ifdef CONFIG_MCAST_TFTP |
| 576 | dev->mcast = keystone2_eth_bcast_addr; |
| 577 | #endif |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 578 | |
| 579 | eth_register(dev); |
| 580 | |
Khoronzhuk, Ivan | 550c5ce | 2014-10-17 20:44:34 +0300 | [diff] [blame] | 581 | /* Register MDIO bus if it's not registered yet */ |
| 582 | if (!mdio_bus) { |
| 583 | mdio_bus = mdio_alloc(); |
| 584 | mdio_bus->read = keystone2_mdio_read; |
| 585 | mdio_bus->write = keystone2_mdio_write; |
| 586 | mdio_bus->reset = keystone2_mdio_reset; |
| 587 | mdio_bus->priv = (void *)EMAC_MDIO_BASE_ADDR; |
| 588 | sprintf(mdio_bus->name, "ethernet-mdio"); |
| 589 | |
| 590 | res = mdio_register(mdio_bus); |
| 591 | if (res) |
| 592 | return res; |
| 593 | } |
| 594 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 595 | #ifndef CONFIG_SOC_K2G |
Vitaly Andrianov | 312aca4 | 2015-02-11 14:05:41 -0500 | [diff] [blame] | 596 | keystone2_net_serdes_setup(); |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 597 | #endif |
Vitaly Andrianov | 312aca4 | 2015-02-11 14:05:41 -0500 | [diff] [blame] | 598 | |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 599 | /* Create phy device and bind it with driver */ |
| 600 | #ifdef CONFIG_KSNET_MDIO_PHY_CONFIG_ENABLE |
| 601 | phy_dev = phy_connect(mdio_bus, eth_priv->phy_addr, |
Mugunthan V N | bf7bd4e | 2015-09-19 16:26:48 +0530 | [diff] [blame] | 602 | dev, eth_priv->phy_if); |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 603 | phy_config(phy_dev); |
| 604 | #else |
| 605 | phy_dev = phy_find_by_mask(mdio_bus, 1 << eth_priv->phy_addr, |
Mugunthan V N | bf7bd4e | 2015-09-19 16:26:48 +0530 | [diff] [blame] | 606 | eth_priv->phy_if); |
Khoronzhuk, Ivan | 3fe9362 | 2014-10-17 20:44:35 +0300 | [diff] [blame] | 607 | phy_dev->dev = dev; |
| 608 | #endif |
| 609 | eth_priv->phy_dev = phy_dev; |
| 610 | |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Hao Zhang | 92a16c8 | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 614 | struct ks2_serdes ks2_serdes_sgmii_156p25mhz = { |
| 615 | .clk = SERDES_CLOCK_156P25M, |
| 616 | .rate = SERDES_RATE_5G, |
| 617 | .rate_mode = SERDES_QUARTER_RATE, |
| 618 | .intf = SERDES_PHY_SGMII, |
| 619 | .loopback = 0, |
| 620 | }; |
| 621 | |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 622 | #ifndef CONFIG_SOC_K2G |
Khoronzhuk, Ivan | a43febd | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 623 | static void keystone2_net_serdes_setup(void) |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 624 | { |
Hao Zhang | 92a16c8 | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 625 | ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE, |
| 626 | &ks2_serdes_sgmii_156p25mhz, |
| 627 | CONFIG_KSNET_SERDES_LANES_PER_SGMII); |
| 628 | |
Khoronzhuk, Ivan | 87ac27b | 2014-10-29 13:09:32 +0200 | [diff] [blame] | 629 | #if defined(CONFIG_SOC_K2E) || defined(CONFIG_SOC_K2L) |
Khoronzhuk, Ivan | 3c61502 | 2014-10-17 21:01:13 +0300 | [diff] [blame] | 630 | ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII2_BASE, |
| 631 | &ks2_serdes_sgmii_156p25mhz, |
| 632 | CONFIG_KSNET_SERDES_LANES_PER_SGMII); |
| 633 | #endif |
| 634 | |
Hao Zhang | 92a16c8 | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 635 | /* wait till setup */ |
| 636 | udelay(5000); |
Karicheri, Muralidharan | fc9a8e8 | 2014-04-01 15:01:13 -0400 | [diff] [blame] | 637 | } |
Vitaly Andrianov | 4657a2d | 2015-09-19 16:26:50 +0530 | [diff] [blame] | 638 | #endif |