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