6b13f685e
김민수
BSP 최초 추가
|
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
#include <common.h>
#include <malloc.h>
#include <net.h>
#include <netdev.h>
#include <asm/io.h>
#include <pci.h>
#define PCNET_DEBUG_LEVEL 0 /* 0=off, 1=init, 2=rx/tx */
#define PCNET_DEBUG1(fmt,args...) \
debug_cond(PCNET_DEBUG_LEVEL > 0, fmt ,##args)
#define PCNET_DEBUG2(fmt,args...) \
debug_cond(PCNET_DEBUG_LEVEL > 1, fmt ,##args)
#if !defined(CONF_PCNET_79C973) && defined(CONF_PCNET_79C975)
#error "Macro for PCnet chip version is not defined!"
#endif
#define PCNET_LOG_TX_BUFFERS 0
#define PCNET_LOG_RX_BUFFERS 2
#define TX_RING_SIZE (1 << (PCNET_LOG_TX_BUFFERS))
#define TX_RING_LEN_BITS ((PCNET_LOG_TX_BUFFERS) << 12)
#define RX_RING_SIZE (1 << (PCNET_LOG_RX_BUFFERS))
#define RX_RING_LEN_BITS ((PCNET_LOG_RX_BUFFERS) << 4)
#define PKT_BUF_SZ 1544
struct pcnet_rx_head {
u32 base;
s16 buf_length;
s16 status;
u32 msg_length;
u32 reserved;
};
struct pcnet_tx_head {
u32 base;
s16 length;
s16 status;
u32 misc;
u32 reserved;
};
struct pcnet_init_block {
u16 mode;
u16 tlen_rlen;
u8 phys_addr[6];
u16 reserved;
u32 filter[2];
u32 rx_ring;
u32 tx_ring;
u32 reserved2;
};
struct pcnet_uncached_priv {
struct pcnet_rx_head rx_ring[RX_RING_SIZE];
struct pcnet_tx_head tx_ring[TX_RING_SIZE];
struct pcnet_init_block init_block;
};
typedef struct pcnet_priv {
struct pcnet_uncached_priv *uc;
unsigned char (*rx_buf)[RX_RING_SIZE][PKT_BUF_SZ + 4];
int cur_rx;
int cur_tx;
} pcnet_priv_t;
static pcnet_priv_t *lp;
#define PCNET_RDP 0x10
#define PCNET_RAP 0x12
#define PCNET_RESET 0x14
#define PCNET_BDP 0x16
static u16 pcnet_read_csr(struct eth_device *dev, int index)
{
outw(index, dev->iobase + PCNET_RAP);
return inw(dev->iobase + PCNET_RDP);
}
static void pcnet_write_csr(struct eth_device *dev, int index, u16 val)
{
outw(index, dev->iobase + PCNET_RAP);
outw(val, dev->iobase + PCNET_RDP);
}
static u16 pcnet_read_bcr(struct eth_device *dev, int index)
{
outw(index, dev->iobase + PCNET_RAP);
return inw(dev->iobase + PCNET_BDP);
}
static void pcnet_write_bcr(struct eth_device *dev, int index, u16 val)
{
outw(index, dev->iobase + PCNET_RAP);
outw(val, dev->iobase + PCNET_BDP);
}
static void pcnet_reset(struct eth_device *dev)
{
inw(dev->iobase + PCNET_RESET);
}
static int pcnet_check(struct eth_device *dev)
{
outw(88, dev->iobase + PCNET_RAP);
return inw(dev->iobase + PCNET_RAP) == 88;
}
static int pcnet_init (struct eth_device *dev, bd_t * bis);
static int pcnet_send(struct eth_device *dev, void *packet, int length);
static int pcnet_recv (struct eth_device *dev);
static void pcnet_halt (struct eth_device *dev);
static int pcnet_probe (struct eth_device *dev, bd_t * bis, int dev_num);
#define PCI_TO_MEM(d, a) pci_virt_to_mem((pci_dev_t)d->priv, (a))
#define PCI_TO_MEM_LE(d,a) (u32)(cpu_to_le32(PCI_TO_MEM(d,a)))
static struct pci_device_id supported[] = {
{PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE},
{}
};
int pcnet_initialize(bd_t *bis)
{
pci_dev_t devbusfn;
struct eth_device *dev;
u16 command, status;
int dev_nr = 0;
PCNET_DEBUG1("
pcnet_initialize...
");
for (dev_nr = 0;; dev_nr++) {
devbusfn = pci_find_devices(supported, dev_nr);
if (devbusfn < 0)
break;
dev = (struct eth_device *)malloc(sizeof(*dev));
if (!dev) {
printf("pcnet: Can not allocate memory
");
break;
}
memset(dev, 0, sizeof(*dev));
dev->priv = (void *)devbusfn;
sprintf(dev->name, "pcnet#%d", dev_nr);
pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_0,
(unsigned int *)&dev->iobase);
dev->iobase = pci_io_to_phys(devbusfn, dev->iobase);
dev->iobase &= ~0xf;
PCNET_DEBUG1("%s: devbusfn=0x%x iobase=0x%x: ",
dev->name, devbusfn, dev->iobase);
command = PCI_COMMAND_IO | PCI_COMMAND_MASTER;
pci_write_config_word(devbusfn, PCI_COMMAND, command);
pci_read_config_word(devbusfn, PCI_COMMAND, &status);
if ((status & command) != command) {
printf("%s: Couldn't enable IO access or Bus Mastering
",
dev->name);
free(dev);
continue;
}
pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x40);
if (pcnet_probe(dev, bis, dev_nr) < 0) {
free(dev);
continue;
}
dev->init = pcnet_init;
dev->halt = pcnet_halt;
dev->send = pcnet_send;
dev->recv = pcnet_recv;
eth_register(dev);
}
udelay(10 * 1000);
return dev_nr;
}
static int pcnet_probe(struct eth_device *dev, bd_t *bis, int dev_nr)
{
int chip_version;
char *chipname;
#ifdef PCNET_HAS_PROM
int i;
#endif
pcnet_reset(dev);
if (pcnet_read_csr(dev, 0) != 4 || !pcnet_check(dev)) {
printf("%s: CSR register access check failed
", dev->name);
return -1;
}
chip_version =
pcnet_read_csr(dev, 88) | (pcnet_read_csr(dev, 89) << 16);
if ((chip_version & 0xfff) != 0x003)
return -1;
chip_version = (chip_version >> 12) & 0xffff;
switch (chip_version) {
case 0x2621:
chipname = "PCnet/PCI II 79C970A";
break;
#ifdef CONFIG_PCNET_79C973
case 0x2625:
chipname = "PCnet/FAST III 79C973";
break;
#endif
#ifdef CONFIG_PCNET_79C975
case 0x2627:
chipname = "PCnet/FAST III 79C975";
break;
#endif
default:
printf("%s: PCnet version %#x not supported
",
dev->name, chip_version);
return -1;
}
PCNET_DEBUG1("AMD %s
", chipname);
#ifdef PCNET_HAS_PROM
for (i = 0; i < 3; i++) {
unsigned int val;
val = pcnet_read_csr(dev, i + 12) & 0x0ffff;
dev->enetaddr[2 * i] = val & 0x0ff;
dev->enetaddr[2 * i + 1] = (val >> 8) & 0x0ff;
}
#endif /* PCNET_HAS_PROM */
return 0;
}
static int pcnet_init(struct eth_device *dev, bd_t *bis)
{
struct pcnet_uncached_priv *uc;
int i, val;
u32 addr;
PCNET_DEBUG1("%s: pcnet_init...
", dev->name);
pcnet_write_bcr(dev, 20, 2);
val = pcnet_read_bcr(dev, 2) & ~2;
val |= 2;
pcnet_write_bcr(dev, 2, val);
val = pcnet_read_bcr(dev, 32) & ~0x98;
val |= 0x20;
pcnet_write_bcr(dev, 32, val);
val = pcnet_read_bcr(dev, 18);
val |= 1 << 11;
pcnet_write_bcr(dev, 18, val);
val = pcnet_read_csr(dev, 80);
val |= 0x3 << 10;
pcnet_write_csr(dev, 80, val);
if (lp == NULL) {
addr = (u32)malloc(sizeof(pcnet_priv_t) + 0x10);
addr = (addr + 0xf) & ~0xf;
lp = (pcnet_priv_t *)addr;
addr = (u32)memalign(ARCH_DMA_MINALIGN, sizeof(*lp->uc));
flush_dcache_range(addr, addr + sizeof(*lp->uc));
addr = UNCACHED_SDRAM(addr);
lp->uc = (struct pcnet_uncached_priv *)addr;
addr = (u32)memalign(ARCH_DMA_MINALIGN, sizeof(*lp->rx_buf));
flush_dcache_range(addr, addr + sizeof(*lp->rx_buf));
lp->rx_buf = (void *)addr;
}
uc = lp->uc;
uc->init_block.mode = cpu_to_le16(0x0000);
uc->init_block.filter[0] = 0x00000000;
uc->init_block.filter[1] = 0x00000000;
lp->cur_rx = 0;
for (i = 0; i < RX_RING_SIZE; i++) {
uc->rx_ring[i].base = PCI_TO_MEM_LE(dev, (*lp->rx_buf)[i]);
uc->rx_ring[i].buf_length = cpu_to_le16(-PKT_BUF_SZ);
uc->rx_ring[i].status = cpu_to_le16(0x8000);
PCNET_DEBUG1
("Rx%d: base=0x%x buf_length=0x%hx status=0x%hx
", i,
uc->rx_ring[i].base, uc->rx_ring[i].buf_length,
uc->rx_ring[i].status);
}
lp->cur_tx = 0;
for (i = 0; i < TX_RING_SIZE; i++) {
uc->tx_ring[i].base = 0;
uc->tx_ring[i].status = 0;
}
PCNET_DEBUG1("Init block at 0x%p: MAC", &lp->uc->init_block);
for (i = 0; i < 6; i++) {
lp->uc->init_block.phys_addr[i] = dev->enetaddr[i];
PCNET_DEBUG1(" %02x", lp->uc->init_block.phys_addr[i]);
}
uc->init_block.tlen_rlen = cpu_to_le16(TX_RING_LEN_BITS |
RX_RING_LEN_BITS);
uc->init_block.rx_ring = PCI_TO_MEM_LE(dev, uc->rx_ring);
uc->init_block.tx_ring = PCI_TO_MEM_LE(dev, uc->tx_ring);
PCNET_DEBUG1("
tlen_rlen=0x%x rx_ring=0x%x tx_ring=0x%x
",
uc->init_block.tlen_rlen,
uc->init_block.rx_ring, uc->init_block.tx_ring);
barrier();
addr = PCI_TO_MEM(dev, &lp->uc->init_block);
pcnet_write_csr(dev, 1, addr & 0xffff);
pcnet_write_csr(dev, 2, (addr >> 16) & 0xffff);
pcnet_write_csr(dev, 4, 0x0915);
pcnet_write_csr(dev, 0, 0x0001);
for (i = 10000; i > 0; i--) {
if (pcnet_read_csr(dev, 0) & 0x0100)
break;
udelay(10);
}
if (i <= 0) {
printf("%s: TIMEOUT: controller init failed
", dev->name);
pcnet_reset(dev);
return -1;
}
pcnet_write_csr(dev, 0, 0x0002);
return 0;
}
static int pcnet_send(struct eth_device *dev, void *packet, int pkt_len)
{
int i, status;
struct pcnet_tx_head *entry = &lp->uc->tx_ring[lp->cur_tx];
PCNET_DEBUG2("Tx%d: %d bytes from 0x%p ", lp->cur_tx, pkt_len,
packet);
flush_dcache_range((unsigned long)packet,
(unsigned long)packet + pkt_len);
for (i = 1000; i > 0; i--) {
status = readw(&entry->status);
if ((status & 0x8000) == 0)
break;
udelay(100);
PCNET_DEBUG2(".");
}
if (i <= 0) {
printf("%s: TIMEOUT: Tx%d failed (status = 0x%x)
",
dev->name, lp->cur_tx, status);
pkt_len = 0;
goto failure;
}
writew(-pkt_len, &entry->length);
writel(0, &entry->misc);
writel(PCI_TO_MEM(dev, packet), &entry->base);
writew(0x8300, &entry->status);
pcnet_write_csr(dev, 0, 0x0008);
failure:
if (++lp->cur_tx >= TX_RING_SIZE)
lp->cur_tx = 0;
PCNET_DEBUG2("done
");
return pkt_len;
}
static int pcnet_recv (struct eth_device *dev)
{
struct pcnet_rx_head *entry;
unsigned char *buf;
int pkt_len = 0;
u16 status, err_status;
while (1) {
entry = &lp->uc->rx_ring[lp->cur_rx];
status = readw(&entry->status);
if ((status & 0x8000) != 0)
break;
err_status = status >> 8;
if (err_status != 0x03) {
printf("%s: Rx%d", dev->name, lp->cur_rx);
PCNET_DEBUG1(" (status=0x%x)", err_status);
if (err_status & 0x20)
printf(" Frame");
if (err_status & 0x10)
printf(" Overflow");
if (err_status & 0x08)
printf(" CRC");
if (err_status & 0x04)
printf(" Fifo");
printf(" Error
");
status &= 0x03ff;
} else {
pkt_len = (readl(&entry->msg_length) & 0xfff) - 4;
if (pkt_len < 60) {
printf("%s: Rx%d: invalid packet length %d
",
dev->name, lp->cur_rx, pkt_len);
} else {
buf = (*lp->rx_buf)[lp->cur_rx];
invalidate_dcache_range((unsigned long)buf,
(unsigned long)buf + pkt_len);
NetReceive(buf, pkt_len);
PCNET_DEBUG2("Rx%d: %d bytes from 0x%p
",
lp->cur_rx, pkt_len, buf);
}
}
status |= 0x8000;
writew(status, &entry->status);
if (++lp->cur_rx >= RX_RING_SIZE)
lp->cur_rx = 0;
}
return pkt_len;
}
static void pcnet_halt(struct eth_device *dev)
{
int i;
PCNET_DEBUG1("%s: pcnet_halt...
", dev->name);
pcnet_reset(dev);
for (i = 1000; i > 0; i--) {
if (pcnet_read_csr(dev, 0) & 0x4)
break;
udelay(10);
}
if (i <= 0)
printf("%s: TIMEOUT: controller reset failed
", dev->name);
}
|