blob: 9d2790e561678174b853d462bd139ac148f93caf [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];
31};
32
Marek Vasut689434e2020-03-15 15:03:07 +010033static 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" },
46 { 0, NULL },
47};
48
49#define DRIVERNAME "smc911x"
50
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"
55#endif
56
57#if defined (CONFIG_SMC911X_32_BIT)
Marek Vasut59051472020-03-15 15:36:09 +010058static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010059{
Marek Vasut59051472020-03-15 15:36:09 +010060 return readl(priv->iobase + offset);
Marek Vasut689434e2020-03-15 15:03:07 +010061}
Marek Vasut689434e2020-03-15 15:03:07 +010062
Marek Vasut59051472020-03-15 15:36:09 +010063static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010064{
Marek Vasut59051472020-03-15 15:36:09 +010065 writel(val, priv->iobase + offset);
Marek Vasut689434e2020-03-15 15:03:07 +010066}
Marek Vasut689434e2020-03-15 15:03:07 +010067#elif defined (CONFIG_SMC911X_16_BIT)
Marek Vasut59051472020-03-15 15:36:09 +010068static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010069{
Marek Vasut59051472020-03-15 15:36:09 +010070 return (readw(priv->iobase + offset) & 0xffff) |
71 (readw(priv->iobase + offset + 2) << 16);
Marek Vasut689434e2020-03-15 15:03:07 +010072}
Marek Vasut59051472020-03-15 15:36:09 +010073static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010074{
Marek Vasut59051472020-03-15 15:36:09 +010075 writew(val & 0xffff, priv->iobase + offset);
76 writew(val >> 16, priv->iobase + offset + 2);
Marek Vasut689434e2020-03-15 15:03:07 +010077}
78#else
79#error "SMC911X: undefined bus width"
80#endif /* CONFIG_SMC911X_16_BIT */
81
Marek Vasut59051472020-03-15 15:36:09 +010082static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
Marek Vasut689434e2020-03-15 15:03:07 +010083{
Marek Vasut59051472020-03-15 15:36:09 +010084 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010085 ;
Marek Vasut59051472020-03-15 15:36:09 +010086 smc911x_reg_write(priv, MAC_CSR_CMD,
Marek Vasut689434e2020-03-15 15:03:07 +010087 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
Marek Vasut59051472020-03-15 15:36:09 +010088 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010089 ;
90
Marek Vasut59051472020-03-15 15:36:09 +010091 return smc911x_reg_read(priv, MAC_CSR_DATA);
Marek Vasut689434e2020-03-15 15:03:07 +010092}
93
Marek Vasut59051472020-03-15 15:36:09 +010094static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
Marek Vasut689434e2020-03-15 15:03:07 +010095{
Marek Vasut59051472020-03-15 15:36:09 +010096 while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +010097 ;
Marek Vasut59051472020-03-15 15:36:09 +010098 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)
Marek Vasut689434e2020-03-15 15:03:07 +0100101 ;
102}
103
Marek Vasut59051472020-03-15 15:36:09 +0100104static int smc911x_detect_chip(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +0100105{
106 unsigned long val, i;
107
Marek Vasut59051472020-03-15 15:36:09 +0100108 val = smc911x_reg_read(priv, BYTE_TEST);
Marek Vasut689434e2020-03-15 15:03:07 +0100109 if (val == 0xffffffff) {
110 /* Special case -- no chip present */
111 return -1;
112 } else if (val != 0x87654321) {
113 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
114 return -1;
115 }
116
Marek Vasut59051472020-03-15 15:36:09 +0100117 val = smc911x_reg_read(priv, ID_REV) >> 16;
Marek Vasut689434e2020-03-15 15:03:07 +0100118 for (i = 0; chip_ids[i].id != 0; i++) {
119 if (chip_ids[i].id == val) break;
120 }
121 if (!chip_ids[i].id) {
122 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
123 return -1;
124 }
125
Marek Vasut59051472020-03-15 15:36:09 +0100126 priv->chipid = &chip_ids[i];
Marek Vasut689434e2020-03-15 15:03:07 +0100127
128 return 0;
129}
130
Marek Vasut59051472020-03-15 15:36:09 +0100131static void smc911x_reset(struct smc911x_priv *priv)
Marek Vasut689434e2020-03-15 15:03:07 +0100132{
133 int timeout;
134
135 /*
136 * Take out of PM setting first
137 * Device is already wake up if PMT_CTRL_READY bit is set
138 */
Marek Vasut59051472020-03-15 15:36:09 +0100139 if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
Marek Vasut689434e2020-03-15 15:03:07 +0100140 /* Write to the bytetest will take out of powerdown */
Marek Vasut59051472020-03-15 15:36:09 +0100141 smc911x_reg_write(priv, BYTE_TEST, 0x0);
Marek Vasut689434e2020-03-15 15:03:07 +0100142
143 timeout = 10;
144
145 while (timeout-- &&
Marek Vasut59051472020-03-15 15:36:09 +0100146 !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
Marek Vasut689434e2020-03-15 15:03:07 +0100147 udelay(10);
148 if (timeout < 0) {
149 printf(DRIVERNAME
150 ": timeout waiting for PM restore\n");
151 return;
152 }
153 }
154
155 /* Disable interrupts */
Marek Vasut59051472020-03-15 15:36:09 +0100156 smc911x_reg_write(priv, INT_EN, 0);
Marek Vasut689434e2020-03-15 15:03:07 +0100157
Marek Vasut59051472020-03-15 15:36:09 +0100158 smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
Marek Vasut689434e2020-03-15 15:03:07 +0100159
160 timeout = 1000;
Marek Vasut59051472020-03-15 15:36:09 +0100161 while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
Marek Vasut689434e2020-03-15 15:03:07 +0100162 udelay(10);
163
164 if (timeout < 0) {
165 printf(DRIVERNAME ": reset timeout\n");
166 return;
167 }
168
169 /* Reset the FIFO level and flow control settings */
Marek Vasut59051472020-03-15 15:36:09 +0100170 smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
171 smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
Marek Vasut689434e2020-03-15 15:03:07 +0100172
173 /* Set to LED outputs */
Marek Vasut59051472020-03-15 15:36:09 +0100174 smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
Marek Vasut689434e2020-03-15 15:03:07 +0100175}
176
Marek Vasut59051472020-03-15 15:36:09 +0100177static void smc911x_handle_mac_address(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400178{
179 unsigned long addrh, addrl;
Marek Vasut59051472020-03-15 15:36:09 +0100180 unsigned char *m = priv->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400181
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700182 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
183 addrh = m[4] | (m[5] << 8);
Marek Vasut59051472020-03-15 15:36:09 +0100184 smc911x_set_mac_csr(priv, ADDRL, addrl);
185 smc911x_set_mac_csr(priv, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400186
Mike Frysinger719b6752009-02-11 19:09:54 -0500187 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400188}
189
Marek Vasut59051472020-03-15 15:36:09 +0100190static int smc911x_eth_phy_read(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700191 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400192{
Marek Vasut59051472020-03-15 15:36:09 +0100193 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000194 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400195
Marek Vasut59051472020-03-15 15:36:09 +0100196 smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700197 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400198
Marek Vasut59051472020-03-15 15:36:09 +0100199 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000200 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400201
Marek Vasut59051472020-03-15 15:36:09 +0100202 *val = smc911x_get_mac_csr(priv, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400203
204 return 0;
205}
206
Marek Vasut59051472020-03-15 15:36:09 +0100207static int smc911x_eth_phy_write(struct smc911x_priv *priv,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700208 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400209{
Marek Vasut59051472020-03-15 15:36:09 +0100210 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000211 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400212
Marek Vasut59051472020-03-15 15:36:09 +0100213 smc911x_set_mac_csr(priv, MII_DATA, val);
214 smc911x_set_mac_csr(priv, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400215 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
216
Marek Vasut59051472020-03-15 15:36:09 +0100217 while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000218 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400219 return 0;
220}
221
Marek Vasut59051472020-03-15 15:36:09 +0100222static int smc911x_phy_reset(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400223{
224 u32 reg;
225
Marek Vasut59051472020-03-15 15:36:09 +0100226 reg = smc911x_reg_read(priv, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400227 reg &= ~0xfffff030;
228 reg |= PMT_CTRL_PHY_RST;
Marek Vasut59051472020-03-15 15:36:09 +0100229 smc911x_reg_write(priv, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400230
231 mdelay(100);
232
233 return 0;
234}
235
Marek Vasut59051472020-03-15 15:36:09 +0100236static void smc911x_phy_configure(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400237{
238 int timeout;
239 u16 status;
240
Marek Vasut59051472020-03-15 15:36:09 +0100241 smc911x_phy_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400242
Marek Vasut59051472020-03-15 15:36:09 +0100243 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400244 mdelay(1);
Marek Vasut59051472020-03-15 15:36:09 +0100245 smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
246 smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -0500247 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400248
249 timeout = 5000;
250 do {
251 mdelay(1);
252 if ((timeout--) == 0)
253 goto err_out;
254
Marek Vasut59051472020-03-15 15:36:09 +0100255 if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400256 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500257 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400258
259 printf(DRIVERNAME ": phy initialized\n");
260
261 return;
262
263err_out:
264 printf(DRIVERNAME ": autonegotiation timed out\n");
265}
266
Marek Vasut59051472020-03-15 15:36:09 +0100267static void smc911x_enable(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400268{
269 /* Enable TX */
Marek Vasut59051472020-03-15 15:36:09 +0100270 smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400271
Marek Vasut59051472020-03-15 15:36:09 +0100272 smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400273
Marek Vasut59051472020-03-15 15:36:09 +0100274 smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400275
276 /* no padding to start of packets */
Marek Vasut59051472020-03-15 15:36:09 +0100277 smc911x_reg_write(priv, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400278
Marek Vasut59051472020-03-15 15:36:09 +0100279 smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700280 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400281}
282
Marek Vasutcc830182020-03-15 17:25:27 +0100283static int smc911x_init_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400284{
Marek Vasut59051472020-03-15 15:36:09 +0100285 const struct chip_id *id = priv->chipid;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400286
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100287 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400288
Marek Vasut59051472020-03-15 15:36:09 +0100289 smc911x_reset(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400290
291 /* Configure the PHY, initialize the link state */
Marek Vasut59051472020-03-15 15:36:09 +0100292 smc911x_phy_configure(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400293
Marek Vasut59051472020-03-15 15:36:09 +0100294 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400295
296 /* Turn on Tx + Rx */
Marek Vasut59051472020-03-15 15:36:09 +0100297 smc911x_enable(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400298
299 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400300}
301
Marek Vasutcc830182020-03-15 17:25:27 +0100302static int smc911x_send_common(struct smc911x_priv *priv,
303 void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400304{
305 u32 *data = (u32*)packet;
306 u32 tmplen;
307 u32 status;
308
Marek Vasut59051472020-03-15 15:36:09 +0100309 smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700310 TX_CMD_A_INT_LAST_SEG | length);
Marek Vasut59051472020-03-15 15:36:09 +0100311 smc911x_reg_write(priv, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400312
313 tmplen = (length + 3) / 4;
314
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000315 while (tmplen--)
Marek Vasut59051472020-03-15 15:36:09 +0100316 smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400317
318 /* wait for transmission */
Marek Vasut59051472020-03-15 15:36:09 +0100319 while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700320 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400321
322 /* get status. Ignore 'no carrier' error, it has no meaning for
323 * full duplex operation
324 */
Marek Vasut59051472020-03-15 15:36:09 +0100325 status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700326 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
327 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400328
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000329 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400330 return 0;
331
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" : "");
338
339 return -1;
340}
341
Marek Vasutcc830182020-03-15 17:25:27 +0100342static void smc911x_halt_common(struct smc911x_priv *priv)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400343{
Marek Vasut59051472020-03-15 15:36:09 +0100344 smc911x_reset(priv);
345 smc911x_handle_mac_address(priv);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400346}
347
Marek Vasutcc830182020-03-15 17:25:27 +0100348static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400349{
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400350 u32 pktlen, tmplen;
351 u32 status;
352
Marek Vasutc70631f2020-03-15 17:02:05 +0100353 status = smc911x_reg_read(priv, RX_FIFO_INF);
354 if (!(status & RX_FIFO_INF_RXSUSED))
355 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400356
Marek Vasutc70631f2020-03-15 17:02:05 +0100357 status = smc911x_reg_read(priv, RX_STATUS_FIFO);
358 pktlen = (status & RX_STS_PKT_LEN) >> 16;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400359
Marek Vasutc70631f2020-03-15 17:02:05 +0100360 smc911x_reg_write(priv, RX_CFG, 0);
361
362 tmplen = (pktlen + 3) / 4;
363 while (tmplen--)
364 *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400365
Marek Vasutcc830182020-03-15 17:25:27 +0100366 if (status & RX_STS_ES) {
Marek Vasutc70631f2020-03-15 17:02:05 +0100367 printf(DRIVERNAME
368 ": dropped bad packet. Status: 0x%08x\n",
369 status);
Marek Vasutcc830182020-03-15 17:25:27 +0100370 return 0;
371 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700372
Marek Vasutcc830182020-03-15 17:25:27 +0100373 return pktlen;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700374}
375
Marek Vasut55399442020-03-15 17:39:01 +0100376#ifndef CONFIG_DM_ETH
377
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000378#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
379/* wrapper for smc911x_eth_phy_read */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500380static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
381 int reg)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000382{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500383 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Marek Vasut59051472020-03-15 15:36:09 +0100384 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
Marek Vasut14330e52020-03-15 15:43:20 +0100385 u16 val = 0;
386 int ret;
387
Marek Vasut59051472020-03-15 15:36:09 +0100388 if (!dev || !priv)
Marek Vasut14330e52020-03-15 15:43:20 +0100389 return -ENODEV;
390
Marek Vasut59051472020-03-15 15:36:09 +0100391 ret = smc911x_eth_phy_read(priv, phy, reg, &val);
Marek Vasut14330e52020-03-15 15:43:20 +0100392 if (ret < 0)
393 return ret;
394
395 return val;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000396}
Marek Vasut59051472020-03-15 15:36:09 +0100397
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000398/* wrapper for smc911x_eth_phy_write */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500399static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
400 int reg, u16 val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000401{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500402 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Marek Vasut59051472020-03-15 15:36:09 +0100403 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
Marek Vasut14330e52020-03-15 15:43:20 +0100404
Marek Vasut59051472020-03-15 15:36:09 +0100405 if (!dev || !priv)
Marek Vasut14330e52020-03-15 15:43:20 +0100406 return -ENODEV;
407
Marek Vasut59051472020-03-15 15:36:09 +0100408 return smc911x_eth_phy_write(priv, phy, reg, val);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000409}
Marek Vasut25eb4682020-03-21 17:25:41 +0100410
Marek Vasut59051472020-03-15 15:36:09 +0100411static int smc911x_initialize_mii(struct smc911x_priv *priv)
Marek Vasut25eb4682020-03-21 17:25:41 +0100412{
413 struct mii_dev *mdiodev = mdio_alloc();
414 int ret;
415
416 if (!mdiodev)
417 return -ENOMEM;
418
Marek Vasut59051472020-03-15 15:36:09 +0100419 strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
Marek Vasut25eb4682020-03-21 17:25:41 +0100420 mdiodev->read = smc911x_miiphy_read;
421 mdiodev->write = smc911x_miiphy_write;
422
423 ret = mdio_register(mdiodev);
424 if (ret < 0) {
425 mdio_free(mdiodev);
426 return ret;
427 }
428
429 return 0;
430}
431#else
Marek Vasut59051472020-03-15 15:36:09 +0100432static int smc911x_initialize_mii(struct smc911x_priv *priv)
Marek Vasut25eb4682020-03-21 17:25:41 +0100433{
434 return 0;
435}
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000436#endif
437
Marek Vasutcc830182020-03-15 17:25:27 +0100438static int smc911x_init(struct eth_device *dev, bd_t *bd)
439{
440 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
441
442 return smc911x_init_common(priv);
443}
444
445static void smc911x_halt(struct eth_device *dev)
446{
447 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
448
449 smc911x_halt_common(priv);
450}
451
452static int smc911x_send(struct eth_device *dev, void *packet, int length)
453{
454 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
455
456 return smc911x_send_common(priv, packet, length);
457}
458
459static int smc911x_recv(struct eth_device *dev)
460{
461 struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
462 u32 *data = (u32 *)net_rx_packets[0];
463 int ret;
464
465 ret = smc911x_recv_common(priv, data);
466 if (ret)
467 net_process_received_packet(net_rx_packets[0], ret);
468
469 return ret;
470}
471
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700472int smc911x_initialize(u8 dev_num, int base_addr)
473{
474 unsigned long addrl, addrh;
Marek Vasut59051472020-03-15 15:36:09 +0100475 struct smc911x_priv *priv;
Marek Vasut25eb4682020-03-21 17:25:41 +0100476 int ret;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700477
Marek Vasut59051472020-03-15 15:36:09 +0100478 priv = calloc(1, sizeof(*priv));
479 if (!priv)
Marek Vasutcf613592020-03-15 15:14:18 +0100480 return -ENOMEM;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700481
Marek Vasut59051472020-03-15 15:36:09 +0100482 priv->iobase = base_addr;
483 priv->dev.iobase = base_addr;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700484
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200485 /* Try to detect chip. Will fail if not present. */
Marek Vasut59051472020-03-15 15:36:09 +0100486 ret = smc911x_detect_chip(priv);
Marek Vasut25eb4682020-03-21 17:25:41 +0100487 if (ret) {
488 ret = 0; /* Card not detected is not an error */
489 goto err_detect;
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200490 }
491
Marek Vasut59051472020-03-15 15:36:09 +0100492 addrh = smc911x_get_mac_csr(priv, ADDRH);
493 addrl = smc911x_get_mac_csr(priv, ADDRL);
Seunghyeon Rheea8572f12009-11-02 00:00:00 -0800494 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
495 /* address is obtained from optional eeprom */
Marek Vasut59051472020-03-15 15:36:09 +0100496 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;
Marek Vasut55399442020-03-15 17:39:01 +0100502 memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
Seunghyeon Rheea8572f12009-11-02 00:00:00 -0800503 }
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{
530 struct eth_pdata *plat = dev_get_platdata(dev);
531 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
568static int smc911x_bind(struct udevice *dev)
569{
570 return device_set_name(dev, dev->name);
571}
572
573static int smc911x_probe(struct udevice *dev)
574{
575 struct smc911x_priv *priv = dev_get_priv(dev);
576 unsigned long addrh, addrl;
577 int ret;
578
579 /* Try to detect chip. Will fail if not present. */
580 ret = smc911x_detect_chip(priv);
581 if (ret)
582 return ret;
583
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;
594 }
595
596 return 0;
597}
598
599static int smc911x_ofdata_to_platdata(struct udevice *dev)
600{
601 struct smc911x_priv *priv = dev_get_priv(dev);
602 struct eth_pdata *pdata = dev_get_platdata(dev);
603
604 pdata->iobase = devfdt_get_addr(dev);
605 priv->iobase = pdata->iobase;
606
607 return 0;
608}
609
610static const struct eth_ops smc911x_ops = {
611 .start = smc911x_start,
612 .send = smc911x_send,
613 .recv = smc911x_recv,
614 .stop = smc911x_stop,
615};
616
617static const struct udevice_id smc911x_ids[] = {
618 { .compatible = "smsc,lan9115" },
619 { }
620};
621
622U_BOOT_DRIVER(smc911x) = {
623 .name = "eth_smc911x",
624 .id = UCLASS_ETH,
625 .of_match = smc911x_ids,
626 .bind = smc911x_bind,
627 .ofdata_to_platdata = smc911x_ofdata_to_platdata,
628 .probe = smc911x_probe,
629 .ops = &smc911x_ops,
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,
633};
634#endif