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