3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Designware ethernet IP driver for u-boot
15 #include <linux/compiler.h>
16 #include <linux/err.h>
18 #include "designware.h"
20 static int configure_phy(struct eth_device *dev);
22 static void tx_descs_init(struct eth_device *dev)
24 struct dw_eth_dev *priv = dev->priv;
25 struct eth_dma_regs *dma_p = priv->dma_regs_p;
26 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
27 char *txbuffs = &priv->txbuffs[0];
28 struct dmamacdescr *desc_p;
31 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
32 desc_p = &desc_table_p[idx];
33 desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
34 desc_p->dmamac_next = &desc_table_p[idx + 1];
36 #if defined(CONFIG_DW_ALTDESCRIPTOR)
37 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
38 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
39 DESC_TXSTS_TXCHECKINSCTRL | \
40 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
42 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
43 desc_p->dmamac_cntl = 0;
44 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
46 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
47 desc_p->txrx_status = 0;
51 /* Correcting the last pointer of the chain */
52 desc_p->dmamac_next = &desc_table_p[0];
54 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
55 priv->tx_currdescnum = 0;
58 static void rx_descs_init(struct eth_device *dev)
60 struct dw_eth_dev *priv = dev->priv;
61 struct eth_dma_regs *dma_p = priv->dma_regs_p;
62 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
63 char *rxbuffs = &priv->rxbuffs[0];
64 struct dmamacdescr *desc_p;
67 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
68 desc_p = &desc_table_p[idx];
69 desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
70 desc_p->dmamac_next = &desc_table_p[idx + 1];
73 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
76 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
79 /* Correcting the last pointer of the chain */
80 desc_p->dmamac_next = &desc_table_p[0];
82 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
83 priv->rx_currdescnum = 0;
86 static void descs_init(struct eth_device *dev)
92 static int mac_reset(struct eth_device *dev)
94 struct dw_eth_dev *priv = dev->priv;
95 struct eth_mac_regs *mac_p = priv->mac_regs_p;
96 struct eth_dma_regs *dma_p = priv->dma_regs_p;
99 int timeout = CONFIG_MACRESET_TIMEOUT;
101 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
103 if (priv->interface != PHY_INTERFACE_MODE_RGMII)
104 writel(MII_PORTSELECT, &mac_p->conf);
106 start = get_timer(0);
107 while (get_timer(start) < timeout) {
108 if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
111 /* Try again after 10usec */
118 static int dw_write_hwaddr(struct eth_device *dev)
120 struct dw_eth_dev *priv = dev->priv;
121 struct eth_mac_regs *mac_p = priv->mac_regs_p;
122 u32 macid_lo, macid_hi;
123 u8 *mac_id = &dev->enetaddr[0];
125 macid_lo = mac_id[0] + (mac_id[1] << 8) + \
126 (mac_id[2] << 16) + (mac_id[3] << 24);
127 macid_hi = mac_id[4] + (mac_id[5] << 8);
129 writel(macid_hi, &mac_p->macaddr0hi);
130 writel(macid_lo, &mac_p->macaddr0lo);
135 static int dw_eth_init(struct eth_device *dev, bd_t *bis)
137 struct dw_eth_dev *priv = dev->priv;
138 struct eth_mac_regs *mac_p = priv->mac_regs_p;
139 struct eth_dma_regs *dma_p = priv->dma_regs_p;
142 if (priv->phy_configured != 1)
145 /* Print link status only once */
146 if (!priv->link_printed) {
147 printf("ENET Speed is %d Mbps - %s duplex connection\n",
148 priv->speed, (priv->duplex == HALF) ? "HALF" : "FULL");
149 priv->link_printed = 1;
152 /* Reset ethernet hardware */
153 if (mac_reset(dev) < 0)
156 /* Resore the HW MAC address as it has been lost during MAC reset */
157 dw_write_hwaddr(dev);
159 writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
162 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD |
163 TXSECONDFRAME, &dma_p->opmode);
165 conf = FRAMEBURSTENABLE | DISABLERXOWN;
167 if (priv->speed != 1000)
168 conf |= MII_PORTSELECT;
170 if ((priv->interface != PHY_INTERFACE_MODE_MII) &&
171 (priv->interface != PHY_INTERFACE_MODE_GMII)) {
173 if (priv->speed == 100)
177 if (priv->duplex == FULL)
178 conf |= FULLDPLXMODE;
180 writel(conf, &mac_p->conf);
185 * Start/Enable xfer at dma as well as mac level
187 writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
188 writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
190 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
195 static int dw_eth_send(struct eth_device *dev, void *packet, int length)
197 struct dw_eth_dev *priv = dev->priv;
198 struct eth_dma_regs *dma_p = priv->dma_regs_p;
199 u32 desc_num = priv->tx_currdescnum;
200 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
202 /* Check if the descriptor is owned by CPU */
203 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
204 printf("CPU not owner of tx frame\n");
208 memcpy((void *)desc_p->dmamac_addr, packet, length);
210 #if defined(CONFIG_DW_ALTDESCRIPTOR)
211 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
212 desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
213 DESC_TXCTRL_SIZE1MASK;
215 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
216 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
218 desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
219 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
222 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
225 /* Test the wrap-around condition. */
226 if (++desc_num >= CONFIG_TX_DESCR_NUM)
229 priv->tx_currdescnum = desc_num;
231 /* Start the transmission */
232 writel(POLL_DATA, &dma_p->txpolldemand);
237 static int dw_eth_recv(struct eth_device *dev)
239 struct dw_eth_dev *priv = dev->priv;
240 u32 desc_num = priv->rx_currdescnum;
241 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
243 u32 status = desc_p->txrx_status;
246 /* Check if the owner is the CPU */
247 if (!(status & DESC_RXSTS_OWNBYDMA)) {
249 length = (status & DESC_RXSTS_FRMLENMSK) >> \
250 DESC_RXSTS_FRMLENSHFT;
252 NetReceive(desc_p->dmamac_addr, length);
255 * Make the current descriptor valid again and go to
258 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
260 /* Test the wrap-around condition. */
261 if (++desc_num >= CONFIG_RX_DESCR_NUM)
265 priv->rx_currdescnum = desc_num;
270 static void dw_eth_halt(struct eth_device *dev)
272 struct dw_eth_dev *priv = dev->priv;
275 priv->tx_currdescnum = priv->rx_currdescnum = 0;
278 static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
280 struct dw_eth_dev *priv = dev->priv;
281 struct eth_mac_regs *mac_p = priv->mac_regs_p;
284 int timeout = CONFIG_MDIO_TIMEOUT;
286 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
287 ((reg << MIIREGSHIFT) & MII_REGMSK);
289 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
291 start = get_timer(0);
292 while (get_timer(start) < timeout) {
293 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
294 *val = readl(&mac_p->miidata);
298 /* Try again after 10usec */
305 static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
307 struct dw_eth_dev *priv = dev->priv;
308 struct eth_mac_regs *mac_p = priv->mac_regs_p;
311 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
314 writel(val, &mac_p->miidata);
315 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
316 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
318 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
320 start = get_timer(0);
321 while (get_timer(start) < timeout) {
322 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
327 /* Try again after 10usec */
331 /* Needed as a fix for ST-Phy */
332 eth_mdio_read(dev, addr, reg, &value);
337 #if defined(CONFIG_DW_SEARCH_PHY)
338 static int find_phy(struct eth_device *dev)
344 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
345 oldctrl = ctrl & BMCR_ANENABLE;
347 ctrl ^= BMCR_ANENABLE;
348 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
349 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
350 ctrl &= BMCR_ANENABLE;
352 if (ctrl == oldctrl) {
355 ctrl ^= BMCR_ANENABLE;
356 eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
360 } while (phy_addr < 32);
366 static int dw_reset_phy(struct eth_device *dev)
368 struct dw_eth_dev *priv = dev->priv;
371 int timeout = CONFIG_PHYRESET_TIMEOUT;
372 u32 phy_addr = priv->address;
374 eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
376 start = get_timer(0);
377 while (get_timer(start) < timeout) {
378 eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
379 if (!(ctrl & BMCR_RESET))
382 /* Try again after 10usec */
386 if (get_timer(start) >= CONFIG_PHYRESET_TIMEOUT)
389 #ifdef CONFIG_PHY_RESET_DELAY
390 udelay(CONFIG_PHY_RESET_DELAY);
396 * Add weak default function for board specific PHY configuration
398 int __weak designware_board_phy_init(struct eth_device *dev, int phy_addr,
399 int (*mii_write)(struct eth_device *, u8, u8, u16),
400 int dw_reset_phy(struct eth_device *))
405 static int configure_phy(struct eth_device *dev)
407 struct dw_eth_dev *priv = dev->priv;
410 #if defined(CONFIG_DW_AUTONEG)
416 #if defined(CONFIG_DW_SEARCH_PHY)
417 phy_addr = find_phy(dev);
419 priv->address = phy_addr;
423 phy_addr = priv->address;
427 * Some boards need board specific PHY initialization. This is
428 * after the main driver init code but before the auto negotiation
431 if (designware_board_phy_init(dev, phy_addr,
432 eth_mdio_write, dw_reset_phy) < 0)
435 if (dw_reset_phy(dev) < 0)
438 #if defined(CONFIG_DW_AUTONEG)
439 /* Set Auto-Neg Advertisement capabilities to 10/100 half/full */
440 eth_mdio_write(dev, phy_addr, MII_ADVERTISE, 0x1E1);
442 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
444 bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
446 #if defined(CONFIG_DW_SPEED10M)
447 bmcr &= ~BMCR_SPEED100;
449 #if defined(CONFIG_DW_DUPLEXHALF)
450 bmcr &= ~BMCR_FULLDPLX;
453 if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
456 /* Read the phy status register and populate priv structure */
457 #if defined(CONFIG_DW_AUTONEG)
458 timeout = CONFIG_AUTONEG_TIMEOUT;
459 start = get_timer(0);
460 puts("Waiting for PHY auto negotiation to complete");
461 while (get_timer(start) < timeout) {
462 eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
463 if (bmsr & BMSR_ANEGCOMPLETE) {
464 priv->phy_configured = 1;
468 /* Print dot all 1s to show progress */
469 if ((get_timer(start) % 1000) == 0)
472 /* Try again after 1msec */
476 if (!(bmsr & BMSR_ANEGCOMPLETE))
481 priv->phy_configured = 1;
484 priv->speed = miiphy_speed(dev->name, phy_addr);
485 priv->duplex = miiphy_duplex(dev->name, phy_addr);
490 #if defined(CONFIG_MII)
491 static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
493 struct eth_device *dev;
495 dev = eth_get_dev_by_name(devname);
497 eth_mdio_read(dev, addr, reg, val);
502 static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
504 struct eth_device *dev;
506 dev = eth_get_dev_by_name(devname);
508 eth_mdio_write(dev, addr, reg, val);
514 int designware_initialize(u32 id, ulong base_addr, u32 phy_addr, u32 interface)
516 struct eth_device *dev;
517 struct dw_eth_dev *priv;
519 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
524 * Since the priv structure contains the descriptors which need a strict
525 * buswidth alignment, memalign is used to allocate memory
527 priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
533 memset(dev, 0, sizeof(struct eth_device));
534 memset(priv, 0, sizeof(struct dw_eth_dev));
536 sprintf(dev->name, "mii%d", id);
537 dev->iobase = (int)base_addr;
540 eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
543 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
544 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
546 priv->address = phy_addr;
547 priv->phy_configured = 0;
548 priv->interface = interface;
550 dev->init = dw_eth_init;
551 dev->send = dw_eth_send;
552 dev->recv = dw_eth_recv;
553 dev->halt = dw_eth_halt;
554 dev->write_hwaddr = dw_write_hwaddr;
558 #if defined(CONFIG_MII)
559 miiphy_register(dev->name, dw_mii_read, dw_mii_write);