ohci-s3c24xx.c 43 KB

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