ether8390.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * National Semiconductor DP8390 and clone
  3. * Network Interface Controller.
  4. */
  5. #include "u.h"
  6. #include "../port/lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #include "../port/error.h"
  12. #include "../port/netif.h"
  13. #include "etherif.h"
  14. #include "ether8390.h"
  15. enum { /* NIC core registers */
  16. Cr = 0x00, /* command register, all pages */
  17. /* Page 0, read */
  18. Clda0 = 0x01, /* current local DMA address 0 */
  19. Clda1 = 0x02, /* current local DMA address 1 */
  20. Bnry = 0x03, /* boundary pointer (R/W) */
  21. Tsr = 0x04, /* transmit status register */
  22. Ncr = 0x05, /* number of collisions register */
  23. Fifo = 0x06, /* FIFO */
  24. Isr = 0x07, /* interrupt status register (R/W) */
  25. Crda0 = 0x08, /* current remote DMA address 0 */
  26. Crda1 = 0x09, /* current remote DMA address 1 */
  27. Rsr = 0x0C, /* receive status register */
  28. Ref0 = 0x0D, /* frame alignment errors */
  29. Ref1 = 0x0E, /* CRC errors */
  30. Ref2 = 0x0F, /* missed packet errors */
  31. /* Page 0, write */
  32. Pstart = 0x01, /* page start register */
  33. Pstop = 0x02, /* page stop register */
  34. Tpsr = 0x04, /* transmit page start address */
  35. Tbcr0 = 0x05, /* transmit byte count register 0 */
  36. Tbcr1 = 0x06, /* transmit byte count register 1 */
  37. Rsar0 = 0x08, /* remote start address register 0 */
  38. Rsar1 = 0x09, /* remote start address register 1 */
  39. Rbcr0 = 0x0A, /* remote byte count register 0 */
  40. Rbcr1 = 0x0B, /* remote byte count register 1 */
  41. Rcr = 0x0C, /* receive configuration register */
  42. Tcr = 0x0D, /* transmit configuration register */
  43. Dcr = 0x0E, /* data configuration register */
  44. Imr = 0x0F, /* interrupt mask */
  45. /* Page 1, read/write */
  46. Par0 = 0x01, /* physical address register 0 */
  47. Curr = 0x07, /* current page register */
  48. Mar0 = 0x08, /* multicast address register 0 */
  49. };
  50. enum { /* Cr */
  51. Stp = 0x01, /* stop */
  52. Sta = 0x02, /* start */
  53. Txp = 0x04, /* transmit packet */
  54. Rd0 = 0x08, /* remote DMA command */
  55. Rd1 = 0x10,
  56. Rd2 = 0x20,
  57. RdREAD = Rd0, /* remote read */
  58. RdWRITE = Rd1, /* remote write */
  59. RdSEND = Rd1|Rd0, /* send packet */
  60. RdABORT = Rd2, /* abort/complete remote DMA */
  61. Ps0 = 0x40, /* page select */
  62. Ps1 = 0x80,
  63. Page0 = 0x00,
  64. Page1 = Ps0,
  65. Page2 = Ps1,
  66. };
  67. enum { /* Isr/Imr */
  68. Prx = 0x01, /* packet received */
  69. Ptx = 0x02, /* packet transmitted */
  70. Rxe = 0x04, /* receive error */
  71. Txe = 0x08, /* transmit error */
  72. Ovw = 0x10, /* overwrite warning */
  73. Cnt = 0x20, /* counter overflow */
  74. Rdc = 0x40, /* remote DMA complete */
  75. Rst = 0x80, /* reset status */
  76. };
  77. enum { /* Dcr */
  78. Wts = 0x01, /* word transfer select */
  79. Bos = 0x02, /* byte order select */
  80. Las = 0x04, /* long address select */
  81. Ls = 0x08, /* loopback select */
  82. Arm = 0x10, /* auto-initialise remote */
  83. Ft0 = 0x20, /* FIFO threshold select */
  84. Ft1 = 0x40,
  85. Ft1WORD = 0x00,
  86. Ft2WORD = Ft0,
  87. Ft4WORD = Ft1,
  88. Ft6WORD = Ft1|Ft0,
  89. };
  90. enum { /* Tcr */
  91. Crc = 0x01, /* inhibit CRC */
  92. Lb0 = 0x02, /* encoded loopback control */
  93. Lb1 = 0x04,
  94. LpbkNORMAL = 0x00, /* normal operation */
  95. LpbkNIC = Lb0, /* internal NIC module loopback */
  96. LpbkENDEC = Lb1, /* internal ENDEC module loopback */
  97. LpbkEXTERNAL = Lb1|Lb0, /* external loopback */
  98. Atd = 0x08, /* auto transmit disable */
  99. Ofst = 0x10, /* collision offset enable */
  100. };
  101. enum { /* Tsr */
  102. Ptxok = 0x01, /* packet transmitted */
  103. Col = 0x04, /* transmit collided */
  104. Abt = 0x08, /* tranmit aborted */
  105. Crs = 0x10, /* carrier sense lost */
  106. Fu = 0x20, /* FIFO underrun */
  107. Cdh = 0x40, /* CD heartbeat */
  108. Owc = 0x80, /* out of window collision */
  109. };
  110. enum { /* Rcr */
  111. Sep = 0x01, /* save errored packets */
  112. Ar = 0x02, /* accept runt packets */
  113. Ab = 0x04, /* accept broadcast */
  114. Am = 0x08, /* accept multicast */
  115. Pro = 0x10, /* promiscuous physical */
  116. Mon = 0x20, /* monitor mode */
  117. };
  118. enum { /* Rsr */
  119. Prxok = 0x01, /* packet received intact */
  120. Crce = 0x02, /* CRC error */
  121. Fae = 0x04, /* frame alignment error */
  122. Fo = 0x08, /* FIFO overrun */
  123. Mpa = 0x10, /* missed packet */
  124. Phy = 0x20, /* physical/multicast address */
  125. Dis = 0x40, /* receiver disabled */
  126. Dfr = 0x80, /* deferring */
  127. };
  128. typedef struct Hdr Hdr;
  129. struct Hdr {
  130. uchar status;
  131. uchar next;
  132. uchar len0;
  133. uchar len1;
  134. };
  135. void
  136. dp8390getea(Ether* ether, uchar* ea)
  137. {
  138. Dp8390 *ctlr;
  139. uchar cr;
  140. int i;
  141. ctlr = ether->ctlr;
  142. /*
  143. * Get the ethernet address from the chip.
  144. * Take care to restore the command register
  145. * afterwards.
  146. */
  147. ilock(ctlr);
  148. cr = regr(ctlr, Cr) & ~Txp;
  149. regw(ctlr, Cr, Page1|(~(Ps1|Ps0) & cr));
  150. for(i = 0; i < Eaddrlen; i++)
  151. ea[i] = regr(ctlr, Par0+i);
  152. regw(ctlr, Cr, cr);
  153. iunlock(ctlr);
  154. }
  155. void
  156. dp8390setea(Ether* ether)
  157. {
  158. int i;
  159. uchar cr;
  160. Dp8390 *ctlr;
  161. ctlr = ether->ctlr;
  162. /*
  163. * Set the ethernet address into the chip.
  164. * Take care to restore the command register
  165. * afterwards. Don't care about multicast
  166. * addresses as multicast is never enabled
  167. * (currently).
  168. */
  169. ilock(ctlr);
  170. cr = regr(ctlr, Cr) & ~Txp;
  171. regw(ctlr, Cr, Page1|(~(Ps1|Ps0) & cr));
  172. for(i = 0; i < Eaddrlen; i++)
  173. regw(ctlr, Par0+i, ether->ea[i]);
  174. regw(ctlr, Cr, cr);
  175. iunlock(ctlr);
  176. }
  177. static void*
  178. _dp8390read(Dp8390* ctlr, void* to, ulong from, ulong len)
  179. {
  180. uchar cr;
  181. int timo;
  182. /*
  183. * Read some data at offset 'from' in the card's memory
  184. * using the DP8390 remote DMA facility, and place it at
  185. * 'to' in main memory, via the I/O data port.
  186. */
  187. cr = regr(ctlr, Cr) & ~Txp;
  188. regw(ctlr, Cr, Page0|RdABORT|Sta);
  189. regw(ctlr, Isr, Rdc);
  190. /*
  191. * Set up the remote DMA address and count.
  192. */
  193. len = ROUNDUP(len, ctlr->width);
  194. regw(ctlr, Rbcr0, len & 0xFF);
  195. regw(ctlr, Rbcr1, (len>>8) & 0xFF);
  196. regw(ctlr, Rsar0, from & 0xFF);
  197. regw(ctlr, Rsar1, (from>>8) & 0xFF);
  198. /*
  199. * Start the remote DMA read and suck the data
  200. * out of the I/O port.
  201. */
  202. regw(ctlr, Cr, Page0|RdREAD|Sta);
  203. rdread(ctlr, to, len);
  204. /*
  205. * Wait for the remote DMA to complete. The timeout
  206. * is necessary because this routine may be called on
  207. * a non-existent chip during initialisation and, due
  208. * to the miracles of the bus, it's possible to get this
  209. * far and still be talking to a slot full of nothing.
  210. */
  211. for(timo = 10000; (regr(ctlr, Isr) & Rdc) == 0 && timo; timo--)
  212. ;
  213. regw(ctlr, Isr, Rdc);
  214. regw(ctlr, Cr, cr);
  215. return to;
  216. }
  217. void*
  218. dp8390read(Dp8390* ctlr, void* to, ulong from, ulong len)
  219. {
  220. void *v;
  221. ilock(ctlr);
  222. v = _dp8390read(ctlr, to, from, len);
  223. iunlock(ctlr);
  224. return v;
  225. }
  226. static void*
  227. dp8390write(Dp8390* ctlr, ulong to, void* from, ulong len)
  228. {
  229. ulong crda;
  230. uchar cr;
  231. int timo, width;
  232. /*
  233. * Write some data to offset 'to' in the card's memory
  234. * using the DP8390 remote DMA facility, reading it at
  235. * 'from' in main memory, via the I/O data port.
  236. */
  237. cr = regr(ctlr, Cr) & ~Txp;
  238. regw(ctlr, Cr, Page0|RdABORT|Sta);
  239. regw(ctlr, Isr, Rdc);
  240. len = ROUNDUP(len, ctlr->width);
  241. /*
  242. * Set up the remote DMA address and count.
  243. * This is straight from the DP8390[12D] datasheet,
  244. * hence the initial set up for read.
  245. * Assumption here that the A7000 EtherV card will
  246. * never need a dummyrr.
  247. */
  248. if(ctlr->dummyrr && (ctlr->width == 1 || ctlr->width == 2)){
  249. if(ctlr->width == 2)
  250. width = 1;
  251. else
  252. width = 0;
  253. crda = to-1-width;
  254. regw(ctlr, Rbcr0, (len+1+width) & 0xFF);
  255. regw(ctlr, Rbcr1, ((len+1+width)>>8) & 0xFF);
  256. regw(ctlr, Rsar0, crda & 0xFF);
  257. regw(ctlr, Rsar1, (crda>>8) & 0xFF);
  258. regw(ctlr, Cr, Page0|RdREAD|Sta);
  259. for(;;){
  260. crda = regr(ctlr, Crda0);
  261. crda |= regr(ctlr, Crda1)<<8;
  262. if(crda == to){
  263. /*
  264. * Start the remote DMA write and make sure
  265. * the registers are correct.
  266. */
  267. regw(ctlr, Cr, Page0|RdWRITE|Sta);
  268. crda = regr(ctlr, Crda0);
  269. crda |= regr(ctlr, Crda1)<<8;
  270. if(crda != to)
  271. panic("crda write %lud to %lud\n", crda, to);
  272. break;
  273. }
  274. }
  275. }
  276. else{
  277. regw(ctlr, Rsar0, to & 0xFF);
  278. regw(ctlr, Rsar1, (to>>8) & 0xFF);
  279. regw(ctlr, Rbcr0, len & 0xFF);
  280. regw(ctlr, Rbcr1, (len>>8) & 0xFF);
  281. regw(ctlr, Cr, Page0|RdWRITE|Sta);
  282. }
  283. /*
  284. * Pump the data into the I/O port
  285. * then wait for the remote DMA to finish.
  286. */
  287. rdwrite(ctlr, from, len);
  288. for(timo = 10000; (regr(ctlr, Isr) & Rdc) == 0 && timo; timo--)
  289. ;
  290. regw(ctlr, Isr, Rdc);
  291. regw(ctlr, Cr, cr);
  292. return (void*)to;
  293. }
  294. static void
  295. ringinit(Dp8390* ctlr)
  296. {
  297. regw(ctlr, Pstart, ctlr->pstart);
  298. regw(ctlr, Pstop, ctlr->pstop);
  299. regw(ctlr, Bnry, ctlr->pstop-1);
  300. regw(ctlr, Cr, Page1|RdABORT|Stp);
  301. regw(ctlr, Curr, ctlr->pstart);
  302. regw(ctlr, Cr, Page0|RdABORT|Stp);
  303. ctlr->nxtpkt = ctlr->pstart;
  304. }
  305. static uchar
  306. getcurr(Dp8390* ctlr)
  307. {
  308. uchar cr, curr;
  309. cr = regr(ctlr, Cr) & ~Txp;
  310. regw(ctlr, Cr, Page1|(~(Ps1|Ps0) & cr));
  311. curr = regr(ctlr, Curr);
  312. regw(ctlr, Cr, cr);
  313. return curr;
  314. }
  315. static void
  316. receive(Ether* ether)
  317. {
  318. Dp8390 *ctlr;
  319. uchar curr, *p;
  320. Hdr hdr;
  321. ulong count, data, len;
  322. Block *bp;
  323. ctlr = ether->ctlr;
  324. for(curr = getcurr(ctlr); ctlr->nxtpkt != curr; curr = getcurr(ctlr)){
  325. data = ctlr->nxtpkt*Dp8390BufSz;
  326. if(ctlr->ram)
  327. memmove(&hdr, (void*)(ether->mem+data), sizeof(Hdr));
  328. else
  329. _dp8390read(ctlr, &hdr, data, sizeof(Hdr));
  330. /*
  331. * Don't believe the upper byte count, work it
  332. * out from the software next-page pointer and
  333. * the current next-page pointer.
  334. */
  335. if(hdr.next > ctlr->nxtpkt)
  336. len = hdr.next - ctlr->nxtpkt - 1;
  337. else
  338. len = (ctlr->pstop-ctlr->nxtpkt) + (hdr.next-ctlr->pstart) - 1;
  339. if(hdr.len0 > (Dp8390BufSz-sizeof(Hdr)))
  340. len--;
  341. len = ((len<<8)|hdr.len0)-4;
  342. /*
  343. * Chip is badly scrogged, reinitialise the ring.
  344. */
  345. if(hdr.next < ctlr->pstart || hdr.next >= ctlr->pstop
  346. || len < 60 || len > sizeof(Etherpkt)){
  347. print("dp8390: H#%2.2ux#%2.2ux#%2.2ux#%2.2ux,%lud\n",
  348. hdr.status, hdr.next, hdr.len0, hdr.len1, len);
  349. regw(ctlr, Cr, Page0|RdABORT|Stp);
  350. ringinit(ctlr);
  351. regw(ctlr, Cr, Page0|RdABORT|Sta);
  352. return;
  353. }
  354. /*
  355. * If it's a good packet read it in to the software buffer.
  356. * If the packet wraps round the hardware ring, read it in
  357. * two pieces.
  358. */
  359. if((hdr.status & (Fo|Fae|Crce|Prxok)) == Prxok && (bp = iallocb(len))){
  360. p = bp->rp;
  361. bp->wp = p+len;
  362. data += sizeof(Hdr);
  363. if((data+len) >= ctlr->pstop*Dp8390BufSz){
  364. count = ctlr->pstop*Dp8390BufSz - data;
  365. if(ctlr->ram)
  366. memmove(p, (void*)(ether->mem+data), count);
  367. else
  368. _dp8390read(ctlr, p, data, count);
  369. p += count;
  370. data = ctlr->pstart*Dp8390BufSz;
  371. len -= count;
  372. }
  373. if(len){
  374. if(ctlr->ram)
  375. memmove(p, (void*)(ether->mem+data), len);
  376. else
  377. _dp8390read(ctlr, p, data, len);
  378. }
  379. /*
  380. * Copy the packet to whoever wants it.
  381. */
  382. etheriq(ether, bp, 1);
  383. }
  384. /*
  385. * Finished with this packet, update the
  386. * hardware and software ring pointers.
  387. */
  388. ctlr->nxtpkt = hdr.next;
  389. hdr.next--;
  390. if(hdr.next < ctlr->pstart)
  391. hdr.next = ctlr->pstop-1;
  392. regw(ctlr, Bnry, hdr.next);
  393. }
  394. }
  395. static void
  396. txstart(Ether* ether)
  397. {
  398. int len;
  399. Dp8390 *ctlr;
  400. Block *bp;
  401. uchar minpkt[ETHERMINTU], *rp;
  402. ctlr = ether->ctlr;
  403. /*
  404. * This routine is called both from the top level and from interrupt
  405. * level and expects to be called with ctlr already locked.
  406. */
  407. if(ctlr->txbusy)
  408. return;
  409. bp = qget(ether->oq);
  410. if(bp == nil)
  411. return;
  412. /*
  413. * Make sure the packet is of minimum length;
  414. * copy it to the card's memory by the appropriate means;
  415. * start the transmission.
  416. */
  417. len = BLEN(bp);
  418. rp = bp->rp;
  419. if(len < ETHERMINTU){
  420. rp = minpkt;
  421. memmove(rp, bp->rp, len);
  422. memset(rp+len, 0, ETHERMINTU-len);
  423. len = ETHERMINTU;
  424. }
  425. if(ctlr->ram)
  426. memmove((void*)(ether->mem+ctlr->tstart*Dp8390BufSz), rp, len);
  427. else
  428. dp8390write(ctlr, ctlr->tstart*Dp8390BufSz, rp, len);
  429. freeb(bp);
  430. regw(ctlr, Tbcr0, len & 0xFF);
  431. regw(ctlr, Tbcr1, (len>>8) & 0xFF);
  432. regw(ctlr, Cr, Page0|RdABORT|Txp|Sta);
  433. ether->outpackets++;
  434. ctlr->txbusy = 1;
  435. }
  436. static void
  437. transmit(Ether* ether)
  438. {
  439. Dp8390 *ctlr;
  440. ctlr = ether->ctlr;
  441. ilock(ctlr);
  442. txstart(ether);
  443. iunlock(ctlr);
  444. }
  445. static void
  446. overflow(Ether *ether)
  447. {
  448. Dp8390 *ctlr;
  449. uchar txp;
  450. int resend;
  451. ctlr = ether->ctlr;
  452. /*
  453. * The following procedure is taken from the DP8390[12D] datasheet,
  454. * it seems pretty adamant that this is what has to be done.
  455. */
  456. txp = regr(ctlr, Cr) & Txp;
  457. regw(ctlr, Cr, Page0|RdABORT|Stp);
  458. delay(2);
  459. regw(ctlr, Rbcr0, 0);
  460. regw(ctlr, Rbcr1, 0);
  461. resend = 0;
  462. if(txp && (regr(ctlr, Isr) & (Txe|Ptx)) == 0)
  463. resend = 1;
  464. regw(ctlr, Tcr, LpbkNIC);
  465. regw(ctlr, Cr, Page0|RdABORT|Sta);
  466. receive(ether);
  467. regw(ctlr, Isr, Ovw);
  468. regw(ctlr, Tcr, LpbkNORMAL);
  469. if(resend)
  470. regw(ctlr, Cr, Page0|RdABORT|Txp|Sta);
  471. }
  472. static void
  473. interrupt(Ureg*, void* arg)
  474. {
  475. Ether *ether;
  476. Dp8390 *ctlr;
  477. uchar isr, r;
  478. ether = arg;
  479. ctlr = ether->ctlr;
  480. /*
  481. * While there is something of interest,
  482. * clear all the interrupts and process.
  483. */
  484. ilock(ctlr);
  485. regw(ctlr, Imr, 0x00);
  486. while(isr = (regr(ctlr, Isr) & (Cnt|Ovw|Txe|Rxe|Ptx|Prx))){
  487. if(isr & Ovw){
  488. overflow(ether);
  489. regw(ctlr, Isr, Ovw);
  490. ether->overflows++;
  491. }
  492. /*
  493. * Packets have been received.
  494. * Take a spin round the ring.
  495. */
  496. if(isr & (Rxe|Prx)){
  497. receive(ether);
  498. regw(ctlr, Isr, Rxe|Prx);
  499. }
  500. /*
  501. * A packet completed transmission, successfully or
  502. * not. Start transmission on the next buffered packet,
  503. * and wake the output routine.
  504. */
  505. if(isr & (Txe|Ptx)){
  506. r = regr(ctlr, Tsr);
  507. if((isr & Txe) && (r & (Cdh|Fu|Crs|Abt))){
  508. print("dp8390: Tsr#%2.2ux|", r);
  509. ether->oerrs++;
  510. }
  511. regw(ctlr, Isr, Txe|Ptx);
  512. if(isr & Ptx)
  513. ether->outpackets++;
  514. ctlr->txbusy = 0;
  515. txstart(ether);
  516. }
  517. if(isr & Cnt){
  518. ether->frames += regr(ctlr, Ref0);
  519. ether->crcs += regr(ctlr, Ref1);
  520. ether->buffs += regr(ctlr, Ref2);
  521. regw(ctlr, Isr, Cnt);
  522. }
  523. }
  524. regw(ctlr, Imr, Cnt|Ovw|Txe|Rxe|Ptx|Prx);
  525. iunlock(ctlr);
  526. }
  527. static uchar allmar[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  528. static void
  529. setfilter(Ether *ether, Dp8390 *ctlr)
  530. {
  531. uchar r, cr;
  532. int i;
  533. uchar *mar;
  534. r = Ab;
  535. mar = 0;
  536. if(ether->prom){
  537. r |= Pro|Am;
  538. mar = allmar;
  539. } else if(ether->nmaddr){
  540. r |= Am;
  541. mar = ctlr->mar;
  542. }
  543. if(mar){
  544. cr = regr(ctlr, Cr) & ~Txp;
  545. regw(ctlr, Cr, Page1|(~(Ps1|Ps0) & cr));
  546. for(i = 0; i < 8; i++)
  547. regw(ctlr, Mar0+i, *(mar++));
  548. regw(ctlr, Cr, cr);
  549. }
  550. regw(ctlr, Rcr, r);
  551. }
  552. static void
  553. promiscuous(void *arg, int )
  554. {
  555. Ether *ether;
  556. Dp8390 *ctlr;
  557. ether = arg;
  558. ctlr = ether->ctlr;
  559. ilock(ctlr);
  560. setfilter(ether, ctlr);
  561. iunlock(ctlr);
  562. }
  563. static void
  564. setbit(Dp8390 *ctlr, int bit, int on)
  565. {
  566. int i, h;
  567. i = bit/8;
  568. h = bit%8;
  569. if(on){
  570. if(++(ctlr->mref[bit]) == 1)
  571. ctlr->mar[i] |= 1<<h;
  572. } else {
  573. if(--(ctlr->mref[bit]) <= 0){
  574. ctlr->mref[bit] = 0;
  575. ctlr->mar[i] &= ~(1<<h);
  576. }
  577. }
  578. }
  579. static uchar reverse[64];
  580. static void
  581. multicast(void* arg, uchar *addr, int on)
  582. {
  583. Ether *ether;
  584. Dp8390 *ctlr;
  585. int i;
  586. ulong h;
  587. ether = arg;
  588. ctlr = ether->ctlr;
  589. if(reverse[1] == 0){
  590. for(i = 0; i < 64; i++)
  591. reverse[i] = ((i&1)<<5) | ((i&2)<<3) | ((i&4)<<1)
  592. | ((i&8)>>1) | ((i&16)>>3) | ((i&32)>>5);
  593. }
  594. /*
  595. * change filter bits
  596. */
  597. h = ethercrc(addr, 6);
  598. ilock(ctlr);
  599. setbit(ctlr, reverse[h&0x3f], on);
  600. setfilter(ether, ctlr);
  601. iunlock(ctlr);
  602. }
  603. static void
  604. attach(Ether* ether)
  605. {
  606. Dp8390 *ctlr;
  607. uchar r;
  608. ctlr = ether->ctlr;
  609. /*
  610. * Enable the chip for transmit/receive.
  611. * The init routine leaves the chip in monitor
  612. * mode. Clear the missed-packet counter, it
  613. * increments while in monitor mode.
  614. * Sometimes there's an interrupt pending at this
  615. * point but there's nothing in the Isr, so
  616. * any pending interrupts are cleared and the
  617. * mask of acceptable interrupts is enabled here.
  618. */
  619. r = Ab;
  620. if(ether->prom)
  621. r |= Pro;
  622. if(ether->nmaddr)
  623. r |= Am;
  624. ilock(ctlr);
  625. regw(ctlr, Isr, 0xFF);
  626. regw(ctlr, Imr, Cnt|Ovw|Txe|Rxe|Ptx|Prx);
  627. regw(ctlr, Rcr, r);
  628. r = regr(ctlr, Ref2);
  629. regw(ctlr, Tcr, LpbkNORMAL);
  630. iunlock(ctlr);
  631. USED(r);
  632. }
  633. static void
  634. disable(Dp8390* ctlr)
  635. {
  636. int timo;
  637. /*
  638. * Stop the chip. Set the Stp bit and wait for the chip
  639. * to finish whatever was on its tiny mind before it sets
  640. * the Rst bit.
  641. * The timeout is needed because there may not be a real
  642. * chip there if this is called when probing for a device
  643. * at boot.
  644. */
  645. regw(ctlr, Cr, Page0|RdABORT|Stp);
  646. regw(ctlr, Rbcr0, 0);
  647. regw(ctlr, Rbcr1, 0);
  648. for(timo = 10000; (regr(ctlr, Isr) & Rst) == 0 && timo; timo--)
  649. ;
  650. }
  651. int
  652. dp8390reset(Ether* ether)
  653. {
  654. Dp8390 *ctlr;
  655. ctlr = ether->ctlr;
  656. /*
  657. * This is the initialisation procedure described
  658. * as 'mandatory' in the datasheet, with references
  659. * to the 3C503 technical reference manual.
  660. */
  661. disable(ctlr);
  662. if(ctlr->width != 1)
  663. regw(ctlr, Dcr, Ft4WORD|Ls|Wts);
  664. else
  665. regw(ctlr, Dcr, Ft4WORD|Ls);
  666. regw(ctlr, Rbcr0, 0);
  667. regw(ctlr, Rbcr1, 0);
  668. regw(ctlr, Tcr, LpbkNIC);
  669. regw(ctlr, Rcr, Mon);
  670. /*
  671. * Init the ring hardware and software ring pointers.
  672. * Can't initialise ethernet address as it may not be
  673. * known yet.
  674. */
  675. ringinit(ctlr);
  676. regw(ctlr, Tpsr, ctlr->tstart);
  677. /*
  678. * Clear any pending interrupts and mask then all off.
  679. */
  680. regw(ctlr, Isr, 0xFF);
  681. regw(ctlr, Imr, 0);
  682. /*
  683. * Leave the chip initialised,
  684. * but in monitor mode.
  685. */
  686. regw(ctlr, Cr, Page0|RdABORT|Sta);
  687. /*
  688. * Set up the software configuration.
  689. */
  690. ether->attach = attach;
  691. ether->transmit = transmit;
  692. ether->interrupt = interrupt;
  693. ether->ifstat = 0;
  694. ether->promiscuous = promiscuous;
  695. ether->multicast = multicast;
  696. ether->arg = ether;
  697. return 0;
  698. }