imx21-hcd.c 48 KB

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