usb_ohci.c 45 KB

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