ohci-hcd.c 58 KB

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