1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <command.h>
27#include <malloc.h>
28#include <net.h>
29#include <miiphy.h>
30
31#include "smc911x.h"
32
33u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
34 __attribute__ ((weak, alias ("smc911x_reg_read")));
35void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
36 __attribute__ ((weak, alias ("smc911x_reg_write")));
37
38#define mdelay(n) udelay((n)*1000)
39
40static void smc911x_handle_mac_address(struct eth_device *dev)
41{
42 unsigned long addrh, addrl;
43 uchar *m = dev->enetaddr;
44
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);
49
50 printf(DRIVERNAME ": MAC %pM\n", m);
51}
52
53static int smc911x_miiphy_read(struct eth_device *dev,
54 u8 phy, u8 reg, u16 *val)
55{
56 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
57 ;
58
59 smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
60 MII_ACC_MII_BUSY);
61
62 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
63 ;
64
65 *val = smc911x_get_mac_csr(dev, MII_DATA);
66
67 return 0;
68}
69
70static int smc911x_miiphy_write(struct eth_device *dev,
71 u8 phy, u8 reg, u16 val)
72{
73 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
74 ;
75
76 smc911x_set_mac_csr(dev, MII_DATA, val);
77 smc911x_set_mac_csr(dev, MII_ACC,
78 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
79
80 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
81 ;
82 return 0;
83}
84
85static int smc911x_phy_reset(struct eth_device *dev)
86{
87 u32 reg;
88
89 reg = smc911x_reg_read(dev, PMT_CTRL);
90 reg &= ~0xfffff030;
91 reg |= PMT_CTRL_PHY_RST;
92 smc911x_reg_write(dev, PMT_CTRL, reg);
93
94 mdelay(100);
95
96 return 0;
97}
98
99static void smc911x_phy_configure(struct eth_device *dev)
100{
101 int timeout;
102 u16 status;
103
104 smc911x_phy_reset(dev);
105
106 smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_RESET);
107 mdelay(1);
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);
111
112 timeout = 5000;
113 do {
114 mdelay(1);
115 if ((timeout--) == 0)
116 goto err_out;
117
118 if (smc911x_miiphy_read(dev, 1, PHY_BMSR, &status) != 0)
119 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
130static void smc911x_enable(struct eth_device *dev)
131{
132
133 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
134
135 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
136
137 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
138
139
140 smc911x_reg_write(dev, RX_CFG, 0);
141
142 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
143 MAC_CR_HBDIS);
144
145}
146
147static int smc911x_init(struct eth_device *dev, bd_t * bd)
148{
149 struct chip_id *id = dev->priv;
150
151 printf(DRIVERNAME ": detected %s controller\n", id->name);
152
153 smc911x_reset(dev);
154
155
156 smc911x_phy_configure(dev);
157
158 smc911x_handle_mac_address(dev);
159
160
161 smc911x_enable(dev);
162
163 return 0;
164}
165
166static int smc911x_send(struct eth_device *dev,
167 volatile void *packet, int length)
168{
169 u32 *data = (u32*)packet;
170 u32 tmplen;
171 u32 status;
172
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);
176
177 tmplen = (length + 3) / 4;
178
179 while (tmplen--)
180 pkt_data_push(dev, TX_DATA_FIFO, *data++);
181
182
183 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
184 TX_FIFO_INF_TSUSED) >> 16));
185
186
187
188
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);
192
193 if (!status)
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
206static void smc911x_halt(struct eth_device *dev)
207{
208 smc911x_reset(dev);
209}
210
211static int smc911x_rx(struct eth_device *dev)
212{
213 u32 *data = (u32 *)NetRxPackets[0];
214 u32 pktlen, tmplen;
215 u32 status;
216
217 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
218 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
219 pktlen = (status & RX_STS_PKT_LEN) >> 16;
220
221 smc911x_reg_write(dev, RX_CFG, 0);
222
223 tmplen = (pktlen + 3) / 4;
224 while (tmplen--)
225 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
226
227 if (status & RX_STS_ES)
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}
237
238int 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 return -1;
246 }
247 memset(dev, 0, sizeof(*dev));
248
249 dev->iobase = base_addr;
250
251
252 if (smc911x_detect_chip(dev)) {
253 free(dev);
254 return 0;
255 }
256
257 addrh = smc911x_get_mac_csr(dev, ADDRH);
258 addrl = smc911x_get_mac_csr(dev, ADDRL);
259 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
260
261 dev->enetaddr[0] = addrl;
262 dev->enetaddr[1] = addrl >> 8;
263 dev->enetaddr[2] = addrl >> 16;
264 dev->enetaddr[3] = addrl >> 24;
265 dev->enetaddr[4] = addrh;
266 dev->enetaddr[5] = addrh >> 8;
267 }
268
269 dev->init = smc911x_init;
270 dev->halt = smc911x_halt;
271 dev->send = smc911x_send;
272 dev->recv = smc911x_rx;
273 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
274
275 eth_register(dev);
276 return 1;
277}
278