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>
14 #include <linux/delay.h>
15 #include <linux/types.h>
26 struct eth_device dev;
29 const struct chip_id *chipid;
30 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 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
54 if (priv->use_32_bit_io)
55 return readl(priv->iobase + offset);
57 return (readw(priv->iobase + offset) & 0xffff) |
58 (readw(priv->iobase + offset + 2) << 16);
61 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
63 if (priv->use_32_bit_io) {
64 writel(val, priv->iobase + offset);
66 writew(val & 0xffff, priv->iobase + offset);
67 writew(val >> 16, priv->iobase + offset + 2);
71 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
73 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
75 smc911x_reg_write(priv, MAC_CSR_CMD,
76 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
77 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
80 return smc911x_reg_read(priv, MAC_CSR_DATA);
83 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
85 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
87 smc911x_reg_write(priv, MAC_CSR_DATA, data);
88 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
89 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
93 static int smc911x_detect_chip(struct smc911x_priv *priv)
97 val = smc911x_reg_read(priv, BYTE_TEST);
98 if (val == 0xffffffff) {
99 /* Special case -- no chip present */
101 } else if (val != 0x87654321) {
102 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
106 val = smc911x_reg_read(priv, ID_REV) >> 16;
107 for (i = 0; chip_ids[i].id != 0; i++) {
108 if (chip_ids[i].id == val) break;
110 if (!chip_ids[i].id) {
111 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
115 priv->chipid = &chip_ids[i];
120 static void smc911x_reset(struct smc911x_priv *priv)
125 * Take out of PM setting first
126 * Device is already wake up if PMT_CTRL_READY bit is set
128 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
129 /* Write to the bytetest will take out of powerdown */
130 smc911x_reg_write(priv, BYTE_TEST, 0x0);
135 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
139 ": timeout waiting for PM restore\n");
144 /* Disable interrupts */
145 smc911x_reg_write(priv, INT_EN, 0);
147 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
150 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
154 printf(DRIVERNAME ": reset timeout\n");
158 /* Reset the FIFO level and flow control settings */
159 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
160 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
162 /* Set to LED outputs */
163 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
166 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
168 unsigned long addrh, addrl;
169 unsigned char *m = priv->enetaddr;
171 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
172 addrh = m[4] | (m[5] << 8);
173 smc911x_set_mac_csr(priv, ADDRL, addrl);
174 smc911x_set_mac_csr(priv, ADDRH, addrh);
176 printf(DRIVERNAME ": MAC %pM\n", m);
179 static bool smc911x_read_mac_address(struct smc911x_priv *priv)
183 /* address is obtained from optional eeprom */
184 addrh = smc911x_get_mac_csr(priv, ADDRH);
185 addrl = smc911x_get_mac_csr(priv, ADDRL);
186 if (addrl == 0xffffffff && addrh == 0x0000ffff)
189 priv->enetaddr[0] = addrl;
190 priv->enetaddr[1] = addrl >> 8;
191 priv->enetaddr[2] = addrl >> 16;
192 priv->enetaddr[3] = addrl >> 24;
193 priv->enetaddr[4] = addrh;
194 priv->enetaddr[5] = addrh >> 8;
199 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
200 u8 phy, u8 reg, u16 *val)
202 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
205 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
208 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
211 *val = smc911x_get_mac_csr(priv, MII_DATA);
216 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
217 u8 phy, u8 reg, u16 val)
219 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
222 smc911x_set_mac_csr(priv, MII_DATA, val);
223 smc911x_set_mac_csr(priv, MII_ACC,
224 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
226 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
231 static int smc911x_phy_reset(struct smc911x_priv *priv)
235 reg = smc911x_reg_read(priv, PMT_CTRL);
237 reg |= PMT_CTRL_PHY_RST;
238 smc911x_reg_write(priv, PMT_CTRL, reg);
245 static void smc911x_phy_configure(struct smc911x_priv *priv)
250 smc911x_phy_reset(priv);
252 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
254 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
255 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
261 if ((timeout--) == 0)
264 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
266 } while (!(status & BMSR_LSTATUS));
268 printf(DRIVERNAME ": phy initialized\n");
273 printf(DRIVERNAME ": autonegotiation timed out\n");
276 static void smc911x_enable(struct smc911x_priv *priv)
279 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
281 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
283 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
285 /* no padding to start of packets */
286 smc911x_reg_write(priv, RX_CFG, 0);
288 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
292 static int smc911x_init_common(struct smc911x_priv *priv)
294 const struct chip_id *id = priv->chipid;
296 printf(DRIVERNAME ": detected %s controller\n", id->name);
300 /* Configure the PHY, initialize the link state */
301 smc911x_phy_configure(priv);
303 smc911x_handle_mac_address(priv);
305 /* Turn on Tx + Rx */
306 smc911x_enable(priv);
311 static int smc911x_send_common(struct smc911x_priv *priv,
312 void *packet, int length)
314 u32 *data = (u32*)packet;
318 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
319 TX_CMD_A_INT_LAST_SEG | length);
320 smc911x_reg_write(priv, TX_DATA_FIFO, length);
322 tmplen = (length + 3) / 4;
325 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
327 /* wait for transmission */
328 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
329 TX_FIFO_INF_TSUSED) >> 16));
331 /* get status. Ignore 'no carrier' error, it has no meaning for
332 * full duplex operation
334 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
335 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
336 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
341 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
342 status & TX_STS_LOC ? "TX_STS_LOC " : "",
343 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
344 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
345 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
346 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
351 static void smc911x_halt_common(struct smc911x_priv *priv)
354 smc911x_handle_mac_address(priv);
357 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
362 status = smc911x_reg_read(priv, RX_FIFO_INF);
363 if (!(status & RX_FIFO_INF_RXSUSED))
366 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
367 pktlen = (status & RX_STS_PKT_LEN) >> 16;
369 smc911x_reg_write(priv, RX_CFG, 0);
371 tmplen = (pktlen + 3) / 4;
373 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
375 if (status & RX_STS_ES) {
377 ": dropped bad packet. Status: 0x%08x\n",
385 #ifndef CONFIG_DM_ETH
387 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
388 /* wrapper for smc911x_eth_phy_read */
389 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
392 struct eth_device *dev = eth_get_dev_by_name(bus->name);
393 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
400 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
407 /* wrapper for smc911x_eth_phy_write */
408 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
411 struct eth_device *dev = eth_get_dev_by_name(bus->name);
412 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
417 return smc911x_eth_phy_write(priv, phy, reg, val);
420 static int smc911x_initialize_mii(struct smc911x_priv *priv)
422 struct mii_dev *mdiodev = mdio_alloc();
428 strlcpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
429 mdiodev->read = smc911x_miiphy_read;
430 mdiodev->write = smc911x_miiphy_write;
432 ret = mdio_register(mdiodev);
441 static int smc911x_initialize_mii(struct smc911x_priv *priv)
447 static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
449 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
451 return smc911x_init_common(priv);
454 static void smc911x_halt(struct eth_device *dev)
456 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
458 smc911x_halt_common(priv);
461 static int smc911x_send(struct eth_device *dev, void *packet, int length)
463 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
465 return smc911x_send_common(priv, packet, length);
468 static int smc911x_recv(struct eth_device *dev)
470 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
471 u32 *data = (u32 *)net_rx_packets[0];
474 ret = smc911x_recv_common(priv, data);
476 net_process_received_packet(net_rx_packets[0], ret);
481 int smc911x_initialize(u8 dev_num, phys_addr_t base_addr)
483 struct smc911x_priv *priv;
486 priv = calloc(1, sizeof(*priv));
490 priv->iobase = base_addr;
491 priv->dev.iobase = base_addr;
493 priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
495 /* Try to detect chip. Will fail if not present. */
496 ret = smc911x_detect_chip(priv);
498 ret = 0; /* Card not detected is not an error */
502 if (smc911x_read_mac_address(priv))
503 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
505 priv->dev.init = smc911x_init;
506 priv->dev.halt = smc911x_halt;
507 priv->dev.send = smc911x_send;
508 priv->dev.recv = smc911x_recv;
509 sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
511 eth_register(&priv->dev);
513 ret = smc911x_initialize_mii(priv);
520 eth_unregister(&priv->dev);
526 #else /* ifdef CONFIG_DM_ETH */
528 static int smc911x_start(struct udevice *dev)
530 struct eth_pdata *plat = dev_get_plat(dev);
531 struct smc911x_priv *priv = dev_get_priv(dev);
533 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
535 return smc911x_init_common(priv);
538 static void smc911x_stop(struct udevice *dev)
540 struct smc911x_priv *priv = dev_get_priv(dev);
542 smc911x_halt_common(priv);
545 static int smc911x_send(struct udevice *dev, void *packet, int length)
547 struct smc911x_priv *priv = dev_get_priv(dev);
550 ret = smc911x_send_common(priv, packet, length);
552 return ret ? 0 : -ETIMEDOUT;
555 static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
557 struct smc911x_priv *priv = dev_get_priv(dev);
558 u32 *data = (u32 *)net_rx_packets[0];
561 ret = smc911x_recv_common(priv, data);
563 *packetp = (void *)data;
565 return ret ? ret : -EAGAIN;
568 static int smc911x_read_rom_hwaddr(struct udevice *dev)
570 struct smc911x_priv *priv = dev_get_priv(dev);
571 struct eth_pdata *pdata = dev_get_plat(dev);
573 if (!smc911x_read_mac_address(priv))
576 memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
581 static int smc911x_bind(struct udevice *dev)
583 return device_set_name(dev, dev->name);
586 static int smc911x_probe(struct udevice *dev)
588 struct smc911x_priv *priv = dev_get_priv(dev);
591 /* Try to detect chip. Will fail if not present. */
592 ret = smc911x_detect_chip(priv);
596 smc911x_read_rom_hwaddr(dev);
601 static int smc911x_of_to_plat(struct udevice *dev)
603 struct smc911x_priv *priv = dev_get_priv(dev);
604 struct eth_pdata *pdata = dev_get_plat(dev);
608 pdata->iobase = dev_read_addr(dev);
609 priv->iobase = pdata->iobase;
611 ret = dev_read_u32(dev, "reg-io-width", &io_width);
613 priv->use_32_bit_io = (io_width == 4);
615 priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
620 static const struct eth_ops smc911x_ops = {
621 .start = smc911x_start,
622 .send = smc911x_send,
623 .recv = smc911x_recv,
624 .stop = smc911x_stop,
625 .read_rom_hwaddr = smc911x_read_rom_hwaddr,
628 static const struct udevice_id smc911x_ids[] = {
629 { .compatible = "smsc,lan9115" },
633 U_BOOT_DRIVER(smc911x) = {
634 .name = "eth_smc911x",
636 .of_match = smc911x_ids,
637 .bind = smc911x_bind,
638 .of_to_plat = smc911x_of_to_plat,
639 .probe = smc911x_probe,
641 .priv_auto = sizeof(struct smc911x_priv),
642 .plat_auto = sizeof(struct eth_pdata),
643 .flags = DM_FLAG_ALLOC_PRIV_DMA,