ether2000.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. #include "../port/netif.h"
  9. #include "etherif.h"
  10. #include "ether8390.h"
  11. /*
  12. * Driver written for the 'Notebook Computer Ethernet LAN Adapter',
  13. * a plug-in to the bus-slot on the rear of the Gateway NOMAD 425DXL
  14. * laptop. The manual says NE2000 compatible.
  15. * The interface appears to be pretty well described in the National
  16. * Semiconductor Local Area Network Databook (1992) as one of the
  17. * AT evaluation cards.
  18. *
  19. * The NE2000 is really just a DP8390[12] plus a data port
  20. * and a reset port.
  21. */
  22. enum {
  23. Data = 0x10, /* offset from I/O base of data port */
  24. Reset = 0x1F, /* offset from I/O base of reset port */
  25. };
  26. typedef struct Ctlr Ctlr;
  27. typedef struct Ctlr {
  28. Pcidev* pcidev;
  29. Ctlr* next;
  30. int active;
  31. } Ctlr;
  32. static Ctlr* ctlrhead;
  33. static Ctlr* ctlrtail;
  34. static struct {
  35. char* name;
  36. int id;
  37. } ne2000pci[] = {
  38. { "Realtek 8029", (0x8029<<16)|0x10EC, },
  39. { "Winbond 89C940", (0x0940<<16)|0x1050, },
  40. { nil },
  41. };
  42. static Ctlr*
  43. ne2000match(Ether* edev, int id)
  44. {
  45. int port;
  46. Pcidev *p;
  47. Ctlr *ctlr;
  48. /*
  49. * Any adapter matches if no edev->port is supplied,
  50. * otherwise the ports must match.
  51. */
  52. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  53. if(ctlr->active)
  54. continue;
  55. p = ctlr->pcidev;
  56. if(((p->did<<16)|p->vid) != id)
  57. continue;
  58. port = p->mem[0].bar & ~0x01;
  59. if(edev->port != 0 && edev->port != port)
  60. continue;
  61. /*
  62. * It suffices to fill these in,
  63. * the rest is gleaned from the card.
  64. */
  65. edev->port = port;
  66. edev->irq = p->intl;
  67. ctlr->active = 1;
  68. return ctlr;
  69. }
  70. return nil;
  71. }
  72. static void
  73. ne2000pnp(Ether* edev)
  74. {
  75. int i, id;
  76. Pcidev *p;
  77. Ctlr *ctlr;
  78. /*
  79. * Make a list of all ethernet controllers
  80. * if not already done.
  81. */
  82. if(ctlrhead == nil){
  83. p = nil;
  84. while(p = pcimatch(p, 0, 0)){
  85. if(p->ccrb != 0x02 || p->ccru != 0)
  86. continue;
  87. ctlr = malloc(sizeof(Ctlr));
  88. ctlr->pcidev = p;
  89. if(ctlrhead != nil)
  90. ctlrtail->next = ctlr;
  91. else
  92. ctlrhead = ctlr;
  93. ctlrtail = ctlr;
  94. }
  95. }
  96. /*
  97. * Is it a card with an unrecognised vid+did?
  98. * Normally a search is made through all the found controllers
  99. * for one which matches any of the known vid+did pairs.
  100. * If a vid+did pair is specified a search is made for that
  101. * specific controller only.
  102. */
  103. id = 0;
  104. for(i = 0; i < edev->nopt; i++){
  105. if(cistrncmp(edev->opt[i], "id=", 3) == 0)
  106. id = strtol(&edev->opt[i][3], nil, 0);
  107. }
  108. if(id != 0)
  109. ne2000match(edev, id);
  110. else for(i = 0; ne2000pci[i].name; i++){
  111. if(ne2000match(edev, ne2000pci[i].id) != nil)
  112. break;
  113. }
  114. }
  115. static int
  116. ne2000reset(Ether* edev)
  117. {
  118. ushort buf[16];
  119. ulong port;
  120. Dp8390 *dp8390;
  121. int i;
  122. uchar ea[Eaddrlen];
  123. if(edev->port == 0)
  124. ne2000pnp(edev);
  125. /*
  126. * Set up the software configuration.
  127. * Use defaults for irq, mem and size
  128. * if not specified.
  129. * Must have a port, no more default.
  130. */
  131. if(edev->port == 0)
  132. return -1;
  133. if(edev->irq == 0)
  134. edev->irq = 2;
  135. if(edev->mem == 0)
  136. edev->mem = 0x4000;
  137. if(edev->size == 0)
  138. edev->size = 16*1024;
  139. port = edev->port;
  140. if(ioalloc(edev->port, 0x20, 0, "ne2000") < 0)
  141. return -1;
  142. edev->ctlr = malloc(sizeof(Dp8390));
  143. dp8390 = edev->ctlr;
  144. dp8390->width = 2;
  145. dp8390->ram = 0;
  146. dp8390->port = port;
  147. dp8390->data = port+Data;
  148. dp8390->tstart = HOWMANY(edev->mem, Dp8390BufSz);
  149. dp8390->pstart = dp8390->tstart + HOWMANY(sizeof(Etherpkt), Dp8390BufSz);
  150. dp8390->pstop = dp8390->tstart + HOWMANY(edev->size, Dp8390BufSz);
  151. dp8390->dummyrr = 1;
  152. for(i = 0; i < edev->nopt; i++){
  153. if(strcmp(edev->opt[i], "nodummyrr"))
  154. continue;
  155. dp8390->dummyrr = 0;
  156. break;
  157. }
  158. /*
  159. * Reset the board. This is done by doing a read
  160. * followed by a write to the Reset address.
  161. */
  162. buf[0] = inb(port+Reset);
  163. delay(2);
  164. outb(port+Reset, buf[0]);
  165. delay(2);
  166. /*
  167. * Init the (possible) chip, then use the (possible)
  168. * chip to read the (possible) PROM for ethernet address
  169. * and a marker byte.
  170. * Could just look at the DP8390 command register after
  171. * initialisation has been tried, but that wouldn't be
  172. * enough, there are other ethernet boards which could
  173. * match.
  174. * Parallels has buf[0x0E] == 0x00 whereas real hardware
  175. * usually has 0x57.
  176. */
  177. dp8390reset(edev);
  178. memset(buf, 0, sizeof(buf));
  179. dp8390read(dp8390, buf, 0, sizeof(buf));
  180. i = buf[0x0E] & 0xFF;
  181. if((i != 0x00 && i != 0x57) || (buf[0x0F] & 0xFF) != 0x57){
  182. iofree(edev->port);
  183. free(edev->ctlr);
  184. return -1;
  185. }
  186. /*
  187. * Stupid machine. Shorts were asked for,
  188. * shorts were delivered, although the PROM is a byte array.
  189. * Set the ethernet address.
  190. */
  191. memset(ea, 0, Eaddrlen);
  192. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  193. for(i = 0; i < sizeof(edev->ea); i++)
  194. edev->ea[i] = buf[i];
  195. }
  196. dp8390setea(edev);
  197. return 0;
  198. }
  199. void
  200. ether2000link(void)
  201. {
  202. addethercard("NE2000", ne2000reset);
  203. }