musb_host.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MUSB OTG driver host support
  4. *
  5. * Copyright 2005 Mentor Graphics Corporation
  6. * Copyright (C) 2005-2006 by Texas Instruments
  7. * Copyright (C) 2006-2007 Nokia Corporation
  8. * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/dma-mapping.h>
  18. #include "musb_core.h"
  19. #include "musb_host.h"
  20. #include "musb_trace.h"
  21. #define UDISK_INTERVAL 0x10
  22. /* MUSB HOST status 22-mar-2006
  23. *
  24. * - There's still lots of partial code duplication for fault paths, so
  25. * they aren't handled as consistently as they need to be.
  26. *
  27. * - PIO mostly behaved when last tested.
  28. * + including ep0, with all usbtest cases 9, 10
  29. * + usbtest 14 (ep0out) doesn't seem to run at all
  30. * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
  31. * configurations, but otherwise double buffering passes basic tests.
  32. * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
  33. *
  34. * - DMA (CPPI) ... partially behaves, not currently recommended
  35. * + about 1/15 the speed of typical EHCI implementations (PCI)
  36. * + RX, all too often reqpkt seems to misbehave after tx
  37. * + TX, no known issues (other than evident silicon issue)
  38. *
  39. * - DMA (Mentor/OMAP) ...has at least toggle update problems
  40. *
  41. * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
  42. * starvation ... nothing yet for TX, interrupt, or bulk.
  43. *
  44. * - Not tested with HNP, but some SRP paths seem to behave.
  45. *
  46. * NOTE 24-August-2006:
  47. *
  48. * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
  49. * extra endpoint for periodic use enabling hub + keybd + mouse. That
  50. * mostly works, except that with "usbnet" it's easy to trigger cases
  51. * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
  52. * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
  53. * although ARP RX wins. (That test was done with a full speed link.)
  54. */
  55. /*
  56. * NOTE on endpoint usage:
  57. *
  58. * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
  59. * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
  60. * (Yes, bulk _could_ use more of the endpoints than that, and would even
  61. * benefit from it.)
  62. *
  63. * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
  64. * So far that scheduling is both dumb and optimistic: the endpoint will be
  65. * "claimed" until its software queue is no longer refilled. No multiplexing
  66. * of transfers between endpoints, or anything clever.
  67. */
  68. struct musb *hcd_to_musb(struct usb_hcd *hcd)
  69. {
  70. return *(struct musb **) hcd->hcd_priv;
  71. }
  72. static void musb_ep_program(struct musb *musb, u8 epnum,
  73. struct urb *urb, int is_out,
  74. u8 *buf, u32 offset, u32 len);
  75. /*
  76. * Clear TX fifo. Needed to avoid BABBLE errors.
  77. */
  78. static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
  79. {
  80. struct musb *musb = ep->musb;
  81. void __iomem *epio = ep->regs;
  82. u16 csr;
  83. #if NICHOLAS_ADD
  84. int retries = 1;
  85. #else
  86. int retries = 1000;
  87. #endif
  88. csr = musb_readw(epio, MUSB_TXCSR);
  89. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  90. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
  91. musb_writew(epio, MUSB_TXCSR, csr);
  92. csr = musb_readw(epio, MUSB_TXCSR);
  93. /*
  94. * FIXME: sometimes the tx fifo flush failed, it has been
  95. * observed during device disconnect on AM335x.
  96. *
  97. * To reproduce the issue, ensure tx urb(s) are queued when
  98. * unplug the usb device which is connected to AM335x usb
  99. * host port.
  100. *
  101. * I found using a usb-ethernet device and running iperf
  102. * (client on AM335x) has very high chance to trigger it.
  103. *
  104. * Better to turn on musb_dbg() in musb_cleanup_urb() with
  105. * CPPI enabled to see the issue when aborting the tx channel.
  106. */
  107. if (dev_WARN_ONCE(musb->controller, retries-- < 1,
  108. "Could not flush host TX%d fifo: csr: %04x\n",
  109. ep->epnum, csr)){
  110. printk(KERN_ALERT "Host TX FIFONOTEMPTY csr: %02x\n", csr);
  111. return;
  112. }
  113. mdelay(1);
  114. }
  115. }
  116. static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
  117. {
  118. void __iomem *epio = ep->regs;
  119. u16 csr;
  120. int retries = 5;
  121. /* scrub any data left in the fifo */
  122. do {
  123. csr = musb_readw(epio, MUSB_TXCSR);
  124. if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
  125. break;
  126. musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
  127. csr = musb_readw(epio, MUSB_TXCSR);
  128. udelay(10);
  129. } while (--retries);
  130. WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
  131. ep->epnum, csr);
  132. /* and reset for the next transfer */
  133. musb_writew(epio, MUSB_TXCSR, 0);
  134. }
  135. /*
  136. * Start transmit. Caller is responsible for locking shared resources.
  137. * musb must be locked.
  138. */
  139. static inline void musb_h_tx_start(struct musb_hw_ep *ep)
  140. {
  141. u16 txcsr;
  142. /* NOTE: no locks here; caller should lock and select EP */
  143. if (ep->epnum) {
  144. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  145. txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
  146. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  147. } else {
  148. txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
  149. musb_writew(ep->regs, MUSB_CSR0, txcsr);
  150. }
  151. }
  152. static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
  153. {
  154. u16 txcsr;
  155. /* NOTE: no locks here; caller should lock and select EP */
  156. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  157. txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
  158. if (is_cppi_enabled(ep->musb))
  159. txcsr |= MUSB_TXCSR_DMAMODE;
  160. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  161. }
  162. static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
  163. {
  164. if (is_in != 0 || ep->is_shared_fifo)
  165. ep->in_qh = qh;
  166. if (is_in == 0 || ep->is_shared_fifo)
  167. ep->out_qh = qh;
  168. }
  169. static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
  170. {
  171. return is_in ? ep->in_qh : ep->out_qh;
  172. }
  173. /*
  174. * Start the URB at the front of an endpoint's queue
  175. * end must be claimed from the caller.
  176. *
  177. * Context: controller locked, irqs blocked
  178. */
  179. static void
  180. musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
  181. {
  182. u32 len;
  183. void __iomem *mbase = musb->mregs;
  184. struct urb *urb = next_urb(qh);
  185. void *buf = urb->transfer_buffer;
  186. u32 offset = 0;
  187. struct musb_hw_ep *hw_ep = qh->hw_ep;
  188. int epnum = hw_ep->epnum;
  189. if(epnum >= MUSB_C_NUM_EPS) //add++
  190. {
  191. printk(KERN_ERR "ERR: %s, Invalid epnum:%d, exit.\n.", __FUNCTION__, epnum);
  192. return;
  193. }
  194. /* initialize software qh state */
  195. qh->offset = 0;
  196. qh->segsize = 0;
  197. /* gather right source of data */
  198. switch (qh->type) {
  199. case USB_ENDPOINT_XFER_CONTROL:
  200. /* control transfers always start with SETUP */
  201. is_in = 0;
  202. musb->ep0_stage = MUSB_EP0_START;
  203. buf = urb->setup_packet;
  204. len = 8;
  205. break;
  206. case USB_ENDPOINT_XFER_ISOC:
  207. qh->iso_idx = 0;
  208. qh->frame = 0;
  209. offset = urb->iso_frame_desc[0].offset;
  210. len = urb->iso_frame_desc[0].length;
  211. break;
  212. default: /* bulk, interrupt */
  213. /* actual_length may be nonzero on retry paths */
  214. buf = urb->transfer_buffer + urb->actual_length;
  215. len = urb->transfer_buffer_length - urb->actual_length;
  216. }
  217. trace_musb_urb_start(musb, urb);
  218. /* Configure endpoint */
  219. musb_ep_set_qh(hw_ep, is_in, qh);
  220. musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
  221. /* transmit may have more work: start it when it is time */
  222. if (is_in)
  223. return;
  224. /* determine if the time is right for a periodic transfer */
  225. switch (qh->type) {
  226. case USB_ENDPOINT_XFER_ISOC:
  227. case USB_ENDPOINT_XFER_INT:
  228. musb_dbg(musb, "check whether there's still time for periodic Tx");
  229. /* FIXME this doesn't implement that scheduling policy ...
  230. * or handle framecounter wrapping
  231. */
  232. if (1) { /* Always assume URB_ISO_ASAP */
  233. /* REVISIT the SOF irq handler shouldn't duplicate
  234. * this code; and we don't init urb->start_frame...
  235. */
  236. qh->frame = 0;
  237. goto start;
  238. } else {
  239. qh->frame = urb->start_frame;
  240. /* enable SOF interrupt so we can count down */
  241. musb_dbg(musb, "SOF for %d", epnum);
  242. #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
  243. musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
  244. #endif
  245. }
  246. break;
  247. default:
  248. start:
  249. musb_dbg(musb, "Start TX%d %s", epnum,
  250. hw_ep->tx_channel ? "dma" : "pio");
  251. if (!hw_ep->tx_channel)
  252. musb_h_tx_start(hw_ep);
  253. else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  254. musb_h_tx_dma_start(hw_ep);
  255. }
  256. }
  257. /* Context: caller owns controller lock, IRQs are blocked */
  258. static void musb_giveback(struct musb *musb, struct urb *urb, int status)
  259. __releases(musb->lock)
  260. __acquires(musb->lock)
  261. {
  262. trace_musb_urb_gb(musb, urb);
  263. usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
  264. spin_unlock(&musb->lock);
  265. usb_hcd_giveback_urb(musb->hcd, urb, status);
  266. spin_lock(&musb->lock);
  267. }
  268. /* For bulk/interrupt endpoints only */
  269. static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
  270. struct urb *urb)
  271. {
  272. void __iomem *epio = qh->hw_ep->regs;
  273. u16 csr;
  274. /*
  275. * FIXME: the current Mentor DMA code seems to have
  276. * problems getting toggle correct.
  277. */
  278. if (is_in)
  279. csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
  280. else
  281. csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
  282. usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
  283. }
  284. /*
  285. * Advance this hardware endpoint's queue, completing the specified URB and
  286. * advancing to either the next URB queued to that qh, or else invalidating
  287. * that qh and advancing to the next qh scheduled after the current one.
  288. *
  289. * Context: caller owns controller lock, IRQs are blocked
  290. */
  291. static void musb_advance_schedule(struct musb *musb, struct urb *urb,
  292. struct musb_hw_ep *hw_ep, int is_in)
  293. {
  294. struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
  295. struct musb_hw_ep *ep = qh->hw_ep;
  296. int ready = qh->is_ready;
  297. int status;
  298. status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
  299. /* save toggle eagerly, for paranoia */
  300. switch (qh->type) {
  301. case USB_ENDPOINT_XFER_BULK:
  302. case USB_ENDPOINT_XFER_INT:
  303. musb_save_toggle(qh, is_in, urb);
  304. break;
  305. case USB_ENDPOINT_XFER_ISOC:
  306. if (status == 0 && urb->error_count)
  307. status = -EXDEV;
  308. break;
  309. }
  310. qh->is_ready = 0;
  311. musb_giveback(musb, urb, status);
  312. qh->is_ready = ready;
  313. /* reclaim resources (and bandwidth) ASAP; deschedule it, and
  314. * invalidate qh as soon as list_empty(&hep->urb_list)
  315. */
  316. if (list_empty(&qh->hep->urb_list)) {
  317. struct list_head *head;
  318. struct dma_controller *dma = musb->dma_controller;
  319. if (is_in) {
  320. ep->rx_reinit = 1;
  321. if (ep->rx_channel) {
  322. dma->channel_release(ep->rx_channel);
  323. ep->rx_channel = NULL;
  324. }
  325. } else {
  326. ep->tx_reinit = 1;
  327. if (ep->tx_channel) {
  328. dma->channel_release(ep->tx_channel);
  329. ep->tx_channel = NULL;
  330. }
  331. }
  332. /* Clobber old pointers to this qh */
  333. musb_ep_set_qh(ep, is_in, NULL);
  334. qh->hep->hcpriv = NULL;
  335. switch (qh->type) {
  336. case USB_ENDPOINT_XFER_CONTROL:
  337. case USB_ENDPOINT_XFER_BULK:
  338. /* fifo policy for these lists, except that NAKing
  339. * should rotate a qh to the end (for fairness).
  340. */
  341. if (qh->mux == 1) {
  342. head = qh->ring.prev;
  343. list_del(&qh->ring);
  344. kfree(qh);
  345. qh = first_qh(head);
  346. break;
  347. }
  348. /* else: fall through */
  349. case USB_ENDPOINT_XFER_ISOC:
  350. case USB_ENDPOINT_XFER_INT:
  351. /* this is where periodic bandwidth should be
  352. * de-allocated if it's tracked and allocated;
  353. * and where we'd update the schedule tree...
  354. */
  355. kfree(qh);
  356. qh = NULL;
  357. break;
  358. }
  359. }
  360. if (qh != NULL && qh->is_ready) {
  361. musb_dbg(musb, "... next ep%d %cX urb %p",
  362. hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
  363. musb_start_urb(musb, is_in, qh);
  364. }
  365. }
  366. static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
  367. {
  368. /* we don't want fifo to fill itself again;
  369. * ignore dma (various models),
  370. * leave toggle alone (may not have been saved yet)
  371. */
  372. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
  373. csr &= ~(MUSB_RXCSR_H_REQPKT
  374. | MUSB_RXCSR_H_AUTOREQ
  375. | MUSB_RXCSR_AUTOCLEAR);
  376. /* write 2x to allow double buffering */
  377. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  378. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  379. /* flush writebuffer */
  380. return musb_readw(hw_ep->regs, MUSB_RXCSR);
  381. }
  382. /*
  383. * PIO RX for a packet (or part of it).
  384. */
  385. static bool
  386. musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
  387. {
  388. u16 rx_count;
  389. u8 *buf;
  390. u16 csr;
  391. bool done = false;
  392. u32 length;
  393. int do_flush = 0;
  394. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  395. void __iomem *epio = hw_ep->regs;
  396. struct musb_qh *qh = hw_ep->in_qh;
  397. int pipe = urb->pipe;
  398. void *buffer = urb->transfer_buffer;
  399. /* musb_ep_select(mbase, epnum); */
  400. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  401. musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
  402. urb->transfer_buffer, qh->offset,
  403. urb->transfer_buffer_length);
  404. if (rx_count > 512) {
  405. printk(KERN_ALERT "ERR: %s rx_count = %d\n", __FUNCTION__, rx_count);
  406. }
  407. /* unload FIFO */
  408. if (usb_pipeisoc(pipe)) {
  409. int status = 0;
  410. struct usb_iso_packet_descriptor *d;
  411. if (iso_err) {
  412. status = -EILSEQ;
  413. urb->error_count++;
  414. }
  415. d = urb->iso_frame_desc + qh->iso_idx;
  416. buf = buffer + d->offset;
  417. length = d->length;
  418. if (rx_count > length) {
  419. if (status == 0) {
  420. status = -EOVERFLOW;
  421. urb->error_count++;
  422. }
  423. musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
  424. do_flush = 1;
  425. } else
  426. length = rx_count;
  427. urb->actual_length += length;
  428. d->actual_length = length;
  429. d->status = status;
  430. /* see if we are done */
  431. done = (++qh->iso_idx >= urb->number_of_packets);
  432. } else {
  433. /* non-isoch */
  434. buf = buffer + qh->offset;
  435. length = urb->transfer_buffer_length - qh->offset;
  436. if (rx_count > length) {
  437. if (urb->status == -EINPROGRESS)
  438. urb->status = -EOVERFLOW;
  439. musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
  440. do_flush = 1;
  441. } else
  442. length = rx_count;
  443. urb->actual_length += length;
  444. qh->offset += length;
  445. /* see if we are done */
  446. done = (urb->actual_length == urb->transfer_buffer_length)
  447. || (rx_count < qh->maxpacket)
  448. || (urb->status != -EINPROGRESS);
  449. if (done
  450. && (urb->status == -EINPROGRESS)
  451. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  452. && (urb->actual_length
  453. < urb->transfer_buffer_length))
  454. urb->status = -EREMOTEIO;
  455. }
  456. musb_read_fifo(hw_ep, length, buf);
  457. csr = musb_readw(epio, MUSB_RXCSR);
  458. csr |= MUSB_RXCSR_H_WZC_BITS;
  459. if (unlikely(do_flush))
  460. musb_h_flush_rxfifo(hw_ep, csr);
  461. else {
  462. /* REVISIT this assumes AUTOCLEAR is never set */
  463. csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
  464. if (!done)
  465. csr |= MUSB_RXCSR_H_REQPKT;
  466. musb_writew(epio, MUSB_RXCSR, csr);
  467. }
  468. return done;
  469. }
  470. /* we don't always need to reinit a given side of an endpoint...
  471. * when we do, use tx/rx reinit routine and then construct a new CSR
  472. * to address data toggle, NYET, and DMA or PIO.
  473. *
  474. * it's possible that driver bugs (especially for DMA) or aborting a
  475. * transfer might have left the endpoint busier than it should be.
  476. * the busy/not-empty tests are basically paranoia.
  477. */
  478. static void
  479. musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
  480. {
  481. struct musb_hw_ep *ep = musb->endpoints + epnum;
  482. u16 csr;
  483. /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
  484. * That always uses tx_reinit since ep0 repurposes TX register
  485. * offsets; the initial SETUP packet is also a kind of OUT.
  486. */
  487. /* if programmed for Tx, put it in RX mode */
  488. if (ep->is_shared_fifo) {
  489. csr = musb_readw(ep->regs, MUSB_TXCSR);
  490. if (csr & MUSB_TXCSR_MODE) {
  491. musb_h_tx_flush_fifo(ep);
  492. csr = musb_readw(ep->regs, MUSB_TXCSR);
  493. musb_writew(ep->regs, MUSB_TXCSR,
  494. csr | MUSB_TXCSR_FRCDATATOG);
  495. #if NICHOLAS_ADD
  496. csr = musb_readw(ep->regs, MUSB_TXCSR);
  497. csr &= ~MUSB_TXCSR_MODE;
  498. musb_writew(ep->regs, MUSB_TXCSR, csr);
  499. #endif
  500. }
  501. /*
  502. * Clear the MODE bit (and everything else) to enable Rx.
  503. * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
  504. */
  505. #if NICHOLAS_ADD
  506. if (csr & MUSB_TXCSR_DMAMODE)
  507. {
  508. csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_AUTOSET);
  509. musb_writew(ep->regs, MUSB_TXCSR, csr);
  510. csr &= ~MUSB_TXCSR_DMAMODE;
  511. musb_writew(ep->regs, MUSB_TXCSR, csr);
  512. }
  513. #else
  514. if (csr & MUSB_TXCSR_DMAMODE)
  515. musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
  516. #endif
  517. musb_writew(ep->regs, MUSB_TXCSR, 0);
  518. /* scrub all previous state, clearing toggle */
  519. } else {
  520. csr = musb_readw(ep->regs, MUSB_RXCSR);
  521. if (csr & MUSB_RXCSR_RXPKTRDY)
  522. WARNING("rx%d, packet/%d ready?\n", ep->epnum,
  523. musb_readw(ep->regs, MUSB_RXCOUNT));
  524. musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
  525. }
  526. /* target addr and (for multipoint) hub addr/port */
  527. if (musb->is_multipoint) {
  528. musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
  529. musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
  530. musb_write_rxhubport(musb, epnum, qh->h_port_reg);
  531. } else
  532. musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
  533. /* protocol/endpoint, interval/NAKlimit, i/o size */
  534. musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
  535. musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
  536. /* NOTE: bulk combining rewrites high bits of maxpacket */
  537. /* Set RXMAXP with the FIFO size of the endpoint
  538. * to disable double buffer mode.
  539. */
  540. musb_writew(ep->regs, MUSB_RXMAXP,
  541. qh->maxpacket | ((qh->hb_mult - 1) << 11));
  542. ep->rx_reinit = 0;
  543. }
  544. static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
  545. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  546. struct urb *urb, u32 offset,
  547. u32 *length, u8 *mode)
  548. {
  549. struct dma_channel *channel = hw_ep->tx_channel;
  550. void __iomem *epio = hw_ep->regs;
  551. u16 pkt_size = qh->maxpacket;
  552. u16 csr;
  553. if (*length > channel->max_len)
  554. *length = channel->max_len;
  555. csr = musb_readw(epio, MUSB_TXCSR);
  556. if (*length > pkt_size) {
  557. *mode = 1;
  558. csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
  559. /* autoset shouldn't be set in high bandwidth */
  560. /*
  561. * Enable Autoset according to table
  562. * below
  563. * bulk_split hb_mult Autoset_Enable
  564. * 0 1 Yes(Normal)
  565. * 0 >1 No(High BW ISO)
  566. * 1 1 Yes(HS bulk)
  567. * 1 >1 Yes(FS bulk)
  568. */
  569. if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
  570. can_bulk_split(hw_ep->musb, qh->type)))
  571. csr |= MUSB_TXCSR_AUTOSET;
  572. } else {
  573. *mode = 0;
  574. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
  575. csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
  576. }
  577. channel->desired_mode = *mode;
  578. musb_writew(epio, MUSB_TXCSR, csr);
  579. }
  580. static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
  581. struct musb_hw_ep *hw_ep,
  582. struct musb_qh *qh,
  583. struct urb *urb,
  584. u32 offset,
  585. u32 *length,
  586. u8 *mode)
  587. {
  588. struct dma_channel *channel = hw_ep->tx_channel;
  589. channel->actual_len = 0;
  590. /*
  591. * TX uses "RNDIS" mode automatically but needs help
  592. * to identify the zero-length-final-packet case.
  593. */
  594. *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
  595. }
  596. static bool musb_tx_dma_program(struct dma_controller *dma,
  597. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  598. struct urb *urb, u32 offset, u32 length)
  599. {
  600. struct dma_channel *channel = hw_ep->tx_channel;
  601. u16 pkt_size = qh->maxpacket;
  602. u8 mode;
  603. if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
  604. musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
  605. &length, &mode);
  606. else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
  607. musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
  608. &length, &mode);
  609. else
  610. return false;
  611. qh->segsize = length;
  612. /*
  613. * Ensure the data reaches to main memory before starting
  614. * DMA transfer
  615. */
  616. wmb();
  617. if (!dma->channel_program(channel, pkt_size, mode,
  618. urb->transfer_dma + offset, length)) {
  619. void __iomem *epio = hw_ep->regs;
  620. u16 csr;
  621. dma->channel_release(channel);
  622. hw_ep->tx_channel = NULL;
  623. csr = musb_readw(epio, MUSB_TXCSR);
  624. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  625. musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
  626. return false;
  627. }
  628. return true;
  629. }
  630. #if NICHOLAS_ADD
  631. void musb_dma_channel_release(struct musb *musb)
  632. {
  633. struct dma_controller *dma_controller;
  634. struct musb_hw_ep *hw_ep;
  635. u8 i;
  636. dma_controller = musb->dma_controller;
  637. for(i=0; (i<musb->config->num_eps) && (i<MUSB_C_NUM_EPS); i++)
  638. {
  639. hw_ep = musb->endpoints + i;
  640. if(hw_ep->rx_channel)
  641. {
  642. dma_controller->channel_release(hw_ep->rx_channel);
  643. hw_ep->rx_channel = NULL;
  644. }
  645. if(hw_ep->tx_channel)
  646. {
  647. dma_controller->channel_release(hw_ep->tx_channel);
  648. hw_ep->tx_channel = NULL;
  649. }
  650. }
  651. }
  652. #endif
  653. /*
  654. * Program an HDRC endpoint as per the given URB
  655. * Context: irqs blocked, controller lock held
  656. */
  657. static void musb_ep_program(struct musb *musb, u8 epnum,
  658. struct urb *urb, int is_out,
  659. u8 *buf, u32 offset, u32 len)
  660. {
  661. struct dma_controller *dma_controller;
  662. struct dma_channel *dma_channel;
  663. u8 dma_ok;
  664. void __iomem *mbase = musb->mregs;
  665. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  666. void __iomem *epio = hw_ep->regs;
  667. struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
  668. u16 packet_sz = qh->maxpacket;
  669. u8 use_dma = 1;
  670. u16 csr;
  671. musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
  672. "h_addr%02x h_port%02x bytes %d",
  673. is_out ? "-->" : "<--",
  674. epnum, urb, urb->dev->speed,
  675. qh->addr_reg, qh->epnum, is_out ? "out" : "in",
  676. qh->h_addr_reg, qh->h_port_reg,
  677. len);
  678. musb_ep_select(mbase, epnum);
  679. if (is_out && !len) {
  680. use_dma = 0;
  681. csr = musb_readw(epio, MUSB_TXCSR);
  682. csr &= ~MUSB_TXCSR_DMAENAB;
  683. musb_writew(epio, MUSB_TXCSR, csr);
  684. hw_ep->tx_channel = NULL;
  685. }
  686. /* candidate for DMA? */
  687. dma_controller = musb->dma_controller;
  688. if (use_dma && is_dma_capable() && epnum && dma_controller) {
  689. dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
  690. if (!dma_channel) {
  691. dma_channel = dma_controller->channel_alloc(
  692. dma_controller, hw_ep, is_out);
  693. if (is_out)
  694. hw_ep->tx_channel = dma_channel;
  695. else
  696. hw_ep->rx_channel = dma_channel;
  697. }
  698. } else
  699. dma_channel = NULL;
  700. /* make sure we clear DMAEnab, autoSet bits from previous run */
  701. /* OUT/transmit/EP0 or IN/receive? */
  702. if (is_out) {
  703. u16 csr;
  704. u16 int_txe;
  705. u16 load_count;
  706. csr = musb_readw(epio, MUSB_TXCSR);
  707. /* disable interrupt in case we flush */
  708. int_txe = musb->intrtxe;
  709. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  710. /* general endpoint setup */
  711. if (epnum) {
  712. /* flush all old state, set default */
  713. /*
  714. * We could be flushing valid
  715. * packets in double buffering
  716. * case
  717. */
  718. if (!hw_ep->tx_double_buffered)
  719. musb_h_tx_flush_fifo(hw_ep);
  720. /*
  721. * We must not clear the DMAMODE bit before or in
  722. * the same cycle with the DMAENAB bit, so we clear
  723. * the latter first...
  724. */
  725. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  726. | MUSB_TXCSR_AUTOSET
  727. | MUSB_TXCSR_DMAENAB
  728. | MUSB_TXCSR_FRCDATATOG
  729. | MUSB_TXCSR_H_RXSTALL
  730. | MUSB_TXCSR_H_ERROR
  731. | MUSB_TXCSR_TXPKTRDY
  732. );
  733. csr |= MUSB_TXCSR_MODE;
  734. if (!hw_ep->tx_double_buffered) {
  735. if (usb_gettoggle(urb->dev, qh->epnum, 1))
  736. csr |= MUSB_TXCSR_H_WR_DATATOGGLE
  737. | MUSB_TXCSR_H_DATATOGGLE;
  738. else
  739. csr |= MUSB_TXCSR_CLRDATATOG;
  740. }
  741. musb_writew(epio, MUSB_TXCSR, csr);
  742. /* REVISIT may need to clear FLUSHFIFO ... */
  743. csr &= ~MUSB_TXCSR_DMAMODE;
  744. musb_writew(epio, MUSB_TXCSR, csr);
  745. csr = musb_readw(epio, MUSB_TXCSR);
  746. } else {
  747. /* endpoint 0: just flush */
  748. musb_h_ep0_flush_fifo(hw_ep);
  749. }
  750. /* target addr and (for multipoint) hub addr/port */
  751. if (musb->is_multipoint) {
  752. musb_write_txfunaddr(musb, epnum, qh->addr_reg);
  753. musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
  754. musb_write_txhubport(musb, epnum, qh->h_port_reg);
  755. /* FIXME if !epnum, do the same for RX ... */
  756. } else
  757. musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
  758. /* protocol/endpoint/interval/NAKlimit */
  759. if (epnum) {
  760. musb_ep_select(mbase, epnum);
  761. musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
  762. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  763. if (can_bulk_split(musb, qh->type)) {
  764. qh->hb_mult = hw_ep->max_packet_sz_tx
  765. / packet_sz;
  766. musb_writew(epio, MUSB_TXMAXP, packet_sz
  767. | ((qh->hb_mult) - 1) << 11);
  768. } else {
  769. musb_writew(epio, MUSB_TXMAXP,
  770. qh->maxpacket |
  771. ((qh->hb_mult - 1) << 11));
  772. }
  773. } else {
  774. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  775. if (musb->is_multipoint)
  776. musb_writeb(epio, MUSB_TYPE0,
  777. qh->type_reg);
  778. }
  779. #if NICHOLAS_ADD
  780. if (can_bulk_split(musb, qh->type))
  781. load_count = min((u32) hw_ep->max_packet_sz_tx,
  782. len);
  783. else
  784. load_count = min((u32) packet_sz, len);
  785. if (dma_channel && musb_tx_dma_program(dma_controller,
  786. hw_ep, qh, urb, offset, load_count))
  787. load_count = 0;
  788. #else
  789. if (can_bulk_split(musb, qh->type))
  790. load_count = min((u32) hw_ep->max_packet_sz_tx,
  791. len);
  792. else
  793. load_count = min((u32) packet_sz, len);
  794. if (dma_channel && musb_tx_dma_program(dma_controller,
  795. hw_ep, qh, urb, offset, len))
  796. load_count = 0;
  797. #endif
  798. if (load_count) {
  799. /* PIO to load FIFO */
  800. qh->segsize = load_count;
  801. if (!buf) {
  802. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  803. SG_MITER_ATOMIC
  804. | SG_MITER_FROM_SG);
  805. if (!sg_miter_next(&qh->sg_miter)) {
  806. dev_err(musb->controller,
  807. "error: sg"
  808. "list empty\n");
  809. sg_miter_stop(&qh->sg_miter);
  810. goto finish;
  811. }
  812. buf = qh->sg_miter.addr + urb->sg->offset +
  813. urb->actual_length;
  814. load_count = min_t(u32, load_count,
  815. qh->sg_miter.length);
  816. musb_write_fifo(hw_ep, load_count, buf);
  817. qh->sg_miter.consumed = load_count;
  818. sg_miter_stop(&qh->sg_miter);
  819. } else
  820. musb_write_fifo(hw_ep, load_count, buf);
  821. }
  822. finish:
  823. /* re-enable interrupt */
  824. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  825. /* IN/receive */
  826. } else {
  827. u16 csr;
  828. if (hw_ep->rx_reinit) {
  829. musb_rx_reinit(musb, qh, epnum);
  830. /* init new state: toggle and NYET, maybe DMA later */
  831. if (usb_gettoggle(urb->dev, qh->epnum, 0))
  832. csr = MUSB_RXCSR_H_WR_DATATOGGLE
  833. | MUSB_RXCSR_H_DATATOGGLE;
  834. else
  835. #if NICHOLAS_ADD
  836. csr |= MUSB_RXCSR_CLRDATATOG;
  837. #else
  838. csr = 0;
  839. #endif
  840. if (qh->type == USB_ENDPOINT_XFER_INT)
  841. csr |= MUSB_RXCSR_DISNYET;
  842. } else {
  843. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  844. if (csr & (MUSB_RXCSR_RXPKTRDY
  845. | MUSB_RXCSR_DMAENAB
  846. | MUSB_RXCSR_H_REQPKT))
  847. ERR("broken !rx_reinit, ep%d csr %04x\n",
  848. hw_ep->epnum, csr);
  849. /* scrub any stale state, leaving toggle alone */
  850. csr &= MUSB_RXCSR_DISNYET;
  851. }
  852. /* kick things off */
  853. if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
  854. /* Candidate for DMA */
  855. dma_channel->actual_len = 0L;
  856. qh->segsize = len;
  857. /* AUTOREQ is in a DMA register */
  858. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  859. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  860. /*
  861. * Unless caller treats short RX transfers as
  862. * errors, we dare not queue multiple transfers.
  863. */
  864. dma_ok = dma_controller->channel_program(dma_channel,
  865. packet_sz, !(urb->transfer_flags &
  866. URB_SHORT_NOT_OK),
  867. urb->transfer_dma + offset,
  868. qh->segsize);
  869. if (!dma_ok) {
  870. dma_controller->channel_release(dma_channel);
  871. hw_ep->rx_channel = dma_channel = NULL;
  872. } else
  873. csr |= MUSB_RXCSR_DMAENAB;
  874. }
  875. csr |= MUSB_RXCSR_H_REQPKT;
  876. musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
  877. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  878. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  879. }
  880. }
  881. /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
  882. * the end; avoids starvation for other endpoints.
  883. */
  884. static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
  885. int is_in)
  886. {
  887. struct dma_channel *dma;
  888. struct urb *urb;
  889. void __iomem *mbase = musb->mregs;
  890. void __iomem *epio = ep->regs;
  891. struct musb_qh *cur_qh, *next_qh;
  892. u16 rx_csr, tx_csr;
  893. musb_ep_select(mbase, ep->epnum);
  894. if (is_in) {
  895. dma = is_dma_capable() ? ep->rx_channel : NULL;
  896. /*
  897. * Need to stop the transaction by clearing REQPKT first
  898. * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
  899. * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
  900. */
  901. rx_csr = musb_readw(epio, MUSB_RXCSR);
  902. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  903. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  904. musb_writew(epio, MUSB_RXCSR, rx_csr);
  905. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  906. musb_writew(epio, MUSB_RXCSR, rx_csr);
  907. cur_qh = first_qh(&musb->in_bulk);
  908. } else {
  909. dma = is_dma_capable() ? ep->tx_channel : NULL;
  910. /* clear nak timeout bit */
  911. tx_csr = musb_readw(epio, MUSB_TXCSR);
  912. tx_csr |= MUSB_TXCSR_H_WZC_BITS;
  913. tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
  914. musb_writew(epio, MUSB_TXCSR, tx_csr);
  915. cur_qh = first_qh(&musb->out_bulk);
  916. }
  917. if (cur_qh) {
  918. urb = next_urb(cur_qh);
  919. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  920. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  921. musb->dma_controller->channel_abort(dma);
  922. urb->actual_length += dma->actual_len;
  923. dma->actual_len = 0L;
  924. }
  925. musb_save_toggle(cur_qh, is_in, urb);
  926. if (is_in) {
  927. /* move cur_qh to end of queue */
  928. list_move_tail(&cur_qh->ring, &musb->in_bulk);
  929. /* get the next qh from musb->in_bulk */
  930. next_qh = first_qh(&musb->in_bulk);
  931. /* set rx_reinit and schedule the next qh */
  932. ep->rx_reinit = 1;
  933. } else {
  934. /* move cur_qh to end of queue */
  935. list_move_tail(&cur_qh->ring, &musb->out_bulk);
  936. /* get the next qh from musb->out_bulk */
  937. next_qh = first_qh(&musb->out_bulk);
  938. /* set tx_reinit and schedule the next qh */
  939. ep->tx_reinit = 1;
  940. }
  941. if (next_qh)
  942. musb_start_urb(musb, is_in, next_qh);
  943. }
  944. }
  945. /*
  946. * Service the default endpoint (ep0) as host.
  947. * Return true until it's time to start the status stage.
  948. */
  949. static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
  950. {
  951. bool more = false;
  952. u8 *fifo_dest = NULL;
  953. u16 fifo_count = 0;
  954. struct musb_hw_ep *hw_ep = musb->control_ep;
  955. struct musb_qh *qh = hw_ep->in_qh;
  956. struct usb_ctrlrequest *request;
  957. switch (musb->ep0_stage) {
  958. case MUSB_EP0_IN:
  959. fifo_dest = urb->transfer_buffer + urb->actual_length;
  960. fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
  961. urb->actual_length);
  962. if (fifo_count < len)
  963. urb->status = -EOVERFLOW;
  964. musb_read_fifo(hw_ep, fifo_count, fifo_dest);
  965. urb->actual_length += fifo_count;
  966. if (len < qh->maxpacket) {
  967. /* always terminate on short read; it's
  968. * rarely reported as an error.
  969. */
  970. } else if (urb->actual_length <
  971. urb->transfer_buffer_length)
  972. more = true;
  973. break;
  974. case MUSB_EP0_START:
  975. request = (struct usb_ctrlrequest *) urb->setup_packet;
  976. if (!request->wLength) {
  977. musb_dbg(musb, "start no-DATA");
  978. break;
  979. } else if (request->bRequestType & USB_DIR_IN) {
  980. musb_dbg(musb, "start IN-DATA");
  981. musb->ep0_stage = MUSB_EP0_IN;
  982. more = true;
  983. break;
  984. } else {
  985. musb_dbg(musb, "start OUT-DATA");
  986. musb->ep0_stage = MUSB_EP0_OUT;
  987. more = true;
  988. }
  989. /* FALLTHROUGH */
  990. case MUSB_EP0_OUT:
  991. fifo_count = min_t(size_t, qh->maxpacket,
  992. urb->transfer_buffer_length -
  993. urb->actual_length);
  994. if (fifo_count) {
  995. fifo_dest = (u8 *) (urb->transfer_buffer
  996. + urb->actual_length);
  997. musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
  998. fifo_count,
  999. (fifo_count == 1) ? "" : "s",
  1000. fifo_dest);
  1001. musb_write_fifo(hw_ep, fifo_count, fifo_dest);
  1002. urb->actual_length += fifo_count;
  1003. more = true;
  1004. }
  1005. break;
  1006. default:
  1007. ERR("bogus ep0 stage %d\n", musb->ep0_stage);
  1008. break;
  1009. }
  1010. return more;
  1011. }
  1012. /*
  1013. * Handle default endpoint interrupt as host. Only called in IRQ time
  1014. * from musb_interrupt().
  1015. *
  1016. * called with controller irqlocked
  1017. */
  1018. irqreturn_t musb_h_ep0_irq(struct musb *musb)
  1019. {
  1020. struct urb *urb;
  1021. u16 csr, len;
  1022. int status = 0;
  1023. void __iomem *mbase = musb->mregs;
  1024. struct musb_hw_ep *hw_ep = musb->control_ep;
  1025. void __iomem *epio = hw_ep->regs;
  1026. struct musb_qh *qh = hw_ep->in_qh;
  1027. bool complete = false;
  1028. irqreturn_t retval = IRQ_NONE;
  1029. /* ep0 only has one queue, "in" */
  1030. urb = next_urb(qh);
  1031. musb_ep_select(mbase, 0);
  1032. csr = musb_readw(epio, MUSB_CSR0);
  1033. len = (csr & MUSB_CSR0_RXPKTRDY)
  1034. ? musb_readb(epio, MUSB_COUNT0)
  1035. : 0;
  1036. musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
  1037. csr, qh, len, urb, musb->ep0_stage);
  1038. /* if we just did status stage, we are done */
  1039. if (MUSB_EP0_STATUS == musb->ep0_stage) {
  1040. retval = IRQ_HANDLED;
  1041. complete = true;
  1042. }
  1043. /* prepare status */
  1044. if (csr & MUSB_CSR0_H_RXSTALL) {
  1045. musb_dbg(musb, "STALLING ENDPOINT");
  1046. status = -EPIPE;
  1047. } else if (csr & MUSB_CSR0_H_ERROR) {
  1048. musb_dbg(musb, "no response, csr0 %04x", csr);
  1049. status = -EPROTO;
  1050. } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
  1051. musb_dbg(musb, "control NAK timeout");
  1052. /* NOTE: this code path would be a good place to PAUSE a
  1053. * control transfer, if another one is queued, so that
  1054. * ep0 is more likely to stay busy. That's already done
  1055. * for bulk RX transfers.
  1056. *
  1057. * if (qh->ring.next != &musb->control), then
  1058. * we have a candidate... NAKing is *NOT* an error
  1059. */
  1060. musb_writew(epio, MUSB_CSR0, 0);
  1061. retval = IRQ_HANDLED;
  1062. }
  1063. if (status) {
  1064. musb_dbg(musb, "aborting");
  1065. retval = IRQ_HANDLED;
  1066. if (urb)
  1067. urb->status = status;
  1068. complete = true;
  1069. /* use the proper sequence to abort the transfer */
  1070. if (csr & MUSB_CSR0_H_REQPKT) {
  1071. csr &= ~MUSB_CSR0_H_REQPKT;
  1072. musb_writew(epio, MUSB_CSR0, csr);
  1073. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  1074. musb_writew(epio, MUSB_CSR0, csr);
  1075. } else {
  1076. musb_h_ep0_flush_fifo(hw_ep);
  1077. }
  1078. if (qh)
  1079. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  1080. else
  1081. musb_writeb(epio, MUSB_NAKLIMIT0, 0);
  1082. /* clear it */
  1083. musb_writew(epio, MUSB_CSR0, 0);
  1084. }
  1085. if (unlikely(!urb)) {
  1086. /* stop endpoint since we have no place for its data, this
  1087. * SHOULD NEVER HAPPEN! */
  1088. ERR("no URB for end 0\n");
  1089. musb_h_ep0_flush_fifo(hw_ep);
  1090. goto done;
  1091. }
  1092. if (!complete) {
  1093. /* call common logic and prepare response */
  1094. if (musb_h_ep0_continue(musb, len, urb)) {
  1095. /* more packets required */
  1096. csr = (MUSB_EP0_IN == musb->ep0_stage)
  1097. ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
  1098. } else {
  1099. /* data transfer complete; perform status phase */
  1100. if (usb_pipeout(urb->pipe)
  1101. || !urb->transfer_buffer_length)
  1102. csr = MUSB_CSR0_H_STATUSPKT
  1103. | MUSB_CSR0_H_REQPKT;
  1104. else
  1105. csr = MUSB_CSR0_H_STATUSPKT
  1106. | MUSB_CSR0_TXPKTRDY;
  1107. /* disable ping token in status phase */
  1108. csr |= MUSB_CSR0_H_DIS_PING;
  1109. /* flag status stage */
  1110. musb->ep0_stage = MUSB_EP0_STATUS;
  1111. musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
  1112. }
  1113. musb_writew(epio, MUSB_CSR0, csr);
  1114. retval = IRQ_HANDLED;
  1115. } else
  1116. musb->ep0_stage = MUSB_EP0_IDLE;
  1117. /* call completion handler if done */
  1118. if (complete)
  1119. musb_advance_schedule(musb, urb, hw_ep, 1);
  1120. done:
  1121. return retval;
  1122. }
  1123. #ifdef CONFIG_USB_INVENTRA_DMA
  1124. /* Host side TX (OUT) using Mentor DMA works as follows:
  1125. submit_urb ->
  1126. - if queue was empty, Program Endpoint
  1127. - ... which starts DMA to fifo in mode 1 or 0
  1128. DMA Isr (transfer complete) -> TxAvail()
  1129. - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
  1130. only in musb_cleanup_urb)
  1131. - TxPktRdy has to be set in mode 0 or for
  1132. short packets in mode 1.
  1133. */
  1134. #endif
  1135. /* Service a Tx-Available or dma completion irq for the endpoint */
  1136. void musb_host_tx(struct musb *musb, u8 epnum)
  1137. {
  1138. int pipe;
  1139. bool done = false;
  1140. u16 tx_csr;
  1141. size_t length = 0;
  1142. size_t offset = 0;
  1143. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1144. void __iomem *epio = hw_ep->regs;
  1145. struct musb_qh *qh = hw_ep->out_qh;
  1146. struct urb *urb = next_urb(qh);
  1147. u32 status = 0;
  1148. void __iomem *mbase = musb->mregs;
  1149. struct dma_channel *dma;
  1150. bool transfer_pending = false;
  1151. musb_ep_select(mbase, epnum);
  1152. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1153. /* with CPPI, DMA sometimes triggers "extra" irqs */
  1154. if (!urb) {
  1155. musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
  1156. return;
  1157. }
  1158. pipe = urb->pipe;
  1159. dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
  1160. trace_musb_urb_tx(musb, urb);
  1161. musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
  1162. dma ? ", dma" : "");
  1163. /* check for errors */
  1164. if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
  1165. /* dma was disabled, fifo flushed */
  1166. musb_dbg(musb, "TX end %d stall", epnum);
  1167. /* stall; record URB status */
  1168. status = -EPIPE;
  1169. } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
  1170. /* (NON-ISO) dma was disabled, fifo flushed */
  1171. musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
  1172. #if NICHOLAS_ADD
  1173. musb_writew(epio, MUSB_TXCSR,
  1174. MUSB_TXCSR_H_WZC_BITS
  1175. | MUSB_TXCSR_TXPKTRDY);
  1176. if(qh->intv_reg == UDISK_INTERVAL){
  1177. //Is U Disk
  1178. status = -ETIMEDOUT;
  1179. }
  1180. else{
  1181. //IS Phone
  1182. return;
  1183. }
  1184. #else
  1185. status = -ETIMEDOUT;
  1186. #endif
  1187. } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
  1188. if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
  1189. && !list_is_singular(&musb->out_bulk)) {
  1190. musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
  1191. musb_bulk_nak_timeout(musb, hw_ep, 0);
  1192. } else {
  1193. musb_dbg(musb, "TX ep%d device not responding", epnum);
  1194. /* NOTE: this code path would be a good place to PAUSE a
  1195. * transfer, if there's some other (nonperiodic) tx urb
  1196. * that could use this fifo. (dma complicates it...)
  1197. * That's already done for bulk RX transfers.
  1198. *
  1199. * if (bulk && qh->ring.next != &musb->out_bulk), then
  1200. * we have a candidate... NAKing is *NOT* an error
  1201. */
  1202. musb_ep_select(mbase, epnum);
  1203. musb_writew(epio, MUSB_TXCSR,
  1204. MUSB_TXCSR_H_WZC_BITS
  1205. | MUSB_TXCSR_TXPKTRDY);
  1206. }
  1207. return;
  1208. }
  1209. done:
  1210. if (status) {
  1211. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1212. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1213. musb->dma_controller->channel_abort(dma);
  1214. }
  1215. /* do the proper sequence to abort the transfer in the
  1216. * usb core; the dma engine should already be stopped.
  1217. */
  1218. musb_h_tx_flush_fifo(hw_ep);
  1219. tx_csr &= ~(MUSB_TXCSR_AUTOSET
  1220. | MUSB_TXCSR_DMAENAB
  1221. | MUSB_TXCSR_H_ERROR
  1222. | MUSB_TXCSR_H_RXSTALL
  1223. | MUSB_TXCSR_H_NAKTIMEOUT
  1224. );
  1225. musb_ep_select(mbase, epnum);
  1226. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1227. /* REVISIT may need to clear FLUSHFIFO ... */
  1228. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1229. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  1230. done = true;
  1231. }
  1232. /* second cppi case */
  1233. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1234. musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
  1235. return;
  1236. }
  1237. if (is_dma_capable() && dma && !status) {
  1238. /*
  1239. * DMA has completed. But if we're using DMA mode 1 (multi
  1240. * packet DMA), we need a terminal TXPKTRDY interrupt before
  1241. * we can consider this transfer completed, lest we trash
  1242. * its last packet when writing the next URB's data. So we
  1243. * switch back to mode 0 to get that interrupt; we'll come
  1244. * back here once it happens.
  1245. */
  1246. if (tx_csr & MUSB_TXCSR_DMAMODE) {
  1247. /*
  1248. * We shouldn't clear DMAMODE with DMAENAB set; so
  1249. * clear them in a safe order. That should be OK
  1250. * once TXPKTRDY has been set (and I've never seen
  1251. * it being 0 at this moment -- DMA interrupt latency
  1252. * is significant) but if it hasn't been then we have
  1253. * no choice but to stop being polite and ignore the
  1254. * programmer's guide... :-)
  1255. *
  1256. * Note that we must write TXCSR with TXPKTRDY cleared
  1257. * in order not to re-trigger the packet send (this bit
  1258. * can't be cleared by CPU), and there's another caveat:
  1259. * TXPKTRDY may be set shortly and then cleared in the
  1260. * double-buffered FIFO mode, so we do an extra TXCSR
  1261. * read for debouncing...
  1262. */
  1263. tx_csr &= musb_readw(epio, MUSB_TXCSR);
  1264. if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
  1265. tx_csr &= ~(MUSB_TXCSR_DMAENAB |
  1266. MUSB_TXCSR_TXPKTRDY);
  1267. musb_writew(epio, MUSB_TXCSR,
  1268. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1269. }
  1270. tx_csr &= ~(MUSB_TXCSR_DMAMODE |
  1271. MUSB_TXCSR_TXPKTRDY);
  1272. musb_writew(epio, MUSB_TXCSR,
  1273. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1274. /*
  1275. * There is no guarantee that we'll get an interrupt
  1276. * after clearing DMAMODE as we might have done this
  1277. * too late (after TXPKTRDY was cleared by controller).
  1278. * Re-read TXCSR as we have spoiled its previous value.
  1279. */
  1280. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1281. }
  1282. /*
  1283. * We may get here from a DMA completion or TXPKTRDY interrupt.
  1284. * In any case, we must check the FIFO status here and bail out
  1285. * only if the FIFO still has data -- that should prevent the
  1286. * "missed" TXPKTRDY interrupts and deal with double-buffered
  1287. * FIFO mode too...
  1288. */
  1289. if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
  1290. musb_dbg(musb,
  1291. "DMA complete but FIFO not empty, CSR %04x",
  1292. tx_csr);
  1293. return;
  1294. }
  1295. }
  1296. if (!status || dma || usb_pipeisoc(pipe)) {
  1297. if (dma)
  1298. length = dma->actual_len;
  1299. else
  1300. length = qh->segsize;
  1301. qh->offset += length;
  1302. if (usb_pipeisoc(pipe)) {
  1303. struct usb_iso_packet_descriptor *d;
  1304. d = urb->iso_frame_desc + qh->iso_idx;
  1305. d->actual_length = length;
  1306. d->status = status;
  1307. if (++qh->iso_idx >= urb->number_of_packets) {
  1308. done = true;
  1309. } else {
  1310. d++;
  1311. offset = d->offset;
  1312. length = d->length;
  1313. }
  1314. } else if (dma && urb->transfer_buffer_length == qh->offset) {
  1315. done = true;
  1316. } else {
  1317. /* see if we need to send more data, or ZLP */
  1318. if (qh->segsize < qh->maxpacket)
  1319. done = true;
  1320. else if (qh->offset == urb->transfer_buffer_length
  1321. && !(urb->transfer_flags
  1322. & URB_ZERO_PACKET))
  1323. done = true;
  1324. if (!done) {
  1325. offset = qh->offset;
  1326. #if NICHOLAS_ADD
  1327. if (can_bulk_split(musb, qh->type))
  1328. length = min((u32) hw_ep->max_packet_sz_tx, urb->transfer_buffer_length - offset);
  1329. else
  1330. length = min((u32) qh->maxpacket, urb->transfer_buffer_length - offset);
  1331. #else
  1332. length = urb->transfer_buffer_length - offset;
  1333. #endif
  1334. transfer_pending = true;
  1335. }
  1336. }
  1337. }
  1338. /* urb->status != -EINPROGRESS means request has been faulted,
  1339. * so we must abort this transfer after cleanup
  1340. */
  1341. if (urb->status != -EINPROGRESS) {
  1342. done = true;
  1343. if (status == 0)
  1344. status = urb->status;
  1345. }
  1346. if (done) {
  1347. /* set status */
  1348. urb->status = status;
  1349. urb->actual_length = qh->offset;
  1350. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
  1351. return;
  1352. } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
  1353. if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
  1354. offset, length)) {
  1355. if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  1356. musb_h_tx_dma_start(hw_ep);
  1357. return;
  1358. }
  1359. } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
  1360. musb_dbg(musb, "not complete, but DMA enabled?");
  1361. return;
  1362. }
  1363. /*
  1364. * PIO: start next packet in this URB.
  1365. *
  1366. * REVISIT: some docs say that when hw_ep->tx_double_buffered,
  1367. * (and presumably, FIFO is not half-full) we should write *two*
  1368. * packets before updating TXCSR; other docs disagree...
  1369. */
  1370. if (length > qh->maxpacket)
  1371. length = qh->maxpacket;
  1372. /* Unmap the buffer so that CPU can use it */
  1373. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1374. /*
  1375. * We need to map sg if the transfer_buffer is
  1376. * NULL.
  1377. */
  1378. if (!urb->transfer_buffer) {
  1379. /* sg_miter_start is already done in musb_ep_program */
  1380. if (!sg_miter_next(&qh->sg_miter)) {
  1381. dev_err(musb->controller, "error: sg list empty\n");
  1382. sg_miter_stop(&qh->sg_miter);
  1383. status = -EINVAL;
  1384. goto done;
  1385. }
  1386. length = min_t(u32, length, qh->sg_miter.length);
  1387. musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
  1388. qh->sg_miter.consumed = length;
  1389. sg_miter_stop(&qh->sg_miter);
  1390. } else {
  1391. musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
  1392. }
  1393. qh->segsize = length;
  1394. musb_ep_select(mbase, epnum);
  1395. #if NICHOLAS_ADD
  1396. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1397. musb_writew(epio, MUSB_TXCSR,
  1398. tx_csr | MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1399. #else
  1400. musb_writew(epio, MUSB_TXCSR,
  1401. MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1402. #endif
  1403. }
  1404. #ifdef CONFIG_USB_TI_CPPI41_DMA
  1405. /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
  1406. static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1407. struct musb_hw_ep *hw_ep,
  1408. struct musb_qh *qh,
  1409. struct urb *urb,
  1410. size_t len)
  1411. {
  1412. struct dma_channel *channel = hw_ep->rx_channel;
  1413. void __iomem *epio = hw_ep->regs;
  1414. dma_addr_t *buf;
  1415. u32 length;
  1416. u16 val;
  1417. buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
  1418. (u32)urb->transfer_dma;
  1419. length = urb->iso_frame_desc[qh->iso_idx].length;
  1420. val = musb_readw(epio, MUSB_RXCSR);
  1421. val |= MUSB_RXCSR_DMAENAB;
  1422. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1423. return dma->channel_program(channel, qh->maxpacket, 0,
  1424. (u32)buf, length);
  1425. }
  1426. #else
  1427. static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1428. struct musb_hw_ep *hw_ep,
  1429. struct musb_qh *qh,
  1430. struct urb *urb,
  1431. size_t len)
  1432. {
  1433. return false;
  1434. }
  1435. #endif
  1436. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
  1437. defined(CONFIG_USB_TI_CPPI41_DMA)
  1438. /* Host side RX (IN) using Mentor DMA works as follows:
  1439. submit_urb ->
  1440. - if queue was empty, ProgramEndpoint
  1441. - first IN token is sent out (by setting ReqPkt)
  1442. LinuxIsr -> RxReady()
  1443. /\ => first packet is received
  1444. | - Set in mode 0 (DmaEnab, ~ReqPkt)
  1445. | -> DMA Isr (transfer complete) -> RxReady()
  1446. | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
  1447. | - if urb not complete, send next IN token (ReqPkt)
  1448. | | else complete urb.
  1449. | |
  1450. ---------------------------
  1451. *
  1452. * Nuances of mode 1:
  1453. * For short packets, no ack (+RxPktRdy) is sent automatically
  1454. * (even if AutoClear is ON)
  1455. * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
  1456. * automatically => major problem, as collecting the next packet becomes
  1457. * difficult. Hence mode 1 is not used.
  1458. *
  1459. * REVISIT
  1460. * All we care about at this driver level is that
  1461. * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
  1462. * (b) termination conditions are: short RX, or buffer full;
  1463. * (c) fault modes include
  1464. * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
  1465. * (and that endpoint's dma queue stops immediately)
  1466. * - overflow (full, PLUS more bytes in the terminal packet)
  1467. *
  1468. * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
  1469. * thus be a great candidate for using mode 1 ... for all but the
  1470. * last packet of one URB's transfer.
  1471. */
  1472. static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1473. struct musb_hw_ep *hw_ep,
  1474. struct musb_qh *qh,
  1475. struct urb *urb,
  1476. size_t len)
  1477. {
  1478. struct dma_channel *channel = hw_ep->rx_channel;
  1479. void __iomem *epio = hw_ep->regs;
  1480. u16 val;
  1481. int pipe;
  1482. bool done;
  1483. pipe = urb->pipe;
  1484. if (usb_pipeisoc(pipe)) {
  1485. struct usb_iso_packet_descriptor *d;
  1486. d = urb->iso_frame_desc + qh->iso_idx;
  1487. d->actual_length = len;
  1488. /* even if there was an error, we did the dma
  1489. * for iso_frame_desc->length
  1490. */
  1491. if (d->status != -EILSEQ && d->status != -EOVERFLOW)
  1492. d->status = 0;
  1493. if (++qh->iso_idx >= urb->number_of_packets) {
  1494. done = true;
  1495. } else {
  1496. /* REVISIT: Why ignore return value here? */
  1497. if (musb_dma_cppi41(hw_ep->musb))
  1498. done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
  1499. urb, len);
  1500. done = false;
  1501. }
  1502. } else {
  1503. /* done if urb buffer is full or short packet is recd */
  1504. done = (urb->actual_length + len >=
  1505. urb->transfer_buffer_length
  1506. || channel->actual_len < qh->maxpacket
  1507. || channel->rx_packet_done);
  1508. }
  1509. /* send IN token for next packet, without AUTOREQ */
  1510. if (!done) {
  1511. val = musb_readw(epio, MUSB_RXCSR);
  1512. val |= MUSB_RXCSR_H_REQPKT;
  1513. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1514. }
  1515. return done;
  1516. }
  1517. /* Disadvantage of using mode 1:
  1518. * It's basically usable only for mass storage class; essentially all
  1519. * other protocols also terminate transfers on short packets.
  1520. *
  1521. * Details:
  1522. * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
  1523. * If you try to use mode 1 for (transfer_buffer_length - 512), and try
  1524. * to use the extra IN token to grab the last packet using mode 0, then
  1525. * the problem is that you cannot be sure when the device will send the
  1526. * last packet and RxPktRdy set. Sometimes the packet is recd too soon
  1527. * such that it gets lost when RxCSR is re-set at the end of the mode 1
  1528. * transfer, while sometimes it is recd just a little late so that if you
  1529. * try to configure for mode 0 soon after the mode 1 transfer is
  1530. * completed, you will find rxcount 0. Okay, so you might think why not
  1531. * wait for an interrupt when the pkt is recd. Well, you won't get any!
  1532. */
  1533. static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1534. struct musb_hw_ep *hw_ep,
  1535. struct musb_qh *qh,
  1536. struct urb *urb,
  1537. size_t len,
  1538. u8 iso_err)
  1539. {
  1540. struct musb *musb = hw_ep->musb;
  1541. void __iomem *epio = hw_ep->regs;
  1542. struct dma_channel *channel = hw_ep->rx_channel;
  1543. u16 rx_count, val;
  1544. int length, pipe, done;
  1545. dma_addr_t buf;
  1546. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  1547. pipe = urb->pipe;
  1548. if (rx_count > 512) {
  1549. printk(KERN_ALERT "ERR: %s rx_count = %d\n", __FUNCTION__, rx_count);
  1550. }
  1551. if (usb_pipeisoc(pipe)) {
  1552. int d_status = 0;
  1553. struct usb_iso_packet_descriptor *d;
  1554. d = urb->iso_frame_desc + qh->iso_idx;
  1555. if (iso_err) {
  1556. d_status = -EILSEQ;
  1557. urb->error_count++;
  1558. }
  1559. if (rx_count > d->length) {
  1560. if (d_status == 0) {
  1561. d_status = -EOVERFLOW;
  1562. urb->error_count++;
  1563. }
  1564. musb_dbg(musb, "** OVERFLOW %d into %d",
  1565. rx_count, d->length);
  1566. length = d->length;
  1567. } else
  1568. length = rx_count;
  1569. d->status = d_status;
  1570. buf = urb->transfer_dma + d->offset;
  1571. } else {
  1572. length = rx_count;
  1573. buf = urb->transfer_dma + urb->actual_length;
  1574. }
  1575. channel->desired_mode = 0;
  1576. #ifdef USE_MODE1
  1577. /* because of the issue below, mode 1 will
  1578. * only rarely behave with correct semantics.
  1579. */
  1580. if ((urb->transfer_flags & URB_SHORT_NOT_OK)
  1581. && (urb->transfer_buffer_length - urb->actual_length)
  1582. > qh->maxpacket)
  1583. channel->desired_mode = 1;
  1584. if (rx_count < hw_ep->max_packet_sz_rx) {
  1585. length = rx_count;
  1586. channel->desired_mode = 0;
  1587. } else {
  1588. length = urb->transfer_buffer_length;
  1589. }
  1590. #endif
  1591. /* See comments above on disadvantages of using mode 1 */
  1592. val = musb_readw(epio, MUSB_RXCSR);
  1593. val &= ~MUSB_RXCSR_H_REQPKT;
  1594. if (channel->desired_mode == 0)
  1595. val &= ~MUSB_RXCSR_H_AUTOREQ;
  1596. else
  1597. val |= MUSB_RXCSR_H_AUTOREQ;
  1598. val |= MUSB_RXCSR_DMAENAB;
  1599. /* autoclear shouldn't be set in high bandwidth */
  1600. if (qh->hb_mult == 1)
  1601. val |= MUSB_RXCSR_AUTOCLEAR;
  1602. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1603. /* REVISIT if when actual_length != 0,
  1604. * transfer_buffer_length needs to be
  1605. * adjusted first...
  1606. */
  1607. #if NICHOLAS_ADD
  1608. //Nicholas fix bug
  1609. if(length > urb->transfer_buffer_length)
  1610. length = urb->transfer_buffer_length;
  1611. #endif
  1612. done = dma->channel_program(channel, qh->maxpacket,
  1613. channel->desired_mode,
  1614. buf, length);
  1615. if (!done) {
  1616. dma->channel_release(channel);
  1617. hw_ep->rx_channel = NULL;
  1618. channel = NULL;
  1619. val = musb_readw(epio, MUSB_RXCSR);
  1620. val &= ~(MUSB_RXCSR_DMAENAB
  1621. | MUSB_RXCSR_H_AUTOREQ
  1622. | MUSB_RXCSR_AUTOCLEAR);
  1623. musb_writew(epio, MUSB_RXCSR, val);
  1624. }
  1625. return done;
  1626. }
  1627. #else
  1628. static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1629. struct musb_hw_ep *hw_ep,
  1630. struct musb_qh *qh,
  1631. struct urb *urb,
  1632. size_t len)
  1633. {
  1634. return false;
  1635. }
  1636. static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1637. struct musb_hw_ep *hw_ep,
  1638. struct musb_qh *qh,
  1639. struct urb *urb,
  1640. size_t len,
  1641. u8 iso_err)
  1642. {
  1643. return false;
  1644. }
  1645. #endif
  1646. /*
  1647. * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
  1648. * and high-bandwidth IN transfer cases.
  1649. */
  1650. void musb_host_rx(struct musb *musb, u8 epnum)
  1651. {
  1652. struct urb *urb;
  1653. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1654. struct dma_controller *c = musb->dma_controller;
  1655. void __iomem *epio = hw_ep->regs;
  1656. struct musb_qh *qh = hw_ep->in_qh;
  1657. size_t xfer_len;
  1658. void __iomem *mbase = musb->mregs;
  1659. u16 rx_csr, val;
  1660. bool iso_err = false;
  1661. bool done = false;
  1662. u32 status;
  1663. struct dma_channel *dma;
  1664. unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
  1665. musb_ep_select(mbase, epnum);
  1666. urb = next_urb(qh);
  1667. dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
  1668. status = 0;
  1669. xfer_len = 0;
  1670. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1671. val = rx_csr;
  1672. if (unlikely(!urb)) {
  1673. /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
  1674. * usbtest #11 (unlinks) triggers it regularly, sometimes
  1675. * with fifo full. (Only with DMA??)
  1676. */
  1677. musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
  1678. epnum, val, musb_readw(epio, MUSB_RXCOUNT));
  1679. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1680. return;
  1681. }
  1682. trace_musb_urb_rx(musb, urb);
  1683. /* check for errors, concurrent stall & unlink is not really
  1684. * handled yet! */
  1685. if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
  1686. musb_dbg(musb, "RX end %d STALL", epnum);
  1687. /* stall; record URB status */
  1688. status = -EPIPE;
  1689. } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
  1690. musb_dbg(musb, "end %d RX proto error", epnum);
  1691. #if NICHOLAS_ADD
  1692. musb_ep_select(mbase, epnum);
  1693. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  1694. rx_csr &= ~MUSB_RXCSR_H_ERROR;
  1695. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1696. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1697. rx_csr |= MUSB_RXCSR_FLUSHFIFO;
  1698. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1699. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1700. rx_csr |= MUSB_RXCSR_H_REQPKT;
  1701. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1702. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1703. if(qh->intv_reg == UDISK_INTERVAL){
  1704. //Is U Disk
  1705. status = -ETIMEDOUT;
  1706. }
  1707. else{
  1708. //IS Phone
  1709. goto finish;
  1710. }
  1711. #else
  1712. status = -EPROTO;
  1713. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1714. rx_csr &= ~MUSB_RXCSR_H_ERROR;
  1715. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1716. #endif
  1717. } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
  1718. if (USB_ENDPOINT_XFER_ISOC != qh->type) {
  1719. musb_dbg(musb, "RX end %d NAK timeout", epnum);
  1720. /* NOTE: NAKing is *NOT* an error, so we want to
  1721. * continue. Except ... if there's a request for
  1722. * another QH, use that instead of starving it.
  1723. *
  1724. * Devices like Ethernet and serial adapters keep
  1725. * reads posted at all times, which will starve
  1726. * other devices without this logic.
  1727. */
  1728. if (usb_pipebulk(urb->pipe)
  1729. && qh->mux == 1
  1730. && !list_is_singular(&musb->in_bulk)) {
  1731. musb_bulk_nak_timeout(musb, hw_ep, 1);
  1732. return;
  1733. }
  1734. musb_ep_select(mbase, epnum);
  1735. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1736. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1737. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  1738. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1739. #if NICHOLAS_ADD
  1740. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1741. rx_csr |= MUSB_RXCSR_FLUSHFIFO;
  1742. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1743. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1744. rx_csr |= MUSB_RXCSR_H_REQPKT;
  1745. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1746. goto finish;
  1747. #else
  1748. goto finish;
  1749. #endif
  1750. } else {
  1751. musb_dbg(musb, "RX end %d ISO data error", epnum);
  1752. /* packet error reported later */
  1753. iso_err = true;
  1754. }
  1755. } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
  1756. musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
  1757. epnum);
  1758. status = -EPROTO;
  1759. }
  1760. /* faults abort the transfer */
  1761. if (status) {
  1762. /* clean up dma and collect transfer count */
  1763. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1764. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1765. musb->dma_controller->channel_abort(dma);
  1766. xfer_len = dma->actual_len;
  1767. }
  1768. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1769. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1770. done = true;
  1771. goto finish;
  1772. }
  1773. if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
  1774. /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
  1775. ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
  1776. goto finish;
  1777. }
  1778. /* thorough shutdown for now ... given more precise fault handling
  1779. * and better queueing support, we might keep a DMA pipeline going
  1780. * while processing this irq for earlier completions.
  1781. */
  1782. /* FIXME this is _way_ too much in-line logic for Mentor DMA */
  1783. if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
  1784. (rx_csr & MUSB_RXCSR_H_REQPKT)) {
  1785. /* REVISIT this happened for a while on some short reads...
  1786. * the cleanup still needs investigation... looks bad...
  1787. * and also duplicates dma cleanup code above ... plus,
  1788. * shouldn't this be the "half full" double buffer case?
  1789. */
  1790. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1791. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1792. musb->dma_controller->channel_abort(dma);
  1793. xfer_len = dma->actual_len;
  1794. done = true;
  1795. }
  1796. musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
  1797. xfer_len, dma ? ", dma" : "");
  1798. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1799. musb_ep_select(mbase, epnum);
  1800. musb_writew(epio, MUSB_RXCSR,
  1801. MUSB_RXCSR_H_WZC_BITS | rx_csr);
  1802. }
  1803. if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
  1804. xfer_len = dma->actual_len;
  1805. val &= ~(MUSB_RXCSR_DMAENAB
  1806. | MUSB_RXCSR_H_AUTOREQ
  1807. | MUSB_RXCSR_AUTOCLEAR
  1808. | MUSB_RXCSR_RXPKTRDY);
  1809. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1810. if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1811. musb_dma_cppi41(musb)) {
  1812. done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
  1813. musb_dbg(hw_ep->musb,
  1814. "ep %d dma %s, rxcsr %04x, rxcount %d",
  1815. epnum, done ? "off" : "reset",
  1816. musb_readw(epio, MUSB_RXCSR),
  1817. musb_readw(epio, MUSB_RXCOUNT));
  1818. } else {
  1819. done = true;
  1820. }
  1821. } else if (urb->status == -EINPROGRESS) {
  1822. /* if no errors, be sure a packet is ready for unloading */
  1823. if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
  1824. status = -EPROTO;
  1825. ERR("Rx interrupt with no errors or packet!\n");
  1826. /* FIXME this is another "SHOULD NEVER HAPPEN" */
  1827. /* SCRUB (RX) */
  1828. /* do the proper sequence to abort the transfer */
  1829. musb_ep_select(mbase, epnum);
  1830. val &= ~MUSB_RXCSR_H_REQPKT;
  1831. musb_writew(epio, MUSB_RXCSR, val);
  1832. goto finish;
  1833. }
  1834. /* we are expecting IN packets */
  1835. if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1836. musb_dma_cppi41(musb)) && dma) {
  1837. musb_dbg(hw_ep->musb,
  1838. "RX%d count %d, buffer 0x%llx len %d/%d",
  1839. epnum, musb_readw(epio, MUSB_RXCOUNT),
  1840. (unsigned long long) urb->transfer_dma
  1841. + urb->actual_length,
  1842. qh->offset,
  1843. urb->transfer_buffer_length);
  1844. if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
  1845. xfer_len, iso_err)) {
  1846. goto finish;
  1847. }
  1848. else {
  1849. #if NICHOLAS_ADD
  1850. dma = NULL;
  1851. #else
  1852. dev_err(musb->controller, "error: rx_dma failed\n");
  1853. #endif
  1854. }
  1855. }
  1856. if (!dma) {
  1857. unsigned int received_len;
  1858. /* Unmap the buffer so that CPU can use it */
  1859. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1860. /*
  1861. * We need to map sg if the transfer_buffer is
  1862. * NULL.
  1863. */
  1864. if (!urb->transfer_buffer) {
  1865. qh->use_sg = true;
  1866. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  1867. sg_flags);
  1868. }
  1869. if (qh->use_sg) {
  1870. if (!sg_miter_next(&qh->sg_miter)) {
  1871. dev_err(musb->controller, "error: sg list empty\n");
  1872. sg_miter_stop(&qh->sg_miter);
  1873. status = -EINVAL;
  1874. done = true;
  1875. goto finish;
  1876. }
  1877. urb->transfer_buffer = qh->sg_miter.addr;
  1878. received_len = urb->actual_length;
  1879. qh->offset = 0x0;
  1880. done = musb_host_packet_rx(musb, urb, epnum,
  1881. iso_err);
  1882. /* Calculate the number of bytes received */
  1883. received_len = urb->actual_length -
  1884. received_len;
  1885. qh->sg_miter.consumed = received_len;
  1886. sg_miter_stop(&qh->sg_miter);
  1887. } else {
  1888. done = musb_host_packet_rx(musb, urb,
  1889. epnum, iso_err);
  1890. }
  1891. musb_dbg(musb, "read %spacket", done ? "last " : "");
  1892. }
  1893. }
  1894. finish:
  1895. urb->actual_length += xfer_len;
  1896. qh->offset += xfer_len;
  1897. if (done) {
  1898. if (qh->use_sg) {
  1899. qh->use_sg = false;
  1900. urb->transfer_buffer = NULL;
  1901. }
  1902. if (urb->status == -EINPROGRESS)
  1903. urb->status = status;
  1904. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
  1905. }
  1906. }
  1907. /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
  1908. * the software schedule associates multiple such nodes with a given
  1909. * host side hardware endpoint + direction; scheduling may activate
  1910. * that hardware endpoint.
  1911. */
  1912. static int musb_schedule(
  1913. struct musb *musb,
  1914. struct musb_qh *qh,
  1915. int is_in)
  1916. {
  1917. int idle = 0;
  1918. int best_diff;
  1919. int best_end, epnum;
  1920. struct musb_hw_ep *hw_ep = NULL;
  1921. struct list_head *head = NULL;
  1922. u8 toggle;
  1923. u8 txtype;
  1924. struct urb *urb = next_urb(qh);
  1925. /* use fixed hardware for control and bulk */
  1926. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  1927. head = &musb->control;
  1928. hw_ep = musb->control_ep;
  1929. goto success;
  1930. }
  1931. /* else, periodic transfers get muxed to other endpoints */
  1932. /*
  1933. * We know this qh hasn't been scheduled, so all we need to do
  1934. * is choose which hardware endpoint to put it on ...
  1935. *
  1936. * REVISIT what we really want here is a regular schedule tree
  1937. * like e.g. OHCI uses.
  1938. */
  1939. best_diff = 4096 * 2;
  1940. best_end = -1;
  1941. for (epnum = 1, hw_ep = musb->endpoints + 1;
  1942. (epnum < musb->nr_endpoints) && (epnum < MUSB_C_NUM_EPS);
  1943. epnum++, hw_ep++) {
  1944. int diff;
  1945. if (musb_ep_get_qh(hw_ep, is_in) != NULL)
  1946. continue;
  1947. if (hw_ep == musb->bulk_ep)
  1948. continue;
  1949. if (is_in)
  1950. diff = hw_ep->max_packet_sz_rx;
  1951. else
  1952. diff = hw_ep->max_packet_sz_tx;
  1953. diff -= (qh->maxpacket * qh->hb_mult);
  1954. if (diff >= 0 && best_diff > diff) {
  1955. /*
  1956. * Mentor controller has a bug in that if we schedule
  1957. * a BULK Tx transfer on an endpoint that had earlier
  1958. * handled ISOC then the BULK transfer has to start on
  1959. * a zero toggle. If the BULK transfer starts on a 1
  1960. * toggle then this transfer will fail as the mentor
  1961. * controller starts the Bulk transfer on a 0 toggle
  1962. * irrespective of the programming of the toggle bits
  1963. * in the TXCSR register. Check for this condition
  1964. * while allocating the EP for a Tx Bulk transfer. If
  1965. * so skip this EP.
  1966. */
  1967. hw_ep = musb->endpoints + epnum;
  1968. toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
  1969. txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
  1970. >> 4) & 0x3;
  1971. if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
  1972. toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
  1973. continue;
  1974. best_diff = diff;
  1975. best_end = epnum;
  1976. }
  1977. }
  1978. /* use bulk reserved ep1 if no other ep is free */
  1979. if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
  1980. hw_ep = musb->bulk_ep;
  1981. if (is_in)
  1982. head = &musb->in_bulk;
  1983. else
  1984. head = &musb->out_bulk;
  1985. /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
  1986. * multiplexed. This scheme does not work in high speed to full
  1987. * speed scenario as NAK interrupts are not coming from a
  1988. * full speed device connected to a high speed device.
  1989. * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
  1990. * 4 (8 frame or 8ms) for FS device.
  1991. */
  1992. #if NICHOLAS_ADD
  1993. if (is_in && qh->dev)
  1994. #else
  1995. if (qh->dev)
  1996. #endif
  1997. qh->intv_reg =
  1998. (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
  1999. goto success;
  2000. } else if (best_end < 0) {
  2001. dev_err(musb->controller,
  2002. "%s hwep alloc failed for %dx%d\n",
  2003. musb_ep_xfertype_string(qh->type),
  2004. qh->hb_mult, qh->maxpacket);
  2005. return -ENOSPC;
  2006. }
  2007. idle = 1;
  2008. qh->mux = 0;
  2009. hw_ep = musb->endpoints + best_end;
  2010. musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
  2011. success:
  2012. if (head) {
  2013. idle = list_empty(head);
  2014. list_add_tail(&qh->ring, head);
  2015. qh->mux = 1;
  2016. }
  2017. qh->hw_ep = hw_ep;
  2018. qh->hep->hcpriv = qh;
  2019. if (idle)
  2020. musb_start_urb(musb, is_in, qh);
  2021. return 0;
  2022. }
  2023. static int musb_urb_enqueue(
  2024. struct usb_hcd *hcd,
  2025. struct urb *urb,
  2026. gfp_t mem_flags)
  2027. {
  2028. unsigned long flags;
  2029. struct musb *musb = hcd_to_musb(hcd);
  2030. struct usb_host_endpoint *hep = urb->ep;
  2031. struct musb_qh *qh;
  2032. struct usb_endpoint_descriptor *epd = &hep->desc;
  2033. int ret;
  2034. unsigned type_reg;
  2035. #if NICHOLAS_ADD
  2036. u8 interval = 0;
  2037. #else
  2038. unsigned interval;
  2039. #endif
  2040. /* host role must be active */
  2041. if (!is_host_active(musb) || !musb->is_active)
  2042. return -ENODEV;
  2043. trace_musb_urb_enq(musb, urb);
  2044. spin_lock_irqsave(&musb->lock, flags);
  2045. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  2046. qh = ret ? NULL : hep->hcpriv;
  2047. if (qh)
  2048. urb->hcpriv = qh;
  2049. spin_unlock_irqrestore(&musb->lock, flags);
  2050. /* DMA mapping was already done, if needed, and this urb is on
  2051. * hep->urb_list now ... so we're done, unless hep wasn't yet
  2052. * scheduled onto a live qh.
  2053. *
  2054. * REVISIT best to keep hep->hcpriv valid until the endpoint gets
  2055. * disabled, testing for empty qh->ring and avoiding qh setup costs
  2056. * except for the first urb queued after a config change.
  2057. */
  2058. if (qh || ret)
  2059. return ret;
  2060. /* Allocate and initialize qh, minimizing the work done each time
  2061. * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
  2062. *
  2063. * REVISIT consider a dedicated qh kmem_cache, so it's harder
  2064. * for bugs in other kernel code to break this driver...
  2065. */
  2066. qh = kzalloc(sizeof *qh, mem_flags);
  2067. if (!qh) {
  2068. spin_lock_irqsave(&musb->lock, flags);
  2069. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2070. spin_unlock_irqrestore(&musb->lock, flags);
  2071. return -ENOMEM;
  2072. }
  2073. qh->hep = hep;
  2074. qh->dev = urb->dev;
  2075. INIT_LIST_HEAD(&qh->ring);
  2076. qh->is_ready = 1;
  2077. qh->maxpacket = usb_endpoint_maxp(epd);
  2078. qh->type = usb_endpoint_type(epd);
  2079. /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
  2080. * Some musb cores don't support high bandwidth ISO transfers; and
  2081. * we don't (yet!) support high bandwidth interrupt transfers.
  2082. */
  2083. qh->hb_mult = usb_endpoint_maxp_mult(epd);
  2084. if (qh->hb_mult > 1) {
  2085. int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
  2086. if (ok)
  2087. ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
  2088. || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
  2089. if (!ok) {
  2090. dev_err(musb->controller,
  2091. "high bandwidth %s (%dx%d) not supported\n",
  2092. musb_ep_xfertype_string(qh->type),
  2093. qh->hb_mult, qh->maxpacket & 0x7ff);
  2094. ret = -EMSGSIZE;
  2095. goto done;
  2096. }
  2097. qh->maxpacket &= 0x7ff;
  2098. }
  2099. qh->epnum = usb_endpoint_num(epd);
  2100. /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
  2101. qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
  2102. /* precompute rxtype/txtype/type0 register */
  2103. type_reg = (qh->type << 4) | qh->epnum;
  2104. switch (urb->dev->speed) {
  2105. case USB_SPEED_LOW:
  2106. type_reg |= 0xc0;
  2107. break;
  2108. case USB_SPEED_FULL:
  2109. type_reg |= 0x80;
  2110. break;
  2111. default:
  2112. type_reg |= 0x40;
  2113. }
  2114. qh->type_reg = type_reg;
  2115. /* Precompute RXINTERVAL/TXINTERVAL register */
  2116. switch (qh->type) {
  2117. case USB_ENDPOINT_XFER_INT:
  2118. /*
  2119. * Full/low speeds use the linear encoding,
  2120. * high speed uses the logarithmic encoding.
  2121. */
  2122. if (urb->dev->speed <= USB_SPEED_FULL) {
  2123. interval = max_t(u8, epd->bInterval, 1);
  2124. break;
  2125. }
  2126. /* FALLTHROUGH */
  2127. case USB_ENDPOINT_XFER_ISOC:
  2128. /* ISO always uses logarithmic encoding */
  2129. interval = min_t(u8, epd->bInterval, 16);
  2130. break;
  2131. default:
  2132. /* REVISIT we actually want to use NAK limits, hinting to the
  2133. * transfer scheduling logic to try some other qh, e.g. try
  2134. * for 2 msec first:
  2135. *
  2136. * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
  2137. *
  2138. * The downside of disabling this is that transfer scheduling
  2139. * gets VERY unfair for nonperiodic transfers; a misbehaving
  2140. * peripheral could make that hurt. That's perfectly normal
  2141. * for reads from network or serial adapters ... so we have
  2142. * partial NAKlimit support for bulk RX.
  2143. *
  2144. * The upside of disabling it is simpler transfer scheduling.
  2145. */
  2146. #if NICHOLAS_ADD
  2147. if(urb->dev->actconfig)
  2148. {
  2149. struct usb_interface *intf = urb->dev->actconfig->interface[0];
  2150. if(intf)
  2151. {
  2152. struct usb_host_interface *cur_altsetting = intf->cur_altsetting;
  2153. if(cur_altsetting)
  2154. {
  2155. struct usb_interface_descriptor *desc = &cur_altsetting->desc;;
  2156. if(desc)
  2157. {
  2158. if(desc->bInterfaceClass == 0x8) //Is U Disk
  2159. interval = UDISK_INTERVAL;
  2160. else
  2161. interval = 0;
  2162. }
  2163. }
  2164. }
  2165. }
  2166. #else
  2167. interval = 0;
  2168. #endif
  2169. }
  2170. qh->intv_reg = interval;
  2171. /* precompute addressing for external hub/tt ports */
  2172. if (musb->is_multipoint) {
  2173. struct usb_device *parent = urb->dev->parent;
  2174. if (parent != hcd->self.root_hub) {
  2175. qh->h_addr_reg = (u8) parent->devnum;
  2176. /* set up tt info if needed */
  2177. if (urb->dev->tt) {
  2178. qh->h_port_reg = (u8) urb->dev->ttport;
  2179. if (urb->dev->tt->hub)
  2180. qh->h_addr_reg =
  2181. (u8) urb->dev->tt->hub->devnum;
  2182. if (urb->dev->tt->multi)
  2183. qh->h_addr_reg |= 0x80;
  2184. }
  2185. }
  2186. }
  2187. /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
  2188. * until we get real dma queues (with an entry for each urb/buffer),
  2189. * we only have work to do in the former case.
  2190. */
  2191. spin_lock_irqsave(&musb->lock, flags);
  2192. if (hep->hcpriv || !next_urb(qh)) {
  2193. /* some concurrent activity submitted another urb to hep...
  2194. * odd, rare, error prone, but legal.
  2195. */
  2196. kfree(qh);
  2197. qh = NULL;
  2198. ret = 0;
  2199. } else
  2200. ret = musb_schedule(musb, qh,
  2201. epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
  2202. if (ret == 0) {
  2203. urb->hcpriv = qh;
  2204. /* FIXME set urb->start_frame for iso/intr, it's tested in
  2205. * musb_start_urb(), but otherwise only konicawc cares ...
  2206. */
  2207. }
  2208. spin_unlock_irqrestore(&musb->lock, flags);
  2209. done:
  2210. if (ret != 0) {
  2211. spin_lock_irqsave(&musb->lock, flags);
  2212. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2213. spin_unlock_irqrestore(&musb->lock, flags);
  2214. kfree(qh);
  2215. }
  2216. return ret;
  2217. }
  2218. /*
  2219. * abort a transfer that's at the head of a hardware queue.
  2220. * called with controller locked, irqs blocked
  2221. * that hardware queue advances to the next transfer, unless prevented
  2222. */
  2223. static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
  2224. {
  2225. struct musb_hw_ep *ep = qh->hw_ep;
  2226. struct musb *musb = ep->musb;
  2227. void __iomem *epio = ep->regs;
  2228. unsigned hw_end = ep->epnum;
  2229. void __iomem *regs = ep->musb->mregs;
  2230. int is_in = usb_pipein(urb->pipe);
  2231. int status = 0;
  2232. u16 csr;
  2233. struct dma_channel *dma = NULL;
  2234. musb_ep_select(regs, hw_end);
  2235. if (is_dma_capable()) {
  2236. dma = is_in ? ep->rx_channel : ep->tx_channel;
  2237. if (dma) {
  2238. status = ep->musb->dma_controller->channel_abort(dma);
  2239. musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
  2240. is_in ? 'R' : 'T', ep->epnum,
  2241. urb, status);
  2242. urb->actual_length += dma->actual_len;
  2243. }
  2244. }
  2245. /* turn off DMA requests, discard state, stop polling ... */
  2246. if (ep->epnum && is_in) {
  2247. /* giveback saves bulk toggle */
  2248. csr = musb_h_flush_rxfifo(ep, 0);
  2249. /* clear the endpoint's irq status here to avoid bogus irqs */
  2250. if (is_dma_capable() && dma)
  2251. musb_platform_clear_ep_rxintr(musb, ep->epnum);
  2252. } else if (ep->epnum) {
  2253. musb_h_tx_flush_fifo(ep);
  2254. csr = musb_readw(epio, MUSB_TXCSR);
  2255. csr &= ~(MUSB_TXCSR_AUTOSET
  2256. | MUSB_TXCSR_DMAENAB
  2257. | MUSB_TXCSR_H_RXSTALL
  2258. | MUSB_TXCSR_H_NAKTIMEOUT
  2259. | MUSB_TXCSR_H_ERROR
  2260. | MUSB_TXCSR_TXPKTRDY);
  2261. musb_writew(epio, MUSB_TXCSR, csr);
  2262. /* REVISIT may need to clear FLUSHFIFO ... */
  2263. musb_writew(epio, MUSB_TXCSR, csr);
  2264. /* flush cpu writebuffer */
  2265. csr = musb_readw(epio, MUSB_TXCSR);
  2266. } else {
  2267. musb_h_ep0_flush_fifo(ep);
  2268. }
  2269. if (status == 0)
  2270. musb_advance_schedule(ep->musb, urb, ep, is_in);
  2271. return status;
  2272. }
  2273. static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2274. {
  2275. struct musb *musb = hcd_to_musb(hcd);
  2276. struct musb_qh *qh;
  2277. unsigned long flags;
  2278. int is_in = usb_pipein(urb->pipe);
  2279. int ret;
  2280. trace_musb_urb_deq(musb, urb);
  2281. spin_lock_irqsave(&musb->lock, flags);
  2282. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  2283. if (ret)
  2284. goto done;
  2285. qh = urb->hcpriv;
  2286. if (!qh)
  2287. goto done;
  2288. /*
  2289. * Any URB not actively programmed into endpoint hardware can be
  2290. * immediately given back; that's any URB not at the head of an
  2291. * endpoint queue, unless someday we get real DMA queues. And even
  2292. * if it's at the head, it might not be known to the hardware...
  2293. *
  2294. * Otherwise abort current transfer, pending DMA, etc.; urb->status
  2295. * has already been updated. This is a synchronous abort; it'd be
  2296. * OK to hold off until after some IRQ, though.
  2297. *
  2298. * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
  2299. */
  2300. if (!qh->is_ready
  2301. || urb->urb_list.prev != &qh->hep->urb_list
  2302. || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
  2303. int ready = qh->is_ready;
  2304. qh->is_ready = 0;
  2305. musb_giveback(musb, urb, 0);
  2306. qh->is_ready = ready;
  2307. /* If nothing else (usually musb_giveback) is using it
  2308. * and its URB list has emptied, recycle this qh.
  2309. */
  2310. if (ready && list_empty(&qh->hep->urb_list)) {
  2311. qh->hep->hcpriv = NULL;
  2312. list_del(&qh->ring);
  2313. kfree(qh);
  2314. }
  2315. } else
  2316. ret = musb_cleanup_urb(urb, qh);
  2317. done:
  2318. spin_unlock_irqrestore(&musb->lock, flags);
  2319. return ret;
  2320. }
  2321. /* disable an endpoint */
  2322. static void
  2323. musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  2324. {
  2325. u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
  2326. unsigned long flags;
  2327. struct musb *musb = hcd_to_musb(hcd);
  2328. struct musb_qh *qh;
  2329. struct urb *urb;
  2330. spin_lock_irqsave(&musb->lock, flags);
  2331. qh = hep->hcpriv;
  2332. if (qh == NULL)
  2333. goto exit;
  2334. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  2335. /* Kick the first URB off the hardware, if needed */
  2336. qh->is_ready = 0;
  2337. if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
  2338. urb = next_urb(qh);
  2339. /* make software (then hardware) stop ASAP */
  2340. if (!urb->unlinked)
  2341. urb->status = -ESHUTDOWN;
  2342. /* cleanup */
  2343. musb_cleanup_urb(urb, qh);
  2344. /* Then nuke all the others ... and advance the
  2345. * queue on hw_ep (e.g. bulk ring) when we're done.
  2346. */
  2347. while (!list_empty(&hep->urb_list)) {
  2348. urb = next_urb(qh);
  2349. urb->status = -ESHUTDOWN;
  2350. musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
  2351. }
  2352. } else {
  2353. /* Just empty the queue; the hardware is busy with
  2354. * other transfers, and since !qh->is_ready nothing
  2355. * will activate any of these as it advances.
  2356. */
  2357. while (!list_empty(&hep->urb_list))
  2358. musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
  2359. hep->hcpriv = NULL;
  2360. list_del(&qh->ring);
  2361. kfree(qh);
  2362. }
  2363. exit:
  2364. spin_unlock_irqrestore(&musb->lock, flags);
  2365. }
  2366. static int musb_h_get_frame_number(struct usb_hcd *hcd)
  2367. {
  2368. struct musb *musb = hcd_to_musb(hcd);
  2369. return musb_readw(musb->mregs, MUSB_FRAME);
  2370. }
  2371. static int musb_h_start(struct usb_hcd *hcd)
  2372. {
  2373. struct musb *musb = hcd_to_musb(hcd);
  2374. /* NOTE: musb_start() is called when the hub driver turns
  2375. * on port power, or when (OTG) peripheral starts.
  2376. */
  2377. hcd->state = HC_STATE_RUNNING;
  2378. musb->port1_status = 0;
  2379. return 0;
  2380. }
  2381. static void musb_h_stop(struct usb_hcd *hcd)
  2382. {
  2383. musb_stop(hcd_to_musb(hcd));
  2384. hcd->state = HC_STATE_HALT;
  2385. }
  2386. static int musb_bus_suspend(struct usb_hcd *hcd)
  2387. {
  2388. struct musb *musb = hcd_to_musb(hcd);
  2389. u8 devctl;
  2390. int ret;
  2391. ret = musb_port_suspend(musb, true);
  2392. if (ret)
  2393. return ret;
  2394. if (!is_host_active(musb))
  2395. return 0;
  2396. switch (musb->xceiv->otg->state) {
  2397. case OTG_STATE_A_SUSPEND:
  2398. return 0;
  2399. case OTG_STATE_A_WAIT_VRISE:
  2400. /* ID could be grounded even if there's no device
  2401. * on the other end of the cable. NOTE that the
  2402. * A_WAIT_VRISE timers are messy with MUSB...
  2403. */
  2404. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2405. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  2406. musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
  2407. break;
  2408. default:
  2409. break;
  2410. }
  2411. if (musb->is_active) {
  2412. WARNING("trying to suspend as %s while active\n",
  2413. usb_otg_state_string(musb->xceiv->otg->state));
  2414. return -EBUSY;
  2415. } else
  2416. return 0;
  2417. }
  2418. static int musb_bus_resume(struct usb_hcd *hcd)
  2419. {
  2420. struct musb *musb = hcd_to_musb(hcd);
  2421. if (musb->config &&
  2422. musb->config->host_port_deassert_reset_at_resume)
  2423. musb_port_reset(musb, false);
  2424. return 0;
  2425. }
  2426. #ifndef CONFIG_MUSB_PIO_ONLY
  2427. #define MUSB_USB_DMA_ALIGN 4
  2428. struct musb_temp_buffer {
  2429. void *kmalloc_ptr;
  2430. void *old_xfer_buffer;
  2431. u8 data[0];
  2432. };
  2433. static void musb_free_temp_buffer(struct urb *urb)
  2434. {
  2435. enum dma_data_direction dir;
  2436. struct musb_temp_buffer *temp;
  2437. size_t length;
  2438. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2439. return;
  2440. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2441. temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
  2442. data);
  2443. if (dir == DMA_FROM_DEVICE) {
  2444. if (usb_pipeisoc(urb->pipe))
  2445. length = urb->transfer_buffer_length;
  2446. else
  2447. length = urb->actual_length;
  2448. memcpy(temp->old_xfer_buffer, temp->data, length);
  2449. }
  2450. urb->transfer_buffer = temp->old_xfer_buffer;
  2451. kfree(temp->kmalloc_ptr);
  2452. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2453. }
  2454. static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  2455. {
  2456. enum dma_data_direction dir;
  2457. struct musb_temp_buffer *temp;
  2458. void *kmalloc_ptr;
  2459. size_t kmalloc_size;
  2460. if (urb->num_sgs || urb->sg ||
  2461. urb->transfer_buffer_length == 0 ||
  2462. !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
  2463. return 0;
  2464. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2465. /* Allocate a buffer with enough padding for alignment */
  2466. kmalloc_size = urb->transfer_buffer_length +
  2467. sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
  2468. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2469. if (!kmalloc_ptr)
  2470. return -ENOMEM;
  2471. /* Position our struct temp_buffer such that data is aligned */
  2472. temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
  2473. temp->kmalloc_ptr = kmalloc_ptr;
  2474. temp->old_xfer_buffer = urb->transfer_buffer;
  2475. if (dir == DMA_TO_DEVICE)
  2476. memcpy(temp->data, urb->transfer_buffer,
  2477. urb->transfer_buffer_length);
  2478. urb->transfer_buffer = temp->data;
  2479. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2480. return 0;
  2481. }
  2482. static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2483. gfp_t mem_flags)
  2484. {
  2485. struct musb *musb = hcd_to_musb(hcd);
  2486. int ret;
  2487. /*
  2488. * The DMA engine in RTL1.8 and above cannot handle
  2489. * DMA addresses that are not aligned to a 4 byte boundary.
  2490. * For such engine implemented (un)map_urb_for_dma hooks.
  2491. * Do not use these hooks for RTL<1.8
  2492. */
  2493. if (musb->hwvers < MUSB_HWVERS_1800)
  2494. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2495. ret = musb_alloc_temp_buffer(urb, mem_flags);
  2496. if (ret)
  2497. return ret;
  2498. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2499. if (ret)
  2500. musb_free_temp_buffer(urb);
  2501. return ret;
  2502. }
  2503. static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2504. {
  2505. struct musb *musb = hcd_to_musb(hcd);
  2506. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2507. /* Do not use this hook for RTL<1.8 (see description above) */
  2508. if (musb->hwvers < MUSB_HWVERS_1800)
  2509. return;
  2510. musb_free_temp_buffer(urb);
  2511. }
  2512. #endif /* !CONFIG_MUSB_PIO_ONLY */
  2513. static int musb_reset_usb_controller(struct usb_hcd *hcd, int mode)
  2514. {
  2515. struct musb *musb = NULL;
  2516. int ret = -1;
  2517. (void)mode;
  2518. if (NULL == hcd)
  2519. return ret;
  2520. musb = hcd_to_musb(hcd);
  2521. if (NULL == musb) {
  2522. return ret;
  2523. }
  2524. //printk("\n######### %s start at line:%d musb:%x\n\n", __func__, __LINE__, (int)musb);
  2525. mod_timer(&musb->musb_reset_timer, jiffies + msecs_to_jiffies(3000));
  2526. return 0;
  2527. }
  2528. static const struct hc_driver musb_hc_driver = {
  2529. .description = "musb-hcd",
  2530. .product_desc = "MUSB HDRC host driver",
  2531. .hcd_priv_size = sizeof(struct musb *),
  2532. .flags = HCD_USB2 | HCD_MEMORY,
  2533. /* not using irq handler or reset hooks from usbcore, since
  2534. * those must be shared with peripheral code for OTG configs
  2535. */
  2536. .start = musb_h_start,
  2537. .stop = musb_h_stop,
  2538. .get_frame_number = musb_h_get_frame_number,
  2539. .urb_enqueue = musb_urb_enqueue,
  2540. .urb_dequeue = musb_urb_dequeue,
  2541. .endpoint_disable = musb_h_disable,
  2542. #ifndef CONFIG_MUSB_PIO_ONLY
  2543. .map_urb_for_dma = musb_map_urb_for_dma,
  2544. .unmap_urb_for_dma = musb_unmap_urb_for_dma,
  2545. #endif
  2546. .hub_status_data = musb_hub_status_data,
  2547. .hub_control = musb_hub_control,
  2548. .bus_suspend = musb_bus_suspend,
  2549. .bus_resume = musb_bus_resume,
  2550. .reset_usb_controller = musb_reset_usb_controller
  2551. /* .start_port_reset = NULL, */
  2552. /* .hub_irq_enable = NULL, */
  2553. };
  2554. int musb_host_alloc(struct musb *musb)
  2555. {
  2556. struct device *dev = musb->controller;
  2557. /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
  2558. musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
  2559. if (!musb->hcd)
  2560. return -EINVAL;
  2561. *musb->hcd->hcd_priv = (unsigned long) musb;
  2562. musb->hcd->self.uses_pio_for_control = 1;
  2563. musb->hcd->uses_new_polling = 1;
  2564. musb->hcd->has_tt = 1;
  2565. return 0;
  2566. }
  2567. void musb_host_cleanup(struct musb *musb)
  2568. {
  2569. if (musb->port_mode == MUSB_PERIPHERAL)
  2570. return;
  2571. usb_remove_hcd(musb->hcd);
  2572. }
  2573. void musb_host_free(struct musb *musb)
  2574. {
  2575. usb_put_hcd(musb->hcd);
  2576. }
  2577. int musb_host_setup(struct musb *musb, int power_budget)
  2578. {
  2579. int ret;
  2580. struct usb_hcd *hcd = musb->hcd;
  2581. if (musb->port_mode == MUSB_HOST) {
  2582. MUSB_HST_MODE(musb);
  2583. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  2584. }
  2585. otg_set_host(musb->xceiv->otg, &hcd->self);
  2586. /* don't support otg protocols */
  2587. hcd->self.otg_port = 0;
  2588. musb->xceiv->otg->host = &hcd->self;
  2589. hcd->power_budget = 2 * (power_budget ? : 250);
  2590. hcd->skip_phy_initialization = 1;
  2591. ret = usb_add_hcd(hcd, 0, 0);
  2592. if (ret < 0)
  2593. return ret;
  2594. device_wakeup_enable(hcd->self.controller);
  2595. return 0;
  2596. }
  2597. void musb_host_resume_root_hub(struct musb *musb)
  2598. {
  2599. usb_hcd_resume_root_hub(musb->hcd);
  2600. }
  2601. void musb_host_poke_root_hub(struct musb *musb)
  2602. {
  2603. MUSB_HST_MODE(musb);
  2604. if (musb->hcd->status_urb)
  2605. usb_hcd_poll_rh_status(musb->hcd);
  2606. else
  2607. usb_hcd_resume_root_hub(musb->hcd);
  2608. }