00001 #ifdef ALLMULTI
00002 #error multicast support is not yet implemented
00003 #endif
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 FILE_LICENCE ( GPL_ANY );
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "etherboot.h"
00047 #include "nic.h"
00048 #include <gpxe/pci.h>
00049 #include <gpxe/ethernet.h>
00050
00051 #undef DAVICOM_DEBUG
00052 #undef DAVICOM_DEBUG_WHERE
00053
00054 #define TX_TIME_OUT 2*TICKS_PER_SEC
00055
00056
00057 enum davicom_offsets {
00058 CSR0=0, CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
00059 CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
00060 CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78, CSR16=0x80, CSR20=0xA0
00061 };
00062
00063
00064 #define EEPROM_ADDRLEN 6
00065 #define EEPROM_SIZE 32
00066
00067
00068
00069
00070 static unsigned char ee_data[EEPROM_SIZE];
00071
00072
00073 #define EE_WRITE_CMD (5 << addr_len)
00074 #define EE_READ_CMD (6 << addr_len)
00075 #define EE_ERASE_CMD (7 << addr_len)
00076
00077
00078 #define EE_SHIFT_CLK 0x02
00079 #define EE_CS 0x01
00080 #define EE_DATA_WRITE 0x04
00081 #define EE_WRITE_0 0x01
00082 #define EE_WRITE_1 0x05
00083 #define EE_DATA_READ 0x08
00084 #define EE_ENB (0x4800 | EE_CS)
00085
00086
00087 #define PHY_DATA_0 0x0
00088 #define PHY_DATA_1 0x20000
00089 #define MDCLKH 0x10000
00090
00091
00092
00093
00094 #define eeprom_delay() inl(ee_addr)
00095
00096
00097
00098
00099
00100
00101
00102
00103 struct txdesc {
00104 volatile unsigned long status;
00105 unsigned long buf1sz:11,
00106 buf2sz:11,
00107 control:10;
00108 const unsigned char *buf1addr;
00109 const unsigned char *buf2addr;
00110 };
00111
00112 struct rxdesc {
00113 volatile unsigned long status;
00114 unsigned long buf1sz:11,
00115 buf2sz:11,
00116 control:10;
00117 unsigned char *buf1addr;
00118 unsigned char *buf2addr;
00119 };
00120
00121
00122 #define BUFLEN 1536
00123
00124
00125
00126
00127
00128 static struct nic_operations davicom_operations;
00129
00130
00131 static unsigned short vendor, dev_id;
00132 static unsigned long ioaddr;
00133
00134
00135
00136
00137
00138 #define NTXD 2
00139 #define NRXD 4
00140 struct {
00141 struct txdesc txd[NTXD] __attribute__ ((aligned(4)));
00142 unsigned char txb[BUFLEN] __attribute__ ((aligned(4)));
00143 struct rxdesc rxd[NRXD] __attribute__ ((aligned(4)));
00144 unsigned char rxb[NRXD * BUFLEN] __attribute__ ((aligned(4)));
00145 } davicom_bufs __shared;
00146 #define txd davicom_bufs.txd
00147 #define txb davicom_bufs.txb
00148 #define rxd davicom_bufs.rxd
00149 #define rxb davicom_bufs.rxb
00150 static int rxd_tail;
00151 static int TxPtr;
00152
00153
00154
00155
00156
00157 static void whereami(const char *str);
00158 static int read_eeprom(unsigned long ioaddr, int location, int addr_len);
00159 static int davicom_probe(struct nic *nic,struct pci_device *pci);
00160 static void davicom_init_chain(struct nic *nic);
00161 static void davicom_reset(struct nic *nic);
00162 static void davicom_transmit(struct nic *nic, const char *d, unsigned int t,
00163 unsigned int s, const char *p);
00164 static int davicom_poll(struct nic *nic, int retrieve);
00165 static void davicom_disable(struct nic *nic);
00166 #ifdef DAVICOM_DEBUG
00167 static void davicom_more(void);
00168 #endif
00169 static void davicom_wait(unsigned int nticks);
00170 static int phy_read(int);
00171 static void phy_write(int, u16);
00172 static void phy_write_1bit(u32, u32);
00173 static int phy_read_1bit(u32);
00174 static void davicom_media_chk(struct nic *);
00175
00176
00177
00178
00179
00180 static inline void whereami(const char *str)
00181 {
00182 printf("%s\n", str);
00183
00184 }
00185
00186 #ifdef DAVICOM_DEBUG
00187 static void davicom_more()
00188 {
00189 printf("\n\n-- more --");
00190 while (!iskey())
00191 ;
00192 getchar();
00193 printf("\n\n");
00194 }
00195 #endif
00196
00197 static void davicom_wait(unsigned int nticks)
00198 {
00199 unsigned int to = currticks() + nticks;
00200 while (currticks() < to)
00201 ;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 static int phy_read(int location)
00212 {
00213 int i, phy_addr=1;
00214 u16 phy_data;
00215 u32 io_dcr9;
00216
00217 whereami("phy_read\n");
00218
00219 io_dcr9 = ioaddr + CSR9;
00220
00221
00222 for (i=0; i<34; i++)
00223 phy_write_1bit(io_dcr9, PHY_DATA_1);
00224
00225
00226 phy_write_1bit(io_dcr9, PHY_DATA_0);
00227 phy_write_1bit(io_dcr9, PHY_DATA_1);
00228
00229
00230 phy_write_1bit(io_dcr9, PHY_DATA_1);
00231 phy_write_1bit(io_dcr9, PHY_DATA_0);
00232
00233
00234 for (i=0x10; i>0; i=i>>1)
00235 phy_write_1bit(io_dcr9, phy_addr&i ? PHY_DATA_1: PHY_DATA_0);
00236
00237
00238 for (i=0x10; i>0; i=i>>1)
00239 phy_write_1bit(io_dcr9, location&i ? PHY_DATA_1: PHY_DATA_0);
00240
00241
00242 phy_read_1bit(io_dcr9);
00243
00244
00245 for (phy_data=0, i=0; i<16; i++) {
00246 phy_data<<=1;
00247 phy_data|=phy_read_1bit(io_dcr9);
00248 }
00249
00250 return phy_data;
00251 }
00252
00253
00254
00255
00256 static void phy_write(int location, u16 phy_data)
00257 {
00258 u16 i, phy_addr=1;
00259 u32 io_dcr9;
00260
00261 whereami("phy_write\n");
00262
00263 io_dcr9 = ioaddr + CSR9;
00264
00265
00266 for (i=0; i<34; i++)
00267 phy_write_1bit(io_dcr9, PHY_DATA_1);
00268
00269
00270 phy_write_1bit(io_dcr9, PHY_DATA_0);
00271 phy_write_1bit(io_dcr9, PHY_DATA_1);
00272
00273
00274 phy_write_1bit(io_dcr9, PHY_DATA_0);
00275 phy_write_1bit(io_dcr9, PHY_DATA_1);
00276
00277
00278 for (i=0x10; i>0; i=i>>1)
00279 phy_write_1bit(io_dcr9, phy_addr&i ? PHY_DATA_1: PHY_DATA_0);
00280
00281
00282 for (i=0x10; i>0; i=i>>1)
00283 phy_write_1bit(io_dcr9, location&i ? PHY_DATA_1: PHY_DATA_0);
00284
00285
00286 phy_write_1bit(io_dcr9, PHY_DATA_1);
00287 phy_write_1bit(io_dcr9, PHY_DATA_0);
00288
00289
00290 for (i=0x8000; i>0; i>>=1)
00291 phy_write_1bit(io_dcr9, phy_data&i ? PHY_DATA_1: PHY_DATA_0);
00292 }
00293
00294
00295
00296
00297 static void phy_write_1bit(u32 ee_addr, u32 phy_data)
00298 {
00299 whereami("phy_write_1bit\n");
00300 outl(phy_data, ee_addr);
00301 eeprom_delay();
00302 outl(phy_data|MDCLKH, ee_addr);
00303 eeprom_delay();
00304 outl(phy_data, ee_addr);
00305 eeprom_delay();
00306 }
00307
00308
00309
00310
00311 static int phy_read_1bit(u32 ee_addr)
00312 {
00313 int phy_data;
00314
00315 whereami("phy_read_1bit\n");
00316
00317 outl(0x50000, ee_addr);
00318 eeprom_delay();
00319
00320 phy_data=(inl(ee_addr)>>19) & 0x1;
00321
00322 outl(0x40000, ee_addr);
00323 eeprom_delay();
00324
00325 return phy_data;
00326 }
00327
00328
00329
00330
00331 static void HPNA_process(void)
00332 {
00333
00334 if ( (phy_read(3) & 0xfff0) == 0xb900 ) {
00335 if ( phy_read(31) == 0x4404 ) {
00336
00337 if (phy_read(3) == 0xb901)
00338 phy_write(16, 0x5);
00339 else
00340 phy_write(16, 0x1005);
00341 phy_write(25, ((phy_read(24) + 3) & 0xff) | 0xf000);
00342 } else {
00343
00344 phy_write(16, 0x5);
00345 phy_write(25, (phy_read(25) & 0xff00) + 2);
00346 }
00347 }
00348 }
00349
00350
00351
00352
00353 static void davicom_media_chk(struct nic * nic __unused)
00354 {
00355 unsigned long to, csr6;
00356
00357 csr6 = 0x00200000;
00358 outl(csr6, ioaddr + CSR6);
00359
00360 #define PCI_DEVICE_ID_DM9009 0x9009
00361 if (vendor == PCI_VENDOR_ID_DAVICOM && dev_id == PCI_DEVICE_ID_DM9009) {
00362
00363 phy_write(0, 0);
00364 } else {
00365
00366 to = currticks() + 2 * TICKS_PER_SEC;
00367 while ( ((phy_read(1) & 0x24)!=0x24) && (currticks() < to))
00368 ;
00369
00370 if ( (phy_read(1) & 0x24) == 0x24 ) {
00371 if (phy_read(17) & 0xa000)
00372 csr6 |= 0x00000200;
00373 } else
00374 csr6 |= 0x00040000;
00375 }
00376
00377
00378 outl(csr6, ioaddr + CSR6);
00379
00380
00381 if (csr6 & 0x40000)
00382 HPNA_process();
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 static int read_eeprom(unsigned long ioaddr, int location, int addr_len)
00394 {
00395 int i;
00396 unsigned short retval = 0;
00397 long ee_addr = ioaddr + CSR9;
00398 int read_cmd = location | EE_READ_CMD;
00399
00400 whereami("read_eeprom\n");
00401
00402 outl(EE_ENB & ~EE_CS, ee_addr);
00403 outl(EE_ENB, ee_addr);
00404
00405
00406 for (i = 4 + addr_len; i >= 0; i--) {
00407 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
00408 outl(EE_ENB | dataval, ee_addr);
00409 eeprom_delay();
00410 outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
00411 eeprom_delay();
00412 }
00413 outl(EE_ENB, ee_addr);
00414
00415 for (i = 16; i > 0; i--) {
00416 outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
00417 eeprom_delay();
00418 retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
00419 outl(EE_ENB, ee_addr);
00420 eeprom_delay();
00421 }
00422
00423
00424 outl(EE_ENB & ~EE_CS, ee_addr);
00425 return retval;
00426 }
00427
00428
00429
00430
00431
00432 static void davicom_init_chain(struct nic *nic)
00433 {
00434 int i;
00435
00436
00437
00438
00439 for (i=0; i<NTXD; i++) {
00440 txd[i].buf1addr = (void *)virt_to_bus(&txb[0]);
00441 txd[i].buf2addr = (void *)virt_to_bus(&txd[i+1]);
00442 txd[i].buf1sz = 0;
00443 txd[i].buf2sz = 0;
00444 txd[i].control = 0x184;
00445 txd[i].status = 0x00000000;
00446 }
00447
00448
00449
00450 for (i=0; i<192; i++) txb[i] = 0xFF;
00451 txb[0] = nic->node_addr[0];
00452 txb[1] = nic->node_addr[1];
00453 txb[4] = nic->node_addr[2];
00454 txb[5] = nic->node_addr[3];
00455 txb[8] = nic->node_addr[4];
00456 txb[9] = nic->node_addr[5];
00457
00458
00459 for (i=0; i<NRXD; i++) {
00460 rxd[i].buf1addr = (void *)virt_to_bus(&rxb[i * BUFLEN]);
00461 rxd[i].buf2addr = (void *)virt_to_bus(&rxd[i+1]);
00462 rxd[i].buf1sz = BUFLEN;
00463 rxd[i].buf2sz = 0;
00464 rxd[i].control = 0x4;
00465 rxd[i].status = 0x80000000;
00466 }
00467
00468
00469 txd[NTXD - 1].buf2addr = (void *)virt_to_bus(&txd[0]);
00470 rxd[NRXD - 1].buf2addr = (void *)virt_to_bus(&rxd[0]);
00471 TxPtr = 0;
00472 rxd_tail = 0;
00473 }
00474
00475
00476
00477
00478
00479 static void davicom_reset(struct nic *nic)
00480 {
00481 unsigned long to;
00482
00483 whereami("davicom_reset\n");
00484
00485
00486 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
00487
00488
00489 outl(0x00000001, ioaddr + CSR0);
00490
00491 davicom_wait(TICKS_PER_SEC);
00492
00493
00494 outl(0x0C00000, ioaddr + CSR0);
00495
00496
00497 davicom_init_chain(nic);
00498
00499
00500 outl(virt_to_bus(&rxd[0]), ioaddr + CSR3);
00501 outl(virt_to_bus(&txd[0]), ioaddr + CSR4);
00502
00503
00504
00505 davicom_media_chk(nic);
00506
00507
00508 txd[TxPtr].buf1sz = 192;
00509 txd[TxPtr].control = 0x024;
00510 txd[TxPtr].status = 0x80000000;
00511
00512
00513 outl(inl(ioaddr + CSR6) | 0x00002000, ioaddr + CSR6);
00514
00515 outl(0, ioaddr + CSR1);
00516
00517 to = currticks() + TX_TIME_OUT;
00518 while ((txd[TxPtr].status & 0x80000000) && (currticks() < to))
00519 ;
00520
00521 if (currticks() >= to) {
00522 printf ("TX Setup Timeout!\n");
00523 }
00524
00525 TxPtr = (++TxPtr >= NTXD) ? 0:TxPtr;
00526
00527 #ifdef DAVICOM_DEBUG
00528 printf("txd.status = %X\n", txd.status);
00529 printf("ticks = %d\n", currticks() - (to - TX_TIME_OUT));
00530 davicom_more();
00531 #endif
00532
00533
00534 outl(inl(ioaddr + CSR6) | 0x00000002, ioaddr + CSR6);
00535
00536 outl(0, ioaddr + CSR2);
00537 }
00538
00539
00540
00541
00542
00543 static void davicom_transmit(struct nic *nic, const char *d, unsigned int t,
00544 unsigned int s, const char *p)
00545 {
00546 unsigned long to;
00547
00548 whereami("davicom_transmit\n");
00549
00550
00551
00552
00553
00554 memcpy(&txb[0], d, ETH_ALEN);
00555 memcpy(&txb[ETH_ALEN], nic->node_addr, ETH_ALEN);
00556 txb[ETH_ALEN*2] = (t >> 8) & 0xFF;
00557 txb[ETH_ALEN*2+1] = t & 0xFF;
00558 memcpy(&txb[ETH_HLEN], p, s);
00559
00560
00561 txd[TxPtr].buf1sz = ETH_HLEN+s;
00562 txd[TxPtr].control = 0x00000184;
00563 txd[TxPtr].status = 0x80000000;
00564
00565
00566 outl(0, ioaddr + CSR1);
00567
00568 to = currticks() + TX_TIME_OUT;
00569 while ((txd[TxPtr].status & 0x80000000) && (currticks() < to))
00570 ;
00571
00572 if (currticks() >= to) {
00573 printf ("TX Timeout!\n");
00574 }
00575
00576
00577 TxPtr = (++TxPtr >= NTXD) ? 0:TxPtr;
00578
00579 }
00580
00581
00582
00583
00584 static int davicom_poll(struct nic *nic, int retrieve)
00585 {
00586 whereami("davicom_poll\n");
00587
00588 if (rxd[rxd_tail].status & 0x80000000)
00589 return 0;
00590
00591 if ( ! retrieve ) return 1;
00592
00593 whereami("davicom_poll got one\n");
00594
00595 nic->packetlen = (rxd[rxd_tail].status & 0x3FFF0000) >> 16;
00596
00597 if( rxd[rxd_tail].status & 0x00008000){
00598 rxd[rxd_tail].status = 0x80000000;
00599 rxd_tail++;
00600 if (rxd_tail == NRXD) rxd_tail = 0;
00601 return 0;
00602 }
00603
00604
00605
00606
00607
00608
00609 memcpy(nic->packet, rxb + rxd_tail * BUFLEN, nic->packetlen);
00610
00611
00612 rxd[rxd_tail].status = 0x80000000;
00613 rxd_tail++;
00614 if (rxd_tail == NRXD) rxd_tail = 0;
00615
00616 return 1;
00617 }
00618
00619
00620
00621
00622 static void davicom_disable ( struct nic *nic ) {
00623
00624 whereami("davicom_disable\n");
00625
00626 davicom_reset(nic);
00627
00628
00629 outl(0x00000000, ioaddr + CSR7);
00630
00631
00632 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
00633
00634
00635 inl(ioaddr + CSR8);
00636 }
00637
00638
00639
00640
00641
00642 static void davicom_irq(struct nic *nic __unused, irq_action_t action __unused)
00643 {
00644 switch ( action ) {
00645 case DISABLE :
00646 break;
00647 case ENABLE :
00648 break;
00649 case FORCE :
00650 break;
00651 }
00652 }
00653
00654
00655
00656
00657
00658 static int davicom_probe ( struct nic *nic, struct pci_device *pci ) {
00659
00660 unsigned int i;
00661
00662 whereami("davicom_probe\n");
00663
00664 if (pci->ioaddr == 0)
00665 return 0;
00666
00667 vendor = pci->vendor;
00668 dev_id = pci->device;
00669 ioaddr = pci->ioaddr;
00670
00671 nic->ioaddr = pci->ioaddr;
00672 nic->irqno = 0;
00673
00674
00675 pci_write_config_dword(pci, 0x40, 0x00000000);
00676
00677
00678 outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
00679
00680
00681 inl(ioaddr + CSR8);
00682
00683
00684
00685 for (i = 0; i < sizeof(ee_data)/2; i++)
00686 ((unsigned short *)ee_data)[i] =
00687 le16_to_cpu(read_eeprom(ioaddr, i, EEPROM_ADDRLEN));
00688
00689
00690 for (i=0; i<ETH_ALEN; i++)
00691 nic->node_addr[i] = ee_data[20+i];
00692
00693 DBG ( "Davicom %s at IOADDR %4.4lx\n", eth_ntoa ( nic->node_addr ), ioaddr );
00694
00695
00696 davicom_reset(nic);
00697 nic->nic_op = &davicom_operations;
00698 return 1;
00699 }
00700
00701 static struct nic_operations davicom_operations = {
00702 .connect = dummy_connect,
00703 .poll = davicom_poll,
00704 .transmit = davicom_transmit,
00705 .irq = davicom_irq,
00706
00707 };
00708
00709 static struct pci_device_id davicom_nics[] = {
00710 PCI_ROM(0x1282, 0x9100, "davicom9100", "Davicom 9100", 0),
00711 PCI_ROM(0x1282, 0x9102, "davicom9102", "Davicom 9102", 0),
00712 PCI_ROM(0x1282, 0x9009, "davicom9009", "Davicom 9009", 0),
00713 PCI_ROM(0x1282, 0x9132, "davicom9132", "Davicom 9132", 0),
00714 };
00715
00716 PCI_DRIVER ( davicom_driver, davicom_nics, PCI_NO_CLASS );
00717
00718 DRIVER ( "DAVICOM", nic_driver, pci_driver, davicom_driver,
00719 davicom_probe, davicom_disable );
00720
00721
00722
00723
00724
00725
00726
00727