usb_ohci.c 52 KB

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