blob: 18a729cfbcab8af2352c1700460f8af201a763a6 [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
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000038#define mdelay(n) udelay((n)*1000)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040039
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070040static void smx911x_handle_mac_address(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040041{
42 unsigned long addrh, addrl;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070043 uchar *m = dev->enetaddr;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040044
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070045 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
46 addrh = m[4] | (m[5] << 8);
47 smc911x_set_mac_csr(dev, ADDRL, addrl);
48 smc911x_set_mac_csr(dev, ADDRH, addrh);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040049
Mike Frysinger719b6752009-02-11 19:09:54 -050050 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040051}
52
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070053static int smc911x_miiphy_read(struct eth_device *dev,
54 u8 phy, u8 reg, u16 *val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040055{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070056 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000057 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040058
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070059 smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
60 MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040061
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
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070065 *val = smc911x_get_mac_csr(dev, MII_DATA);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040066
67 return 0;
68}
69
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070070static int smc911x_miiphy_write(struct eth_device *dev,
71 u8 phy, u8 reg, u16 val)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040072{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070073 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000074 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040075
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070076 smc911x_set_mac_csr(dev, MII_DATA, val);
77 smc911x_set_mac_csr(dev, MII_ACC,
Sascha Hauerf43a42d2008-04-15 00:08:20 -040078 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
79
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070080 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000081 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040082 return 0;
83}
84
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070085static int smc911x_phy_reset(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040086{
87 u32 reg;
88
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070089 reg = smc911x_reg_read(dev, PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040090 reg &= ~0xfffff030;
91 reg |= PMT_CTRL_PHY_RST;
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070092 smc911x_reg_write(dev, PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040093
94 mdelay(100);
95
96 return 0;
97}
98
Ben Warrenfbfdd3a2009-07-20 22:01:11 -070099static void smc911x_phy_configure(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400100{
101 int timeout;
102 u16 status;
103
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700104 smc911x_phy_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400105
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700106 smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_RESET);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400107 mdelay(1);
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700108 smc911x_miiphy_write(dev, 1, PHY_ANAR, 0x01e1);
109 smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_AUTON |
110 PHY_BMCR_RST_NEG);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400111
112 timeout = 5000;
113 do {
114 mdelay(1);
115 if ((timeout--) == 0)
116 goto err_out;
117
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700118 if (smc911x_miiphy_read(dev, 1, PHY_BMSR, &status) != 0)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400119 goto err_out;
120 } while (!(status & PHY_BMSR_LS));
121
122 printf(DRIVERNAME ": phy initialized\n");
123
124 return;
125
126err_out:
127 printf(DRIVERNAME ": autonegotiation timed out\n");
128}
129
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700130static void smc911x_enable(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400131{
132 /* Enable TX */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700133 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400134
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700135 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400136
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700137 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400138
139 /* no padding to start of packets */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700140 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400141
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700142 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
143 MAC_CR_HBDIS);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400144
145}
146
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700147static int smc911x_init(struct eth_device *dev, bd_t * bd)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400148{
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400149 printf(DRIVERNAME ": initializing\n");
150
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700151 if (smc911x_detect_chip(dev))
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400152 goto err_out;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400153
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700154 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400155
156 /* Configure the PHY, initialize the link state */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700157 smc911x_phy_configure(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400158
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700159 smx911x_handle_mac_address(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400160
161 /* Turn on Tx + Rx */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700162 smc911x_enable(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400163
164 return 0;
165
166err_out:
167 return -1;
168}
169
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700170static int smc911x_send(struct eth_device *dev,
171 volatile void *packet, int length)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400172{
173 u32 *data = (u32*)packet;
174 u32 tmplen;
175 u32 status;
176
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700177 smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
178 TX_CMD_A_INT_LAST_SEG | length);
179 smc911x_reg_write(dev, TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400180
181 tmplen = (length + 3) / 4;
182
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000183 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700184 pkt_data_push(dev, TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400185
186 /* wait for transmission */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700187 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
188 TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400189
190 /* get status. Ignore 'no carrier' error, it has no meaning for
191 * full duplex operation
192 */
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700193 status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
194 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
195 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400196
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000197 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400198 return 0;
199
200 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
201 status & TX_STS_LOC ? "TX_STS_LOC " : "",
202 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
203 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
204 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
205 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
206
207 return -1;
208}
209
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700210static void smc911x_halt(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400211{
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700212 smc911x_reset(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400213}
214
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700215static int smc911x_rx(struct eth_device *dev)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400216{
217 u32 *data = (u32 *)NetRxPackets[0];
218 u32 pktlen, tmplen;
219 u32 status;
220
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700221 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
222 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400223 pktlen = (status & RX_STS_PKT_LEN) >> 16;
224
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700225 smc911x_reg_write(dev, RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400226
227 tmplen = (pktlen + 2+ 3) / 4;
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000228 while (tmplen--)
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700229 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400230
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000231 if (status & RX_STS_ES)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400232 printf(DRIVERNAME
233 ": dropped bad packet. Status: 0x%08x\n",
234 status);
235 else
236 NetReceive(NetRxPackets[0], pktlen);
237 }
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700238
239 return 0;
240}
241
242int smc911x_initialize(u8 dev_num, int base_addr)
243{
244 unsigned long addrl, addrh;
245 struct eth_device *dev;
246
247 dev = malloc(sizeof(*dev));
248 if (!dev) {
249 free(dev);
250 return 0;
251 }
252 memset(dev, 0, sizeof(*dev));
253
254 dev->iobase = base_addr;
255
256 addrh = smc911x_get_mac_csr(dev, ADDRH);
257 addrl = smc911x_get_mac_csr(dev, ADDRL);
258 dev->enetaddr[0] = addrl;
259 dev->enetaddr[1] = addrl >> 8;
260 dev->enetaddr[2] = addrl >> 16;
261 dev->enetaddr[3] = addrl >> 24;
262 dev->enetaddr[4] = addrh;
263 dev->enetaddr[5] = addrh >> 8;
264
265 dev->init = smc911x_init;
266 dev->halt = smc911x_halt;
267 dev->send = smc911x_send;
268 dev->recv = smc911x_rx;
269 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400270
Ben Warrenfbfdd3a2009-07-20 22:01:11 -0700271 eth_register(dev);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400272 return 0;
273}