usbehcipc.c 5.8 KB

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