1#ifdef ALLMULTI
2#error multicast support is not yet implemented
3#endif
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18FILE_LICENCE ( GPL_ANY );
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#include "etherboot.h"
47#include "nic.h"
48#include <gpxe/pci.h>
49#include <gpxe/ethernet.h>
50
51#undef DAVICOM_DEBUG
52#undef DAVICOM_DEBUG_WHERE
53
54#define TX_TIME_OUT 2*TICKS_PER_SEC
55
56
57enum davicom_offsets {
58 CSR0=0, CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
59 CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
60 CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78, CSR16=0x80, CSR20=0xA0
61};
62
63
64#define EEPROM_ADDRLEN 6
65#define EEPROM_SIZE 32
66
67
68
69
70static unsigned char ee_data[EEPROM_SIZE];
71
72
73#define EE_WRITE_CMD (5 << addr_len)
74#define EE_READ_CMD (6 << addr_len)
75#define EE_ERASE_CMD (7 << addr_len)
76
77
78#define EE_SHIFT_CLK 0x02
79#define EE_CS 0x01
80#define EE_DATA_WRITE 0x04
81#define EE_WRITE_0 0x01
82#define EE_WRITE_1 0x05
83#define EE_DATA_READ 0x08
84#define EE_ENB (0x4800 | EE_CS)
85
86
87#define PHY_DATA_0 0x0
88#define PHY_DATA_1 0x20000
89#define MDCLKH 0x10000
90
91
92
93
94#define eeprom_delay() inl(ee_addr)
95
96
97
98
99
100
101
102
103struct txdesc {
104 volatile unsigned long status;
105 unsigned long buf1sz:11,
106 buf2sz:11,
107 control:10;
108 const unsigned char *buf1addr;
109 const unsigned char *buf2addr;
110};
111
112struct rxdesc {
113 volatile unsigned long status;
114 unsigned long buf1sz:11,
115 buf2sz:11,
116 control:10;
117 unsigned char *buf1addr;
118 unsigned char *buf2addr;
119};
120
121
122#define BUFLEN 1536
123
124
125
126
127
128static struct nic_operations davicom_operations;
129
130
131static unsigned short vendor, dev_id;
132static unsigned long ioaddr;
133
134
135
136
137
138#define NTXD 2
139#define NRXD 4
140struct {
141 struct txdesc txd[NTXD] __attribute__ ((aligned(4)));
142 unsigned char txb[BUFLEN] __attribute__ ((aligned(4)));
143 struct rxdesc rxd[NRXD] __attribute__ ((aligned(4)));
144 unsigned char rxb[NRXD * BUFLEN] __attribute__ ((aligned(4)));
145} davicom_bufs __shared;
146#define txd davicom_bufs.txd
147#define txb davicom_bufs.txb
148#define rxd davicom_bufs.rxd
149#define rxb davicom_bufs.rxb
150static int rxd_tail;
151static int TxPtr;
152
153
154
155
156
157static void whereami(const char *str);
158static int read_eeprom(unsigned long ioaddr, int location, int addr_len);
159static int davicom_probe(struct nic *nic,struct pci_device *pci);
160static void davicom_init_chain(struct nic *nic);
161static void davicom_reset(struct nic *nic);
162static void davicom_transmit(struct nic *nic, const char *d, unsigned int t,
163 unsigned int s, const char *p);
164static int davicom_poll(struct nic *nic, int retrieve);
165static void davicom_disable(struct nic *nic);
166#ifdef DAVICOM_DEBUG
167static void davicom_more(void);
168#endif
169static void davicom_wait(unsigned int nticks);
170static int phy_read(int);
171static void phy_write(int, u16);
172static void phy_write_1bit(u32, u32);
173static int phy_read_1bit(u32);
174static void davicom_media_chk(struct nic *);
175
176
177
178
179
180static inline void whereami(const char *str)
181{
182 printf("%s\n", str);
183
184}
185
186#ifdef DAVICOM_DEBUG
187static void davicom_more()
188{
189 printf("\n\n-- more --");
190 while (!iskey())
191 ;
192 getchar();
193 printf("\n\n");
194}
195#endif
196
197static void davicom_wait(unsigned int nticks)
198{
199 unsigned int to = currticks() + nticks;
200 while (currticks() < to)
201 ;
202}
203
204
205
206
207
208
209
210
211static int phy_read(int location)
212{
213 int i, phy_addr=1;
214 u16 phy_data;
215 u32 io_dcr9;
216
217 whereami("phy_read\n");
218
219 io_dcr9 = ioaddr + CSR9;
220
221
222 for (i=0; i<34; i++)
223 phy_write_1bit(io_dcr9, PHY_DATA_1);
224
225
226 phy_write_1bit(io_dcr9, PHY_DATA_0);
227 phy_write_1bit(io_dcr9, PHY_DATA_1);
228
229
230 phy_write_1bit(io_dcr9, PHY_DATA_1);
231 phy_write_1bit(io_dcr9, PHY_DATA_0);
232
233
234 for (i=0x10; i>0; i=i>>1)
235 phy_write_1bit(io_dcr9, phy_addr&i ? PHY_DATA_1: PHY_DATA_0);
236
237
238 for (i=0x10; i>0; i=i>>1)
239 phy_write_1bit(io_dcr9, location&i ? PHY_DATA_1: PHY_DATA_0);
240
241
242 phy_read_1bit(io_dcr9);
243
244
245 for (phy_data=0, i=0; i<16; i++) {
246 phy_data<<=1;
247 phy_data|=phy_read_1bit(io_dcr9);
248 }
249
250 return phy_data;
251}
252
253
254
255
256static void phy_write(int location, u16 phy_data)
257{
258 u16 i, phy_addr=1;
259 u32 io_dcr9;
260
261 whereami("phy_write\n");
262
263 io_dcr9 = ioaddr + CSR9;
264
265
266 for (i=0; i<34; i++)
267 phy_write_1bit(io_dcr9, PHY_DATA_1);
268
269
270 phy_write_1bit(io_dcr9, PHY_DATA_0);
271 phy_write_1bit(io_dcr9, PHY_DATA_1);
272
273
274 phy_write_1bit(io_dcr9, PHY_DATA_0);
275 phy_write_1bit(io_dcr9, PHY_DATA_1);
276
277
278 for (i=0x10; i>0; i=i>>1)
279 phy_write_1bit(io_dcr9, phy_addr&i ? PHY_DATA_1: PHY_DATA_0);
280
281
282 for (i=0x10; i>0; i=i>>1)
283 phy_write_1bit(io_dcr9, location&i ? PHY_DATA_1: PHY_DATA_0);
284
285
286 phy_write_1bit(io_dcr9, PHY_DATA_1);
287 phy_write_1bit(io_dcr9, PHY_DATA_0);
288
289
290 for (i=0x8000; i>0; i>>=1)
291 phy_write_1bit(io_dcr9, phy_data&i ? PHY_DATA_1: PHY_DATA_0);
292}
293
294
295
296
297static void phy_write_1bit(u32 ee_addr, u32 phy_data)
298{
299 whereami("phy_write_1bit\n");
300 outl(phy_data, ee_addr);
301 eeprom_delay();
302 outl(phy_data|MDCLKH, ee_addr);
303 eeprom_delay();
304 outl(phy_data, ee_addr);
305 eeprom_delay();
306}
307
308
309
310
311static int phy_read_1bit(u32 ee_addr)
312{
313 int phy_data;
314
315 whereami("phy_read_1bit\n");
316
317 outl(0x50000, ee_addr);
318 eeprom_delay();
319
320 phy_data=(inl(ee_addr)>>19) & 0x1;
321
322 outl(0x40000, ee_addr);
323 eeprom_delay();
324
325 return phy_data;
326}
327
328
329
330
331static void HPNA_process(void)
332{
333
334 if ( (phy_read(3) & 0xfff0) == 0xb900 ) {
335 if ( phy_read(31) == 0x4404 ) {
336
337 if (phy_read(3) == 0xb901)
338 phy_write(16, 0x5);
339 else
340 phy_write(16, 0x1005);
341 phy_write(25, ((phy_read(24) + 3) & 0xff) | 0xf000);
342 } else {
343
344 phy_write(16, 0x5);
345 phy_write(25, (phy_read(25) & 0xff00) + 2);
346 }
347 }
348}
349
350
351
352
353static void davicom_media_chk(struct nic * nic __unused)
354{
355 unsigned long to, csr6;
356
357 csr6 = 0x00200000;
358 outl(csr6, ioaddr + CSR6);
359
360#define PCI_DEVICE_ID_DM9009 0x9009
361 if (vendor == PCI_VENDOR_ID_DAVICOM && dev_id == PCI_DEVICE_ID_DM9009) {
362
363 phy_write(0, 0);
364 } else {
365
366 to = currticks() + 2 * TICKS_PER_SEC;
367 while ( ((phy_read(1) & 0x24)!=0x24) && (currticks() < to))
368 ;
369
370 if ( (phy_read(1) & 0x24) == 0x24 ) {
371 if (phy_read(17) & 0xa000)
372 csr6 |= 0x00000200;
373 } else
374 csr6 |= 0x00040000;
375 }
376
377
378 outl(csr6, ioaddr + CSR6);
379
380
381 if (csr6 & 0x40000)
382 HPNA_process();
383}
384
385
386
387
388
389
390
391
392
393static int read_eeprom(unsigned long ioaddr, int location, int addr_len)
394{
395 int i;
396 unsigned short retval = 0;
397 long ee_addr = ioaddr + CSR9;
398 int read_cmd = location | EE_READ_CMD;
399
400 whereami("read_eeprom\n");
401
402 outl(EE_ENB & ~EE_CS, ee_addr);
403 outl(EE_ENB, ee_addr);
404
405
406 for (i = 4 + addr_len; i >= 0; i--) {
407 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
408 outl(EE_ENB | dataval, ee_addr);
409 eeprom_delay();
410 outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
411 eeprom_delay();
412 }
413 outl(EE_ENB, ee_addr);
414
415 for (i = 16; i > 0; i--) {
416 outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
417 eeprom_delay();
418 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
419 outl(EE_ENB, ee_addr);
420 eeprom_delay();
421 }
422
423
424 outl(EE_ENB & ~EE_CS, ee_addr);
425 return retval;
426}
427
428
429
430
431
432static void davicom_init_chain(struct nic *nic)
433{
434 int i;
435
436
437
438
439 for (i=0; i<NTXD; i++) {
440 txd[i].buf1addr = (void *)virt_to_bus(&txb[0]);
441 txd[i].buf2addr = (void *)virt_to_bus(&txd[i+1]);
442 txd[i].buf1sz = 0;
443 txd[i].buf2sz = 0;
444 txd[i].control = 0x184;
445 txd[i].status = 0x00000000;
446 }
447
448
449
450 for (i=0; i<192; i++) txb[i] = 0xFF;
451 txb[0] = nic->node_addr[0];
452 txb[1] = nic->node_addr[1];
453 txb[4] = nic->node_addr[2];
454 txb[5] = nic->node_addr[3];
455 txb[8] = nic->node_addr[4];
456 txb[9] = nic->node_addr[5];
457
458
459 for (i=0; i<NRXD; i++) {
460 rxd[i].buf1addr = (void *)virt_to_bus(&rxb[i * BUFLEN]);
461 rxd[i].buf2addr = (void *)virt_to_bus(&rxd[i+1]);
462 rxd[i].buf1sz = BUFLEN;
463 rxd[i].buf2sz = 0;
464 rxd[i].control = 0x4;
465 rxd[i].status = 0x80000000;
466 }
467
468
469 txd[NTXD - 1].buf2addr = (void *)virt_to_bus(&txd[0]);
470 rxd[NRXD - 1].buf2addr = (void *)virt_to_bus(&rxd[0]);
471 TxPtr = 0;
472 rxd_tail = 0;
473}
474
475
476
477
478
479static void davicom_reset(struct nic *nic)
480{
481 unsigned long to;
482
483 whereami("davicom_reset\n");
484
485
486 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
487
488
489 outl(0x00000001, ioaddr + CSR0);
490
491 davicom_wait(TICKS_PER_SEC);
492
493
494 outl(0x0C00000, ioaddr + CSR0);
495
496
497 davicom_init_chain(nic);
498
499
500 outl(virt_to_bus(&rxd[0]), ioaddr + CSR3);
501 outl(virt_to_bus(&txd[0]), ioaddr + CSR4);
502
503
504
505 davicom_media_chk(nic);
506
507
508 txd[TxPtr].buf1sz = 192;
509 txd[TxPtr].control = 0x024;
510 txd[TxPtr].status = 0x80000000;
511
512
513 outl(inl(ioaddr + CSR6) | 0x00002000, ioaddr + CSR6);
514
515 outl(0, ioaddr + CSR1);
516
517 to = currticks() + TX_TIME_OUT;
518 while ((txd[TxPtr].status & 0x80000000) && (currticks() < to))
519 ;
520
521 if (currticks() >= to) {
522 printf ("TX Setup Timeout!\n");
523 }
524
525 TxPtr = (++TxPtr >= NTXD) ? 0:TxPtr;
526
527#ifdef DAVICOM_DEBUG
528 printf("txd.status = %X\n", txd.status);
529 printf("ticks = %d\n", currticks() - (to - TX_TIME_OUT));
530 davicom_more();
531#endif
532
533
534 outl(inl(ioaddr + CSR6) | 0x00000002, ioaddr + CSR6);
535
536 outl(0, ioaddr + CSR2);
537}
538
539
540
541
542
543static void davicom_transmit(struct nic *nic, const char *d, unsigned int t,
544 unsigned int s, const char *p)
545{
546 unsigned long to;
547
548 whereami("davicom_transmit\n");
549
550
551
552
553
554 memcpy(&txb[0], d, ETH_ALEN);
555 memcpy(&txb[ETH_ALEN], nic->node_addr, ETH_ALEN);
556 txb[ETH_ALEN*2] = (t >> 8) & 0xFF;
557 txb[ETH_ALEN*2+1] = t & 0xFF;
558 memcpy(&txb[ETH_HLEN], p, s);
559
560
561 txd[TxPtr].buf1sz = ETH_HLEN+s;
562 txd[TxPtr].control = 0x00000184;
563 txd[TxPtr].status = 0x80000000;
564
565
566 outl(0, ioaddr + CSR1);
567
568 to = currticks() + TX_TIME_OUT;
569 while ((txd[TxPtr].status & 0x80000000) && (currticks() < to))
570 ;
571
572 if (currticks() >= to) {
573 printf ("TX Timeout!\n");
574 }
575
576
577 TxPtr = (++TxPtr >= NTXD) ? 0:TxPtr;
578
579}
580
581
582
583
584static int davicom_poll(struct nic *nic, int retrieve)
585{
586 whereami("davicom_poll\n");
587
588 if (rxd[rxd_tail].status & 0x80000000)
589 return 0;
590
591 if ( ! retrieve ) return 1;
592
593 whereami("davicom_poll got one\n");
594
595 nic->packetlen = (rxd[rxd_tail].status & 0x3FFF0000) >> 16;
596
597 if( rxd[rxd_tail].status & 0x00008000){
598 rxd[rxd_tail].status = 0x80000000;
599 rxd_tail++;
600 if (rxd_tail == NRXD) rxd_tail = 0;
601 return 0;
602 }
603
604
605
606
607
608
609 memcpy(nic->packet, rxb + rxd_tail * BUFLEN, nic->packetlen);
610
611
612 rxd[rxd_tail].status = 0x80000000;
613 rxd_tail++;
614 if (rxd_tail == NRXD) rxd_tail = 0;
615
616 return 1;
617}
618
619
620
621
622static void davicom_disable ( struct nic *nic ) {
623
624 whereami("davicom_disable\n");
625
626 davicom_reset(nic);
627
628
629 outl(0x00000000, ioaddr + CSR7);
630
631
632 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
633
634
635 inl(ioaddr + CSR8);
636}
637
638
639
640
641
642static void davicom_irq(struct nic *nic __unused, irq_action_t action __unused)
643{
644 switch ( action ) {
645 case DISABLE :
646 break;
647 case ENABLE :
648 break;
649 case FORCE :
650 break;
651 }
652}
653
654
655
656
657
658static int davicom_probe ( struct nic *nic, struct pci_device *pci ) {
659
660 unsigned int i;
661
662 whereami("davicom_probe\n");
663
664 if (pci->ioaddr == 0)
665 return 0;
666
667 vendor = pci->vendor;
668 dev_id = pci->device;
669 ioaddr = pci->ioaddr;
670
671 nic->ioaddr = pci->ioaddr;
672 nic->irqno = 0;
673
674
675 pci_write_config_dword(pci, 0x40, 0x00000000);
676
677
678 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
679
680
681 inl(ioaddr + CSR8);
682
683
684
685 for (i = 0; i < sizeof(ee_data)/2; i++)
686 ((unsigned short *)ee_data)[i] =
687 le16_to_cpu(read_eeprom(ioaddr, i, EEPROM_ADDRLEN));
688
689
690 for (i=0; i<ETH_ALEN; i++)
691 nic->node_addr[i] = ee_data[20+i];
692
693 DBG ( "Davicom %s at IOADDR %4.4lx\n", eth_ntoa ( nic->node_addr ), ioaddr );
694
695
696 davicom_reset(nic);
697 nic->nic_op = &davicom_operations;
698 return 1;
699}
700
701static struct nic_operations davicom_operations = {
702 .connect = dummy_connect,
703 .poll = davicom_poll,
704 .transmit = davicom_transmit,
705 .irq = davicom_irq,
706
707};
708
709static struct pci_device_id davicom_nics[] = {
710PCI_ROM(0x1282, 0x9100, "davicom9100", "Davicom 9100", 0),
711PCI_ROM(0x1282, 0x9102, "davicom9102", "Davicom 9102", 0),
712PCI_ROM(0x1282, 0x9009, "davicom9009", "Davicom 9009", 0),
713PCI_ROM(0x1282, 0x9132, "davicom9132", "Davicom 9132", 0),
714};
715
716PCI_DRIVER ( davicom_driver, davicom_nics, PCI_NO_CLASS );
717
718DRIVER ( "DAVICOM", nic_driver, pci_driver, davicom_driver,
719 davicom_probe, davicom_disable );
720
721
722
723
724
725
726
727
728