ether589.c 4.5 KB

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