usb_ohci.c 51 KB

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