usb_ohci.c 50 KB

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