ohci-hcd.c 51 KB

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