au1x00_usb_ohci.c 42 KB

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