musb_host.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*
  2. * MUSB OTG driver host support
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  25. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  28. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/delay.h>
  37. #include <linux/sched.h>
  38. #include <linux/slab.h>
  39. #include <linux/errno.h>
  40. #include <linux/init.h>
  41. #include <linux/list.h>
  42. #include "musb_core.h"
  43. #include "musb_host.h"
  44. /* MUSB HOST status 22-mar-2006
  45. *
  46. * - There's still lots of partial code duplication for fault paths, so
  47. * they aren't handled as consistently as they need to be.
  48. *
  49. * - PIO mostly behaved when last tested.
  50. * + including ep0, with all usbtest cases 9, 10
  51. * + usbtest 14 (ep0out) doesn't seem to run at all
  52. * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
  53. * configurations, but otherwise double buffering passes basic tests.
  54. * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
  55. *
  56. * - DMA (CPPI) ... partially behaves, not currently recommended
  57. * + about 1/15 the speed of typical EHCI implementations (PCI)
  58. * + RX, all too often reqpkt seems to misbehave after tx
  59. * + TX, no known issues (other than evident silicon issue)
  60. *
  61. * - DMA (Mentor/OMAP) ...has at least toggle update problems
  62. *
  63. * - Still no traffic scheduling code to make NAKing for bulk or control
  64. * transfers unable to starve other requests; or to make efficient use
  65. * of hardware with periodic transfers. (Note that network drivers
  66. * commonly post bulk reads that stay pending for a long time; these
  67. * would make very visible trouble.)
  68. *
  69. * - Not tested with HNP, but some SRP paths seem to behave.
  70. *
  71. * NOTE 24-August-2006:
  72. *
  73. * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
  74. * extra endpoint for periodic use enabling hub + keybd + mouse. That
  75. * mostly works, except that with "usbnet" it's easy to trigger cases
  76. * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
  77. * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
  78. * although ARP RX wins. (That test was done with a full speed link.)
  79. */
  80. /*
  81. * NOTE on endpoint usage:
  82. *
  83. * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
  84. * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
  85. *
  86. * (Yes, bulk _could_ use more of the endpoints than that, and would even
  87. * benefit from it ... one remote device may easily be NAKing while others
  88. * need to perform transfers in that same direction. The same thing could
  89. * be done in software though, assuming dma cooperates.)
  90. *
  91. * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
  92. * So far that scheduling is both dumb and optimistic: the endpoint will be
  93. * "claimed" until its software queue is no longer refilled. No multiplexing
  94. * of transfers between endpoints, or anything clever.
  95. */
  96. static void musb_ep_program(struct musb *musb, u8 epnum,
  97. struct urb *urb, unsigned int nOut,
  98. u8 *buf, u32 len);
  99. /*
  100. * Clear TX fifo. Needed to avoid BABBLE errors.
  101. */
  102. static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
  103. {
  104. void __iomem *epio = ep->regs;
  105. u16 csr;
  106. u16 lastcsr = 0;
  107. int retries = 1000;
  108. csr = musb_readw(epio, MUSB_TXCSR);
  109. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  110. if (csr != lastcsr)
  111. DBG(3, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
  112. lastcsr = csr;
  113. csr |= MUSB_TXCSR_FLUSHFIFO;
  114. musb_writew(epio, MUSB_TXCSR, csr);
  115. csr = musb_readw(epio, MUSB_TXCSR);
  116. if (WARN(retries-- < 1,
  117. "Could not flush host TX%d fifo: csr: %04x\n",
  118. ep->epnum, csr))
  119. return;
  120. mdelay(1);
  121. }
  122. }
  123. /*
  124. * Start transmit. Caller is responsible for locking shared resources.
  125. * musb must be locked.
  126. */
  127. static inline void musb_h_tx_start(struct musb_hw_ep *ep)
  128. {
  129. u16 txcsr;
  130. /* NOTE: no locks here; caller should lock and select EP */
  131. if (ep->epnum) {
  132. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  133. txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
  134. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  135. } else {
  136. txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
  137. musb_writew(ep->regs, MUSB_CSR0, txcsr);
  138. }
  139. }
  140. static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
  141. {
  142. u16 txcsr;
  143. /* NOTE: no locks here; caller should lock and select EP */
  144. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  145. txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
  146. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  147. }
  148. /*
  149. * Start the URB at the front of an endpoint's queue
  150. * end must be claimed from the caller.
  151. *
  152. * Context: controller locked, irqs blocked
  153. */
  154. static void
  155. musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
  156. {
  157. u16 frame;
  158. u32 len;
  159. void *buf;
  160. void __iomem *mbase = musb->mregs;
  161. struct urb *urb = next_urb(qh);
  162. struct musb_hw_ep *hw_ep = qh->hw_ep;
  163. unsigned pipe = urb->pipe;
  164. u8 address = usb_pipedevice(pipe);
  165. int epnum = hw_ep->epnum;
  166. /* initialize software qh state */
  167. qh->offset = 0;
  168. qh->segsize = 0;
  169. /* gather right source of data */
  170. switch (qh->type) {
  171. case USB_ENDPOINT_XFER_CONTROL:
  172. /* control transfers always start with SETUP */
  173. is_in = 0;
  174. hw_ep->out_qh = qh;
  175. musb->ep0_stage = MUSB_EP0_START;
  176. buf = urb->setup_packet;
  177. len = 8;
  178. break;
  179. case USB_ENDPOINT_XFER_ISOC:
  180. qh->iso_idx = 0;
  181. qh->frame = 0;
  182. buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
  183. len = urb->iso_frame_desc[0].length;
  184. break;
  185. default: /* bulk, interrupt */
  186. buf = urb->transfer_buffer;
  187. len = urb->transfer_buffer_length;
  188. }
  189. DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
  190. qh, urb, address, qh->epnum,
  191. is_in ? "in" : "out",
  192. ({char *s; switch (qh->type) {
  193. case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
  194. case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
  195. case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
  196. default: s = "-intr"; break;
  197. }; s; }),
  198. epnum, buf, len);
  199. /* Configure endpoint */
  200. if (is_in || hw_ep->is_shared_fifo)
  201. hw_ep->in_qh = qh;
  202. else
  203. hw_ep->out_qh = qh;
  204. musb_ep_program(musb, epnum, urb, !is_in, buf, len);
  205. /* transmit may have more work: start it when it is time */
  206. if (is_in)
  207. return;
  208. /* determine if the time is right for a periodic transfer */
  209. switch (qh->type) {
  210. case USB_ENDPOINT_XFER_ISOC:
  211. case USB_ENDPOINT_XFER_INT:
  212. DBG(3, "check whether there's still time for periodic Tx\n");
  213. qh->iso_idx = 0;
  214. frame = musb_readw(mbase, MUSB_FRAME);
  215. /* FIXME this doesn't implement that scheduling policy ...
  216. * or handle framecounter wrapping
  217. */
  218. if ((urb->transfer_flags & URB_ISO_ASAP)
  219. || (frame >= urb->start_frame)) {
  220. /* REVISIT the SOF irq handler shouldn't duplicate
  221. * this code; and we don't init urb->start_frame...
  222. */
  223. qh->frame = 0;
  224. goto start;
  225. } else {
  226. qh->frame = urb->start_frame;
  227. /* enable SOF interrupt so we can count down */
  228. DBG(1, "SOF for %d\n", epnum);
  229. #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
  230. musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
  231. #endif
  232. }
  233. break;
  234. default:
  235. start:
  236. DBG(4, "Start TX%d %s\n", epnum,
  237. hw_ep->tx_channel ? "dma" : "pio");
  238. if (!hw_ep->tx_channel)
  239. musb_h_tx_start(hw_ep);
  240. else if (is_cppi_enabled() || tusb_dma_omap())
  241. cppi_host_txdma_start(hw_ep);
  242. }
  243. }
  244. /* caller owns controller lock, irqs are blocked */
  245. static void
  246. __musb_giveback(struct musb *musb, struct urb *urb, int status)
  247. __releases(musb->lock)
  248. __acquires(musb->lock)
  249. {
  250. DBG(({ int level; switch (status) {
  251. case 0:
  252. level = 4;
  253. break;
  254. /* common/boring faults */
  255. case -EREMOTEIO:
  256. case -ESHUTDOWN:
  257. case -ECONNRESET:
  258. case -EPIPE:
  259. level = 3;
  260. break;
  261. default:
  262. level = 2;
  263. break;
  264. }; level; }),
  265. "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
  266. urb, urb->complete, status,
  267. usb_pipedevice(urb->pipe),
  268. usb_pipeendpoint(urb->pipe),
  269. usb_pipein(urb->pipe) ? "in" : "out",
  270. urb->actual_length, urb->transfer_buffer_length
  271. );
  272. usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
  273. spin_unlock(&musb->lock);
  274. usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
  275. spin_lock(&musb->lock);
  276. }
  277. /* for bulk/interrupt endpoints only */
  278. static inline void
  279. musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
  280. {
  281. struct usb_device *udev = urb->dev;
  282. u16 csr;
  283. void __iomem *epio = ep->regs;
  284. struct musb_qh *qh;
  285. /* FIXME: the current Mentor DMA code seems to have
  286. * problems getting toggle correct.
  287. */
  288. if (is_in || ep->is_shared_fifo)
  289. qh = ep->in_qh;
  290. else
  291. qh = ep->out_qh;
  292. if (!is_in) {
  293. csr = musb_readw(epio, MUSB_TXCSR);
  294. usb_settoggle(udev, qh->epnum, 1,
  295. (csr & MUSB_TXCSR_H_DATATOGGLE)
  296. ? 1 : 0);
  297. } else {
  298. csr = musb_readw(epio, MUSB_RXCSR);
  299. usb_settoggle(udev, qh->epnum, 0,
  300. (csr & MUSB_RXCSR_H_DATATOGGLE)
  301. ? 1 : 0);
  302. }
  303. }
  304. /* caller owns controller lock, irqs are blocked */
  305. static struct musb_qh *
  306. musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
  307. {
  308. int is_in;
  309. struct musb_hw_ep *ep = qh->hw_ep;
  310. struct musb *musb = ep->musb;
  311. int ready = qh->is_ready;
  312. if (ep->is_shared_fifo)
  313. is_in = 1;
  314. else
  315. is_in = usb_pipein(urb->pipe);
  316. /* save toggle eagerly, for paranoia */
  317. switch (qh->type) {
  318. case USB_ENDPOINT_XFER_BULK:
  319. case USB_ENDPOINT_XFER_INT:
  320. musb_save_toggle(ep, is_in, urb);
  321. break;
  322. case USB_ENDPOINT_XFER_ISOC:
  323. if (status == 0 && urb->error_count)
  324. status = -EXDEV;
  325. break;
  326. }
  327. qh->is_ready = 0;
  328. __musb_giveback(musb, urb, status);
  329. qh->is_ready = ready;
  330. /* reclaim resources (and bandwidth) ASAP; deschedule it, and
  331. * invalidate qh as soon as list_empty(&hep->urb_list)
  332. */
  333. if (list_empty(&qh->hep->urb_list)) {
  334. struct list_head *head;
  335. if (is_in)
  336. ep->rx_reinit = 1;
  337. else
  338. ep->tx_reinit = 1;
  339. /* clobber old pointers to this qh */
  340. if (is_in || ep->is_shared_fifo)
  341. ep->in_qh = NULL;
  342. else
  343. ep->out_qh = NULL;
  344. qh->hep->hcpriv = NULL;
  345. switch (qh->type) {
  346. case USB_ENDPOINT_XFER_CONTROL:
  347. case USB_ENDPOINT_XFER_BULK:
  348. /* fifo policy for these lists, except that NAKing
  349. * should rotate a qh to the end (for fairness).
  350. */
  351. if (qh->mux == 1) {
  352. head = qh->ring.prev;
  353. list_del(&qh->ring);
  354. kfree(qh);
  355. qh = first_qh(head);
  356. break;
  357. }
  358. case USB_ENDPOINT_XFER_ISOC:
  359. case USB_ENDPOINT_XFER_INT:
  360. /* this is where periodic bandwidth should be
  361. * de-allocated if it's tracked and allocated;
  362. * and where we'd update the schedule tree...
  363. */
  364. musb->periodic[ep->epnum] = NULL;
  365. kfree(qh);
  366. qh = NULL;
  367. break;
  368. }
  369. }
  370. return qh;
  371. }
  372. /*
  373. * Advance this hardware endpoint's queue, completing the specified urb and
  374. * advancing to either the next urb queued to that qh, or else invalidating
  375. * that qh and advancing to the next qh scheduled after the current one.
  376. *
  377. * Context: caller owns controller lock, irqs are blocked
  378. */
  379. static void
  380. musb_advance_schedule(struct musb *musb, struct urb *urb,
  381. struct musb_hw_ep *hw_ep, int is_in)
  382. {
  383. struct musb_qh *qh;
  384. if (is_in || hw_ep->is_shared_fifo)
  385. qh = hw_ep->in_qh;
  386. else
  387. qh = hw_ep->out_qh;
  388. if (urb->status == -EINPROGRESS)
  389. qh = musb_giveback(qh, urb, 0);
  390. else
  391. qh = musb_giveback(qh, urb, urb->status);
  392. if (qh != NULL && qh->is_ready) {
  393. DBG(4, "... next ep%d %cX urb %p\n",
  394. hw_ep->epnum, is_in ? 'R' : 'T',
  395. next_urb(qh));
  396. musb_start_urb(musb, is_in, qh);
  397. }
  398. }
  399. static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
  400. {
  401. /* we don't want fifo to fill itself again;
  402. * ignore dma (various models),
  403. * leave toggle alone (may not have been saved yet)
  404. */
  405. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
  406. csr &= ~(MUSB_RXCSR_H_REQPKT
  407. | MUSB_RXCSR_H_AUTOREQ
  408. | MUSB_RXCSR_AUTOCLEAR);
  409. /* write 2x to allow double buffering */
  410. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  411. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  412. /* flush writebuffer */
  413. return musb_readw(hw_ep->regs, MUSB_RXCSR);
  414. }
  415. /*
  416. * PIO RX for a packet (or part of it).
  417. */
  418. static bool
  419. musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
  420. {
  421. u16 rx_count;
  422. u8 *buf;
  423. u16 csr;
  424. bool done = false;
  425. u32 length;
  426. int do_flush = 0;
  427. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  428. void __iomem *epio = hw_ep->regs;
  429. struct musb_qh *qh = hw_ep->in_qh;
  430. int pipe = urb->pipe;
  431. void *buffer = urb->transfer_buffer;
  432. /* musb_ep_select(mbase, epnum); */
  433. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  434. DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
  435. urb->transfer_buffer, qh->offset,
  436. urb->transfer_buffer_length);
  437. /* unload FIFO */
  438. if (usb_pipeisoc(pipe)) {
  439. int status = 0;
  440. struct usb_iso_packet_descriptor *d;
  441. if (iso_err) {
  442. status = -EILSEQ;
  443. urb->error_count++;
  444. }
  445. d = urb->iso_frame_desc + qh->iso_idx;
  446. buf = buffer + d->offset;
  447. length = d->length;
  448. if (rx_count > length) {
  449. if (status == 0) {
  450. status = -EOVERFLOW;
  451. urb->error_count++;
  452. }
  453. DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
  454. do_flush = 1;
  455. } else
  456. length = rx_count;
  457. urb->actual_length += length;
  458. d->actual_length = length;
  459. d->status = status;
  460. /* see if we are done */
  461. done = (++qh->iso_idx >= urb->number_of_packets);
  462. } else {
  463. /* non-isoch */
  464. buf = buffer + qh->offset;
  465. length = urb->transfer_buffer_length - qh->offset;
  466. if (rx_count > length) {
  467. if (urb->status == -EINPROGRESS)
  468. urb->status = -EOVERFLOW;
  469. DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
  470. do_flush = 1;
  471. } else
  472. length = rx_count;
  473. urb->actual_length += length;
  474. qh->offset += length;
  475. /* see if we are done */
  476. done = (urb->actual_length == urb->transfer_buffer_length)
  477. || (rx_count < qh->maxpacket)
  478. || (urb->status != -EINPROGRESS);
  479. if (done
  480. && (urb->status == -EINPROGRESS)
  481. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  482. && (urb->actual_length
  483. < urb->transfer_buffer_length))
  484. urb->status = -EREMOTEIO;
  485. }
  486. musb_read_fifo(hw_ep, length, buf);
  487. csr = musb_readw(epio, MUSB_RXCSR);
  488. csr |= MUSB_RXCSR_H_WZC_BITS;
  489. if (unlikely(do_flush))
  490. musb_h_flush_rxfifo(hw_ep, csr);
  491. else {
  492. /* REVISIT this assumes AUTOCLEAR is never set */
  493. csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
  494. if (!done)
  495. csr |= MUSB_RXCSR_H_REQPKT;
  496. musb_writew(epio, MUSB_RXCSR, csr);
  497. }
  498. return done;
  499. }
  500. /* we don't always need to reinit a given side of an endpoint...
  501. * when we do, use tx/rx reinit routine and then construct a new CSR
  502. * to address data toggle, NYET, and DMA or PIO.
  503. *
  504. * it's possible that driver bugs (especially for DMA) or aborting a
  505. * transfer might have left the endpoint busier than it should be.
  506. * the busy/not-empty tests are basically paranoia.
  507. */
  508. static void
  509. musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
  510. {
  511. u16 csr;
  512. /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
  513. * That always uses tx_reinit since ep0 repurposes TX register
  514. * offsets; the initial SETUP packet is also a kind of OUT.
  515. */
  516. /* if programmed for Tx, put it in RX mode */
  517. if (ep->is_shared_fifo) {
  518. csr = musb_readw(ep->regs, MUSB_TXCSR);
  519. if (csr & MUSB_TXCSR_MODE) {
  520. musb_h_tx_flush_fifo(ep);
  521. musb_writew(ep->regs, MUSB_TXCSR,
  522. MUSB_TXCSR_FRCDATATOG);
  523. }
  524. /* clear mode (and everything else) to enable Rx */
  525. musb_writew(ep->regs, MUSB_TXCSR, 0);
  526. /* scrub all previous state, clearing toggle */
  527. } else {
  528. csr = musb_readw(ep->regs, MUSB_RXCSR);
  529. if (csr & MUSB_RXCSR_RXPKTRDY)
  530. WARNING("rx%d, packet/%d ready?\n", ep->epnum,
  531. musb_readw(ep->regs, MUSB_RXCOUNT));
  532. musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
  533. }
  534. /* target addr and (for multipoint) hub addr/port */
  535. if (musb->is_multipoint) {
  536. musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
  537. musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
  538. musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
  539. } else
  540. musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
  541. /* protocol/endpoint, interval/NAKlimit, i/o size */
  542. musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
  543. musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
  544. /* NOTE: bulk combining rewrites high bits of maxpacket */
  545. musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
  546. ep->rx_reinit = 0;
  547. }
  548. /*
  549. * Program an HDRC endpoint as per the given URB
  550. * Context: irqs blocked, controller lock held
  551. */
  552. static void musb_ep_program(struct musb *musb, u8 epnum,
  553. struct urb *urb, unsigned int is_out,
  554. u8 *buf, u32 len)
  555. {
  556. struct dma_controller *dma_controller;
  557. struct dma_channel *dma_channel;
  558. u8 dma_ok;
  559. void __iomem *mbase = musb->mregs;
  560. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  561. void __iomem *epio = hw_ep->regs;
  562. struct musb_qh *qh;
  563. u16 packet_sz;
  564. if (!is_out || hw_ep->is_shared_fifo)
  565. qh = hw_ep->in_qh;
  566. else
  567. qh = hw_ep->out_qh;
  568. packet_sz = qh->maxpacket;
  569. DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
  570. "h_addr%02x h_port%02x bytes %d\n",
  571. is_out ? "-->" : "<--",
  572. epnum, urb, urb->dev->speed,
  573. qh->addr_reg, qh->epnum, is_out ? "out" : "in",
  574. qh->h_addr_reg, qh->h_port_reg,
  575. len);
  576. musb_ep_select(mbase, epnum);
  577. /* candidate for DMA? */
  578. dma_controller = musb->dma_controller;
  579. if (is_dma_capable() && epnum && dma_controller) {
  580. dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
  581. if (!dma_channel) {
  582. dma_channel = dma_controller->channel_alloc(
  583. dma_controller, hw_ep, is_out);
  584. if (is_out)
  585. hw_ep->tx_channel = dma_channel;
  586. else
  587. hw_ep->rx_channel = dma_channel;
  588. }
  589. } else
  590. dma_channel = NULL;
  591. /* make sure we clear DMAEnab, autoSet bits from previous run */
  592. /* OUT/transmit/EP0 or IN/receive? */
  593. if (is_out) {
  594. u16 csr;
  595. u16 int_txe;
  596. u16 load_count;
  597. csr = musb_readw(epio, MUSB_TXCSR);
  598. /* disable interrupt in case we flush */
  599. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  600. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  601. /* general endpoint setup */
  602. if (epnum) {
  603. /* ASSERT: TXCSR_DMAENAB was already cleared */
  604. /* flush all old state, set default */
  605. musb_h_tx_flush_fifo(hw_ep);
  606. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  607. | MUSB_TXCSR_DMAMODE
  608. | MUSB_TXCSR_FRCDATATOG
  609. | MUSB_TXCSR_H_RXSTALL
  610. | MUSB_TXCSR_H_ERROR
  611. | MUSB_TXCSR_TXPKTRDY
  612. );
  613. csr |= MUSB_TXCSR_MODE;
  614. if (usb_gettoggle(urb->dev,
  615. qh->epnum, 1))
  616. csr |= MUSB_TXCSR_H_WR_DATATOGGLE
  617. | MUSB_TXCSR_H_DATATOGGLE;
  618. else
  619. csr |= MUSB_TXCSR_CLRDATATOG;
  620. /* twice in case of double packet buffering */
  621. musb_writew(epio, MUSB_TXCSR, csr);
  622. /* REVISIT may need to clear FLUSHFIFO ... */
  623. musb_writew(epio, MUSB_TXCSR, csr);
  624. csr = musb_readw(epio, MUSB_TXCSR);
  625. } else {
  626. /* endpoint 0: just flush */
  627. musb_writew(epio, MUSB_CSR0,
  628. csr | MUSB_CSR0_FLUSHFIFO);
  629. musb_writew(epio, MUSB_CSR0,
  630. csr | MUSB_CSR0_FLUSHFIFO);
  631. }
  632. /* target addr and (for multipoint) hub addr/port */
  633. if (musb->is_multipoint) {
  634. musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
  635. musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
  636. musb_write_txhubport(mbase, epnum, qh->h_port_reg);
  637. /* FIXME if !epnum, do the same for RX ... */
  638. } else
  639. musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
  640. /* protocol/endpoint/interval/NAKlimit */
  641. if (epnum) {
  642. musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
  643. if (can_bulk_split(musb, qh->type))
  644. musb_writew(epio, MUSB_TXMAXP,
  645. packet_sz
  646. | ((hw_ep->max_packet_sz_tx /
  647. packet_sz) - 1) << 11);
  648. else
  649. musb_writew(epio, MUSB_TXMAXP,
  650. packet_sz);
  651. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  652. } else {
  653. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  654. if (musb->is_multipoint)
  655. musb_writeb(epio, MUSB_TYPE0,
  656. qh->type_reg);
  657. }
  658. if (can_bulk_split(musb, qh->type))
  659. load_count = min((u32) hw_ep->max_packet_sz_tx,
  660. len);
  661. else
  662. load_count = min((u32) packet_sz, len);
  663. #ifdef CONFIG_USB_INVENTRA_DMA
  664. if (dma_channel) {
  665. /* clear previous state */
  666. csr = musb_readw(epio, MUSB_TXCSR);
  667. csr &= ~(MUSB_TXCSR_AUTOSET
  668. | MUSB_TXCSR_DMAMODE
  669. | MUSB_TXCSR_DMAENAB);
  670. csr |= MUSB_TXCSR_MODE;
  671. musb_writew(epio, MUSB_TXCSR,
  672. csr | MUSB_TXCSR_MODE);
  673. qh->segsize = min(len, dma_channel->max_len);
  674. if (qh->segsize <= packet_sz)
  675. dma_channel->desired_mode = 0;
  676. else
  677. dma_channel->desired_mode = 1;
  678. if (dma_channel->desired_mode == 0) {
  679. csr &= ~(MUSB_TXCSR_AUTOSET
  680. | MUSB_TXCSR_DMAMODE);
  681. csr |= (MUSB_TXCSR_DMAENAB);
  682. /* against programming guide */
  683. } else
  684. csr |= (MUSB_TXCSR_AUTOSET
  685. | MUSB_TXCSR_DMAENAB
  686. | MUSB_TXCSR_DMAMODE);
  687. musb_writew(epio, MUSB_TXCSR, csr);
  688. dma_ok = dma_controller->channel_program(
  689. dma_channel, packet_sz,
  690. dma_channel->desired_mode,
  691. urb->transfer_dma,
  692. qh->segsize);
  693. if (dma_ok) {
  694. load_count = 0;
  695. } else {
  696. dma_controller->channel_release(dma_channel);
  697. if (is_out)
  698. hw_ep->tx_channel = NULL;
  699. else
  700. hw_ep->rx_channel = NULL;
  701. dma_channel = NULL;
  702. }
  703. }
  704. #endif
  705. /* candidate for DMA */
  706. if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
  707. /* program endpoint CSRs first, then setup DMA.
  708. * assume CPPI setup succeeds.
  709. * defer enabling dma.
  710. */
  711. csr = musb_readw(epio, MUSB_TXCSR);
  712. csr &= ~(MUSB_TXCSR_AUTOSET
  713. | MUSB_TXCSR_DMAMODE
  714. | MUSB_TXCSR_DMAENAB);
  715. csr |= MUSB_TXCSR_MODE;
  716. musb_writew(epio, MUSB_TXCSR,
  717. csr | MUSB_TXCSR_MODE);
  718. dma_channel->actual_len = 0L;
  719. qh->segsize = len;
  720. /* TX uses "rndis" mode automatically, but needs help
  721. * to identify the zero-length-final-packet case.
  722. */
  723. dma_ok = dma_controller->channel_program(
  724. dma_channel, packet_sz,
  725. (urb->transfer_flags
  726. & URB_ZERO_PACKET)
  727. == URB_ZERO_PACKET,
  728. urb->transfer_dma,
  729. qh->segsize);
  730. if (dma_ok) {
  731. load_count = 0;
  732. } else {
  733. dma_controller->channel_release(dma_channel);
  734. hw_ep->tx_channel = NULL;
  735. dma_channel = NULL;
  736. /* REVISIT there's an error path here that
  737. * needs handling: can't do dma, but
  738. * there's no pio buffer address...
  739. */
  740. }
  741. }
  742. if (load_count) {
  743. /* ASSERT: TXCSR_DMAENAB was already cleared */
  744. /* PIO to load FIFO */
  745. qh->segsize = load_count;
  746. musb_write_fifo(hw_ep, load_count, buf);
  747. csr = musb_readw(epio, MUSB_TXCSR);
  748. csr &= ~(MUSB_TXCSR_DMAENAB
  749. | MUSB_TXCSR_DMAMODE
  750. | MUSB_TXCSR_AUTOSET);
  751. /* write CSR */
  752. csr |= MUSB_TXCSR_MODE;
  753. if (epnum)
  754. musb_writew(epio, MUSB_TXCSR, csr);
  755. }
  756. /* re-enable interrupt */
  757. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  758. /* IN/receive */
  759. } else {
  760. u16 csr;
  761. if (hw_ep->rx_reinit) {
  762. musb_rx_reinit(musb, qh, hw_ep);
  763. /* init new state: toggle and NYET, maybe DMA later */
  764. if (usb_gettoggle(urb->dev, qh->epnum, 0))
  765. csr = MUSB_RXCSR_H_WR_DATATOGGLE
  766. | MUSB_RXCSR_H_DATATOGGLE;
  767. else
  768. csr = 0;
  769. if (qh->type == USB_ENDPOINT_XFER_INT)
  770. csr |= MUSB_RXCSR_DISNYET;
  771. } else {
  772. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  773. if (csr & (MUSB_RXCSR_RXPKTRDY
  774. | MUSB_RXCSR_DMAENAB
  775. | MUSB_RXCSR_H_REQPKT))
  776. ERR("broken !rx_reinit, ep%d csr %04x\n",
  777. hw_ep->epnum, csr);
  778. /* scrub any stale state, leaving toggle alone */
  779. csr &= MUSB_RXCSR_DISNYET;
  780. }
  781. /* kick things off */
  782. if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
  783. /* candidate for DMA */
  784. if (dma_channel) {
  785. dma_channel->actual_len = 0L;
  786. qh->segsize = len;
  787. /* AUTOREQ is in a DMA register */
  788. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  789. csr = musb_readw(hw_ep->regs,
  790. MUSB_RXCSR);
  791. /* unless caller treats short rx transfers as
  792. * errors, we dare not queue multiple transfers.
  793. */
  794. dma_ok = dma_controller->channel_program(
  795. dma_channel, packet_sz,
  796. !(urb->transfer_flags
  797. & URB_SHORT_NOT_OK),
  798. urb->transfer_dma,
  799. qh->segsize);
  800. if (!dma_ok) {
  801. dma_controller->channel_release(
  802. dma_channel);
  803. hw_ep->rx_channel = NULL;
  804. dma_channel = NULL;
  805. } else
  806. csr |= MUSB_RXCSR_DMAENAB;
  807. }
  808. }
  809. csr |= MUSB_RXCSR_H_REQPKT;
  810. DBG(7, "RXCSR%d := %04x\n", epnum, csr);
  811. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  812. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  813. }
  814. }
  815. /*
  816. * Service the default endpoint (ep0) as host.
  817. * Return true until it's time to start the status stage.
  818. */
  819. static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
  820. {
  821. bool more = false;
  822. u8 *fifo_dest = NULL;
  823. u16 fifo_count = 0;
  824. struct musb_hw_ep *hw_ep = musb->control_ep;
  825. struct musb_qh *qh = hw_ep->in_qh;
  826. struct usb_ctrlrequest *request;
  827. switch (musb->ep0_stage) {
  828. case MUSB_EP0_IN:
  829. fifo_dest = urb->transfer_buffer + urb->actual_length;
  830. fifo_count = min(len, ((u16) (urb->transfer_buffer_length
  831. - urb->actual_length)));
  832. if (fifo_count < len)
  833. urb->status = -EOVERFLOW;
  834. musb_read_fifo(hw_ep, fifo_count, fifo_dest);
  835. urb->actual_length += fifo_count;
  836. if (len < qh->maxpacket) {
  837. /* always terminate on short read; it's
  838. * rarely reported as an error.
  839. */
  840. } else if (urb->actual_length <
  841. urb->transfer_buffer_length)
  842. more = true;
  843. break;
  844. case MUSB_EP0_START:
  845. request = (struct usb_ctrlrequest *) urb->setup_packet;
  846. if (!request->wLength) {
  847. DBG(4, "start no-DATA\n");
  848. break;
  849. } else if (request->bRequestType & USB_DIR_IN) {
  850. DBG(4, "start IN-DATA\n");
  851. musb->ep0_stage = MUSB_EP0_IN;
  852. more = true;
  853. break;
  854. } else {
  855. DBG(4, "start OUT-DATA\n");
  856. musb->ep0_stage = MUSB_EP0_OUT;
  857. more = true;
  858. }
  859. /* FALLTHROUGH */
  860. case MUSB_EP0_OUT:
  861. fifo_count = min(qh->maxpacket, ((u16)
  862. (urb->transfer_buffer_length
  863. - urb->actual_length)));
  864. if (fifo_count) {
  865. fifo_dest = (u8 *) (urb->transfer_buffer
  866. + urb->actual_length);
  867. DBG(3, "Sending %d byte%s to ep0 fifo %p\n",
  868. fifo_count,
  869. (fifo_count == 1) ? "" : "s",
  870. fifo_dest);
  871. musb_write_fifo(hw_ep, fifo_count, fifo_dest);
  872. urb->actual_length += fifo_count;
  873. more = true;
  874. }
  875. break;
  876. default:
  877. ERR("bogus ep0 stage %d\n", musb->ep0_stage);
  878. break;
  879. }
  880. return more;
  881. }
  882. /*
  883. * Handle default endpoint interrupt as host. Only called in IRQ time
  884. * from musb_interrupt().
  885. *
  886. * called with controller irqlocked
  887. */
  888. irqreturn_t musb_h_ep0_irq(struct musb *musb)
  889. {
  890. struct urb *urb;
  891. u16 csr, len;
  892. int status = 0;
  893. void __iomem *mbase = musb->mregs;
  894. struct musb_hw_ep *hw_ep = musb->control_ep;
  895. void __iomem *epio = hw_ep->regs;
  896. struct musb_qh *qh = hw_ep->in_qh;
  897. bool complete = false;
  898. irqreturn_t retval = IRQ_NONE;
  899. /* ep0 only has one queue, "in" */
  900. urb = next_urb(qh);
  901. musb_ep_select(mbase, 0);
  902. csr = musb_readw(epio, MUSB_CSR0);
  903. len = (csr & MUSB_CSR0_RXPKTRDY)
  904. ? musb_readb(epio, MUSB_COUNT0)
  905. : 0;
  906. DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
  907. csr, qh, len, urb, musb->ep0_stage);
  908. /* if we just did status stage, we are done */
  909. if (MUSB_EP0_STATUS == musb->ep0_stage) {
  910. retval = IRQ_HANDLED;
  911. complete = true;
  912. }
  913. /* prepare status */
  914. if (csr & MUSB_CSR0_H_RXSTALL) {
  915. DBG(6, "STALLING ENDPOINT\n");
  916. status = -EPIPE;
  917. } else if (csr & MUSB_CSR0_H_ERROR) {
  918. DBG(2, "no response, csr0 %04x\n", csr);
  919. status = -EPROTO;
  920. } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
  921. DBG(2, "control NAK timeout\n");
  922. /* NOTE: this code path would be a good place to PAUSE a
  923. * control transfer, if another one is queued, so that
  924. * ep0 is more likely to stay busy.
  925. *
  926. * if (qh->ring.next != &musb->control), then
  927. * we have a candidate... NAKing is *NOT* an error
  928. */
  929. musb_writew(epio, MUSB_CSR0, 0);
  930. retval = IRQ_HANDLED;
  931. }
  932. if (status) {
  933. DBG(6, "aborting\n");
  934. retval = IRQ_HANDLED;
  935. if (urb)
  936. urb->status = status;
  937. complete = true;
  938. /* use the proper sequence to abort the transfer */
  939. if (csr & MUSB_CSR0_H_REQPKT) {
  940. csr &= ~MUSB_CSR0_H_REQPKT;
  941. musb_writew(epio, MUSB_CSR0, csr);
  942. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  943. musb_writew(epio, MUSB_CSR0, csr);
  944. } else {
  945. csr |= MUSB_CSR0_FLUSHFIFO;
  946. musb_writew(epio, MUSB_CSR0, csr);
  947. musb_writew(epio, MUSB_CSR0, csr);
  948. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  949. musb_writew(epio, MUSB_CSR0, csr);
  950. }
  951. musb_writeb(epio, MUSB_NAKLIMIT0, 0);
  952. /* clear it */
  953. musb_writew(epio, MUSB_CSR0, 0);
  954. }
  955. if (unlikely(!urb)) {
  956. /* stop endpoint since we have no place for its data, this
  957. * SHOULD NEVER HAPPEN! */
  958. ERR("no URB for end 0\n");
  959. musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
  960. musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
  961. musb_writew(epio, MUSB_CSR0, 0);
  962. goto done;
  963. }
  964. if (!complete) {
  965. /* call common logic and prepare response */
  966. if (musb_h_ep0_continue(musb, len, urb)) {
  967. /* more packets required */
  968. csr = (MUSB_EP0_IN == musb->ep0_stage)
  969. ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
  970. } else {
  971. /* data transfer complete; perform status phase */
  972. if (usb_pipeout(urb->pipe)
  973. || !urb->transfer_buffer_length)
  974. csr = MUSB_CSR0_H_STATUSPKT
  975. | MUSB_CSR0_H_REQPKT;
  976. else
  977. csr = MUSB_CSR0_H_STATUSPKT
  978. | MUSB_CSR0_TXPKTRDY;
  979. /* flag status stage */
  980. musb->ep0_stage = MUSB_EP0_STATUS;
  981. DBG(5, "ep0 STATUS, csr %04x\n", csr);
  982. }
  983. musb_writew(epio, MUSB_CSR0, csr);
  984. retval = IRQ_HANDLED;
  985. } else
  986. musb->ep0_stage = MUSB_EP0_IDLE;
  987. /* call completion handler if done */
  988. if (complete)
  989. musb_advance_schedule(musb, urb, hw_ep, 1);
  990. done:
  991. return retval;
  992. }
  993. #ifdef CONFIG_USB_INVENTRA_DMA
  994. /* Host side TX (OUT) using Mentor DMA works as follows:
  995. submit_urb ->
  996. - if queue was empty, Program Endpoint
  997. - ... which starts DMA to fifo in mode 1 or 0
  998. DMA Isr (transfer complete) -> TxAvail()
  999. - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
  1000. only in musb_cleanup_urb)
  1001. - TxPktRdy has to be set in mode 0 or for
  1002. short packets in mode 1.
  1003. */
  1004. #endif
  1005. /* Service a Tx-Available or dma completion irq for the endpoint */
  1006. void musb_host_tx(struct musb *musb, u8 epnum)
  1007. {
  1008. int pipe;
  1009. bool done = false;
  1010. u16 tx_csr;
  1011. size_t wLength = 0;
  1012. u8 *buf = NULL;
  1013. struct urb *urb;
  1014. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1015. void __iomem *epio = hw_ep->regs;
  1016. struct musb_qh *qh = hw_ep->is_shared_fifo ? hw_ep->in_qh
  1017. : hw_ep->out_qh;
  1018. u32 status = 0;
  1019. void __iomem *mbase = musb->mregs;
  1020. struct dma_channel *dma;
  1021. urb = next_urb(qh);
  1022. musb_ep_select(mbase, epnum);
  1023. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1024. /* with CPPI, DMA sometimes triggers "extra" irqs */
  1025. if (!urb) {
  1026. DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  1027. goto finish;
  1028. }
  1029. pipe = urb->pipe;
  1030. dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
  1031. DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
  1032. dma ? ", dma" : "");
  1033. /* check for errors */
  1034. if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
  1035. /* dma was disabled, fifo flushed */
  1036. DBG(3, "TX end %d stall\n", epnum);
  1037. /* stall; record URB status */
  1038. status = -EPIPE;
  1039. } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
  1040. /* (NON-ISO) dma was disabled, fifo flushed */
  1041. DBG(3, "TX 3strikes on ep=%d\n", epnum);
  1042. status = -ETIMEDOUT;
  1043. } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
  1044. DBG(6, "TX end=%d device not responding\n", epnum);
  1045. /* NOTE: this code path would be a good place to PAUSE a
  1046. * transfer, if there's some other (nonperiodic) tx urb
  1047. * that could use this fifo. (dma complicates it...)
  1048. *
  1049. * if (bulk && qh->ring.next != &musb->out_bulk), then
  1050. * we have a candidate... NAKing is *NOT* an error
  1051. */
  1052. musb_ep_select(mbase, epnum);
  1053. musb_writew(epio, MUSB_TXCSR,
  1054. MUSB_TXCSR_H_WZC_BITS
  1055. | MUSB_TXCSR_TXPKTRDY);
  1056. goto finish;
  1057. }
  1058. if (status) {
  1059. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1060. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1061. (void) musb->dma_controller->channel_abort(dma);
  1062. }
  1063. /* do the proper sequence to abort the transfer in the
  1064. * usb core; the dma engine should already be stopped.
  1065. */
  1066. musb_h_tx_flush_fifo(hw_ep);
  1067. tx_csr &= ~(MUSB_TXCSR_AUTOSET
  1068. | MUSB_TXCSR_DMAENAB
  1069. | MUSB_TXCSR_H_ERROR
  1070. | MUSB_TXCSR_H_RXSTALL
  1071. | MUSB_TXCSR_H_NAKTIMEOUT
  1072. );
  1073. musb_ep_select(mbase, epnum);
  1074. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1075. /* REVISIT may need to clear FLUSHFIFO ... */
  1076. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1077. musb_writeb(epio, MUSB_TXINTERVAL, 0);
  1078. done = true;
  1079. }
  1080. /* second cppi case */
  1081. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1082. DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  1083. goto finish;
  1084. }
  1085. /* REVISIT this looks wrong... */
  1086. if (!status || dma || usb_pipeisoc(pipe)) {
  1087. if (dma)
  1088. wLength = dma->actual_len;
  1089. else
  1090. wLength = qh->segsize;
  1091. qh->offset += wLength;
  1092. if (usb_pipeisoc(pipe)) {
  1093. struct usb_iso_packet_descriptor *d;
  1094. d = urb->iso_frame_desc + qh->iso_idx;
  1095. d->actual_length = qh->segsize;
  1096. if (++qh->iso_idx >= urb->number_of_packets) {
  1097. done = true;
  1098. } else {
  1099. d++;
  1100. buf = urb->transfer_buffer + d->offset;
  1101. wLength = d->length;
  1102. }
  1103. } else if (dma) {
  1104. done = true;
  1105. } else {
  1106. /* see if we need to send more data, or ZLP */
  1107. if (qh->segsize < qh->maxpacket)
  1108. done = true;
  1109. else if (qh->offset == urb->transfer_buffer_length
  1110. && !(urb->transfer_flags
  1111. & URB_ZERO_PACKET))
  1112. done = true;
  1113. if (!done) {
  1114. buf = urb->transfer_buffer
  1115. + qh->offset;
  1116. wLength = urb->transfer_buffer_length
  1117. - qh->offset;
  1118. }
  1119. }
  1120. }
  1121. /* urb->status != -EINPROGRESS means request has been faulted,
  1122. * so we must abort this transfer after cleanup
  1123. */
  1124. if (urb->status != -EINPROGRESS) {
  1125. done = true;
  1126. if (status == 0)
  1127. status = urb->status;
  1128. }
  1129. if (done) {
  1130. /* set status */
  1131. urb->status = status;
  1132. urb->actual_length = qh->offset;
  1133. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
  1134. } else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
  1135. /* WARN_ON(!buf); */
  1136. /* REVISIT: some docs say that when hw_ep->tx_double_buffered,
  1137. * (and presumably, fifo is not half-full) we should write TWO
  1138. * packets before updating TXCSR ... other docs disagree ...
  1139. */
  1140. /* PIO: start next packet in this URB */
  1141. wLength = min(qh->maxpacket, (u16) wLength);
  1142. musb_write_fifo(hw_ep, wLength, buf);
  1143. qh->segsize = wLength;
  1144. musb_ep_select(mbase, epnum);
  1145. musb_writew(epio, MUSB_TXCSR,
  1146. MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1147. } else
  1148. DBG(1, "not complete, but dma enabled?\n");
  1149. finish:
  1150. return;
  1151. }
  1152. #ifdef CONFIG_USB_INVENTRA_DMA
  1153. /* Host side RX (IN) using Mentor DMA works as follows:
  1154. submit_urb ->
  1155. - if queue was empty, ProgramEndpoint
  1156. - first IN token is sent out (by setting ReqPkt)
  1157. LinuxIsr -> RxReady()
  1158. /\ => first packet is received
  1159. | - Set in mode 0 (DmaEnab, ~ReqPkt)
  1160. | -> DMA Isr (transfer complete) -> RxReady()
  1161. | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
  1162. | - if urb not complete, send next IN token (ReqPkt)
  1163. | | else complete urb.
  1164. | |
  1165. ---------------------------
  1166. *
  1167. * Nuances of mode 1:
  1168. * For short packets, no ack (+RxPktRdy) is sent automatically
  1169. * (even if AutoClear is ON)
  1170. * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
  1171. * automatically => major problem, as collecting the next packet becomes
  1172. * difficult. Hence mode 1 is not used.
  1173. *
  1174. * REVISIT
  1175. * All we care about at this driver level is that
  1176. * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
  1177. * (b) termination conditions are: short RX, or buffer full;
  1178. * (c) fault modes include
  1179. * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
  1180. * (and that endpoint's dma queue stops immediately)
  1181. * - overflow (full, PLUS more bytes in the terminal packet)
  1182. *
  1183. * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
  1184. * thus be a great candidate for using mode 1 ... for all but the
  1185. * last packet of one URB's transfer.
  1186. */
  1187. #endif
  1188. /*
  1189. * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
  1190. * and high-bandwidth IN transfer cases.
  1191. */
  1192. void musb_host_rx(struct musb *musb, u8 epnum)
  1193. {
  1194. struct urb *urb;
  1195. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1196. void __iomem *epio = hw_ep->regs;
  1197. struct musb_qh *qh = hw_ep->in_qh;
  1198. size_t xfer_len;
  1199. void __iomem *mbase = musb->mregs;
  1200. int pipe;
  1201. u16 rx_csr, val;
  1202. bool iso_err = false;
  1203. bool done = false;
  1204. u32 status;
  1205. struct dma_channel *dma;
  1206. musb_ep_select(mbase, epnum);
  1207. urb = next_urb(qh);
  1208. dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
  1209. status = 0;
  1210. xfer_len = 0;
  1211. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1212. val = rx_csr;
  1213. if (unlikely(!urb)) {
  1214. /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
  1215. * usbtest #11 (unlinks) triggers it regularly, sometimes
  1216. * with fifo full. (Only with DMA??)
  1217. */
  1218. DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
  1219. musb_readw(epio, MUSB_RXCOUNT));
  1220. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1221. return;
  1222. }
  1223. pipe = urb->pipe;
  1224. DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
  1225. epnum, rx_csr, urb->actual_length,
  1226. dma ? dma->actual_len : 0);
  1227. /* check for errors, concurrent stall & unlink is not really
  1228. * handled yet! */
  1229. if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
  1230. DBG(3, "RX end %d STALL\n", epnum);
  1231. /* stall; record URB status */
  1232. status = -EPIPE;
  1233. } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
  1234. DBG(3, "end %d RX proto error\n", epnum);
  1235. status = -EPROTO;
  1236. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1237. } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
  1238. if (USB_ENDPOINT_XFER_ISOC != qh->type) {
  1239. /* NOTE this code path would be a good place to PAUSE a
  1240. * transfer, if there's some other (nonperiodic) rx urb
  1241. * that could use this fifo. (dma complicates it...)
  1242. *
  1243. * if (bulk && qh->ring.next != &musb->in_bulk), then
  1244. * we have a candidate... NAKing is *NOT* an error
  1245. */
  1246. DBG(6, "RX end %d NAK timeout\n", epnum);
  1247. musb_ep_select(mbase, epnum);
  1248. musb_writew(epio, MUSB_RXCSR,
  1249. MUSB_RXCSR_H_WZC_BITS
  1250. | MUSB_RXCSR_H_REQPKT);
  1251. goto finish;
  1252. } else {
  1253. DBG(4, "RX end %d ISO data error\n", epnum);
  1254. /* packet error reported later */
  1255. iso_err = true;
  1256. }
  1257. }
  1258. /* faults abort the transfer */
  1259. if (status) {
  1260. /* clean up dma and collect transfer count */
  1261. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1262. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1263. (void) musb->dma_controller->channel_abort(dma);
  1264. xfer_len = dma->actual_len;
  1265. }
  1266. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1267. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1268. done = true;
  1269. goto finish;
  1270. }
  1271. if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
  1272. /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
  1273. ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
  1274. goto finish;
  1275. }
  1276. /* thorough shutdown for now ... given more precise fault handling
  1277. * and better queueing support, we might keep a DMA pipeline going
  1278. * while processing this irq for earlier completions.
  1279. */
  1280. /* FIXME this is _way_ too much in-line logic for Mentor DMA */
  1281. #ifndef CONFIG_USB_INVENTRA_DMA
  1282. if (rx_csr & MUSB_RXCSR_H_REQPKT) {
  1283. /* REVISIT this happened for a while on some short reads...
  1284. * the cleanup still needs investigation... looks bad...
  1285. * and also duplicates dma cleanup code above ... plus,
  1286. * shouldn't this be the "half full" double buffer case?
  1287. */
  1288. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1289. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1290. (void) musb->dma_controller->channel_abort(dma);
  1291. xfer_len = dma->actual_len;
  1292. done = true;
  1293. }
  1294. DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
  1295. xfer_len, dma ? ", dma" : "");
  1296. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1297. musb_ep_select(mbase, epnum);
  1298. musb_writew(epio, MUSB_RXCSR,
  1299. MUSB_RXCSR_H_WZC_BITS | rx_csr);
  1300. }
  1301. #endif
  1302. if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
  1303. xfer_len = dma->actual_len;
  1304. val &= ~(MUSB_RXCSR_DMAENAB
  1305. | MUSB_RXCSR_H_AUTOREQ
  1306. | MUSB_RXCSR_AUTOCLEAR
  1307. | MUSB_RXCSR_RXPKTRDY);
  1308. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1309. #ifdef CONFIG_USB_INVENTRA_DMA
  1310. if (usb_pipeisoc(pipe)) {
  1311. struct usb_iso_packet_descriptor *d;
  1312. d = urb->iso_frame_desc + qh->iso_idx;
  1313. d->actual_length = xfer_len;
  1314. /* even if there was an error, we did the dma
  1315. * for iso_frame_desc->length
  1316. */
  1317. if (d->status != EILSEQ && d->status != -EOVERFLOW)
  1318. d->status = 0;
  1319. if (++qh->iso_idx >= urb->number_of_packets)
  1320. done = true;
  1321. else
  1322. done = false;
  1323. } else {
  1324. /* done if urb buffer is full or short packet is recd */
  1325. done = (urb->actual_length + xfer_len >=
  1326. urb->transfer_buffer_length
  1327. || dma->actual_len < qh->maxpacket);
  1328. }
  1329. /* send IN token for next packet, without AUTOREQ */
  1330. if (!done) {
  1331. val |= MUSB_RXCSR_H_REQPKT;
  1332. musb_writew(epio, MUSB_RXCSR,
  1333. MUSB_RXCSR_H_WZC_BITS | val);
  1334. }
  1335. DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
  1336. done ? "off" : "reset",
  1337. musb_readw(epio, MUSB_RXCSR),
  1338. musb_readw(epio, MUSB_RXCOUNT));
  1339. #else
  1340. done = true;
  1341. #endif
  1342. } else if (urb->status == -EINPROGRESS) {
  1343. /* if no errors, be sure a packet is ready for unloading */
  1344. if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
  1345. status = -EPROTO;
  1346. ERR("Rx interrupt with no errors or packet!\n");
  1347. /* FIXME this is another "SHOULD NEVER HAPPEN" */
  1348. /* SCRUB (RX) */
  1349. /* do the proper sequence to abort the transfer */
  1350. musb_ep_select(mbase, epnum);
  1351. val &= ~MUSB_RXCSR_H_REQPKT;
  1352. musb_writew(epio, MUSB_RXCSR, val);
  1353. goto finish;
  1354. }
  1355. /* we are expecting IN packets */
  1356. #ifdef CONFIG_USB_INVENTRA_DMA
  1357. if (dma) {
  1358. struct dma_controller *c;
  1359. u16 rx_count;
  1360. int ret, length;
  1361. dma_addr_t buf;
  1362. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  1363. DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
  1364. epnum, rx_count,
  1365. urb->transfer_dma
  1366. + urb->actual_length,
  1367. qh->offset,
  1368. urb->transfer_buffer_length);
  1369. c = musb->dma_controller;
  1370. if (usb_pipeisoc(pipe)) {
  1371. int status = 0;
  1372. struct usb_iso_packet_descriptor *d;
  1373. d = urb->iso_frame_desc + qh->iso_idx;
  1374. if (iso_err) {
  1375. status = -EILSEQ;
  1376. urb->error_count++;
  1377. }
  1378. if (rx_count > d->length) {
  1379. if (status == 0) {
  1380. status = -EOVERFLOW;
  1381. urb->error_count++;
  1382. }
  1383. DBG(2, "** OVERFLOW %d into %d\n",\
  1384. rx_count, d->length);
  1385. length = d->length;
  1386. } else
  1387. length = rx_count;
  1388. d->status = status;
  1389. buf = urb->transfer_dma + d->offset;
  1390. } else {
  1391. length = rx_count;
  1392. buf = urb->transfer_dma +
  1393. urb->actual_length;
  1394. }
  1395. dma->desired_mode = 0;
  1396. #ifdef USE_MODE1
  1397. /* because of the issue below, mode 1 will
  1398. * only rarely behave with correct semantics.
  1399. */
  1400. if ((urb->transfer_flags &
  1401. URB_SHORT_NOT_OK)
  1402. && (urb->transfer_buffer_length -
  1403. urb->actual_length)
  1404. > qh->maxpacket)
  1405. dma->desired_mode = 1;
  1406. if (rx_count < hw_ep->max_packet_sz_rx) {
  1407. length = rx_count;
  1408. dma->bDesiredMode = 0;
  1409. } else {
  1410. length = urb->transfer_buffer_length;
  1411. }
  1412. #endif
  1413. /* Disadvantage of using mode 1:
  1414. * It's basically usable only for mass storage class; essentially all
  1415. * other protocols also terminate transfers on short packets.
  1416. *
  1417. * Details:
  1418. * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
  1419. * If you try to use mode 1 for (transfer_buffer_length - 512), and try
  1420. * to use the extra IN token to grab the last packet using mode 0, then
  1421. * the problem is that you cannot be sure when the device will send the
  1422. * last packet and RxPktRdy set. Sometimes the packet is recd too soon
  1423. * such that it gets lost when RxCSR is re-set at the end of the mode 1
  1424. * transfer, while sometimes it is recd just a little late so that if you
  1425. * try to configure for mode 0 soon after the mode 1 transfer is
  1426. * completed, you will find rxcount 0. Okay, so you might think why not
  1427. * wait for an interrupt when the pkt is recd. Well, you won't get any!
  1428. */
  1429. val = musb_readw(epio, MUSB_RXCSR);
  1430. val &= ~MUSB_RXCSR_H_REQPKT;
  1431. if (dma->desired_mode == 0)
  1432. val &= ~MUSB_RXCSR_H_AUTOREQ;
  1433. else
  1434. val |= MUSB_RXCSR_H_AUTOREQ;
  1435. val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
  1436. musb_writew(epio, MUSB_RXCSR,
  1437. MUSB_RXCSR_H_WZC_BITS | val);
  1438. /* REVISIT if when actual_length != 0,
  1439. * transfer_buffer_length needs to be
  1440. * adjusted first...
  1441. */
  1442. ret = c->channel_program(
  1443. dma, qh->maxpacket,
  1444. dma->desired_mode, buf, length);
  1445. if (!ret) {
  1446. c->channel_release(dma);
  1447. hw_ep->rx_channel = NULL;
  1448. dma = NULL;
  1449. /* REVISIT reset CSR */
  1450. }
  1451. }
  1452. #endif /* Mentor DMA */
  1453. if (!dma) {
  1454. done = musb_host_packet_rx(musb, urb,
  1455. epnum, iso_err);
  1456. DBG(6, "read %spacket\n", done ? "last " : "");
  1457. }
  1458. }
  1459. finish:
  1460. urb->actual_length += xfer_len;
  1461. qh->offset += xfer_len;
  1462. if (done) {
  1463. if (urb->status == -EINPROGRESS)
  1464. urb->status = status;
  1465. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
  1466. }
  1467. }
  1468. /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
  1469. * the software schedule associates multiple such nodes with a given
  1470. * host side hardware endpoint + direction; scheduling may activate
  1471. * that hardware endpoint.
  1472. */
  1473. static int musb_schedule(
  1474. struct musb *musb,
  1475. struct musb_qh *qh,
  1476. int is_in)
  1477. {
  1478. int idle;
  1479. int best_diff;
  1480. int best_end, epnum;
  1481. struct musb_hw_ep *hw_ep = NULL;
  1482. struct list_head *head = NULL;
  1483. /* use fixed hardware for control and bulk */
  1484. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  1485. head = &musb->control;
  1486. hw_ep = musb->control_ep;
  1487. goto success;
  1488. }
  1489. /* else, periodic transfers get muxed to other endpoints */
  1490. /* FIXME this doesn't consider direction, so it can only
  1491. * work for one half of the endpoint hardware, and assumes
  1492. * the previous cases handled all non-shared endpoints...
  1493. */
  1494. /* we know this qh hasn't been scheduled, so all we need to do
  1495. * is choose which hardware endpoint to put it on ...
  1496. *
  1497. * REVISIT what we really want here is a regular schedule tree
  1498. * like e.g. OHCI uses, but for now musb->periodic is just an
  1499. * array of the _single_ logical endpoint associated with a
  1500. * given physical one (identity mapping logical->physical).
  1501. *
  1502. * that simplistic approach makes TT scheduling a lot simpler;
  1503. * there is none, and thus none of its complexity...
  1504. */
  1505. best_diff = 4096;
  1506. best_end = -1;
  1507. for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
  1508. int diff;
  1509. if (musb->periodic[epnum])
  1510. continue;
  1511. hw_ep = &musb->endpoints[epnum];
  1512. if (hw_ep == musb->bulk_ep)
  1513. continue;
  1514. if (is_in)
  1515. diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
  1516. else
  1517. diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
  1518. if (diff >= 0 && best_diff > diff) {
  1519. best_diff = diff;
  1520. best_end = epnum;
  1521. }
  1522. }
  1523. /* use bulk reserved ep1 if no other ep is free */
  1524. if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
  1525. hw_ep = musb->bulk_ep;
  1526. if (is_in)
  1527. head = &musb->in_bulk;
  1528. else
  1529. head = &musb->out_bulk;
  1530. goto success;
  1531. } else if (best_end < 0) {
  1532. return -ENOSPC;
  1533. }
  1534. idle = 1;
  1535. qh->mux = 0;
  1536. hw_ep = musb->endpoints + best_end;
  1537. musb->periodic[best_end] = qh;
  1538. DBG(4, "qh %p periodic slot %d\n", qh, best_end);
  1539. success:
  1540. if (head) {
  1541. idle = list_empty(head);
  1542. list_add_tail(&qh->ring, head);
  1543. qh->mux = 1;
  1544. }
  1545. qh->hw_ep = hw_ep;
  1546. qh->hep->hcpriv = qh;
  1547. if (idle)
  1548. musb_start_urb(musb, is_in, qh);
  1549. return 0;
  1550. }
  1551. static int musb_urb_enqueue(
  1552. struct usb_hcd *hcd,
  1553. struct urb *urb,
  1554. gfp_t mem_flags)
  1555. {
  1556. unsigned long flags;
  1557. struct musb *musb = hcd_to_musb(hcd);
  1558. struct usb_host_endpoint *hep = urb->ep;
  1559. struct musb_qh *qh = hep->hcpriv;
  1560. struct usb_endpoint_descriptor *epd = &hep->desc;
  1561. int ret;
  1562. unsigned type_reg;
  1563. unsigned interval;
  1564. /* host role must be active */
  1565. if (!is_host_active(musb) || !musb->is_active)
  1566. return -ENODEV;
  1567. spin_lock_irqsave(&musb->lock, flags);
  1568. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1569. spin_unlock_irqrestore(&musb->lock, flags);
  1570. if (ret)
  1571. return ret;
  1572. /* DMA mapping was already done, if needed, and this urb is on
  1573. * hep->urb_list ... so there's little to do unless hep wasn't
  1574. * yet scheduled onto a live qh.
  1575. *
  1576. * REVISIT best to keep hep->hcpriv valid until the endpoint gets
  1577. * disabled, testing for empty qh->ring and avoiding qh setup costs
  1578. * except for the first urb queued after a config change.
  1579. */
  1580. if (qh) {
  1581. urb->hcpriv = qh;
  1582. return 0;
  1583. }
  1584. /* Allocate and initialize qh, minimizing the work done each time
  1585. * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
  1586. *
  1587. * REVISIT consider a dedicated qh kmem_cache, so it's harder
  1588. * for bugs in other kernel code to break this driver...
  1589. */
  1590. qh = kzalloc(sizeof *qh, mem_flags);
  1591. if (!qh) {
  1592. spin_lock_irqsave(&musb->lock, flags);
  1593. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1594. spin_unlock_irqrestore(&musb->lock, flags);
  1595. return -ENOMEM;
  1596. }
  1597. qh->hep = hep;
  1598. qh->dev = urb->dev;
  1599. INIT_LIST_HEAD(&qh->ring);
  1600. qh->is_ready = 1;
  1601. qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
  1602. /* no high bandwidth support yet */
  1603. if (qh->maxpacket & ~0x7ff) {
  1604. ret = -EMSGSIZE;
  1605. goto done;
  1606. }
  1607. qh->epnum = usb_endpoint_num(epd);
  1608. qh->type = usb_endpoint_type(epd);
  1609. /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
  1610. qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
  1611. /* precompute rxtype/txtype/type0 register */
  1612. type_reg = (qh->type << 4) | qh->epnum;
  1613. switch (urb->dev->speed) {
  1614. case USB_SPEED_LOW:
  1615. type_reg |= 0xc0;
  1616. break;
  1617. case USB_SPEED_FULL:
  1618. type_reg |= 0x80;
  1619. break;
  1620. default:
  1621. type_reg |= 0x40;
  1622. }
  1623. qh->type_reg = type_reg;
  1624. /* precompute rxinterval/txinterval register */
  1625. interval = min((u8)16, epd->bInterval); /* log encoding */
  1626. switch (qh->type) {
  1627. case USB_ENDPOINT_XFER_INT:
  1628. /* fullspeed uses linear encoding */
  1629. if (USB_SPEED_FULL == urb->dev->speed) {
  1630. interval = epd->bInterval;
  1631. if (!interval)
  1632. interval = 1;
  1633. }
  1634. /* FALLTHROUGH */
  1635. case USB_ENDPOINT_XFER_ISOC:
  1636. /* iso always uses log encoding */
  1637. break;
  1638. default:
  1639. /* REVISIT we actually want to use NAK limits, hinting to the
  1640. * transfer scheduling logic to try some other qh, e.g. try
  1641. * for 2 msec first:
  1642. *
  1643. * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
  1644. *
  1645. * The downside of disabling this is that transfer scheduling
  1646. * gets VERY unfair for nonperiodic transfers; a misbehaving
  1647. * peripheral could make that hurt. Or for reads, one that's
  1648. * perfectly normal: network and other drivers keep reads
  1649. * posted at all times, having one pending for a week should
  1650. * be perfectly safe.
  1651. *
  1652. * The upside of disabling it is avoidng transfer scheduling
  1653. * code to put this aside for while.
  1654. */
  1655. interval = 0;
  1656. }
  1657. qh->intv_reg = interval;
  1658. /* precompute addressing for external hub/tt ports */
  1659. if (musb->is_multipoint) {
  1660. struct usb_device *parent = urb->dev->parent;
  1661. if (parent != hcd->self.root_hub) {
  1662. qh->h_addr_reg = (u8) parent->devnum;
  1663. /* set up tt info if needed */
  1664. if (urb->dev->tt) {
  1665. qh->h_port_reg = (u8) urb->dev->ttport;
  1666. if (urb->dev->tt->hub)
  1667. qh->h_addr_reg =
  1668. (u8) urb->dev->tt->hub->devnum;
  1669. if (urb->dev->tt->multi)
  1670. qh->h_addr_reg |= 0x80;
  1671. }
  1672. }
  1673. }
  1674. /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
  1675. * until we get real dma queues (with an entry for each urb/buffer),
  1676. * we only have work to do in the former case.
  1677. */
  1678. spin_lock_irqsave(&musb->lock, flags);
  1679. if (hep->hcpriv) {
  1680. /* some concurrent activity submitted another urb to hep...
  1681. * odd, rare, error prone, but legal.
  1682. */
  1683. kfree(qh);
  1684. ret = 0;
  1685. } else
  1686. ret = musb_schedule(musb, qh,
  1687. epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
  1688. if (ret == 0) {
  1689. urb->hcpriv = qh;
  1690. /* FIXME set urb->start_frame for iso/intr, it's tested in
  1691. * musb_start_urb(), but otherwise only konicawc cares ...
  1692. */
  1693. }
  1694. spin_unlock_irqrestore(&musb->lock, flags);
  1695. done:
  1696. if (ret != 0) {
  1697. spin_lock_irqsave(&musb->lock, flags);
  1698. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1699. spin_unlock_irqrestore(&musb->lock, flags);
  1700. kfree(qh);
  1701. }
  1702. return ret;
  1703. }
  1704. /*
  1705. * abort a transfer that's at the head of a hardware queue.
  1706. * called with controller locked, irqs blocked
  1707. * that hardware queue advances to the next transfer, unless prevented
  1708. */
  1709. static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
  1710. {
  1711. struct musb_hw_ep *ep = qh->hw_ep;
  1712. void __iomem *epio = ep->regs;
  1713. unsigned hw_end = ep->epnum;
  1714. void __iomem *regs = ep->musb->mregs;
  1715. u16 csr;
  1716. int status = 0;
  1717. musb_ep_select(regs, hw_end);
  1718. if (is_dma_capable()) {
  1719. struct dma_channel *dma;
  1720. dma = is_in ? ep->rx_channel : ep->tx_channel;
  1721. if (dma) {
  1722. status = ep->musb->dma_controller->channel_abort(dma);
  1723. DBG(status ? 1 : 3,
  1724. "abort %cX%d DMA for urb %p --> %d\n",
  1725. is_in ? 'R' : 'T', ep->epnum,
  1726. urb, status);
  1727. urb->actual_length += dma->actual_len;
  1728. }
  1729. }
  1730. /* turn off DMA requests, discard state, stop polling ... */
  1731. if (is_in) {
  1732. /* giveback saves bulk toggle */
  1733. csr = musb_h_flush_rxfifo(ep, 0);
  1734. /* REVISIT we still get an irq; should likely clear the
  1735. * endpoint's irq status here to avoid bogus irqs.
  1736. * clearing that status is platform-specific...
  1737. */
  1738. } else {
  1739. musb_h_tx_flush_fifo(ep);
  1740. csr = musb_readw(epio, MUSB_TXCSR);
  1741. csr &= ~(MUSB_TXCSR_AUTOSET
  1742. | MUSB_TXCSR_DMAENAB
  1743. | MUSB_TXCSR_H_RXSTALL
  1744. | MUSB_TXCSR_H_NAKTIMEOUT
  1745. | MUSB_TXCSR_H_ERROR
  1746. | MUSB_TXCSR_TXPKTRDY);
  1747. musb_writew(epio, MUSB_TXCSR, csr);
  1748. /* REVISIT may need to clear FLUSHFIFO ... */
  1749. musb_writew(epio, MUSB_TXCSR, csr);
  1750. /* flush cpu writebuffer */
  1751. csr = musb_readw(epio, MUSB_TXCSR);
  1752. }
  1753. if (status == 0)
  1754. musb_advance_schedule(ep->musb, urb, ep, is_in);
  1755. return status;
  1756. }
  1757. static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1758. {
  1759. struct musb *musb = hcd_to_musb(hcd);
  1760. struct musb_qh *qh;
  1761. struct list_head *sched;
  1762. unsigned long flags;
  1763. int ret;
  1764. DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
  1765. usb_pipedevice(urb->pipe),
  1766. usb_pipeendpoint(urb->pipe),
  1767. usb_pipein(urb->pipe) ? "in" : "out");
  1768. spin_lock_irqsave(&musb->lock, flags);
  1769. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1770. if (ret)
  1771. goto done;
  1772. qh = urb->hcpriv;
  1773. if (!qh)
  1774. goto done;
  1775. /* Any URB not actively programmed into endpoint hardware can be
  1776. * immediately given back; that's any URB not at the head of an
  1777. * endpoint queue, unless someday we get real DMA queues. And even
  1778. * if it's at the head, it might not be known to the hardware...
  1779. *
  1780. * Otherwise abort current transfer, pending dma, etc.; urb->status
  1781. * has already been updated. This is a synchronous abort; it'd be
  1782. * OK to hold off until after some IRQ, though.
  1783. */
  1784. if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
  1785. ret = -EINPROGRESS;
  1786. else {
  1787. switch (qh->type) {
  1788. case USB_ENDPOINT_XFER_CONTROL:
  1789. sched = &musb->control;
  1790. break;
  1791. case USB_ENDPOINT_XFER_BULK:
  1792. if (qh->mux == 1) {
  1793. if (usb_pipein(urb->pipe))
  1794. sched = &musb->in_bulk;
  1795. else
  1796. sched = &musb->out_bulk;
  1797. break;
  1798. }
  1799. default:
  1800. /* REVISIT when we get a schedule tree, periodic
  1801. * transfers won't always be at the head of a
  1802. * singleton queue...
  1803. */
  1804. sched = NULL;
  1805. break;
  1806. }
  1807. }
  1808. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  1809. if (ret < 0 || (sched && qh != first_qh(sched))) {
  1810. int ready = qh->is_ready;
  1811. ret = 0;
  1812. qh->is_ready = 0;
  1813. __musb_giveback(musb, urb, 0);
  1814. qh->is_ready = ready;
  1815. /* If nothing else (usually musb_giveback) is using it
  1816. * and its URB list has emptied, recycle this qh.
  1817. */
  1818. if (ready && list_empty(&qh->hep->urb_list)) {
  1819. qh->hep->hcpriv = NULL;
  1820. list_del(&qh->ring);
  1821. kfree(qh);
  1822. }
  1823. } else
  1824. ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
  1825. done:
  1826. spin_unlock_irqrestore(&musb->lock, flags);
  1827. return ret;
  1828. }
  1829. /* disable an endpoint */
  1830. static void
  1831. musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  1832. {
  1833. u8 epnum = hep->desc.bEndpointAddress;
  1834. unsigned long flags;
  1835. struct musb *musb = hcd_to_musb(hcd);
  1836. u8 is_in = epnum & USB_DIR_IN;
  1837. struct musb_qh *qh = hep->hcpriv;
  1838. struct urb *urb, *tmp;
  1839. struct list_head *sched;
  1840. if (!qh)
  1841. return;
  1842. spin_lock_irqsave(&musb->lock, flags);
  1843. switch (qh->type) {
  1844. case USB_ENDPOINT_XFER_CONTROL:
  1845. sched = &musb->control;
  1846. break;
  1847. case USB_ENDPOINT_XFER_BULK:
  1848. if (qh->mux == 1) {
  1849. if (is_in)
  1850. sched = &musb->in_bulk;
  1851. else
  1852. sched = &musb->out_bulk;
  1853. break;
  1854. }
  1855. default:
  1856. /* REVISIT when we get a schedule tree, periodic transfers
  1857. * won't always be at the head of a singleton queue...
  1858. */
  1859. sched = NULL;
  1860. break;
  1861. }
  1862. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  1863. /* kick first urb off the hardware, if needed */
  1864. qh->is_ready = 0;
  1865. if (!sched || qh == first_qh(sched)) {
  1866. urb = next_urb(qh);
  1867. /* make software (then hardware) stop ASAP */
  1868. if (!urb->unlinked)
  1869. urb->status = -ESHUTDOWN;
  1870. /* cleanup */
  1871. musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
  1872. } else
  1873. urb = NULL;
  1874. /* then just nuke all the others */
  1875. list_for_each_entry_safe_from(urb, tmp, &hep->urb_list, urb_list)
  1876. musb_giveback(qh, urb, -ESHUTDOWN);
  1877. spin_unlock_irqrestore(&musb->lock, flags);
  1878. }
  1879. static int musb_h_get_frame_number(struct usb_hcd *hcd)
  1880. {
  1881. struct musb *musb = hcd_to_musb(hcd);
  1882. return musb_readw(musb->mregs, MUSB_FRAME);
  1883. }
  1884. static int musb_h_start(struct usb_hcd *hcd)
  1885. {
  1886. struct musb *musb = hcd_to_musb(hcd);
  1887. /* NOTE: musb_start() is called when the hub driver turns
  1888. * on port power, or when (OTG) peripheral starts.
  1889. */
  1890. hcd->state = HC_STATE_RUNNING;
  1891. musb->port1_status = 0;
  1892. return 0;
  1893. }
  1894. static void musb_h_stop(struct usb_hcd *hcd)
  1895. {
  1896. musb_stop(hcd_to_musb(hcd));
  1897. hcd->state = HC_STATE_HALT;
  1898. }
  1899. static int musb_bus_suspend(struct usb_hcd *hcd)
  1900. {
  1901. struct musb *musb = hcd_to_musb(hcd);
  1902. if (musb->xceiv.state == OTG_STATE_A_SUSPEND)
  1903. return 0;
  1904. if (is_host_active(musb) && musb->is_active) {
  1905. WARNING("trying to suspend as %s is_active=%i\n",
  1906. otg_state_string(musb), musb->is_active);
  1907. return -EBUSY;
  1908. } else
  1909. return 0;
  1910. }
  1911. static int musb_bus_resume(struct usb_hcd *hcd)
  1912. {
  1913. /* resuming child port does the work */
  1914. return 0;
  1915. }
  1916. const struct hc_driver musb_hc_driver = {
  1917. .description = "musb-hcd",
  1918. .product_desc = "MUSB HDRC host driver",
  1919. .hcd_priv_size = sizeof(struct musb),
  1920. .flags = HCD_USB2 | HCD_MEMORY,
  1921. /* not using irq handler or reset hooks from usbcore, since
  1922. * those must be shared with peripheral code for OTG configs
  1923. */
  1924. .start = musb_h_start,
  1925. .stop = musb_h_stop,
  1926. .get_frame_number = musb_h_get_frame_number,
  1927. .urb_enqueue = musb_urb_enqueue,
  1928. .urb_dequeue = musb_urb_dequeue,
  1929. .endpoint_disable = musb_h_disable,
  1930. .hub_status_data = musb_hub_status_data,
  1931. .hub_control = musb_hub_control,
  1932. .bus_suspend = musb_bus_suspend,
  1933. .bus_resume = musb_bus_resume,
  1934. /* .start_port_reset = NULL, */
  1935. /* .hub_irq_enable = NULL, */
  1936. };