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 defined (CONFIG_SMC911X_16_BIT)
53 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
54 CONFIG_SMC911X_16_BIT shall be set"
57 #if defined (CONFIG_SMC911X_32_BIT)
58 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
60 return readl(priv->iobase + offset);
63 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
65 writel(val, priv->iobase + offset);
67 #elif defined (CONFIG_SMC911X_16_BIT)
68 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
70 return (readw(priv->iobase + offset) & 0xffff) |
71 (readw(priv->iobase + offset + 2) << 16);
73 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
75 writew(val & 0xffff, priv->iobase + offset);
76 writew(val >> 16, priv->iobase + offset + 2);
79 #error "SMC911X: undefined bus width"
80 #endif /* CONFIG_SMC911X_16_BIT */
82 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
84 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
86 smc911x_reg_write(priv, MAC_CSR_CMD,
87 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
88 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
91 return smc911x_reg_read(priv, MAC_CSR_DATA);
94 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
96 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
98 smc911x_reg_write(priv, MAC_CSR_DATA, data);
99 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
100 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
104 static int smc911x_detect_chip(struct smc911x_priv *priv)
106 unsigned long val, i;
108 val = smc911x_reg_read(priv, BYTE_TEST);
109 if (val == 0xffffffff) {
110 /* Special case -- no chip present */
112 } else if (val != 0x87654321) {
113 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
117 val = smc911x_reg_read(priv, ID_REV) >> 16;
118 for (i = 0; chip_ids[i].id != 0; i++) {
119 if (chip_ids[i].id == val) break;
121 if (!chip_ids[i].id) {
122 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
126 priv->chipid = &chip_ids[i];
131 static void smc911x_reset(struct smc911x_priv *priv)
136 * Take out of PM setting first
137 * Device is already wake up if PMT_CTRL_READY bit is set
139 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
140 /* Write to the bytetest will take out of powerdown */
141 smc911x_reg_write(priv, BYTE_TEST, 0x0);
146 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
150 ": timeout waiting for PM restore\n");
155 /* Disable interrupts */
156 smc911x_reg_write(priv, INT_EN, 0);
158 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
161 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
165 printf(DRIVERNAME ": reset timeout\n");
169 /* Reset the FIFO level and flow control settings */
170 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
171 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
173 /* Set to LED outputs */
174 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
177 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
179 unsigned long addrh, addrl;
180 unsigned char *m = priv->enetaddr;
182 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
183 addrh = m[4] | (m[5] << 8);
184 smc911x_set_mac_csr(priv, ADDRL, addrl);
185 smc911x_set_mac_csr(priv, ADDRH, addrh);
187 printf(DRIVERNAME ": MAC %pM\n", m);
190 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
191 u8 phy, u8 reg, u16 *val)
193 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
196 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
199 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
202 *val = smc911x_get_mac_csr(priv, MII_DATA);
207 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
208 u8 phy, u8 reg, u16 val)
210 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
213 smc911x_set_mac_csr(priv, MII_DATA, val);
214 smc911x_set_mac_csr(priv, MII_ACC,
215 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
217 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
222 static int smc911x_phy_reset(struct smc911x_priv *priv)
226 reg = smc911x_reg_read(priv, PMT_CTRL);
228 reg |= PMT_CTRL_PHY_RST;
229 smc911x_reg_write(priv, PMT_CTRL, reg);
236 static void smc911x_phy_configure(struct smc911x_priv *priv)
241 smc911x_phy_reset(priv);
243 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
245 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
246 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
252 if ((timeout--) == 0)
255 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
257 } while (!(status & BMSR_LSTATUS));
259 printf(DRIVERNAME ": phy initialized\n");
264 printf(DRIVERNAME ": autonegotiation timed out\n");
267 static void smc911x_enable(struct smc911x_priv *priv)
270 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
272 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
274 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
276 /* no padding to start of packets */
277 smc911x_reg_write(priv, RX_CFG, 0);
279 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
283 static int smc911x_init_common(struct smc911x_priv *priv)
285 const struct chip_id *id = priv->chipid;
287 printf(DRIVERNAME ": detected %s controller\n", id->name);
291 /* Configure the PHY, initialize the link state */
292 smc911x_phy_configure(priv);
294 smc911x_handle_mac_address(priv);
296 /* Turn on Tx + Rx */
297 smc911x_enable(priv);
302 static int smc911x_send_common(struct smc911x_priv *priv,
303 void *packet, int length)
305 u32 *data = (u32*)packet;
309 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
310 TX_CMD_A_INT_LAST_SEG | length);
311 smc911x_reg_write(priv, TX_DATA_FIFO, length);
313 tmplen = (length + 3) / 4;
316 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
318 /* wait for transmission */
319 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
320 TX_FIFO_INF_TSUSED) >> 16));
322 /* get status. Ignore 'no carrier' error, it has no meaning for
323 * full duplex operation
325 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
326 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
327 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
332 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
333 status & TX_STS_LOC ? "TX_STS_LOC " : "",
334 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
335 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
336 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
337 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
342 static void smc911x_halt_common(struct smc911x_priv *priv)
345 smc911x_handle_mac_address(priv);
348 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
353 status = smc911x_reg_read(priv, RX_FIFO_INF);
354 if (!(status & RX_FIFO_INF_RXSUSED))
357 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
358 pktlen = (status & RX_STS_PKT_LEN) >> 16;
360 smc911x_reg_write(priv, RX_CFG, 0);
362 tmplen = (pktlen + 3) / 4;
364 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
366 if (status & RX_STS_ES) {
368 ": dropped bad packet. Status: 0x%08x\n",
376 #ifndef CONFIG_DM_ETH
378 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
379 /* wrapper for smc911x_eth_phy_read */
380 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
383 struct eth_device *dev = eth_get_dev_by_name(bus->name);
384 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
391 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
398 /* wrapper for smc911x_eth_phy_write */
399 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
402 struct eth_device *dev = eth_get_dev_by_name(bus->name);
403 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
408 return smc911x_eth_phy_write(priv, phy, reg, val);
411 static int smc911x_initialize_mii(struct smc911x_priv *priv)
413 struct mii_dev *mdiodev = mdio_alloc();
419 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
420 mdiodev->read = smc911x_miiphy_read;
421 mdiodev->write = smc911x_miiphy_write;
423 ret = mdio_register(mdiodev);
432 static int smc911x_initialize_mii(struct smc911x_priv *priv)
438 static int smc911x_init(struct eth_device *dev, bd_t *bd)
440 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
442 return smc911x_init_common(priv);
445 static void smc911x_halt(struct eth_device *dev)
447 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
449 smc911x_halt_common(priv);
452 static int smc911x_send(struct eth_device *dev, void *packet, int length)
454 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
456 return smc911x_send_common(priv, packet, length);
459 static int smc911x_recv(struct eth_device *dev)
461 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
462 u32 *data = (u32 *)net_rx_packets[0];
465 ret = smc911x_recv_common(priv, data);
467 net_process_received_packet(net_rx_packets[0], ret);
472 int smc911x_initialize(u8 dev_num, int base_addr)
474 unsigned long addrl, addrh;
475 struct smc911x_priv *priv;
478 priv = calloc(1, sizeof(*priv));
482 priv->iobase = base_addr;
483 priv->dev.iobase = base_addr;
485 /* Try to detect chip. Will fail if not present. */
486 ret = smc911x_detect_chip(priv);
488 ret = 0; /* Card not detected is not an error */
492 addrh = smc911x_get_mac_csr(priv, ADDRH);
493 addrl = smc911x_get_mac_csr(priv, ADDRL);
494 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
495 /* address is obtained from optional eeprom */
496 priv->enetaddr[0] = addrl;
497 priv->enetaddr[1] = addrl >> 8;
498 priv->enetaddr[2] = addrl >> 16;
499 priv->enetaddr[3] = addrl >> 24;
500 priv->enetaddr[4] = addrh;
501 priv->enetaddr[5] = addrh >> 8;
502 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_platdata(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_bind(struct udevice *dev)
570 return device_set_name(dev, dev->name);
573 static int smc911x_probe(struct udevice *dev)
575 struct smc911x_priv *priv = dev_get_priv(dev);
576 unsigned long addrh, addrl;
579 /* Try to detect chip. Will fail if not present. */
580 ret = smc911x_detect_chip(priv);
584 addrh = smc911x_get_mac_csr(priv, ADDRH);
585 addrl = smc911x_get_mac_csr(priv, ADDRL);
586 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
587 /* address is obtained from optional eeprom */
588 priv->enetaddr[0] = addrl;
589 priv->enetaddr[1] = addrl >> 8;
590 priv->enetaddr[2] = addrl >> 16;
591 priv->enetaddr[3] = addrl >> 24;
592 priv->enetaddr[4] = addrh;
593 priv->enetaddr[5] = addrh >> 8;
599 static int smc911x_ofdata_to_platdata(struct udevice *dev)
601 struct smc911x_priv *priv = dev_get_priv(dev);
602 struct eth_pdata *pdata = dev_get_platdata(dev);
604 pdata->iobase = devfdt_get_addr(dev);
605 priv->iobase = pdata->iobase;
610 static const struct eth_ops smc911x_ops = {
611 .start = smc911x_start,
612 .send = smc911x_send,
613 .recv = smc911x_recv,
614 .stop = smc911x_stop,
617 static const struct udevice_id smc911x_ids[] = {
618 { .compatible = "smsc,lan9115" },
622 U_BOOT_DRIVER(smc911x) = {
623 .name = "eth_smc911x",
625 .of_match = smc911x_ids,
626 .bind = smc911x_bind,
627 .ofdata_to_platdata = smc911x_ofdata_to_platdata,
628 .probe = smc911x_probe,
630 .priv_auto_alloc_size = sizeof(struct smc911x_priv),
631 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
632 .flags = DM_FLAG_ALLOC_PRIV_DMA,