blob: 30f2dc266bfef71d9534d41d0ae76c7ae9e1f839 [file] [log] [blame]
Sascha Hauerde1b6862008-04-15 00:08:20 -04001/*
2 * SMSC LAN9[12]1[567] Network driver
3 *
Stelian Popcce9cfd2008-05-08 22:52:09 +02004 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauerde1b6862008-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 Hauerde1b6862008-04-15 00:08:20 -040026#include <command.h>
27#include <net.h>
28#include <miiphy.h>
29
Mike Frysinger75ba6d62009-02-23 10:29:47 -050030#include "smc911x.h"
Sascha Hauerde1b6862008-04-15 00:08:20 -040031
Nobuhiro Iwamatsu33314472008-08-28 13:40:44 +090032u32 pkt_data_pull(u32 addr) \
Stefan Roese890a02e2008-11-12 13:31:02 +010033 __attribute__ ((weak, alias ("smc911x_reg_read")));
Nobuhiro Iwamatsu33314472008-08-28 13:40:44 +090034void pkt_data_push(u32 addr, u32 val) \
Stefan Roese890a02e2008-11-12 13:31:02 +010035 __attribute__ ((weak, alias ("smc911x_reg_write")));
Nobuhiro Iwamatsu33314472008-08-28 13:40:44 +090036
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000037#define mdelay(n) udelay((n)*1000)
Sascha Hauerde1b6862008-04-15 00:08:20 -040038
Sascha Hauerde1b6862008-04-15 00:08:20 -040039static int smx911x_handle_mac_address(bd_t *bd)
40{
41 unsigned long addrh, addrl;
Mike Frysinger03f3d8d2009-02-11 19:09:54 -050042 uchar m[6];
Sascha Hauerde1b6862008-04-15 00:08:20 -040043
44 /* if the environment has a valid mac address then use it */
Mike Frysinger03f3d8d2009-02-11 19:09:54 -050045 if (!eth_getenv_enetaddr("ethaddr", m)) {
Sascha Hauerde1b6862008-04-15 00:08:20 -040046 /* if not, try to get one from the eeprom */
47 addrh = smc911x_get_mac_csr(ADDRH);
48 addrl = smc911x_get_mac_csr(ADDRL);
49
50 m[0] = (addrl ) & 0xff;
51 m[1] = (addrl >> 8 ) & 0xff;
52 m[2] = (addrl >> 16 ) & 0xff;
53 m[3] = (addrl >> 24 ) & 0xff;
54 m[4] = (addrh ) & 0xff;
55 m[5] = (addrh >> 8 ) & 0xff;
56
57 /* we get 0xff when there is no eeprom connected */
58 if ((m[0] & m[1] & m[2] & m[3] & m[4] & m[5]) == 0xff) {
59 printf(DRIVERNAME ": no valid mac address in environment "
60 "and no eeprom found\n");
61 return -1;
62 }
Mike Frysinger03f3d8d2009-02-11 19:09:54 -050063
64 eth_setenv_enetaddr("ethaddr", m);
Sascha Hauerde1b6862008-04-15 00:08:20 -040065 }
66
Mike Frysinger03f3d8d2009-02-11 19:09:54 -050067 printf(DRIVERNAME ": MAC %pM\n", m);
Sascha Hauerde1b6862008-04-15 00:08:20 -040068
69 return 0;
70}
71
72static int smc911x_miiphy_read(u8 phy, u8 reg, u16 *val)
73{
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000074 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
75 ;
Sascha Hauerde1b6862008-04-15 00:08:20 -040076
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000077 smc911x_set_mac_csr(MII_ACC, phy << 11 | reg << 6 | MII_ACC_MII_BUSY);
Sascha Hauerde1b6862008-04-15 00:08:20 -040078
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000079 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
80 ;
Sascha Hauerde1b6862008-04-15 00:08:20 -040081
82 *val = smc911x_get_mac_csr(MII_DATA);
83
84 return 0;
85}
86
87static int smc911x_miiphy_write(u8 phy, u8 reg, u16 val)
88{
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000089 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
90 ;
Sascha Hauerde1b6862008-04-15 00:08:20 -040091
92 smc911x_set_mac_csr(MII_DATA, val);
93 smc911x_set_mac_csr(MII_ACC,
94 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
95
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +000096 while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
97 ;
Sascha Hauerde1b6862008-04-15 00:08:20 -040098 return 0;
99}
100
101static int smc911x_phy_reset(void)
102{
103 u32 reg;
104
Stefan Roese890a02e2008-11-12 13:31:02 +0100105 reg = smc911x_reg_read(PMT_CTRL);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400106 reg &= ~0xfffff030;
107 reg |= PMT_CTRL_PHY_RST;
Stefan Roese890a02e2008-11-12 13:31:02 +0100108 smc911x_reg_write(PMT_CTRL, reg);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400109
110 mdelay(100);
111
112 return 0;
113}
114
115static void smc911x_phy_configure(void)
116{
117 int timeout;
118 u16 status;
119
120 smc911x_phy_reset();
121
122 smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_RESET);
123 mdelay(1);
124 smc911x_miiphy_write(1, PHY_ANAR, 0x01e1);
125 smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
126
127 timeout = 5000;
128 do {
129 mdelay(1);
130 if ((timeout--) == 0)
131 goto err_out;
132
133 if (smc911x_miiphy_read(1, PHY_BMSR, &status) != 0)
134 goto err_out;
135 } while (!(status & PHY_BMSR_LS));
136
137 printf(DRIVERNAME ": phy initialized\n");
138
139 return;
140
141err_out:
142 printf(DRIVERNAME ": autonegotiation timed out\n");
143}
144
Sascha Hauerde1b6862008-04-15 00:08:20 -0400145static void smc911x_enable(void)
146{
147 /* Enable TX */
Stefan Roese890a02e2008-11-12 13:31:02 +0100148 smc911x_reg_write(HW_CFG, 8 << 16 | HW_CFG_SF);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400149
Stefan Roese890a02e2008-11-12 13:31:02 +0100150 smc911x_reg_write(GPT_CFG, GPT_CFG_TIMER_EN | 10000);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400151
Stefan Roese890a02e2008-11-12 13:31:02 +0100152 smc911x_reg_write(TX_CFG, TX_CFG_TX_ON);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400153
154 /* no padding to start of packets */
Stefan Roese890a02e2008-11-12 13:31:02 +0100155 smc911x_reg_write(RX_CFG, 0);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400156
157 smc911x_set_mac_csr(MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | MAC_CR_HBDIS);
158
159}
160
161int eth_init(bd_t *bd)
162{
Sascha Hauerde1b6862008-04-15 00:08:20 -0400163 printf(DRIVERNAME ": initializing\n");
164
Mike Frysinger75ba6d62009-02-23 10:29:47 -0500165 if (smc911x_detect_chip())
Sascha Hauerde1b6862008-04-15 00:08:20 -0400166 goto err_out;
Sascha Hauerde1b6862008-04-15 00:08:20 -0400167
168 smc911x_reset();
169
170 /* Configure the PHY, initialize the link state */
171 smc911x_phy_configure();
172
173 if (smx911x_handle_mac_address(bd))
174 goto err_out;
175
176 /* Turn on Tx + Rx */
177 smc911x_enable();
178
179 return 0;
180
181err_out:
182 return -1;
183}
184
185int eth_send(volatile void *packet, int length)
186{
187 u32 *data = (u32*)packet;
188 u32 tmplen;
189 u32 status;
190
Stefan Roese890a02e2008-11-12 13:31:02 +0100191 smc911x_reg_write(TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | TX_CMD_A_INT_LAST_SEG | length);
192 smc911x_reg_write(TX_DATA_FIFO, length);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400193
194 tmplen = (length + 3) / 4;
195
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +0000196 while (tmplen--)
Nobuhiro Iwamatsu33314472008-08-28 13:40:44 +0900197 pkt_data_push(TX_DATA_FIFO, *data++);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400198
199 /* wait for transmission */
Stefan Roese890a02e2008-11-12 13:31:02 +0100200 while (!((smc911x_reg_read(TX_FIFO_INF) & TX_FIFO_INF_TSUSED) >> 16));
Sascha Hauerde1b6862008-04-15 00:08:20 -0400201
202 /* get status. Ignore 'no carrier' error, it has no meaning for
203 * full duplex operation
204 */
Stefan Roese890a02e2008-11-12 13:31:02 +0100205 status = smc911x_reg_read(TX_STATUS_FIFO) & (TX_STS_LOC | TX_STS_LATE_COLL |
Sascha Hauerde1b6862008-04-15 00:08:20 -0400206 TX_STS_MANY_COLL | TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
207
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +0000208 if (!status)
Sascha Hauerde1b6862008-04-15 00:08:20 -0400209 return 0;
210
211 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
212 status & TX_STS_LOC ? "TX_STS_LOC " : "",
213 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
214 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
215 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
216 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
217
218 return -1;
219}
220
221void eth_halt(void)
222{
223 smc911x_reset();
224}
225
226int eth_rx(void)
227{
228 u32 *data = (u32 *)NetRxPackets[0];
229 u32 pktlen, tmplen;
230 u32 status;
231
Stefan Roese890a02e2008-11-12 13:31:02 +0100232 if ((smc911x_reg_read(RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
233 status = smc911x_reg_read(RX_STATUS_FIFO);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400234 pktlen = (status & RX_STS_PKT_LEN) >> 16;
235
Stefan Roese890a02e2008-11-12 13:31:02 +0100236 smc911x_reg_write(RX_CFG, 0);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400237
238 tmplen = (pktlen + 2+ 3) / 4;
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +0000239 while (tmplen--)
Nobuhiro Iwamatsu33314472008-08-28 13:40:44 +0900240 *data++ = pkt_data_pull(RX_DATA_FIFO);
Sascha Hauerde1b6862008-04-15 00:08:20 -0400241
Guennadi Liakhovetski3e0f3312008-04-29 12:35:08 +0000242 if (status & RX_STS_ES)
Sascha Hauerde1b6862008-04-15 00:08:20 -0400243 printf(DRIVERNAME
244 ": dropped bad packet. Status: 0x%08x\n",
245 status);
246 else
247 NetReceive(NetRxPackets[0], pktlen);
248 }
249
250 return 0;
251}