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