5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * Designware ethernet IP driver for u-boot
31 #include <linux/err.h>
33 #include "designware.h"
35 static void tx_descs_init(struct eth_device *dev)
37 struct dw_eth_dev *priv = dev->priv;
38 struct eth_dma_regs *dma_p = priv->dma_regs_p;
39 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
40 char *txbuffs = &priv->txbuffs[0];
41 struct dmamacdescr *desc_p;
44 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
45 desc_p = &desc_table_p[idx];
46 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
47 desc_p->dmamac_next = &desc_table_p[idx + 1];
49 #if defined(CONFIG_DW_ALTDESCRIPTOR)
50 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
51 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
52 DESC_TXSTS_TXCHECKINSCTRL | \
53 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
55 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
56 desc_p->dmamac_cntl = 0;
57 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
59 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
60 desc_p->txrx_status = 0;
64 /* Correcting the last pointer of the chain */
65 desc_p->dmamac_next = &desc_table_p[0];
67 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
70 static void rx_descs_init(struct eth_device *dev)
72 struct dw_eth_dev *priv = dev->priv;
73 struct eth_dma_regs *dma_p = priv->dma_regs_p;
74 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
75 char *rxbuffs = &priv->rxbuffs[0];
76 struct dmamacdescr *desc_p;
79 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
80 desc_p = &desc_table_p[idx];
81 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
82 desc_p->dmamac_next = &desc_table_p[idx + 1];
85 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
88 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
91 /* Correcting the last pointer of the chain */
92 desc_p->dmamac_next = &desc_table_p[0];
94 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
97 static void descs_init(struct eth_device *dev)
103 static int mac_reset(struct eth_device *dev)
105 struct dw_eth_dev *priv = dev->priv;
106 struct eth_mac_regs *mac_p = priv->mac_regs_p;
107 struct eth_dma_regs *dma_p = priv->dma_regs_p;
109 int timeout = CONFIG_MACRESET_TIMEOUT;
111 writel(DMAMAC_SRST, &dma_p->busmode);
112 writel(MII_PORTSELECT, &mac_p->conf);
115 if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
123 static int dw_write_hwaddr(struct eth_device *dev)
125 struct dw_eth_dev *priv = dev->priv;
126 struct eth_mac_regs *mac_p = priv->mac_regs_p;
127 u32 macid_lo, macid_hi;
128 u8 *mac_id = &dev->enetaddr[0];
130 macid_lo = mac_id[0] + (mac_id[1] << 8) + \
131 (mac_id[2] << 16) + (mac_id[3] << 24);
132 macid_hi = mac_id[4] + (mac_id[5] << 8);
134 writel(macid_hi, &mac_p->macaddr0hi);
135 writel(macid_lo, &mac_p->macaddr0lo);
140 static int dw_eth_init(struct eth_device *dev, bd_t *bis)
142 struct dw_eth_dev *priv = dev->priv;
143 struct eth_mac_regs *mac_p = priv->mac_regs_p;
144 struct eth_dma_regs *dma_p = priv->dma_regs_p;
147 /* Reset ethernet hardware */
148 if (mac_reset(dev) < 0)
151 /* Resore the HW MAC address as it has been lost during MAC reset */
152 dw_write_hwaddr(dev);
154 writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
157 writel(FLUSHTXFIFO | readl(&dma_p->opmode), &dma_p->opmode);
158 writel(STOREFORWARD | TXSECONDFRAME, &dma_p->opmode);
160 conf = FRAMEBURSTENABLE | DISABLERXOWN;
162 if (priv->speed != SPEED_1000M)
163 conf |= MII_PORTSELECT;
165 if (priv->duplex == FULL_DUPLEX)
166 conf |= FULLDPLXMODE;
168 writel(conf, &mac_p->conf);
173 * Start/Enable xfer at dma as well as mac level
175 writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
176 writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
178 writel(readl(&mac_p->conf) | RXENABLE, &mac_p->conf);
179 writel(readl(&mac_p->conf) | TXENABLE, &mac_p->conf);
184 static int dw_eth_send(struct eth_device *dev, volatile void *packet,
187 struct dw_eth_dev *priv = dev->priv;
188 struct eth_dma_regs *dma_p = priv->dma_regs_p;
189 u32 desc_num = priv->tx_currdescnum;
190 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
192 /* Check if the descriptor is owned by CPU */
193 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
194 printf("CPU not owner of tx frame\n");
198 memcpy((void *)desc_p->dmamac_addr, (void *)packet, length);
200 #if defined(CONFIG_DW_ALTDESCRIPTOR)
201 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
202 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
203 DESC_TXCTRL_SIZE1MASK;
205 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
206 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
208 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
209 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
212 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
215 /* Test the wrap-around condition. */
216 if (++desc_num >= CONFIG_TX_DESCR_NUM)
219 priv->tx_currdescnum = desc_num;
221 /* Start the transmission */
222 writel(POLL_DATA, &dma_p->txpolldemand);
227 static int dw_eth_recv(struct eth_device *dev)
229 struct dw_eth_dev *priv = dev->priv;
230 u32 desc_num = priv->rx_currdescnum;
231 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
233 u32 status = desc_p->txrx_status;
236 /* Check if the owner is the CPU */
237 if (!(status & DESC_RXSTS_OWNBYDMA)) {
239 length = (status & DESC_RXSTS_FRMLENMSK) >> \
240 DESC_RXSTS_FRMLENSHFT;
242 NetReceive(desc_p->dmamac_addr, length);
245 * Make the current descriptor valid again and go to
248 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
250 /* Test the wrap-around condition. */
251 if (++desc_num >= CONFIG_RX_DESCR_NUM)
255 priv->rx_currdescnum = desc_num;
260 static void dw_eth_halt(struct eth_device *dev)
262 struct dw_eth_dev *priv = dev->priv;
265 priv->tx_currdescnum = priv->rx_currdescnum = 0;
268 static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
270 struct dw_eth_dev *priv = dev->priv;
271 struct eth_mac_regs *mac_p = priv->mac_regs_p;
273 int timeout = CONFIG_MDIO_TIMEOUT;
275 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
276 ((reg << MIIREGSHIFT) & MII_REGMSK);
278 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
281 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
282 *val = readl(&mac_p->miidata);
291 static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
293 struct dw_eth_dev *priv = dev->priv;
294 struct eth_mac_regs *mac_p = priv->mac_regs_p;
296 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
299 writel(val, &mac_p->miidata);
300 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
301 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
303 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
306 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
313 /* Needed as a fix for ST-Phy */
314 eth_mdio_read(dev, addr, reg, &value);
319 #if defined(CONFIG_DW_SEARCH_PHY)
320 static int find_phy(struct eth_device *dev)
326 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
327 oldctrl = ctrl & BMCR_ANENABLE;
329 ctrl ^= BMCR_ANENABLE;
330 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
331 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
332 ctrl &= BMCR_ANENABLE;
334 if (ctrl == oldctrl) {
337 ctrl ^= BMCR_ANENABLE;
338 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
342 } while (phy_addr < 32);
348 static int dw_reset_phy(struct eth_device *dev)
350 struct dw_eth_dev *priv = dev->priv;
352 int timeout = CONFIG_PHYRESET_TIMEOUT;
353 u32 phy_addr = priv->address;
355 eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
357 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
358 if (!(ctrl & BMCR_RESET))
366 #ifdef CONFIG_PHY_RESET_DELAY
367 udelay(CONFIG_PHY_RESET_DELAY);
372 static int configure_phy(struct eth_device *dev)
374 struct dw_eth_dev *priv = dev->priv;
377 #if defined(CONFIG_DW_AUTONEG)
385 #if defined(CONFIG_DW_SEARCH_PHY)
386 phy_addr = find_phy(dev);
388 priv->address = phy_addr;
392 phy_addr = priv->address;
394 if (dw_reset_phy(dev) < 0)
397 #if defined(CONFIG_DW_AUTONEG)
398 bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_SPEED100 | \
399 BMCR_FULLDPLX | BMCR_SPEED1000;
401 bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
403 #if defined(CONFIG_DW_SPEED10M)
404 bmcr &= ~BMCR_SPEED100;
406 #if defined(CONFIG_DW_DUPLEXHALF)
407 bmcr &= ~BMCR_FULLDPLX;
410 if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
413 /* Read the phy status register and populate priv structure */
414 #if defined(CONFIG_DW_AUTONEG)
415 timeout = CONFIG_AUTONEG_TIMEOUT;
417 eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
418 if (bmsr & BMSR_ANEGCOMPLETE)
423 eth_mdio_read(dev, phy_addr, MII_LPA, &anlpar);
424 eth_mdio_read(dev, phy_addr, MII_STAT1000, &btsr);
426 if (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
427 priv->speed = SPEED_1000M;
428 if (btsr & PHY_1000BTSR_1000FD)
429 priv->duplex = FULL_DUPLEX;
431 priv->duplex = HALF_DUPLEX;
433 if (anlpar & LPA_100)
434 priv->speed = SPEED_100M;
436 priv->speed = SPEED_10M;
438 if (anlpar & (LPA_10FULL | LPA_100FULL))
439 priv->duplex = FULL_DUPLEX;
441 priv->duplex = HALF_DUPLEX;
444 if (eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl) < 0)
447 if (ctrl & BMCR_FULLDPLX)
448 priv->duplex = FULL_DUPLEX;
450 priv->duplex = HALF_DUPLEX;
452 if (ctrl & BMCR_SPEED1000)
453 priv->speed = SPEED_1000M;
454 else if (ctrl & BMCR_SPEED100)
455 priv->speed = SPEED_100M;
457 priv->speed = SPEED_10M;
462 #if defined(CONFIG_MII)
463 static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
465 struct eth_device *dev;
467 dev = eth_get_dev_by_name(devname);
469 eth_mdio_read(dev, addr, reg, val);
474 static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
476 struct eth_device *dev;
478 dev = eth_get_dev_by_name(devname);
480 eth_mdio_write(dev, addr, reg, val);
486 int designware_initialize(u32 id, ulong base_addr, u32 phy_addr)
488 struct eth_device *dev;
489 struct dw_eth_dev *priv;
491 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
496 * Since the priv structure contains the descriptors which need a strict
497 * buswidth alignment, memalign is used to allocate memory
499 priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
505 memset(dev, 0, sizeof(struct eth_device));
506 memset(priv, 0, sizeof(struct dw_eth_dev));
508 sprintf(dev->name, "mii%d", id);
509 dev->iobase = (int)base_addr;
512 eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
515 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
516 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
518 priv->address = phy_addr;
520 if (mac_reset(dev) < 0)
523 if (configure_phy(dev) < 0) {
524 printf("Phy could not be configured\n");
528 dev->init = dw_eth_init;
529 dev->send = dw_eth_send;
530 dev->recv = dw_eth_recv;
531 dev->halt = dw_eth_halt;
532 dev->write_hwaddr = dw_write_hwaddr;
536 #if defined(CONFIG_MII)
537 miiphy_register(dev->name, dw_mii_read, dw_mii_write);