usb_ohci.c 45 KB

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