/
r6040.cpp
executable file
·451 lines (350 loc) · 9.99 KB
/
r6040.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// Low level ring-buffer manipulation for r6040 ethernet.
//
#include "io.h"
#include <string.h>
#include <stdio.h>
#include "setup.h"
#include "r6040.h"
#include "iolib.h"
#include "stdlib.h"
#include "timer.h"
#include "ether.h"
#include "config.h"
// definitions for
#define IOADDR 0xe800
#define MCR0 (IOADDR + 0x00)
#define TX_START_LOW (IOADDR + 0x2c)
#define TX_START_HIGH (IOADDR + 0x30)
#define RX_START_LOW (IOADDR + 0x34)
#define RX_START_HIGH (IOADDR + 0x38)
#define INT_STATUS (IOADDR + 0x3c)
#define MMDIO (IOADDR + 0x20) /* MDIO control register */
#define MDIO_WRITE 0x4000 /* MDIO write */
#define MDIO_READ 0x2000 /* MDIO read */
#define MMRD (IOADDR + 0x24) /* MDIO read data register */
#define MMWD (IOADDR + 0x28) /* MDIO write data register */
#define R6040_PKT_MAX_LEN 1536
// Descriptor definition and manip functions.
// doubles as the TX and RX descriptor
struct _eth_desc {
unsigned short DST;
unsigned short DLEN;
unsigned char* DBP;
struct _eth_desc* DNX;
unsigned short HIDX;
unsigned short Reserved1;
unsigned short Reserved2;
// Extra info, used by driver
unsigned short DLEN_orig;
};
// Diagnostic functions.
void addr_dump16(const char* name, unsigned short val)
{
printf("%s: 0x%04x ", name, val);
}
void addr_dump32(const char* name, unsigned long val)
{
printf("%s: 0x%x ", name, unsigned(val));
}
// print the descriptor
void desc_dump(struct _eth_desc* d)
{
printf("%x\n", unsigned(d));
addr_dump16("DST", d->DST);
addr_dump16("DLEN", d->DLEN);
addr_dump32("DBP", (unsigned long)d->DBP);
addr_dump32("DNX", (unsigned long)d->DNX);
addr_dump16("HIDX", d->HIDX);
printf("\n");
// get the address of the buffer.
hex_dump(d->DBP, 32);
printf("\n");
}
void io_dump(const char* name, unsigned offset)
{
unsigned short tmp;
tmp = inw(IOADDR + offset);
printf("%s: 0x%04x\n", name, tmp);
}
// Pass in the next pointer
struct _eth_desc* rxd_init(size_t pkt_size)
{
// allocate the descriptor memory
struct _eth_desc* rxd = (struct _eth_desc*)dma_malloc(pkt_size);
// Clear it
memset(rxd,0,sizeof(struct _eth_desc));
// allocate some space for a packet
unsigned char* pkt = (unsigned char*)dma_malloc(pkt_size);
// clear pkt area
memset(pkt,0,pkt_size);
// Make this one owned by the MAC
rxd->DST = 0x8000;
// Set the buffer pointer
rxd->DBP = pkt;
rxd->DLEN = pkt_size;
return rxd;
}
void r6040_tx_enable()
{
unsigned short tmp = inw(MCR0);
outw(tmp | (1 << 12), MCR0);
}
void r6040_tx_disable()
{
unsigned short tmp = inw(MCR0);
outw(tmp & ~(1 << 12), MCR0);
}
void r6040_rx_enable()
{
unsigned short tmp = inw(MCR0);
outw(tmp | (1 << 1), MCR0);
// outw(2, MCR0);
}
void r6040_rx_disable()
{
outw(0, MCR0);
}
// Dump the interesting registers
void r6040_dump()
{
printf("\r\n== DUMP ==\n");
io_dump("MAC0", 0x00);
io_dump("MAC1", 0x04);
io_dump("MAC Bus control", 0x08);
io_dump("TX start0", 0x2c);
io_dump("TX start1", 0x30);
io_dump("RX start0", 0x34);
io_dump("RX start1", 0x38);
io_dump("INT Stat", 0x3c);
}
void r6040_set_tx_start(struct _eth_desc* desc)
{
unsigned long tmp = (unsigned long)desc;
outw((tmp & 0xffff), TX_START_LOW);
tmp >>= 16;
outw((tmp & 0xffff), TX_START_HIGH);
}
void r6040_set_rx_start(struct _eth_desc* desc)
{
unsigned long tmp = (unsigned long)desc;
outw((tmp & 0xffff), RX_START_LOW);
tmp >>= 16;
outw((tmp & 0xffff), RX_START_HIGH);
}
////////////////////////////////////////////////////////////////
// Interface
// Keep track of what we've allocated, so we can easily walk it.
struct _eth_desc* g_rx_descriptor_list[R6040_RX_DESCRIPTORS];
struct _eth_desc* g_rx_descriptor_next;
struct _eth_desc* g_tx_descriptor_next;
// setup descriptors, start packet reception
void r6040_init(void)
{
int i;
r6040_rx_disable();
r6040_tx_disable();
for (i=0;i<R6040_RX_DESCRIPTORS;i++)
{
g_rx_descriptor_list[i] = rxd_init(R6040_PKT_MAX_LEN); // most packets will be no larger than this
if (i) g_rx_descriptor_list[i-1]->DNX = g_rx_descriptor_list[i];
}
// Make ring buffer.
g_rx_descriptor_list[R6040_RX_DESCRIPTORS-1]->DNX = g_rx_descriptor_list[0];
r6040_set_rx_start(g_rx_descriptor_list[0]);
g_rx_descriptor_next = g_rx_descriptor_list[0];
for (i=0;i<R6040_RX_DESCRIPTORS;i++)
{
// desc_dump(g_rx_descriptor_list[i]);
}
// printf("r6040 init\n");
g_tx_descriptor_next = rxd_init(R6040_PKT_MAX_LEN);
}
void DiscardDescriptor()
{
// reset the length field to original value.
g_rx_descriptor_next->DLEN = g_rx_descriptor_next->DLEN_orig;
g_rx_descriptor_next->DST = 0x8000; // give back to the MAC.
g_rx_descriptor_next = g_rx_descriptor_next->DNX; // Move along
}
// Returns size of pkt, or zero if none received.
size_t r6040_rx(unsigned char* pkt, size_t max_len)
{
size_t ret=0;
if (g_rx_descriptor_next->DST & 0x8000)
{
// Still owned by the MAC, nothing received
return ret;
}
if (!(g_rx_descriptor_next->DST & (1 << 14)))
{
printf("Descriptor discarded with error: %04x\n", g_rx_descriptor_next->DST);
DiscardDescriptor();
return ret;
}
// If the buffer isn't long enough discard this packet.
if (g_rx_descriptor_next->DLEN > max_len)
{
printf("Descriptor descarded (buffer too short)\n"
"Packet length: %04x\n", g_rx_descriptor_next->DLEN );
printf("Buffer length: %x\n", unsigned(max_len));
DiscardDescriptor();
return ret;
}
// Otherwise copy out and advance buffer pointer
//printf("Received packet\n");
memcpy(pkt, g_rx_descriptor_next->DBP, g_rx_descriptor_next->DLEN);
ret = g_rx_descriptor_next->DLEN;
ret -= 4; // chop the checksum, we don't need it.
DiscardDescriptor();
return ret;
}
// queue packet for transmission
void r6040_tx(unsigned char* pkt, size_t length)
{
r6040_tx_disable();
if (length>R6040_PKT_MAX_LEN)
{
printf("TX packet exceeds maximum length, skipping\n");
return;
}
// Zero the packet if it's a runt
if (length<60) memset(g_tx_descriptor_next->DBP, 0, 60);
memcpy(g_tx_descriptor_next->DBP, pkt, length);
g_tx_descriptor_next->DLEN = (length<60)? 60:length;
// Copy the descriptor address (will have been set to zero by last op).
r6040_set_tx_start(g_tx_descriptor_next);
// Make the mac own it.
g_tx_descriptor_next->DST = 0x8000;
//desc_dump(g_tx_descriptor_next);
// Start xmit.
r6040_tx_enable();
//printf("Waiting to get ownership\n");
// poll for mac to no longer own it.
while (g_tx_descriptor_next->DST & 0x8000)
{
}
// Stop any other activity.
r6040_tx_disable();
//desc_dump(g_tx_descriptor_next);
}
unsigned short r6040_mdio_read(int reg, int phy)
{
outw(MDIO_READ + reg + (phy << 8), MMDIO);
/* Wait for the read bit to be cleared */
while (inw(MMDIO) & MDIO_READ);
return inw(MMRD);
}
void r6040_phy_printf()
{
unsigned short tmp;
tmp = r6040_mdio_read(0,1);
printf("MII Control: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(1,1);
printf("MII Status: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(4,1);
printf("PHY4: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(5,1);
printf("PHY5: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(6,1);
printf("PHY6: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(16,1);
printf("PHY16: %04x\n", unsigned(tmp));
tmp = r6040_mdio_read(17,1);
printf("PHY17: %04x\n", unsigned(tmp));
}
// Wait for linkup, or timeout, return the time taken for the link to come up.
int r6040_wait_linkup(int delay, int* uptime)
{
u16 established = 0;
u16 i;
// read the vendor information
//tmp = r6040_mdio_read(2,1);
//print_hex16(tmp); printf("\n");
//tmp = r6040_mdio_read(3,1);
//print_hex16(tmp); printf("\n");
*uptime = 0;
for (i=0;i<delay;i++)
{
// Check if link up.
established = r6040_mdio_read(1,1) & (1<<2);
if (established) break;
// If user pressed a key skip this wait.
if (keyready()) return 0;
timer_mdelay10();
(*uptime)++; // how long we waited
}
if (established)
{
printf("NIC up in: %d0ms\n", i);
}
else
{
printf("No network link established - cable disconnected?\n");
}
return established;
}
#define REG_MAC1 0xe868
#define REG_MAC2 0xe86a
#define REG_MAC3 0xe86c
static void r6040_set_common_mac()
{
// mac addresses for the MAC E800
// MAC 1 multicast addresses
outw(0xffff, 0xe870);
outw(0xffff, 0xe872);
outw(0xffff, 0xe874);
outw(0xffff, 0xe878);
outw(0xffff, 0xe87a);
outw(0xffff, 0xe87c);
outw(0xffff, 0xe880);
outw(0xffff, 0xe882);
outw(0xffff, 0xe884);
outw(0x9f07, 0xe888); // MAC status change register
}
void r6040_print_common_mac()
{
// Now read them back, to check they got set OK.
printf("%02x", unsigned(inw(REG_MAC1) & 0xff));
printf("%02x", unsigned((inw(REG_MAC1) >> 8) & 0xff));
printf("%02x", unsigned(inw(REG_MAC2) & 0xff));
printf("%02x", unsigned((inw(REG_MAC2) >> 8) & 0xff));
printf("%02x", unsigned(inw(REG_MAC3) & 0xff));
printf("%02x", unsigned((inw(REG_MAC3) >> 8) & 0xff));
}
int r6040_get_mac_suffix(char* buffer, u32 len)
{
// copy unique mac into buffer
char tmp[3];
get_hex8(tmp, sizeof(tmp), unsigned((inw(REG_MAC2) >> 8) & 0xff));
strcat(buffer, tmp);
get_hex8(tmp, sizeof(tmp), unsigned(inw(REG_MAC3) & 0xff));
strcat(buffer, tmp);
get_hex8(tmp, sizeof(tmp), unsigned((inw(REG_MAC3) >> 8) & 0xff));
strcat(buffer, tmp);
return 6;
}
// Just setup the addresses, don't start packet reception.
void r6040_set_mac()
{
r6040_set_common_mac();
// MAC address for eth0
if (ether_mac_unset())
{
// We always want the MAC to be something sensible, otherwise the board
// won't be ethernet programmable.
outw(0x0100, REG_MAC1);
outw(0x0302, REG_MAC2);
outw(0x0504, REG_MAC3);
}
else
{
outw(ETHER_MAC->addr0, REG_MAC1);
outw(ETHER_MAC->addr1, REG_MAC2);
outw(ETHER_MAC->addr2, REG_MAC3);
}
// MAC address for eth1, never used.
outw(0x0000, 0xe968);
outw(0x0100, 0xe96a);
outw(0x0300, 0xe96c);
}