usb_ohci.c 51 KB

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