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];
33 static const struct chip_id chip_ids[] = {
34 { CHIP_89218, "LAN89218" },
35 { CHIP_9115, "LAN9115" },
36 { CHIP_9116, "LAN9116" },
37 { CHIP_9117, "LAN9117" },
38 { CHIP_9118, "LAN9118" },
39 { CHIP_9211, "LAN9211" },
40 { CHIP_9215, "LAN9215" },
41 { CHIP_9216, "LAN9216" },
42 { CHIP_9217, "LAN9217" },
43 { CHIP_9218, "LAN9218" },
44 { CHIP_9220, "LAN9220" },
45 { CHIP_9221, "LAN9221" },
49 #define DRIVERNAME "smc911x"
51 #if defined (CONFIG_SMC911X_32_BIT)
52 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
54 return readl(priv->iobase + offset);
57 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
59 writel(val, priv->iobase + offset);
62 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
64 return (readw(priv->iobase + offset) & 0xffff) |
65 (readw(priv->iobase + offset + 2) << 16);
67 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
69 writew(val & 0xffff, priv->iobase + offset);
70 writew(val >> 16, priv->iobase + offset + 2);
72 #endif /* CONFIG_SMC911X_32_BIT */
74 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
76 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
78 smc911x_reg_write(priv, MAC_CSR_CMD,
79 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
80 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
83 return smc911x_reg_read(priv, MAC_CSR_DATA);
86 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
88 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
90 smc911x_reg_write(priv, MAC_CSR_DATA, data);
91 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
92 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
96 static int smc911x_detect_chip(struct smc911x_priv *priv)
100 val = smc911x_reg_read(priv, BYTE_TEST);
101 if (val == 0xffffffff) {
102 /* Special case -- no chip present */
104 } else if (val != 0x87654321) {
105 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
109 val = smc911x_reg_read(priv, ID_REV) >> 16;
110 for (i = 0; chip_ids[i].id != 0; i++) {
111 if (chip_ids[i].id == val) break;
113 if (!chip_ids[i].id) {
114 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
118 priv->chipid = &chip_ids[i];
123 static void smc911x_reset(struct smc911x_priv *priv)
128 * Take out of PM setting first
129 * Device is already wake up if PMT_CTRL_READY bit is set
131 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
132 /* Write to the bytetest will take out of powerdown */
133 smc911x_reg_write(priv, BYTE_TEST, 0x0);
138 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
142 ": timeout waiting for PM restore\n");
147 /* Disable interrupts */
148 smc911x_reg_write(priv, INT_EN, 0);
150 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
153 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
157 printf(DRIVERNAME ": reset timeout\n");
161 /* Reset the FIFO level and flow control settings */
162 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
163 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
165 /* Set to LED outputs */
166 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
169 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
171 unsigned long addrh, addrl;
172 unsigned char *m = priv->enetaddr;
174 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
175 addrh = m[4] | (m[5] << 8);
176 smc911x_set_mac_csr(priv, ADDRL, addrl);
177 smc911x_set_mac_csr(priv, ADDRH, addrh);
179 printf(DRIVERNAME ": MAC %pM\n", m);
182 static bool smc911x_read_mac_address(struct smc911x_priv *priv)
186 /* address is obtained from optional eeprom */
187 addrh = smc911x_get_mac_csr(priv, ADDRH);
188 addrl = smc911x_get_mac_csr(priv, ADDRL);
189 if (addrl == 0xffffffff && addrh == 0x0000ffff)
192 priv->enetaddr[0] = addrl;
193 priv->enetaddr[1] = addrl >> 8;
194 priv->enetaddr[2] = addrl >> 16;
195 priv->enetaddr[3] = addrl >> 24;
196 priv->enetaddr[4] = addrh;
197 priv->enetaddr[5] = addrh >> 8;
202 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
203 u8 phy, u8 reg, u16 *val)
205 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
208 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
211 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
214 *val = smc911x_get_mac_csr(priv, MII_DATA);
219 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
220 u8 phy, u8 reg, u16 val)
222 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
225 smc911x_set_mac_csr(priv, MII_DATA, val);
226 smc911x_set_mac_csr(priv, MII_ACC,
227 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
229 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
234 static int smc911x_phy_reset(struct smc911x_priv *priv)
238 reg = smc911x_reg_read(priv, PMT_CTRL);
240 reg |= PMT_CTRL_PHY_RST;
241 smc911x_reg_write(priv, PMT_CTRL, reg);
248 static void smc911x_phy_configure(struct smc911x_priv *priv)
253 smc911x_phy_reset(priv);
255 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
257 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
258 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
264 if ((timeout--) == 0)
267 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
269 } while (!(status & BMSR_LSTATUS));
271 printf(DRIVERNAME ": phy initialized\n");
276 printf(DRIVERNAME ": autonegotiation timed out\n");
279 static void smc911x_enable(struct smc911x_priv *priv)
282 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
284 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
286 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
288 /* no padding to start of packets */
289 smc911x_reg_write(priv, RX_CFG, 0);
291 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
295 static int smc911x_init_common(struct smc911x_priv *priv)
297 const struct chip_id *id = priv->chipid;
299 printf(DRIVERNAME ": detected %s controller\n", id->name);
303 /* Configure the PHY, initialize the link state */
304 smc911x_phy_configure(priv);
306 smc911x_handle_mac_address(priv);
308 /* Turn on Tx + Rx */
309 smc911x_enable(priv);
314 static int smc911x_send_common(struct smc911x_priv *priv,
315 void *packet, int length)
317 u32 *data = (u32*)packet;
321 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
322 TX_CMD_A_INT_LAST_SEG | length);
323 smc911x_reg_write(priv, TX_DATA_FIFO, length);
325 tmplen = (length + 3) / 4;
328 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
330 /* wait for transmission */
331 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
332 TX_FIFO_INF_TSUSED) >> 16));
334 /* get status. Ignore 'no carrier' error, it has no meaning for
335 * full duplex operation
337 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
338 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
339 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
344 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
345 status & TX_STS_LOC ? "TX_STS_LOC " : "",
346 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
347 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
348 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
349 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
354 static void smc911x_halt_common(struct smc911x_priv *priv)
357 smc911x_handle_mac_address(priv);
360 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
365 status = smc911x_reg_read(priv, RX_FIFO_INF);
366 if (!(status & RX_FIFO_INF_RXSUSED))
369 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
370 pktlen = (status & RX_STS_PKT_LEN) >> 16;
372 smc911x_reg_write(priv, RX_CFG, 0);
374 tmplen = (pktlen + 3) / 4;
376 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
378 if (status & RX_STS_ES) {
380 ": dropped bad packet. Status: 0x%08x\n",
388 #ifndef CONFIG_DM_ETH
390 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
391 /* wrapper for smc911x_eth_phy_read */
392 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
395 struct eth_device *dev = eth_get_dev_by_name(bus->name);
396 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
403 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
410 /* wrapper for smc911x_eth_phy_write */
411 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
414 struct eth_device *dev = eth_get_dev_by_name(bus->name);
415 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
420 return smc911x_eth_phy_write(priv, phy, reg, val);
423 static int smc911x_initialize_mii(struct smc911x_priv *priv)
425 struct mii_dev *mdiodev = mdio_alloc();
431 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
432 mdiodev->read = smc911x_miiphy_read;
433 mdiodev->write = smc911x_miiphy_write;
435 ret = mdio_register(mdiodev);
444 static int smc911x_initialize_mii(struct smc911x_priv *priv)
450 static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
452 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
454 return smc911x_init_common(priv);
457 static void smc911x_halt(struct eth_device *dev)
459 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
461 smc911x_halt_common(priv);
464 static int smc911x_send(struct eth_device *dev, void *packet, int length)
466 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
468 return smc911x_send_common(priv, packet, length);
471 static int smc911x_recv(struct eth_device *dev)
473 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
474 u32 *data = (u32 *)net_rx_packets[0];
477 ret = smc911x_recv_common(priv, data);
479 net_process_received_packet(net_rx_packets[0], ret);
484 int smc911x_initialize(u8 dev_num, int base_addr)
486 struct smc911x_priv *priv;
489 priv = calloc(1, sizeof(*priv));
493 priv->iobase = base_addr;
494 priv->dev.iobase = base_addr;
496 /* Try to detect chip. Will fail if not present. */
497 ret = smc911x_detect_chip(priv);
499 ret = 0; /* Card not detected is not an error */
503 if (smc911x_read_mac_address(priv))
504 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
506 priv->dev.init = smc911x_init;
507 priv->dev.halt = smc911x_halt;
508 priv->dev.send = smc911x_send;
509 priv->dev.recv = smc911x_recv;
510 sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
512 eth_register(&priv->dev);
514 ret = smc911x_initialize_mii(priv);
521 eth_unregister(&priv->dev);
527 #else /* ifdef CONFIG_DM_ETH */
529 static int smc911x_start(struct udevice *dev)
531 struct eth_pdata *plat = dev_get_plat(dev);
532 struct smc911x_priv *priv = dev_get_priv(dev);
534 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
536 return smc911x_init_common(priv);
539 static void smc911x_stop(struct udevice *dev)
541 struct smc911x_priv *priv = dev_get_priv(dev);
543 smc911x_halt_common(priv);
546 static int smc911x_send(struct udevice *dev, void *packet, int length)
548 struct smc911x_priv *priv = dev_get_priv(dev);
551 ret = smc911x_send_common(priv, packet, length);
553 return ret ? 0 : -ETIMEDOUT;
556 static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
558 struct smc911x_priv *priv = dev_get_priv(dev);
559 u32 *data = (u32 *)net_rx_packets[0];
562 ret = smc911x_recv_common(priv, data);
564 *packetp = (void *)data;
566 return ret ? ret : -EAGAIN;
569 static int smc911x_read_rom_hwaddr(struct udevice *dev)
571 struct smc911x_priv *priv = dev_get_priv(dev);
572 struct eth_pdata *pdata = dev_get_plat(dev);
574 if (!smc911x_read_mac_address(priv))
577 memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
582 static int smc911x_bind(struct udevice *dev)
584 return device_set_name(dev, dev->name);
587 static int smc911x_probe(struct udevice *dev)
589 struct smc911x_priv *priv = dev_get_priv(dev);
592 /* Try to detect chip. Will fail if not present. */
593 ret = smc911x_detect_chip(priv);
597 smc911x_read_rom_hwaddr(dev);
602 static int smc911x_of_to_plat(struct udevice *dev)
604 struct smc911x_priv *priv = dev_get_priv(dev);
605 struct eth_pdata *pdata = dev_get_plat(dev);
607 pdata->iobase = dev_read_addr(dev);
608 priv->iobase = pdata->iobase;
613 static const struct eth_ops smc911x_ops = {
614 .start = smc911x_start,
615 .send = smc911x_send,
616 .recv = smc911x_recv,
617 .stop = smc911x_stop,
618 .read_rom_hwaddr = smc911x_read_rom_hwaddr,
621 static const struct udevice_id smc911x_ids[] = {
622 { .compatible = "smsc,lan9115" },
626 U_BOOT_DRIVER(smc911x) = {
627 .name = "eth_smc911x",
629 .of_match = smc911x_ids,
630 .bind = smc911x_bind,
631 .of_to_plat = smc911x_of_to_plat,
632 .probe = smc911x_probe,
634 .priv_auto = sizeof(struct smc911x_priv),
635 .plat_auto = sizeof(struct eth_pdata),
636 .flags = DM_FLAG_ALLOC_PRIV_DMA,