usb_ohci.c 44 KB

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