ohci-hcd.c 53 KB

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