blob: 1ded8f01d0cba308647d492284c1124b162d6881 [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>
27#include <net.h>
28#include <miiphy.h>
29
Mike Frysingerad8e4f42009-02-23 10:29:47 -050030#include "smc911x.h"
Sascha Hauerf43a42d2008-04-15 00:08:20 -040031
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090032u32 pkt_data_pull(u32 addr) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010033 __attribute__ ((weak, alias ("smc911x_reg_read")));
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090034void pkt_data_push(u32 addr, u32 val) \
Stefan Roese62f3ed62008-11-12 13:31:02 +010035 __attribute__ ((weak, alias ("smc911x_reg_write")));
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +090036
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000037#define mdelay(n) udelay((n)*1000)
Sascha Hauerf43a42d2008-04-15 00:08:20 -040038
Sascha Hauerf43a42d2008-04-15 00:08:20 -040039static int smx911x_handle_mac_address(bd_t *bd)
40{
41 unsigned long addrh, addrl;
42 unsigned char *m = bd->bi_enetaddr;
43
44 /* if the environment has a valid mac address then use it */
45 if ((m[0] | m[1] | m[2] | m[3] | m[4] | m[5])) {
46 addrl = m[0] | m[1] << 8 | m[2] << 16 | m[3] << 24;
47 addrh = m[4] | m[5] << 8;
48 smc911x_set_mac_csr(ADDRH, addrh);
49 smc911x_set_mac_csr(ADDRL, addrl);
50 } else {
51 /* if not, try to get one from the eeprom */
52 addrh = smc911x_get_mac_csr(ADDRH);
53 addrl = smc911x_get_mac_csr(ADDRL);
54
55 m[0] = (addrl ) & 0xff;
56 m[1] = (addrl >> 8 ) & 0xff;
57 m[2] = (addrl >> 16 ) & 0xff;
58 m[3] = (addrl >> 24 ) & 0xff;
59 m[4] = (addrh ) & 0xff;
60 m[5] = (addrh >> 8 ) & 0xff;
61
62 /* we get 0xff when there is no eeprom connected */
63 if ((m[0] & m[1] & m[2] & m[3] & m[4] & m[5]) == 0xff) {
64 printf(DRIVERNAME ": no valid mac address in environment "
65 "and no eeprom found\n");
66 return -1;
67 }
68 }
69
70 printf(DRIVERNAME ": MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
71 m[0], m[1], m[2], m[3], m[4], m[5]);
72
73 return 0;
74}
75
76static int smc911x_miiphy_read(u8 phy, u8 reg, u16 *val)
77{
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000078 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
79 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040080
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000081 smc911x_set_mac_csr(MII_ACC, phy << 11 | reg << 6 | MII_ACC_MII_BUSY);
Sascha Hauerf43a42d2008-04-15 00:08:20 -040082
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000083 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
84 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040085
86 *val = smc911x_get_mac_csr(MII_DATA);
87
88 return 0;
89}
90
91static int smc911x_miiphy_write(u8 phy, u8 reg, u16 val)
92{
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +000093 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
94 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -040095
96 smc911x_set_mac_csr(MII_DATA, val);
97 smc911x_set_mac_csr(MII_ACC,
98 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
99
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000100 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
101 ;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400102 return 0;
103}
104
105static int smc911x_phy_reset(void)
106{
107 u32 reg;
108
Stefan Roese62f3ed62008-11-12 13:31:02 +0100109 reg = smc911x_reg_read(PMT_CTRL);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400110 reg &= ~0xfffff030;
111 reg |= PMT_CTRL_PHY_RST;
Stefan Roese62f3ed62008-11-12 13:31:02 +0100112 smc911x_reg_write(PMT_CTRL, reg);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400113
114 mdelay(100);
115
116 return 0;
117}
118
119static void smc911x_phy_configure(void)
120{
121 int timeout;
122 u16 status;
123
124 smc911x_phy_reset();
125
126 smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_RESET);
127 mdelay(1);
128 smc911x_miiphy_write(1, PHY_ANAR, 0x01e1);
129 smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
130
131 timeout = 5000;
132 do {
133 mdelay(1);
134 if ((timeout--) == 0)
135 goto err_out;
136
137 if (smc911x_miiphy_read(1, PHY_BMSR, &status) != 0)
138 goto err_out;
139 } while (!(status & PHY_BMSR_LS));
140
141 printf(DRIVERNAME ": phy initialized\n");
142
143 return;
144
145err_out:
146 printf(DRIVERNAME ": autonegotiation timed out\n");
147}
148
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400149static void smc911x_enable(void)
150{
151 /* Enable TX */
Stefan Roese62f3ed62008-11-12 13:31:02 +0100152 smc911x_reg_write(HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400153
Stefan Roese62f3ed62008-11-12 13:31:02 +0100154 smc911x_reg_write(GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400155
Stefan Roese62f3ed62008-11-12 13:31:02 +0100156 smc911x_reg_write(TX_CFG, TX_CFG_TX_ON);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400157
158 /* no padding to start of packets */
Stefan Roese62f3ed62008-11-12 13:31:02 +0100159 smc911x_reg_write(RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400160
161 smc911x_set_mac_csr(MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | MAC_CR_HBDIS);
162
163}
164
165int eth_init(bd_t *bd)
166{
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400167 printf(DRIVERNAME ": initializing\n");
168
Mike Frysingerad8e4f42009-02-23 10:29:47 -0500169 if (smc911x_detect_chip())
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400170 goto err_out;
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400171
172 smc911x_reset();
173
174 /* Configure the PHY, initialize the link state */
175 smc911x_phy_configure();
176
177 if (smx911x_handle_mac_address(bd))
178 goto err_out;
179
180 /* Turn on Tx + Rx */
181 smc911x_enable();
182
183 return 0;
184
185err_out:
186 return -1;
187}
188
189int eth_send(volatile void *packet, int length)
190{
191 u32 *data = (u32*)packet;
192 u32 tmplen;
193 u32 status;
194
Stefan Roese62f3ed62008-11-12 13:31:02 +0100195 smc911x_reg_write(TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | TX_CMD_A_INT_LAST_SEG | length);
196 smc911x_reg_write(TX_DATA_FIFO, length);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400197
198 tmplen = (length + 3) / 4;
199
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000200 while (tmplen--)
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +0900201 pkt_data_push(TX_DATA_FIFO, *data++);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400202
203 /* wait for transmission */
Stefan Roese62f3ed62008-11-12 13:31:02 +0100204 while (!((smc911x_reg_read(TX_FIFO_INF) & TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400205
206 /* get status. Ignore 'no carrier' error, it has no meaning for
207 * full duplex operation
208 */
Stefan Roese62f3ed62008-11-12 13:31:02 +0100209 status = smc911x_reg_read(TX_STATUS_FIFO) & (TX_STS_LOC | TX_STS_LATE_COLL |
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400210 TX_STS_MANY_COLL | TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
211
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000212 if (!status)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400213 return 0;
214
215 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
216 status & TX_STS_LOC ? "TX_STS_LOC " : "",
217 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
218 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
219 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
220 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
221
222 return -1;
223}
224
225void eth_halt(void)
226{
227 smc911x_reset();
228}
229
230int eth_rx(void)
231{
232 u32 *data = (u32 *)NetRxPackets[0];
233 u32 pktlen, tmplen;
234 u32 status;
235
Stefan Roese62f3ed62008-11-12 13:31:02 +0100236 if ((smc911x_reg_read(RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
237 status = smc911x_reg_read(RX_STATUS_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400238 pktlen = (status & RX_STS_PKT_LEN) >> 16;
239
Stefan Roese62f3ed62008-11-12 13:31:02 +0100240 smc911x_reg_write(RX_CFG, 0);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400241
242 tmplen = (pktlen + 2+ 3) / 4;
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000243 while (tmplen--)
Nobuhiro Iwamatsu0af6a432008-08-28 13:40:44 +0900244 *data++ = pkt_data_pull(RX_DATA_FIFO);
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400245
Guennadi Liakhovetski588bd2b2008-04-29 12:35:08 +0000246 if (status & RX_STS_ES)
Sascha Hauerf43a42d2008-04-15 00:08:20 -0400247 printf(DRIVERNAME
248 ": dropped bad packet. Status: 0x%08x\n",
249 status);
250 else
251 NetReceive(NetRxPackets[0], pktlen);
252 }
253
254 return 0;
255}