blob: 3afebee440220b93e85dd1974ed51bcc116b436a [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
8#include <common.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -04009#include <command.h>
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070010#include <malloc.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040011#include <net.h>
12#include <miiphy.h>
Marek Vasutfa8efac2020-03-15 14:42:23 +010013#include <linux/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Marek Vasut689434e2020-03-15 15:03:07 +010015#include <linux/types.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040016
Mike Frysingerad8e4f42009-02-23 10:29:47 -050017#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040018
Marek Vasut689434e2020-03-15 15:03:07 +010019struct chip_id {
20 u16 id;
21 char *name;
22};
23
Marek Vasut59051472020-03-15 15:36:09 +010024struct smc911x_priv {
Marek Vasut55399442020-03-15 17:39:01 +010025#ifndef CONFIG_DM_ETH
Marek Vasut59051472020-03-15 15:36:09 +010026 struct eth_device dev;
Marek Vasut55399442020-03-15 17:39:01 +010027#endif
Marek Vasut59051472020-03-15 15:36:09 +010028 phys_addr_t iobase;
29 const struct chip_id *chipid;
30 unsigned char enetaddr[6];
Andre Przywara9d09b0a2021-06-28 14:30:31 +010031 bool use_32_bit_io;
Marek Vasut59051472020-03-15 15:36:09 +010032};
33
Marek Vasut689434e2020-03-15 15:03:07 +010034static 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" },
47 { 0, NULL },
48};
49
50#define DRIVERNAME "smc911x"
51
Marek Vasut59051472020-03-15 15:36:09 +010052static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010053{
Andre Przywara9d09b0a2021-06-28 14:30:31 +010054 if (priv->use_32_bit_io)
55 return readl(priv->iobase + offset);
Marek Vasut689434e2020-03-15 15:03:07 +010056
Marek Vasut59051472020-03-15 15:36:09 +010057 return (readw(priv->iobase + offset) & 0xffff) |
58 (readw(priv->iobase + offset + 2) << 16);
Marek Vasut689434e2020-03-15 15:03:07 +010059}
Andre Przywara9d09b0a2021-06-28 14:30:31 +010060
Marek Vasut59051472020-03-15 15:36:09 +010061static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010062{
Andre Przywara9d09b0a2021-06-28 14:30:31 +010063 if (priv->use_32_bit_io) {
64 writel(val, priv->iobase + offset);
65 } else {
66 writew(val & 0xffff, priv->iobase + offset);
67 writew(val >> 16, priv->iobase + offset + 2);
68 }
Marek Vasut689434e2020-03-15 15:03:07 +010069}
Marek Vasut689434e2020-03-15 15:03:07 +010070
Marek Vasut59051472020-03-15 15:36:09 +010071static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
Marek Vasut689434e2020-03-15 15:03:07 +010072{
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 ;
Marek Vasut59051472020-03-15 15:36:09 +010075 smc911x_reg_write(priv, MAC_CSR_CMD,
Marek Vasut689434e2020-03-15 15:03:07 +010076 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
Marek Vasut59051472020-03-15 15:36:09 +010077 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010078 ;
79
Marek Vasut59051472020-03-15 15:36:09 +010080 return smc911x_reg_read(priv, MAC_CSR_DATA);
Marek Vasut689434e2020-03-15 15:03:07 +010081}
82
Marek Vasut59051472020-03-15 15:36:09 +010083static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
Marek Vasut689434e2020-03-15 15:03:07 +010084{
Marek Vasut59051472020-03-15 15:36:09 +010085 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010086 ;
Marek Vasut59051472020-03-15 15:36:09 +010087 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)
Marek Vasut689434e2020-03-15 15:03:07 +010090 ;
91}
92
Marek Vasut59051472020-03-15 15:36:09 +010093static int smc911x_detect_chip(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +010094{
95 unsigned long val, i;
96
Marek Vasut59051472020-03-15 15:36:09 +010097 val = smc911x_reg_read(priv, BYTE_TEST);
Marek Vasut689434e2020-03-15 15:03:07 +010098 if (val == 0xffffffff) {
99 /* Special case -- no chip present */
100 return -1;
101 } else if (val != 0x87654321) {
102 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
103 return -1;
104 }
105
Marek Vasut59051472020-03-15 15:36:09 +0100106 val = smc911x_reg_read(priv, ID_REV) >> 16;
Marek Vasut689434e2020-03-15 15:03:07 +0100107 for (i = 0; chip_ids[i].id != 0; i++) {
108 if (chip_ids[i].id == val) break;
109 }
110 if (!chip_ids[i].id) {
111 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
112 return -1;
113 }
114
Marek Vasut59051472020-03-15 15:36:09 +0100115 priv->chipid = &chip_ids[i];
Marek Vasut689434e2020-03-15 15:03:07 +0100116
117 return 0;
118}
119
Marek Vasut59051472020-03-15 15:36:09 +0100120static void smc911x_reset(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +0100121{
122 int timeout;
123
124 /*
125 * Take out of PM setting first
126 * Device is already wake up if PMT_CTRL_READY bit is set
127 */
Marek Vasut59051472020-03-15 15:36:09 +0100128 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
Marek Vasut689434e2020-03-15 15:03:07 +0100129 /* Write to the bytetest will take out of powerdown */
Marek Vasut59051472020-03-15 15:36:09 +0100130 smc911x_reg_write(priv, BYTE_TEST, 0x0);
Marek Vasut689434e2020-03-15 15:03:07 +0100131
132 timeout = 10;
133
134 while (timeout-- &&
Marek Vasut59051472020-03-15 15:36:09 +0100135 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
Marek Vasut689434e2020-03-15 15:03:07 +0100136 udelay(10);
137 if (timeout < 0) {
138 printf(DRIVERNAME
139 ": timeout waiting for PM restore\n");
140 return;
141 }
142 }
143
144 /* Disable interrupts */
Marek Vasut59051472020-03-15 15:36:09 +0100145 smc911x_reg_write(priv, INT_EN, 0);
Marek Vasut689434e2020-03-15 15:03:07 +0100146
Marek Vasut59051472020-03-15 15:36:09 +0100147 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
Marek Vasut689434e2020-03-15 15:03:07 +0100148
149 timeout = 1000;
Marek Vasut59051472020-03-15 15:36:09 +0100150 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +0100151 udelay(10);
152
153 if (timeout < 0) {
154 printf(DRIVERNAME ": reset timeout\n");
155 return;
156 }
157
158 /* Reset the FIFO level and flow control settings */
Marek Vasut59051472020-03-15 15:36:09 +0100159 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
160 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
Marek Vasut689434e2020-03-15 15:03:07 +0100161
162 /* Set to LED outputs */
Marek Vasut59051472020-03-15 15:36:09 +0100163 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
Marek Vasut689434e2020-03-15 15:03:07 +0100164}
165
Marek Vasut59051472020-03-15 15:36:09 +0100166static void smc911x_handle_mac_address(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400167{
168 unsigned long addrh, addrl;
Marek Vasut59051472020-03-15 15:36:09 +0100169 unsigned char *m = priv->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400170
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700171 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
172 addrh = m[4] | (m[5] << 8);
Marek Vasut59051472020-03-15 15:36:09 +0100173 smc911x_set_mac_csr(priv, ADDRL, addrl);
174 smc911x_set_mac_csr(priv, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400175
Mike Frysinger719b6752009-02-11 19:09:54 -0500176 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400177}
178
Andre Przywara240bfb12020-06-11 12:03:17 +0100179static bool smc911x_read_mac_address(struct smc911x_priv *priv)
180{
181 u32 addrh, addrl;
182
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)
187 return false;
188
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;
195
196 return true;
197}
198
Marek Vasut59051472020-03-15 15:36:09 +0100199static int smc911x_eth_phy_read(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700200 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400201{
Marek Vasut59051472020-03-15 15:36:09 +0100202 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000203 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400204
Marek Vasut59051472020-03-15 15:36:09 +0100205 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700206 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400207
Marek Vasut59051472020-03-15 15:36:09 +0100208 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000209 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400210
Marek Vasut59051472020-03-15 15:36:09 +0100211 *val = smc911x_get_mac_csr(priv, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400212
213 return 0;
214}
215
Marek Vasut59051472020-03-15 15:36:09 +0100216static int smc911x_eth_phy_write(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700217 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400218{
Marek Vasut59051472020-03-15 15:36:09 +0100219 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000220 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400221
Marek Vasut59051472020-03-15 15:36:09 +0100222 smc911x_set_mac_csr(priv, MII_DATA, val);
223 smc911x_set_mac_csr(priv, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400224 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
225
Marek Vasut59051472020-03-15 15:36:09 +0100226 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000227 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400228 return 0;
229}
230
Marek Vasut59051472020-03-15 15:36:09 +0100231static int smc911x_phy_reset(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400232{
233 u32 reg;
234
Marek Vasut59051472020-03-15 15:36:09 +0100235 reg = smc911x_reg_read(priv, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400236 reg &= ~0xfffff030;
237 reg |= PMT_CTRL_PHY_RST;
Marek Vasut59051472020-03-15 15:36:09 +0100238 smc911x_reg_write(priv, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400239
240 mdelay(100);
241
242 return 0;
243}
244
Marek Vasut59051472020-03-15 15:36:09 +0100245static void smc911x_phy_configure(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400246{
247 int timeout;
248 u16 status;
249
Marek Vasut59051472020-03-15 15:36:09 +0100250 smc911x_phy_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400251
Marek Vasut59051472020-03-15 15:36:09 +0100252 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400253 mdelay(1);
Marek Vasut59051472020-03-15 15:36:09 +0100254 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
255 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -0500256 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400257
258 timeout = 5000;
259 do {
260 mdelay(1);
261 if ((timeout--) == 0)
262 goto err_out;
263
Marek Vasut59051472020-03-15 15:36:09 +0100264 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400265 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500266 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400267
268 printf(DRIVERNAME ": phy initialized\n");
269
270 return;
271
272err_out:
273 printf(DRIVERNAME ": autonegotiation timed out\n");
274}
275
Marek Vasut59051472020-03-15 15:36:09 +0100276static void smc911x_enable(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400277{
278 /* Enable TX */
Marek Vasut59051472020-03-15 15:36:09 +0100279 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400280
Marek Vasut59051472020-03-15 15:36:09 +0100281 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400282
Marek Vasut59051472020-03-15 15:36:09 +0100283 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400284
285 /* no padding to start of packets */
Marek Vasut59051472020-03-15 15:36:09 +0100286 smc911x_reg_write(priv, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400287
Marek Vasut59051472020-03-15 15:36:09 +0100288 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700289 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400290}
291
Marek Vasutcc830182020-03-15 17:25:27 +0100292static int smc911x_init_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400293{
Marek Vasut59051472020-03-15 15:36:09 +0100294 const struct chip_id *id = priv->chipid;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400295
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100296 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400297
Marek Vasut59051472020-03-15 15:36:09 +0100298 smc911x_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400299
300 /* Configure the PHY, initialize the link state */
Marek Vasut59051472020-03-15 15:36:09 +0100301 smc911x_phy_configure(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400302
Marek Vasut59051472020-03-15 15:36:09 +0100303 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400304
305 /* Turn on Tx + Rx */
Marek Vasut59051472020-03-15 15:36:09 +0100306 smc911x_enable(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400307
308 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400309}
310
Marek Vasutcc830182020-03-15 17:25:27 +0100311static int smc911x_send_common(struct smc911x_priv *priv,
312 void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400313{
314 u32 *data = (u32*)packet;
315 u32 tmplen;
316 u32 status;
317
Marek Vasut59051472020-03-15 15:36:09 +0100318 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700319 TX_CMD_A_INT_LAST_SEG | length);
Marek Vasut59051472020-03-15 15:36:09 +0100320 smc911x_reg_write(priv, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400321
322 tmplen = (length + 3) / 4;
323
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000324 while (tmplen--)
Marek Vasut59051472020-03-15 15:36:09 +0100325 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400326
327 /* wait for transmission */
Marek Vasut59051472020-03-15 15:36:09 +0100328 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700329 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400330
331 /* get status. Ignore 'no carrier' error, it has no meaning for
332 * full duplex operation
333 */
Marek Vasut59051472020-03-15 15:36:09 +0100334 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700335 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
336 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400337
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000338 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400339 return 0;
340
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" : "");
347
348 return -1;
349}
350
Marek Vasutcc830182020-03-15 17:25:27 +0100351static void smc911x_halt_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400352{
Marek Vasut59051472020-03-15 15:36:09 +0100353 smc911x_reset(priv);
354 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400355}
356
Marek Vasutcc830182020-03-15 17:25:27 +0100357static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400358{
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400359 u32 pktlen, tmplen;
360 u32 status;
361
Marek Vasutc70631f2020-03-15 17:02:05 +0100362 status = smc911x_reg_read(priv, RX_FIFO_INF);
363 if (!(status & RX_FIFO_INF_RXSUSED))
364 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400365
Marek Vasutc70631f2020-03-15 17:02:05 +0100366 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
367 pktlen = (status & RX_STS_PKT_LEN) >> 16;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400368
Marek Vasutc70631f2020-03-15 17:02:05 +0100369 smc911x_reg_write(priv, RX_CFG, 0);
370
371 tmplen = (pktlen + 3) / 4;
372 while (tmplen--)
373 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400374
Marek Vasutcc830182020-03-15 17:25:27 +0100375 if (status & RX_STS_ES) {
Marek Vasutc70631f2020-03-15 17:02:05 +0100376 printf(DRIVERNAME
377 ": dropped bad packet. Status: 0x%08x\n",
378 status);
Marek Vasutcc830182020-03-15 17:25:27 +0100379 return 0;
380 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700381
Marek Vasutcc830182020-03-15 17:25:27 +0100382 return pktlen;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700383}
384
Marek Vasut55399442020-03-15 17:39:01 +0100385#ifndef CONFIG_DM_ETH
386
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000387#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
388/* wrapper for smc911x_eth_phy_read */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500389static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
390 int reg)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000391{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500392 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Marek Vasut59051472020-03-15 15:36:09 +0100393 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
Marek Vasut14330e52020-03-15 15:43:20 +0100394 u16 val = 0;
395 int ret;
396
Marek Vasut59051472020-03-15 15:36:09 +0100397 if (!dev || !priv)
Marek Vasut14330e52020-03-15 15:43:20 +0100398 return -ENODEV;
399
Marek Vasut59051472020-03-15 15:36:09 +0100400 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
Marek Vasut14330e52020-03-15 15:43:20 +0100401 if (ret < 0)
402 return ret;
403
404 return val;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000405}
Marek Vasut59051472020-03-15 15:36:09 +0100406
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000407/* wrapper for smc911x_eth_phy_write */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500408static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
409 int reg, u16 val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000410{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500411 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Marek Vasut59051472020-03-15 15:36:09 +0100412 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
Marek Vasut14330e52020-03-15 15:43:20 +0100413
Marek Vasut59051472020-03-15 15:36:09 +0100414 if (!dev || !priv)
Marek Vasut14330e52020-03-15 15:43:20 +0100415 return -ENODEV;
416
Marek Vasut59051472020-03-15 15:36:09 +0100417 return smc911x_eth_phy_write(priv, phy, reg, val);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000418}
Marek Vasut25eb4682020-03-21 17:25:41 +0100419
Marek Vasut59051472020-03-15 15:36:09 +0100420static int smc911x_initialize_mii(struct smc911x_priv *priv)
Marek Vasut25eb4682020-03-21 17:25:41 +0100421{
422 struct mii_dev *mdiodev = mdio_alloc();
423 int ret;
424
425 if (!mdiodev)
426 return -ENOMEM;
427
Marek Vasut59051472020-03-15 15:36:09 +0100428 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
Marek Vasut25eb4682020-03-21 17:25:41 +0100429 mdiodev->read = smc911x_miiphy_read;
430 mdiodev->write = smc911x_miiphy_write;
431
432 ret = mdio_register(mdiodev);
433 if (ret < 0) {
434 mdio_free(mdiodev);
435 return ret;
436 }
437
438 return 0;
439}
440#else
Marek Vasut59051472020-03-15 15:36:09 +0100441static int smc911x_initialize_mii(struct smc911x_priv *priv)
Marek Vasut25eb4682020-03-21 17:25:41 +0100442{
443 return 0;
444}
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000445#endif
446
Masahiro Yamada940a9222020-06-26 15:13:34 +0900447static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
Marek Vasutcc830182020-03-15 17:25:27 +0100448{
449 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
450
451 return smc911x_init_common(priv);
452}
453
454static void smc911x_halt(struct eth_device *dev)
455{
456 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
457
458 smc911x_halt_common(priv);
459}
460
461static int smc911x_send(struct eth_device *dev, void *packet, int length)
462{
463 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
464
465 return smc911x_send_common(priv, packet, length);
466}
467
468static int smc911x_recv(struct eth_device *dev)
469{
470 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
471 u32 *data = (u32 *)net_rx_packets[0];
472 int ret;
473
474 ret = smc911x_recv_common(priv, data);
475 if (ret)
476 net_process_received_packet(net_rx_packets[0], ret);
477
478 return ret;
479}
480
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700481int smc911x_initialize(u8 dev_num, int base_addr)
482{
Marek Vasut59051472020-03-15 15:36:09 +0100483 struct smc911x_priv *priv;
Marek Vasut25eb4682020-03-21 17:25:41 +0100484 int ret;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700485
Marek Vasut59051472020-03-15 15:36:09 +0100486 priv = calloc(1, sizeof(*priv));
487 if (!priv)
Marek Vasutcf613592020-03-15 15:14:18 +0100488 return -ENOMEM;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700489
Marek Vasut59051472020-03-15 15:36:09 +0100490 priv->iobase = base_addr;
491 priv->dev.iobase = base_addr;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700492
Andre Przywara9d09b0a2021-06-28 14:30:31 +0100493 priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
494
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200495 /* Try to detect chip. Will fail if not present. */
Marek Vasut59051472020-03-15 15:36:09 +0100496 ret = smc911x_detect_chip(priv);
Marek Vasut25eb4682020-03-21 17:25:41 +0100497 if (ret) {
498 ret = 0; /* Card not detected is not an error */
499 goto err_detect;
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200500 }
501
Andre Przywara240bfb12020-06-11 12:03:17 +0100502 if (smc911x_read_mac_address(priv))
Marek Vasut55399442020-03-15 17:39:01 +0100503 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700504
Marek Vasut59051472020-03-15 15:36:09 +0100505 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);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400510
Marek Vasut59051472020-03-15 15:36:09 +0100511 eth_register(&priv->dev);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000512
Marek Vasut59051472020-03-15 15:36:09 +0100513 ret = smc911x_initialize_mii(priv);
Marek Vasut25eb4682020-03-21 17:25:41 +0100514 if (ret)
515 goto err_mii;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000516
Mike Rapoport907975a2009-11-12 15:35:08 +0200517 return 1;
Marek Vasut25eb4682020-03-21 17:25:41 +0100518
519err_mii:
Marek Vasut59051472020-03-15 15:36:09 +0100520 eth_unregister(&priv->dev);
Marek Vasut25eb4682020-03-21 17:25:41 +0100521err_detect:
Marek Vasut59051472020-03-15 15:36:09 +0100522 free(priv);
Marek Vasut25eb4682020-03-21 17:25:41 +0100523 return ret;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400524}
Marek Vasut55399442020-03-15 17:39:01 +0100525
526#else /* ifdef CONFIG_DM_ETH */
527
528static int smc911x_start(struct udevice *dev)
529{
Simon Glassfa20e932020-12-03 16:55:20 -0700530 struct eth_pdata *plat = dev_get_plat(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100531 struct smc911x_priv *priv = dev_get_priv(dev);
532
533 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
534
535 return smc911x_init_common(priv);
536}
537
538static void smc911x_stop(struct udevice *dev)
539{
540 struct smc911x_priv *priv = dev_get_priv(dev);
541
542 smc911x_halt_common(priv);
543}
544
545static int smc911x_send(struct udevice *dev, void *packet, int length)
546{
547 struct smc911x_priv *priv = dev_get_priv(dev);
548 int ret;
549
550 ret = smc911x_send_common(priv, packet, length);
551
552 return ret ? 0 : -ETIMEDOUT;
553}
554
555static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
556{
557 struct smc911x_priv *priv = dev_get_priv(dev);
558 u32 *data = (u32 *)net_rx_packets[0];
559 int ret;
560
561 ret = smc911x_recv_common(priv, data);
562 if (ret)
563 *packetp = (void *)data;
564
565 return ret ? ret : -EAGAIN;
566}
567
Andre Przywara240bfb12020-06-11 12:03:17 +0100568static int smc911x_read_rom_hwaddr(struct udevice *dev)
569{
570 struct smc911x_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700571 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywara240bfb12020-06-11 12:03:17 +0100572
573 if (!smc911x_read_mac_address(priv))
574 return -ENODEV;
575
576 memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
577
578 return 0;
579}
580
Marek Vasut55399442020-03-15 17:39:01 +0100581static int smc911x_bind(struct udevice *dev)
582{
583 return device_set_name(dev, dev->name);
584}
585
586static int smc911x_probe(struct udevice *dev)
587{
588 struct smc911x_priv *priv = dev_get_priv(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100589 int ret;
590
591 /* Try to detect chip. Will fail if not present. */
592 ret = smc911x_detect_chip(priv);
593 if (ret)
594 return ret;
595
Andre Przywara240bfb12020-06-11 12:03:17 +0100596 smc911x_read_rom_hwaddr(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100597
598 return 0;
599}
600
Simon Glassaad29ae2020-12-03 16:55:21 -0700601static int smc911x_of_to_plat(struct udevice *dev)
Marek Vasut55399442020-03-15 17:39:01 +0100602{
603 struct smc911x_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700604 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywara9d09b0a2021-06-28 14:30:31 +0100605 u32 io_width;
606 int ret;
Marek Vasut55399442020-03-15 17:39:01 +0100607
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900608 pdata->iobase = dev_read_addr(dev);
Marek Vasut55399442020-03-15 17:39:01 +0100609 priv->iobase = pdata->iobase;
610
Andre Przywara9d09b0a2021-06-28 14:30:31 +0100611 ret = dev_read_u32(dev, "reg-io-width", &io_width);
612 if (!ret)
613 priv->use_32_bit_io = (io_width == 4);
614 else
615 priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
616
Marek Vasut55399442020-03-15 17:39:01 +0100617 return 0;
618}
619
620static const struct eth_ops smc911x_ops = {
621 .start = smc911x_start,
622 .send = smc911x_send,
623 .recv = smc911x_recv,
624 .stop = smc911x_stop,
Andre Przywara240bfb12020-06-11 12:03:17 +0100625 .read_rom_hwaddr = smc911x_read_rom_hwaddr,
Marek Vasut55399442020-03-15 17:39:01 +0100626};
627
628static const struct udevice_id smc911x_ids[] = {
629 { .compatible = "smsc,lan9115" },
630 { }
631};
632
633U_BOOT_DRIVER(smc911x) = {
634 .name = "eth_smc911x",
635 .id = UCLASS_ETH,
636 .of_match = smc911x_ids,
637 .bind = smc911x_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700638 .of_to_plat = smc911x_of_to_plat,
Marek Vasut55399442020-03-15 17:39:01 +0100639 .probe = smc911x_probe,
640 .ops = &smc911x_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700641 .priv_auto = sizeof(struct smc911x_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700642 .plat_auto = sizeof(struct eth_pdata),
Marek Vasut55399442020-03-15 17:39:01 +0100643 .flags = DM_FLAG_ALLOC_PRIV_DMA,
644};
645#endif