usbehcipc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * PC-specific code for
  3. * USB Enhanced Host Controller Interface (EHCI) driver
  4. * High speed USB 2.0.
  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/usb.h"
  14. #include "../port/portusbehci.h"
  15. #include "usbehci.h"
  16. static Ctlr* ctlrs[Nhcis];
  17. static int maxehci = Nhcis;
  18. /* Isn't this cap list search in a helper function? */
  19. static void
  20. getehci(Ctlr* ctlr)
  21. {
  22. int i, ptr, cap, sem;
  23. ptr = (ctlr->capio->capparms >> Ceecpshift) & Ceecpmask;
  24. for(; ptr != 0; ptr = pcicfgr8(ctlr->pcidev, ptr+1)){
  25. if(ptr < 0x40 || (ptr & ~0xFC))
  26. break;
  27. cap = pcicfgr8(ctlr->pcidev, ptr);
  28. if(cap != Clegacy)
  29. continue;
  30. sem = pcicfgr8(ctlr->pcidev, ptr+CLbiossem);
  31. if(sem == 0)
  32. continue;
  33. pcicfgw8(ctlr->pcidev, ptr+CLossem, 1);
  34. for(i = 0; i < 100; i++){
  35. if(pcicfgr8(ctlr->pcidev, ptr+CLbiossem) == 0)
  36. break;
  37. delay(10);
  38. }
  39. if(i == 100)
  40. dprint("ehci %#p: bios timed out\n", ctlr->capio);
  41. pcicfgw32(ctlr->pcidev, ptr+CLcontrol, 0); /* no SMIs */
  42. ctlr->opio->config = 0;
  43. coherence();
  44. return;
  45. }
  46. }
  47. static void
  48. ehcireset(Ctlr *ctlr)
  49. {
  50. Eopio *opio;
  51. int i;
  52. ilock(ctlr);
  53. dprint("ehci %#p reset\n", ctlr->capio);
  54. opio = ctlr->opio;
  55. /*
  56. * Turn off legacy mode. Some controllers won't
  57. * interrupt us as expected otherwise.
  58. */
  59. ehcirun(ctlr, 0);
  60. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  61. /*
  62. * reclaim from bios
  63. */
  64. getehci(ctlr);
  65. /* clear high 32 bits of address signals if it's 64 bits capable.
  66. * This is probably not needed but it does not hurt and others do it.
  67. */
  68. if((ctlr->capio->capparms & C64) != 0){
  69. dprint("ehci: 64 bits\n");
  70. opio->seg = 0;
  71. coherence();
  72. }
  73. if(ehcidebugcapio != ctlr->capio){
  74. opio->cmd |= Chcreset; /* controller reset */
  75. coherence();
  76. for(i = 0; i < 100; i++){
  77. if((opio->cmd & Chcreset) == 0)
  78. break;
  79. delay(1);
  80. }
  81. if(i == 100)
  82. print("ehci %#p controller reset timed out\n", ctlr->capio);
  83. }
  84. /* requesting more interrupts per µframe may miss interrupts */
  85. opio->cmd &= ~Citcmask;
  86. opio->cmd |= 1 << Citcshift; /* max of 1 intr. per 125 µs */
  87. coherence();
  88. switch(opio->cmd & Cflsmask){
  89. case Cfls1024:
  90. ctlr->nframes = 1024;
  91. break;
  92. case Cfls512:
  93. ctlr->nframes = 512;
  94. break;
  95. case Cfls256:
  96. ctlr->nframes = 256;
  97. break;
  98. default:
  99. panic("ehci: unknown fls %ld", opio->cmd & Cflsmask);
  100. }
  101. dprint("ehci: %d frames\n", ctlr->nframes);
  102. iunlock(ctlr);
  103. }
  104. static void
  105. setdebug(Hci*, int d)
  106. {
  107. ehcidebug = d;
  108. }
  109. static void
  110. shutdown(Hci *hp)
  111. {
  112. int i;
  113. Ctlr *ctlr;
  114. Eopio *opio;
  115. ctlr = hp->aux;
  116. ilock(ctlr);
  117. opio = ctlr->opio;
  118. opio->cmd |= Chcreset; /* controller reset */
  119. coherence();
  120. for(i = 0; i < 100; i++){
  121. if((opio->cmd & Chcreset) == 0)
  122. break;
  123. delay(1);
  124. }
  125. if(i >= 100)
  126. print("ehci %#p controller reset timed out\n", ctlr->capio);
  127. delay(100);
  128. ehcirun(ctlr, 0);
  129. opio->frbase = 0;
  130. iunlock(ctlr);
  131. }
  132. static void
  133. scanpci(void)
  134. {
  135. static int already = 0;
  136. int i;
  137. ulong io;
  138. Ctlr *ctlr;
  139. Pcidev *p;
  140. Ecapio *capio;
  141. if(already)
  142. return;
  143. already = 1;
  144. p = nil;
  145. while ((p = pcimatch(p, 0, 0)) != nil) {
  146. /*
  147. * Find EHCI controllers (Programming Interface = 0x20).
  148. */
  149. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  150. continue;
  151. switch(p->ccrp){
  152. case 0x20:
  153. io = p->mem[0].bar & ~0x0f;
  154. break;
  155. default:
  156. continue;
  157. }
  158. if(0 && p->vid == Vintel && p->did == 0x3b34) {
  159. print("usbehci: ignoring known bad ctlr %#ux/%#ux\n",
  160. p->vid, p->did);
  161. continue;
  162. }
  163. if(io == 0){
  164. print("usbehci: %x %x: failed to map registers\n",
  165. p->vid, p->did);
  166. continue;
  167. }
  168. if(p->intl == 0xff || p->intl == 0) {
  169. print("usbehci: no irq assigned for port %#lux\n", io);
  170. continue;
  171. }
  172. dprint("usbehci: %#x %#x: port %#lux size %#x irq %d\n",
  173. p->vid, p->did, io, p->mem[0].size, p->intl);
  174. ctlr = malloc(sizeof(Ctlr));
  175. if (ctlr == nil)
  176. panic("usbehci: out of memory");
  177. ctlr->pcidev = p;
  178. capio = ctlr->capio = vmap(io, p->mem[0].size);
  179. ctlr->opio = (Eopio*)((uintptr)capio + (capio->cap & 0xff));
  180. pcisetbme(p);
  181. pcisetpms(p, 0);
  182. for(i = 0; i < Nhcis; i++)
  183. if(ctlrs[i] == nil){
  184. ctlrs[i] = ctlr;
  185. break;
  186. }
  187. if(i >= Nhcis)
  188. print("ehci: bug: more than %d controllers\n", Nhcis);
  189. /*
  190. * currently, if we enable a second ehci controller on zt
  191. * systems w x58m motherboard, we'll wedge solid after iunlock
  192. * in init for the second one.
  193. */
  194. if (i >= maxehci) {
  195. print("usbehci: ignoring controllers after first %d, "
  196. "at %#p\n", maxehci, io);
  197. ctlrs[i] = nil;
  198. }
  199. }
  200. }
  201. static int
  202. reset(Hci *hp)
  203. {
  204. int i;
  205. char *s;
  206. Ctlr *ctlr;
  207. Ecapio *capio;
  208. Pcidev *p;
  209. static Lock resetlck;
  210. s = getconf("*maxehci");
  211. if (s != nil && s[0] >= '0' && s[0] <= '9')
  212. maxehci = atoi(s);
  213. if(maxehci == 0 || getconf("*nousbehci"))
  214. return -1;
  215. ilock(&resetlck);
  216. scanpci();
  217. /*
  218. * Any adapter matches if no hp->port is supplied,
  219. * otherwise the ports must match.
  220. */
  221. ctlr = nil;
  222. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  223. ctlr = ctlrs[i];
  224. if(ctlr->active == 0)
  225. if(hp->port == 0 || hp->port == (uintptr)ctlr->capio){
  226. ctlr->active = 1;
  227. break;
  228. }
  229. }
  230. iunlock(&resetlck);
  231. if(i >= Nhcis || ctlrs[i] == nil)
  232. return -1;
  233. p = ctlr->pcidev;
  234. hp->aux = ctlr;
  235. hp->port = (uintptr)ctlr->capio;
  236. hp->irq = p->intl;
  237. hp->tbdf = p->tbdf;
  238. capio = ctlr->capio;
  239. hp->nports = capio->parms & Cnports;
  240. ddprint("echi: %s, ncc %lud npcc %lud\n",
  241. capio->parms & 0x10000 ? "leds" : "no leds",
  242. (capio->parms >> 12) & 0xf, (capio->parms >> 8) & 0xf);
  243. ddprint("ehci: routing %s, %sport power ctl, %d ports\n",
  244. capio->parms & 0x40 ? "explicit" : "automatic",
  245. capio->parms & 0x10 ? "" : "no ", hp->nports);
  246. ehcireset(ctlr);
  247. ehcimeminit(ctlr);
  248. /*
  249. * Linkage to the generic HCI driver.
  250. */
  251. ehcilinkage(hp);
  252. hp->shutdown = shutdown;
  253. hp->debug = setdebug;
  254. return 0;
  255. }
  256. void
  257. usbehcilink(void)
  258. {
  259. addhcitype("ehci", reset);
  260. }