ether8390.c 17 KB

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