ne2000.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* NE2000-compatible (Realtek RTL8029AS) Ethernet driver.
  2. PCI discovery, byte-mode remote DMA, on-card SRAM ring, polling (no IRQ).
  3. Register map (byte offsets from the I/O base; page selected by CR bits
  4. PS0/PS1). Page 0: 0x00 CR, 0x01 PSTART, 0x02 PSTOP, 0x03 BNRY, 0x04
  5. TPSR(TSR), 0x05/0x06 TBCR, 0x07 ISR, 0x08/0x09 RSAR, 0x0A/0x0B RBCR,
  6. 0x0C RCR, 0x0D TCR, 0x0E DCR, 0x0F IMR. Page 1: 0x01-0x06 PAR0-5,
  7. 0x07 CURR. Data port 0x10, reset port 0x1F. */
  8. #include "io.h"
  9. #include "pci.h"
  10. #include "timer.h"
  11. #include "ne2000.h"
  12. /* Register offsets. */
  13. #define NE_CR 0x00
  14. #define NE_PSTART 0x01
  15. #define NE_PSTOP 0x02
  16. #define NE_BNRY 0x03
  17. #define NE_TSR 0x04 /* read: transmit status */
  18. #define NE_TPSR 0x04 /* write: transmit page start */
  19. #define NE_TBCR0 0x05
  20. #define NE_TBCR1 0x06
  21. #define NE_ISR 0x07
  22. #define NE_RSAR0 0x08
  23. #define NE_RSAR1 0x09
  24. #define NE_RBCR0 0x0A
  25. #define NE_RBCR1 0x0B
  26. #define NE_RCR 0x0C
  27. #define NE_TCR 0x0D
  28. #define NE_DCR 0x0E
  29. #define NE_IMR 0x0F
  30. #define NE_DATA 0x10
  31. #define NE_RESET 0x1F
  32. /* CR bits. */
  33. #define CR_STP 0x01
  34. #define CR_START 0x02
  35. #define CR_TXP 0x04
  36. #define CR_RREAD 0x08
  37. #define CR_RWRITE 0x10
  38. #define CR_NODMA 0x20
  39. #define CR_PAGE1 0x40
  40. /* ISR bits. */
  41. #define ISR_PTX 0x02
  42. #define ISR_TXE 0x08
  43. #define ISR_OVW 0x10
  44. #define ISR_RST 0x80
  45. /* RCR bits. */
  46. #define RCR_AB 0x04
  47. #define RCR_PRO 0x10
  48. /* TSR bits. */
  49. #define TSR_PTX 0x01
  50. /* On-card SRAM layout (16 KB at 0x4000): TX buffer 0x4000-0x45FF,
  51. RX ring 0x4600-0x7FFF. Pages are 256 bytes. */
  52. #define TX_PAGE 0x40
  53. #define RX_START 0x46
  54. #define RX_STOP 0x80
  55. /* PCI IDs. */
  56. #define RTL8029_VENDOR 0x10ECu
  57. #define RTL8029_DEVICE 0x8029u
  58. static unsigned short iobase;
  59. static unsigned char mac[6];
  60. static int present;
  61. static int inited;
  62. static unsigned char nread(unsigned char reg)
  63. {
  64. return port_inb((unsigned short)(iobase + reg));
  65. }
  66. static void nwrite(unsigned char reg, unsigned char val)
  67. {
  68. port_outb((unsigned short)(iobase + reg), val);
  69. }
  70. static void delay_ms(unsigned int ms)
  71. {
  72. unsigned int t0 = get_ticks_ms();
  73. while (get_ticks_ms() - t0 < ms)
  74. ;
  75. }
  76. static void remote_read(unsigned short addr, unsigned char *dst, unsigned short len)
  77. {
  78. unsigned short i;
  79. nwrite(NE_CR, CR_NODMA | CR_START); /* page 0, keep NIC running */
  80. nwrite(NE_RBCR0, 0);
  81. nwrite(NE_RBCR1, 0);
  82. nwrite(NE_RSAR0, addr & 0xFF);
  83. nwrite(NE_RSAR1, addr >> 8);
  84. nwrite(NE_RBCR0, len & 0xFF);
  85. nwrite(NE_RBCR1, len >> 8);
  86. nwrite(NE_CR, CR_RREAD | CR_START); /* remote read, page 0 */
  87. for (i = 0; i + 1 < len; i += 2) {
  88. unsigned short w = port_inw((unsigned short)(iobase + NE_DATA));
  89. dst[i] = (unsigned char)(w & 0xFF);
  90. dst[i + 1] = (unsigned char)(w >> 8);
  91. }
  92. if (len & 1)
  93. dst[len - 1] = port_inb((unsigned short)(iobase + NE_DATA));
  94. }
  95. static void remote_write(unsigned short addr, const unsigned char *src, unsigned short len)
  96. {
  97. unsigned short i;
  98. nwrite(NE_CR, CR_NODMA | CR_START);
  99. nwrite(NE_RBCR0, 0);
  100. nwrite(NE_RBCR1, 0);
  101. nwrite(NE_RSAR0, addr & 0xFF);
  102. nwrite(NE_RSAR1, addr >> 8);
  103. nwrite(NE_RBCR0, len & 0xFF);
  104. nwrite(NE_RBCR1, len >> 8);
  105. nwrite(NE_CR, CR_RWRITE | CR_START); /* remote write, page 0 */
  106. for (i = 0; i + 1 < len; i += 2)
  107. port_outw((unsigned short)(iobase + NE_DATA),
  108. (unsigned short)(src[i] | (src[i + 1] << 8)));
  109. if (len & 1)
  110. port_outb((unsigned short)(iobase + NE_DATA), src[len - 1]);
  111. }
  112. int ne2000_init(void)
  113. {
  114. unsigned int bus, dev;
  115. if (inited)
  116. return present ? 0 : -1;
  117. inited = 1;
  118. present = 0;
  119. iobase = 0;
  120. /* PCI scan for Realtek RTL8029. */
  121. for (bus = 0; bus < 256 && !present; bus++) {
  122. for (dev = 0; dev < 32; dev++) {
  123. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  124. unsigned int cmd, bar;
  125. if (id == 0xFFFFFFFFu || id == 0)
  126. continue;
  127. if ((id & 0xFFFFu) != RTL8029_VENDOR)
  128. continue;
  129. if (((id >> 16) & 0xFFFFu) != RTL8029_DEVICE)
  130. continue;
  131. /* Enable I/O space + bus master. */
  132. cmd = pci_cfg_read32(bus, dev, 0, 0x04);
  133. pci_cfg_write32(bus, dev, 0, 0x04, cmd | 0x0005u);
  134. bar = pci_cfg_read32(bus, dev, 0, 0x10);
  135. if (!(bar & 0x1u))
  136. continue; /* not an I/O-space BAR */
  137. iobase = (unsigned short)(bar & 0xFFFCu);
  138. present = 1;
  139. }
  140. }
  141. if (!present)
  142. return -1;
  143. /* Reset (read then write the reset port). */
  144. nwrite(NE_RESET, nread(NE_RESET));
  145. delay_ms(20);
  146. /* Wait for reset completion (ISR RST bit), then clear ISR. */
  147. {
  148. unsigned int t0 = get_ticks_ms();
  149. while (!(nread(NE_ISR) & ISR_RST)) {
  150. if (get_ticks_ms() - t0 > 500)
  151. break;
  152. }
  153. nwrite(NE_ISR, 0xFF);
  154. }
  155. /* Stop, page 0. */
  156. nwrite(NE_CR, CR_NODMA | CR_STP);
  157. /* DCR: word-wide remote DMA (16-bit), normal (non-loopback), 8-byte FIFO. */
  158. nwrite(NE_DCR, 0x49);
  159. nwrite(NE_RBCR0, 0);
  160. nwrite(NE_RBCR1, 0);
  161. /* Read the station address PROM. The NE2000/RTL8029 maps the MAC into
  162. the first 32 bytes of remote-DMA-addressable memory, at even offsets. */
  163. {
  164. unsigned char prom[32];
  165. unsigned int i;
  166. remote_read(0x0000, prom, 32);
  167. for (i = 0; i < 6; i++)
  168. mac[i] = prom[2 * i];
  169. }
  170. /* RX: promiscuous + broadcast (easiest to observe traffic). */
  171. nwrite(NE_RCR, RCR_PRO | RCR_AB);
  172. /* TX: normal, append CRC. */
  173. nwrite(NE_TCR, 0x00);
  174. /* Ring buffer. */
  175. nwrite(NE_PSTART, RX_START);
  176. nwrite(NE_PSTOP, RX_STOP);
  177. nwrite(NE_BNRY, RX_START);
  178. /* Clear ISR; mask all interrupts (polling). */
  179. nwrite(NE_ISR, 0xFF);
  180. nwrite(NE_IMR, 0x00);
  181. /* Page 1: write the MAC to PAR0-5 (so unicast frames to us are
  182. accepted even if promiscuous mode is unreliable), then set CURR. */
  183. nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
  184. {
  185. unsigned int i;
  186. for (i = 0; i < 6; i++)
  187. nwrite((unsigned char)(NE_PSTART + i), mac[i]);
  188. }
  189. nwrite(0x07, RX_START + 1); /* CURR = first free page */
  190. /* Page 0, start the NIC. */
  191. nwrite(NE_CR, CR_NODMA | CR_START);
  192. return 0;
  193. }
  194. int ne2000_present(void)
  195. {
  196. return present;
  197. }
  198. void ne2000_get_mac(unsigned char out[6])
  199. {
  200. unsigned int i;
  201. for (i = 0; i < 6; i++)
  202. out[i] = mac[i];
  203. }
  204. int ne2000_rx(unsigned char *buf, unsigned int maxlen)
  205. {
  206. unsigned char bnry, cur, pkt, next, status;
  207. unsigned short addr, len;
  208. unsigned int guard = 0;
  209. if (!present)
  210. return -1;
  211. for (;;) {
  212. /* Safety net: a full ring is only ~230 frames, so bail out
  213. rather than spin forever if the card misbehaves. */
  214. if (++guard > 1024)
  215. return -1;
  216. /* BNRY (page 0) and CURR (page 1). */
  217. nwrite(NE_CR, CR_NODMA | CR_START);
  218. /* Ring overflow: the card wrapped and clobbered unread frames,
  219. so the "next" pointers are unreliable. Discard the whole ring
  220. and start fresh, or we'd re-read stale frames (duplicates). */
  221. if (nread(NE_ISR) & ISR_OVW) {
  222. nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
  223. cur = nread(0x07);
  224. nwrite(NE_CR, CR_NODMA | CR_START);
  225. if (cur == RX_START)
  226. nwrite(NE_BNRY, RX_STOP - 1);
  227. else
  228. nwrite(NE_BNRY, cur - 1);
  229. nwrite(NE_ISR, ISR_OVW);
  230. return -1;
  231. }
  232. bnry = nread(NE_BNRY);
  233. nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
  234. cur = nread(0x07);
  235. nwrite(NE_CR, CR_NODMA | CR_START);
  236. /* Next unread frame starts at page bnry+1 (wrapping). */
  237. pkt = bnry + 1;
  238. if (pkt >= RX_STOP)
  239. pkt = RX_START;
  240. /* No new frames once the next-unread page catches up to CURR. */
  241. if (pkt == cur)
  242. return -1;
  243. addr = (unsigned short)pkt << 8;
  244. /* 4-byte header: status, next page, length lo, length hi. */
  245. {
  246. unsigned char hdr[4];
  247. remote_read(addr, hdr, 4);
  248. status = hdr[0];
  249. next = hdr[1];
  250. len = (unsigned short)hdr[2] | ((unsigned short)hdr[3] << 8);
  251. }
  252. /* Advance boundary past this frame. */
  253. if (next == RX_START)
  254. nwrite(NE_BNRY, RX_STOP - 1);
  255. else
  256. nwrite(NE_BNRY, next - 1);
  257. /* Skip errored or oversized frames. */
  258. if (!(status & 0x01))
  259. continue;
  260. if (len < 4 || (unsigned int)(len - 4) > maxlen)
  261. continue;
  262. remote_read(addr + 4, buf, (unsigned short)(len - 4));
  263. return (int)(len - 4);
  264. }
  265. }
  266. int ne2000_tx(const unsigned char *buf, unsigned int len)
  267. {
  268. static unsigned char pad[60];
  269. const unsigned char *p = buf;
  270. unsigned int txlen = len;
  271. unsigned int t0;
  272. unsigned char isr, tsr;
  273. if (!present)
  274. return -1;
  275. if (len < 14 || len > 1514)
  276. return -1;
  277. /* Pad to the 60-byte Ethernet minimum (the NIC does not auto-pad;
  278. a runt frame would be dropped by the switch/peer). */
  279. if (len < 60) {
  280. unsigned int i;
  281. for (i = 0; i < len; i++)
  282. pad[i] = buf[i];
  283. for (i = len; i < 60; i++)
  284. pad[i] = 0;
  285. p = pad;
  286. txlen = 60;
  287. }
  288. /* Copy the frame into on-card SRAM. */
  289. remote_write((unsigned short)(TX_PAGE << 8), p, (unsigned short)txlen);
  290. /* Program transmit page + byte count. */
  291. nwrite(NE_CR, CR_NODMA | CR_START);
  292. nwrite(NE_TPSR, TX_PAGE);
  293. nwrite(NE_TBCR0, txlen & 0xFF);
  294. nwrite(NE_TBCR1, (txlen >> 8) & 0xFF);
  295. /* Clear TX status bits, then fire. */
  296. nwrite(NE_ISR, ISR_PTX | ISR_TXE);
  297. nwrite(NE_CR, CR_NODMA | CR_START | CR_TXP);
  298. /* Poll for completion. */
  299. t0 = get_ticks_ms();
  300. for (;;) {
  301. isr = nread(NE_ISR);
  302. if (isr & (ISR_PTX | ISR_TXE))
  303. break;
  304. if (get_ticks_ms() - t0 > 100)
  305. return -1;
  306. }
  307. tsr = nread(NE_TSR);
  308. if (!(tsr & TSR_PTX))
  309. return -1; /* collision/abort/underrun */
  310. return 0;
  311. }
  312. static void put_hex2(unsigned char v)
  313. {
  314. static const char hex[] = "0123456789ABCDEF";
  315. io_putchar(hex[(v >> 4) & 0xF]);
  316. io_putchar(hex[v & 0xF]);
  317. }
  318. void ne2000_print_info(void)
  319. {
  320. unsigned int i;
  321. if (!present) {
  322. io_putstr("net: no RTL8029AS found\n");
  323. return;
  324. }
  325. io_putstr("RTL8029AS iobase=0x");
  326. io_puthex(iobase);
  327. io_putstr(" mac=");
  328. for (i = 0; i < 6; i++) {
  329. if (i)
  330. io_putchar(':');
  331. put_hex2(mac[i]);
  332. }
  333. io_putchar('\n');
  334. }