Vipin KUMAR | 5b1b188 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 9 | * Designware ethernet IP driver for U-Boot |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 14 | #include <errno.h> |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 15 | #include <miiphy.h> |
| 16 | #include <malloc.h> |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 17 | #include <pci.h> |
Stefan Roese | ef76025 | 2012-05-07 12:04:25 +0200 | [diff] [blame] | 18 | #include <linux/compiler.h> |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <asm/io.h> |
| 21 | #include "designware.h" |
| 22 | |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 25 | static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 26 | { |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 27 | #ifdef CONFIG_DM_ETH |
| 28 | struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv); |
| 29 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 30 | #else |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 31 | struct eth_mac_regs *mac_p = bus->priv; |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 32 | #endif |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 33 | ulong start; |
| 34 | u16 miiaddr; |
| 35 | int timeout = CONFIG_MDIO_TIMEOUT; |
| 36 | |
| 37 | miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | |
| 38 | ((reg << MIIREGSHIFT) & MII_REGMSK); |
| 39 | |
| 40 | writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr); |
| 41 | |
| 42 | start = get_timer(0); |
| 43 | while (get_timer(start) < timeout) { |
| 44 | if (!(readl(&mac_p->miiaddr) & MII_BUSY)) |
| 45 | return readl(&mac_p->miidata); |
| 46 | udelay(10); |
| 47 | }; |
| 48 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 49 | return -ETIMEDOUT; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 53 | u16 val) |
| 54 | { |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 55 | #ifdef CONFIG_DM_ETH |
| 56 | struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv); |
| 57 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 58 | #else |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 59 | struct eth_mac_regs *mac_p = bus->priv; |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 60 | #endif |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 61 | ulong start; |
| 62 | u16 miiaddr; |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 63 | int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 64 | |
| 65 | writel(val, &mac_p->miidata); |
| 66 | miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | |
| 67 | ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE; |
| 68 | |
| 69 | writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr); |
| 70 | |
| 71 | start = get_timer(0); |
| 72 | while (get_timer(start) < timeout) { |
| 73 | if (!(readl(&mac_p->miiaddr) & MII_BUSY)) { |
| 74 | ret = 0; |
| 75 | break; |
| 76 | } |
| 77 | udelay(10); |
| 78 | }; |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 83 | #if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO) |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 84 | static int dw_mdio_reset(struct mii_dev *bus) |
| 85 | { |
| 86 | struct udevice *dev = bus->priv; |
| 87 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 88 | struct dw_eth_pdata *pdata = dev_get_platdata(dev); |
| 89 | int ret; |
| 90 | |
| 91 | if (!dm_gpio_is_valid(&priv->reset_gpio)) |
| 92 | return 0; |
| 93 | |
| 94 | /* reset the phy */ |
| 95 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | udelay(pdata->reset_delays[0]); |
| 100 | |
| 101 | ret = dm_gpio_set_value(&priv->reset_gpio, 1); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | udelay(pdata->reset_delays[1]); |
| 106 | |
| 107 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | udelay(pdata->reset_delays[2]); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | static int dw_mdio_init(const char *name, void *priv) |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 118 | { |
| 119 | struct mii_dev *bus = mdio_alloc(); |
| 120 | |
| 121 | if (!bus) { |
| 122 | printf("Failed to allocate MDIO bus\n"); |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 123 | return -ENOMEM; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bus->read = dw_mdio_read; |
| 127 | bus->write = dw_mdio_write; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 128 | snprintf(bus->name, sizeof(bus->name), "%s", name); |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 129 | #if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO) |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 130 | bus->reset = dw_mdio_reset; |
| 131 | #endif |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 132 | |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 133 | bus->priv = priv; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 134 | |
| 135 | return mdio_register(bus); |
| 136 | } |
Vipin Kumar | 13edd17 | 2012-03-26 00:09:56 +0000 | [diff] [blame] | 137 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 138 | static void tx_descs_init(struct dw_eth_dev *priv) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 139 | { |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 140 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 141 | struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0]; |
| 142 | char *txbuffs = &priv->txbuffs[0]; |
| 143 | struct dmamacdescr *desc_p; |
| 144 | u32 idx; |
| 145 | |
| 146 | for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) { |
| 147 | desc_p = &desc_table_p[idx]; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 148 | desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE]; |
| 149 | desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1]; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 150 | |
| 151 | #if defined(CONFIG_DW_ALTDESCRIPTOR) |
| 152 | desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST | |
Marek Vasut | 2b26109 | 2015-12-20 03:59:23 +0100 | [diff] [blame] | 153 | DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | |
| 154 | DESC_TXSTS_TXCHECKINSCTRL | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 155 | DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS); |
| 156 | |
| 157 | desc_p->txrx_status |= DESC_TXSTS_TXCHAIN; |
| 158 | desc_p->dmamac_cntl = 0; |
| 159 | desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA); |
| 160 | #else |
| 161 | desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN; |
| 162 | desc_p->txrx_status = 0; |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | /* Correcting the last pointer of the chain */ |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 167 | desc_p->dmamac_next = (ulong)&desc_table_p[0]; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 168 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 169 | /* Flush all Tx buffer descriptors at once */ |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 170 | flush_dcache_range((ulong)priv->tx_mac_descrtable, |
| 171 | (ulong)priv->tx_mac_descrtable + |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 172 | sizeof(priv->tx_mac_descrtable)); |
| 173 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 174 | writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr); |
Alexey Brodkin | 74cb708 | 2014-01-13 13:28:38 +0400 | [diff] [blame] | 175 | priv->tx_currdescnum = 0; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 176 | } |
| 177 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 178 | static void rx_descs_init(struct dw_eth_dev *priv) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 179 | { |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 180 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 181 | struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0]; |
| 182 | char *rxbuffs = &priv->rxbuffs[0]; |
| 183 | struct dmamacdescr *desc_p; |
| 184 | u32 idx; |
| 185 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 186 | /* Before passing buffers to GMAC we need to make sure zeros |
| 187 | * written there right after "priv" structure allocation were |
| 188 | * flushed into RAM. |
| 189 | * Otherwise there's a chance to get some of them flushed in RAM when |
| 190 | * GMAC is already pushing data to RAM via DMA. This way incoming from |
| 191 | * GMAC data will be corrupted. */ |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 192 | flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE); |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 193 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 194 | for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) { |
| 195 | desc_p = &desc_table_p[idx]; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 196 | desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]; |
| 197 | desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1]; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 198 | |
| 199 | desc_p->dmamac_cntl = |
Marek Vasut | 2b26109 | 2015-12-20 03:59:23 +0100 | [diff] [blame] | 200 | (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 201 | DESC_RXCTRL_RXCHAIN; |
| 202 | |
| 203 | desc_p->txrx_status = DESC_RXSTS_OWNBYDMA; |
| 204 | } |
| 205 | |
| 206 | /* Correcting the last pointer of the chain */ |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 207 | desc_p->dmamac_next = (ulong)&desc_table_p[0]; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 208 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 209 | /* Flush all Rx buffer descriptors at once */ |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 210 | flush_dcache_range((ulong)priv->rx_mac_descrtable, |
| 211 | (ulong)priv->rx_mac_descrtable + |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 212 | sizeof(priv->rx_mac_descrtable)); |
| 213 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 214 | writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr); |
Alexey Brodkin | 74cb708 | 2014-01-13 13:28:38 +0400 | [diff] [blame] | 215 | priv->rx_currdescnum = 0; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 216 | } |
| 217 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 218 | static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 219 | { |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 220 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 221 | u32 macid_lo, macid_hi; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 222 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 223 | macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) + |
| 224 | (mac_id[3] << 24); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 225 | macid_hi = mac_id[4] + (mac_id[5] << 8); |
| 226 | |
| 227 | writel(macid_hi, &mac_p->macaddr0hi); |
| 228 | writel(macid_lo, &mac_p->macaddr0lo); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Simon Glass | 0ea38db | 2017-01-11 11:46:08 +0100 | [diff] [blame] | 233 | static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p, |
| 234 | struct phy_device *phydev) |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 235 | { |
| 236 | u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN; |
| 237 | |
| 238 | if (!phydev->link) { |
| 239 | printf("%s: No link.\n", phydev->dev->name); |
Simon Glass | 0ea38db | 2017-01-11 11:46:08 +0100 | [diff] [blame] | 240 | return 0; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (phydev->speed != 1000) |
| 244 | conf |= MII_PORTSELECT; |
Alexey Brodkin | b884c3f | 2016-01-13 16:59:36 +0300 | [diff] [blame] | 245 | else |
| 246 | conf &= ~MII_PORTSELECT; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 247 | |
| 248 | if (phydev->speed == 100) |
| 249 | conf |= FES_100; |
| 250 | |
| 251 | if (phydev->duplex) |
| 252 | conf |= FULLDPLXMODE; |
| 253 | |
| 254 | writel(conf, &mac_p->conf); |
| 255 | |
| 256 | printf("Speed: %d, %s duplex%s\n", phydev->speed, |
| 257 | (phydev->duplex) ? "full" : "half", |
| 258 | (phydev->port == PORT_FIBRE) ? ", fiber mode" : ""); |
Simon Glass | 0ea38db | 2017-01-11 11:46:08 +0100 | [diff] [blame] | 259 | |
| 260 | return 0; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 261 | } |
| 262 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 263 | static void _dw_eth_halt(struct dw_eth_dev *priv) |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 264 | { |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 265 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 266 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 267 | |
| 268 | writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf); |
| 269 | writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode); |
| 270 | |
| 271 | phy_shutdown(priv->phydev); |
| 272 | } |
| 273 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 274 | int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 275 | { |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 276 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 277 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 278 | unsigned int start; |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 279 | int ret; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 280 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 281 | writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode); |
Vipin Kumar | 13edd17 | 2012-03-26 00:09:56 +0000 | [diff] [blame] | 282 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 283 | start = get_timer(0); |
| 284 | while (readl(&dma_p->busmode) & DMAMAC_SRST) { |
Alexey Brodkin | 875143f | 2015-01-13 17:10:24 +0300 | [diff] [blame] | 285 | if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) { |
| 286 | printf("DMA reset timeout\n"); |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 287 | return -ETIMEDOUT; |
Alexey Brodkin | 875143f | 2015-01-13 17:10:24 +0300 | [diff] [blame] | 288 | } |
Stefan Roese | ef76025 | 2012-05-07 12:04:25 +0200 | [diff] [blame] | 289 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 290 | mdelay(100); |
| 291 | }; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 292 | |
Bin Meng | f3edfd3 | 2015-06-15 18:40:19 +0800 | [diff] [blame] | 293 | /* |
| 294 | * Soft reset above clears HW address registers. |
| 295 | * So we have to set it here once again. |
| 296 | */ |
| 297 | _dw_write_hwaddr(priv, enetaddr); |
| 298 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 299 | rx_descs_init(priv); |
| 300 | tx_descs_init(priv); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 301 | |
Ian Campbell | 49692c5 | 2014-05-08 22:26:35 +0100 | [diff] [blame] | 302 | writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 303 | |
Sonic Zhang | d227922 | 2015-01-29 14:38:50 +0800 | [diff] [blame] | 304 | #ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 305 | writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD, |
| 306 | &dma_p->opmode); |
Sonic Zhang | d227922 | 2015-01-29 14:38:50 +0800 | [diff] [blame] | 307 | #else |
| 308 | writel(readl(&dma_p->opmode) | FLUSHTXFIFO, |
| 309 | &dma_p->opmode); |
| 310 | #endif |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 311 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 312 | writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 313 | |
Sonic Zhang | 2ddaf13 | 2015-01-29 13:37:31 +0800 | [diff] [blame] | 314 | #ifdef CONFIG_DW_AXI_BURST_LEN |
| 315 | writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus); |
| 316 | #endif |
| 317 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 318 | /* Start up the PHY */ |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 319 | ret = phy_startup(priv->phydev); |
| 320 | if (ret) { |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 321 | printf("Could not initialize PHY %s\n", |
| 322 | priv->phydev->dev->name); |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 323 | return ret; |
Vipin Kumar | 9afc1af | 2012-05-07 13:06:44 +0530 | [diff] [blame] | 324 | } |
| 325 | |
Simon Glass | 0ea38db | 2017-01-11 11:46:08 +0100 | [diff] [blame] | 326 | ret = dw_adjust_link(priv, mac_p, priv->phydev); |
| 327 | if (ret) |
| 328 | return ret; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 329 | |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 333 | int designware_eth_enable(struct dw_eth_dev *priv) |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 334 | { |
| 335 | struct eth_mac_regs *mac_p = priv->mac_regs_p; |
| 336 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 337 | if (!priv->phydev->link) |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 338 | return -EIO; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 339 | |
Armando Visconti | aa51005 | 2012-03-26 00:09:55 +0000 | [diff] [blame] | 340 | writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 345 | static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 346 | { |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 347 | struct eth_dma_regs *dma_p = priv->dma_regs_p; |
| 348 | u32 desc_num = priv->tx_currdescnum; |
| 349 | struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num]; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 350 | ulong desc_start = (ulong)desc_p; |
| 351 | ulong desc_end = desc_start + |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 352 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 353 | ulong data_start = desc_p->dmamac_addr; |
| 354 | ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN); |
Ian Campbell | 964ea7c | 2014-05-08 22:26:33 +0100 | [diff] [blame] | 355 | /* |
| 356 | * Strictly we only need to invalidate the "txrx_status" field |
| 357 | * for the following check, but on some platforms we cannot |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 358 | * invalidate only 4 bytes, so we flush the entire descriptor, |
| 359 | * which is 16 bytes in total. This is safe because the |
| 360 | * individual descriptors in the array are each aligned to |
| 361 | * ARCH_DMA_MINALIGN and padded appropriately. |
Ian Campbell | 964ea7c | 2014-05-08 22:26:33 +0100 | [diff] [blame] | 362 | */ |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 363 | invalidate_dcache_range(desc_start, desc_end); |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 364 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 365 | /* Check if the descriptor is owned by CPU */ |
| 366 | if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) { |
| 367 | printf("CPU not owner of tx frame\n"); |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 368 | return -EPERM; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 369 | } |
| 370 | |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 371 | memcpy((void *)data_start, packet, length); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 372 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 373 | /* Flush data to be sent */ |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 374 | flush_dcache_range(data_start, data_end); |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 375 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 376 | #if defined(CONFIG_DW_ALTDESCRIPTOR) |
| 377 | desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST; |
Marek Vasut | 2b26109 | 2015-12-20 03:59:23 +0100 | [diff] [blame] | 378 | desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 379 | DESC_TXCTRL_SIZE1MASK; |
| 380 | |
| 381 | desc_p->txrx_status &= ~(DESC_TXSTS_MSK); |
| 382 | desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA; |
| 383 | #else |
Marek Vasut | 2b26109 | 2015-12-20 03:59:23 +0100 | [diff] [blame] | 384 | desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & |
| 385 | DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 386 | DESC_TXCTRL_TXFIRST; |
| 387 | |
| 388 | desc_p->txrx_status = DESC_TXSTS_OWNBYDMA; |
| 389 | #endif |
| 390 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 391 | /* Flush modified buffer descriptor */ |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 392 | flush_dcache_range(desc_start, desc_end); |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 393 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 394 | /* Test the wrap-around condition. */ |
| 395 | if (++desc_num >= CONFIG_TX_DESCR_NUM) |
| 396 | desc_num = 0; |
| 397 | |
| 398 | priv->tx_currdescnum = desc_num; |
| 399 | |
| 400 | /* Start the transmission */ |
| 401 | writel(POLL_DATA, &dma_p->txpolldemand); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 406 | static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 407 | { |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 408 | u32 status, desc_num = priv->rx_currdescnum; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 409 | struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num]; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 410 | int length = -EAGAIN; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 411 | ulong desc_start = (ulong)desc_p; |
| 412 | ulong desc_end = desc_start + |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 413 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 414 | ulong data_start = desc_p->dmamac_addr; |
| 415 | ulong data_end; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 416 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 417 | /* Invalidate entire buffer descriptor */ |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 418 | invalidate_dcache_range(desc_start, desc_end); |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 419 | |
| 420 | status = desc_p->txrx_status; |
| 421 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 422 | /* Check if the owner is the CPU */ |
| 423 | if (!(status & DESC_RXSTS_OWNBYDMA)) { |
| 424 | |
Marek Vasut | 2b26109 | 2015-12-20 03:59:23 +0100 | [diff] [blame] | 425 | length = (status & DESC_RXSTS_FRMLENMSK) >> |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 426 | DESC_RXSTS_FRMLENSHFT; |
| 427 | |
Alexey Brodkin | 50b0df8 | 2014-01-22 20:49:09 +0400 | [diff] [blame] | 428 | /* Invalidate received data */ |
Marek Vasut | 96cec17 | 2014-09-15 01:05:23 +0200 | [diff] [blame] | 429 | data_end = data_start + roundup(length, ARCH_DMA_MINALIGN); |
| 430 | invalidate_dcache_range(data_start, data_end); |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 431 | *packetp = (uchar *)(ulong)desc_p->dmamac_addr; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 432 | } |
| 433 | |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 434 | return length; |
| 435 | } |
| 436 | |
| 437 | static int _dw_free_pkt(struct dw_eth_dev *priv) |
| 438 | { |
| 439 | u32 desc_num = priv->rx_currdescnum; |
| 440 | struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num]; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 441 | ulong desc_start = (ulong)desc_p; |
| 442 | ulong desc_end = desc_start + |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 443 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
| 444 | |
| 445 | /* |
| 446 | * Make the current descriptor valid again and go to |
| 447 | * the next one |
| 448 | */ |
| 449 | desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA; |
| 450 | |
| 451 | /* Flush only status field - others weren't changed */ |
| 452 | flush_dcache_range(desc_start, desc_end); |
| 453 | |
| 454 | /* Test the wrap-around condition. */ |
| 455 | if (++desc_num >= CONFIG_RX_DESCR_NUM) |
| 456 | desc_num = 0; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 457 | priv->rx_currdescnum = desc_num; |
| 458 | |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 459 | return 0; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 460 | } |
| 461 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 462 | static int dw_phy_init(struct dw_eth_dev *priv, void *dev) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 463 | { |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 464 | struct phy_device *phydev; |
Alexey Brodkin | 6968ec9 | 2016-01-13 16:59:37 +0300 | [diff] [blame] | 465 | int mask = 0xffffffff, ret; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 466 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 467 | #ifdef CONFIG_PHY_ADDR |
| 468 | mask = 1 << CONFIG_PHY_ADDR; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 469 | #endif |
| 470 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 471 | phydev = phy_find_by_mask(priv->bus, mask, priv->interface); |
| 472 | if (!phydev) |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 473 | return -ENODEV; |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 474 | |
Ian Campbell | 15e82e5 | 2014-04-28 20:14:05 +0100 | [diff] [blame] | 475 | phy_connect_dev(phydev, dev); |
| 476 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 477 | phydev->supported &= PHY_GBIT_FEATURES; |
Alexey Brodkin | 6968ec9 | 2016-01-13 16:59:37 +0300 | [diff] [blame] | 478 | if (priv->max_speed) { |
| 479 | ret = phy_set_supported(phydev, priv->max_speed); |
| 480 | if (ret) |
| 481 | return ret; |
| 482 | } |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 483 | phydev->advertising = phydev->supported; |
| 484 | |
| 485 | priv->phydev = phydev; |
| 486 | phy_config(phydev); |
| 487 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 491 | #ifndef CONFIG_DM_ETH |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 492 | static int dw_eth_init(struct eth_device *dev, bd_t *bis) |
| 493 | { |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 494 | int ret; |
| 495 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 496 | ret = designware_eth_init(dev->priv, dev->enetaddr); |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 497 | if (!ret) |
| 498 | ret = designware_eth_enable(dev->priv); |
| 499 | |
| 500 | return ret; |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int dw_eth_send(struct eth_device *dev, void *packet, int length) |
| 504 | { |
| 505 | return _dw_eth_send(dev->priv, packet, length); |
| 506 | } |
| 507 | |
| 508 | static int dw_eth_recv(struct eth_device *dev) |
| 509 | { |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 510 | uchar *packet; |
| 511 | int length; |
| 512 | |
| 513 | length = _dw_eth_recv(dev->priv, &packet); |
| 514 | if (length == -EAGAIN) |
| 515 | return 0; |
| 516 | net_process_received_packet(packet, length); |
| 517 | |
| 518 | _dw_free_pkt(dev->priv); |
| 519 | |
| 520 | return 0; |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static void dw_eth_halt(struct eth_device *dev) |
| 524 | { |
| 525 | return _dw_eth_halt(dev->priv); |
| 526 | } |
| 527 | |
| 528 | static int dw_write_hwaddr(struct eth_device *dev) |
| 529 | { |
| 530 | return _dw_write_hwaddr(dev->priv, dev->enetaddr); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 531 | } |
| 532 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 533 | int designware_initialize(ulong base_addr, u32 interface) |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 534 | { |
| 535 | struct eth_device *dev; |
| 536 | struct dw_eth_dev *priv; |
| 537 | |
| 538 | dev = (struct eth_device *) malloc(sizeof(struct eth_device)); |
| 539 | if (!dev) |
| 540 | return -ENOMEM; |
| 541 | |
| 542 | /* |
| 543 | * Since the priv structure contains the descriptors which need a strict |
| 544 | * buswidth alignment, memalign is used to allocate memory |
| 545 | */ |
Ian Campbell | 1c848a2 | 2014-05-08 22:26:32 +0100 | [diff] [blame] | 546 | priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN, |
| 547 | sizeof(struct dw_eth_dev)); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 548 | if (!priv) { |
| 549 | free(dev); |
| 550 | return -ENOMEM; |
| 551 | } |
| 552 | |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 553 | if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) { |
| 554 | printf("designware: buffers are outside DMA memory\n"); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 558 | memset(dev, 0, sizeof(struct eth_device)); |
| 559 | memset(priv, 0, sizeof(struct dw_eth_dev)); |
| 560 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 561 | sprintf(dev->name, "dwmac.%lx", base_addr); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 562 | dev->iobase = (int)base_addr; |
| 563 | dev->priv = priv; |
| 564 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 565 | priv->dev = dev; |
| 566 | priv->mac_regs_p = (struct eth_mac_regs *)base_addr; |
| 567 | priv->dma_regs_p = (struct eth_dma_regs *)(base_addr + |
| 568 | DW_DMA_BASE_OFFSET); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 569 | |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 570 | dev->init = dw_eth_init; |
| 571 | dev->send = dw_eth_send; |
| 572 | dev->recv = dw_eth_recv; |
| 573 | dev->halt = dw_eth_halt; |
| 574 | dev->write_hwaddr = dw_write_hwaddr; |
| 575 | |
| 576 | eth_register(dev); |
| 577 | |
Alexey Brodkin | 92a190a | 2014-01-22 20:54:06 +0400 | [diff] [blame] | 578 | priv->interface = interface; |
| 579 | |
| 580 | dw_mdio_init(dev->name, priv->mac_regs_p); |
| 581 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 582 | |
Simon Glass | 64dcd25 | 2015-04-05 16:07:40 -0600 | [diff] [blame] | 583 | return dw_phy_init(priv, dev); |
Vipin KUMAR | 5b1b188 | 2010-06-29 10:53:34 +0530 | [diff] [blame] | 584 | } |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 585 | #endif |
| 586 | |
| 587 | #ifdef CONFIG_DM_ETH |
| 588 | static int designware_eth_start(struct udevice *dev) |
| 589 | { |
| 590 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 591 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 592 | int ret; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 593 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 594 | ret = designware_eth_init(priv, pdata->enetaddr); |
Simon Glass | f63f28e | 2017-01-11 11:46:09 +0100 | [diff] [blame] | 595 | if (ret) |
| 596 | return ret; |
| 597 | ret = designware_eth_enable(priv); |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | |
| 601 | return 0; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 602 | } |
| 603 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 604 | int designware_eth_send(struct udevice *dev, void *packet, int length) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 605 | { |
| 606 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 607 | |
| 608 | return _dw_eth_send(priv, packet, length); |
| 609 | } |
| 610 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 611 | int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 612 | { |
| 613 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 614 | |
| 615 | return _dw_eth_recv(priv, packetp); |
| 616 | } |
| 617 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 618 | int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 619 | { |
| 620 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 621 | |
| 622 | return _dw_free_pkt(priv); |
| 623 | } |
| 624 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 625 | void designware_eth_stop(struct udevice *dev) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 626 | { |
| 627 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 628 | |
| 629 | return _dw_eth_halt(priv); |
| 630 | } |
| 631 | |
Simon Glass | e72ced2 | 2017-01-11 11:46:10 +0100 | [diff] [blame] | 632 | int designware_eth_write_hwaddr(struct udevice *dev) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 633 | { |
| 634 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 635 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 636 | |
| 637 | return _dw_write_hwaddr(priv, pdata->enetaddr); |
| 638 | } |
| 639 | |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 640 | static int designware_eth_bind(struct udevice *dev) |
| 641 | { |
| 642 | #ifdef CONFIG_DM_PCI |
| 643 | static int num_cards; |
| 644 | char name[20]; |
| 645 | |
| 646 | /* Create a unique device name for PCI type devices */ |
| 647 | if (device_is_on_pci_bus(dev)) { |
| 648 | sprintf(name, "eth_designware#%u", num_cards++); |
| 649 | device_set_name(dev, name); |
| 650 | } |
| 651 | #endif |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Sjoerd Simons | b9e08d0 | 2017-01-11 11:46:07 +0100 | [diff] [blame] | 656 | int designware_eth_probe(struct udevice *dev) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 657 | { |
| 658 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 659 | struct dw_eth_dev *priv = dev_get_priv(dev); |
Bin Meng | f0dc73c | 2015-09-03 05:37:29 -0700 | [diff] [blame] | 660 | u32 iobase = pdata->iobase; |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 661 | ulong ioaddr; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 662 | int ret; |
| 663 | |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 664 | #ifdef CONFIG_DM_PCI |
| 665 | /* |
| 666 | * If we are on PCI bus, either directly attached to a PCI root port, |
| 667 | * or via a PCI bridge, fill in platdata before we probe the hardware. |
| 668 | */ |
| 669 | if (device_is_on_pci_bus(dev)) { |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 670 | dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase); |
| 671 | iobase &= PCI_BASE_ADDRESS_MEM_MASK; |
Bin Meng | 6758a6c | 2016-02-02 05:58:00 -0800 | [diff] [blame] | 672 | iobase = dm_pci_mem_to_phys(dev, iobase); |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 673 | |
| 674 | pdata->iobase = iobase; |
| 675 | pdata->phy_interface = PHY_INTERFACE_MODE_RMII; |
| 676 | } |
| 677 | #endif |
| 678 | |
Bin Meng | f0dc73c | 2015-09-03 05:37:29 -0700 | [diff] [blame] | 679 | debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv); |
Beniamino Galvani | 0e1a3e3 | 2016-05-08 08:30:15 +0200 | [diff] [blame] | 680 | ioaddr = iobase; |
| 681 | priv->mac_regs_p = (struct eth_mac_regs *)ioaddr; |
| 682 | priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET); |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 683 | priv->interface = pdata->phy_interface; |
Alexey Brodkin | 6968ec9 | 2016-01-13 16:59:37 +0300 | [diff] [blame] | 684 | priv->max_speed = pdata->max_speed; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 685 | |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 686 | dw_mdio_init(dev->name, dev); |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 687 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 688 | |
| 689 | ret = dw_phy_init(priv, dev); |
| 690 | debug("%s, ret=%d\n", __func__, ret); |
| 691 | |
| 692 | return ret; |
| 693 | } |
| 694 | |
Bin Meng | 5d2459f | 2015-10-07 21:32:38 -0700 | [diff] [blame] | 695 | static int designware_eth_remove(struct udevice *dev) |
| 696 | { |
| 697 | struct dw_eth_dev *priv = dev_get_priv(dev); |
| 698 | |
| 699 | free(priv->phydev); |
| 700 | mdio_unregister(priv->bus); |
| 701 | mdio_free(priv->bus); |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Sjoerd Simons | b9e08d0 | 2017-01-11 11:46:07 +0100 | [diff] [blame] | 706 | const struct eth_ops designware_eth_ops = { |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 707 | .start = designware_eth_start, |
| 708 | .send = designware_eth_send, |
| 709 | .recv = designware_eth_recv, |
| 710 | .free_pkt = designware_eth_free_pkt, |
| 711 | .stop = designware_eth_stop, |
| 712 | .write_hwaddr = designware_eth_write_hwaddr, |
| 713 | }; |
| 714 | |
Sjoerd Simons | b9e08d0 | 2017-01-11 11:46:07 +0100 | [diff] [blame] | 715 | int designware_eth_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 716 | { |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 717 | struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev); |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 718 | #ifdef CONFIG_DM_GPIO |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 719 | struct dw_eth_dev *priv = dev_get_priv(dev); |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 720 | #endif |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 721 | struct eth_pdata *pdata = &dw_pdata->eth_pdata; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 722 | const char *phy_mode; |
Alexey Brodkin | 6968ec9 | 2016-01-13 16:59:37 +0300 | [diff] [blame] | 723 | const fdt32_t *cell; |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 724 | #ifdef CONFIG_DM_GPIO |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 725 | int reset_flags = GPIOD_IS_OUT; |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 726 | #endif |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 727 | int ret = 0; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 728 | |
| 729 | pdata->iobase = dev_get_addr(dev); |
| 730 | pdata->phy_interface = -1; |
| 731 | phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL); |
| 732 | if (phy_mode) |
| 733 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 734 | if (pdata->phy_interface == -1) { |
| 735 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | |
Alexey Brodkin | 6968ec9 | 2016-01-13 16:59:37 +0300 | [diff] [blame] | 739 | pdata->max_speed = 0; |
| 740 | cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL); |
| 741 | if (cell) |
| 742 | pdata->max_speed = fdt32_to_cpu(*cell); |
| 743 | |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 744 | #ifdef CONFIG_DM_GPIO |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 745 | if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, |
| 746 | "snps,reset-active-low")) |
| 747 | reset_flags |= GPIOD_ACTIVE_LOW; |
| 748 | |
| 749 | ret = gpio_request_by_name(dev, "snps,reset-gpio", 0, |
| 750 | &priv->reset_gpio, reset_flags); |
| 751 | if (ret == 0) { |
| 752 | ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset, |
| 753 | "snps,reset-delays-us", dw_pdata->reset_delays, 3); |
| 754 | } else if (ret == -ENOENT) { |
| 755 | ret = 0; |
| 756 | } |
Alexey Brodkin | 66d027e | 2016-06-27 13:17:51 +0300 | [diff] [blame] | 757 | #endif |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 758 | |
| 759 | return ret; |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static const struct udevice_id designware_eth_ids[] = { |
| 763 | { .compatible = "allwinner,sun7i-a20-gmac" }, |
Marek Vasut | b962859 | 2015-07-25 18:38:44 +0200 | [diff] [blame] | 764 | { .compatible = "altr,socfpga-stmmac" }, |
Beniamino Galvani | cfe2556 | 2016-08-16 11:49:50 +0200 | [diff] [blame] | 765 | { .compatible = "amlogic,meson6-dwmac" }, |
Heiner Kallweit | 655217d | 2017-01-27 21:25:59 +0100 | [diff] [blame^] | 766 | { .compatible = "amlogic,meson-gx-dwmac" }, |
Michael Kurz | b20b70f | 2017-01-22 16:04:27 +0100 | [diff] [blame] | 767 | { .compatible = "st,stm32-dwmac" }, |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 768 | { } |
| 769 | }; |
| 770 | |
Marek Vasut | 9f76f10 | 2015-07-25 18:42:34 +0200 | [diff] [blame] | 771 | U_BOOT_DRIVER(eth_designware) = { |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 772 | .name = "eth_designware", |
| 773 | .id = UCLASS_ETH, |
| 774 | .of_match = designware_eth_ids, |
| 775 | .ofdata_to_platdata = designware_eth_ofdata_to_platdata, |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 776 | .bind = designware_eth_bind, |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 777 | .probe = designware_eth_probe, |
Bin Meng | 5d2459f | 2015-10-07 21:32:38 -0700 | [diff] [blame] | 778 | .remove = designware_eth_remove, |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 779 | .ops = &designware_eth_ops, |
| 780 | .priv_auto_alloc_size = sizeof(struct dw_eth_dev), |
Sjoerd Simons | 90b7fc9 | 2016-02-28 22:24:55 +0100 | [diff] [blame] | 781 | .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata), |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 782 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 783 | }; |
Bin Meng | 8b7ee66 | 2015-09-11 03:24:35 -0700 | [diff] [blame] | 784 | |
| 785 | static struct pci_device_id supported[] = { |
| 786 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) }, |
| 787 | { } |
| 788 | }; |
| 789 | |
| 790 | U_BOOT_PCI_DEVICE(eth_designware, supported); |
Simon Glass | 75577ba | 2015-04-05 16:07:41 -0600 | [diff] [blame] | 791 | #endif |