ne2000.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_RST 0x80
  44. /* RCR bits. */
  45. #define RCR_AB 0x04
  46. #define RCR_PRO 0x10
  47. /* TSR bits. */
  48. #define TSR_PTX 0x01
  49. /* On-card SRAM layout (16 KB at 0x4000): TX buffer 0x4000-0x45FF,
  50. RX ring 0x4600-0x7FFF. Pages are 256 bytes. */
  51. #define TX_PAGE 0x40
  52. #define RX_START 0x46
  53. #define RX_STOP 0x80
  54. /* PCI IDs. */
  55. #define RTL8029_VENDOR 0x10ECu
  56. #define RTL8029_DEVICE 0x8029u
  57. static unsigned short iobase;
  58. static unsigned char mac[6];
  59. static int present;
  60. static int inited;
  61. static unsigned char nread(unsigned char reg)
  62. {
  63. return port_inb((unsigned short)(iobase + reg));
  64. }
  65. static void nwrite(unsigned char reg, unsigned char val)
  66. {
  67. port_outb((unsigned short)(iobase + reg), val);
  68. }
  69. static void delay_ms(unsigned int ms)
  70. {
  71. unsigned int t0 = get_ticks_ms();
  72. while (get_ticks_ms() - t0 < ms)
  73. ;
  74. }
  75. static void remote_read(unsigned short addr, unsigned char *dst, unsigned short len)
  76. {
  77. unsigned short i;
  78. nwrite(NE_CR, CR_NODMA | CR_START); /* page 0, keep NIC running */
  79. nwrite(NE_RBCR0, 0);
  80. nwrite(NE_RBCR1, 0);
  81. nwrite(NE_RSAR0, addr & 0xFF);
  82. nwrite(NE_RSAR1, addr >> 8);
  83. nwrite(NE_RBCR0, len & 0xFF);
  84. nwrite(NE_RBCR1, len >> 8);
  85. nwrite(NE_CR, CR_RREAD | CR_START); /* remote read, page 0 */
  86. for (i = 0; i + 1 < len; i += 2) {
  87. unsigned short w = port_inw((unsigned short)(iobase + NE_DATA));
  88. dst[i] = (unsigned char)(w & 0xFF);
  89. dst[i + 1] = (unsigned char)(w >> 8);
  90. }
  91. if (len & 1)
  92. dst[len - 1] = port_inb((unsigned short)(iobase + NE_DATA));
  93. }
  94. static void remote_write(unsigned short addr, const unsigned char *src, unsigned short len)
  95. {
  96. unsigned short i;
  97. nwrite(NE_CR, CR_NODMA | CR_START);
  98. nwrite(NE_RBCR0, 0);
  99. nwrite(NE_RBCR1, 0);
  100. nwrite(NE_RSAR0, addr & 0xFF);
  101. nwrite(NE_RSAR1, addr >> 8);
  102. nwrite(NE_RBCR0, len & 0xFF);
  103. nwrite(NE_RBCR1, len >> 8);
  104. nwrite(NE_CR, CR_RWRITE | CR_START); /* remote write, page 0 */
  105. for (i = 0; i + 1 < len; i += 2)
  106. port_outw((unsigned short)(iobase + NE_DATA),
  107. (unsigned short)(src[i] | (src[i + 1] << 8)));
  108. if (len & 1)
  109. port_outb((unsigned short)(iobase + NE_DATA), src[len - 1]);
  110. }
  111. int ne2000_init(void)
  112. {
  113. unsigned int bus, dev;
  114. if (inited)
  115. return present ? 0 : -1;
  116. inited = 1;
  117. present = 0;
  118. iobase = 0;
  119. /* PCI scan for Realtek RTL8029. */
  120. for (bus = 0; bus < 256 && !present; bus++) {
  121. for (dev = 0; dev < 32; dev++) {
  122. unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
  123. unsigned int cmd, bar;
  124. if (id == 0xFFFFFFFFu || id == 0)
  125. continue;
  126. if ((id & 0xFFFFu) != RTL8029_VENDOR)
  127. continue;
  128. if (((id >> 16) & 0xFFFFu) != RTL8029_DEVICE)
  129. continue;
  130. /* Enable I/O space + bus master. */
  131. cmd = pci_cfg_read32(bus, dev, 0, 0x04);
  132. pci_cfg_write32(bus, dev, 0, 0x04, cmd | 0x0005u);
  133. bar = pci_cfg_read32(bus, dev, 0, 0x10);
  134. if (!(bar & 0x1u))
  135. continue; /* not an I/O-space BAR */
  136. iobase = (unsigned short)(bar & 0xFFFCu);
  137. present = 1;
  138. }
  139. }
  140. if (!present)
  141. return -1;
  142. /* Reset (read then write the reset port). */
  143. nwrite(NE_RESET, nread(NE_RESET));
  144. delay_ms(20);
  145. /* Wait for reset completion (ISR RST bit), then clear ISR. */
  146. {
  147. unsigned int t0 = get_ticks_ms();
  148. while (!(nread(NE_ISR) & ISR_RST)) {
  149. if (get_ticks_ms() - t0 > 500)
  150. break;
  151. }
  152. nwrite(NE_ISR, 0xFF);
  153. }
  154. /* Stop, page 0. */
  155. nwrite(NE_CR, CR_NODMA | CR_STP);
  156. /* DCR: word-wide remote DMA (16-bit), normal (non-loopback), 8-byte FIFO. */
  157. nwrite(NE_DCR, 0x49);
  158. nwrite(NE_RBCR0, 0);
  159. nwrite(NE_RBCR1, 0);
  160. /* Read the station address PROM. The NE2000/RTL8029 maps the MAC into
  161. the first 32 bytes of remote-DMA-addressable memory, at even offsets. */
  162. {
  163. unsigned char prom[32];
  164. unsigned int i;
  165. remote_read(0x0000, prom, 32);
  166. for (i = 0; i < 6; i++)
  167. mac[i] = prom[2 * i];
  168. }
  169. /* RX: promiscuous + broadcast (easiest to observe traffic). */
  170. nwrite(NE_RCR, RCR_PRO | RCR_AB);
  171. /* TX: normal, append CRC. */
  172. nwrite(NE_TCR, 0x00);
  173. /* Ring buffer. */
  174. nwrite(NE_PSTART, RX_START);
  175. nwrite(NE_PSTOP, RX_STOP);
  176. nwrite(NE_BNRY, RX_START);
  177. /* Clear ISR; mask all interrupts (polling). */
  178. nwrite(NE_ISR, 0xFF);
  179. nwrite(NE_IMR, 0x00);
  180. /* Page 1: set CURR to the first free page. */
  181. nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
  182. nwrite(0x07, RX_START + 1);
  183. /* Page 0, start the NIC. */
  184. nwrite(NE_CR, CR_NODMA | CR_START);
  185. return 0;
  186. }
  187. int ne2000_present(void)
  188. {
  189. return present;
  190. }
  191. void ne2000_get_mac(unsigned char out[6])
  192. {
  193. unsigned int i;
  194. for (i = 0; i < 6; i++)
  195. out[i] = mac[i];
  196. }
  197. int ne2000_rx(unsigned char *buf, unsigned int maxlen)
  198. {
  199. unsigned char bnry, cur, pkt, next, status;
  200. unsigned short addr, len;
  201. unsigned int guard = 0;
  202. if (!present)
  203. return -1;
  204. for (;;) {
  205. /* Safety net: a full ring is only ~230 frames, so bail out
  206. rather than spin forever if the card misbehaves. */
  207. if (++guard > 1024)
  208. return -1;
  209. /* BNRY (page 0) and CURR (page 1). */
  210. nwrite(NE_CR, CR_NODMA | CR_START);
  211. bnry = nread(NE_BNRY);
  212. nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
  213. cur = nread(0x07);
  214. nwrite(NE_CR, CR_NODMA | CR_START);
  215. /* Next unread frame starts at page bnry+1 (wrapping). */
  216. pkt = bnry + 1;
  217. if (pkt >= RX_STOP)
  218. pkt = RX_START;
  219. /* No new frames once the next-unread page catches up to CURR. */
  220. if (pkt == cur)
  221. return -1;
  222. addr = (unsigned short)pkt << 8;
  223. /* 4-byte header: status, next page, length lo, length hi. */
  224. {
  225. unsigned char hdr[4];
  226. remote_read(addr, hdr, 4);
  227. status = hdr[0];
  228. next = hdr[1];
  229. len = (unsigned short)hdr[2] | ((unsigned short)hdr[3] << 8);
  230. }
  231. /* Advance boundary past this frame. */
  232. if (next == RX_START)
  233. nwrite(NE_BNRY, RX_STOP - 1);
  234. else
  235. nwrite(NE_BNRY, next - 1);
  236. /* Skip errored or oversized frames. */
  237. if (!(status & 0x01))
  238. continue;
  239. if (len < 4 || (unsigned int)(len - 4) > maxlen)
  240. continue;
  241. remote_read(addr + 4, buf, (unsigned short)(len - 4));
  242. return (int)(len - 4);
  243. }
  244. }
  245. int ne2000_tx(const unsigned char *buf, unsigned int len)
  246. {
  247. unsigned int t0;
  248. unsigned char isr, tsr;
  249. if (!present)
  250. return -1;
  251. if (len < 14 || len > 1514)
  252. return -1;
  253. /* Copy the frame into on-card SRAM. */
  254. remote_write((unsigned short)(TX_PAGE << 8), buf, (unsigned short)len);
  255. /* Program transmit page + byte count. */
  256. nwrite(NE_CR, CR_NODMA | CR_START);
  257. nwrite(NE_TPSR, TX_PAGE);
  258. nwrite(NE_TBCR0, len & 0xFF);
  259. nwrite(NE_TBCR1, (len >> 8) & 0xFF);
  260. /* Clear TX status bits, then fire. */
  261. nwrite(NE_ISR, ISR_PTX | ISR_TXE);
  262. nwrite(NE_CR, CR_NODMA | CR_START | CR_TXP);
  263. /* Poll for completion. */
  264. t0 = get_ticks_ms();
  265. for (;;) {
  266. isr = nread(NE_ISR);
  267. if (isr & (ISR_PTX | ISR_TXE))
  268. break;
  269. if (get_ticks_ms() - t0 > 100)
  270. return -1;
  271. }
  272. tsr = nread(NE_TSR);
  273. if (!(tsr & TSR_PTX))
  274. return -1; /* collision/abort/underrun */
  275. return 0;
  276. }
  277. static void put_hex2(unsigned char v)
  278. {
  279. static const char hex[] = "0123456789ABCDEF";
  280. io_putchar(hex[(v >> 4) & 0xF]);
  281. io_putchar(hex[v & 0xF]);
  282. }
  283. void ne2000_print_info(void)
  284. {
  285. unsigned int i;
  286. if (!present) {
  287. io_putstr("net: no RTL8029AS found\n");
  288. return;
  289. }
  290. io_putstr("RTL8029AS iobase=0x");
  291. io_puthex(iobase);
  292. io_putstr(" mac=");
  293. for (i = 0; i < 6; i++) {
  294. if (i)
  295. io_putchar(':');
  296. put_hex2(mac[i]);
  297. }
  298. io_putchar('\n');
  299. }