usb_ohci.c 51 KB

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