ohci-hcd.c 48 KB

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