ether82557.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Intel 82557 Fast Ethernet PCI Bus LAN Controller
  11. * as found on the Intel EtherExpress PRO/100B. This chip is full
  12. * of smarts, unfortunately none of them are in the right place.
  13. * To do:
  14. * the PCI scanning code could be made common to other adapters;
  15. * PCI code needs rewritten to handle byte, word, dword accesses
  16. * and using the devno as a bus+dev+function triplet.
  17. */
  18. #include "u.h"
  19. #include "lib.h"
  20. #include "mem.h"
  21. #include "dat.h"
  22. #include "fns.h"
  23. #include "io.h"
  24. #include "etherif.h"
  25. enum {
  26. Nrfd = 4, /* receive frame area */
  27. NullPointer = 0xFFFFFFFF, /* 82557 NULL pointer */
  28. };
  29. enum { /* CSR */
  30. Status = 0x00, /* byte or word (word includes Ack) */
  31. Ack = 0x01, /* byte */
  32. CommandR = 0x02, /* byte or word (word includes Interrupt) */
  33. Interrupt = 0x03, /* byte */
  34. Pointer = 0x04, /* dword */
  35. Port = 0x08, /* dword */
  36. Fcr = 0x0C, /* Flash control register */
  37. Ecr = 0x0E, /* EEPROM control register */
  38. Mcr = 0x10, /* MDI control register */
  39. };
  40. enum { /* Status */
  41. RUidle = 0x0000,
  42. RUsuspended = 0x0004,
  43. RUnoresources = 0x0008,
  44. RUready = 0x0010,
  45. RUrbd = 0x0020, /* bit */
  46. RUstatus = 0x003F, /* mask */
  47. CUidle = 0x0000,
  48. CUsuspended = 0x0040,
  49. CUactive = 0x0080,
  50. CUstatus = 0x00C0, /* mask */
  51. StatSWI = 0x0400, /* SoftWare generated Interrupt */
  52. StatMDI = 0x0800, /* MDI r/w done */
  53. StatRNR = 0x1000, /* Receive unit Not Ready */
  54. StatCNA = 0x2000, /* Command unit Not Active (Active->Idle) */
  55. StatFR = 0x4000, /* Finished Receiving */
  56. StatCX = 0x8000, /* Command eXecuted */
  57. StatTNO = 0x8000, /* Transmit NOT OK */
  58. };
  59. enum { /* Command (byte) */
  60. CUnop = 0x00,
  61. CUstart = 0x10,
  62. CUresume = 0x20,
  63. LoadDCA = 0x40, /* Load Dump Counters Address */
  64. DumpSC = 0x50, /* Dump Statistical Counters */
  65. LoadCUB = 0x60, /* Load CU Base */
  66. ResetSA = 0x70, /* Dump and Reset Statistical Counters */
  67. RUstart = 0x01,
  68. RUresume = 0x02,
  69. RUabort = 0x04,
  70. LoadHDS = 0x05, /* Load Header Data Size */
  71. LoadRUB = 0x06, /* Load RU Base */
  72. RBDresume = 0x07, /* Resume frame reception */
  73. };
  74. enum { /* Interrupt (byte) */
  75. InterruptM = 0x01, /* interrupt Mask */
  76. InterruptSI = 0x02, /* Software generated Interrupt */
  77. };
  78. enum { /* Ecr */
  79. EEsk = 0x01, /* serial clock */
  80. EEcs = 0x02, /* chip select */
  81. EEdi = 0x04, /* serial data in */
  82. EEdo = 0x08, /* serial data out */
  83. EEstart = 0x04, /* start bit */
  84. EEread = 0x02, /* read opcode */
  85. };
  86. enum { /* Mcr */
  87. MDIread = 0x08000000, /* read opcode */
  88. MDIwrite = 0x04000000, /* write opcode */
  89. MDIready = 0x10000000, /* ready bit */
  90. MDIie = 0x20000000, /* interrupt enable */
  91. };
  92. typedef struct Rfd {
  93. int field;
  94. ulong link;
  95. ulong rbd;
  96. ushort count;
  97. ushort size;
  98. Etherpkt;
  99. } Rfd;
  100. enum { /* field */
  101. RfdCollision = 0x00000001,
  102. RfdIA = 0x00000002, /* IA match */
  103. RfdRxerr = 0x00000010, /* PHY character error */
  104. RfdType = 0x00000020, /* Type frame */
  105. RfdRunt = 0x00000080,
  106. RfdOverrun = 0x00000100,
  107. RfdBuffer = 0x00000200,
  108. RfdAlignment = 0x00000400,
  109. RfdCRC = 0x00000800,
  110. RfdOK = 0x00002000, /* frame received OK */
  111. RfdC = 0x00008000, /* reception Complete */
  112. RfdSF = 0x00080000, /* Simplified or Flexible (1) Rfd */
  113. RfdH = 0x00100000, /* Header RFD */
  114. RfdI = 0x20000000, /* Interrupt after completion */
  115. RfdS = 0x40000000, /* Suspend after completion */
  116. RfdEL = 0x80000000, /* End of List */
  117. };
  118. enum { /* count */
  119. RfdF = 0x00004000,
  120. RfdEOF = 0x00008000,
  121. };
  122. typedef struct Cb {
  123. int command;
  124. uint32_t link;
  125. uint8_t data[24]; /* CbIAS + CbConfigure */
  126. } Cb;
  127. typedef struct TxCB {
  128. int command;
  129. uint32_t link;
  130. uint32_t tbd;
  131. uint16_t count;
  132. uint8_t threshold;
  133. uint8_t number;
  134. } TxCB;
  135. enum { /* action command */
  136. CbOK = 0x00002000, /* DMA completed OK */
  137. CbC = 0x00008000, /* execution Complete */
  138. CbNOP = 0x00000000,
  139. CbIAS = 0x00010000, /* Indvidual Address Setup */
  140. CbConfigure = 0x00020000,
  141. CbMAS = 0x00030000, /* Multicast Address Setup */
  142. CbTransmit = 0x00040000,
  143. CbDump = 0x00060000,
  144. CbDiagnose = 0x00070000,
  145. CbCommand = 0x00070000, /* mask */
  146. CbSF = 0x00080000, /* CbTransmit */
  147. CbI = 0x20000000, /* Interrupt after completion */
  148. CbS = 0x40000000, /* Suspend after completion */
  149. CbEL = 0x80000000, /* End of List */
  150. };
  151. enum { /* CbTransmit count */
  152. CbEOF = 0x00008000,
  153. };
  154. typedef struct Ctlr Ctlr;
  155. typedef struct Ctlr {
  156. int port;
  157. Pcidev* pcidev;
  158. Ctlr* next;
  159. int active;
  160. int eepromsz; /* address size in bits */
  161. uint16_t* eeprom;
  162. int ctlrno;
  163. char* type;
  164. uint8_t configdata[24];
  165. Rfd rfd[Nrfd];
  166. int rfdl;
  167. int rfdx;
  168. Block* cbqhead;
  169. Block* cbqtail;
  170. int cbqbusy;
  171. } Ctlr;
  172. static Ctlr* ctlrhead;
  173. static Ctlr* ctlrtail;
  174. static uint8_t configdata[24] = {
  175. 0x16, /* byte count */
  176. 0x44, /* Rx/Tx FIFO limit */
  177. 0x00, /* adaptive IFS */
  178. 0x00,
  179. 0x04, /* Rx DMA maximum byte count */
  180. 0x84, /* Tx DMA maximum byte count */
  181. 0x33, /* late SCB, CNA interrupts */
  182. 0x01, /* discard short Rx frames */
  183. 0x00, /* 503/MII */
  184. 0x00,
  185. 0x2E, /* normal operation, NSAI */
  186. 0x00, /* linear priority */
  187. 0x60, /* inter-frame spacing */
  188. 0x00,
  189. 0xF2,
  190. 0x48, /* promiscuous mode off */
  191. 0x00,
  192. 0x40,
  193. 0xF2, /* transmit padding enable */
  194. 0x80, /* full duplex pin enable */
  195. 0x3F, /* no Multi IA */
  196. 0x05, /* no Multi Cast ALL */
  197. };
  198. #define csr8r(c, r) (inb((c)->port+(r)))
  199. #define csr16r(c, r) (ins((c)->port+(r)))
  200. #define csr32r(c, r) (inl((c)->port+(r)))
  201. #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
  202. #define csr16w(c, r, w) (outs((c)->port+(r), (uint16_t)(w)))
  203. #define csr32w(c, r, l) (outl((c)->port+(r), (uint32_t)(l)))
  204. static void
  205. custart(Ctlr* ctlr)
  206. {
  207. if(ctlr->cbqhead == 0){
  208. ctlr->cbqbusy = 0;
  209. return;
  210. }
  211. ctlr->cbqbusy = 1;
  212. csr32w(ctlr, Pointer, PADDR(ctlr->cbqhead->rp));
  213. while(csr8r(ctlr, CommandR))
  214. ;
  215. csr8w(ctlr, CommandR, CUstart);
  216. }
  217. static void
  218. action(Ctlr* ctlr, Block* bp)
  219. {
  220. Cb *cb;
  221. cb = (Cb*)bp->rp;
  222. cb->command |= CbEL;
  223. if(ctlr->cbqhead){
  224. ctlr->cbqtail->next = bp;
  225. cb = (Cb*)ctlr->cbqtail->rp;
  226. cb->link = PADDR(bp->rp);
  227. cb->command &= ~CbEL;
  228. }
  229. else
  230. ctlr->cbqhead = bp;
  231. ctlr->cbqtail = bp;
  232. if(ctlr->cbqbusy == 0)
  233. custart(ctlr);
  234. }
  235. static void
  236. attach(Ether* ether)
  237. {
  238. int status;
  239. Ctlr *ctlr;
  240. ctlr = ether->ctlr;
  241. status = csr16r(ctlr, Status);
  242. if((status & RUstatus) == RUidle){
  243. csr32w(ctlr, Pointer, PADDR(&ctlr->rfd[ctlr->rfdx]));
  244. while(csr8r(ctlr, CommandR))
  245. ;
  246. csr8w(ctlr, CommandR, RUstart);
  247. }
  248. }
  249. static void
  250. configure(void* arg, int promiscuous)
  251. {
  252. Ctlr *ctlr;
  253. Block *bp;
  254. Cb *cb;
  255. ctlr = ((Ether*)arg)->ctlr;
  256. bp = allocb(sizeof(Cb));
  257. cb = (Cb*)bp->rp;
  258. bp->wp += sizeof(Cb);
  259. cb->command = CbConfigure;
  260. cb->link = NullPointer;
  261. memmove(cb->data, ctlr->configdata, sizeof(ctlr->configdata));
  262. if(promiscuous)
  263. cb->data[15] |= 0x01;
  264. action(ctlr, bp);
  265. }
  266. static void
  267. transmit(Ether* ether)
  268. {
  269. Block *bp;
  270. TxCB *txcb;
  271. RingBuf *tb;
  272. for(tb = &ether->tb[ether->ti]; tb->owner == Interface; tb = &ether->tb[ether->ti]){
  273. bp = allocb(tb->len+sizeof(TxCB));
  274. txcb = (TxCB*)bp->wp;
  275. bp->wp += sizeof(TxCB);
  276. txcb->command = CbTransmit;
  277. txcb->link = NullPointer;
  278. txcb->tbd = NullPointer;
  279. txcb->count = CbEOF|tb->len;
  280. txcb->threshold = 2;
  281. txcb->number = 0;
  282. memmove(bp->wp, tb->pkt, tb->len);
  283. memmove(bp->wp+Eaddrlen, ether->ea, Eaddrlen);
  284. bp->wp += tb->len;
  285. action(ether->ctlr, bp);
  286. tb->owner = Host;
  287. ether->ti = NEXT(ether->ti, ether->ntb);
  288. }
  289. }
  290. static void
  291. interrupt(Ureg*, void* arg)
  292. {
  293. Rfd *rfd;
  294. Block *bp;
  295. Ctlr *ctlr;
  296. Ether *ether;
  297. int status;
  298. RingBuf *rb;
  299. ether = arg;
  300. ctlr = ether->ctlr;
  301. for(;;){
  302. status = csr16r(ctlr, Status);
  303. csr8w(ctlr, Ack, (status>>8) & 0xFF);
  304. if((status & (StatCX|StatFR|StatCNA|StatRNR)) == 0)
  305. return;
  306. if(status & StatFR){
  307. rfd = &ctlr->rfd[ctlr->rfdx];
  308. while(rfd->field & RfdC){
  309. rb = &ether->rb[ether->ri];
  310. if(rb->owner == Interface){
  311. rb->owner = Host;
  312. rb->len = rfd->count & 0x3FFF;
  313. memmove(rb->pkt, rfd->d, rfd->count & 0x3FFF);
  314. ether->ri = NEXT(ether->ri, ether->nrb);
  315. }
  316. /*
  317. * Reinitialise the frame for reception and bump
  318. * the receive frame processing index;
  319. * bump the sentinel index, mark the new sentinel
  320. * and clear the old sentinel suspend bit;
  321. * set bp and rfd for the next receive frame to
  322. * process.
  323. */
  324. rfd->field = 0;
  325. rfd->count = 0;
  326. ctlr->rfdx = NEXT(ctlr->rfdx, Nrfd);
  327. rfd = &ctlr->rfd[ctlr->rfdl];
  328. ctlr->rfdl = NEXT(ctlr->rfdl, Nrfd);
  329. ctlr->rfd[ctlr->rfdl].field |= RfdS;
  330. rfd->field &= ~RfdS;
  331. rfd = &ctlr->rfd[ctlr->rfdx];
  332. }
  333. status &= ~StatFR;
  334. }
  335. if(status & StatRNR){
  336. while(csr8r(ctlr, CommandR))
  337. ;
  338. csr8w(ctlr, CommandR, RUresume);
  339. status &= ~StatRNR;
  340. }
  341. if(status & StatCNA){
  342. while(bp = ctlr->cbqhead){
  343. if((((Cb*)bp->rp)->command & CbC) == 0)
  344. break;
  345. ctlr->cbqhead = bp->next;
  346. freeb(bp);
  347. }
  348. custart(ctlr);
  349. status &= ~StatCNA;
  350. }
  351. if(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI))
  352. panic("%s#%d: status %uX\n", ctlr->type, ctlr->ctlrno, status);
  353. }
  354. }
  355. static void
  356. ctlrinit(Ctlr* ctlr)
  357. {
  358. int i;
  359. Rfd *rfd;
  360. uint32_t link;
  361. link = NullPointer;
  362. for(i = Nrfd-1; i >= 0; i--){
  363. rfd = &ctlr->rfd[i];
  364. rfd->field = 0;
  365. rfd->link = link;
  366. link = PADDR(rfd);
  367. rfd->rbd = NullPointer;
  368. rfd->count = 0;
  369. rfd->size = sizeof(Etherpkt);
  370. }
  371. ctlr->rfd[Nrfd-1].link = PADDR(&ctlr->rfd[0]);
  372. ctlr->rfdl = 0;
  373. ctlr->rfd[0].field |= RfdS;
  374. ctlr->rfdx = 2;
  375. memmove(ctlr->configdata, configdata, sizeof(configdata));
  376. }
  377. static int
  378. miir(Ctlr* ctlr, int phyadd, int regadd)
  379. {
  380. int mcr, timo;
  381. csr32w(ctlr, Mcr, MDIread|(phyadd<<21)|(regadd<<16));
  382. mcr = 0;
  383. for(timo = 64; timo; timo--){
  384. mcr = csr32r(ctlr, Mcr);
  385. if(mcr & MDIready)
  386. break;
  387. microdelay(1);
  388. }
  389. if(mcr & MDIready)
  390. return mcr & 0xFFFF;
  391. return -1;
  392. }
  393. static int
  394. miiw(Ctlr* ctlr, int phyadd, int regadd, int data)
  395. {
  396. int mcr, timo;
  397. csr32w(ctlr, Mcr, MDIwrite|(phyadd<<21)|(regadd<<16)|(data & 0xFFFF));
  398. mcr = 0;
  399. for(timo = 64; timo; timo--){
  400. mcr = csr32r(ctlr, Mcr);
  401. if(mcr & MDIready)
  402. break;
  403. microdelay(1);
  404. }
  405. if(mcr & MDIready)
  406. return 0;
  407. return -1;
  408. }
  409. static int
  410. hy93c46r(Ctlr* ctlr, int r)
  411. {
  412. int data, i, op, size;
  413. /*
  414. * Hyundai HY93C46 or equivalent serial EEPROM.
  415. * This sequence for reading a 16-bit register 'r'
  416. * in the EEPROM is taken straight from Section
  417. * 3.3.4.2 of the Intel 82557 User's Guide.
  418. */
  419. reread:
  420. csr16w(ctlr, Ecr, EEcs);
  421. op = EEstart|EEread;
  422. for(i = 2; i >= 0; i--){
  423. data = (((op>>i) & 0x01)<<2)|EEcs;
  424. csr16w(ctlr, Ecr, data);
  425. csr16w(ctlr, Ecr, data|EEsk);
  426. microdelay(1);
  427. csr16w(ctlr, Ecr, data);
  428. microdelay(1);
  429. }
  430. /*
  431. * First time through must work out the EEPROM size.
  432. */
  433. if((size = ctlr->eepromsz) == 0)
  434. size = 8;
  435. for(size = size-1; size >= 0; size--){
  436. data = (((r>>size) & 0x01)<<2)|EEcs;
  437. csr16w(ctlr, Ecr, data);
  438. csr16w(ctlr, Ecr, data|EEsk);
  439. delay(1);
  440. csr16w(ctlr, Ecr, data);
  441. microdelay(1);
  442. if(!(csr16r(ctlr, Ecr) & EEdo))
  443. break;
  444. }
  445. data = 0;
  446. for(i = 15; i >= 0; i--){
  447. csr16w(ctlr, Ecr, EEcs|EEsk);
  448. microdelay(1);
  449. if(csr16r(ctlr, Ecr) & EEdo)
  450. data |= (1<<i);
  451. csr16w(ctlr, Ecr, EEcs);
  452. microdelay(1);
  453. }
  454. csr16w(ctlr, Ecr, 0);
  455. if(ctlr->eepromsz == 0){
  456. ctlr->eepromsz = 8-size;
  457. ctlr->eeprom = malloc((1<<ctlr->eepromsz)*sizeof(uint16_t));
  458. goto reread;
  459. }
  460. return data;
  461. }
  462. static void
  463. i82557pci(void)
  464. {
  465. Pcidev *p;
  466. Ctlr *ctlr;
  467. p = nil;
  468. while(p = pcimatch(p, 0x8086, 0)){
  469. switch(p->did){
  470. default:
  471. continue;
  472. case 0x1031: /* Intel 82562EM */
  473. case 0x1050: /* Intel 82562EZ */
  474. case 0x1039: /* Intel 82801BD PRO/100 VE */
  475. case 0x103A: /* Intel 82562 PRO/100 VE */
  476. case 0x1064: /* Intel 82562 PRO/100 VE */
  477. case 0x2449: /* Intel 82562ET */
  478. case 0x1209: /* Intel 82559ER */
  479. case 0x1229: /* Intel 8255[789] */
  480. case 0x1030: /* Intel 82559 InBusiness 10/100 */
  481. break;
  482. }
  483. /*
  484. * bar[0] is the memory-mapped register address (4KB),
  485. * bar[1] is the I/O port register address (32 bytes) and
  486. * bar[2] is for the flash ROM (1MB).
  487. */
  488. ctlr = malloc(sizeof(Ctlr));
  489. ctlr->port = p->mem[1].bar & ~0x01;
  490. ctlr->pcidev = p;
  491. if(ctlrhead != nil)
  492. ctlrtail->next = ctlr;
  493. else
  494. ctlrhead = ctlr;
  495. ctlrtail = ctlr;
  496. pcisetbme(p);
  497. }
  498. }
  499. static void
  500. detach(Ether* ether)
  501. {
  502. Ctlr *ctlr;
  503. ctlr = ether->ctlr;
  504. csr32w(ctlr, Port, 0);
  505. delay(1);
  506. while(csr8r(ctlr, CommandR))
  507. ;
  508. pciclrbme(ctlr->pcidev);
  509. }
  510. static int
  511. scanphy(Ctlr* ctlr)
  512. {
  513. int i, oui, x;
  514. for(i = 0; i < 32; i++){
  515. if((oui = miir(ctlr, i, 2)) == -1 || oui == 0 || oui == 0xFFFF)
  516. continue;
  517. oui <<= 6;
  518. x = miir(ctlr, i, 3);
  519. oui |= x>>10;
  520. //print("phy%d: oui %uX reg1 %uX\n", i, oui, miir(ctlr, i, 1));
  521. if(oui == 0xAA00)
  522. ctlr->eeprom[6] = 0x07<<8;
  523. else if(oui == 0x80017){
  524. if(x & 0x01)
  525. ctlr->eeprom[6] = 0x0A<<8;
  526. else
  527. ctlr->eeprom[6] = 0x04<<8;
  528. }
  529. return i;
  530. }
  531. return -1;
  532. }
  533. int
  534. i82557reset(Ether* ether)
  535. {
  536. int anar, anlpar, bmcr, bmsr, force, i, phyaddr, x;
  537. unsigned short sum;
  538. Block *bp;
  539. uint8_t ea[Eaddrlen];
  540. Ctlr *ctlr;
  541. Cb *cb;
  542. if(ctlrhead == nil)
  543. i82557pci();
  544. /*
  545. * Any adapter matches if no ether->port is supplied,
  546. * otherwise the ports must match.
  547. */
  548. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  549. if(ctlr->active)
  550. continue;
  551. if(ether->port == 0 || ether->port == ctlr->port){
  552. ctlr->active = 1;
  553. break;
  554. }
  555. }
  556. if(ctlr == nil)
  557. return -1;
  558. /*
  559. * Initialise the Ctlr structure.
  560. * Perform a software reset after which need to ensure busmastering
  561. * is still enabled. The EtherExpress PRO/100B appears to leave
  562. * the PCI configuration alone (see the 'To do' list above) so punt
  563. * for now.
  564. * Load the RUB and CUB registers for linear addressing (0).
  565. */
  566. ether->ctlr = ctlr;
  567. ether->port = ctlr->port;
  568. ether->irq = ctlr->pcidev->intl;
  569. ether->tbdf = ctlr->pcidev->tbdf;
  570. ctlr->ctlrno = ether->ctlrno;
  571. ctlr->type = ether->type;
  572. csr32w(ctlr, Port, 0);
  573. delay(1);
  574. while(csr8r(ctlr, CommandR))
  575. ;
  576. csr32w(ctlr, Pointer, 0);
  577. csr8w(ctlr, CommandR, LoadRUB);
  578. while(csr8r(ctlr, CommandR))
  579. ;
  580. csr8w(ctlr, CommandR, LoadCUB);
  581. /*
  582. * Initialise the action and receive frame areas.
  583. */
  584. ctlrinit(ctlr);
  585. /*
  586. * Read the EEPROM.
  587. * Do a dummy read first to get the size
  588. * and allocate ctlr->eeprom.
  589. */
  590. hy93c46r(ctlr, 0);
  591. sum = 0;
  592. for(i = 0; i < (1<<ctlr->eepromsz); i++){
  593. x = hy93c46r(ctlr, i);
  594. ctlr->eeprom[i] = x;
  595. sum += x;
  596. }
  597. if(sum != 0xBABA)
  598. print("#l%d: EEPROM checksum - 0x%4.4uX\n", ether->ctlrno, sum);
  599. /*
  600. * Eeprom[6] indicates whether there is a PHY and whether
  601. * it's not 10Mb-only, in which case use the given PHY address
  602. * to set any PHY specific options and determine the speed.
  603. * Unfortunately, sometimes the EEPROM is blank except for
  604. * the ether address and checksum; in this case look at the
  605. * controller type and if it's am 82558 or 82559 it has an
  606. * embedded PHY so scan for that.
  607. * If no PHY, assume 82503 (serial) operation.
  608. */
  609. if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000))
  610. phyaddr = ctlr->eeprom[6] & 0x00FF;
  611. else
  612. switch(ctlr->pcidev->rid){
  613. case 0x01: /* 82557 A-step */
  614. case 0x02: /* 82557 B-step */
  615. case 0x03: /* 82557 C-step */
  616. default:
  617. phyaddr = -1;
  618. break;
  619. case 0x04: /* 82558 A-step */
  620. case 0x05: /* 82558 B-step */
  621. case 0x06: /* 82559 A-step */
  622. case 0x07: /* 82559 B-step */
  623. case 0x08: /* 82559 C-step */
  624. case 0x09: /* 82559ER A-step */
  625. phyaddr = scanphy(ctlr);
  626. break;
  627. }
  628. if(phyaddr >= 0){
  629. /*
  630. * Resolve the highest common ability of the two
  631. * link partners. In descending order:
  632. * 0x0100 100BASE-TX Full Duplex
  633. * 0x0200 100BASE-T4
  634. * 0x0080 100BASE-TX
  635. * 0x0040 10BASE-T Full Duplex
  636. * 0x0020 10BASE-T
  637. */
  638. anar = miir(ctlr, phyaddr, 0x04);
  639. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  640. anar &= anlpar;
  641. bmcr = 0;
  642. if(anar & 0x380)
  643. bmcr = 0x2000;
  644. if(anar & 0x0140)
  645. bmcr |= 0x0100;
  646. switch((ctlr->eeprom[6]>>8) & 0x001F){
  647. case 0x04: /* DP83840 */
  648. case 0x0A: /* DP83840A */
  649. /*
  650. * The DP83840[A] requires some tweaking for
  651. * reliable operation.
  652. * The manual says bit 10 should be unconditionally
  653. * set although it supposedly only affects full-duplex
  654. * operation (an & 0x0140).
  655. */
  656. x = miir(ctlr, phyaddr, 0x17) & ~0x0520;
  657. x |= 0x0420;
  658. for(i = 0; i < ether->nopt; i++){
  659. if(cistrcmp(ether->opt[i], "congestioncontrol"))
  660. continue;
  661. x |= 0x0100;
  662. break;
  663. }
  664. miiw(ctlr, phyaddr, 0x17, x);
  665. /*
  666. * If the link partner can't autonegotiate, determine
  667. * the speed from elsewhere.
  668. */
  669. if(anlpar == 0){
  670. miir(ctlr, phyaddr, 0x01);
  671. bmsr = miir(ctlr, phyaddr, 0x01);
  672. x = miir(ctlr, phyaddr, 0x19);
  673. if((bmsr & 0x0004) && !(x & 0x0040))
  674. bmcr = 0x2000;
  675. }
  676. break;
  677. case 0x07: /* Intel 82555 */
  678. /*
  679. * Auto-negotiation may fail if the other end is
  680. * a DP83840A and the cable is short.
  681. */
  682. bmsr = miir(ctlr, phyaddr, 0x01);
  683. if((miir(ctlr, phyaddr, 0) & 0x1000) && !(bmsr & 0x0020)){
  684. miiw(ctlr, phyaddr, 0x1A, 0x2010);
  685. x = miir(ctlr, phyaddr, 0);
  686. miiw(ctlr, phyaddr, 0, 0x0200|x);
  687. for(i = 0; i < 3000; i++){
  688. delay(1);
  689. if(miir(ctlr, phyaddr, 0x01) & 0x0020)
  690. break;
  691. }
  692. miiw(ctlr, phyaddr, 0x1A, 0x2000);
  693. anar = miir(ctlr, phyaddr, 0x04);
  694. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  695. anar &= anlpar;
  696. bmcr = 0;
  697. if(anar & 0x380)
  698. bmcr = 0x2000;
  699. if(anar & 0x0140)
  700. bmcr |= 0x0100;
  701. }
  702. break;
  703. }
  704. /*
  705. * Force speed and duplex if no auto-negotiation.
  706. */
  707. if(anlpar == 0){
  708. force = 0;
  709. for(i = 0; i < ether->nopt; i++){
  710. if(cistrcmp(ether->opt[i], "fullduplex") == 0){
  711. force = 1;
  712. bmcr |= 0x0100;
  713. ctlr->configdata[19] |= 0x40;
  714. }
  715. else if(cistrcmp(ether->opt[i], "speed") == 0){
  716. force = 1;
  717. x = strtol(&ether->opt[i][6], 0, 0);
  718. if(x == 10)
  719. bmcr &= ~0x2000;
  720. else if(x == 100)
  721. bmcr |= 0x2000;
  722. else
  723. force = 0;
  724. }
  725. }
  726. if(force)
  727. miiw(ctlr, phyaddr, 0x00, bmcr);
  728. }
  729. ctlr->configdata[8] = 1;
  730. ctlr->configdata[15] &= ~0x80;
  731. }
  732. else{
  733. ctlr->configdata[8] = 0;
  734. ctlr->configdata[15] |= 0x80;
  735. }
  736. /*
  737. * Load the chip configuration
  738. */
  739. configure(ether, 0);
  740. /*
  741. * Check if the adapter's station address is to be overridden.
  742. * If not, read it from the EEPROM and set in ether->ea prior to loading
  743. * the station address with the Individual Address Setup command.
  744. */
  745. memset(ea, 0, Eaddrlen);
  746. if(memcmp(ea, ether->ea, Eaddrlen) == 0){
  747. for(i = 0; i < Eaddrlen/2; i++){
  748. x = ctlr->eeprom[i];
  749. ether->ea[2*i] = x & 0xFF;
  750. ether->ea[2*i+1] = (x>>8) & 0xFF;
  751. }
  752. }
  753. bp = allocb(sizeof(Cb));
  754. cb = (Cb*)bp->rp;
  755. bp->wp += sizeof(Cb);
  756. cb->command = CbIAS;
  757. cb->link = NullPointer;
  758. memmove(cb->data, ether->ea, Eaddrlen);
  759. action(ctlr, bp);
  760. /*
  761. * Linkage to the generic ethernet driver.
  762. */
  763. ether->attach = attach;
  764. ether->transmit = transmit;
  765. ether->interrupt = interrupt;
  766. ether->detach = detach;
  767. return 0;
  768. }