ohci-hcd.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. /*
  2. * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
  3. *
  4. * Interrupt support is added. Now, it has been tested
  5. * on ULI1575 chip and works well with USB keyboard.
  6. *
  7. * (C) Copyright 2007
  8. * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
  9. *
  10. * (C) Copyright 2003
  11. * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
  12. *
  13. * Note: Much of this code has been derived from Linux 2.4
  14. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  15. * (C) Copyright 2000-2002 David Brownell
  16. *
  17. * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
  18. * ebenard@eukrea.com - based on s3c24x0's driver
  19. *
  20. * See file CREDITS for list of people who contributed to this
  21. * project.
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License as
  25. * published by the Free Software Foundation; either version 2 of
  26. * the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  36. * MA 02111-1307 USA
  37. *
  38. */
  39. /*
  40. * IMPORTANT NOTES
  41. * 1 - Read doc/README.generic_usb_ohci
  42. * 2 - this driver is intended for use with USB Mass Storage Devices
  43. * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
  44. * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
  45. * to activate workaround for bug #41 or this driver will NOT work!
  46. */
  47. #include <common.h>
  48. #include <asm/byteorder.h>
  49. #if defined(CONFIG_PCI_OHCI)
  50. # include <pci.h>
  51. #if !defined(CONFIG_PCI_OHCI_DEVNO)
  52. #define CONFIG_PCI_OHCI_DEVNO 0
  53. #endif
  54. #endif
  55. #include <malloc.h>
  56. #include <usb.h>
  57. #include "ohci.h"
  58. #ifdef CONFIG_AT91RM9200
  59. #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
  60. #endif
  61. #if defined(CONFIG_ARM920T) || \
  62. defined(CONFIG_S3C24X0) || \
  63. defined(CONFIG_440EP) || \
  64. defined(CONFIG_PCI_OHCI) || \
  65. defined(CONFIG_MPC5200) || \
  66. defined(CONFIG_SYS_OHCI_USE_NPS)
  67. # define OHCI_USE_NPS /* force NoPowerSwitching mode */
  68. #endif
  69. #undef OHCI_VERBOSE_DEBUG /* not always helpful */
  70. #undef DEBUG
  71. #undef SHOW_INFO
  72. #undef OHCI_FILL_TRACE
  73. /* For initializing controller (mask in an HCFS mode too) */
  74. #define OHCI_CONTROL_INIT \
  75. (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
  76. #define min_t(type, x, y) \
  77. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  78. #ifdef CONFIG_PCI_OHCI
  79. static struct pci_device_id ohci_pci_ids[] = {
  80. {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
  81. {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
  82. {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
  83. /* Please add supported PCI OHCI controller ids here */
  84. {0, 0}
  85. };
  86. #endif
  87. #ifdef CONFIG_PCI_EHCI_DEVNO
  88. static struct pci_device_id ehci_pci_ids[] = {
  89. {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
  90. /* Please add supported PCI EHCI controller ids here */
  91. {0, 0}
  92. };
  93. #endif
  94. #ifdef DEBUG
  95. #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
  96. #else
  97. #define dbg(format, arg...) do {} while (0)
  98. #endif /* DEBUG */
  99. #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
  100. #ifdef SHOW_INFO
  101. #define info(format, arg...) printf("INFO: " format "\n", ## arg)
  102. #else
  103. #define info(format, arg...) do {} while (0)
  104. #endif
  105. #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
  106. # define m16_swap(x) cpu_to_be16(x)
  107. # define m32_swap(x) cpu_to_be32(x)
  108. #else
  109. # define m16_swap(x) cpu_to_le16(x)
  110. # define m32_swap(x) cpu_to_le32(x)
  111. #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
  112. /* global ohci_t */
  113. static ohci_t gohci;
  114. /* this must be aligned to a 256 byte boundary */
  115. struct ohci_hcca ghcca[1];
  116. /* a pointer to the aligned storage */
  117. struct ohci_hcca *phcca;
  118. /* this allocates EDs for all possible endpoints */
  119. struct ohci_device ohci_dev;
  120. /* device which was disconnected */
  121. struct usb_device *devgone;
  122. static inline u32 roothub_a(struct ohci *hc)
  123. { return ohci_readl(&hc->regs->roothub.a); }
  124. static inline u32 roothub_b(struct ohci *hc)
  125. { return ohci_readl(&hc->regs->roothub.b); }
  126. static inline u32 roothub_status(struct ohci *hc)
  127. { return ohci_readl(&hc->regs->roothub.status); }
  128. static inline u32 roothub_portstatus(struct ohci *hc, int i)
  129. { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
  130. /* forward declaration */
  131. static int hc_interrupt(void);
  132. static void td_submit_job(struct usb_device *dev, unsigned long pipe,
  133. void *buffer, int transfer_len,
  134. struct devrequest *setup, urb_priv_t *urb,
  135. int interval);
  136. /*-------------------------------------------------------------------------*
  137. * URB support functions
  138. *-------------------------------------------------------------------------*/
  139. /* free HCD-private data associated with this URB */
  140. static void urb_free_priv(urb_priv_t *urb)
  141. {
  142. int i;
  143. int last;
  144. struct td *td;
  145. last = urb->length - 1;
  146. if (last >= 0) {
  147. for (i = 0; i <= last; i++) {
  148. td = urb->td[i];
  149. if (td) {
  150. td->usb_dev = NULL;
  151. urb->td[i] = NULL;
  152. }
  153. }
  154. }
  155. free(urb);
  156. }
  157. /*-------------------------------------------------------------------------*/
  158. #ifdef DEBUG
  159. static int sohci_get_current_frame_number(struct usb_device *dev);
  160. /* debug| print the main components of an URB
  161. * small: 0) header + data packets 1) just header */
  162. static void pkt_print(urb_priv_t *purb, struct usb_device *dev,
  163. unsigned long pipe, void *buffer, int transfer_len,
  164. struct devrequest *setup, char *str, int small)
  165. {
  166. dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
  167. str,
  168. sohci_get_current_frame_number(dev),
  169. usb_pipedevice(pipe),
  170. usb_pipeendpoint(pipe),
  171. usb_pipeout(pipe)? 'O': 'I',
  172. usb_pipetype(pipe) < 2 ? \
  173. (usb_pipeint(pipe)? "INTR": "ISOC"): \
  174. (usb_pipecontrol(pipe)? "CTRL": "BULK"),
  175. (purb ? purb->actual_length : 0),
  176. transfer_len, dev->status);
  177. #ifdef OHCI_VERBOSE_DEBUG
  178. if (!small) {
  179. int i, len;
  180. if (usb_pipecontrol(pipe)) {
  181. printf(__FILE__ ": cmd(8):");
  182. for (i = 0; i < 8 ; i++)
  183. printf(" %02x", ((__u8 *) setup) [i]);
  184. printf("\n");
  185. }
  186. if (transfer_len > 0 && buffer) {
  187. printf(__FILE__ ": data(%d/%d):",
  188. (purb ? purb->actual_length : 0),
  189. transfer_len);
  190. len = usb_pipeout(pipe)? transfer_len:
  191. (purb ? purb->actual_length : 0);
  192. for (i = 0; i < 16 && i < len; i++)
  193. printf(" %02x", ((__u8 *) buffer) [i]);
  194. printf("%s\n", i < len? "...": "");
  195. }
  196. }
  197. #endif
  198. }
  199. /* just for debugging; prints non-empty branches of the int ed tree
  200. * inclusive iso eds */
  201. void ep_print_int_eds(ohci_t *ohci, char *str)
  202. {
  203. int i, j;
  204. __u32 *ed_p;
  205. for (i = 0; i < 32; i++) {
  206. j = 5;
  207. ed_p = &(ohci->hcca->int_table [i]);
  208. if (*ed_p == 0)
  209. continue;
  210. printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
  211. while (*ed_p != 0 && j--) {
  212. ed_t *ed = (ed_t *)m32_swap(ed_p);
  213. printf(" ed: %4x;", ed->hwINFO);
  214. ed_p = &ed->hwNextED;
  215. }
  216. printf("\n");
  217. }
  218. }
  219. static void ohci_dump_intr_mask(char *label, __u32 mask)
  220. {
  221. dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
  222. label,
  223. mask,
  224. (mask & OHCI_INTR_MIE) ? " MIE" : "",
  225. (mask & OHCI_INTR_OC) ? " OC" : "",
  226. (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
  227. (mask & OHCI_INTR_FNO) ? " FNO" : "",
  228. (mask & OHCI_INTR_UE) ? " UE" : "",
  229. (mask & OHCI_INTR_RD) ? " RD" : "",
  230. (mask & OHCI_INTR_SF) ? " SF" : "",
  231. (mask & OHCI_INTR_WDH) ? " WDH" : "",
  232. (mask & OHCI_INTR_SO) ? " SO" : ""
  233. );
  234. }
  235. static void maybe_print_eds(char *label, __u32 value)
  236. {
  237. ed_t *edp = (ed_t *)value;
  238. if (value) {
  239. dbg("%s %08x", label, value);
  240. dbg("%08x", edp->hwINFO);
  241. dbg("%08x", edp->hwTailP);
  242. dbg("%08x", edp->hwHeadP);
  243. dbg("%08x", edp->hwNextED);
  244. }
  245. }
  246. static char *hcfs2string(int state)
  247. {
  248. switch (state) {
  249. case OHCI_USB_RESET: return "reset";
  250. case OHCI_USB_RESUME: return "resume";
  251. case OHCI_USB_OPER: return "operational";
  252. case OHCI_USB_SUSPEND: return "suspend";
  253. }
  254. return "?";
  255. }
  256. /* dump control and status registers */
  257. static void ohci_dump_status(ohci_t *controller)
  258. {
  259. struct ohci_regs *regs = controller->regs;
  260. __u32 temp;
  261. temp = ohci_readl(&regs->revision) & 0xff;
  262. if (temp != 0x10)
  263. dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
  264. temp = ohci_readl(&regs->control);
  265. dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
  266. (temp & OHCI_CTRL_RWE) ? " RWE" : "",
  267. (temp & OHCI_CTRL_RWC) ? " RWC" : "",
  268. (temp & OHCI_CTRL_IR) ? " IR" : "",
  269. hcfs2string(temp & OHCI_CTRL_HCFS),
  270. (temp & OHCI_CTRL_BLE) ? " BLE" : "",
  271. (temp & OHCI_CTRL_CLE) ? " CLE" : "",
  272. (temp & OHCI_CTRL_IE) ? " IE" : "",
  273. (temp & OHCI_CTRL_PLE) ? " PLE" : "",
  274. temp & OHCI_CTRL_CBSR
  275. );
  276. temp = ohci_readl(&regs->cmdstatus);
  277. dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
  278. (temp & OHCI_SOC) >> 16,
  279. (temp & OHCI_OCR) ? " OCR" : "",
  280. (temp & OHCI_BLF) ? " BLF" : "",
  281. (temp & OHCI_CLF) ? " CLF" : "",
  282. (temp & OHCI_HCR) ? " HCR" : ""
  283. );
  284. ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
  285. ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
  286. maybe_print_eds("ed_periodcurrent",
  287. ohci_readl(&regs->ed_periodcurrent));
  288. maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
  289. maybe_print_eds("ed_controlcurrent",
  290. ohci_readl(&regs->ed_controlcurrent));
  291. maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
  292. maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
  293. maybe_print_eds("donehead", ohci_readl(&regs->donehead));
  294. }
  295. static void ohci_dump_roothub(ohci_t *controller, int verbose)
  296. {
  297. __u32 temp, ndp, i;
  298. temp = roothub_a(controller);
  299. ndp = (temp & RH_A_NDP);
  300. #ifdef CONFIG_AT91C_PQFP_UHPBUG
  301. ndp = (ndp == 2) ? 1:0;
  302. #endif
  303. if (verbose) {
  304. dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
  305. ((temp & RH_A_POTPGT) >> 24) & 0xff,
  306. (temp & RH_A_NOCP) ? " NOCP" : "",
  307. (temp & RH_A_OCPM) ? " OCPM" : "",
  308. (temp & RH_A_DT) ? " DT" : "",
  309. (temp & RH_A_NPS) ? " NPS" : "",
  310. (temp & RH_A_PSM) ? " PSM" : "",
  311. ndp
  312. );
  313. temp = roothub_b(controller);
  314. dbg("roothub.b: %08x PPCM=%04x DR=%04x",
  315. temp,
  316. (temp & RH_B_PPCM) >> 16,
  317. (temp & RH_B_DR)
  318. );
  319. temp = roothub_status(controller);
  320. dbg("roothub.status: %08x%s%s%s%s%s%s",
  321. temp,
  322. (temp & RH_HS_CRWE) ? " CRWE" : "",
  323. (temp & RH_HS_OCIC) ? " OCIC" : "",
  324. (temp & RH_HS_LPSC) ? " LPSC" : "",
  325. (temp & RH_HS_DRWE) ? " DRWE" : "",
  326. (temp & RH_HS_OCI) ? " OCI" : "",
  327. (temp & RH_HS_LPS) ? " LPS" : ""
  328. );
  329. }
  330. for (i = 0; i < ndp; i++) {
  331. temp = roothub_portstatus(controller, i);
  332. dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
  333. i,
  334. temp,
  335. (temp & RH_PS_PRSC) ? " PRSC" : "",
  336. (temp & RH_PS_OCIC) ? " OCIC" : "",
  337. (temp & RH_PS_PSSC) ? " PSSC" : "",
  338. (temp & RH_PS_PESC) ? " PESC" : "",
  339. (temp & RH_PS_CSC) ? " CSC" : "",
  340. (temp & RH_PS_LSDA) ? " LSDA" : "",
  341. (temp & RH_PS_PPS) ? " PPS" : "",
  342. (temp & RH_PS_PRS) ? " PRS" : "",
  343. (temp & RH_PS_POCI) ? " POCI" : "",
  344. (temp & RH_PS_PSS) ? " PSS" : "",
  345. (temp & RH_PS_PES) ? " PES" : "",
  346. (temp & RH_PS_CCS) ? " CCS" : ""
  347. );
  348. }
  349. }
  350. static void ohci_dump(ohci_t *controller, int verbose)
  351. {
  352. dbg("OHCI controller usb-%s state", controller->slot_name);
  353. /* dumps some of the state we know about */
  354. ohci_dump_status(controller);
  355. if (verbose)
  356. ep_print_int_eds(controller, "hcca");
  357. dbg("hcca frame #%04x", controller->hcca->frame_no);
  358. ohci_dump_roothub(controller, 1);
  359. }
  360. #endif /* DEBUG */
  361. /*-------------------------------------------------------------------------*
  362. * Interface functions (URB)
  363. *-------------------------------------------------------------------------*/
  364. /* get a transfer request */
  365. int sohci_submit_job(urb_priv_t *urb, struct devrequest *setup)
  366. {
  367. ohci_t *ohci;
  368. ed_t *ed;
  369. urb_priv_t *purb_priv = urb;
  370. int i, size = 0;
  371. struct usb_device *dev = urb->dev;
  372. unsigned long pipe = urb->pipe;
  373. void *buffer = urb->transfer_buffer;
  374. int transfer_len = urb->transfer_buffer_length;
  375. int interval = urb->interval;
  376. ohci = &gohci;
  377. /* when controller's hung, permit only roothub cleanup attempts
  378. * such as powering down ports */
  379. if (ohci->disabled) {
  380. err("sohci_submit_job: EPIPE");
  381. return -1;
  382. }
  383. /* we're about to begin a new transaction here so mark the
  384. * URB unfinished */
  385. urb->finished = 0;
  386. /* every endpoint has a ed, locate and fill it */
  387. ed = ep_add_ed(dev, pipe, interval, 1);
  388. if (!ed) {
  389. err("sohci_submit_job: ENOMEM");
  390. return -1;
  391. }
  392. /* for the private part of the URB we need the number of TDs (size) */
  393. switch (usb_pipetype(pipe)) {
  394. case PIPE_BULK: /* one TD for every 4096 Byte */
  395. size = (transfer_len - 1) / 4096 + 1;
  396. break;
  397. case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
  398. size = (transfer_len == 0)? 2:
  399. (transfer_len - 1) / 4096 + 3;
  400. break;
  401. case PIPE_INTERRUPT: /* 1 TD */
  402. size = 1;
  403. break;
  404. }
  405. ed->purb = urb;
  406. if (size >= (N_URB_TD - 1)) {
  407. err("need %d TDs, only have %d", size, N_URB_TD);
  408. return -1;
  409. }
  410. purb_priv->pipe = pipe;
  411. /* fill the private part of the URB */
  412. purb_priv->length = size;
  413. purb_priv->ed = ed;
  414. purb_priv->actual_length = 0;
  415. /* allocate the TDs */
  416. /* note that td[0] was allocated in ep_add_ed */
  417. for (i = 0; i < size; i++) {
  418. purb_priv->td[i] = td_alloc(dev);
  419. if (!purb_priv->td[i]) {
  420. purb_priv->length = i;
  421. urb_free_priv(purb_priv);
  422. err("sohci_submit_job: ENOMEM");
  423. return -1;
  424. }
  425. }
  426. if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
  427. urb_free_priv(purb_priv);
  428. err("sohci_submit_job: EINVAL");
  429. return -1;
  430. }
  431. /* link the ed into a chain if is not already */
  432. if (ed->state != ED_OPER)
  433. ep_link(ohci, ed);
  434. /* fill the TDs and link it to the ed */
  435. td_submit_job(dev, pipe, buffer, transfer_len,
  436. setup, purb_priv, interval);
  437. return 0;
  438. }
  439. static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
  440. {
  441. struct ohci_regs *regs = hc->regs;
  442. switch (usb_pipetype(urb->pipe)) {
  443. case PIPE_INTERRUPT:
  444. /* implicitly requeued */
  445. if (urb->dev->irq_handle &&
  446. (urb->dev->irq_act_len = urb->actual_length)) {
  447. ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
  448. ohci_readl(&regs->intrenable); /* PCI posting flush */
  449. urb->dev->irq_handle(urb->dev);
  450. ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
  451. ohci_readl(&regs->intrdisable); /* PCI posting flush */
  452. }
  453. urb->actual_length = 0;
  454. td_submit_job(
  455. urb->dev,
  456. urb->pipe,
  457. urb->transfer_buffer,
  458. urb->transfer_buffer_length,
  459. NULL,
  460. urb,
  461. urb->interval);
  462. break;
  463. case PIPE_CONTROL:
  464. case PIPE_BULK:
  465. break;
  466. default:
  467. return 0;
  468. }
  469. return 1;
  470. }
  471. /*-------------------------------------------------------------------------*/
  472. #ifdef DEBUG
  473. /* tell us the current USB frame number */
  474. static int sohci_get_current_frame_number(struct usb_device *usb_dev)
  475. {
  476. ohci_t *ohci = &gohci;
  477. return m16_swap(ohci->hcca->frame_no);
  478. }
  479. #endif
  480. /*-------------------------------------------------------------------------*
  481. * ED handling functions
  482. *-------------------------------------------------------------------------*/
  483. /* search for the right branch to insert an interrupt ed into the int tree
  484. * do some load ballancing;
  485. * returns the branch and
  486. * sets the interval to interval = 2^integer (ld (interval)) */
  487. static int ep_int_ballance(ohci_t *ohci, int interval, int load)
  488. {
  489. int i, branch = 0;
  490. /* search for the least loaded interrupt endpoint
  491. * branch of all 32 branches
  492. */
  493. for (i = 0; i < 32; i++)
  494. if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
  495. branch = i;
  496. branch = branch % interval;
  497. for (i = branch; i < 32; i += interval)
  498. ohci->ohci_int_load [i] += load;
  499. return branch;
  500. }
  501. /*-------------------------------------------------------------------------*/
  502. /* 2^int( ld (inter)) */
  503. static int ep_2_n_interval(int inter)
  504. {
  505. int i;
  506. for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
  507. return 1 << i;
  508. }
  509. /*-------------------------------------------------------------------------*/
  510. /* the int tree is a binary tree
  511. * in order to process it sequentially the indexes of the branches have to
  512. * be mapped the mapping reverses the bits of a word of num_bits length */
  513. static int ep_rev(int num_bits, int word)
  514. {
  515. int i, wout = 0;
  516. for (i = 0; i < num_bits; i++)
  517. wout |= (((word >> i) & 1) << (num_bits - i - 1));
  518. return wout;
  519. }
  520. /*-------------------------------------------------------------------------*
  521. * ED handling functions
  522. *-------------------------------------------------------------------------*/
  523. /* link an ed into one of the HC chains */
  524. static int ep_link(ohci_t *ohci, ed_t *edi)
  525. {
  526. volatile ed_t *ed = edi;
  527. int int_branch;
  528. int i;
  529. int inter;
  530. int interval;
  531. int load;
  532. __u32 *ed_p;
  533. ed->state = ED_OPER;
  534. ed->int_interval = 0;
  535. switch (ed->type) {
  536. case PIPE_CONTROL:
  537. ed->hwNextED = 0;
  538. if (ohci->ed_controltail == NULL)
  539. ohci_writel(ed, &ohci->regs->ed_controlhead);
  540. else
  541. ohci->ed_controltail->hwNextED =
  542. m32_swap((unsigned long)ed);
  543. ed->ed_prev = ohci->ed_controltail;
  544. if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
  545. !ohci->ed_rm_list[1] && !ohci->sleeping) {
  546. ohci->hc_control |= OHCI_CTRL_CLE;
  547. ohci_writel(ohci->hc_control, &ohci->regs->control);
  548. }
  549. ohci->ed_controltail = edi;
  550. break;
  551. case PIPE_BULK:
  552. ed->hwNextED = 0;
  553. if (ohci->ed_bulktail == NULL)
  554. ohci_writel(ed, &ohci->regs->ed_bulkhead);
  555. else
  556. ohci->ed_bulktail->hwNextED =
  557. m32_swap((unsigned long)ed);
  558. ed->ed_prev = ohci->ed_bulktail;
  559. if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
  560. !ohci->ed_rm_list[1] && !ohci->sleeping) {
  561. ohci->hc_control |= OHCI_CTRL_BLE;
  562. ohci_writel(ohci->hc_control, &ohci->regs->control);
  563. }
  564. ohci->ed_bulktail = edi;
  565. break;
  566. case PIPE_INTERRUPT:
  567. load = ed->int_load;
  568. interval = ep_2_n_interval(ed->int_period);
  569. ed->int_interval = interval;
  570. int_branch = ep_int_ballance(ohci, interval, load);
  571. ed->int_branch = int_branch;
  572. for (i = 0; i < ep_rev(6, interval); i += inter) {
  573. inter = 1;
  574. for (ed_p = &(ohci->hcca->int_table[\
  575. ep_rev(5, i) + int_branch]);
  576. (*ed_p != 0) &&
  577. (((ed_t *)ed_p)->int_interval >= interval);
  578. ed_p = &(((ed_t *)ed_p)->hwNextED))
  579. inter = ep_rev(6,
  580. ((ed_t *)ed_p)->int_interval);
  581. ed->hwNextED = *ed_p;
  582. *ed_p = m32_swap((unsigned long)ed);
  583. }
  584. break;
  585. }
  586. return 0;
  587. }
  588. /*-------------------------------------------------------------------------*/
  589. /* scan the periodic table to find and unlink this ED */
  590. static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
  591. unsigned index, unsigned period)
  592. {
  593. for (; index < NUM_INTS; index += period) {
  594. __u32 *ed_p = &ohci->hcca->int_table [index];
  595. /* ED might have been unlinked through another path */
  596. while (*ed_p != 0) {
  597. if (((struct ed *)
  598. m32_swap((unsigned long)ed_p)) == ed) {
  599. *ed_p = ed->hwNextED;
  600. break;
  601. }
  602. ed_p = &(((struct ed *)
  603. m32_swap((unsigned long)ed_p))->hwNextED);
  604. }
  605. }
  606. }
  607. /* unlink an ed from one of the HC chains.
  608. * just the link to the ed is unlinked.
  609. * the link from the ed still points to another operational ed or 0
  610. * so the HC can eventually finish the processing of the unlinked ed */
  611. static int ep_unlink(ohci_t *ohci, ed_t *edi)
  612. {
  613. volatile ed_t *ed = edi;
  614. int i;
  615. ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
  616. switch (ed->type) {
  617. case PIPE_CONTROL:
  618. if (ed->ed_prev == NULL) {
  619. if (!ed->hwNextED) {
  620. ohci->hc_control &= ~OHCI_CTRL_CLE;
  621. ohci_writel(ohci->hc_control,
  622. &ohci->regs->control);
  623. }
  624. ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
  625. &ohci->regs->ed_controlhead);
  626. } else {
  627. ed->ed_prev->hwNextED = ed->hwNextED;
  628. }
  629. if (ohci->ed_controltail == ed) {
  630. ohci->ed_controltail = ed->ed_prev;
  631. } else {
  632. ((ed_t *)m32_swap(
  633. *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
  634. }
  635. break;
  636. case PIPE_BULK:
  637. if (ed->ed_prev == NULL) {
  638. if (!ed->hwNextED) {
  639. ohci->hc_control &= ~OHCI_CTRL_BLE;
  640. ohci_writel(ohci->hc_control,
  641. &ohci->regs->control);
  642. }
  643. ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
  644. &ohci->regs->ed_bulkhead);
  645. } else {
  646. ed->ed_prev->hwNextED = ed->hwNextED;
  647. }
  648. if (ohci->ed_bulktail == ed) {
  649. ohci->ed_bulktail = ed->ed_prev;
  650. } else {
  651. ((ed_t *)m32_swap(
  652. *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
  653. }
  654. break;
  655. case PIPE_INTERRUPT:
  656. periodic_unlink(ohci, ed, 0, 1);
  657. for (i = ed->int_branch; i < 32; i += ed->int_interval)
  658. ohci->ohci_int_load[i] -= ed->int_load;
  659. break;
  660. }
  661. ed->state = ED_UNLINK;
  662. return 0;
  663. }
  664. /*-------------------------------------------------------------------------*/
  665. /* add/reinit an endpoint; this should be done once at the
  666. * usb_set_configuration command, but the USB stack is a little bit
  667. * stateless so we do it at every transaction if the state of the ed
  668. * is ED_NEW then a dummy td is added and the state is changed to
  669. * ED_UNLINK in all other cases the state is left unchanged the ed
  670. * info fields are setted anyway even though most of them should not
  671. * change
  672. */
  673. static ed_t *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe,
  674. int interval, int load)
  675. {
  676. td_t *td;
  677. ed_t *ed_ret;
  678. volatile ed_t *ed;
  679. ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
  680. (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
  681. if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
  682. err("ep_add_ed: pending delete");
  683. /* pending delete request */
  684. return NULL;
  685. }
  686. if (ed->state == ED_NEW) {
  687. /* dummy td; end of td list for ed */
  688. td = td_alloc(usb_dev);
  689. ed->hwTailP = m32_swap((unsigned long)td);
  690. ed->hwHeadP = ed->hwTailP;
  691. ed->state = ED_UNLINK;
  692. ed->type = usb_pipetype(pipe);
  693. ohci_dev.ed_cnt++;
  694. }
  695. ed->hwINFO = m32_swap(usb_pipedevice(pipe)
  696. | usb_pipeendpoint(pipe) << 7
  697. | (usb_pipeisoc(pipe)? 0x8000: 0)
  698. | (usb_pipecontrol(pipe)? 0: \
  699. (usb_pipeout(pipe)? 0x800: 0x1000))
  700. | (usb_dev->speed == USB_SPEED_LOW) << 13
  701. | usb_maxpacket(usb_dev, pipe) << 16);
  702. if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
  703. ed->int_period = interval;
  704. ed->int_load = load;
  705. }
  706. return ed_ret;
  707. }
  708. /*-------------------------------------------------------------------------*
  709. * TD handling functions
  710. *-------------------------------------------------------------------------*/
  711. /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
  712. static void td_fill(ohci_t *ohci, unsigned int info,
  713. void *data, int len,
  714. struct usb_device *dev, int index, urb_priv_t *urb_priv)
  715. {
  716. volatile td_t *td, *td_pt;
  717. #ifdef OHCI_FILL_TRACE
  718. int i;
  719. #endif
  720. if (index > urb_priv->length) {
  721. err("index > length");
  722. return;
  723. }
  724. /* use this td as the next dummy */
  725. td_pt = urb_priv->td [index];
  726. td_pt->hwNextTD = 0;
  727. /* fill the old dummy TD */
  728. td = urb_priv->td [index] =
  729. (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
  730. td->ed = urb_priv->ed;
  731. td->next_dl_td = NULL;
  732. td->index = index;
  733. td->data = (__u32)data;
  734. #ifdef OHCI_FILL_TRACE
  735. if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
  736. for (i = 0; i < len; i++)
  737. printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
  738. printf("\n");
  739. }
  740. #endif
  741. if (!len)
  742. data = 0;
  743. td->hwINFO = m32_swap(info);
  744. td->hwCBP = m32_swap((unsigned long)data);
  745. if (data)
  746. td->hwBE = m32_swap((unsigned long)(data + len - 1));
  747. else
  748. td->hwBE = 0;
  749. td->hwNextTD = m32_swap((unsigned long)td_pt);
  750. /* append to queue */
  751. td->ed->hwTailP = td->hwNextTD;
  752. }
  753. /*-------------------------------------------------------------------------*/
  754. /* prepare all TDs of a transfer */
  755. static void td_submit_job(struct usb_device *dev, unsigned long pipe,
  756. void *buffer, int transfer_len,
  757. struct devrequest *setup, urb_priv_t *urb,
  758. int interval)
  759. {
  760. ohci_t *ohci = &gohci;
  761. int data_len = transfer_len;
  762. void *data;
  763. int cnt = 0;
  764. __u32 info = 0;
  765. unsigned int toggle = 0;
  766. /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
  767. * bits for reseting */
  768. if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
  769. toggle = TD_T_TOGGLE;
  770. } else {
  771. toggle = TD_T_DATA0;
  772. usb_settoggle(dev, usb_pipeendpoint(pipe),
  773. usb_pipeout(pipe), 1);
  774. }
  775. urb->td_cnt = 0;
  776. if (data_len)
  777. data = buffer;
  778. else
  779. data = 0;
  780. switch (usb_pipetype(pipe)) {
  781. case PIPE_BULK:
  782. info = usb_pipeout(pipe)?
  783. TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
  784. while (data_len > 4096) {
  785. td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
  786. data, 4096, dev, cnt, urb);
  787. data += 4096; data_len -= 4096; cnt++;
  788. }
  789. info = usb_pipeout(pipe)?
  790. TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
  791. td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
  792. data_len, dev, cnt, urb);
  793. cnt++;
  794. if (!ohci->sleeping) {
  795. /* start bulk list */
  796. ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
  797. }
  798. break;
  799. case PIPE_CONTROL:
  800. /* Setup phase */
  801. info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
  802. td_fill(ohci, info, setup, 8, dev, cnt++, urb);
  803. /* Optional Data phase */
  804. if (data_len > 0) {
  805. info = usb_pipeout(pipe)?
  806. TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
  807. TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
  808. /* NOTE: mishandles transfers >8K, some >4K */
  809. td_fill(ohci, info, data, data_len, dev, cnt++, urb);
  810. }
  811. /* Status phase */
  812. info = usb_pipeout(pipe)?
  813. TD_CC | TD_DP_IN | TD_T_DATA1:
  814. TD_CC | TD_DP_OUT | TD_T_DATA1;
  815. td_fill(ohci, info, data, 0, dev, cnt++, urb);
  816. if (!ohci->sleeping) {
  817. /* start Control list */
  818. ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
  819. }
  820. break;
  821. case PIPE_INTERRUPT:
  822. info = usb_pipeout(urb->pipe)?
  823. TD_CC | TD_DP_OUT | toggle:
  824. TD_CC | TD_R | TD_DP_IN | toggle;
  825. td_fill(ohci, info, data, data_len, dev, cnt++, urb);
  826. break;
  827. }
  828. if (urb->length != cnt)
  829. dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
  830. }
  831. /*-------------------------------------------------------------------------*
  832. * Done List handling functions
  833. *-------------------------------------------------------------------------*/
  834. /* calculate the transfer length and update the urb */
  835. static void dl_transfer_length(td_t *td)
  836. {
  837. __u32 tdBE, tdCBP;
  838. urb_priv_t *lurb_priv = td->ed->purb;
  839. tdBE = m32_swap(td->hwBE);
  840. tdCBP = m32_swap(td->hwCBP);
  841. if (!(usb_pipecontrol(lurb_priv->pipe) &&
  842. ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
  843. if (tdBE != 0) {
  844. if (td->hwCBP == 0)
  845. lurb_priv->actual_length += tdBE - td->data + 1;
  846. else
  847. lurb_priv->actual_length += tdCBP - td->data;
  848. }
  849. }
  850. }
  851. /*-------------------------------------------------------------------------*/
  852. static void check_status(td_t *td_list)
  853. {
  854. urb_priv_t *lurb_priv = td_list->ed->purb;
  855. int urb_len = lurb_priv->length;
  856. __u32 *phwHeadP = &td_list->ed->hwHeadP;
  857. int cc;
  858. cc = TD_CC_GET(m32_swap(td_list->hwINFO));
  859. if (cc) {
  860. err(" USB-error: %s (%x)", cc_to_string[cc], cc);
  861. if (*phwHeadP & m32_swap(0x1)) {
  862. if (lurb_priv &&
  863. ((td_list->index + 1) < urb_len)) {
  864. *phwHeadP =
  865. (lurb_priv->td[urb_len - 1]->hwNextTD &\
  866. m32_swap(0xfffffff0)) |
  867. (*phwHeadP & m32_swap(0x2));
  868. lurb_priv->td_cnt += urb_len -
  869. td_list->index - 1;
  870. } else
  871. *phwHeadP &= m32_swap(0xfffffff2);
  872. }
  873. #ifdef CONFIG_MPC5200
  874. td_list->hwNextTD = 0;
  875. #endif
  876. }
  877. }
  878. /* replies to the request have to be on a FIFO basis so
  879. * we reverse the reversed done-list */
  880. static td_t *dl_reverse_done_list(ohci_t *ohci)
  881. {
  882. __u32 td_list_hc;
  883. td_t *td_rev = NULL;
  884. td_t *td_list = NULL;
  885. td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
  886. ohci->hcca->done_head = 0;
  887. while (td_list_hc) {
  888. td_list = (td_t *)td_list_hc;
  889. check_status(td_list);
  890. td_list->next_dl_td = td_rev;
  891. td_rev = td_list;
  892. td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
  893. }
  894. return td_list;
  895. }
  896. /*-------------------------------------------------------------------------*/
  897. /*-------------------------------------------------------------------------*/
  898. static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
  899. {
  900. if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
  901. urb->finished = sohci_return_job(ohci, urb);
  902. else
  903. dbg("finish_urb: strange.., ED state %x, \n", status);
  904. }
  905. /*
  906. * Used to take back a TD from the host controller. This would normally be
  907. * called from within dl_done_list, however it may be called directly if the
  908. * HC no longer sees the TD and it has not appeared on the donelist (after
  909. * two frames). This bug has been observed on ZF Micro systems.
  910. */
  911. static int takeback_td(ohci_t *ohci, td_t *td_list)
  912. {
  913. ed_t *ed;
  914. int cc;
  915. int stat = 0;
  916. /* urb_t *urb; */
  917. urb_priv_t *lurb_priv;
  918. __u32 tdINFO, edHeadP, edTailP;
  919. tdINFO = m32_swap(td_list->hwINFO);
  920. ed = td_list->ed;
  921. lurb_priv = ed->purb;
  922. dl_transfer_length(td_list);
  923. lurb_priv->td_cnt++;
  924. /* error code of transfer */
  925. cc = TD_CC_GET(tdINFO);
  926. if (cc) {
  927. err("USB-error: %s (%x)", cc_to_string[cc], cc);
  928. stat = cc_to_error[cc];
  929. }
  930. /* see if this done list makes for all TD's of current URB,
  931. * and mark the URB finished if so */
  932. if (lurb_priv->td_cnt == lurb_priv->length)
  933. finish_urb(ohci, lurb_priv, ed->state);
  934. dbg("dl_done_list: processing TD %x, len %x\n",
  935. lurb_priv->td_cnt, lurb_priv->length);
  936. if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
  937. edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
  938. edTailP = m32_swap(ed->hwTailP);
  939. /* unlink eds if they are not busy */
  940. if ((edHeadP == edTailP) && (ed->state == ED_OPER))
  941. ep_unlink(ohci, ed);
  942. }
  943. return stat;
  944. }
  945. static int dl_done_list(ohci_t *ohci)
  946. {
  947. int stat = 0;
  948. td_t *td_list = dl_reverse_done_list(ohci);
  949. while (td_list) {
  950. td_t *td_next = td_list->next_dl_td;
  951. stat = takeback_td(ohci, td_list);
  952. td_list = td_next;
  953. }
  954. return stat;
  955. }
  956. /*-------------------------------------------------------------------------*
  957. * Virtual Root Hub
  958. *-------------------------------------------------------------------------*/
  959. /* Device descriptor */
  960. static __u8 root_hub_dev_des[] =
  961. {
  962. 0x12, /* __u8 bLength; */
  963. 0x01, /* __u8 bDescriptorType; Device */
  964. 0x10, /* __u16 bcdUSB; v1.1 */
  965. 0x01,
  966. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  967. 0x00, /* __u8 bDeviceSubClass; */
  968. 0x00, /* __u8 bDeviceProtocol; */
  969. 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
  970. 0x00, /* __u16 idVendor; */
  971. 0x00,
  972. 0x00, /* __u16 idProduct; */
  973. 0x00,
  974. 0x00, /* __u16 bcdDevice; */
  975. 0x00,
  976. 0x00, /* __u8 iManufacturer; */
  977. 0x01, /* __u8 iProduct; */
  978. 0x00, /* __u8 iSerialNumber; */
  979. 0x01 /* __u8 bNumConfigurations; */
  980. };
  981. /* Configuration descriptor */
  982. static __u8 root_hub_config_des[] =
  983. {
  984. 0x09, /* __u8 bLength; */
  985. 0x02, /* __u8 bDescriptorType; Configuration */
  986. 0x19, /* __u16 wTotalLength; */
  987. 0x00,
  988. 0x01, /* __u8 bNumInterfaces; */
  989. 0x01, /* __u8 bConfigurationValue; */
  990. 0x00, /* __u8 iConfiguration; */
  991. 0x40, /* __u8 bmAttributes;
  992. Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
  993. 0x00, /* __u8 MaxPower; */
  994. /* interface */
  995. 0x09, /* __u8 if_bLength; */
  996. 0x04, /* __u8 if_bDescriptorType; Interface */
  997. 0x00, /* __u8 if_bInterfaceNumber; */
  998. 0x00, /* __u8 if_bAlternateSetting; */
  999. 0x01, /* __u8 if_bNumEndpoints; */
  1000. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  1001. 0x00, /* __u8 if_bInterfaceSubClass; */
  1002. 0x00, /* __u8 if_bInterfaceProtocol; */
  1003. 0x00, /* __u8 if_iInterface; */
  1004. /* endpoint */
  1005. 0x07, /* __u8 ep_bLength; */
  1006. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  1007. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  1008. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  1009. 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
  1010. 0x00,
  1011. 0xff /* __u8 ep_bInterval; 255 ms */
  1012. };
  1013. static unsigned char root_hub_str_index0[] =
  1014. {
  1015. 0x04, /* __u8 bLength; */
  1016. 0x03, /* __u8 bDescriptorType; String-descriptor */
  1017. 0x09, /* __u8 lang ID */
  1018. 0x04, /* __u8 lang ID */
  1019. };
  1020. static unsigned char root_hub_str_index1[] =
  1021. {
  1022. 28, /* __u8 bLength; */
  1023. 0x03, /* __u8 bDescriptorType; String-descriptor */
  1024. 'O', /* __u8 Unicode */
  1025. 0, /* __u8 Unicode */
  1026. 'H', /* __u8 Unicode */
  1027. 0, /* __u8 Unicode */
  1028. 'C', /* __u8 Unicode */
  1029. 0, /* __u8 Unicode */
  1030. 'I', /* __u8 Unicode */
  1031. 0, /* __u8 Unicode */
  1032. ' ', /* __u8 Unicode */
  1033. 0, /* __u8 Unicode */
  1034. 'R', /* __u8 Unicode */
  1035. 0, /* __u8 Unicode */
  1036. 'o', /* __u8 Unicode */
  1037. 0, /* __u8 Unicode */
  1038. 'o', /* __u8 Unicode */
  1039. 0, /* __u8 Unicode */
  1040. 't', /* __u8 Unicode */
  1041. 0, /* __u8 Unicode */
  1042. ' ', /* __u8 Unicode */
  1043. 0, /* __u8 Unicode */
  1044. 'H', /* __u8 Unicode */
  1045. 0, /* __u8 Unicode */
  1046. 'u', /* __u8 Unicode */
  1047. 0, /* __u8 Unicode */
  1048. 'b', /* __u8 Unicode */
  1049. 0, /* __u8 Unicode */
  1050. };
  1051. /* Hub class-specific descriptor is constructed dynamically */
  1052. /*-------------------------------------------------------------------------*/
  1053. #define OK(x) len = (x); break
  1054. #ifdef DEBUG
  1055. #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
  1056. &gohci.regs->roothub.status); }
  1057. #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
  1058. (x)); ohci_writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); }
  1059. #else
  1060. #define WR_RH_STAT(x) ohci_writel((x), &gohci.regs->roothub.status)
  1061. #define WR_RH_PORTSTAT(x) ohci_writel((x), \
  1062. &gohci.regs->roothub.portstatus[wIndex-1])
  1063. #endif
  1064. #define RD_RH_STAT roothub_status(&gohci)
  1065. #define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
  1066. /* request to virtual root hub */
  1067. int rh_check_port_status(ohci_t *controller)
  1068. {
  1069. __u32 temp, ndp, i;
  1070. int res;
  1071. res = -1;
  1072. temp = roothub_a(controller);
  1073. ndp = (temp & RH_A_NDP);
  1074. #ifdef CONFIG_AT91C_PQFP_UHPBUG
  1075. ndp = (ndp == 2) ? 1:0;
  1076. #endif
  1077. for (i = 0; i < ndp; i++) {
  1078. temp = roothub_portstatus(controller, i);
  1079. /* check for a device disconnect */
  1080. if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
  1081. (RH_PS_PESC | RH_PS_CSC)) &&
  1082. ((temp & RH_PS_CCS) == 0)) {
  1083. res = i;
  1084. break;
  1085. }
  1086. }
  1087. return res;
  1088. }
  1089. static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  1090. void *buffer, int transfer_len, struct devrequest *cmd)
  1091. {
  1092. void *data = buffer;
  1093. int leni = transfer_len;
  1094. int len = 0;
  1095. int stat = 0;
  1096. __u16 bmRType_bReq;
  1097. __u16 wValue;
  1098. __u16 wIndex;
  1099. __u16 wLength;
  1100. ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
  1101. #ifdef DEBUG
  1102. pkt_print(NULL, dev, pipe, buffer, transfer_len,
  1103. cmd, "SUB(rh)", usb_pipein(pipe));
  1104. #else
  1105. mdelay(1);
  1106. #endif
  1107. if (usb_pipeint(pipe)) {
  1108. info("Root-Hub submit IRQ: NOT implemented");
  1109. return 0;
  1110. }
  1111. bmRType_bReq = cmd->requesttype | (cmd->request << 8);
  1112. wValue = le16_to_cpu(cmd->value);
  1113. wIndex = le16_to_cpu(cmd->index);
  1114. wLength = le16_to_cpu(cmd->length);
  1115. info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
  1116. dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
  1117. switch (bmRType_bReq) {
  1118. /* Request Destination:
  1119. without flags: Device,
  1120. RH_INTERFACE: interface,
  1121. RH_ENDPOINT: endpoint,
  1122. RH_CLASS means HUB here,
  1123. RH_OTHER | RH_CLASS almost ever means HUB_PORT here
  1124. */
  1125. case RH_GET_STATUS:
  1126. *(u16 *)databuf = cpu_to_le16(1);
  1127. OK(2);
  1128. case RH_GET_STATUS | RH_INTERFACE:
  1129. *(u16 *)databuf = cpu_to_le16(0);
  1130. OK(2);
  1131. case RH_GET_STATUS | RH_ENDPOINT:
  1132. *(u16 *)databuf = cpu_to_le16(0);
  1133. OK(2);
  1134. case RH_GET_STATUS | RH_CLASS:
  1135. *(u32 *)databuf = cpu_to_le32(
  1136. RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
  1137. OK(4);
  1138. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  1139. *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
  1140. OK(4);
  1141. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  1142. switch (wValue) {
  1143. case (RH_ENDPOINT_STALL):
  1144. OK(0);
  1145. }
  1146. break;
  1147. case RH_CLEAR_FEATURE | RH_CLASS:
  1148. switch (wValue) {
  1149. case RH_C_HUB_LOCAL_POWER:
  1150. OK(0);
  1151. case (RH_C_HUB_OVER_CURRENT):
  1152. WR_RH_STAT(RH_HS_OCIC);
  1153. OK(0);
  1154. }
  1155. break;
  1156. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  1157. switch (wValue) {
  1158. case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
  1159. case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
  1160. case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
  1161. case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
  1162. case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
  1163. case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
  1164. case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
  1165. case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
  1166. }
  1167. break;
  1168. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  1169. switch (wValue) {
  1170. case (RH_PORT_SUSPEND):
  1171. WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
  1172. case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
  1173. if (RD_RH_PORTSTAT & RH_PS_CCS)
  1174. WR_RH_PORTSTAT(RH_PS_PRS);
  1175. OK(0);
  1176. case (RH_PORT_POWER):
  1177. WR_RH_PORTSTAT(RH_PS_PPS);
  1178. mdelay(100);
  1179. OK(0);
  1180. case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
  1181. if (RD_RH_PORTSTAT & RH_PS_CCS)
  1182. WR_RH_PORTSTAT(RH_PS_PES);
  1183. OK(0);
  1184. }
  1185. break;
  1186. case RH_SET_ADDRESS:
  1187. gohci.rh.devnum = wValue;
  1188. OK(0);
  1189. case RH_GET_DESCRIPTOR:
  1190. switch ((wValue & 0xff00) >> 8) {
  1191. case (0x01): /* device descriptor */
  1192. len = min_t(unsigned int,
  1193. leni,
  1194. min_t(unsigned int,
  1195. sizeof(root_hub_dev_des),
  1196. wLength));
  1197. databuf = root_hub_dev_des; OK(len);
  1198. case (0x02): /* configuration descriptor */
  1199. len = min_t(unsigned int,
  1200. leni,
  1201. min_t(unsigned int,
  1202. sizeof(root_hub_config_des),
  1203. wLength));
  1204. databuf = root_hub_config_des; OK(len);
  1205. case (0x03): /* string descriptors */
  1206. if (wValue == 0x0300) {
  1207. len = min_t(unsigned int,
  1208. leni,
  1209. min_t(unsigned int,
  1210. sizeof(root_hub_str_index0),
  1211. wLength));
  1212. databuf = root_hub_str_index0;
  1213. OK(len);
  1214. }
  1215. if (wValue == 0x0301) {
  1216. len = min_t(unsigned int,
  1217. leni,
  1218. min_t(unsigned int,
  1219. sizeof(root_hub_str_index1),
  1220. wLength));
  1221. databuf = root_hub_str_index1;
  1222. OK(len);
  1223. }
  1224. default:
  1225. stat = USB_ST_STALLED;
  1226. }
  1227. break;
  1228. case RH_GET_DESCRIPTOR | RH_CLASS:
  1229. {
  1230. __u32 temp = roothub_a(&gohci);
  1231. databuf[0] = 9; /* min length; */
  1232. databuf[1] = 0x29;
  1233. databuf[2] = temp & RH_A_NDP;
  1234. #ifdef CONFIG_AT91C_PQFP_UHPBUG
  1235. databuf[2] = (databuf[2] == 2) ? 1 : 0;
  1236. #endif
  1237. databuf[3] = 0;
  1238. if (temp & RH_A_PSM) /* per-port power switching? */
  1239. databuf[3] |= 0x1;
  1240. if (temp & RH_A_NOCP) /* no overcurrent reporting? */
  1241. databuf[3] |= 0x10;
  1242. else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
  1243. databuf[3] |= 0x8;
  1244. databuf[4] = 0;
  1245. databuf[5] = (temp & RH_A_POTPGT) >> 24;
  1246. databuf[6] = 0;
  1247. temp = roothub_b(&gohci);
  1248. databuf[7] = temp & RH_B_DR;
  1249. if (databuf[2] < 7) {
  1250. databuf[8] = 0xff;
  1251. } else {
  1252. databuf[0] += 2;
  1253. databuf[8] = (temp & RH_B_DR) >> 8;
  1254. databuf[10] = databuf[9] = 0xff;
  1255. }
  1256. len = min_t(unsigned int, leni,
  1257. min_t(unsigned int, databuf[0], wLength));
  1258. OK(len);
  1259. }
  1260. case RH_GET_CONFIGURATION:
  1261. databuf[0] = 0x01;
  1262. OK(1);
  1263. case RH_SET_CONFIGURATION:
  1264. WR_RH_STAT(0x10000);
  1265. OK(0);
  1266. default:
  1267. dbg("unsupported root hub command");
  1268. stat = USB_ST_STALLED;
  1269. }
  1270. #ifdef DEBUG
  1271. ohci_dump_roothub(&gohci, 1);
  1272. #else
  1273. mdelay(1);
  1274. #endif
  1275. len = min_t(int, len, leni);
  1276. if (data != databuf)
  1277. memcpy(data, databuf, len);
  1278. dev->act_len = len;
  1279. dev->status = stat;
  1280. #ifdef DEBUG
  1281. pkt_print(NULL, dev, pipe, buffer,
  1282. transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
  1283. #else
  1284. mdelay(1);
  1285. #endif
  1286. return stat;
  1287. }
  1288. /*-------------------------------------------------------------------------*/
  1289. /* common code for handling submit messages - used for all but root hub */
  1290. /* accesses. */
  1291. int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1292. int transfer_len, struct devrequest *setup, int interval)
  1293. {
  1294. int stat = 0;
  1295. int maxsize = usb_maxpacket(dev, pipe);
  1296. int timeout;
  1297. urb_priv_t *urb;
  1298. urb = malloc(sizeof(urb_priv_t));
  1299. memset(urb, 0, sizeof(urb_priv_t));
  1300. urb->dev = dev;
  1301. urb->pipe = pipe;
  1302. urb->transfer_buffer = buffer;
  1303. urb->transfer_buffer_length = transfer_len;
  1304. urb->interval = interval;
  1305. /* device pulled? Shortcut the action. */
  1306. if (devgone == dev) {
  1307. dev->status = USB_ST_CRC_ERR;
  1308. return 0;
  1309. }
  1310. #ifdef DEBUG
  1311. urb->actual_length = 0;
  1312. pkt_print(urb, dev, pipe, buffer, transfer_len,
  1313. setup, "SUB", usb_pipein(pipe));
  1314. #else
  1315. mdelay(1);
  1316. #endif
  1317. if (!maxsize) {
  1318. err("submit_common_message: pipesize for pipe %lx is zero",
  1319. pipe);
  1320. return -1;
  1321. }
  1322. if (sohci_submit_job(urb, setup) < 0) {
  1323. err("sohci_submit_job failed");
  1324. return -1;
  1325. }
  1326. #if 0
  1327. mdelay(10);
  1328. /* ohci_dump_status(&gohci); */
  1329. #endif
  1330. timeout = USB_TIMEOUT_MS(pipe);
  1331. /* wait for it to complete */
  1332. for (;;) {
  1333. /* check whether the controller is done */
  1334. stat = hc_interrupt();
  1335. if (stat < 0) {
  1336. stat = USB_ST_CRC_ERR;
  1337. break;
  1338. }
  1339. /* NOTE: since we are not interrupt driven in U-Boot and always
  1340. * handle only one URB at a time, we cannot assume the
  1341. * transaction finished on the first successful return from
  1342. * hc_interrupt().. unless the flag for current URB is set,
  1343. * meaning that all TD's to/from device got actually
  1344. * transferred and processed. If the current URB is not
  1345. * finished we need to re-iterate this loop so as
  1346. * hc_interrupt() gets called again as there needs to be some
  1347. * more TD's to process still */
  1348. if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
  1349. /* 0xff is returned for an SF-interrupt */
  1350. break;
  1351. }
  1352. if (--timeout) {
  1353. mdelay(1);
  1354. if (!urb->finished)
  1355. dbg("*");
  1356. } else {
  1357. err("CTL:TIMEOUT ");
  1358. dbg("submit_common_msg: TO status %x\n", stat);
  1359. urb->finished = 1;
  1360. stat = USB_ST_CRC_ERR;
  1361. break;
  1362. }
  1363. }
  1364. dev->status = stat;
  1365. dev->act_len = transfer_len;
  1366. #ifdef DEBUG
  1367. pkt_print(urb, dev, pipe, buffer, transfer_len,
  1368. setup, "RET(ctlr)", usb_pipein(pipe));
  1369. #else
  1370. mdelay(1);
  1371. #endif
  1372. /* free TDs in urb_priv */
  1373. if (!usb_pipeint(pipe))
  1374. urb_free_priv(urb);
  1375. return 0;
  1376. }
  1377. /* submit routines called from usb.c */
  1378. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1379. int transfer_len)
  1380. {
  1381. info("submit_bulk_msg");
  1382. return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
  1383. }
  1384. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1385. int transfer_len, struct devrequest *setup)
  1386. {
  1387. int maxsize = usb_maxpacket(dev, pipe);
  1388. info("submit_control_msg");
  1389. #ifdef DEBUG
  1390. pkt_print(NULL, dev, pipe, buffer, transfer_len,
  1391. setup, "SUB", usb_pipein(pipe));
  1392. #else
  1393. mdelay(1);
  1394. #endif
  1395. if (!maxsize) {
  1396. err("submit_control_message: pipesize for pipe %lx is zero",
  1397. pipe);
  1398. return -1;
  1399. }
  1400. if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
  1401. gohci.rh.dev = dev;
  1402. /* root hub - redirect */
  1403. return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
  1404. setup);
  1405. }
  1406. return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
  1407. }
  1408. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1409. int transfer_len, int interval)
  1410. {
  1411. info("submit_int_msg");
  1412. return submit_common_msg(dev, pipe, buffer, transfer_len, NULL,
  1413. interval);
  1414. }
  1415. /*-------------------------------------------------------------------------*
  1416. * HC functions
  1417. *-------------------------------------------------------------------------*/
  1418. /* reset the HC and BUS */
  1419. static int hc_reset(ohci_t *ohci)
  1420. {
  1421. #ifdef CONFIG_PCI_EHCI_DEVNO
  1422. pci_dev_t pdev;
  1423. #endif
  1424. int timeout = 30;
  1425. int smm_timeout = 50; /* 0,5 sec */
  1426. dbg("%s\n", __FUNCTION__);
  1427. #ifdef CONFIG_PCI_EHCI_DEVNO
  1428. /*
  1429. * Some multi-function controllers (e.g. ISP1562) allow root hub
  1430. * resetting via EHCI registers only.
  1431. */
  1432. pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
  1433. if (pdev != -1) {
  1434. u32 base;
  1435. int timeout = 1000;
  1436. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
  1437. base += EHCI_USBCMD_OFF;
  1438. ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
  1439. while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
  1440. if (timeout-- <= 0) {
  1441. printf("USB RootHub reset timed out!");
  1442. break;
  1443. }
  1444. udelay(1);
  1445. }
  1446. } else
  1447. printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
  1448. #endif
  1449. if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
  1450. /* SMM owns the HC, request ownership */
  1451. ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
  1452. info("USB HC TakeOver from SMM");
  1453. while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
  1454. mdelay(10);
  1455. if (--smm_timeout == 0) {
  1456. err("USB HC TakeOver failed!");
  1457. return -1;
  1458. }
  1459. }
  1460. }
  1461. /* Disable HC interrupts */
  1462. ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
  1463. dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
  1464. ohci->slot_name,
  1465. ohci_readl(&ohci->regs->control));
  1466. /* Reset USB (needed by some controllers) */
  1467. ohci->hc_control = 0;
  1468. ohci_writel(ohci->hc_control, &ohci->regs->control);
  1469. /* HC Reset requires max 10 us delay */
  1470. ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
  1471. while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
  1472. if (--timeout == 0) {
  1473. err("USB HC reset timed out!");
  1474. return -1;
  1475. }
  1476. udelay(1);
  1477. }
  1478. return 0;
  1479. }
  1480. /*-------------------------------------------------------------------------*/
  1481. /* Start an OHCI controller, set the BUS operational
  1482. * enable interrupts
  1483. * connect the virtual root hub */
  1484. static int hc_start(ohci_t *ohci)
  1485. {
  1486. __u32 mask;
  1487. unsigned int fminterval;
  1488. ohci->disabled = 1;
  1489. /* Tell the controller where the control and bulk lists are
  1490. * The lists are empty now. */
  1491. ohci_writel(0, &ohci->regs->ed_controlhead);
  1492. ohci_writel(0, &ohci->regs->ed_bulkhead);
  1493. ohci_writel((__u32)ohci->hcca,
  1494. &ohci->regs->hcca); /* reset clears this */
  1495. fminterval = 0x2edf;
  1496. ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
  1497. fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
  1498. ohci_writel(fminterval, &ohci->regs->fminterval);
  1499. ohci_writel(0x628, &ohci->regs->lsthresh);
  1500. /* start controller operations */
  1501. ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
  1502. ohci->disabled = 0;
  1503. ohci_writel(ohci->hc_control, &ohci->regs->control);
  1504. /* disable all interrupts */
  1505. mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
  1506. OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
  1507. OHCI_INTR_OC | OHCI_INTR_MIE);
  1508. ohci_writel(mask, &ohci->regs->intrdisable);
  1509. /* clear all interrupts */
  1510. mask &= ~OHCI_INTR_MIE;
  1511. ohci_writel(mask, &ohci->regs->intrstatus);
  1512. /* Choose the interrupts we care about now - but w/o MIE */
  1513. mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
  1514. ohci_writel(mask, &ohci->regs->intrenable);
  1515. #ifdef OHCI_USE_NPS
  1516. /* required for AMD-756 and some Mac platforms */
  1517. ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
  1518. &ohci->regs->roothub.a);
  1519. ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
  1520. #endif /* OHCI_USE_NPS */
  1521. /* POTPGT delay is bits 24-31, in 2 ms units. */
  1522. mdelay((roothub_a(ohci) >> 23) & 0x1fe);
  1523. /* connect the virtual root hub */
  1524. ohci->rh.devnum = 0;
  1525. return 0;
  1526. }
  1527. /*-------------------------------------------------------------------------*/
  1528. /* an interrupt happens */
  1529. static int hc_interrupt(void)
  1530. {
  1531. ohci_t *ohci = &gohci;
  1532. struct ohci_regs *regs = ohci->regs;
  1533. int ints;
  1534. int stat = -1;
  1535. if ((ohci->hcca->done_head != 0) &&
  1536. !(m32_swap(ohci->hcca->done_head) & 0x01)) {
  1537. ints = OHCI_INTR_WDH;
  1538. } else {
  1539. ints = ohci_readl(&regs->intrstatus);
  1540. if (ints == ~(u32)0) {
  1541. ohci->disabled++;
  1542. err("%s device removed!", ohci->slot_name);
  1543. return -1;
  1544. } else {
  1545. ints &= ohci_readl(&regs->intrenable);
  1546. if (ints == 0) {
  1547. dbg("hc_interrupt: returning..\n");
  1548. return 0xff;
  1549. }
  1550. }
  1551. }
  1552. /* dbg("Interrupt: %x frame: %x", ints,
  1553. le16_to_cpu(ohci->hcca->frame_no)); */
  1554. if (ints & OHCI_INTR_RHSC)
  1555. stat = 0xff;
  1556. if (ints & OHCI_INTR_UE) {
  1557. ohci->disabled++;
  1558. err("OHCI Unrecoverable Error, controller usb-%s disabled",
  1559. ohci->slot_name);
  1560. /* e.g. due to PCI Master/Target Abort */
  1561. #ifdef DEBUG
  1562. ohci_dump(ohci, 1);
  1563. #else
  1564. mdelay(1);
  1565. #endif
  1566. /* FIXME: be optimistic, hope that bug won't repeat often. */
  1567. /* Make some non-interrupt context restart the controller. */
  1568. /* Count and limit the retries though; either hardware or */
  1569. /* software errors can go forever... */
  1570. hc_reset(ohci);
  1571. return -1;
  1572. }
  1573. if (ints & OHCI_INTR_WDH) {
  1574. mdelay(1);
  1575. ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
  1576. (void)ohci_readl(&regs->intrdisable); /* flush */
  1577. stat = dl_done_list(&gohci);
  1578. ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
  1579. (void)ohci_readl(&regs->intrdisable); /* flush */
  1580. }
  1581. if (ints & OHCI_INTR_SO) {
  1582. dbg("USB Schedule overrun\n");
  1583. ohci_writel(OHCI_INTR_SO, &regs->intrenable);
  1584. stat = -1;
  1585. }
  1586. /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
  1587. if (ints & OHCI_INTR_SF) {
  1588. unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
  1589. mdelay(1);
  1590. ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
  1591. if (ohci->ed_rm_list[frame] != NULL)
  1592. ohci_writel(OHCI_INTR_SF, &regs->intrenable);
  1593. stat = 0xff;
  1594. }
  1595. ohci_writel(ints, &regs->intrstatus);
  1596. return stat;
  1597. }
  1598. /*-------------------------------------------------------------------------*/
  1599. /*-------------------------------------------------------------------------*/
  1600. /* De-allocate all resources.. */
  1601. static void hc_release_ohci(ohci_t *ohci)
  1602. {
  1603. dbg("USB HC release ohci usb-%s", ohci->slot_name);
  1604. if (!ohci->disabled)
  1605. hc_reset(ohci);
  1606. }
  1607. /*-------------------------------------------------------------------------*/
  1608. /*
  1609. * low level initalisation routine, called from usb.c
  1610. */
  1611. static char ohci_inited = 0;
  1612. int usb_lowlevel_init(int index, void **controller)
  1613. {
  1614. #ifdef CONFIG_PCI_OHCI
  1615. pci_dev_t pdev;
  1616. #endif
  1617. #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
  1618. /* cpu dependant init */
  1619. if (usb_cpu_init())
  1620. return -1;
  1621. #endif
  1622. #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
  1623. /* board dependant init */
  1624. if (usb_board_init())
  1625. return -1;
  1626. #endif
  1627. memset(&gohci, 0, sizeof(ohci_t));
  1628. /* align the storage */
  1629. if ((__u32)&ghcca[0] & 0xff) {
  1630. err("HCCA not aligned!!");
  1631. return -1;
  1632. }
  1633. phcca = &ghcca[0];
  1634. info("aligned ghcca %p", phcca);
  1635. memset(&ohci_dev, 0, sizeof(struct ohci_device));
  1636. if ((__u32)&ohci_dev.ed[0] & 0x7) {
  1637. err("EDs not aligned!!");
  1638. return -1;
  1639. }
  1640. memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
  1641. if ((__u32)gtd & 0x7) {
  1642. err("TDs not aligned!!");
  1643. return -1;
  1644. }
  1645. ptd = gtd;
  1646. gohci.hcca = phcca;
  1647. memset(phcca, 0, sizeof(struct ohci_hcca));
  1648. gohci.disabled = 1;
  1649. gohci.sleeping = 0;
  1650. gohci.irq = -1;
  1651. #ifdef CONFIG_PCI_OHCI
  1652. pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
  1653. if (pdev != -1) {
  1654. u16 vid, did;
  1655. u32 base;
  1656. pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
  1657. pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
  1658. printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
  1659. vid, did, (pdev >> 16) & 0xff,
  1660. (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
  1661. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
  1662. printf("OHCI regs address 0x%08x\n", base);
  1663. gohci.regs = (struct ohci_regs *)base;
  1664. } else
  1665. return -1;
  1666. #else
  1667. gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
  1668. #endif
  1669. gohci.flags = 0;
  1670. gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
  1671. if (hc_reset (&gohci) < 0) {
  1672. hc_release_ohci (&gohci);
  1673. err ("can't reset usb-%s", gohci.slot_name);
  1674. #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
  1675. /* board dependant cleanup */
  1676. usb_board_init_fail();
  1677. #endif
  1678. #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
  1679. /* cpu dependant cleanup */
  1680. usb_cpu_init_fail();
  1681. #endif
  1682. return -1;
  1683. }
  1684. if (hc_start(&gohci) < 0) {
  1685. err("can't start usb-%s", gohci.slot_name);
  1686. hc_release_ohci(&gohci);
  1687. /* Initialization failed */
  1688. #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
  1689. /* board dependant cleanup */
  1690. usb_board_stop();
  1691. #endif
  1692. #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
  1693. /* cpu dependant cleanup */
  1694. usb_cpu_stop();
  1695. #endif
  1696. return -1;
  1697. }
  1698. #ifdef DEBUG
  1699. ohci_dump(&gohci, 1);
  1700. #else
  1701. mdelay(1);
  1702. #endif
  1703. ohci_inited = 1;
  1704. return 0;
  1705. }
  1706. int usb_lowlevel_stop(int index)
  1707. {
  1708. /* this gets called really early - before the controller has */
  1709. /* even been initialized! */
  1710. if (!ohci_inited)
  1711. return 0;
  1712. /* TODO release any interrupts, etc. */
  1713. /* call hc_release_ohci() here ? */
  1714. hc_reset(&gohci);
  1715. #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
  1716. /* board dependant cleanup */
  1717. if (usb_board_stop())
  1718. return -1;
  1719. #endif
  1720. #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
  1721. /* cpu dependant cleanup */
  1722. if (usb_cpu_stop())
  1723. return -1;
  1724. #endif
  1725. /* This driver is no longer initialised. It needs a new low-level
  1726. * init (board/cpu) before it can be used again. */
  1727. ohci_inited = 0;
  1728. return 0;
  1729. }