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