blob: b2aed7e14ad200f040ca147f66f4064a4c3adc10 [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 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040026#include <command.h>
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070027#include <malloc.h>
Sascha Hauerf43a42d2008-04-15 00:08:20 -040028#include <net.h>
29#include <miiphy.h>
30
Mike Frysingerad8e4f42009-02-23 10:29:47 -050031#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040032
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070033u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010034 __attribute__ ((weak, alias ("smc911x_reg_read")));
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070035void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010036 __attribute__ ((weak, alias ("smc911x_reg_write")));
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090037
Mike Rapoport0b3cc382009-11-11 10:03:09 +020038static void smc911x_handle_mac_address(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040039{
40 unsigned long addrh, addrl;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070041 uchar *m = dev->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040042
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070043 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
44 addrh = m[4] | (m[5] << 8);
45 smc911x_set_mac_csr(dev, ADDRL, addrl);
46 smc911x_set_mac_csr(dev, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040047
Mike Frysinger719b6752009-02-11 19:09:54 -050048 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040049}
50
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000051static int smc911x_eth_phy_read(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_ACC, phy << 11 | reg << 6 |
58 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040059
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070060 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000061 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040062
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070063 *val = smc911x_get_mac_csr(dev, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040064
65 return 0;
66}
67
Helmut Raigerbe8f9c02011-06-29 00:12:14 +000068static int smc911x_eth_phy_write(struct eth_device *dev,
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070069 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040070{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070071 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000072 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040073
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070074 smc911x_set_mac_csr(dev, MII_DATA, val);
75 smc911x_set_mac_csr(dev, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -040076 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
77
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070078 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000079 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040080 return 0;
81}
82
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070083static int smc911x_phy_reset(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040084{
85 u32 reg;
86
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070087 reg = smc911x_reg_read(dev, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040088 reg &= ~0xfffff030;
89 reg |= PMT_CTRL_PHY_RST;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070090 smc911x_reg_write(dev, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040091
92 mdelay(100);
93
94 return 0;
95}
96
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070097static void smc911x_phy_configure(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040098{
99 int timeout;
100 u16 status;
101
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700102 smc911x_phy_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400103
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000104 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400105 mdelay(1);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000106 smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
107 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
Mike Frysingerd63ee712010-12-23 15:40:12 -0500108 BMCR_ANRESTART);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400109
110 timeout = 5000;
111 do {
112 mdelay(1);
113 if ((timeout--) == 0)
114 goto err_out;
115
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000116 if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400117 goto err_out;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500118 } while (!(status & BMSR_LSTATUS));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400119
120 printf(DRIVERNAME ": phy initialized\n");
121
122 return;
123
124err_out:
125 printf(DRIVERNAME ": autonegotiation timed out\n");
126}
127
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700128static void smc911x_enable(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400129{
130 /* Enable TX */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700131 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400132
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700133 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400134
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700135 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400136
137 /* no padding to start of packets */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700138 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400139
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700140 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
141 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400142
143}
144
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700145static int smc911x_init(struct eth_device *dev, bd_t * bd)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400146{
Olof Johansson64aabc22009-09-29 10:21:29 -0400147 struct chip_id *id = dev->priv;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400148
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100149 printf(DRIVERNAME ": detected %s controller\n", id->name);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400150
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700151 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400152
153 /* Configure the PHY, initialize the link state */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700154 smc911x_phy_configure(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400155
Mike Rapoport0b3cc382009-11-11 10:03:09 +0200156 smc911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400157
158 /* Turn on Tx + Rx */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700159 smc911x_enable(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400160
161 return 0;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400162}
163
Anatolij Gustschin8c6f60a2012-05-20 12:22:55 +0000164static int smc911x_send(struct eth_device *dev, void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400165{
166 u32 *data = (u32*)packet;
167 u32 tmplen;
168 u32 status;
169
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700170 smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
171 TX_CMD_A_INT_LAST_SEG | length);
172 smc911x_reg_write(dev, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400173
174 tmplen = (length + 3) / 4;
175
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000176 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700177 pkt_data_push(dev, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400178
179 /* wait for transmission */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700180 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
181 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400182
183 /* get status. Ignore 'no carrier' error, it has no meaning for
184 * full duplex operation
185 */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700186 status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
187 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
188 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400189
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000190 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400191 return 0;
192
193 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
194 status & TX_STS_LOC ? "TX_STS_LOC " : "",
195 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
196 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
197 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
198 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
199
200 return -1;
201}
202
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700203static void smc911x_halt(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400204{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700205 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400206}
207
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700208static int smc911x_rx(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400209{
210 u32 *data = (u32 *)NetRxPackets[0];
211 u32 pktlen, tmplen;
212 u32 status;
213
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700214 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
215 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400216 pktlen = (status & RX_STS_PKT_LEN) >> 16;
217
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700218 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400219
Valentin Yakovenkov8cb08012010-04-23 09:40:23 +0400220 tmplen = (pktlen + 3) / 4;
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000221 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700222 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400223
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000224 if (status & RX_STS_ES)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400225 printf(DRIVERNAME
226 ": dropped bad packet. Status: 0x%08x\n",
227 status);
228 else
229 NetReceive(NetRxPackets[0], pktlen);
230 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700231
232 return 0;
233}
234
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000235#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
236/* wrapper for smc911x_eth_phy_read */
Wolfgang Denkd1f10ba2011-09-08 01:04:27 +0000237static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000238{
239 struct eth_device *dev = eth_get_dev_by_name(devname);
240 if (dev)
241 return smc911x_eth_phy_read(dev, phy, reg, val);
242 return -1;
243}
244/* wrapper for smc911x_eth_phy_write */
Wolfgang Denkd1f10ba2011-09-08 01:04:27 +0000245static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val)
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000246{
247 struct eth_device *dev = eth_get_dev_by_name(devname);
248 if (dev)
249 return smc911x_eth_phy_write(dev, phy, reg, val);
250 return -1;
251}
252#endif
253
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700254int smc911x_initialize(u8 dev_num, int base_addr)
255{
256 unsigned long addrl, addrh;
257 struct eth_device *dev;
258
259 dev = malloc(sizeof(*dev));
260 if (!dev) {
Mike Rapoport907975a2009-11-12 15:35:08 +0200261 return -1;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700262 }
263 memset(dev, 0, sizeof(*dev));
264
265 dev->iobase = base_addr;
266
Steve Sakomanb3ec49d2009-10-20 18:21:18 +0200267 /* Try to detect chip. Will fail if not present. */
268 if (smc911x_detect_chip(dev)) {
269 free(dev);
270 return 0;
271 }
272
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700273 addrh = smc911x_get_mac_csr(dev, ADDRH);
274 addrl = smc911x_get_mac_csr(dev, ADDRL);
Seunghyeon Rheea8572f12009-11-02 00:00:00 -0800275 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
276 /* address is obtained from optional eeprom */
277 dev->enetaddr[0] = addrl;
278 dev->enetaddr[1] = addrl >> 8;
279 dev->enetaddr[2] = addrl >> 16;
280 dev->enetaddr[3] = addrl >> 24;
281 dev->enetaddr[4] = addrh;
282 dev->enetaddr[5] = addrh >> 8;
283 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700284
285 dev->init = smc911x_init;
286 dev->halt = smc911x_halt;
287 dev->send = smc911x_send;
288 dev->recv = smc911x_rx;
289 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400290
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700291 eth_register(dev);
Helmut Raigerbe8f9c02011-06-29 00:12:14 +0000292
293#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
294 miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write);
295#endif
296
Mike Rapoport907975a2009-11-12 15:35:08 +0200297 return 1;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400298}