usb_ohci.c 41 KB

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