usb_ohci.c 45 KB

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