musb_host.c 78 KB

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