blob: f39ba40944f39d1ddfd28f0c6fce7902c8e5054a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sascha Hauerf43a42d2008-04-15 00:08:20 -04002/*
3 * SMSC LAN9[12]1[567] Network driver
4 *
Stelian Pop50497522008-05-08 22:52:09 +02005 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauerf43a42d2008-04-15 00:08:20 -04006 */
7
Sascha Hauerf43a42d2008-04-15 00:08:20 -04008#include <command.h>
Ben Warrenfbfdd3a2009-07-20 22:01:11 -07009#include <malloc.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040010#include <net.h>
11#include <miiphy.h>
Marek Vasutfa8efac2020-03-15 14:42:23 +010012#include <linux/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Marek Vasut689434e2020-03-15 15:03:07 +010014#include <linux/types.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040015
Mike Frysingerad8e4f42009-02-23 10:29:47 -050016#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040017
Marek Vasut689434e2020-03-15 15:03:07 +010018struct chip_id {
19 u16 id;
20 char *name;
21};
22
Marek Vasut59051472020-03-15 15:36:09 +010023struct smc911x_priv {
Marek Vasut59051472020-03-15 15:36:09 +010024 phys_addr_t iobase;
25 const struct chip_id *chipid;
26 unsigned char enetaddr[6];
Andre Przywara9d09b0a2021-06-28 14:30:31 +010027 bool use_32_bit_io;
Marek Vasut59051472020-03-15 15:36:09 +010028};
29
Marek Vasut689434e2020-03-15 15:03:07 +010030static const struct chip_id chip_ids[] = {
31 { CHIP_89218, "LAN89218" },
32 { CHIP_9115, "LAN9115" },
33 { CHIP_9116, "LAN9116" },
34 { CHIP_9117, "LAN9117" },
35 { CHIP_9118, "LAN9118" },
36 { CHIP_9211, "LAN9211" },
37 { CHIP_9215, "LAN9215" },
38 { CHIP_9216, "LAN9216" },
39 { CHIP_9217, "LAN9217" },
40 { CHIP_9218, "LAN9218" },
41 { CHIP_9220, "LAN9220" },
42 { CHIP_9221, "LAN9221" },
43 { 0, NULL },
44};
45
46#define DRIVERNAME "smc911x"
47
Marek Vasut59051472020-03-15 15:36:09 +010048static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010049{
Andre Przywara9d09b0a2021-06-28 14:30:31 +010050 if (priv->use_32_bit_io)
51 return readl(priv->iobase + offset);
Marek Vasut689434e2020-03-15 15:03:07 +010052
Marek Vasut59051472020-03-15 15:36:09 +010053 return (readw(priv->iobase + offset) & 0xffff) |
54 (readw(priv->iobase + offset + 2) << 16);
Marek Vasut689434e2020-03-15 15:03:07 +010055}
Andre Przywara9d09b0a2021-06-28 14:30:31 +010056
Marek Vasut59051472020-03-15 15:36:09 +010057static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010058{
Andre Przywara9d09b0a2021-06-28 14:30:31 +010059 if (priv->use_32_bit_io) {
60 writel(val, priv->iobase + offset);
61 } else {
62 writew(val & 0xffff, priv->iobase + offset);
63 writew(val >> 16, priv->iobase + offset + 2);
64 }
Marek Vasut689434e2020-03-15 15:03:07 +010065}
Marek Vasut689434e2020-03-15 15:03:07 +010066
Marek Vasut59051472020-03-15 15:36:09 +010067static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
Marek Vasut689434e2020-03-15 15:03:07 +010068{
Marek Vasut59051472020-03-15 15:36:09 +010069 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010070 ;
Marek Vasut59051472020-03-15 15:36:09 +010071 smc911x_reg_write(priv, MAC_CSR_CMD,
Marek Vasut689434e2020-03-15 15:03:07 +010072 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
Marek Vasut59051472020-03-15 15:36:09 +010073 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010074 ;
75
Marek Vasut59051472020-03-15 15:36:09 +010076 return smc911x_reg_read(priv, MAC_CSR_DATA);
Marek Vasut689434e2020-03-15 15:03:07 +010077}
78
Marek Vasut59051472020-03-15 15:36:09 +010079static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
Marek Vasut689434e2020-03-15 15:03:07 +010080{
Marek Vasut59051472020-03-15 15:36:09 +010081 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010082 ;
Marek Vasut59051472020-03-15 15:36:09 +010083 smc911x_reg_write(priv, MAC_CSR_DATA, data);
84 smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
85 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010086 ;
87}
88
Marek Vasut59051472020-03-15 15:36:09 +010089static int smc911x_detect_chip(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +010090{
91 unsigned long val, i;
92
Marek Vasut59051472020-03-15 15:36:09 +010093 val = smc911x_reg_read(priv, BYTE_TEST);
Marek Vasut689434e2020-03-15 15:03:07 +010094 if (val == 0xffffffff) {
95 /* Special case -- no chip present */
96 return -1;
97 } else if (val != 0x87654321) {
98 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
99 return -1;
100 }
101
Marek Vasut59051472020-03-15 15:36:09 +0100102 val = smc911x_reg_read(priv, ID_REV) >> 16;
Marek Vasut689434e2020-03-15 15:03:07 +0100103 for (i = 0; chip_ids[i].id != 0; i++) {
104 if (chip_ids[i].id == val) break;
105 }
106 if (!chip_ids[i].id) {
107 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
108 return -1;
109 }
110
Marek Vasut59051472020-03-15 15:36:09 +0100111 priv->chipid = &chip_ids[i];
Marek Vasut689434e2020-03-15 15:03:07 +0100112
113 return 0;
114}
115
Marek Vasut59051472020-03-15 15:36:09 +0100116static void smc911x_reset(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +0100117{
118 int timeout;
119
120 /*
121 * Take out of PM setting first
122 * Device is already wake up if PMT_CTRL_READY bit is set
123 */
Marek Vasut59051472020-03-15 15:36:09 +0100124 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
Marek Vasut689434e2020-03-15 15:03:07 +0100125 /* Write to the bytetest will take out of powerdown */
Marek Vasut59051472020-03-15 15:36:09 +0100126 smc911x_reg_write(priv, BYTE_TEST, 0x0);
Marek Vasut689434e2020-03-15 15:03:07 +0100127
128 timeout = 10;
129
130 while (timeout-- &&
Marek Vasut59051472020-03-15 15:36:09 +0100131 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
Marek Vasut689434e2020-03-15 15:03:07 +0100132 udelay(10);
133 if (timeout < 0) {
134 printf(DRIVERNAME
135 ": timeout waiting for PM restore\n");
136 return;
137 }
138 }
139
140 /* Disable interrupts */
Marek Vasut59051472020-03-15 15:36:09 +0100141 smc911x_reg_write(priv, INT_EN, 0);
Marek Vasut689434e2020-03-15 15:03:07 +0100142
Marek Vasut59051472020-03-15 15:36:09 +0100143 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
Marek Vasut689434e2020-03-15 15:03:07 +0100144
145 timeout = 1000;
Marek Vasut59051472020-03-15 15:36:09 +0100146 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +0100147 udelay(10);
148
149 if (timeout < 0) {
150 printf(DRIVERNAME ": reset timeout\n");
151 return;
152 }
153
154 /* Reset the FIFO level and flow control settings */
Marek Vasut59051472020-03-15 15:36:09 +0100155 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
156 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
Marek Vasut689434e2020-03-15 15:03:07 +0100157
158 /* Set to LED outputs */
Marek Vasut59051472020-03-15 15:36:09 +0100159 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
Marek Vasut689434e2020-03-15 15:03:07 +0100160}
161
Marek Vasut59051472020-03-15 15:36:09 +0100162static void smc911x_handle_mac_address(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400163{
164 unsigned long addrh, addrl;
Marek Vasut59051472020-03-15 15:36:09 +0100165 unsigned char *m = priv->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400166
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700167 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
168 addrh = m[4] | (m[5] << 8);
Marek Vasut59051472020-03-15 15:36:09 +0100169 smc911x_set_mac_csr(priv, ADDRL, addrl);
170 smc911x_set_mac_csr(priv, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400171
Mike Frysinger719b6752009-02-11 19:09:54 -0500172 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400173}
174
Andre Przywara240bfb12020-06-11 12:03:17 +0100175static bool smc911x_read_mac_address(struct smc911x_priv *priv)
176{
177 u32 addrh, addrl;
178
179 /* address is obtained from optional eeprom */
180 addrh = smc911x_get_mac_csr(priv, ADDRH);
181 addrl = smc911x_get_mac_csr(priv, ADDRL);
182 if (addrl == 0xffffffff && addrh == 0x0000ffff)
183 return false;
184
185 priv->enetaddr[0] = addrl;
186 priv->enetaddr[1] = addrl >> 8;
187 priv->enetaddr[2] = addrl >> 16;
188 priv->enetaddr[3] = addrl >> 24;
189 priv->enetaddr[4] = addrh;
190 priv->enetaddr[5] = addrh >> 8;
191
192 return true;
193}
194
Marek Vasut59051472020-03-15 15:36:09 +0100195static int smc911x_eth_phy_read(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700196 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400197{
Marek Vasut59051472020-03-15 15:36:09 +0100198 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000199 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400200
Marek Vasut59051472020-03-15 15:36:09 +0100201 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700202 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400203
Marek Vasut59051472020-03-15 15:36:09 +0100204 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000205 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400206
Marek Vasut59051472020-03-15 15:36:09 +0100207 *val = smc911x_get_mac_csr(priv, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400208
209 return 0;
210}
211
Marek Vasut59051472020-03-15 15:36:09 +0100212static int smc911x_eth_phy_write(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700213 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400214{
Marek Vasut59051472020-03-15 15:36:09 +0100215 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000216 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400217
Marek Vasut59051472020-03-15 15:36:09 +0100218 smc911x_set_mac_csr(priv, MII_DATA, val);
219 smc911x_set_mac_csr(priv, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400220 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
221
Marek Vasut59051472020-03-15 15:36:09 +0100222 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000223 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400224 return 0;
225}
226
Marek Vasut59051472020-03-15 15:36:09 +0100227static int smc911x_phy_reset(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400228{
229 u32 reg;
230
Marek Vasut59051472020-03-15 15:36:09 +0100231 reg = smc911x_reg_read(priv, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400232 reg &= ~0xfffff030;
233 reg |= PMT_CTRL_PHY_RST;
Marek Vasut59051472020-03-15 15:36:09 +0100234 smc911x_reg_write(priv, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400235
236 mdelay(100);
237
238 return 0;
239}
240
Marek Vasut59051472020-03-15 15:36:09 +0100241static void smc911x_phy_configure(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400242{
243 int timeout;
244 u16 status;
245
Marek Vasut59051472020-03-15 15:36:09 +0100246 smc911x_phy_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400247
Marek Vasut59051472020-03-15 15:36:09 +0100248 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400249 mdelay(1);
Marek Vasut59051472020-03-15 15:36:09 +0100250 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
251 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -0500252 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400253
254 timeout = 5000;
255 do {
256 mdelay(1);
257 if ((timeout--) == 0)
258 goto err_out;
259
Marek Vasut59051472020-03-15 15:36:09 +0100260 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400261 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500262 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400263
264 printf(DRIVERNAME ": phy initialized\n");
265
266 return;
267
268err_out:
269 printf(DRIVERNAME ": autonegotiation timed out\n");
270}
271
Marek Vasut59051472020-03-15 15:36:09 +0100272static void smc911x_enable(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400273{
274 /* Enable TX */
Marek Vasut59051472020-03-15 15:36:09 +0100275 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400276
Marek Vasut59051472020-03-15 15:36:09 +0100277 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400278
Marek Vasut59051472020-03-15 15:36:09 +0100279 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400280
281 /* no padding to start of packets */
Marek Vasut59051472020-03-15 15:36:09 +0100282 smc911x_reg_write(priv, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400283
Marek Vasut59051472020-03-15 15:36:09 +0100284 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700285 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400286}
287
Marek Vasutcc830182020-03-15 17:25:27 +0100288static int smc911x_init_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400289{
Marek Vasut59051472020-03-15 15:36:09 +0100290 const struct chip_id *id = priv->chipid;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400291
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100292 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400293
Marek Vasut59051472020-03-15 15:36:09 +0100294 smc911x_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400295
296 /* Configure the PHY, initialize the link state */
Marek Vasut59051472020-03-15 15:36:09 +0100297 smc911x_phy_configure(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400298
Marek Vasut59051472020-03-15 15:36:09 +0100299 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400300
301 /* Turn on Tx + Rx */
Marek Vasut59051472020-03-15 15:36:09 +0100302 smc911x_enable(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400303
304 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400305}
306
Marek Vasutcc830182020-03-15 17:25:27 +0100307static int smc911x_send_common(struct smc911x_priv *priv,
308 void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400309{
310 u32 *data = (u32*)packet;
311 u32 tmplen;
312 u32 status;
313
Marek Vasut59051472020-03-15 15:36:09 +0100314 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700315 TX_CMD_A_INT_LAST_SEG | length);
Marek Vasut59051472020-03-15 15:36:09 +0100316 smc911x_reg_write(priv, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400317
318 tmplen = (length + 3) / 4;
319
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000320 while (tmplen--)
Marek Vasut59051472020-03-15 15:36:09 +0100321 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400322
323 /* wait for transmission */
Marek Vasut59051472020-03-15 15:36:09 +0100324 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700325 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400326
327 /* get status. Ignore 'no carrier' error, it has no meaning for
328 * full duplex operation
329 */
Marek Vasut59051472020-03-15 15:36:09 +0100330 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700331 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
332 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400333
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000334 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400335 return 0;
336
337 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
338 status & TX_STS_LOC ? "TX_STS_LOC " : "",
339 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
340 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
341 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
342 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
343
344 return -1;
345}
346
Marek Vasutcc830182020-03-15 17:25:27 +0100347static void smc911x_halt_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400348{
Marek Vasut59051472020-03-15 15:36:09 +0100349 smc911x_reset(priv);
350 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400351}
352
Marek Vasutcc830182020-03-15 17:25:27 +0100353static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400354{
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400355 u32 pktlen, tmplen;
356 u32 status;
357
Marek Vasutc70631f2020-03-15 17:02:05 +0100358 status = smc911x_reg_read(priv, RX_FIFO_INF);
359 if (!(status & RX_FIFO_INF_RXSUSED))
360 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400361
Marek Vasutc70631f2020-03-15 17:02:05 +0100362 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
363 pktlen = (status & RX_STS_PKT_LEN) >> 16;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400364
Marek Vasutc70631f2020-03-15 17:02:05 +0100365 smc911x_reg_write(priv, RX_CFG, 0);
366
367 tmplen = (pktlen + 3) / 4;
368 while (tmplen--)
369 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400370
Marek Vasutcc830182020-03-15 17:25:27 +0100371 if (status & RX_STS_ES) {
Marek Vasutc70631f2020-03-15 17:02:05 +0100372 printf(DRIVERNAME
373 ": dropped bad packet. Status: 0x%08x\n",
374 status);
Marek Vasutcc830182020-03-15 17:25:27 +0100375 return 0;
376 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700377
Marek Vasutcc830182020-03-15 17:25:27 +0100378 return pktlen;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700379}
380
Marek Vasut55399442020-03-15 17:39:01 +0100381static int smc911x_start(struct udevice *dev)
382{
Simon Glassfa20e932020-12-03 16:55:20 -0700383 struct eth_pdata *plat = dev_get_plat(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100384 struct smc911x_priv *priv = dev_get_priv(dev);
385
386 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
387
388 return smc911x_init_common(priv);
389}
390
391static void smc911x_stop(struct udevice *dev)
392{
393 struct smc911x_priv *priv = dev_get_priv(dev);
394
395 smc911x_halt_common(priv);
396}
397
398static int smc911x_send(struct udevice *dev, void *packet, int length)
399{
400 struct smc911x_priv *priv = dev_get_priv(dev);
401 int ret;
402
403 ret = smc911x_send_common(priv, packet, length);
404
Maxim Uvarovf9891472023-12-26 21:46:13 +0600405 return ret ? -ETIMEDOUT : 0;
Marek Vasut55399442020-03-15 17:39:01 +0100406}
407
408static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
409{
410 struct smc911x_priv *priv = dev_get_priv(dev);
411 u32 *data = (u32 *)net_rx_packets[0];
412 int ret;
413
414 ret = smc911x_recv_common(priv, data);
415 if (ret)
416 *packetp = (void *)data;
417
418 return ret ? ret : -EAGAIN;
419}
420
Andre Przywara240bfb12020-06-11 12:03:17 +0100421static int smc911x_read_rom_hwaddr(struct udevice *dev)
422{
423 struct smc911x_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700424 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywara240bfb12020-06-11 12:03:17 +0100425
426 if (!smc911x_read_mac_address(priv))
427 return -ENODEV;
428
429 memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
430
431 return 0;
432}
433
Marek Vasut55399442020-03-15 17:39:01 +0100434static int smc911x_bind(struct udevice *dev)
435{
436 return device_set_name(dev, dev->name);
437}
438
439static int smc911x_probe(struct udevice *dev)
440{
441 struct smc911x_priv *priv = dev_get_priv(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100442 int ret;
443
444 /* Try to detect chip. Will fail if not present. */
445 ret = smc911x_detect_chip(priv);
446 if (ret)
447 return ret;
448
Andre Przywara240bfb12020-06-11 12:03:17 +0100449 smc911x_read_rom_hwaddr(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100450
451 return 0;
452}
453
Simon Glassaad29ae2020-12-03 16:55:21 -0700454static int smc911x_of_to_plat(struct udevice *dev)
Marek Vasut55399442020-03-15 17:39:01 +0100455{
456 struct smc911x_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700457 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywara9d09b0a2021-06-28 14:30:31 +0100458 u32 io_width;
459 int ret;
Marek Vasut55399442020-03-15 17:39:01 +0100460
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900461 pdata->iobase = dev_read_addr(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100462 priv->iobase = pdata->iobase;
463
Andre Przywara9d09b0a2021-06-28 14:30:31 +0100464 ret = dev_read_u32(dev, "reg-io-width", &io_width);
465 if (!ret)
466 priv->use_32_bit_io = (io_width == 4);
467 else
468 priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
469
Marek Vasut55399442020-03-15 17:39:01 +0100470 return 0;
471}
472
473static const struct eth_ops smc911x_ops = {
474 .start = smc911x_start,
475 .send = smc911x_send,
476 .recv = smc911x_recv,
477 .stop = smc911x_stop,
Andre Przywara240bfb12020-06-11 12:03:17 +0100478 .read_rom_hwaddr = smc911x_read_rom_hwaddr,
Marek Vasut55399442020-03-15 17:39:01 +0100479};
480
481static const struct udevice_id smc911x_ids[] = {
482 { .compatible = "smsc,lan9115" },
483 { }
484};
485
486U_BOOT_DRIVER(smc911x) = {
487 .name = "eth_smc911x",
488 .id = UCLASS_ETH,
489 .of_match = smc911x_ids,
490 .bind = smc911x_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700491 .of_to_plat = smc911x_of_to_plat,
Marek Vasut55399442020-03-15 17:39:01 +0100492 .probe = smc911x_probe,
493 .ops = &smc911x_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700494 .priv_auto = sizeof(struct smc911x_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700495 .plat_auto = sizeof(struct eth_pdata),
Marek Vasut55399442020-03-15 17:39:01 +0100496 .flags = DM_FLAG_ALLOC_PRIV_DMA,
497};