Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Designware ethernet IP driver for u-boot |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <miiphy.h> |
| 14 | #include <malloc.h> |
Stefan Roese | d27e86c | 2012-05-07 12:04:25 +0200 | [diff] [blame] | 15 | #include <linux/compiler.h> |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 16 | #include <linux/err.h> |
| 17 | #include <asm/io.h> |
| 18 | #include "designware.h" |
| 19 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 20 | #if !defined(CONFIG_PHYLIB) |
| 21 | # error "DesignWare Ether MAC requires PHYLIB - missing CONFIG_PHYLIB" |
| 22 | #endif |
| 23 | |
| 24 | static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 25 | { |
| 26 | struct eth_mac_regs *mac_p = bus->priv; |
| 27 | ulong start; |
| 28 | u16 miiaddr; |
| 29 | int timeout = CONFIG_MDIO_TIMEOUT; |
| 30 | |
| 31 | miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | |
| 32 | ((reg << MIIREGSHIFT) & MII_REGMSK); |
| 33 | |
| 34 | writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr); |
| 35 | |
| 36 | start = get_timer(0); |
| 37 | while (get_timer(start) < timeout) { |
| 38 | if (!(readl(&mac_p->miiaddr) & MII_BUSY)) |
| 39 | return readl(&mac_p->miidata); |
| 40 | udelay(10); |
| 41 | }; |
| 42 | |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 47 | u16 val) |
| 48 | { |
| 49 | struct eth_mac_regs *mac_p = bus->priv; |
| 50 | ulong start; |
| 51 | u16 miiaddr; |
| 52 | int ret = -1, timeout = CONFIG_MDIO_TIMEOUT; |
| 53 | |
| 54 | writel(val, &mac_p->miidata); |
| 55 | miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | |
| 56 | ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE; |
| 57 | |
| 58 | writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr); |
| 59 | |
| 60 | start = get_timer(0); |
| 61 | while (get_timer(start) < timeout) { |
| 62 | if (!(readl(&mac_p->miiaddr) & MII_BUSY)) { |
| 63 | ret = 0; |
| 64 | break; |
| 65 | } |
| 66 | udelay(10); |
| 67 | }; |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int dw_mdio_init(char *name, struct eth_mac_regs *mac_regs_p) |
| 73 | { |
| 74 | struct mii_dev *bus = mdio_alloc(); |
| 75 | |
| 76 | if (!bus) { |
| 77 | printf("Failed to allocate MDIO bus\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | bus->read = dw_mdio_read; |
| 82 | bus->write = dw_mdio_write; |
| 83 | sprintf(bus->name, name); |
| 84 | |
| 85 | bus->priv = (void *)mac_regs_p; |
| 86 | |
| 87 | return mdio_register(bus); |
| 88 | } |
Vipin Kumar | b6c5999 | 2012-03-26 00:09:56 +0000 | [diff] [blame] | 89 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 90 | static void tx_descs_init(struct eth_device *dev) |
| 91 | { |
| 92 | struct dw_eth_dev *priv = dev->priv; |
| 93 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 94 | struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0]; |
| 95 | char *txbuffs = &priv->txbuffs[0]; |
| 96 | struct dmamacdescr *desc_p; |
| 97 | u32 idx; |
| 98 | |
| 99 | for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) { |
| 100 | desc_p = &desc_table_p[idx]; |
| 101 | desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE]; |
| 102 | desc_p->dmamac_next = &desc_table_p[idx + 1]; |
| 103 | |
| 104 | #if defined(CONFIG_DW_ALTDESCRIPTOR) |
| 105 | desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST | |
| 106 | DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \ |
| 107 | DESC_TXSTS_TXCHECKINSCTRL | \ |
| 108 | DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS); |
| 109 | |
| 110 | desc_p->txrx_status |= DESC_TXSTS_TXCHAIN; |
| 111 | desc_p->dmamac_cntl = 0; |
| 112 | desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA); |
| 113 | #else |
| 114 | desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN; |
| 115 | desc_p->txrx_status = 0; |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | /* Correcting the last pointer of the chain */ |
| 120 | desc_p->dmamac_next = &desc_table_p[0]; |
| 121 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 122 | /* Flush all Tx buffer descriptors at once */ |
| 123 | flush_dcache_range((unsigned int)priv->tx_mac_descrtable, |
| 124 | (unsigned int)priv->tx_mac_descrtable + |
| 125 | sizeof(priv->tx_mac_descrtable)); |
| 126 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 127 | writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr); |
Alexey Brodkin | 4695ddd | 2014-01-13 13:28:38 +0400 | [diff] [blame] | 128 | priv->tx_currdescnum = 0; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void rx_descs_init(struct eth_device *dev) |
| 132 | { |
| 133 | struct dw_eth_dev *priv = dev->priv; |
| 134 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 135 | struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0]; |
| 136 | char *rxbuffs = &priv->rxbuffs[0]; |
| 137 | struct dmamacdescr *desc_p; |
| 138 | u32 idx; |
| 139 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 140 | /* Before passing buffers to GMAC we need to make sure zeros |
| 141 | * written there right after "priv" structure allocation were |
| 142 | * flushed into RAM. |
| 143 | * Otherwise there's a chance to get some of them flushed in RAM when |
| 144 | * GMAC is already pushing data to RAM via DMA. This way incoming from |
| 145 | * GMAC data will be corrupted. */ |
| 146 | flush_dcache_range((unsigned int)rxbuffs, (unsigned int)rxbuffs + |
| 147 | RX_TOTAL_BUFSIZE); |
| 148 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 149 | for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) { |
| 150 | desc_p = &desc_table_p[idx]; |
| 151 | desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE]; |
| 152 | desc_p->dmamac_next = &desc_table_p[idx + 1]; |
| 153 | |
| 154 | desc_p->dmamac_cntl = |
| 155 | (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \ |
| 156 | DESC_RXCTRL_RXCHAIN; |
| 157 | |
| 158 | desc_p->txrx_status = DESC_RXSTS_OWNBYDMA; |
| 159 | } |
| 160 | |
| 161 | /* Correcting the last pointer of the chain */ |
| 162 | desc_p->dmamac_next = &desc_table_p[0]; |
| 163 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 164 | /* Flush all Rx buffer descriptors at once */ |
| 165 | flush_dcache_range((unsigned int)priv->rx_mac_descrtable, |
| 166 | (unsigned int)priv->rx_mac_descrtable + |
| 167 | sizeof(priv->rx_mac_descrtable)); |
| 168 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 169 | writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr); |
Alexey Brodkin | 4695ddd | 2014-01-13 13:28:38 +0400 | [diff] [blame] | 170 | priv->rx_currdescnum = 0; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 171 | } |
| 172 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 173 | static int dw_write_hwaddr(struct eth_device *dev) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 174 | { |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 175 | struct dw_eth_dev *priv = dev->priv; |
| 176 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 177 | u32 macid_lo, macid_hi; |
| 178 | u8 *mac_id = &dev->enetaddr[0]; |
| 179 | |
| 180 | macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) + |
| 181 | (mac_id[3] << 24); |
| 182 | macid_hi = mac_id[4] + (mac_id[5] << 8); |
| 183 | |
| 184 | writel(macid_hi, &mac_p->macaddr0hi); |
| 185 | writel(macid_lo, &mac_p->macaddr0lo); |
| 186 | |
| 187 | return 0; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 190 | static void dw_adjust_link(struct eth_mac_regs *mac_p, |
| 191 | struct phy_device *phydev) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 192 | { |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 193 | u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 194 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 195 | if (!phydev->link) { |
| 196 | printf("%s: No link.\n", phydev->dev->name); |
| 197 | return; |
| 198 | } |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 199 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 200 | if (phydev->speed != 1000) |
| 201 | conf |= MII_PORTSELECT; |
Vipin Kumar | f567e41 | 2012-12-13 17:22:51 +0530 | [diff] [blame] | 202 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 203 | if (phydev->speed == 100) |
| 204 | conf |= FES_100; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 205 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 206 | if (phydev->duplex) |
| 207 | conf |= FULLDPLXMODE; |
Amit Virdi | 470e884 | 2012-03-26 00:09:59 +0000 | [diff] [blame] | 208 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 209 | writel(conf, &mac_p->conf); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 210 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 211 | printf("Speed: %d, %s duplex%s\n", phydev->speed, |
| 212 | (phydev->duplex) ? "full" : "half", |
| 213 | (phydev->port == PORT_FIBRE) ? ", fiber mode" : ""); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 214 | } |
| 215 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 216 | static void dw_eth_halt(struct eth_device *dev) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 217 | { |
| 218 | struct dw_eth_dev *priv = dev->priv; |
| 219 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 220 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 221 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 222 | writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf); |
| 223 | writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 224 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 225 | phy_shutdown(priv->phydev); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int dw_eth_init(struct eth_device *dev, bd_t *bis) |
| 229 | { |
| 230 | struct dw_eth_dev *priv = dev->priv; |
| 231 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 232 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 233 | unsigned int start; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 234 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 235 | writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode); |
Vipin Kumar | b6c5999 | 2012-03-26 00:09:56 +0000 | [diff] [blame] | 236 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 237 | start = get_timer(0); |
| 238 | while (readl(&dma_p->busmode) & DMAMAC_SRST) { |
| 239 | if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) |
| 240 | return -1; |
Stefan Roese | d27e86c | 2012-05-07 12:04:25 +0200 | [diff] [blame] | 241 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 242 | mdelay(100); |
| 243 | }; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 244 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 245 | /* Soft reset above clears HW address registers. |
| 246 | * So we have to set it here once again */ |
Vipin KUMAR | e6ff5ca | 2012-03-26 00:09:52 +0000 | [diff] [blame] | 247 | dw_write_hwaddr(dev); |
| 248 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 249 | rx_descs_init(dev); |
| 250 | tx_descs_init(dev); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 251 | |
Ian Campbell | 4164b74 | 2014-05-08 22:26:35 +0100 | [diff] [blame] | 252 | writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 253 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 254 | writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD, |
| 255 | &dma_p->opmode); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 256 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 257 | writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode); |
Vipin Kumar | 7443d60 | 2012-05-07 13:06:44 +0530 | [diff] [blame] | 258 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 259 | /* Start up the PHY */ |
| 260 | if (phy_startup(priv->phydev)) { |
| 261 | printf("Could not initialize PHY %s\n", |
| 262 | priv->phydev->dev->name); |
| 263 | return -1; |
Vipin Kumar | 7443d60 | 2012-05-07 13:06:44 +0530 | [diff] [blame] | 264 | } |
| 265 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 266 | dw_adjust_link(mac_p, priv->phydev); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 267 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 268 | if (!priv->phydev->link) |
| 269 | return -1; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 270 | |
Armando Visconti | 038c9d5 | 2012-03-26 00:09:55 +0000 | [diff] [blame] | 271 | writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 276 | static int dw_eth_send(struct eth_device *dev, void *packet, int length) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 277 | { |
| 278 | struct dw_eth_dev *priv = dev->priv; |
| 279 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 280 | u32 desc_num = priv->tx_currdescnum; |
| 281 | struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num]; |
| 282 | |
Ian Campbell | 0e690fd | 2014-05-08 22:26:33 +0100 | [diff] [blame] | 283 | /* |
| 284 | * Strictly we only need to invalidate the "txrx_status" field |
| 285 | * for the following check, but on some platforms we cannot |
| 286 | * invalidate only 4 bytes, so roundup to |
| 287 | * ARCH_DMA_MINALIGN. This is safe because the individual |
| 288 | * descriptors in the array are each aligned to |
| 289 | * ARCH_DMA_MINALIGN. |
| 290 | */ |
| 291 | invalidate_dcache_range( |
| 292 | (unsigned long)desc_p, |
| 293 | (unsigned long)desc_p + |
| 294 | roundup(sizeof(desc_p->txrx_status), ARCH_DMA_MINALIGN)); |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 295 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 296 | /* Check if the descriptor is owned by CPU */ |
| 297 | if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) { |
| 298 | printf("CPU not owner of tx frame\n"); |
| 299 | return -1; |
| 300 | } |
| 301 | |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 302 | memcpy((void *)desc_p->dmamac_addr, packet, length); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 303 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 304 | /* Flush data to be sent */ |
| 305 | flush_dcache_range((unsigned long)desc_p->dmamac_addr, |
Marek Vasut | 16ef0a8 | 2014-09-15 00:50:37 +0200 | [diff] [blame^] | 306 | (unsigned long)desc_p->dmamac_addr + |
| 307 | roundup(length, ARCH_DMA_MINALIGN)); |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 308 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 309 | #if defined(CONFIG_DW_ALTDESCRIPTOR) |
| 310 | desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST; |
| 311 | desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \ |
| 312 | DESC_TXCTRL_SIZE1MASK; |
| 313 | |
| 314 | desc_p->txrx_status &= ~(DESC_TXSTS_MSK); |
| 315 | desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA; |
| 316 | #else |
| 317 | desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \ |
| 318 | DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \ |
| 319 | DESC_TXCTRL_TXFIRST; |
| 320 | |
| 321 | desc_p->txrx_status = DESC_TXSTS_OWNBYDMA; |
| 322 | #endif |
| 323 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 324 | /* Flush modified buffer descriptor */ |
| 325 | flush_dcache_range((unsigned long)desc_p, |
| 326 | (unsigned long)desc_p + sizeof(struct dmamacdescr)); |
| 327 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 328 | /* Test the wrap-around condition. */ |
| 329 | if (++desc_num >= CONFIG_TX_DESCR_NUM) |
| 330 | desc_num = 0; |
| 331 | |
| 332 | priv->tx_currdescnum = desc_num; |
| 333 | |
| 334 | /* Start the transmission */ |
| 335 | writel(POLL_DATA, &dma_p->txpolldemand); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int dw_eth_recv(struct eth_device *dev) |
| 341 | { |
| 342 | struct dw_eth_dev *priv = dev->priv; |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 343 | u32 status, desc_num = priv->rx_currdescnum; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 344 | struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num]; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 345 | int length = 0; |
| 346 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 347 | /* Invalidate entire buffer descriptor */ |
| 348 | invalidate_dcache_range((unsigned long)desc_p, |
| 349 | (unsigned long)desc_p + |
| 350 | sizeof(struct dmamacdescr)); |
| 351 | |
| 352 | status = desc_p->txrx_status; |
| 353 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 354 | /* Check if the owner is the CPU */ |
| 355 | if (!(status & DESC_RXSTS_OWNBYDMA)) { |
| 356 | |
| 357 | length = (status & DESC_RXSTS_FRMLENMSK) >> \ |
| 358 | DESC_RXSTS_FRMLENSHFT; |
| 359 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 360 | /* Invalidate received data */ |
| 361 | invalidate_dcache_range((unsigned long)desc_p->dmamac_addr, |
| 362 | (unsigned long)desc_p->dmamac_addr + |
Ian Campbell | 0e690fd | 2014-05-08 22:26:33 +0100 | [diff] [blame] | 363 | roundup(length, ARCH_DMA_MINALIGN)); |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 364 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 365 | NetReceive(desc_p->dmamac_addr, length); |
| 366 | |
| 367 | /* |
| 368 | * Make the current descriptor valid again and go to |
| 369 | * the next one |
| 370 | */ |
| 371 | desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA; |
| 372 | |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 373 | /* Flush only status field - others weren't changed */ |
| 374 | flush_dcache_range((unsigned long)&desc_p->txrx_status, |
| 375 | (unsigned long)&desc_p->txrx_status + |
Marek Vasut | 16ef0a8 | 2014-09-15 00:50:37 +0200 | [diff] [blame^] | 376 | roundup(sizeof(desc_p->txrx_status), |
| 377 | ARCH_DMA_MINALIGN)); |
Alexey Brodkin | 0d3b22e | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 378 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 379 | /* Test the wrap-around condition. */ |
| 380 | if (++desc_num >= CONFIG_RX_DESCR_NUM) |
| 381 | desc_num = 0; |
| 382 | } |
| 383 | |
| 384 | priv->rx_currdescnum = desc_num; |
| 385 | |
| 386 | return length; |
| 387 | } |
| 388 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 389 | static int dw_phy_init(struct eth_device *dev) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 390 | { |
| 391 | struct dw_eth_dev *priv = dev->priv; |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 392 | struct phy_device *phydev; |
| 393 | int mask = 0xffffffff; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 394 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 395 | #ifdef CONFIG_PHY_ADDR |
| 396 | mask = 1 << CONFIG_PHY_ADDR; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 397 | #endif |
| 398 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 399 | phydev = phy_find_by_mask(priv->bus, mask, priv->interface); |
| 400 | if (!phydev) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 401 | return -1; |
| 402 | |
Ian Campbell | 7d55550 | 2014-04-28 20:14:05 +0100 | [diff] [blame] | 403 | phy_connect_dev(phydev, dev); |
| 404 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 405 | phydev->supported &= PHY_GBIT_FEATURES; |
| 406 | phydev->advertising = phydev->supported; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 407 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 408 | priv->phydev = phydev; |
| 409 | phy_config(phydev); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 410 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 411 | return 1; |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 412 | } |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 413 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 414 | int designware_initialize(ulong base_addr, u32 interface) |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 415 | { |
| 416 | struct eth_device *dev; |
| 417 | struct dw_eth_dev *priv; |
| 418 | |
| 419 | dev = (struct eth_device *) malloc(sizeof(struct eth_device)); |
| 420 | if (!dev) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | /* |
| 424 | * Since the priv structure contains the descriptors which need a strict |
| 425 | * buswidth alignment, memalign is used to allocate memory |
| 426 | */ |
Ian Campbell | 07c92fc | 2014-05-08 22:26:32 +0100 | [diff] [blame] | 427 | priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN, |
| 428 | sizeof(struct dw_eth_dev)); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 429 | if (!priv) { |
| 430 | free(dev); |
| 431 | return -ENOMEM; |
| 432 | } |
| 433 | |
| 434 | memset(dev, 0, sizeof(struct eth_device)); |
| 435 | memset(priv, 0, sizeof(struct dw_eth_dev)); |
| 436 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 437 | sprintf(dev->name, "dwmac.%lx", base_addr); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 438 | dev->iobase = (int)base_addr; |
| 439 | dev->priv = priv; |
| 440 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 441 | priv->dev = dev; |
| 442 | priv->mac_regs_p = (struct eth_mac_regs *)base_addr; |
| 443 | priv->dma_regs_p = (struct eth_dma_regs *)(base_addr + |
| 444 | DW_DMA_BASE_OFFSET); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 445 | |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 446 | dev->init = dw_eth_init; |
| 447 | dev->send = dw_eth_send; |
| 448 | dev->recv = dw_eth_recv; |
| 449 | dev->halt = dw_eth_halt; |
| 450 | dev->write_hwaddr = dw_write_hwaddr; |
| 451 | |
| 452 | eth_register(dev); |
| 453 | |
Alexey Brodkin | 9a0b130 | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 454 | priv->interface = interface; |
| 455 | |
| 456 | dw_mdio_init(dev->name, priv->mac_regs_p); |
| 457 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 458 | |
| 459 | return dw_phy_init(dev); |
Vipin KUMAR | 1f87312 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 460 | } |