blob: feae8c09cf168117db9a9ab6d4ddbb95f147b691 [file] [log] [blame]
Sascha Hauerf43a42d2008-04-15 00:08:20 -04001/*
2 * SMSC LAN9[12]1[567] Network driver
3 *
Stelian Pop50497522008-05-08 22:52:09 +02004 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauerf43a42d2008-04-15 00:08:20 -04005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Sascha Hauerf43a42d2008-04-15 00:08:20 -04007 */
8
9#include <common.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040010#include <command.h>
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070011#include <malloc.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040012#include <net.h>
13#include <miiphy.h>
14
Mike Frysingerad8e4f42009-02-23 10:29:47 -050015#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040016
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070017u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010018 __attribute__ ((weak, alias ("smc911x_reg_read")));
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070019void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010020 __attribute__ ((weak, alias ("smc911x_reg_write")));
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090021
Mike Rapoport0b3cc382009-11-11 10:03:09 +020022static void smc911x_handle_mac_address(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040023{
24 unsigned long addrh, addrl;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070025 uchar *m = dev->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040026
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070027 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
28 addrh = m[4] | (m[5] << 8);
29 smc911x_set_mac_csr(dev, ADDRL, addrl);
30 smc911x_set_mac_csr(dev, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040031
Mike Frysinger719b6752009-02-11 19:09:54 -050032 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040033}
34
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000035static int smc911x_eth_phy_read(struct eth_device *dev,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070036 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040037{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070038 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000039 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040040
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070041 smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
42 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040043
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070044 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000045 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040046
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070047 *val = smc911x_get_mac_csr(dev, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040048
49 return 0;
50}
51
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000052static int smc911x_eth_phy_write(struct eth_device *dev,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070053 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040054{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070055 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000056 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040057
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070058 smc911x_set_mac_csr(dev, MII_DATA, val);
59 smc911x_set_mac_csr(dev, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -040060 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
61
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070062 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000063 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040064 return 0;
65}
66
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070067static int smc911x_phy_reset(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040068{
69 u32 reg;
70
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070071 reg = smc911x_reg_read(dev, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040072 reg &= ~0xfffff030;
73 reg |= PMT_CTRL_PHY_RST;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070074 smc911x_reg_write(dev, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040075
76 mdelay(100);
77
78 return 0;
79}
80
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070081static void smc911x_phy_configure(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040082{
83 int timeout;
84 u16 status;
85
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070086 smc911x_phy_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040087
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000088 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040089 mdelay(1);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000090 smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
91 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -050092 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040093
94 timeout = 5000;
95 do {
96 mdelay(1);
97 if ((timeout--) == 0)
98 goto err_out;
99
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000100 if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400101 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500102 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400103
104 printf(DRIVERNAME ": phy initialized\n");
105
106 return;
107
108err_out:
109 printf(DRIVERNAME ": autonegotiation timed out\n");
110}
111
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700112static void smc911x_enable(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400113{
114 /* Enable TX */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700115 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400116
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700117 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400118
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700119 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400120
121 /* no padding to start of packets */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700122 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400123
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700124 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
125 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400126
127}
128
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700129static int smc911x_init(struct eth_device *dev, bd_t * bd)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400130{
Olof Johansson64aabc22009-09-29 10:21:29 -0400131 struct chip_id *id = dev->priv;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400132
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100133 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400134
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700135 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400136
137 /* Configure the PHY, initialize the link state */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700138 smc911x_phy_configure(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400139
Mike Rapoport0b3cc382009-11-11 10:03:09 +0200140 smc911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400141
142 /* Turn on Tx + Rx */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700143 smc911x_enable(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400144
145 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400146}
147
Anatolij Gustschin8c6f60a2012-05-20 12:22:55 +0000148static int smc911x_send(struct eth_device *dev, void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400149{
150 u32 *data = (u32*)packet;
151 u32 tmplen;
152 u32 status;
153
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700154 smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
155 TX_CMD_A_INT_LAST_SEG | length);
156 smc911x_reg_write(dev, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400157
158 tmplen = (length + 3) / 4;
159
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000160 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700161 pkt_data_push(dev, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400162
163 /* wait for transmission */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700164 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
165 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400166
167 /* get status. Ignore 'no carrier' error, it has no meaning for
168 * full duplex operation
169 */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700170 status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
171 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
172 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400173
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000174 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400175 return 0;
176
177 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
178 status & TX_STS_LOC ? "TX_STS_LOC " : "",
179 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
180 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
181 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
182 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
183
184 return -1;
185}
186
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700187static void smc911x_halt(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400188{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700189 smc911x_reset(dev);
Marek Vasutc5794ed2014-10-11 18:42:58 +0200190 smc911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400191}
192
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700193static int smc911x_rx(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400194{
Joe Hershberger9f09a362015-04-08 01:41:06 -0500195 u32 *data = (u32 *)net_rx_packets[0];
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400196 u32 pktlen, tmplen;
197 u32 status;
198
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700199 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
200 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400201 pktlen = (status & RX_STS_PKT_LEN) >> 16;
202
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700203 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400204
Valentin Yakovenkov8cb08012010-04-23 09:40:23 +0400205 tmplen = (pktlen + 3) / 4;
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000206 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700207 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400208
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000209 if (status & RX_STS_ES)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400210 printf(DRIVERNAME
211 ": dropped bad packet. Status: 0x%08x\n",
212 status);
213 else
Joe Hershberger9f09a362015-04-08 01:41:06 -0500214 net_process_received_packet(net_rx_packets[0], pktlen);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400215 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700216
217 return 0;
218}
219
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000220#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
221/* wrapper for smc911x_eth_phy_read */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500222static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
223 int reg)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000224{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500225 u16 val = 0;
226 struct eth_device *dev = eth_get_dev_by_name(bus->name);
227 if (dev) {
228 int retval = smc911x_eth_phy_read(dev, phy, reg, &val);
229 if (retval < 0)
230 return retval;
231 return val;
232 }
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500233 return -ENODEV;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000234}
235/* wrapper for smc911x_eth_phy_write */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500236static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
237 int reg, u16 val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000238{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500239 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000240 if (dev)
241 return smc911x_eth_phy_write(dev, phy, reg, val);
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500242 return -ENODEV;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000243}
244#endif
245
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700246int smc911x_initialize(u8 dev_num, int base_addr)
247{
248 unsigned long addrl, addrh;
249 struct eth_device *dev;
250
251 dev = malloc(sizeof(*dev));
252 if (!dev) {
Mike Rapoport907975a2009-11-12 15:35:08 +0200253 return -1;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700254 }
255 memset(dev, 0, sizeof(*dev));
256
257 dev->iobase = base_addr;
258
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200259 /* Try to detect chip. Will fail if not present. */
260 if (smc911x_detect_chip(dev)) {
261 free(dev);
262 return 0;
263 }
264
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700265 addrh = smc911x_get_mac_csr(dev, ADDRH);
266 addrl = smc911x_get_mac_csr(dev, ADDRL);
Seunghyeon Rheea8572f12009-11-02 00:00:00 -0800267 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
268 /* address is obtained from optional eeprom */
269 dev->enetaddr[0] = addrl;
270 dev->enetaddr[1] = addrl >> 8;
271 dev->enetaddr[2] = addrl >> 16;
272 dev->enetaddr[3] = addrl >> 24;
273 dev->enetaddr[4] = addrh;
274 dev->enetaddr[5] = addrh >> 8;
275 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700276
277 dev->init = smc911x_init;
278 dev->halt = smc911x_halt;
279 dev->send = smc911x_send;
280 dev->recv = smc911x_rx;
281 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400282
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700283 eth_register(dev);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000284
285#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500286 int retval;
287 struct mii_dev *mdiodev = mdio_alloc();
288 if (!mdiodev)
289 return -ENOMEM;
290 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
291 mdiodev->read = smc911x_miiphy_read;
292 mdiodev->write = smc911x_miiphy_write;
293
294 retval = mdio_register(mdiodev);
295 if (retval < 0)
296 return retval;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000297#endif
298
Mike Rapoport907975a2009-11-12 15:35:08 +0200299 return 1;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400300}