ether2000.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if(ctlr == nil)
  89. error(Enomem);
  90. ctlr->pcidev = p;
  91. if(ctlrhead != nil)
  92. ctlrtail->next = ctlr;
  93. else
  94. ctlrhead = ctlr;
  95. ctlrtail = ctlr;
  96. }
  97. }
  98. /*
  99. * Is it a card with an unrecognised vid+did?
  100. * Normally a search is made through all the found controllers
  101. * for one which matches any of the known vid+did pairs.
  102. * If a vid+did pair is specified a search is made for that
  103. * specific controller only.
  104. */
  105. id = 0;
  106. for(i = 0; i < edev->nopt; i++){
  107. if(cistrncmp(edev->opt[i], "id=", 3) == 0)
  108. id = strtol(&edev->opt[i][3], nil, 0);
  109. }
  110. if(id != 0)
  111. ne2000match(edev, id);
  112. else for(i = 0; ne2000pci[i].name; i++){
  113. if(ne2000match(edev, ne2000pci[i].id) != nil)
  114. break;
  115. }
  116. }
  117. static int
  118. ne2000reset(Ether* edev)
  119. {
  120. ushort buf[16];
  121. ulong port;
  122. Dp8390 *dp8390;
  123. int i;
  124. uchar ea[Eaddrlen];
  125. if(edev->port == 0)
  126. ne2000pnp(edev);
  127. /*
  128. * Set up the software configuration.
  129. * Use defaults for irq, mem and size
  130. * if not specified.
  131. * Must have a port, no more default.
  132. */
  133. if(edev->port == 0)
  134. return -1;
  135. if(edev->irq == 0)
  136. edev->irq = 2;
  137. if(edev->mem == 0)
  138. edev->mem = 0x4000;
  139. if(edev->size == 0)
  140. edev->size = 16*1024;
  141. port = edev->port;
  142. if(ioalloc(edev->port, 0x20, 0, "ne2000") < 0)
  143. return -1;
  144. edev->ctlr = malloc(sizeof(Dp8390));
  145. dp8390 = edev->ctlr;
  146. if(dp8390 == nil)
  147. error(Enomem);
  148. dp8390->width = 2;
  149. dp8390->ram = 0;
  150. dp8390->port = port;
  151. dp8390->data = port+Data;
  152. dp8390->tstart = HOWMANY(edev->mem, Dp8390BufSz);
  153. dp8390->pstart = dp8390->tstart + HOWMANY(sizeof(Etherpkt), Dp8390BufSz);
  154. dp8390->pstop = dp8390->tstart + HOWMANY(edev->size, Dp8390BufSz);
  155. dp8390->dummyrr = 1;
  156. for(i = 0; i < edev->nopt; i++){
  157. if(strcmp(edev->opt[i], "nodummyrr"))
  158. continue;
  159. dp8390->dummyrr = 0;
  160. break;
  161. }
  162. /*
  163. * Reset the board. This is done by doing a read
  164. * followed by a write to the Reset address.
  165. */
  166. buf[0] = inb(port+Reset);
  167. delay(2);
  168. outb(port+Reset, buf[0]);
  169. delay(2);
  170. /*
  171. * Init the (possible) chip, then use the (possible)
  172. * chip to read the (possible) PROM for ethernet address
  173. * and a marker byte.
  174. * Could just look at the DP8390 command register after
  175. * initialisation has been tried, but that wouldn't be
  176. * enough, there are other ethernet boards which could
  177. * match.
  178. * Parallels has buf[0x0E] == 0x00 whereas real hardware
  179. * usually has 0x57.
  180. */
  181. dp8390reset(edev);
  182. memset(buf, 0, sizeof(buf));
  183. dp8390read(dp8390, buf, 0, sizeof(buf));
  184. i = buf[0x0E] & 0xFF;
  185. if((i != 0x00 && i != 0x57) || (buf[0x0F] & 0xFF) != 0x57){
  186. iofree(edev->port);
  187. free(edev->ctlr);
  188. return -1;
  189. }
  190. /*
  191. * Stupid machine. Shorts were asked for,
  192. * shorts were delivered, although the PROM is a byte array.
  193. * Set the ethernet address.
  194. */
  195. memset(ea, 0, Eaddrlen);
  196. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  197. for(i = 0; i < sizeof(edev->ea); i++)
  198. edev->ea[i] = buf[i];
  199. }
  200. dp8390setea(edev);
  201. return 0;
  202. }
  203. void
  204. ether2000link(void)
  205. {
  206. addethercard("NE2000", ne2000reset);
  207. }