ohci-hcd.c 58 KB

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