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