You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ne2k_isa.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**************************************************************************
  2. ETHERBOOT - BOOTP/TFTP Bootstrap Program
  3. Author: Martin Renters
  4. Date: May/94
  5. This code is based heavily on David Greenman's if_ed.c driver
  6. Copyright (C) 1993-1994, David Greenman, Martin Renters.
  7. This software may be used, modified, copied, distributed, and sold, in
  8. both source and binary form provided that the above copyright and these
  9. terms are retained. Under no circumstances are the authors responsible for
  10. the proper functioning of this software, nor do the authors assume any
  11. responsibility for damages incurred with its use.
  12. Multicast support added by Timothy Legge (timlegge@users.sourceforge.net) 09/28/2003
  13. Relocation support added by Ken Yap (ken_yap@users.sourceforge.net) 28/12/02
  14. Card Detect support adapted from the eCos driver (Christian Plessl <cplessl@ee.ethz.ch>)
  15. Extracted from ns8390.c and adapted by Pantelis Koukousoulas <pktoss@gmail.com>
  16. **************************************************************************/
  17. FILE_LICENCE ( BSD2 );
  18. #include "ns8390.h"
  19. #include "etherboot.h"
  20. #include "nic.h"
  21. #include <ipxe/ethernet.h>
  22. #include <ipxe/isa.h>
  23. #include <errno.h>
  24. #define ASIC_PIO NE_DATA
  25. static unsigned char eth_vendor, eth_flags;
  26. static unsigned short eth_nic_base, eth_asic_base;
  27. static unsigned char eth_memsize, eth_rx_start, eth_tx_start;
  28. static Address eth_bmem, eth_rmem;
  29. static unsigned char eth_drain_receiver;
  30. static struct nic_operations ne_operations;
  31. static void ne_reset(struct nic *nic, struct isa_device *isa);
  32. static isa_probe_addr_t ne_probe_addrs[] = { 0x300, 0x280, 0x320, 0x340, 0x380, 0x220, };
  33. /**************************************************************************
  34. ETH_PIO_READ - Read a frame via Programmed I/O
  35. **************************************************************************/
  36. static void eth_pio_read(unsigned int src, unsigned char *dst, unsigned int cnt) {
  37. outb(D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
  38. outb(cnt, eth_nic_base + D8390_P0_RBCR0);
  39. outb(cnt >> 8, eth_nic_base + D8390_P0_RBCR1);
  40. outb(src, eth_nic_base + D8390_P0_RSAR0);
  41. outb(src >> 8, eth_nic_base + D8390_P0_RSAR1);
  42. outb(D8390_COMMAND_RD0 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
  43. if (eth_flags & FLAG_16BIT)
  44. cnt = (cnt + 1) >> 1;
  45. while (cnt--) {
  46. if (eth_flags & FLAG_16BIT) {
  47. *((unsigned short *) dst) = inw(eth_asic_base + ASIC_PIO);
  48. dst += 2;
  49. } else
  50. *(dst++) = inb(eth_asic_base + ASIC_PIO);
  51. }
  52. }
  53. /**************************************************************************
  54. ETH_PIO_WRITE - Write a frame via Programmed I/O
  55. **************************************************************************/
  56. static void eth_pio_write(const unsigned char *src, unsigned int dst,
  57. unsigned int cnt) {
  58. outb(D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
  59. outb(D8390_ISR_RDC, eth_nic_base + D8390_P0_ISR);
  60. outb(cnt, eth_nic_base + D8390_P0_RBCR0);
  61. outb(cnt >> 8, eth_nic_base + D8390_P0_RBCR1);
  62. outb(dst, eth_nic_base + D8390_P0_RSAR0);
  63. outb(dst >> 8, eth_nic_base + D8390_P0_RSAR1);
  64. outb(D8390_COMMAND_RD1 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
  65. if (eth_flags & FLAG_16BIT)
  66. cnt = (cnt + 1) >> 1;
  67. while (cnt--) {
  68. if (eth_flags & FLAG_16BIT) {
  69. outw(*((unsigned short *) src), eth_asic_base + ASIC_PIO);
  70. src += 2;
  71. } else
  72. outb(*(src++), eth_asic_base + ASIC_PIO);
  73. }
  74. }
  75. /**************************************************************************
  76. enable_multicast - Enable Multicast
  77. **************************************************************************/
  78. static void enable_multicast(unsigned short eth_nic_base) {
  79. unsigned char mcfilter[8];
  80. int i;
  81. memset(mcfilter, 0xFF, 8);
  82. outb(4, eth_nic_base + D8390_P0_RCR);
  83. outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS1, eth_nic_base + D8390_P0_COMMAND);
  84. for (i = 0; i < 8; i++) {
  85. outb(mcfilter[i], eth_nic_base + 8 + i);
  86. if (inb(eth_nic_base + 8 + i) != mcfilter[i])
  87. DBG("Error SMC 83C690 Multicast filter read/write mishap %d\n",
  88. i);
  89. }
  90. outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS0, eth_nic_base + D8390_P0_COMMAND);
  91. outb(4 | 0x08, eth_nic_base + D8390_P0_RCR);
  92. }
  93. /**************************************************************************
  94. NE_PROBE1 - Look for an adapter on the ISA bus
  95. **************************************************************************/
  96. static int ne_probe1(isa_probe_addr_t ioaddr) {
  97. //From the eCos driver
  98. unsigned int regd;
  99. unsigned int state;
  100. state = inb(ioaddr);
  101. outb(ioaddr, D8390_COMMAND_RD2 | D8390_COMMAND_PS1 | D8390_COMMAND_STP);
  102. regd = inb(ioaddr + D8390_P0_TCR);
  103. if (inb(ioaddr + D8390_P0_TCR)) {
  104. outb(ioaddr, state);
  105. outb(ioaddr + 0x0d, regd);
  106. return 0;
  107. }
  108. return 1;
  109. }
  110. /**************************************************************************
  111. NE_PROBE - Initialize an adapter ???
  112. **************************************************************************/
  113. static int ne_probe(struct nic *nic, struct isa_device *isa) {
  114. int i;
  115. unsigned char c;
  116. unsigned char romdata[16];
  117. unsigned char testbuf[32];
  118. eth_vendor = VENDOR_NONE;
  119. eth_drain_receiver = 0;
  120. nic->irqno = 0;
  121. nic->ioaddr = isa->ioaddr;
  122. eth_nic_base = isa->ioaddr;
  123. /******************************************************************
  124. Search for NE1000/2000 if no WD/SMC or 3com cards
  125. ******************************************************************/
  126. if (eth_vendor == VENDOR_NONE) {
  127. static unsigned char test[] = "NE*000 memory";
  128. eth_bmem = 0; /* No shared memory */
  129. eth_flags = FLAG_PIO;
  130. eth_asic_base = eth_nic_base + NE_ASIC_OFFSET;
  131. eth_memsize = MEM_16384;
  132. eth_tx_start = 32;
  133. eth_rx_start = 32 + D8390_TXBUF_SIZE;
  134. c = inb(eth_asic_base + NE_RESET);
  135. outb(c, eth_asic_base + NE_RESET);
  136. (void) inb(0x84);
  137. outb(D8390_COMMAND_STP | D8390_COMMAND_RD2, eth_nic_base
  138. + D8390_P0_COMMAND);
  139. outb(D8390_RCR_MON, eth_nic_base + D8390_P0_RCR);
  140. outb(D8390_DCR_FT1 | D8390_DCR_LS, eth_nic_base + D8390_P0_DCR);
  141. outb(MEM_8192, eth_nic_base + D8390_P0_PSTART);
  142. outb(MEM_16384, eth_nic_base + D8390_P0_PSTOP);
  143. eth_pio_write((unsigned char *) test, 8192, sizeof(test));
  144. eth_pio_read(8192, testbuf, sizeof(test));
  145. if (!memcmp(test, testbuf, sizeof(test)))
  146. goto out;
  147. eth_flags |= FLAG_16BIT;
  148. eth_memsize = MEM_32768;
  149. eth_tx_start = 64;
  150. eth_rx_start = 64 + D8390_TXBUF_SIZE;
  151. outb(D8390_DCR_WTS | D8390_DCR_FT1 | D8390_DCR_LS, eth_nic_base
  152. + D8390_P0_DCR);
  153. outb(MEM_16384, eth_nic_base + D8390_P0_PSTART);
  154. outb(MEM_32768, eth_nic_base + D8390_P0_PSTOP);
  155. eth_pio_write((unsigned char *) test, 16384, sizeof(test));
  156. eth_pio_read(16384, testbuf, sizeof(test));
  157. if (!memcmp(testbuf, test, sizeof(test)))
  158. goto out;
  159. out:
  160. if (eth_nic_base == 0)
  161. return (0);
  162. if (eth_nic_base > ISA_MAX_ADDR) /* PCI probably */
  163. eth_flags |= FLAG_16BIT;
  164. eth_vendor = VENDOR_NOVELL;
  165. eth_pio_read(0, romdata, sizeof(romdata));
  166. for (i = 0; i < ETH_ALEN; i++) {
  167. nic->node_addr[i] = romdata[i + ((eth_flags & FLAG_16BIT) ? i : 0)];
  168. }
  169. nic->ioaddr = eth_nic_base;
  170. DBG("\nNE%c000 base %4.4x, MAC Addr %s\n",
  171. (eth_flags & FLAG_16BIT) ? '2' : '1', eth_nic_base, eth_ntoa(
  172. nic->node_addr));
  173. }
  174. if (eth_vendor == VENDOR_NONE)
  175. return (0);
  176. if (eth_vendor != VENDOR_3COM)
  177. eth_rmem = eth_bmem;
  178. ne_reset(nic, isa);
  179. nic->nic_op = &ne_operations;
  180. return 1;
  181. }
  182. /**************************************************************************
  183. NE_DISABLE - Turn off adapter
  184. **************************************************************************/
  185. static void ne_disable(struct nic *nic, struct isa_device *isa) {
  186. ne_reset(nic, isa);
  187. }
  188. /**************************************************************************
  189. NE_RESET - Reset adapter
  190. **************************************************************************/
  191. static void ne_reset(struct nic *nic, struct isa_device *isa __unused)
  192. {
  193. int i;
  194. eth_drain_receiver = 0;
  195. outb(D8390_COMMAND_PS0 | D8390_COMMAND_RD2 |
  196. D8390_COMMAND_STP, eth_nic_base+D8390_P0_COMMAND);
  197. if (eth_flags & FLAG_16BIT)
  198. outb(0x49, eth_nic_base+D8390_P0_DCR);
  199. else
  200. outb(0x48, eth_nic_base+D8390_P0_DCR);
  201. outb(0, eth_nic_base+D8390_P0_RBCR0);
  202. outb(0, eth_nic_base+D8390_P0_RBCR1);
  203. outb(0x20, eth_nic_base+D8390_P0_RCR); /* monitor mode */
  204. outb(2, eth_nic_base+D8390_P0_TCR);
  205. outb(eth_tx_start, eth_nic_base+D8390_P0_TPSR);
  206. outb(eth_rx_start, eth_nic_base+D8390_P0_PSTART);
  207. outb(eth_memsize, eth_nic_base+D8390_P0_PSTOP);
  208. outb(eth_memsize - 1, eth_nic_base+D8390_P0_BOUND);
  209. outb(0xFF, eth_nic_base+D8390_P0_ISR);
  210. outb(0, eth_nic_base+D8390_P0_IMR);
  211. outb(D8390_COMMAND_PS1 |
  212. D8390_COMMAND_RD2 | D8390_COMMAND_STP, eth_nic_base+D8390_P0_COMMAND);
  213. for (i=0; i<ETH_ALEN; i++)
  214. outb(nic->node_addr[i], eth_nic_base+D8390_P1_PAR0+i);
  215. for (i=0; i<ETH_ALEN; i++)
  216. outb(0xFF, eth_nic_base+D8390_P1_MAR0+i);
  217. outb(eth_rx_start, eth_nic_base+D8390_P1_CURR);
  218. outb(D8390_COMMAND_PS0 |
  219. D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base+D8390_P0_COMMAND);
  220. outb(0xFF, eth_nic_base+D8390_P0_ISR);
  221. outb(0, eth_nic_base+D8390_P0_TCR); /* transmitter on */
  222. outb(4, eth_nic_base+D8390_P0_RCR); /* allow rx broadcast frames */
  223. enable_multicast(eth_nic_base);
  224. }
  225. /**************************************************************************
  226. NE_POLL - Wait for a frame
  227. **************************************************************************/
  228. static int ne_poll(struct nic *nic __unused, int retrieve __unused)
  229. {
  230. int ret = 0;
  231. unsigned char rstat, curr, next;
  232. unsigned short len, frag;
  233. unsigned short pktoff;
  234. unsigned char *p;
  235. struct ringbuffer pkthdr;
  236. rstat = inb(eth_nic_base+D8390_P0_RSR);
  237. if (!(rstat & D8390_RSTAT_PRX)) return(0);
  238. next = inb(eth_nic_base+D8390_P0_BOUND)+1;
  239. if (next >= eth_memsize) next = eth_rx_start;
  240. outb(D8390_COMMAND_PS1, eth_nic_base+D8390_P0_COMMAND);
  241. curr = inb(eth_nic_base+D8390_P1_CURR);
  242. outb(D8390_COMMAND_PS0, eth_nic_base+D8390_P0_COMMAND);
  243. if (curr >= eth_memsize) curr=eth_rx_start;
  244. if (curr == next) return(0);
  245. if ( ! retrieve ) return 1;
  246. pktoff = next << 8;
  247. if (eth_flags & FLAG_PIO)
  248. eth_pio_read(pktoff, (unsigned char *)&pkthdr, 4);
  249. else
  250. memcpy(&pkthdr, bus_to_virt(eth_rmem + pktoff), 4);
  251. pktoff += sizeof(pkthdr);
  252. /* incoming length includes FCS so must sub 4 */
  253. len = pkthdr.len - 4;
  254. if ((pkthdr.status & D8390_RSTAT_PRX) == 0 || len < ETH_ZLEN
  255. || len> ETH_FRAME_LEN) {
  256. DBG("Bogus packet, ignoring\n");
  257. return (0);
  258. }
  259. else {
  260. p = nic->packet;
  261. nic->packetlen = len; /* available to caller */
  262. frag = (eth_memsize << 8) - pktoff;
  263. if (len> frag) { /* We have a wrap-around */
  264. /* read first part */
  265. if (eth_flags & FLAG_PIO)
  266. eth_pio_read(pktoff, p, frag);
  267. else
  268. memcpy(p, bus_to_virt(eth_rmem + pktoff), frag);
  269. pktoff = eth_rx_start << 8;
  270. p += frag;
  271. len -= frag;
  272. }
  273. /* read second part */
  274. if (eth_flags & FLAG_PIO)
  275. eth_pio_read(pktoff, p, len);
  276. else
  277. memcpy(p, bus_to_virt(eth_rmem + pktoff), len);
  278. ret = 1;
  279. }
  280. next = pkthdr.next; /* frame number of next packet */
  281. if (next == eth_rx_start)
  282. next = eth_memsize;
  283. outb(next-1, eth_nic_base+D8390_P0_BOUND);
  284. return(ret);
  285. }
  286. /**************************************************************************
  287. NE_TRANSMIT - Transmit a frame
  288. **************************************************************************/
  289. static void ne_transmit(struct nic *nic, const char *d, /* Destination */
  290. unsigned int t, /* Type */
  291. unsigned int s, /* size */
  292. const char *p) { /* Packet */
  293. /* Programmed I/O */
  294. unsigned short type;
  295. type = (t >> 8) | (t << 8);
  296. eth_pio_write((unsigned char *) d, eth_tx_start << 8, ETH_ALEN);
  297. eth_pio_write(nic->node_addr, (eth_tx_start << 8) + ETH_ALEN, ETH_ALEN);
  298. /* bcc generates worse code without (const+const) below */
  299. eth_pio_write((unsigned char *) &type, (eth_tx_start << 8) + (ETH_ALEN
  300. + ETH_ALEN), 2);
  301. eth_pio_write((unsigned char *) p, (eth_tx_start << 8) + ETH_HLEN, s);
  302. s += ETH_HLEN;
  303. if (s < ETH_ZLEN)
  304. s = ETH_ZLEN;
  305. outb(D8390_COMMAND_PS0 | D8390_COMMAND_RD2 | D8390_COMMAND_STA,
  306. eth_nic_base + D8390_P0_COMMAND);
  307. outb(eth_tx_start, eth_nic_base + D8390_P0_TPSR);
  308. outb(s, eth_nic_base + D8390_P0_TBCR0);
  309. outb(s >> 8, eth_nic_base + D8390_P0_TBCR1);
  310. outb(D8390_COMMAND_PS0 | D8390_COMMAND_TXP | D8390_COMMAND_RD2
  311. | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
  312. }
  313. static struct nic_operations ne_operations = { .connect = dummy_connect,
  314. .poll = ne_poll, .transmit = ne_transmit, .irq = dummy_irq,
  315. };
  316. ISA_DRIVER ( ne_driver, ne_probe_addrs, ne_probe1,
  317. GENERIC_ISAPNP_VENDOR, 0x0600 );
  318. DRIVER ( "ne", nic_driver, isapnp_driver, ne_driver,
  319. ne_probe, ne_disable );
  320. ISA_ROM("ne","NE1000/2000 and clones");