ether589.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * 3C589 and 3C562.
  3. * To do:
  4. * check xcvr10Base2 still works (is GlobalReset necessary?).
  5. */
  6. #include "u.h"
  7. #include "../port/lib.h"
  8. #include "mem.h"
  9. #include "dat.h"
  10. #include "fns.h"
  11. #include "io.h"
  12. #include "../port/error.h"
  13. #include "../port/netif.h"
  14. #include "etherif.h"
  15. enum { /* all windows */
  16. CommandR = 0x000E,
  17. IntStatusR = 0x000E,
  18. };
  19. enum { /* Commands */
  20. GlobalReset = 0x0000,
  21. SelectRegisterWindow = 0x0001,
  22. RxReset = 0x0005,
  23. TxReset = 0x000B,
  24. AcknowledgeInterrupt = 0x000D,
  25. };
  26. enum { /* IntStatus bits */
  27. commandInProgress = 0x1000,
  28. };
  29. #define COMMAND(port, cmd, a) outs((port)+CommandR, ((cmd)<<11)|(a))
  30. #define STATUS(port) ins((port)+IntStatusR)
  31. enum { /* Window 0 - setup */
  32. Wsetup = 0x0000,
  33. /* registers */
  34. ManufacturerID = 0x0000, /* 3C5[08]*, 3C59[27] */
  35. ProductID = 0x0002, /* 3C5[08]*, 3C59[27] */
  36. ConfigControl = 0x0004, /* 3C5[08]*, 3C59[27] */
  37. AddressConfig = 0x0006, /* 3C5[08]*, 3C59[27] */
  38. ResourceConfig = 0x0008, /* 3C5[08]*, 3C59[27] */
  39. EepromCommand = 0x000A,
  40. EepromData = 0x000C,
  41. /* AddressConfig Bits */
  42. autoSelect9 = 0x0080,
  43. xcvrMask9 = 0xC000,
  44. /* ConfigControl bits */
  45. Ena = 0x0001,
  46. base10TAvailable9 = 0x0200,
  47. coaxAvailable9 = 0x1000,
  48. auiAvailable9 = 0x2000,
  49. /* EepromCommand bits */
  50. EepromReadRegister = 0x0080,
  51. EepromBusy = 0x8000,
  52. };
  53. enum { /* Window 1 - operating set */
  54. Wop = 0x0001,
  55. };
  56. enum { /* Window 3 - FIFO management */
  57. Wfifo = 0x0003,
  58. /* registers */
  59. InternalConfig = 0x0000, /* 3C509B, 3C589, 3C59[0257] */
  60. /* InternalConfig bits */
  61. xcvr10BaseT = 0x00000000,
  62. xcvr10Base2 = 0x00300000,
  63. };
  64. enum { /* Window 4 - diagnostic */
  65. Wdiagnostic = 0x0004,
  66. /* registers */
  67. MediaStatus = 0x000A,
  68. /* MediaStatus bits */
  69. linkBeatDetect = 0x0800,
  70. };
  71. extern int etherelnk3reset(Ether*);
  72. static char *tcmpcmcia[] = {
  73. "3C589", /* 3COM 589[ABCD] */
  74. "3C562", /* 3COM 562 */
  75. "589E", /* 3COM Megahertz 589E */
  76. nil,
  77. };
  78. static int
  79. configASIC(Ether* ether, int port, int xcvr)
  80. {
  81. int x;
  82. /* set Window 0 configuration registers */
  83. COMMAND(port, SelectRegisterWindow, Wsetup);
  84. outs(port+ConfigControl, Ena);
  85. /* IRQ must be 3 on 3C589/3C562 */
  86. outs(port + ResourceConfig, 0x3F00);
  87. x = ins(port+AddressConfig) & ~xcvrMask9;
  88. x |= (xcvr>>20)<<14;
  89. outs(port+AddressConfig, x);
  90. COMMAND(port, TxReset, 0);
  91. while(STATUS(port) & commandInProgress)
  92. ;
  93. COMMAND(port, RxReset, 0);
  94. while(STATUS(port) & commandInProgress)
  95. ;
  96. return etherelnk3reset(ether);
  97. }
  98. static int
  99. reset(Ether* ether)
  100. {
  101. int i, t, slot;
  102. char *type;
  103. int port;
  104. enum { WantAny, Want10BT, Want10B2 };
  105. int want;
  106. uchar ea[6];
  107. char *p;
  108. if(ether->irq == 0)
  109. ether->irq = 10;
  110. if(ether->port == 0)
  111. ether->port = 0x240;
  112. port = ether->port;
  113. if(ioalloc(port, 0x10, 0, "3C589") < 0)
  114. return -1;
  115. type = nil;
  116. slot = -1;
  117. for(i = 0; tcmpcmcia[i] != nil; i++){
  118. type = tcmpcmcia[i];
  119. if((slot = pcmspecial(type, ether)) >= 0)
  120. break;
  121. }
  122. ether->type = type; /* must be set before calling configASIC */
  123. if(slot < 0){
  124. iofree(port);
  125. return -1;
  126. }
  127. /*
  128. * Read Ethernet address from card memory
  129. * on 3C562, but only if the user has not
  130. * overridden it.
  131. */
  132. memset(ea, 0, sizeof ea);
  133. if(memcmp(ea, ether->ea, 6) == 0 && strcmp(type, "3C562") == 0) {
  134. if(pcmcistuple(slot, 0x88, -1, ea, 6) == 6) {
  135. for(i = 0; i < 6; i += 2){
  136. t = ea[i];
  137. ea[i] = ea[i+1];
  138. ea[i+1] = t;
  139. }
  140. memmove(ether->ea, ea, 6);
  141. }
  142. }
  143. /*
  144. * Allow user to specify desired media in plan9.ini
  145. */
  146. want = WantAny;
  147. for(i = 0; i < ether->nopt; i++){
  148. if(cistrncmp(ether->opt[i], "media=", 6) != 0)
  149. continue;
  150. p = ether->opt[i]+6;
  151. if(cistrcmp(p, "10base2") == 0)
  152. want = Want10B2;
  153. else if(cistrcmp(p, "10baseT") == 0)
  154. want = Want10BT;
  155. }
  156. /* try configuring as a 10BaseT */
  157. if(want==WantAny || want==Want10BT){
  158. if(configASIC(ether, port, xcvr10BaseT) < 0){
  159. pcmspecialclose(slot);
  160. iofree(port);
  161. return -1;
  162. }
  163. delay(100);
  164. COMMAND(port, SelectRegisterWindow, Wdiagnostic);
  165. if((ins(port+MediaStatus)&linkBeatDetect) || want==Want10BT){
  166. COMMAND(port, SelectRegisterWindow, Wop);
  167. print("#l%d: xcvr10BaseT %s\n", ether->ctlrno, type);
  168. return 0;
  169. }
  170. }
  171. /* try configuring as a 10base2 */
  172. if(want==WantAny || want==Want10B2){
  173. COMMAND(port, GlobalReset, 0);
  174. if(configASIC(ether, port, xcvr10Base2) < 0){
  175. pcmspecialclose(slot);
  176. iofree(port);
  177. return -1;
  178. }
  179. print("#l%d: xcvr10Base2 %s\n", ether->ctlrno, type);
  180. return 0;
  181. }
  182. return -1; /* not reached */
  183. }
  184. void
  185. ether589link(void)
  186. {
  187. addethercard("3C589", reset);
  188. addethercard("3C562", reset);
  189. addethercard("589E", reset);
  190. }