usb_ohci.c 51 KB

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