usb_ohci.c 52 KB

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