]>
Commit | Line | Data |
---|---|---|
de1b686b SH |
1 | /* |
2 | * SMSC LAN9[12]1[567] Network driver | |
3 | * | |
cce9cfda | 4 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
de1b686b SH |
5 | * |
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> | |
de1b686b | 26 | #include <command.h> |
736fead8 | 27 | #include <malloc.h> |
de1b686b SH |
28 | #include <net.h> |
29 | #include <miiphy.h> | |
30 | ||
75ba6d69 | 31 | #include "smc911x.h" |
de1b686b | 32 | |
736fead8 | 33 | u32 pkt_data_pull(struct eth_device *dev, u32 addr) \ |
890a02e8 | 34 | __attribute__ ((weak, alias ("smc911x_reg_read"))); |
736fead8 | 35 | void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \ |
890a02e8 | 36 | __attribute__ ((weak, alias ("smc911x_reg_write"))); |
33314470 | 37 | |
3e0f331c | 38 | #define mdelay(n) udelay((n)*1000) |
de1b686b | 39 | |
45b6b65c | 40 | static void smc911x_handle_mac_address(struct eth_device *dev) |
de1b686b SH |
41 | { |
42 | unsigned long addrh, addrl; | |
736fead8 | 43 | uchar *m = dev->enetaddr; |
de1b686b | 44 | |
736fead8 BW |
45 | 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); | |
de1b686b | 49 | |
736fead8 | 50 | printf(DRIVERNAME ": MAC %pM\n", m); |
de1b686b SH |
51 | } |
52 | ||
736fead8 BW |
53 | static int smc911x_miiphy_read(struct eth_device *dev, |
54 | u8 phy, u8 reg, u16 *val) | |
de1b686b | 55 | { |
736fead8 | 56 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
3e0f331c | 57 | ; |
de1b686b | 58 | |
736fead8 BW |
59 | smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 | |
60 | MII_ACC_MII_BUSY); | |
de1b686b | 61 | |
736fead8 | 62 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
3e0f331c | 63 | ; |
de1b686b | 64 | |
736fead8 | 65 | *val = smc911x_get_mac_csr(dev, MII_DATA); |
de1b686b SH |
66 | |
67 | return 0; | |
68 | } | |
69 | ||
736fead8 BW |
70 | static int smc911x_miiphy_write(struct eth_device *dev, |
71 | u8 phy, u8 reg, u16 val) | |
de1b686b | 72 | { |
736fead8 | 73 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
3e0f331c | 74 | ; |
de1b686b | 75 | |
736fead8 BW |
76 | smc911x_set_mac_csr(dev, MII_DATA, val); |
77 | smc911x_set_mac_csr(dev, MII_ACC, | |
de1b686b SH |
78 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
79 | ||
736fead8 | 80 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
3e0f331c | 81 | ; |
de1b686b SH |
82 | return 0; |
83 | } | |
84 | ||
736fead8 | 85 | static int smc911x_phy_reset(struct eth_device *dev) |
de1b686b SH |
86 | { |
87 | u32 reg; | |
88 | ||
736fead8 | 89 | reg = smc911x_reg_read(dev, PMT_CTRL); |
de1b686b SH |
90 | reg &= ~0xfffff030; |
91 | reg |= PMT_CTRL_PHY_RST; | |
736fead8 | 92 | smc911x_reg_write(dev, PMT_CTRL, reg); |
de1b686b SH |
93 | |
94 | mdelay(100); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
736fead8 | 99 | static void smc911x_phy_configure(struct eth_device *dev) |
de1b686b SH |
100 | { |
101 | int timeout; | |
102 | u16 status; | |
103 | ||
736fead8 | 104 | smc911x_phy_reset(dev); |
de1b686b | 105 | |
736fead8 | 106 | smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_RESET); |
de1b686b | 107 | mdelay(1); |
736fead8 BW |
108 | smc911x_miiphy_write(dev, 1, PHY_ANAR, 0x01e1); |
109 | smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_AUTON | | |
110 | PHY_BMCR_RST_NEG); | |
de1b686b SH |
111 | |
112 | timeout = 5000; | |
113 | do { | |
114 | mdelay(1); | |
115 | if ((timeout--) == 0) | |
116 | goto err_out; | |
117 | ||
736fead8 | 118 | if (smc911x_miiphy_read(dev, 1, PHY_BMSR, &status) != 0) |
de1b686b SH |
119 | goto err_out; |
120 | } while (!(status & PHY_BMSR_LS)); | |
121 | ||
122 | printf(DRIVERNAME ": phy initialized\n"); | |
123 | ||
124 | return; | |
125 | ||
126 | err_out: | |
127 | printf(DRIVERNAME ": autonegotiation timed out\n"); | |
128 | } | |
129 | ||
736fead8 | 130 | static void smc911x_enable(struct eth_device *dev) |
de1b686b SH |
131 | { |
132 | /* Enable TX */ | |
736fead8 | 133 | smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF); |
de1b686b | 134 | |
736fead8 | 135 | smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
de1b686b | 136 | |
736fead8 | 137 | smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON); |
de1b686b SH |
138 | |
139 | /* no padding to start of packets */ | |
736fead8 | 140 | smc911x_reg_write(dev, RX_CFG, 0); |
de1b686b | 141 | |
736fead8 BW |
142 | smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | |
143 | MAC_CR_HBDIS); | |
de1b686b SH |
144 | |
145 | } | |
146 | ||
736fead8 | 147 | static int smc911x_init(struct eth_device *dev, bd_t * bd) |
de1b686b | 148 | { |
2a6cc97b | 149 | struct chip_id *id = dev->priv; |
de1b686b | 150 | |
4946775c | 151 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
de1b686b | 152 | |
736fead8 | 153 | smc911x_reset(dev); |
de1b686b SH |
154 | |
155 | /* Configure the PHY, initialize the link state */ | |
736fead8 | 156 | smc911x_phy_configure(dev); |
de1b686b | 157 | |
45b6b65c | 158 | smc911x_handle_mac_address(dev); |
de1b686b SH |
159 | |
160 | /* Turn on Tx + Rx */ | |
736fead8 | 161 | smc911x_enable(dev); |
de1b686b SH |
162 | |
163 | return 0; | |
de1b686b SH |
164 | } |
165 | ||
736fead8 BW |
166 | static int smc911x_send(struct eth_device *dev, |
167 | volatile void *packet, int length) | |
de1b686b SH |
168 | { |
169 | u32 *data = (u32*)packet; | |
170 | u32 tmplen; | |
171 | u32 status; | |
172 | ||
736fead8 BW |
173 | smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | |
174 | TX_CMD_A_INT_LAST_SEG | length); | |
175 | smc911x_reg_write(dev, TX_DATA_FIFO, length); | |
de1b686b SH |
176 | |
177 | tmplen = (length + 3) / 4; | |
178 | ||
3e0f331c | 179 | while (tmplen--) |
736fead8 | 180 | pkt_data_push(dev, TX_DATA_FIFO, *data++); |
de1b686b SH |
181 | |
182 | /* wait for transmission */ | |
736fead8 BW |
183 | while (!((smc911x_reg_read(dev, TX_FIFO_INF) & |
184 | TX_FIFO_INF_TSUSED) >> 16)); | |
de1b686b SH |
185 | |
186 | /* get status. Ignore 'no carrier' error, it has no meaning for | |
187 | * full duplex operation | |
188 | */ | |
736fead8 BW |
189 | status = smc911x_reg_read(dev, TX_STATUS_FIFO) & |
190 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | | |
191 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); | |
de1b686b | 192 | |
3e0f331c | 193 | if (!status) |
de1b686b SH |
194 | return 0; |
195 | ||
196 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", | |
197 | status & TX_STS_LOC ? "TX_STS_LOC " : "", | |
198 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", | |
199 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", | |
200 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", | |
201 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); | |
202 | ||
203 | return -1; | |
204 | } | |
205 | ||
736fead8 | 206 | static void smc911x_halt(struct eth_device *dev) |
de1b686b | 207 | { |
736fead8 | 208 | smc911x_reset(dev); |
de1b686b SH |
209 | } |
210 | ||
736fead8 | 211 | static int smc911x_rx(struct eth_device *dev) |
de1b686b SH |
212 | { |
213 | u32 *data = (u32 *)NetRxPackets[0]; | |
214 | u32 pktlen, tmplen; | |
215 | u32 status; | |
216 | ||
736fead8 BW |
217 | if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) { |
218 | status = smc911x_reg_read(dev, RX_STATUS_FIFO); | |
de1b686b SH |
219 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
220 | ||
736fead8 | 221 | smc911x_reg_write(dev, RX_CFG, 0); |
de1b686b SH |
222 | |
223 | tmplen = (pktlen + 2+ 3) / 4; | |
3e0f331c | 224 | while (tmplen--) |
736fead8 | 225 | *data++ = pkt_data_pull(dev, RX_DATA_FIFO); |
de1b686b | 226 | |
3e0f331c | 227 | if (status & RX_STS_ES) |
de1b686b SH |
228 | printf(DRIVERNAME |
229 | ": dropped bad packet. Status: 0x%08x\n", | |
230 | status); | |
231 | else | |
232 | NetReceive(NetRxPackets[0], pktlen); | |
233 | } | |
234 | ||
235 | return 0; | |
236 | } | |
736fead8 BW |
237 | |
238 | int smc911x_initialize(u8 dev_num, int base_addr) | |
239 | { | |
240 | unsigned long addrl, addrh; | |
241 | struct eth_device *dev; | |
242 | ||
243 | dev = malloc(sizeof(*dev)); | |
244 | if (!dev) { | |
245 | free(dev); | |
fbd47b67 | 246 | return -1; |
736fead8 BW |
247 | } |
248 | memset(dev, 0, sizeof(*dev)); | |
249 | ||
250 | dev->iobase = base_addr; | |
251 | ||
4bc3d2af SS |
252 | /* Try to detect chip. Will fail if not present. */ |
253 | if (smc911x_detect_chip(dev)) { | |
254 | free(dev); | |
255 | return 0; | |
256 | } | |
257 | ||
736fead8 BW |
258 | addrh = smc911x_get_mac_csr(dev, ADDRH); |
259 | addrl = smc911x_get_mac_csr(dev, ADDRL); | |
76771e59 SR |
260 | if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) { |
261 | /* address is obtained from optional eeprom */ | |
262 | dev->enetaddr[0] = addrl; | |
263 | dev->enetaddr[1] = addrl >> 8; | |
264 | dev->enetaddr[2] = addrl >> 16; | |
265 | dev->enetaddr[3] = addrl >> 24; | |
266 | dev->enetaddr[4] = addrh; | |
267 | dev->enetaddr[5] = addrh >> 8; | |
268 | } | |
736fead8 BW |
269 | |
270 | dev->init = smc911x_init; | |
271 | dev->halt = smc911x_halt; | |
272 | dev->send = smc911x_send; | |
273 | dev->recv = smc911x_rx; | |
274 | sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num); | |
275 | ||
276 | eth_register(dev); | |
fbd47b67 | 277 | return 1; |
736fead8 | 278 | } |