usb_ohci.c 45 KB

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