1 // SPDX-License-Identifier: GPL-2.0+
3 * SMSC LAN9[12]1[567] Network driver
5 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
15 #include <linux/delay.h>
16 #include <linux/types.h>
27 struct eth_device dev;
30 const struct chip_id *chipid;
31 unsigned char enetaddr[6];
34 static const struct chip_id chip_ids[] = {
35 { CHIP_89218, "LAN89218" },
36 { CHIP_9115, "LAN9115" },
37 { CHIP_9116, "LAN9116" },
38 { CHIP_9117, "LAN9117" },
39 { CHIP_9118, "LAN9118" },
40 { CHIP_9211, "LAN9211" },
41 { CHIP_9215, "LAN9215" },
42 { CHIP_9216, "LAN9216" },
43 { CHIP_9217, "LAN9217" },
44 { CHIP_9218, "LAN9218" },
45 { CHIP_9220, "LAN9220" },
46 { CHIP_9221, "LAN9221" },
50 #define DRIVERNAME "smc911x"
52 #if defined (CONFIG_SMC911X_32_BIT) && \
53 defined (CONFIG_SMC911X_16_BIT)
54 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
55 CONFIG_SMC911X_16_BIT shall be set"
58 #if defined (CONFIG_SMC911X_32_BIT)
59 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
61 return readl(priv->iobase + offset);
64 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
66 writel(val, priv->iobase + offset);
68 #elif defined (CONFIG_SMC911X_16_BIT)
69 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
71 return (readw(priv->iobase + offset) & 0xffff) |
72 (readw(priv->iobase + offset + 2) << 16);
74 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
76 writew(val & 0xffff, priv->iobase + offset);
77 writew(val >> 16, priv->iobase + offset + 2);
80 #error "SMC911X: undefined bus width"
81 #endif /* CONFIG_SMC911X_16_BIT */
83 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
85 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
87 smc911x_reg_write(priv, MAC_CSR_CMD,
88 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
89 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
92 return smc911x_reg_read(priv, MAC_CSR_DATA);
95 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
97 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
99 smc911x_reg_write(priv, MAC_CSR_DATA, data);
100 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
101 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
105 static int smc911x_detect_chip(struct smc911x_priv *priv)
107 unsigned long val, i;
109 val = smc911x_reg_read(priv, BYTE_TEST);
110 if (val == 0xffffffff) {
111 /* Special case -- no chip present */
113 } else if (val != 0x87654321) {
114 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
118 val = smc911x_reg_read(priv, ID_REV) >> 16;
119 for (i = 0; chip_ids[i].id != 0; i++) {
120 if (chip_ids[i].id == val) break;
122 if (!chip_ids[i].id) {
123 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
127 priv->chipid = &chip_ids[i];
132 static void smc911x_reset(struct smc911x_priv *priv)
137 * Take out of PM setting first
138 * Device is already wake up if PMT_CTRL_READY bit is set
140 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
141 /* Write to the bytetest will take out of powerdown */
142 smc911x_reg_write(priv, BYTE_TEST, 0x0);
147 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
151 ": timeout waiting for PM restore\n");
156 /* Disable interrupts */
157 smc911x_reg_write(priv, INT_EN, 0);
159 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
162 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
166 printf(DRIVERNAME ": reset timeout\n");
170 /* Reset the FIFO level and flow control settings */
171 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
172 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
174 /* Set to LED outputs */
175 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
178 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
180 unsigned long addrh, addrl;
181 unsigned char *m = priv->enetaddr;
183 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
184 addrh = m[4] | (m[5] << 8);
185 smc911x_set_mac_csr(priv, ADDRL, addrl);
186 smc911x_set_mac_csr(priv, ADDRH, addrh);
188 printf(DRIVERNAME ": MAC %pM\n", m);
189 if (!env_get("ethaddr"))
190 env_set("ethaddr", (const char *)m);
193 static bool smc911x_read_mac_address(struct smc911x_priv *priv)
197 /* address is obtained from optional eeprom */
198 addrh = smc911x_get_mac_csr(priv, ADDRH);
199 addrl = smc911x_get_mac_csr(priv, ADDRL);
200 if (addrl == 0xffffffff && addrh == 0x0000ffff)
203 priv->enetaddr[0] = addrl;
204 priv->enetaddr[1] = addrl >> 8;
205 priv->enetaddr[2] = addrl >> 16;
206 priv->enetaddr[3] = addrl >> 24;
207 priv->enetaddr[4] = addrh;
208 priv->enetaddr[5] = addrh >> 8;
213 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
214 u8 phy, u8 reg, u16 *val)
216 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
219 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
222 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
225 *val = smc911x_get_mac_csr(priv, MII_DATA);
230 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
231 u8 phy, u8 reg, u16 val)
233 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
236 smc911x_set_mac_csr(priv, MII_DATA, val);
237 smc911x_set_mac_csr(priv, MII_ACC,
238 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
240 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
245 static int smc911x_phy_reset(struct smc911x_priv *priv)
249 reg = smc911x_reg_read(priv, PMT_CTRL);
251 reg |= PMT_CTRL_PHY_RST;
252 smc911x_reg_write(priv, PMT_CTRL, reg);
259 static void smc911x_phy_configure(struct smc911x_priv *priv)
264 smc911x_phy_reset(priv);
266 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
268 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
269 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
275 if ((timeout--) == 0)
278 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
280 } while (!(status & BMSR_LSTATUS));
282 printf(DRIVERNAME ": phy initialized\n");
287 printf(DRIVERNAME ": autonegotiation timed out\n");
290 static void smc911x_enable(struct smc911x_priv *priv)
293 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
295 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
297 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
299 /* no padding to start of packets */
300 smc911x_reg_write(priv, RX_CFG, 0);
302 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
306 static int smc911x_init_common(struct smc911x_priv *priv)
308 const struct chip_id *id = priv->chipid;
310 printf(DRIVERNAME ": detected %s controller\n", id->name);
314 /* Configure the PHY, initialize the link state */
315 smc911x_phy_configure(priv);
317 smc911x_handle_mac_address(priv);
319 /* Turn on Tx + Rx */
320 smc911x_enable(priv);
325 static int smc911x_send_common(struct smc911x_priv *priv,
326 void *packet, int length)
328 u32 *data = (u32*)packet;
332 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
333 TX_CMD_A_INT_LAST_SEG | length);
334 smc911x_reg_write(priv, TX_DATA_FIFO, length);
336 tmplen = (length + 3) / 4;
339 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
341 /* wait for transmission */
342 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
343 TX_FIFO_INF_TSUSED) >> 16));
345 /* get status. Ignore 'no carrier' error, it has no meaning for
346 * full duplex operation
348 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
349 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
350 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
355 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
356 status & TX_STS_LOC ? "TX_STS_LOC " : "",
357 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
358 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
359 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
360 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
365 static void smc911x_halt_common(struct smc911x_priv *priv)
368 smc911x_handle_mac_address(priv);
371 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
376 status = smc911x_reg_read(priv, RX_FIFO_INF);
377 if (!(status & RX_FIFO_INF_RXSUSED))
380 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
381 pktlen = (status & RX_STS_PKT_LEN) >> 16;
383 smc911x_reg_write(priv, RX_CFG, 0);
385 tmplen = (pktlen + 3) / 4;
387 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
389 if (status & RX_STS_ES) {
391 ": dropped bad packet. Status: 0x%08x\n",
399 #ifndef CONFIG_DM_ETH
401 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
402 /* wrapper for smc911x_eth_phy_read */
403 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
406 struct eth_device *dev = eth_get_dev_by_name(bus->name);
407 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
414 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
421 /* wrapper for smc911x_eth_phy_write */
422 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
425 struct eth_device *dev = eth_get_dev_by_name(bus->name);
426 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
431 return smc911x_eth_phy_write(priv, phy, reg, val);
434 static int smc911x_initialize_mii(struct smc911x_priv *priv)
436 struct mii_dev *mdiodev = mdio_alloc();
442 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
443 mdiodev->read = smc911x_miiphy_read;
444 mdiodev->write = smc911x_miiphy_write;
446 ret = mdio_register(mdiodev);
455 static int smc911x_initialize_mii(struct smc911x_priv *priv)
461 static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
463 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
465 return smc911x_init_common(priv);
468 static void smc911x_halt(struct eth_device *dev)
470 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
472 smc911x_halt_common(priv);
475 static int smc911x_send(struct eth_device *dev, void *packet, int length)
477 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
479 return smc911x_send_common(priv, packet, length);
482 static int smc911x_recv(struct eth_device *dev)
484 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
485 u32 *data = (u32 *)net_rx_packets[0];
488 ret = smc911x_recv_common(priv, data);
490 net_process_received_packet(net_rx_packets[0], ret);
495 int smc911x_initialize(u8 dev_num, int base_addr)
497 struct smc911x_priv *priv;
500 priv = calloc(1, sizeof(*priv));
504 priv->iobase = base_addr;
505 priv->dev.iobase = base_addr;
507 /* Try to detect chip. Will fail if not present. */
508 ret = smc911x_detect_chip(priv);
510 ret = 0; /* Card not detected is not an error */
514 if (smc911x_read_mac_address(priv))
515 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
517 priv->dev.init = smc911x_init;
518 priv->dev.halt = smc911x_halt;
519 priv->dev.send = smc911x_send;
520 priv->dev.recv = smc911x_recv;
521 sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
523 eth_register(&priv->dev);
525 ret = smc911x_initialize_mii(priv);
532 eth_unregister(&priv->dev);
538 #else /* ifdef CONFIG_DM_ETH */
540 static int smc911x_start(struct udevice *dev)
542 struct eth_pdata *plat = dev_get_platdata(dev);
543 struct smc911x_priv *priv = dev_get_priv(dev);
545 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
547 return smc911x_init_common(priv);
550 static void smc911x_stop(struct udevice *dev)
552 struct smc911x_priv *priv = dev_get_priv(dev);
554 smc911x_halt_common(priv);
557 static int smc911x_send(struct udevice *dev, void *packet, int length)
559 struct smc911x_priv *priv = dev_get_priv(dev);
562 ret = smc911x_send_common(priv, packet, length);
564 return ret ? 0 : -ETIMEDOUT;
567 static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
569 struct smc911x_priv *priv = dev_get_priv(dev);
570 u32 *data = (u32 *)net_rx_packets[0];
573 ret = smc911x_recv_common(priv, data);
575 *packetp = (void *)data;
577 return ret ? ret : -EAGAIN;
580 static int smc911x_read_rom_hwaddr(struct udevice *dev)
582 struct smc911x_priv *priv = dev_get_priv(dev);
583 struct eth_pdata *pdata = dev_get_platdata(dev);
585 if (!smc911x_read_mac_address(priv))
588 memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
593 static int smc911x_bind(struct udevice *dev)
595 return device_set_name(dev, dev->name);
598 static int smc911x_probe(struct udevice *dev)
600 struct smc911x_priv *priv = dev_get_priv(dev);
603 /* Try to detect chip. Will fail if not present. */
604 ret = smc911x_detect_chip(priv);
608 smc911x_read_rom_hwaddr(dev);
613 static int smc911x_ofdata_to_platdata(struct udevice *dev)
615 struct smc911x_priv *priv = dev_get_priv(dev);
616 struct eth_pdata *pdata = dev_get_platdata(dev);
618 pdata->iobase = dev_read_addr(dev);
619 priv->iobase = pdata->iobase;
624 static const struct eth_ops smc911x_ops = {
625 .start = smc911x_start,
626 .send = smc911x_send,
627 .recv = smc911x_recv,
628 .stop = smc911x_stop,
629 .read_rom_hwaddr = smc911x_read_rom_hwaddr,
632 static const struct udevice_id smc911x_ids[] = {
633 { .compatible = "smsc,lan9115" },
637 U_BOOT_DRIVER(smc911x) = {
638 .name = "eth_smc911x",
640 .of_match = smc911x_ids,
641 .bind = smc911x_bind,
642 .ofdata_to_platdata = smc911x_ofdata_to_platdata,
643 .probe = smc911x_probe,
645 .priv_auto_alloc_size = sizeof(struct smc911x_priv),
646 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
647 .flags = DM_FLAG_ALLOC_PRIV_DMA,