usb_ohci.c 51 KB

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