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/types.h>
25 struct eth_device dev;
28 const struct chip_id *chipid;
29 unsigned char enetaddr[6];
32 static const struct chip_id chip_ids[] = {
33 { CHIP_89218, "LAN89218" },
34 { CHIP_9115, "LAN9115" },
35 { CHIP_9116, "LAN9116" },
36 { CHIP_9117, "LAN9117" },
37 { CHIP_9118, "LAN9118" },
38 { CHIP_9211, "LAN9211" },
39 { CHIP_9215, "LAN9215" },
40 { CHIP_9216, "LAN9216" },
41 { CHIP_9217, "LAN9217" },
42 { CHIP_9218, "LAN9218" },
43 { CHIP_9220, "LAN9220" },
44 { CHIP_9221, "LAN9221" },
48 #define DRIVERNAME "smc911x"
50 #if defined (CONFIG_SMC911X_32_BIT) && \
51 defined (CONFIG_SMC911X_16_BIT)
52 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
53 CONFIG_SMC911X_16_BIT shall be set"
56 #if defined (CONFIG_SMC911X_32_BIT)
57 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
59 return readl(priv->iobase + offset);
62 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
64 writel(val, priv->iobase + offset);
66 #elif defined (CONFIG_SMC911X_16_BIT)
67 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
69 return (readw(priv->iobase + offset) & 0xffff) |
70 (readw(priv->iobase + offset + 2) << 16);
72 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
74 writew(val & 0xffff, priv->iobase + offset);
75 writew(val >> 16, priv->iobase + offset + 2);
78 #error "SMC911X: undefined bus width"
79 #endif /* CONFIG_SMC911X_16_BIT */
81 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
83 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
85 smc911x_reg_write(priv, MAC_CSR_CMD,
86 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
87 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
90 return smc911x_reg_read(priv, MAC_CSR_DATA);
93 static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
95 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
97 smc911x_reg_write(priv, MAC_CSR_DATA, data);
98 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
99 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
103 static int smc911x_detect_chip(struct smc911x_priv *priv)
105 unsigned long val, i;
107 val = smc911x_reg_read(priv, BYTE_TEST);
108 if (val == 0xffffffff) {
109 /* Special case -- no chip present */
111 } else if (val != 0x87654321) {
112 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
116 val = smc911x_reg_read(priv, ID_REV) >> 16;
117 for (i = 0; chip_ids[i].id != 0; i++) {
118 if (chip_ids[i].id == val) break;
120 if (!chip_ids[i].id) {
121 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
125 priv->chipid = &chip_ids[i];
130 static void smc911x_reset(struct smc911x_priv *priv)
135 * Take out of PM setting first
136 * Device is already wake up if PMT_CTRL_READY bit is set
138 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
139 /* Write to the bytetest will take out of powerdown */
140 smc911x_reg_write(priv, BYTE_TEST, 0x0);
145 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
149 ": timeout waiting for PM restore\n");
154 /* Disable interrupts */
155 smc911x_reg_write(priv, INT_EN, 0);
157 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
160 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
164 printf(DRIVERNAME ": reset timeout\n");
168 /* Reset the FIFO level and flow control settings */
169 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
170 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
172 /* Set to LED outputs */
173 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
176 static void smc911x_handle_mac_address(struct smc911x_priv *priv)
178 unsigned long addrh, addrl;
179 unsigned char *m = priv->enetaddr;
181 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
182 addrh = m[4] | (m[5] << 8);
183 smc911x_set_mac_csr(priv, ADDRL, addrl);
184 smc911x_set_mac_csr(priv, ADDRH, addrh);
186 printf(DRIVERNAME ": MAC %pM\n", m);
189 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
190 u8 phy, u8 reg, u16 *val)
192 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
195 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
198 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
201 *val = smc911x_get_mac_csr(priv, MII_DATA);
206 static int smc911x_eth_phy_write(struct smc911x_priv *priv,
207 u8 phy, u8 reg, u16 val)
209 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
212 smc911x_set_mac_csr(priv, MII_DATA, val);
213 smc911x_set_mac_csr(priv, MII_ACC,
214 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
216 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
221 static int smc911x_phy_reset(struct smc911x_priv *priv)
225 reg = smc911x_reg_read(priv, PMT_CTRL);
227 reg |= PMT_CTRL_PHY_RST;
228 smc911x_reg_write(priv, PMT_CTRL, reg);
235 static void smc911x_phy_configure(struct smc911x_priv *priv)
240 smc911x_phy_reset(priv);
242 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
244 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
245 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
251 if ((timeout--) == 0)
254 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
256 } while (!(status & BMSR_LSTATUS));
258 printf(DRIVERNAME ": phy initialized\n");
263 printf(DRIVERNAME ": autonegotiation timed out\n");
266 static void smc911x_enable(struct smc911x_priv *priv)
269 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
271 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
273 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
275 /* no padding to start of packets */
276 smc911x_reg_write(priv, RX_CFG, 0);
278 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
282 static int smc911x_init_common(struct smc911x_priv *priv)
284 const struct chip_id *id = priv->chipid;
286 printf(DRIVERNAME ": detected %s controller\n", id->name);
290 /* Configure the PHY, initialize the link state */
291 smc911x_phy_configure(priv);
293 smc911x_handle_mac_address(priv);
295 /* Turn on Tx + Rx */
296 smc911x_enable(priv);
301 static int smc911x_send_common(struct smc911x_priv *priv,
302 void *packet, int length)
304 u32 *data = (u32*)packet;
308 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
309 TX_CMD_A_INT_LAST_SEG | length);
310 smc911x_reg_write(priv, TX_DATA_FIFO, length);
312 tmplen = (length + 3) / 4;
315 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
317 /* wait for transmission */
318 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
319 TX_FIFO_INF_TSUSED) >> 16));
321 /* get status. Ignore 'no carrier' error, it has no meaning for
322 * full duplex operation
324 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
325 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
326 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
331 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
332 status & TX_STS_LOC ? "TX_STS_LOC " : "",
333 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
334 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
335 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
336 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
341 static void smc911x_halt_common(struct smc911x_priv *priv)
344 smc911x_handle_mac_address(priv);
347 static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
352 status = smc911x_reg_read(priv, RX_FIFO_INF);
353 if (!(status & RX_FIFO_INF_RXSUSED))
356 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
357 pktlen = (status & RX_STS_PKT_LEN) >> 16;
359 smc911x_reg_write(priv, RX_CFG, 0);
361 tmplen = (pktlen + 3) / 4;
363 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
365 if (status & RX_STS_ES) {
367 ": dropped bad packet. Status: 0x%08x\n",
375 #ifndef CONFIG_DM_ETH
377 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
378 /* wrapper for smc911x_eth_phy_read */
379 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
382 struct eth_device *dev = eth_get_dev_by_name(bus->name);
383 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
390 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
397 /* wrapper for smc911x_eth_phy_write */
398 static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
401 struct eth_device *dev = eth_get_dev_by_name(bus->name);
402 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
407 return smc911x_eth_phy_write(priv, phy, reg, val);
410 static int smc911x_initialize_mii(struct smc911x_priv *priv)
412 struct mii_dev *mdiodev = mdio_alloc();
418 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
419 mdiodev->read = smc911x_miiphy_read;
420 mdiodev->write = smc911x_miiphy_write;
422 ret = mdio_register(mdiodev);
431 static int smc911x_initialize_mii(struct smc911x_priv *priv)
437 static int smc911x_init(struct eth_device *dev, bd_t *bd)
439 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
441 return smc911x_init_common(priv);
444 static void smc911x_halt(struct eth_device *dev)
446 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
448 smc911x_halt_common(priv);
451 static int smc911x_send(struct eth_device *dev, void *packet, int length)
453 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
455 return smc911x_send_common(priv, packet, length);
458 static int smc911x_recv(struct eth_device *dev)
460 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
461 u32 *data = (u32 *)net_rx_packets[0];
464 ret = smc911x_recv_common(priv, data);
466 net_process_received_packet(net_rx_packets[0], ret);
471 int smc911x_initialize(u8 dev_num, int base_addr)
473 unsigned long addrl, addrh;
474 struct smc911x_priv *priv;
477 priv = calloc(1, sizeof(*priv));
481 priv->iobase = base_addr;
482 priv->dev.iobase = base_addr;
484 /* Try to detect chip. Will fail if not present. */
485 ret = smc911x_detect_chip(priv);
487 ret = 0; /* Card not detected is not an error */
491 addrh = smc911x_get_mac_csr(priv, ADDRH);
492 addrl = smc911x_get_mac_csr(priv, ADDRL);
493 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
494 /* address is obtained from optional eeprom */
495 priv->enetaddr[0] = addrl;
496 priv->enetaddr[1] = addrl >> 8;
497 priv->enetaddr[2] = addrl >> 16;
498 priv->enetaddr[3] = addrl >> 24;
499 priv->enetaddr[4] = addrh;
500 priv->enetaddr[5] = addrh >> 8;
501 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
504 priv->dev.init = smc911x_init;
505 priv->dev.halt = smc911x_halt;
506 priv->dev.send = smc911x_send;
507 priv->dev.recv = smc911x_recv;
508 sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
510 eth_register(&priv->dev);
512 ret = smc911x_initialize_mii(priv);
519 eth_unregister(&priv->dev);
525 #else /* ifdef CONFIG_DM_ETH */
527 static int smc911x_start(struct udevice *dev)
529 struct eth_pdata *plat = dev_get_platdata(dev);
530 struct smc911x_priv *priv = dev_get_priv(dev);
532 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
534 return smc911x_init_common(priv);
537 static void smc911x_stop(struct udevice *dev)
539 struct smc911x_priv *priv = dev_get_priv(dev);
541 smc911x_halt_common(priv);
544 static int smc911x_send(struct udevice *dev, void *packet, int length)
546 struct smc911x_priv *priv = dev_get_priv(dev);
549 ret = smc911x_send_common(priv, packet, length);
551 return ret ? 0 : -ETIMEDOUT;
554 static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
556 struct smc911x_priv *priv = dev_get_priv(dev);
557 u32 *data = (u32 *)net_rx_packets[0];
560 ret = smc911x_recv_common(priv, data);
562 *packetp = (void *)data;
564 return ret ? ret : -EAGAIN;
567 static int smc911x_bind(struct udevice *dev)
569 return device_set_name(dev, dev->name);
572 static int smc911x_probe(struct udevice *dev)
574 struct smc911x_priv *priv = dev_get_priv(dev);
575 unsigned long addrh, addrl;
578 /* Try to detect chip. Will fail if not present. */
579 ret = smc911x_detect_chip(priv);
583 addrh = smc911x_get_mac_csr(priv, ADDRH);
584 addrl = smc911x_get_mac_csr(priv, ADDRL);
585 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
586 /* address is obtained from optional eeprom */
587 priv->enetaddr[0] = addrl;
588 priv->enetaddr[1] = addrl >> 8;
589 priv->enetaddr[2] = addrl >> 16;
590 priv->enetaddr[3] = addrl >> 24;
591 priv->enetaddr[4] = addrh;
592 priv->enetaddr[5] = addrh >> 8;
598 static int smc911x_ofdata_to_platdata(struct udevice *dev)
600 struct smc911x_priv *priv = dev_get_priv(dev);
601 struct eth_pdata *pdata = dev_get_platdata(dev);
603 pdata->iobase = devfdt_get_addr(dev);
604 priv->iobase = pdata->iobase;
609 static const struct eth_ops smc911x_ops = {
610 .start = smc911x_start,
611 .send = smc911x_send,
612 .recv = smc911x_recv,
613 .stop = smc911x_stop,
616 static const struct udevice_id smc911x_ids[] = {
617 { .compatible = "smsc,lan9115" },
621 U_BOOT_DRIVER(smc911x) = {
622 .name = "eth_smc911x",
624 .of_match = smc911x_ids,
625 .bind = smc911x_bind,
626 .ofdata_to_platdata = smc911x_ofdata_to_platdata,
627 .probe = smc911x_probe,
629 .priv_auto_alloc_size = sizeof(struct smc911x_priv),
630 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
631 .flags = DM_FLAG_ALLOC_PRIV_DMA,