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