ohci-hcd.c 51 KB

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