usb_ohci.c 51 KB

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