blob: 257b0385c2a927dcde1465d7dad5476473086bc3 [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>
13
Mike Frysingerad8e4f42009-02-23 10:29:47 -050014#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040015
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070016u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010017 __attribute__ ((weak, alias ("smc911x_reg_read")));
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070018void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010019 __attribute__ ((weak, alias ("smc911x_reg_write")));
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090020
Mike Rapoport0b3cc382009-11-11 10:03:09 +020021static void smc911x_handle_mac_address(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040022{
23 unsigned long addrh, addrl;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070024 uchar *m = dev->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040025
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070026 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
27 addrh = m[4] | (m[5] << 8);
28 smc911x_set_mac_csr(dev, ADDRL, addrl);
29 smc911x_set_mac_csr(dev, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040030
Mike Frysinger719b6752009-02-11 19:09:54 -050031 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040032}
33
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000034static int smc911x_eth_phy_read(struct eth_device *dev,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070035 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040036{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070037 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000038 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040039
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070040 smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
41 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040042
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070043 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000044 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040045
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070046 *val = smc911x_get_mac_csr(dev, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040047
48 return 0;
49}
50
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000051static int smc911x_eth_phy_write(struct eth_device *dev,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070052 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040053{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070054 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000055 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040056
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070057 smc911x_set_mac_csr(dev, MII_DATA, val);
58 smc911x_set_mac_csr(dev, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -040059 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
60
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070061 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000062 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040063 return 0;
64}
65
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070066static int smc911x_phy_reset(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040067{
68 u32 reg;
69
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070070 reg = smc911x_reg_read(dev, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040071 reg &= ~0xfffff030;
72 reg |= PMT_CTRL_PHY_RST;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070073 smc911x_reg_write(dev, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040074
75 mdelay(100);
76
77 return 0;
78}
79
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070080static void smc911x_phy_configure(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040081{
82 int timeout;
83 u16 status;
84
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070085 smc911x_phy_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040086
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000087 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040088 mdelay(1);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000089 smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
90 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -050091 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040092
93 timeout = 5000;
94 do {
95 mdelay(1);
96 if ((timeout--) == 0)
97 goto err_out;
98
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000099 if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400100 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500101 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400102
103 printf(DRIVERNAME ": phy initialized\n");
104
105 return;
106
107err_out:
108 printf(DRIVERNAME ": autonegotiation timed out\n");
109}
110
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700111static void smc911x_enable(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400112{
113 /* Enable TX */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700114 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400115
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700116 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400117
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700118 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400119
120 /* no padding to start of packets */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700121 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400122
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700123 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
124 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400125
126}
127
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700128static int smc911x_init(struct eth_device *dev, bd_t * bd)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400129{
Olof Johansson64aabc22009-09-29 10:21:29 -0400130 struct chip_id *id = dev->priv;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400131
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100132 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400133
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700134 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400135
136 /* Configure the PHY, initialize the link state */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700137 smc911x_phy_configure(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400138
Mike Rapoport0b3cc382009-11-11 10:03:09 +0200139 smc911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400140
141 /* Turn on Tx + Rx */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700142 smc911x_enable(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400143
144 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400145}
146
Anatolij Gustschin8c6f60a2012-05-20 12:22:55 +0000147static int smc911x_send(struct eth_device *dev, void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400148{
149 u32 *data = (u32*)packet;
150 u32 tmplen;
151 u32 status;
152
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700153 smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
154 TX_CMD_A_INT_LAST_SEG | length);
155 smc911x_reg_write(dev, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400156
157 tmplen = (length + 3) / 4;
158
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000159 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700160 pkt_data_push(dev, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400161
162 /* wait for transmission */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700163 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
164 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400165
166 /* get status. Ignore 'no carrier' error, it has no meaning for
167 * full duplex operation
168 */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700169 status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
170 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
171 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400172
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000173 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400174 return 0;
175
176 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
177 status & TX_STS_LOC ? "TX_STS_LOC " : "",
178 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
179 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
180 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
181 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
182
183 return -1;
184}
185
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700186static void smc911x_halt(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400187{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700188 smc911x_reset(dev);
Marek Vasutc5794ed2014-10-11 18:42:58 +0200189 smc911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400190}
191
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700192static int smc911x_rx(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400193{
Joe Hershberger9f09a362015-04-08 01:41:06 -0500194 u32 *data = (u32 *)net_rx_packets[0];
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400195 u32 pktlen, tmplen;
196 u32 status;
197
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700198 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
199 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400200 pktlen = (status & RX_STS_PKT_LEN) >> 16;
201
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700202 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400203
Valentin Yakovenkov8cb08012010-04-23 09:40:23 +0400204 tmplen = (pktlen + 3) / 4;
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000205 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700206 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400207
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000208 if (status & RX_STS_ES)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400209 printf(DRIVERNAME
210 ": dropped bad packet. Status: 0x%08x\n",
211 status);
212 else
Joe Hershberger9f09a362015-04-08 01:41:06 -0500213 net_process_received_packet(net_rx_packets[0], pktlen);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400214 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700215
216 return 0;
217}
218
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000219#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
220/* wrapper for smc911x_eth_phy_read */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500221static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
222 int reg)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000223{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500224 u16 val = 0;
225 struct eth_device *dev = eth_get_dev_by_name(bus->name);
226 if (dev) {
227 int retval = smc911x_eth_phy_read(dev, phy, reg, &val);
228 if (retval < 0)
229 return retval;
230 return val;
231 }
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500232 return -ENODEV;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000233}
234/* wrapper for smc911x_eth_phy_write */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500235static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
236 int reg, u16 val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000237{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500238 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000239 if (dev)
240 return smc911x_eth_phy_write(dev, phy, reg, val);
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500241 return -ENODEV;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000242}
243#endif
244
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700245int smc911x_initialize(u8 dev_num, int base_addr)
246{
247 unsigned long addrl, addrh;
248 struct eth_device *dev;
249
250 dev = malloc(sizeof(*dev));
251 if (!dev) {
Mike Rapoport907975a2009-11-12 15:35:08 +0200252 return -1;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700253 }
254 memset(dev, 0, sizeof(*dev));
255
256 dev->iobase = base_addr;
257
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200258 /* Try to detect chip. Will fail if not present. */
259 if (smc911x_detect_chip(dev)) {
260 free(dev);
261 return 0;
262 }
263
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700264 addrh = smc911x_get_mac_csr(dev, ADDRH);
265 addrl = smc911x_get_mac_csr(dev, ADDRL);
Seunghyeon Rheea8572f12009-11-02 00:00:00 -0800266 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
267 /* address is obtained from optional eeprom */
268 dev->enetaddr[0] = addrl;
269 dev->enetaddr[1] = addrl >> 8;
270 dev->enetaddr[2] = addrl >> 16;
271 dev->enetaddr[3] = addrl >> 24;
272 dev->enetaddr[4] = addrh;
273 dev->enetaddr[5] = addrh >> 8;
274 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700275
276 dev->init = smc911x_init;
277 dev->halt = smc911x_halt;
278 dev->send = smc911x_send;
279 dev->recv = smc911x_rx;
280 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400281
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700282 eth_register(dev);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000283
284#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500285 int retval;
286 struct mii_dev *mdiodev = mdio_alloc();
287 if (!mdiodev)
288 return -ENOMEM;
289 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
290 mdiodev->read = smc911x_miiphy_read;
291 mdiodev->write = smc911x_miiphy_write;
292
293 retval = mdio_register(mdiodev);
294 if (retval < 0)
295 return retval;
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000296#endif
297
Mike Rapoport907975a2009-11-12 15:35:08 +0200298 return 1;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400299}