1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <command.h>
26#include <net.h>
27#include "netarm_eth.h"
28#include <asm/arch/netarm_registers.h>
29
30static int na_mii_poll_busy (void);
31
32static void na_get_mac_addr (void)
33{
34 unsigned short p[3];
35 char *m_addr;
36 char ethaddr[20];
37
38 m_addr = (char *) p;
39
40 p[0] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_1);
41 p[1] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_2);
42 p[2] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_3);
43
44 sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
45 m_addr[0], m_addr[1],
46 m_addr[2], m_addr[3], m_addr[4], m_addr[5]);
47
48 printf ("HW-MAC Address: %s\n", ethaddr);
49
50
51 setenv ("ethaddr", ethaddr);
52}
53
54static void na_mii_write (int reg, int value)
55{
56 int mii_addr;
57
58
59 mii_addr = CONFIG_SYS_ETH_PHY_ADDR + reg;
60 SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
61
62 SET_EADDR (NETARM_ETH_MII_WRITE, value);
63 na_mii_poll_busy ();
64}
65
66static unsigned int na_mii_read (int reg)
67{
68 int mii_addr, val;
69
70
71 mii_addr = CONFIG_SYS_ETH_PHY_ADDR + reg;
72 SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
73
74 SET_EADDR (NETARM_ETH_MII_CMD,
75 GET_EADDR (NETARM_ETH_MII_CMD) | NETARM_ETH_MIIC_RSTAT);
76 na_mii_poll_busy ();
77
78 val = GET_EADDR (NETARM_ETH_MII_READ);
79 return val;
80}
81
82static int na_mii_poll_busy (void)
83{
84
85 reset_timer_masked ();
86 while (get_timer_masked () < NA_MII_POLL_BUSY_DELAY) {
87 if (!(GET_EADDR (NETARM_ETH_MII_IND) & NETARM_ETH_MIII_BUSY)) {
88 return 1;
89 }
90 }
91 printf ("na_mii_busy timeout\n");
92 return (0);
93}
94
95static int na_mii_identify_phy (void)
96{
97 int id_reg_a = 0;
98
99
100 id_reg_a = na_mii_read (MII_PHY_ID);
101
102 if (id_reg_a == 0x0043) {
103
104 return 1;
105 } else if (id_reg_a == 0x0013) {
106
107 return 1;
108 }
109 return (0);
110}
111
112static int na_mii_negotiate (void)
113{
114 int i = 0;
115
116
117 na_mii_write (MII_PHY_AUTONEGADV, 0x01e1);
118
119
120 na_mii_write (MII_PHY_CONTROL, 0x1200);
121
122
123 while (na_mii_read (MII_PHY_STATUS) == 0xffff) {
124 i++;
125 }
126
127
128
129
130
131 for (i = 0; i < 40000; i++) {
132 if ((na_mii_read (MII_PHY_STATUS) & 0x0024) == 0x0024) {
133 return 0;
134 }
135 }
136
137
138
139 return (1);
140}
141
142static unsigned int na_mii_check_speed (void)
143{
144 unsigned int status;
145
146
147 status = na_mii_read (MII_PHY_STATUS);
148
149 if ((status & 0x0004) == 0) {
150 printf ("*Warning* no link detected, set default speed to 100Mbs\n");
151 return 1;
152 } else {
153 if ((na_mii_read (17) & 0x4000) != 0) {
154 printf ("100Mbs link detected\n");
155 return 1;
156 } else {
157 printf ("10Mbs link detected\n");
158 return 0;
159 }
160 }
161 return 0;
162}
163
164static int reset_eth (void)
165{
166 int pt;
167
168 na_get_mac_addr ();
169 pt = na_mii_identify_phy ();
170
171
172 na_mii_write (MII_PHY_CONTROL, 0x8000);
173 reset_timer_masked ();
174 while (get_timer_masked () < NA_MII_NEGOTIATE_DELAY) {
175 if ((na_mii_read (MII_PHY_STATUS) & 0x8000) == 0) {
176 break;
177 }
178 }
179 if (get_timer_masked () >= NA_MII_NEGOTIATE_DELAY)
180 printf ("phy reset timeout\n");
181
182
183 SET_EADDR (NETARM_ETH_PCS_CFG, NETARM_ETH_PCSC_CLKS_25M |
184 NETARM_ETH_PCSC_ENJAB | NETARM_ETH_PCSC_NOCFR);
185
186 na_mii_negotiate ();
187 na_mii_check_speed ();
188
189
190 udelay (10000);
191
192
193
194
195
196 SET_EADDR (NETARM_ETH_STL_CFG,
197 (NETARM_ETH_STLC_AUTOZ | NETARM_ETH_STLC_RXEN));
198
199
200
201
202
203 SET_EADDR (NETARM_ETH_B2B_IPG_GAP_TMR, 0x15);
204 SET_EADDR (NETARM_ETH_NB2B_IPG_GAP_TMR, 0x00000C12);
205
206
207
208
209
210
211 SET_EADDR (NETARM_ETH_MAC_CFG,
212 (NETARM_ETH_MACC_CRCEN |
213 NETARM_ETH_MACC_PADEN | NETARM_ETH_MACC_HUGEN));
214 SET_EADDR (NETARM_ETH_SAL_FILTER, NETARM_ETH_SALF_BROAD);
215
216
217 SET_EADDR (NETARM_ETH_GEN_CTRL,
218 (NETARM_ETH_GCR_ERX | NETARM_ETH_GCR_ETX));
219
220 return (0);
221}
222
223
224extern int eth_init (bd_t * bd)
225{
226 reset_eth ();
227 return 0;
228}
229
230extern void eth_halt (void)
231{
232 SET_EADDR (NETARM_ETH_GEN_CTRL, 0);
233}
234
235
236extern int eth_rx (void)
237{
238 int i;
239 unsigned short rxlen;
240 unsigned int *addr;
241 unsigned int rxstatus, lastrxlen;
242 char *pa;
243
244
245 if ((GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXBR) == 0)
246 return 0;
247
248
249 rxstatus = GET_EADDR (NETARM_ETH_RX_STAT);
250 rxlen = (rxstatus & NETARM_ETH_RXSTAT_SIZE) >> 16;
251
252 if (rxlen == 0)
253 return 0;
254
255
256 SET_EADDR (NETARM_ETH_GEN_STAT,
257 GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_RXBR);
258
259
260
261
262
263 SET_EADDR (NETARM_ETH_GEN_STAT,
264 GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_TXBC);
265
266 addr = (unsigned int *) NetRxPackets[0];
267 pa = (char *) NetRxPackets[0];
268
269
270 for (i = 0; i < rxlen / 4; i++) {
271 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
272 addr++;
273 }
274
275 if (GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXREGR) {
276
277 lastrxlen =
278 (GET_EADDR (NETARM_ETH_GEN_STAT) &
279 NETARM_ETH_GST_RXFDB) >> 28;
280 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
281 switch (lastrxlen) {
282 case 1:
283 *addr &= 0xff000000;
284 break;
285 case 2:
286 *addr &= 0xffff0000;
287 break;
288 case 3:
289 *addr &= 0xffffff00;
290 break;
291 }
292 }
293
294
295 NetReceive (NetRxPackets[0], rxlen);
296
297 return rxlen;
298}
299
300
301extern int eth_send (volatile void *packet, int length)
302{
303 int i, length32;
304 char *pa;
305 unsigned int *pa32, lastp = 0, rest;
306
307 pa = (char *) packet;
308 pa32 = (unsigned int *) packet;
309 length32 = length / 4;
310 rest = length % 4;
311
312
313 switch (rest) {
314 case 0:
315 lastp = pa32[length32];
316 length32--;
317 break;
318 case 1:
319 lastp = pa32[length32] & 0x000000ff;
320 break;
321 case 2:
322 lastp = pa32[length32] & 0x0000ffff;
323 break;
324 case 3:
325 lastp = pa32[length32] & 0x00ffffff;
326 break;
327 }
328
329
330 for (i = 0; i < length32; i++)
331 SET_EADDR (NETARM_ETH_FIFO_DAT1, pa32[i]);
332
333
334
335 SET_EADDR (NETARM_ETH_FIFO_DAT2, lastp);
336
337
338
339
340 i = 0;
341 while (i < 50000) {
342 if ((GET_EADDR (NETARM_ETH_TX_STAT) & NETARM_ETH_TXSTAT_TXOK)
343 == NETARM_ETH_TXSTAT_TXOK)
344 return 0;
345 i++;
346 }
347
348 printf ("eth_send timeout\n");
349 return 1;
350}
351