usb_ohci.c 51 KB

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