usbehcipc.c 5.7 KB

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