imx21-hcd.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. /*
  2. * USB Host Controller Driver for IMX21
  3. *
  4. * Copyright (C) 2006 Loping Dog Embedded Systems
  5. * Copyright (C) 2009 Martin Fuzzey
  6. * Originally written by Jay Monkman <jtm@lopingdog.com>
  7. * Ported to 2.6.30, debugged and enhanced by Martin Fuzzey
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. /*
  24. * The i.MX21 USB hardware contains
  25. * * 32 transfer descriptors (called ETDs)
  26. * * 4Kb of Data memory
  27. *
  28. * The data memory is shared between the host and function controllers
  29. * (but this driver only supports the host controller)
  30. *
  31. * So setting up a transfer involves:
  32. * * Allocating a ETD
  33. * * Fill in ETD with appropriate information
  34. * * Allocating data memory (and putting the offset in the ETD)
  35. * * Activate the ETD
  36. * * Get interrupt when done.
  37. *
  38. * An ETD is assigned to each active endpoint.
  39. *
  40. * Low resource (ETD and Data memory) situations are handled differently for
  41. * isochronous and non insosynchronous transactions :
  42. *
  43. * Non ISOC transfers are queued if either ETDs or Data memory are unavailable
  44. *
  45. * ISOC transfers use 2 ETDs per endpoint to achieve double buffering.
  46. * They allocate both ETDs and Data memory during URB submission
  47. * (and fail if unavailable).
  48. */
  49. #include <linux/clk.h>
  50. #include <linux/io.h>
  51. #include <linux/kernel.h>
  52. #include <linux/list.h>
  53. #include <linux/platform_device.h>
  54. #include <linux/slab.h>
  55. #include <linux/usb.h>
  56. #include <linux/usb/hcd.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/module.h>
  59. #include "imx21-hcd.h"
  60. #ifdef DEBUG
  61. #define DEBUG_LOG_FRAME(imx21, etd, event) \
  62. (etd)->event##_frame = readl((imx21)->regs + USBH_FRMNUB)
  63. #else
  64. #define DEBUG_LOG_FRAME(imx21, etd, event) do { } while (0)
  65. #endif
  66. static const char hcd_name[] = "imx21-hcd";
  67. static inline struct imx21 *hcd_to_imx21(struct usb_hcd *hcd)
  68. {
  69. return (struct imx21 *)hcd->hcd_priv;
  70. }
  71. /* =========================================== */
  72. /* Hardware access helpers */
  73. /* =========================================== */
  74. static inline void set_register_bits(struct imx21 *imx21, u32 offset, u32 mask)
  75. {
  76. void __iomem *reg = imx21->regs + offset;
  77. writel(readl(reg) | mask, reg);
  78. }
  79. static inline void clear_register_bits(struct imx21 *imx21,
  80. u32 offset, u32 mask)
  81. {
  82. void __iomem *reg = imx21->regs + offset;
  83. writel(readl(reg) & ~mask, reg);
  84. }
  85. static inline void clear_toggle_bit(struct imx21 *imx21, u32 offset, u32 mask)
  86. {
  87. void __iomem *reg = imx21->regs + offset;
  88. if (readl(reg) & mask)
  89. writel(mask, reg);
  90. }
  91. static inline void set_toggle_bit(struct imx21 *imx21, u32 offset, u32 mask)
  92. {
  93. void __iomem *reg = imx21->regs + offset;
  94. if (!(readl(reg) & mask))
  95. writel(mask, reg);
  96. }
  97. static void etd_writel(struct imx21 *imx21, int etd_num, int dword, u32 value)
  98. {
  99. writel(value, imx21->regs + USB_ETD_DWORD(etd_num, dword));
  100. }
  101. static u32 etd_readl(struct imx21 *imx21, int etd_num, int dword)
  102. {
  103. return readl(imx21->regs + USB_ETD_DWORD(etd_num, dword));
  104. }
  105. static inline int wrap_frame(int counter)
  106. {
  107. return counter & 0xFFFF;
  108. }
  109. static inline int frame_after(int frame, int after)
  110. {
  111. /* handle wrapping like jiffies time_afer */
  112. return (s16)((s16)after - (s16)frame) < 0;
  113. }
  114. static int imx21_hc_get_frame(struct usb_hcd *hcd)
  115. {
  116. struct imx21 *imx21 = hcd_to_imx21(hcd);
  117. return wrap_frame(readl(imx21->regs + USBH_FRMNUB));
  118. }
  119. static inline bool unsuitable_for_dma(dma_addr_t addr)
  120. {
  121. return (addr & 3) != 0;
  122. }
  123. #include "imx21-dbg.c"
  124. static void nonisoc_urb_completed_for_etd(
  125. struct imx21 *imx21, struct etd_priv *etd, int status);
  126. static void schedule_nonisoc_etd(struct imx21 *imx21, struct urb *urb);
  127. static void free_dmem(struct imx21 *imx21, struct etd_priv *etd);
  128. /* =========================================== */
  129. /* ETD management */
  130. /* =========================================== */
  131. static int alloc_etd(struct imx21 *imx21)
  132. {
  133. int i;
  134. struct etd_priv *etd = imx21->etd;
  135. for (i = 0; i < USB_NUM_ETD; i++, etd++) {
  136. if (etd->alloc == 0) {
  137. memset(etd, 0, sizeof(imx21->etd[0]));
  138. etd->alloc = 1;
  139. debug_etd_allocated(imx21);
  140. return i;
  141. }
  142. }
  143. return -1;
  144. }
  145. static void disactivate_etd(struct imx21 *imx21, int num)
  146. {
  147. int etd_mask = (1 << num);
  148. struct etd_priv *etd = &imx21->etd[num];
  149. writel(etd_mask, imx21->regs + USBH_ETDENCLR);
  150. clear_register_bits(imx21, USBH_ETDDONEEN, etd_mask);
  151. writel(etd_mask, imx21->regs + USB_ETDDMACHANLCLR);
  152. clear_toggle_bit(imx21, USBH_ETDDONESTAT, etd_mask);
  153. etd->active_count = 0;
  154. DEBUG_LOG_FRAME(imx21, etd, disactivated);
  155. }
  156. static void reset_etd(struct imx21 *imx21, int num)
  157. {
  158. struct etd_priv *etd = imx21->etd + num;
  159. int i;
  160. disactivate_etd(imx21, num);
  161. for (i = 0; i < 4; i++)
  162. etd_writel(imx21, num, i, 0);
  163. etd->urb = NULL;
  164. etd->ep = NULL;
  165. etd->td = NULL;
  166. etd->bounce_buffer = NULL;
  167. }
  168. static void free_etd(struct imx21 *imx21, int num)
  169. {
  170. if (num < 0)
  171. return;
  172. if (num >= USB_NUM_ETD) {
  173. dev_err(imx21->dev, "BAD etd=%d!\n", num);
  174. return;
  175. }
  176. if (imx21->etd[num].alloc == 0) {
  177. dev_err(imx21->dev, "ETD %d already free!\n", num);
  178. return;
  179. }
  180. debug_etd_freed(imx21);
  181. reset_etd(imx21, num);
  182. memset(&imx21->etd[num], 0, sizeof(imx21->etd[0]));
  183. }
  184. static void setup_etd_dword0(struct imx21 *imx21,
  185. int etd_num, struct urb *urb, u8 dir, u16 maxpacket)
  186. {
  187. etd_writel(imx21, etd_num, 0,
  188. ((u32) usb_pipedevice(urb->pipe)) << DW0_ADDRESS |
  189. ((u32) usb_pipeendpoint(urb->pipe) << DW0_ENDPNT) |
  190. ((u32) dir << DW0_DIRECT) |
  191. ((u32) ((urb->dev->speed == USB_SPEED_LOW) ?
  192. 1 : 0) << DW0_SPEED) |
  193. ((u32) fmt_urb_to_etd[usb_pipetype(urb->pipe)] << DW0_FORMAT) |
  194. ((u32) maxpacket << DW0_MAXPKTSIZ));
  195. }
  196. /**
  197. * Copy buffer to data controller data memory.
  198. * We cannot use memcpy_toio() because the hardware requires 32bit writes
  199. */
  200. static void copy_to_dmem(
  201. struct imx21 *imx21, int dmem_offset, void *src, int count)
  202. {
  203. void __iomem *dmem = imx21->regs + USBOTG_DMEM + dmem_offset;
  204. u32 word = 0;
  205. u8 *p = src;
  206. int byte = 0;
  207. int i;
  208. for (i = 0; i < count; i++) {
  209. byte = i % 4;
  210. word += (*p++ << (byte * 8));
  211. if (byte == 3) {
  212. writel(word, dmem);
  213. dmem += 4;
  214. word = 0;
  215. }
  216. }
  217. if (count && byte != 3)
  218. writel(word, dmem);
  219. }
  220. static void activate_etd(struct imx21 *imx21, int etd_num, u8 dir)
  221. {
  222. u32 etd_mask = 1 << etd_num;
  223. struct etd_priv *etd = &imx21->etd[etd_num];
  224. if (etd->dma_handle && unsuitable_for_dma(etd->dma_handle)) {
  225. /* For non aligned isoc the condition below is always true */
  226. if (etd->len <= etd->dmem_size) {
  227. /* Fits into data memory, use PIO */
  228. if (dir != TD_DIR_IN) {
  229. copy_to_dmem(imx21,
  230. etd->dmem_offset,
  231. etd->cpu_buffer, etd->len);
  232. }
  233. etd->dma_handle = 0;
  234. } else {
  235. /* Too big for data memory, use bounce buffer */
  236. enum dma_data_direction dmadir;
  237. if (dir == TD_DIR_IN) {
  238. dmadir = DMA_FROM_DEVICE;
  239. etd->bounce_buffer = kmalloc(etd->len,
  240. GFP_ATOMIC);
  241. } else {
  242. dmadir = DMA_TO_DEVICE;
  243. etd->bounce_buffer = kmemdup(etd->cpu_buffer,
  244. etd->len,
  245. GFP_ATOMIC);
  246. }
  247. if (!etd->bounce_buffer) {
  248. dev_err(imx21->dev, "failed bounce alloc\n");
  249. goto err_bounce_alloc;
  250. }
  251. etd->dma_handle =
  252. dma_map_single(imx21->dev,
  253. etd->bounce_buffer,
  254. etd->len,
  255. dmadir);
  256. if (dma_mapping_error(imx21->dev, etd->dma_handle)) {
  257. dev_err(imx21->dev, "failed bounce map\n");
  258. goto err_bounce_map;
  259. }
  260. }
  261. }
  262. clear_toggle_bit(imx21, USBH_ETDDONESTAT, etd_mask);
  263. set_register_bits(imx21, USBH_ETDDONEEN, etd_mask);
  264. clear_toggle_bit(imx21, USBH_XFILLSTAT, etd_mask);
  265. clear_toggle_bit(imx21, USBH_YFILLSTAT, etd_mask);
  266. if (etd->dma_handle) {
  267. set_register_bits(imx21, USB_ETDDMACHANLCLR, etd_mask);
  268. clear_toggle_bit(imx21, USBH_XBUFSTAT, etd_mask);
  269. clear_toggle_bit(imx21, USBH_YBUFSTAT, etd_mask);
  270. writel(etd->dma_handle, imx21->regs + USB_ETDSMSA(etd_num));
  271. set_register_bits(imx21, USB_ETDDMAEN, etd_mask);
  272. } else {
  273. if (dir != TD_DIR_IN) {
  274. /* need to set for ZLP and PIO */
  275. set_toggle_bit(imx21, USBH_XFILLSTAT, etd_mask);
  276. set_toggle_bit(imx21, USBH_YFILLSTAT, etd_mask);
  277. }
  278. }
  279. DEBUG_LOG_FRAME(imx21, etd, activated);
  280. #ifdef DEBUG
  281. if (!etd->active_count) {
  282. int i;
  283. etd->activated_frame = readl(imx21->regs + USBH_FRMNUB);
  284. etd->disactivated_frame = -1;
  285. etd->last_int_frame = -1;
  286. etd->last_req_frame = -1;
  287. for (i = 0; i < 4; i++)
  288. etd->submitted_dwords[i] = etd_readl(imx21, etd_num, i);
  289. }
  290. #endif
  291. etd->active_count = 1;
  292. writel(etd_mask, imx21->regs + USBH_ETDENSET);
  293. return;
  294. err_bounce_map:
  295. kfree(etd->bounce_buffer);
  296. err_bounce_alloc:
  297. free_dmem(imx21, etd);
  298. nonisoc_urb_completed_for_etd(imx21, etd, -ENOMEM);
  299. }
  300. /* =========================================== */
  301. /* Data memory management */
  302. /* =========================================== */
  303. static int alloc_dmem(struct imx21 *imx21, unsigned int size,
  304. struct usb_host_endpoint *ep)
  305. {
  306. unsigned int offset = 0;
  307. struct imx21_dmem_area *area;
  308. struct imx21_dmem_area *tmp;
  309. size += (~size + 1) & 0x3; /* Round to 4 byte multiple */
  310. if (size > DMEM_SIZE) {
  311. dev_err(imx21->dev, "size=%d > DMEM_SIZE(%d)\n",
  312. size, DMEM_SIZE);
  313. return -EINVAL;
  314. }
  315. list_for_each_entry(tmp, &imx21->dmem_list, list) {
  316. if ((size + offset) < offset)
  317. goto fail;
  318. if ((size + offset) <= tmp->offset)
  319. break;
  320. offset = tmp->size + tmp->offset;
  321. if ((offset + size) > DMEM_SIZE)
  322. goto fail;
  323. }
  324. area = kmalloc(sizeof(struct imx21_dmem_area), GFP_ATOMIC);
  325. if (area == NULL)
  326. return -ENOMEM;
  327. area->ep = ep;
  328. area->offset = offset;
  329. area->size = size;
  330. list_add_tail(&area->list, &tmp->list);
  331. debug_dmem_allocated(imx21, size);
  332. return offset;
  333. fail:
  334. return -ENOMEM;
  335. }
  336. /* Memory now available for a queued ETD - activate it */
  337. static void activate_queued_etd(struct imx21 *imx21,
  338. struct etd_priv *etd, u32 dmem_offset)
  339. {
  340. struct urb_priv *urb_priv = etd->urb->hcpriv;
  341. int etd_num = etd - &imx21->etd[0];
  342. u32 maxpacket = etd_readl(imx21, etd_num, 1) >> DW1_YBUFSRTAD;
  343. u8 dir = (etd_readl(imx21, etd_num, 2) >> DW2_DIRPID) & 0x03;
  344. dev_dbg(imx21->dev, "activating queued ETD %d now DMEM available\n",
  345. etd_num);
  346. etd_writel(imx21, etd_num, 1,
  347. ((dmem_offset + maxpacket) << DW1_YBUFSRTAD) | dmem_offset);
  348. etd->dmem_offset = dmem_offset;
  349. urb_priv->active = 1;
  350. activate_etd(imx21, etd_num, dir);
  351. }
  352. static void free_dmem(struct imx21 *imx21, struct etd_priv *etd)
  353. {
  354. struct imx21_dmem_area *area;
  355. struct etd_priv *tmp;
  356. int found = 0;
  357. int offset;
  358. if (!etd->dmem_size)
  359. return;
  360. etd->dmem_size = 0;
  361. offset = etd->dmem_offset;
  362. list_for_each_entry(area, &imx21->dmem_list, list) {
  363. if (area->offset == offset) {
  364. debug_dmem_freed(imx21, area->size);
  365. list_del(&area->list);
  366. kfree(area);
  367. found = 1;
  368. break;
  369. }
  370. }
  371. if (!found) {
  372. dev_err(imx21->dev,
  373. "Trying to free unallocated DMEM %d\n", offset);
  374. return;
  375. }
  376. /* Try again to allocate memory for anything we've queued */
  377. list_for_each_entry_safe(etd, tmp, &imx21->queue_for_dmem, queue) {
  378. offset = alloc_dmem(imx21, etd->dmem_size, etd->ep);
  379. if (offset >= 0) {
  380. list_del(&etd->queue);
  381. activate_queued_etd(imx21, etd, (u32)offset);
  382. }
  383. }
  384. }
  385. static void free_epdmem(struct imx21 *imx21, struct usb_host_endpoint *ep)
  386. {
  387. struct imx21_dmem_area *area, *tmp;
  388. list_for_each_entry_safe(area, tmp, &imx21->dmem_list, list) {
  389. if (area->ep == ep) {
  390. dev_err(imx21->dev,
  391. "Active DMEM %d for disabled ep=%p\n",
  392. area->offset, ep);
  393. list_del(&area->list);
  394. kfree(area);
  395. }
  396. }
  397. }
  398. /* =========================================== */
  399. /* End handling */
  400. /* =========================================== */
  401. /* Endpoint now idle - release its ETD(s) or assign to queued request */
  402. static void ep_idle(struct imx21 *imx21, struct ep_priv *ep_priv)
  403. {
  404. int i;
  405. for (i = 0; i < NUM_ISO_ETDS; i++) {
  406. int etd_num = ep_priv->etd[i];
  407. struct etd_priv *etd;
  408. if (etd_num < 0)
  409. continue;
  410. etd = &imx21->etd[etd_num];
  411. ep_priv->etd[i] = -1;
  412. free_dmem(imx21, etd); /* for isoc */
  413. if (list_empty(&imx21->queue_for_etd)) {
  414. free_etd(imx21, etd_num);
  415. continue;
  416. }
  417. dev_dbg(imx21->dev,
  418. "assigning idle etd %d for queued request\n", etd_num);
  419. ep_priv = list_first_entry(&imx21->queue_for_etd,
  420. struct ep_priv, queue);
  421. list_del(&ep_priv->queue);
  422. reset_etd(imx21, etd_num);
  423. ep_priv->waiting_etd = 0;
  424. ep_priv->etd[i] = etd_num;
  425. if (list_empty(&ep_priv->ep->urb_list)) {
  426. dev_err(imx21->dev, "No urb for queued ep!\n");
  427. continue;
  428. }
  429. schedule_nonisoc_etd(imx21, list_first_entry(
  430. &ep_priv->ep->urb_list, struct urb, urb_list));
  431. }
  432. }
  433. static void urb_done(struct usb_hcd *hcd, struct urb *urb, int status)
  434. __releases(imx21->lock)
  435. __acquires(imx21->lock)
  436. {
  437. struct imx21 *imx21 = hcd_to_imx21(hcd);
  438. struct ep_priv *ep_priv = urb->ep->hcpriv;
  439. struct urb_priv *urb_priv = urb->hcpriv;
  440. debug_urb_completed(imx21, urb, status);
  441. dev_vdbg(imx21->dev, "urb %p done %d\n", urb, status);
  442. kfree(urb_priv->isoc_td);
  443. kfree(urb->hcpriv);
  444. urb->hcpriv = NULL;
  445. usb_hcd_unlink_urb_from_ep(hcd, urb);
  446. spin_unlock(&imx21->lock);
  447. usb_hcd_giveback_urb(hcd, urb, status);
  448. spin_lock(&imx21->lock);
  449. if (list_empty(&ep_priv->ep->urb_list))
  450. ep_idle(imx21, ep_priv);
  451. }
  452. static void nonisoc_urb_completed_for_etd(
  453. struct imx21 *imx21, struct etd_priv *etd, int status)
  454. {
  455. struct usb_host_endpoint *ep = etd->ep;
  456. urb_done(imx21->hcd, etd->urb, status);
  457. etd->urb = NULL;
  458. if (!list_empty(&ep->urb_list)) {
  459. struct urb *urb = list_first_entry(
  460. &ep->urb_list, struct urb, urb_list);
  461. dev_vdbg(imx21->dev, "next URB %p\n", urb);
  462. schedule_nonisoc_etd(imx21, urb);
  463. }
  464. }
  465. /* =========================================== */
  466. /* ISOC Handling ... */
  467. /* =========================================== */
  468. static void schedule_isoc_etds(struct usb_hcd *hcd,
  469. struct usb_host_endpoint *ep)
  470. {
  471. struct imx21 *imx21 = hcd_to_imx21(hcd);
  472. struct ep_priv *ep_priv = ep->hcpriv;
  473. struct etd_priv *etd;
  474. struct urb_priv *urb_priv;
  475. struct td *td;
  476. int etd_num;
  477. int i;
  478. int cur_frame;
  479. u8 dir;
  480. for (i = 0; i < NUM_ISO_ETDS; i++) {
  481. too_late:
  482. if (list_empty(&ep_priv->td_list))
  483. break;
  484. etd_num = ep_priv->etd[i];
  485. if (etd_num < 0)
  486. break;
  487. etd = &imx21->etd[etd_num];
  488. if (etd->urb)
  489. continue;
  490. td = list_entry(ep_priv->td_list.next, struct td, list);
  491. list_del(&td->list);
  492. urb_priv = td->urb->hcpriv;
  493. cur_frame = imx21_hc_get_frame(hcd);
  494. if (frame_after(cur_frame, td->frame)) {
  495. dev_dbg(imx21->dev, "isoc too late frame %d > %d\n",
  496. cur_frame, td->frame);
  497. urb_priv->isoc_status = -EXDEV;
  498. td->urb->iso_frame_desc[
  499. td->isoc_index].actual_length = 0;
  500. td->urb->iso_frame_desc[td->isoc_index].status = -EXDEV;
  501. if (--urb_priv->isoc_remaining == 0)
  502. urb_done(hcd, td->urb, urb_priv->isoc_status);
  503. goto too_late;
  504. }
  505. urb_priv->active = 1;
  506. etd->td = td;
  507. etd->ep = td->ep;
  508. etd->urb = td->urb;
  509. etd->len = td->len;
  510. etd->dma_handle = td->dma_handle;
  511. etd->cpu_buffer = td->cpu_buffer;
  512. debug_isoc_submitted(imx21, cur_frame, td);
  513. dir = usb_pipeout(td->urb->pipe) ? TD_DIR_OUT : TD_DIR_IN;
  514. setup_etd_dword0(imx21, etd_num, td->urb, dir, etd->dmem_size);
  515. etd_writel(imx21, etd_num, 1, etd->dmem_offset);
  516. etd_writel(imx21, etd_num, 2,
  517. (TD_NOTACCESSED << DW2_COMPCODE) |
  518. ((td->frame & 0xFFFF) << DW2_STARTFRM));
  519. etd_writel(imx21, etd_num, 3,
  520. (TD_NOTACCESSED << DW3_COMPCODE0) |
  521. (td->len << DW3_PKTLEN0));
  522. activate_etd(imx21, etd_num, dir);
  523. }
  524. }
  525. static void isoc_etd_done(struct usb_hcd *hcd, int etd_num)
  526. {
  527. struct imx21 *imx21 = hcd_to_imx21(hcd);
  528. int etd_mask = 1 << etd_num;
  529. struct etd_priv *etd = imx21->etd + etd_num;
  530. struct urb *urb = etd->urb;
  531. struct urb_priv *urb_priv = urb->hcpriv;
  532. struct td *td = etd->td;
  533. struct usb_host_endpoint *ep = etd->ep;
  534. int isoc_index = td->isoc_index;
  535. unsigned int pipe = urb->pipe;
  536. int dir_in = usb_pipein(pipe);
  537. int cc;
  538. int bytes_xfrd;
  539. disactivate_etd(imx21, etd_num);
  540. cc = (etd_readl(imx21, etd_num, 3) >> DW3_COMPCODE0) & 0xf;
  541. bytes_xfrd = etd_readl(imx21, etd_num, 3) & 0x3ff;
  542. /* Input doesn't always fill the buffer, don't generate an error
  543. * when this happens.
  544. */
  545. if (dir_in && (cc == TD_DATAUNDERRUN))
  546. cc = TD_CC_NOERROR;
  547. if (cc == TD_NOTACCESSED)
  548. bytes_xfrd = 0;
  549. debug_isoc_completed(imx21,
  550. imx21_hc_get_frame(hcd), td, cc, bytes_xfrd);
  551. if (cc) {
  552. urb_priv->isoc_status = -EXDEV;
  553. dev_dbg(imx21->dev,
  554. "bad iso cc=0x%X frame=%d sched frame=%d "
  555. "cnt=%d len=%d urb=%p etd=%d index=%d\n",
  556. cc, imx21_hc_get_frame(hcd), td->frame,
  557. bytes_xfrd, td->len, urb, etd_num, isoc_index);
  558. }
  559. if (dir_in) {
  560. clear_toggle_bit(imx21, USBH_XFILLSTAT, etd_mask);
  561. if (!etd->dma_handle)
  562. memcpy_fromio(etd->cpu_buffer,
  563. imx21->regs + USBOTG_DMEM + etd->dmem_offset,
  564. bytes_xfrd);
  565. }
  566. urb->actual_length += bytes_xfrd;
  567. urb->iso_frame_desc[isoc_index].actual_length = bytes_xfrd;
  568. urb->iso_frame_desc[isoc_index].status = cc_to_error[cc];
  569. etd->td = NULL;
  570. etd->urb = NULL;
  571. etd->ep = NULL;
  572. if (--urb_priv->isoc_remaining == 0)
  573. urb_done(hcd, urb, urb_priv->isoc_status);
  574. schedule_isoc_etds(hcd, ep);
  575. }
  576. static struct ep_priv *alloc_isoc_ep(
  577. struct imx21 *imx21, struct usb_host_endpoint *ep)
  578. {
  579. struct ep_priv *ep_priv;
  580. int i;
  581. ep_priv = kzalloc(sizeof(struct ep_priv), GFP_ATOMIC);
  582. if (!ep_priv)
  583. return NULL;
  584. for (i = 0; i < NUM_ISO_ETDS; i++)
  585. ep_priv->etd[i] = -1;
  586. INIT_LIST_HEAD(&ep_priv->td_list);
  587. ep_priv->ep = ep;
  588. ep->hcpriv = ep_priv;
  589. return ep_priv;
  590. }
  591. static int alloc_isoc_etds(struct imx21 *imx21, struct ep_priv *ep_priv)
  592. {
  593. int i, j;
  594. int etd_num;
  595. /* Allocate the ETDs if required */
  596. for (i = 0; i < NUM_ISO_ETDS; i++) {
  597. if (ep_priv->etd[i] < 0) {
  598. etd_num = alloc_etd(imx21);
  599. if (etd_num < 0)
  600. goto alloc_etd_failed;
  601. ep_priv->etd[i] = etd_num;
  602. imx21->etd[etd_num].ep = ep_priv->ep;
  603. }
  604. }
  605. return 0;
  606. alloc_etd_failed:
  607. dev_err(imx21->dev, "isoc: Couldn't allocate etd\n");
  608. for (j = 0; j < i; j++) {
  609. free_etd(imx21, ep_priv->etd[j]);
  610. ep_priv->etd[j] = -1;
  611. }
  612. return -ENOMEM;
  613. }
  614. static int imx21_hc_urb_enqueue_isoc(struct usb_hcd *hcd,
  615. struct usb_host_endpoint *ep,
  616. struct urb *urb, gfp_t mem_flags)
  617. {
  618. struct imx21 *imx21 = hcd_to_imx21(hcd);
  619. struct urb_priv *urb_priv;
  620. unsigned long flags;
  621. struct ep_priv *ep_priv;
  622. struct td *td = NULL;
  623. int i;
  624. int ret;
  625. int cur_frame;
  626. u16 maxpacket;
  627. urb_priv = kzalloc(sizeof(struct urb_priv), mem_flags);
  628. if (urb_priv == NULL)
  629. return -ENOMEM;
  630. urb_priv->isoc_td = kzalloc(
  631. sizeof(struct td) * urb->number_of_packets, mem_flags);
  632. if (urb_priv->isoc_td == NULL) {
  633. ret = -ENOMEM;
  634. goto alloc_td_failed;
  635. }
  636. spin_lock_irqsave(&imx21->lock, flags);
  637. if (ep->hcpriv == NULL) {
  638. ep_priv = alloc_isoc_ep(imx21, ep);
  639. if (ep_priv == NULL) {
  640. ret = -ENOMEM;
  641. goto alloc_ep_failed;
  642. }
  643. } else {
  644. ep_priv = ep->hcpriv;
  645. }
  646. ret = alloc_isoc_etds(imx21, ep_priv);
  647. if (ret)
  648. goto alloc_etd_failed;
  649. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  650. if (ret)
  651. goto link_failed;
  652. urb->status = -EINPROGRESS;
  653. urb->actual_length = 0;
  654. urb->error_count = 0;
  655. urb->hcpriv = urb_priv;
  656. urb_priv->ep = ep;
  657. /* allocate data memory for largest packets if not already done */
  658. maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
  659. for (i = 0; i < NUM_ISO_ETDS; i++) {
  660. struct etd_priv *etd = &imx21->etd[ep_priv->etd[i]];
  661. if (etd->dmem_size > 0 && etd->dmem_size < maxpacket) {
  662. /* not sure if this can really occur.... */
  663. dev_err(imx21->dev, "increasing isoc buffer %d->%d\n",
  664. etd->dmem_size, maxpacket);
  665. ret = -EMSGSIZE;
  666. goto alloc_dmem_failed;
  667. }
  668. if (etd->dmem_size == 0) {
  669. etd->dmem_offset = alloc_dmem(imx21, maxpacket, ep);
  670. if (etd->dmem_offset < 0) {
  671. dev_dbg(imx21->dev, "failed alloc isoc dmem\n");
  672. ret = -EAGAIN;
  673. goto alloc_dmem_failed;
  674. }
  675. etd->dmem_size = maxpacket;
  676. }
  677. }
  678. /* calculate frame */
  679. cur_frame = imx21_hc_get_frame(hcd);
  680. if (urb->transfer_flags & URB_ISO_ASAP) {
  681. if (list_empty(&ep_priv->td_list))
  682. urb->start_frame = cur_frame + 5;
  683. else
  684. urb->start_frame = list_entry(
  685. ep_priv->td_list.prev,
  686. struct td, list)->frame + urb->interval;
  687. }
  688. urb->start_frame = wrap_frame(urb->start_frame);
  689. if (frame_after(cur_frame, urb->start_frame)) {
  690. dev_dbg(imx21->dev,
  691. "enqueue: adjusting iso start %d (cur=%d) asap=%d\n",
  692. urb->start_frame, cur_frame,
  693. (urb->transfer_flags & URB_ISO_ASAP) != 0);
  694. urb->start_frame = wrap_frame(cur_frame + 1);
  695. }
  696. /* set up transfers */
  697. td = urb_priv->isoc_td;
  698. for (i = 0; i < urb->number_of_packets; i++, td++) {
  699. unsigned int offset = urb->iso_frame_desc[i].offset;
  700. td->ep = ep;
  701. td->urb = urb;
  702. td->len = urb->iso_frame_desc[i].length;
  703. td->isoc_index = i;
  704. td->frame = wrap_frame(urb->start_frame + urb->interval * i);
  705. td->dma_handle = urb->transfer_dma + offset;
  706. td->cpu_buffer = urb->transfer_buffer + offset;
  707. list_add_tail(&td->list, &ep_priv->td_list);
  708. }
  709. urb_priv->isoc_remaining = urb->number_of_packets;
  710. dev_vdbg(imx21->dev, "setup %d packets for iso frame %d->%d\n",
  711. urb->number_of_packets, urb->start_frame, td->frame);
  712. debug_urb_submitted(imx21, urb);
  713. schedule_isoc_etds(hcd, ep);
  714. spin_unlock_irqrestore(&imx21->lock, flags);
  715. return 0;
  716. alloc_dmem_failed:
  717. usb_hcd_unlink_urb_from_ep(hcd, urb);
  718. link_failed:
  719. alloc_etd_failed:
  720. alloc_ep_failed:
  721. spin_unlock_irqrestore(&imx21->lock, flags);
  722. kfree(urb_priv->isoc_td);
  723. alloc_td_failed:
  724. kfree(urb_priv);
  725. return ret;
  726. }
  727. static void dequeue_isoc_urb(struct imx21 *imx21,
  728. struct urb *urb, struct ep_priv *ep_priv)
  729. {
  730. struct urb_priv *urb_priv = urb->hcpriv;
  731. struct td *td, *tmp;
  732. int i;
  733. if (urb_priv->active) {
  734. for (i = 0; i < NUM_ISO_ETDS; i++) {
  735. int etd_num = ep_priv->etd[i];
  736. if (etd_num != -1 && imx21->etd[etd_num].urb == urb) {
  737. struct etd_priv *etd = imx21->etd + etd_num;
  738. reset_etd(imx21, etd_num);
  739. free_dmem(imx21, etd);
  740. }
  741. }
  742. }
  743. list_for_each_entry_safe(td, tmp, &ep_priv->td_list, list) {
  744. if (td->urb == urb) {
  745. dev_vdbg(imx21->dev, "removing td %p\n", td);
  746. list_del(&td->list);
  747. }
  748. }
  749. }
  750. /* =========================================== */
  751. /* NON ISOC Handling ... */
  752. /* =========================================== */
  753. static void schedule_nonisoc_etd(struct imx21 *imx21, struct urb *urb)
  754. {
  755. unsigned int pipe = urb->pipe;
  756. struct urb_priv *urb_priv = urb->hcpriv;
  757. struct ep_priv *ep_priv = urb_priv->ep->hcpriv;
  758. int state = urb_priv->state;
  759. int etd_num = ep_priv->etd[0];
  760. struct etd_priv *etd;
  761. u32 count;
  762. u16 etd_buf_size;
  763. u16 maxpacket;
  764. u8 dir;
  765. u8 bufround;
  766. u8 datatoggle;
  767. u8 interval = 0;
  768. u8 relpolpos = 0;
  769. if (etd_num < 0) {
  770. dev_err(imx21->dev, "No valid ETD\n");
  771. return;
  772. }
  773. if (readl(imx21->regs + USBH_ETDENSET) & (1 << etd_num))
  774. dev_err(imx21->dev, "submitting to active ETD %d\n", etd_num);
  775. etd = &imx21->etd[etd_num];
  776. maxpacket = usb_maxpacket(urb->dev, pipe, usb_pipeout(pipe));
  777. if (!maxpacket)
  778. maxpacket = 8;
  779. if (usb_pipecontrol(pipe) && (state != US_CTRL_DATA)) {
  780. if (state == US_CTRL_SETUP) {
  781. dir = TD_DIR_SETUP;
  782. if (unsuitable_for_dma(urb->setup_dma))
  783. usb_hcd_unmap_urb_setup_for_dma(imx21->hcd,
  784. urb);
  785. etd->dma_handle = urb->setup_dma;
  786. etd->cpu_buffer = urb->setup_packet;
  787. bufround = 0;
  788. count = 8;
  789. datatoggle = TD_TOGGLE_DATA0;
  790. } else { /* US_CTRL_ACK */
  791. dir = usb_pipeout(pipe) ? TD_DIR_IN : TD_DIR_OUT;
  792. bufround = 0;
  793. count = 0;
  794. datatoggle = TD_TOGGLE_DATA1;
  795. }
  796. } else {
  797. dir = usb_pipeout(pipe) ? TD_DIR_OUT : TD_DIR_IN;
  798. bufround = (dir == TD_DIR_IN) ? 1 : 0;
  799. if (unsuitable_for_dma(urb->transfer_dma))
  800. usb_hcd_unmap_urb_for_dma(imx21->hcd, urb);
  801. etd->dma_handle = urb->transfer_dma;
  802. etd->cpu_buffer = urb->transfer_buffer;
  803. if (usb_pipebulk(pipe) && (state == US_BULK0))
  804. count = 0;
  805. else
  806. count = urb->transfer_buffer_length;
  807. if (usb_pipecontrol(pipe)) {
  808. datatoggle = TD_TOGGLE_DATA1;
  809. } else {
  810. if (usb_gettoggle(
  811. urb->dev,
  812. usb_pipeendpoint(urb->pipe),
  813. usb_pipeout(urb->pipe)))
  814. datatoggle = TD_TOGGLE_DATA1;
  815. else
  816. datatoggle = TD_TOGGLE_DATA0;
  817. }
  818. }
  819. etd->urb = urb;
  820. etd->ep = urb_priv->ep;
  821. etd->len = count;
  822. if (usb_pipeint(pipe)) {
  823. interval = urb->interval;
  824. relpolpos = (readl(imx21->regs + USBH_FRMNUB) + 1) & 0xff;
  825. }
  826. /* Write ETD to device memory */
  827. setup_etd_dword0(imx21, etd_num, urb, dir, maxpacket);
  828. etd_writel(imx21, etd_num, 2,
  829. (u32) interval << DW2_POLINTERV |
  830. ((u32) relpolpos << DW2_RELPOLPOS) |
  831. ((u32) dir << DW2_DIRPID) |
  832. ((u32) bufround << DW2_BUFROUND) |
  833. ((u32) datatoggle << DW2_DATATOG) |
  834. ((u32) TD_NOTACCESSED << DW2_COMPCODE));
  835. /* DMA will always transfer buffer size even if TOBYCNT in DWORD3
  836. is smaller. Make sure we don't overrun the buffer!
  837. */
  838. if (count && count < maxpacket)
  839. etd_buf_size = count;
  840. else
  841. etd_buf_size = maxpacket;
  842. etd_writel(imx21, etd_num, 3,
  843. ((u32) (etd_buf_size - 1) << DW3_BUFSIZE) | (u32) count);
  844. if (!count)
  845. etd->dma_handle = 0;
  846. /* allocate x and y buffer space at once */
  847. etd->dmem_size = (count > maxpacket) ? maxpacket * 2 : maxpacket;
  848. etd->dmem_offset = alloc_dmem(imx21, etd->dmem_size, urb_priv->ep);
  849. if (etd->dmem_offset < 0) {
  850. /* Setup everything we can in HW and update when we get DMEM */
  851. etd_writel(imx21, etd_num, 1, (u32)maxpacket << 16);
  852. dev_dbg(imx21->dev, "Queuing etd %d for DMEM\n", etd_num);
  853. debug_urb_queued_for_dmem(imx21, urb);
  854. list_add_tail(&etd->queue, &imx21->queue_for_dmem);
  855. return;
  856. }
  857. etd_writel(imx21, etd_num, 1,
  858. (((u32) etd->dmem_offset + (u32) maxpacket) << DW1_YBUFSRTAD) |
  859. (u32) etd->dmem_offset);
  860. urb_priv->active = 1;
  861. /* enable the ETD to kick off transfer */
  862. dev_vdbg(imx21->dev, "Activating etd %d for %d bytes %s\n",
  863. etd_num, count, dir != TD_DIR_IN ? "out" : "in");
  864. activate_etd(imx21, etd_num, dir);
  865. }
  866. static void nonisoc_etd_done(struct usb_hcd *hcd, int etd_num)
  867. {
  868. struct imx21 *imx21 = hcd_to_imx21(hcd);
  869. struct etd_priv *etd = &imx21->etd[etd_num];
  870. struct urb *urb = etd->urb;
  871. u32 etd_mask = 1 << etd_num;
  872. struct urb_priv *urb_priv = urb->hcpriv;
  873. int dir;
  874. int cc;
  875. u32 bytes_xfrd;
  876. int etd_done;
  877. disactivate_etd(imx21, etd_num);
  878. dir = (etd_readl(imx21, etd_num, 0) >> DW0_DIRECT) & 0x3;
  879. cc = (etd_readl(imx21, etd_num, 2) >> DW2_COMPCODE) & 0xf;
  880. bytes_xfrd = etd->len - (etd_readl(imx21, etd_num, 3) & 0x1fffff);
  881. /* save toggle carry */
  882. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  883. usb_pipeout(urb->pipe),
  884. (etd_readl(imx21, etd_num, 0) >> DW0_TOGCRY) & 0x1);
  885. if (dir == TD_DIR_IN) {
  886. clear_toggle_bit(imx21, USBH_XFILLSTAT, etd_mask);
  887. clear_toggle_bit(imx21, USBH_YFILLSTAT, etd_mask);
  888. if (etd->bounce_buffer) {
  889. memcpy(etd->cpu_buffer, etd->bounce_buffer, bytes_xfrd);
  890. dma_unmap_single(imx21->dev,
  891. etd->dma_handle, etd->len, DMA_FROM_DEVICE);
  892. } else if (!etd->dma_handle && bytes_xfrd) {/* PIO */
  893. memcpy_fromio(etd->cpu_buffer,
  894. imx21->regs + USBOTG_DMEM + etd->dmem_offset,
  895. bytes_xfrd);
  896. }
  897. }
  898. kfree(etd->bounce_buffer);
  899. etd->bounce_buffer = NULL;
  900. free_dmem(imx21, etd);
  901. urb->error_count = 0;
  902. if (!(urb->transfer_flags & URB_SHORT_NOT_OK)
  903. && (cc == TD_DATAUNDERRUN))
  904. cc = TD_CC_NOERROR;
  905. if (cc != 0)
  906. dev_vdbg(imx21->dev, "cc is 0x%x\n", cc);
  907. etd_done = (cc_to_error[cc] != 0); /* stop if error */
  908. switch (usb_pipetype(urb->pipe)) {
  909. case PIPE_CONTROL:
  910. switch (urb_priv->state) {
  911. case US_CTRL_SETUP:
  912. if (urb->transfer_buffer_length > 0)
  913. urb_priv->state = US_CTRL_DATA;
  914. else
  915. urb_priv->state = US_CTRL_ACK;
  916. break;
  917. case US_CTRL_DATA:
  918. urb->actual_length += bytes_xfrd;
  919. urb_priv->state = US_CTRL_ACK;
  920. break;
  921. case US_CTRL_ACK:
  922. etd_done = 1;
  923. break;
  924. default:
  925. dev_err(imx21->dev,
  926. "Invalid pipe state %d\n", urb_priv->state);
  927. etd_done = 1;
  928. break;
  929. }
  930. break;
  931. case PIPE_BULK:
  932. urb->actual_length += bytes_xfrd;
  933. if ((urb_priv->state == US_BULK)
  934. && (urb->transfer_flags & URB_ZERO_PACKET)
  935. && urb->transfer_buffer_length > 0
  936. && ((urb->transfer_buffer_length %
  937. usb_maxpacket(urb->dev, urb->pipe,
  938. usb_pipeout(urb->pipe))) == 0)) {
  939. /* need a 0-packet */
  940. urb_priv->state = US_BULK0;
  941. } else {
  942. etd_done = 1;
  943. }
  944. break;
  945. case PIPE_INTERRUPT:
  946. urb->actual_length += bytes_xfrd;
  947. etd_done = 1;
  948. break;
  949. }
  950. if (etd_done)
  951. nonisoc_urb_completed_for_etd(imx21, etd, cc_to_error[cc]);
  952. else {
  953. dev_vdbg(imx21->dev, "next state=%d\n", urb_priv->state);
  954. schedule_nonisoc_etd(imx21, urb);
  955. }
  956. }
  957. static struct ep_priv *alloc_ep(void)
  958. {
  959. int i;
  960. struct ep_priv *ep_priv;
  961. ep_priv = kzalloc(sizeof(struct ep_priv), GFP_ATOMIC);
  962. if (!ep_priv)
  963. return NULL;
  964. for (i = 0; i < NUM_ISO_ETDS; ++i)
  965. ep_priv->etd[i] = -1;
  966. return ep_priv;
  967. }
  968. static int imx21_hc_urb_enqueue(struct usb_hcd *hcd,
  969. struct urb *urb, gfp_t mem_flags)
  970. {
  971. struct imx21 *imx21 = hcd_to_imx21(hcd);
  972. struct usb_host_endpoint *ep = urb->ep;
  973. struct urb_priv *urb_priv;
  974. struct ep_priv *ep_priv;
  975. struct etd_priv *etd;
  976. int ret;
  977. unsigned long flags;
  978. dev_vdbg(imx21->dev,
  979. "enqueue urb=%p ep=%p len=%d "
  980. "buffer=%p dma=%08X setupBuf=%p setupDma=%08X\n",
  981. urb, ep,
  982. urb->transfer_buffer_length,
  983. urb->transfer_buffer, urb->transfer_dma,
  984. urb->setup_packet, urb->setup_dma);
  985. if (usb_pipeisoc(urb->pipe))
  986. return imx21_hc_urb_enqueue_isoc(hcd, ep, urb, mem_flags);
  987. urb_priv = kzalloc(sizeof(struct urb_priv), mem_flags);
  988. if (!urb_priv)
  989. return -ENOMEM;
  990. spin_lock_irqsave(&imx21->lock, flags);
  991. ep_priv = ep->hcpriv;
  992. if (ep_priv == NULL) {
  993. ep_priv = alloc_ep();
  994. if (!ep_priv) {
  995. ret = -ENOMEM;
  996. goto failed_alloc_ep;
  997. }
  998. ep->hcpriv = ep_priv;
  999. ep_priv->ep = ep;
  1000. }
  1001. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1002. if (ret)
  1003. goto failed_link;
  1004. urb->status = -EINPROGRESS;
  1005. urb->actual_length = 0;
  1006. urb->error_count = 0;
  1007. urb->hcpriv = urb_priv;
  1008. urb_priv->ep = ep;
  1009. switch (usb_pipetype(urb->pipe)) {
  1010. case PIPE_CONTROL:
  1011. urb_priv->state = US_CTRL_SETUP;
  1012. break;
  1013. case PIPE_BULK:
  1014. urb_priv->state = US_BULK;
  1015. break;
  1016. }
  1017. debug_urb_submitted(imx21, urb);
  1018. if (ep_priv->etd[0] < 0) {
  1019. if (ep_priv->waiting_etd) {
  1020. dev_dbg(imx21->dev,
  1021. "no ETD available already queued %p\n",
  1022. ep_priv);
  1023. debug_urb_queued_for_etd(imx21, urb);
  1024. goto out;
  1025. }
  1026. ep_priv->etd[0] = alloc_etd(imx21);
  1027. if (ep_priv->etd[0] < 0) {
  1028. dev_dbg(imx21->dev,
  1029. "no ETD available queueing %p\n", ep_priv);
  1030. debug_urb_queued_for_etd(imx21, urb);
  1031. list_add_tail(&ep_priv->queue, &imx21->queue_for_etd);
  1032. ep_priv->waiting_etd = 1;
  1033. goto out;
  1034. }
  1035. }
  1036. /* Schedule if no URB already active for this endpoint */
  1037. etd = &imx21->etd[ep_priv->etd[0]];
  1038. if (etd->urb == NULL) {
  1039. DEBUG_LOG_FRAME(imx21, etd, last_req);
  1040. schedule_nonisoc_etd(imx21, urb);
  1041. }
  1042. out:
  1043. spin_unlock_irqrestore(&imx21->lock, flags);
  1044. return 0;
  1045. failed_link:
  1046. failed_alloc_ep:
  1047. spin_unlock_irqrestore(&imx21->lock, flags);
  1048. kfree(urb_priv);
  1049. return ret;
  1050. }
  1051. static int imx21_hc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1052. int status)
  1053. {
  1054. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1055. unsigned long flags;
  1056. struct usb_host_endpoint *ep;
  1057. struct ep_priv *ep_priv;
  1058. struct urb_priv *urb_priv = urb->hcpriv;
  1059. int ret = -EINVAL;
  1060. dev_vdbg(imx21->dev, "dequeue urb=%p iso=%d status=%d\n",
  1061. urb, usb_pipeisoc(urb->pipe), status);
  1062. spin_lock_irqsave(&imx21->lock, flags);
  1063. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1064. if (ret)
  1065. goto fail;
  1066. ep = urb_priv->ep;
  1067. ep_priv = ep->hcpriv;
  1068. debug_urb_unlinked(imx21, urb);
  1069. if (usb_pipeisoc(urb->pipe)) {
  1070. dequeue_isoc_urb(imx21, urb, ep_priv);
  1071. schedule_isoc_etds(hcd, ep);
  1072. } else if (urb_priv->active) {
  1073. int etd_num = ep_priv->etd[0];
  1074. if (etd_num != -1) {
  1075. struct etd_priv *etd = &imx21->etd[etd_num];
  1076. disactivate_etd(imx21, etd_num);
  1077. free_dmem(imx21, etd);
  1078. etd->urb = NULL;
  1079. kfree(etd->bounce_buffer);
  1080. etd->bounce_buffer = NULL;
  1081. }
  1082. }
  1083. urb_done(hcd, urb, status);
  1084. spin_unlock_irqrestore(&imx21->lock, flags);
  1085. return 0;
  1086. fail:
  1087. spin_unlock_irqrestore(&imx21->lock, flags);
  1088. return ret;
  1089. }
  1090. /* =========================================== */
  1091. /* Interrupt dispatch */
  1092. /* =========================================== */
  1093. static void process_etds(struct usb_hcd *hcd, struct imx21 *imx21, int sof)
  1094. {
  1095. int etd_num;
  1096. int enable_sof_int = 0;
  1097. unsigned long flags;
  1098. spin_lock_irqsave(&imx21->lock, flags);
  1099. for (etd_num = 0; etd_num < USB_NUM_ETD; etd_num++) {
  1100. u32 etd_mask = 1 << etd_num;
  1101. u32 enabled = readl(imx21->regs + USBH_ETDENSET) & etd_mask;
  1102. u32 done = readl(imx21->regs + USBH_ETDDONESTAT) & etd_mask;
  1103. struct etd_priv *etd = &imx21->etd[etd_num];
  1104. if (done) {
  1105. DEBUG_LOG_FRAME(imx21, etd, last_int);
  1106. } else {
  1107. /*
  1108. * Kludge warning!
  1109. *
  1110. * When multiple transfers are using the bus we sometimes get into a state
  1111. * where the transfer has completed (the CC field of the ETD is != 0x0F),
  1112. * the ETD has self disabled but the ETDDONESTAT flag is not set
  1113. * (and hence no interrupt occurs).
  1114. * This causes the transfer in question to hang.
  1115. * The kludge below checks for this condition at each SOF and processes any
  1116. * blocked ETDs (after an arbitrary 10 frame wait)
  1117. *
  1118. * With a single active transfer the usbtest test suite will run for days
  1119. * without the kludge.
  1120. * With other bus activity (eg mass storage) even just test1 will hang without
  1121. * the kludge.
  1122. */
  1123. u32 dword0;
  1124. int cc;
  1125. if (etd->active_count && !enabled) /* suspicious... */
  1126. enable_sof_int = 1;
  1127. if (!sof || enabled || !etd->active_count)
  1128. continue;
  1129. cc = etd_readl(imx21, etd_num, 2) >> DW2_COMPCODE;
  1130. if (cc == TD_NOTACCESSED)
  1131. continue;
  1132. if (++etd->active_count < 10)
  1133. continue;
  1134. dword0 = etd_readl(imx21, etd_num, 0);
  1135. dev_dbg(imx21->dev,
  1136. "unblock ETD %d dev=0x%X ep=0x%X cc=0x%02X!\n",
  1137. etd_num, dword0 & 0x7F,
  1138. (dword0 >> DW0_ENDPNT) & 0x0F,
  1139. cc);
  1140. #ifdef DEBUG
  1141. dev_dbg(imx21->dev,
  1142. "frame: act=%d disact=%d"
  1143. " int=%d req=%d cur=%d\n",
  1144. etd->activated_frame,
  1145. etd->disactivated_frame,
  1146. etd->last_int_frame,
  1147. etd->last_req_frame,
  1148. readl(imx21->regs + USBH_FRMNUB));
  1149. imx21->debug_unblocks++;
  1150. #endif
  1151. etd->active_count = 0;
  1152. /* End of kludge */
  1153. }
  1154. if (etd->ep == NULL || etd->urb == NULL) {
  1155. dev_dbg(imx21->dev,
  1156. "Interrupt for unexpected etd %d"
  1157. " ep=%p urb=%p\n",
  1158. etd_num, etd->ep, etd->urb);
  1159. disactivate_etd(imx21, etd_num);
  1160. continue;
  1161. }
  1162. if (usb_pipeisoc(etd->urb->pipe))
  1163. isoc_etd_done(hcd, etd_num);
  1164. else
  1165. nonisoc_etd_done(hcd, etd_num);
  1166. }
  1167. /* only enable SOF interrupt if it may be needed for the kludge */
  1168. if (enable_sof_int)
  1169. set_register_bits(imx21, USBH_SYSIEN, USBH_SYSIEN_SOFINT);
  1170. else
  1171. clear_register_bits(imx21, USBH_SYSIEN, USBH_SYSIEN_SOFINT);
  1172. spin_unlock_irqrestore(&imx21->lock, flags);
  1173. }
  1174. static irqreturn_t imx21_irq(struct usb_hcd *hcd)
  1175. {
  1176. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1177. u32 ints = readl(imx21->regs + USBH_SYSISR);
  1178. if (ints & USBH_SYSIEN_HERRINT)
  1179. dev_dbg(imx21->dev, "Scheduling error\n");
  1180. if (ints & USBH_SYSIEN_SORINT)
  1181. dev_dbg(imx21->dev, "Scheduling overrun\n");
  1182. if (ints & (USBH_SYSISR_DONEINT | USBH_SYSISR_SOFINT))
  1183. process_etds(hcd, imx21, ints & USBH_SYSISR_SOFINT);
  1184. writel(ints, imx21->regs + USBH_SYSISR);
  1185. return IRQ_HANDLED;
  1186. }
  1187. static void imx21_hc_endpoint_disable(struct usb_hcd *hcd,
  1188. struct usb_host_endpoint *ep)
  1189. {
  1190. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1191. unsigned long flags;
  1192. struct ep_priv *ep_priv;
  1193. int i;
  1194. if (ep == NULL)
  1195. return;
  1196. spin_lock_irqsave(&imx21->lock, flags);
  1197. ep_priv = ep->hcpriv;
  1198. dev_vdbg(imx21->dev, "disable ep=%p, ep->hcpriv=%p\n", ep, ep_priv);
  1199. if (!list_empty(&ep->urb_list))
  1200. dev_dbg(imx21->dev, "ep's URB list is not empty\n");
  1201. if (ep_priv != NULL) {
  1202. for (i = 0; i < NUM_ISO_ETDS; i++) {
  1203. if (ep_priv->etd[i] > -1)
  1204. dev_dbg(imx21->dev, "free etd %d for disable\n",
  1205. ep_priv->etd[i]);
  1206. free_etd(imx21, ep_priv->etd[i]);
  1207. }
  1208. kfree(ep_priv);
  1209. ep->hcpriv = NULL;
  1210. }
  1211. for (i = 0; i < USB_NUM_ETD; i++) {
  1212. if (imx21->etd[i].alloc && imx21->etd[i].ep == ep) {
  1213. dev_err(imx21->dev,
  1214. "Active etd %d for disabled ep=%p!\n", i, ep);
  1215. free_etd(imx21, i);
  1216. }
  1217. }
  1218. free_epdmem(imx21, ep);
  1219. spin_unlock_irqrestore(&imx21->lock, flags);
  1220. }
  1221. /* =========================================== */
  1222. /* Hub handling */
  1223. /* =========================================== */
  1224. static int get_hub_descriptor(struct usb_hcd *hcd,
  1225. struct usb_hub_descriptor *desc)
  1226. {
  1227. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1228. desc->bDescriptorType = 0x29; /* HUB descriptor */
  1229. desc->bHubContrCurrent = 0;
  1230. desc->bNbrPorts = readl(imx21->regs + USBH_ROOTHUBA)
  1231. & USBH_ROOTHUBA_NDNSTMPRT_MASK;
  1232. desc->bDescLength = 9;
  1233. desc->bPwrOn2PwrGood = 0;
  1234. desc->wHubCharacteristics = (__force __u16) cpu_to_le16(
  1235. 0x0002 | /* No power switching */
  1236. 0x0010 | /* No over current protection */
  1237. 0);
  1238. desc->u.hs.DeviceRemovable[0] = 1 << 1;
  1239. desc->u.hs.DeviceRemovable[1] = ~0;
  1240. return 0;
  1241. }
  1242. static int imx21_hc_hub_status_data(struct usb_hcd *hcd, char *buf)
  1243. {
  1244. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1245. int ports;
  1246. int changed = 0;
  1247. int i;
  1248. unsigned long flags;
  1249. spin_lock_irqsave(&imx21->lock, flags);
  1250. ports = readl(imx21->regs + USBH_ROOTHUBA)
  1251. & USBH_ROOTHUBA_NDNSTMPRT_MASK;
  1252. if (ports > 7) {
  1253. ports = 7;
  1254. dev_err(imx21->dev, "ports %d > 7\n", ports);
  1255. }
  1256. for (i = 0; i < ports; i++) {
  1257. if (readl(imx21->regs + USBH_PORTSTAT(i)) &
  1258. (USBH_PORTSTAT_CONNECTSC |
  1259. USBH_PORTSTAT_PRTENBLSC |
  1260. USBH_PORTSTAT_PRTSTATSC |
  1261. USBH_PORTSTAT_OVRCURIC |
  1262. USBH_PORTSTAT_PRTRSTSC)) {
  1263. changed = 1;
  1264. buf[0] |= 1 << (i + 1);
  1265. }
  1266. }
  1267. spin_unlock_irqrestore(&imx21->lock, flags);
  1268. if (changed)
  1269. dev_info(imx21->dev, "Hub status changed\n");
  1270. return changed;
  1271. }
  1272. static int imx21_hc_hub_control(struct usb_hcd *hcd,
  1273. u16 typeReq,
  1274. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  1275. {
  1276. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1277. int rc = 0;
  1278. u32 status_write = 0;
  1279. switch (typeReq) {
  1280. case ClearHubFeature:
  1281. dev_dbg(imx21->dev, "ClearHubFeature\n");
  1282. switch (wValue) {
  1283. case C_HUB_OVER_CURRENT:
  1284. dev_dbg(imx21->dev, " OVER_CURRENT\n");
  1285. break;
  1286. case C_HUB_LOCAL_POWER:
  1287. dev_dbg(imx21->dev, " LOCAL_POWER\n");
  1288. break;
  1289. default:
  1290. dev_dbg(imx21->dev, " unknown\n");
  1291. rc = -EINVAL;
  1292. break;
  1293. }
  1294. break;
  1295. case ClearPortFeature:
  1296. dev_dbg(imx21->dev, "ClearPortFeature\n");
  1297. switch (wValue) {
  1298. case USB_PORT_FEAT_ENABLE:
  1299. dev_dbg(imx21->dev, " ENABLE\n");
  1300. status_write = USBH_PORTSTAT_CURCONST;
  1301. break;
  1302. case USB_PORT_FEAT_SUSPEND:
  1303. dev_dbg(imx21->dev, " SUSPEND\n");
  1304. status_write = USBH_PORTSTAT_PRTOVRCURI;
  1305. break;
  1306. case USB_PORT_FEAT_POWER:
  1307. dev_dbg(imx21->dev, " POWER\n");
  1308. status_write = USBH_PORTSTAT_LSDEVCON;
  1309. break;
  1310. case USB_PORT_FEAT_C_ENABLE:
  1311. dev_dbg(imx21->dev, " C_ENABLE\n");
  1312. status_write = USBH_PORTSTAT_PRTENBLSC;
  1313. break;
  1314. case USB_PORT_FEAT_C_SUSPEND:
  1315. dev_dbg(imx21->dev, " C_SUSPEND\n");
  1316. status_write = USBH_PORTSTAT_PRTSTATSC;
  1317. break;
  1318. case USB_PORT_FEAT_C_CONNECTION:
  1319. dev_dbg(imx21->dev, " C_CONNECTION\n");
  1320. status_write = USBH_PORTSTAT_CONNECTSC;
  1321. break;
  1322. case USB_PORT_FEAT_C_OVER_CURRENT:
  1323. dev_dbg(imx21->dev, " C_OVER_CURRENT\n");
  1324. status_write = USBH_PORTSTAT_OVRCURIC;
  1325. break;
  1326. case USB_PORT_FEAT_C_RESET:
  1327. dev_dbg(imx21->dev, " C_RESET\n");
  1328. status_write = USBH_PORTSTAT_PRTRSTSC;
  1329. break;
  1330. default:
  1331. dev_dbg(imx21->dev, " unknown\n");
  1332. rc = -EINVAL;
  1333. break;
  1334. }
  1335. break;
  1336. case GetHubDescriptor:
  1337. dev_dbg(imx21->dev, "GetHubDescriptor\n");
  1338. rc = get_hub_descriptor(hcd, (void *)buf);
  1339. break;
  1340. case GetHubStatus:
  1341. dev_dbg(imx21->dev, " GetHubStatus\n");
  1342. *(__le32 *) buf = 0;
  1343. break;
  1344. case GetPortStatus:
  1345. dev_dbg(imx21->dev, "GetPortStatus: port: %d, 0x%x\n",
  1346. wIndex, USBH_PORTSTAT(wIndex - 1));
  1347. *(__le32 *) buf = readl(imx21->regs +
  1348. USBH_PORTSTAT(wIndex - 1));
  1349. break;
  1350. case SetHubFeature:
  1351. dev_dbg(imx21->dev, "SetHubFeature\n");
  1352. switch (wValue) {
  1353. case C_HUB_OVER_CURRENT:
  1354. dev_dbg(imx21->dev, " OVER_CURRENT\n");
  1355. break;
  1356. case C_HUB_LOCAL_POWER:
  1357. dev_dbg(imx21->dev, " LOCAL_POWER\n");
  1358. break;
  1359. default:
  1360. dev_dbg(imx21->dev, " unknown\n");
  1361. rc = -EINVAL;
  1362. break;
  1363. }
  1364. break;
  1365. case SetPortFeature:
  1366. dev_dbg(imx21->dev, "SetPortFeature\n");
  1367. switch (wValue) {
  1368. case USB_PORT_FEAT_SUSPEND:
  1369. dev_dbg(imx21->dev, " SUSPEND\n");
  1370. status_write = USBH_PORTSTAT_PRTSUSPST;
  1371. break;
  1372. case USB_PORT_FEAT_POWER:
  1373. dev_dbg(imx21->dev, " POWER\n");
  1374. status_write = USBH_PORTSTAT_PRTPWRST;
  1375. break;
  1376. case USB_PORT_FEAT_RESET:
  1377. dev_dbg(imx21->dev, " RESET\n");
  1378. status_write = USBH_PORTSTAT_PRTRSTST;
  1379. break;
  1380. default:
  1381. dev_dbg(imx21->dev, " unknown\n");
  1382. rc = -EINVAL;
  1383. break;
  1384. }
  1385. break;
  1386. default:
  1387. dev_dbg(imx21->dev, " unknown\n");
  1388. rc = -EINVAL;
  1389. break;
  1390. }
  1391. if (status_write)
  1392. writel(status_write, imx21->regs + USBH_PORTSTAT(wIndex - 1));
  1393. return rc;
  1394. }
  1395. /* =========================================== */
  1396. /* Host controller management */
  1397. /* =========================================== */
  1398. static int imx21_hc_reset(struct usb_hcd *hcd)
  1399. {
  1400. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1401. unsigned long timeout;
  1402. unsigned long flags;
  1403. spin_lock_irqsave(&imx21->lock, flags);
  1404. /* Reset the Host controller modules */
  1405. writel(USBOTG_RST_RSTCTRL | USBOTG_RST_RSTRH |
  1406. USBOTG_RST_RSTHSIE | USBOTG_RST_RSTHC,
  1407. imx21->regs + USBOTG_RST_CTRL);
  1408. /* Wait for reset to finish */
  1409. timeout = jiffies + HZ;
  1410. while (readl(imx21->regs + USBOTG_RST_CTRL) != 0) {
  1411. if (time_after(jiffies, timeout)) {
  1412. spin_unlock_irqrestore(&imx21->lock, flags);
  1413. dev_err(imx21->dev, "timeout waiting for reset\n");
  1414. return -ETIMEDOUT;
  1415. }
  1416. spin_unlock_irq(&imx21->lock);
  1417. schedule_timeout_uninterruptible(1);
  1418. spin_lock_irq(&imx21->lock);
  1419. }
  1420. spin_unlock_irqrestore(&imx21->lock, flags);
  1421. return 0;
  1422. }
  1423. static int imx21_hc_start(struct usb_hcd *hcd)
  1424. {
  1425. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1426. unsigned long flags;
  1427. int i, j;
  1428. u32 hw_mode = USBOTG_HWMODE_CRECFG_HOST;
  1429. u32 usb_control = 0;
  1430. hw_mode |= ((imx21->pdata->host_xcvr << USBOTG_HWMODE_HOSTXCVR_SHIFT) &
  1431. USBOTG_HWMODE_HOSTXCVR_MASK);
  1432. hw_mode |= ((imx21->pdata->otg_xcvr << USBOTG_HWMODE_OTGXCVR_SHIFT) &
  1433. USBOTG_HWMODE_OTGXCVR_MASK);
  1434. if (imx21->pdata->host1_txenoe)
  1435. usb_control |= USBCTRL_HOST1_TXEN_OE;
  1436. if (!imx21->pdata->host1_xcverless)
  1437. usb_control |= USBCTRL_HOST1_BYP_TLL;
  1438. if (imx21->pdata->otg_ext_xcvr)
  1439. usb_control |= USBCTRL_OTC_RCV_RXDP;
  1440. spin_lock_irqsave(&imx21->lock, flags);
  1441. writel((USBOTG_CLK_CTRL_HST | USBOTG_CLK_CTRL_MAIN),
  1442. imx21->regs + USBOTG_CLK_CTRL);
  1443. writel(hw_mode, imx21->regs + USBOTG_HWMODE);
  1444. writel(usb_control, imx21->regs + USBCTRL);
  1445. writel(USB_MISCCONTROL_SKPRTRY | USB_MISCCONTROL_ARBMODE,
  1446. imx21->regs + USB_MISCCONTROL);
  1447. /* Clear the ETDs */
  1448. for (i = 0; i < USB_NUM_ETD; i++)
  1449. for (j = 0; j < 4; j++)
  1450. etd_writel(imx21, i, j, 0);
  1451. /* Take the HC out of reset */
  1452. writel(USBH_HOST_CTRL_HCUSBSTE_OPERATIONAL | USBH_HOST_CTRL_CTLBLKSR_1,
  1453. imx21->regs + USBH_HOST_CTRL);
  1454. /* Enable ports */
  1455. if (imx21->pdata->enable_otg_host)
  1456. writel(USBH_PORTSTAT_PRTPWRST | USBH_PORTSTAT_PRTENABST,
  1457. imx21->regs + USBH_PORTSTAT(0));
  1458. if (imx21->pdata->enable_host1)
  1459. writel(USBH_PORTSTAT_PRTPWRST | USBH_PORTSTAT_PRTENABST,
  1460. imx21->regs + USBH_PORTSTAT(1));
  1461. if (imx21->pdata->enable_host2)
  1462. writel(USBH_PORTSTAT_PRTPWRST | USBH_PORTSTAT_PRTENABST,
  1463. imx21->regs + USBH_PORTSTAT(2));
  1464. hcd->state = HC_STATE_RUNNING;
  1465. /* Enable host controller interrupts */
  1466. set_register_bits(imx21, USBH_SYSIEN,
  1467. USBH_SYSIEN_HERRINT |
  1468. USBH_SYSIEN_DONEINT | USBH_SYSIEN_SORINT);
  1469. set_register_bits(imx21, USBOTG_CINT_STEN, USBOTG_HCINT);
  1470. spin_unlock_irqrestore(&imx21->lock, flags);
  1471. return 0;
  1472. }
  1473. static void imx21_hc_stop(struct usb_hcd *hcd)
  1474. {
  1475. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1476. unsigned long flags;
  1477. spin_lock_irqsave(&imx21->lock, flags);
  1478. writel(0, imx21->regs + USBH_SYSIEN);
  1479. clear_register_bits(imx21, USBOTG_CINT_STEN, USBOTG_HCINT);
  1480. clear_register_bits(imx21, USBOTG_CLK_CTRL_HST | USBOTG_CLK_CTRL_MAIN,
  1481. USBOTG_CLK_CTRL);
  1482. spin_unlock_irqrestore(&imx21->lock, flags);
  1483. }
  1484. /* =========================================== */
  1485. /* Driver glue */
  1486. /* =========================================== */
  1487. static struct hc_driver imx21_hc_driver = {
  1488. .description = hcd_name,
  1489. .product_desc = "IMX21 USB Host Controller",
  1490. .hcd_priv_size = sizeof(struct imx21),
  1491. .flags = HCD_USB11,
  1492. .irq = imx21_irq,
  1493. .reset = imx21_hc_reset,
  1494. .start = imx21_hc_start,
  1495. .stop = imx21_hc_stop,
  1496. /* I/O requests */
  1497. .urb_enqueue = imx21_hc_urb_enqueue,
  1498. .urb_dequeue = imx21_hc_urb_dequeue,
  1499. .endpoint_disable = imx21_hc_endpoint_disable,
  1500. /* scheduling support */
  1501. .get_frame_number = imx21_hc_get_frame,
  1502. /* Root hub support */
  1503. .hub_status_data = imx21_hc_hub_status_data,
  1504. .hub_control = imx21_hc_hub_control,
  1505. };
  1506. static struct mx21_usbh_platform_data default_pdata = {
  1507. .host_xcvr = MX21_USBXCVR_TXDIF_RXDIF,
  1508. .otg_xcvr = MX21_USBXCVR_TXDIF_RXDIF,
  1509. .enable_host1 = 1,
  1510. .enable_host2 = 1,
  1511. .enable_otg_host = 1,
  1512. };
  1513. static int imx21_remove(struct platform_device *pdev)
  1514. {
  1515. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  1516. struct imx21 *imx21 = hcd_to_imx21(hcd);
  1517. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1518. remove_debug_files(imx21);
  1519. usb_remove_hcd(hcd);
  1520. if (res != NULL) {
  1521. clk_disable_unprepare(imx21->clk);
  1522. clk_put(imx21->clk);
  1523. iounmap(imx21->regs);
  1524. release_mem_region(res->start, resource_size(res));
  1525. }
  1526. kfree(hcd);
  1527. return 0;
  1528. }
  1529. static int imx21_probe(struct platform_device *pdev)
  1530. {
  1531. struct usb_hcd *hcd;
  1532. struct imx21 *imx21;
  1533. struct resource *res;
  1534. int ret;
  1535. int irq;
  1536. printk(KERN_INFO "%s\n", imx21_hc_driver.product_desc);
  1537. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1538. if (!res)
  1539. return -ENODEV;
  1540. irq = platform_get_irq(pdev, 0);
  1541. if (irq < 0)
  1542. return -ENXIO;
  1543. hcd = usb_create_hcd(&imx21_hc_driver,
  1544. &pdev->dev, dev_name(&pdev->dev));
  1545. if (hcd == NULL) {
  1546. dev_err(&pdev->dev, "Cannot create hcd (%s)\n",
  1547. dev_name(&pdev->dev));
  1548. return -ENOMEM;
  1549. }
  1550. imx21 = hcd_to_imx21(hcd);
  1551. imx21->hcd = hcd;
  1552. imx21->dev = &pdev->dev;
  1553. imx21->pdata = pdev->dev.platform_data;
  1554. if (!imx21->pdata)
  1555. imx21->pdata = &default_pdata;
  1556. spin_lock_init(&imx21->lock);
  1557. INIT_LIST_HEAD(&imx21->dmem_list);
  1558. INIT_LIST_HEAD(&imx21->queue_for_etd);
  1559. INIT_LIST_HEAD(&imx21->queue_for_dmem);
  1560. create_debug_files(imx21);
  1561. res = request_mem_region(res->start, resource_size(res), hcd_name);
  1562. if (!res) {
  1563. ret = -EBUSY;
  1564. goto failed_request_mem;
  1565. }
  1566. imx21->regs = ioremap(res->start, resource_size(res));
  1567. if (imx21->regs == NULL) {
  1568. dev_err(imx21->dev, "Cannot map registers\n");
  1569. ret = -ENOMEM;
  1570. goto failed_ioremap;
  1571. }
  1572. /* Enable clocks source */
  1573. imx21->clk = clk_get(imx21->dev, NULL);
  1574. if (IS_ERR(imx21->clk)) {
  1575. dev_err(imx21->dev, "no clock found\n");
  1576. ret = PTR_ERR(imx21->clk);
  1577. goto failed_clock_get;
  1578. }
  1579. ret = clk_set_rate(imx21->clk, clk_round_rate(imx21->clk, 48000000));
  1580. if (ret)
  1581. goto failed_clock_set;
  1582. ret = clk_prepare_enable(imx21->clk);
  1583. if (ret)
  1584. goto failed_clock_enable;
  1585. dev_info(imx21->dev, "Hardware HC revision: 0x%02X\n",
  1586. (readl(imx21->regs + USBOTG_HWMODE) >> 16) & 0xFF);
  1587. ret = usb_add_hcd(hcd, irq, 0);
  1588. if (ret != 0) {
  1589. dev_err(imx21->dev, "usb_add_hcd() returned %d\n", ret);
  1590. goto failed_add_hcd;
  1591. }
  1592. return 0;
  1593. failed_add_hcd:
  1594. clk_disable_unprepare(imx21->clk);
  1595. failed_clock_enable:
  1596. failed_clock_set:
  1597. clk_put(imx21->clk);
  1598. failed_clock_get:
  1599. iounmap(imx21->regs);
  1600. failed_ioremap:
  1601. release_mem_region(res->start, resource_size(res));
  1602. failed_request_mem:
  1603. remove_debug_files(imx21);
  1604. usb_put_hcd(hcd);
  1605. return ret;
  1606. }
  1607. static struct platform_driver imx21_hcd_driver = {
  1608. .driver = {
  1609. .name = (char *)hcd_name,
  1610. },
  1611. .probe = imx21_probe,
  1612. .remove = imx21_remove,
  1613. .suspend = NULL,
  1614. .resume = NULL,
  1615. };
  1616. module_platform_driver(imx21_hcd_driver);
  1617. MODULE_DESCRIPTION("i.MX21 USB Host controller");
  1618. MODULE_AUTHOR("Martin Fuzzey");
  1619. MODULE_LICENSE("GPL");
  1620. MODULE_ALIAS("platform:imx21-hcd");