musbfsh_host.c 66 KB

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