musb_core.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MUSB OTG driver core code
  4. *
  5. * Copyright 2005 Mentor Graphics Corporation
  6. * Copyright (C) 2005-2006 by Texas Instruments
  7. * Copyright (C) 2006-2007 Nokia Corporation
  8. */
  9. /*
  10. * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
  11. *
  12. * This consists of a Host Controller Driver (HCD) and a peripheral
  13. * controller driver implementing the "Gadget" API; OTG support is
  14. * in the works. These are normal Linux-USB controller drivers which
  15. * use IRQs and have no dedicated thread.
  16. *
  17. * This version of the driver has only been used with products from
  18. * Texas Instruments. Those products integrate the Inventra logic
  19. * with other DMA, IRQ, and bus modules, as well as other logic that
  20. * needs to be reflected in this driver.
  21. *
  22. *
  23. * NOTE: the original Mentor code here was pretty much a collection
  24. * of mechanisms that don't seem to have been fully integrated/working
  25. * for *any* Linux kernel version. This version aims at Linux 2.6.now,
  26. * Key open issues include:
  27. *
  28. * - Lack of host-side transaction scheduling, for all transfer types.
  29. * The hardware doesn't do it; instead, software must.
  30. *
  31. * This is not an issue for OTG devices that don't support external
  32. * hubs, but for more "normal" USB hosts it's a user issue that the
  33. * "multipoint" support doesn't scale in the expected ways. That
  34. * includes DaVinci EVM in a common non-OTG mode.
  35. *
  36. * * Control and bulk use dedicated endpoints, and there's as
  37. * yet no mechanism to either (a) reclaim the hardware when
  38. * peripherals are NAKing, which gets complicated with bulk
  39. * endpoints, or (b) use more than a single bulk endpoint in
  40. * each direction.
  41. *
  42. * RESULT: one device may be perceived as blocking another one.
  43. *
  44. * * Interrupt and isochronous will dynamically allocate endpoint
  45. * hardware, but (a) there's no record keeping for bandwidth;
  46. * (b) in the common case that few endpoints are available, there
  47. * is no mechanism to reuse endpoints to talk to multiple devices.
  48. *
  49. * RESULT: At one extreme, bandwidth can be overcommitted in
  50. * some hardware configurations, no faults will be reported.
  51. * At the other extreme, the bandwidth capabilities which do
  52. * exist tend to be severely undercommitted. You can't yet hook
  53. * up both a keyboard and a mouse to an external USB hub.
  54. */
  55. /*
  56. * This gets many kinds of configuration information:
  57. * - Kconfig for everything user-configurable
  58. * - platform_device for addressing, irq, and platform_data
  59. * - platform_data is mostly for board-specific information
  60. * (plus recentrly, SOC or family details)
  61. *
  62. * Most of the conditional compilation will (someday) vanish.
  63. */
  64. #include <linux/module.h>
  65. #include <linux/kernel.h>
  66. #include <linux/sched.h>
  67. #include <linux/slab.h>
  68. #include <linux/list.h>
  69. #include <linux/kobject.h>
  70. #include <linux/prefetch.h>
  71. #include <linux/platform_device.h>
  72. #include <linux/io.h>
  73. #include <linux/dma-mapping.h>
  74. #include <linux/usb.h>
  75. #include <linux/usb/of.h>
  76. #include "musb_core.h"
  77. #include "musb_trace.h"
  78. #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
  79. #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
  80. #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
  81. #define MUSB_VERSION "6.0"
  82. #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
  83. #define MUSB_DRIVER_NAME "musb-hdrc"
  84. const char musb_driver_name[] = MUSB_DRIVER_NAME;
  85. MODULE_DESCRIPTION(DRIVER_INFO);
  86. MODULE_AUTHOR(DRIVER_AUTHOR);
  87. MODULE_LICENSE("GPL");
  88. MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
  89. static volatile u8 g_usb_abnormal = 0;
  90. /*-------------------------------------------------------------------------*/
  91. static inline struct musb *dev_to_musb(struct device *dev)
  92. {
  93. return dev_get_drvdata(dev);
  94. }
  95. enum musb_mode musb_get_mode(struct device *dev)
  96. {
  97. enum usb_dr_mode mode;
  98. mode = usb_get_dr_mode(dev);
  99. switch (mode) {
  100. case USB_DR_MODE_HOST:
  101. return MUSB_HOST;
  102. case USB_DR_MODE_PERIPHERAL:
  103. return MUSB_PERIPHERAL;
  104. case USB_DR_MODE_OTG:
  105. case USB_DR_MODE_UNKNOWN:
  106. default:
  107. return MUSB_OTG;
  108. }
  109. }
  110. EXPORT_SYMBOL_GPL(musb_get_mode);
  111. /*-------------------------------------------------------------------------*/
  112. static int musb_ulpi_read(struct usb_phy *phy, u32 reg)
  113. {
  114. void __iomem *addr = phy->io_priv;
  115. int i = 0;
  116. u8 r;
  117. u8 power;
  118. int ret;
  119. pm_runtime_get_sync(phy->io_dev);
  120. /* Make sure the transceiver is not in low power mode */
  121. power = musb_readb(addr, MUSB_POWER);
  122. power &= ~MUSB_POWER_SUSPENDM;
  123. musb_writeb(addr, MUSB_POWER, power);
  124. /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
  125. * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
  126. */
  127. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
  128. musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
  129. MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
  130. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  131. & MUSB_ULPI_REG_CMPLT)) {
  132. i++;
  133. if (i == 10000) {
  134. ret = -ETIMEDOUT;
  135. goto out;
  136. }
  137. }
  138. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  139. r &= ~MUSB_ULPI_REG_CMPLT;
  140. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  141. ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
  142. out:
  143. pm_runtime_put(phy->io_dev);
  144. return ret;
  145. }
  146. static int musb_ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
  147. {
  148. void __iomem *addr = phy->io_priv;
  149. int i = 0;
  150. u8 r = 0;
  151. u8 power;
  152. int ret = 0;
  153. pm_runtime_get_sync(phy->io_dev);
  154. /* Make sure the transceiver is not in low power mode */
  155. power = musb_readb(addr, MUSB_POWER);
  156. power &= ~MUSB_POWER_SUSPENDM;
  157. musb_writeb(addr, MUSB_POWER, power);
  158. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
  159. musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)val);
  160. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
  161. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  162. & MUSB_ULPI_REG_CMPLT)) {
  163. i++;
  164. if (i == 10000) {
  165. ret = -ETIMEDOUT;
  166. goto out;
  167. }
  168. }
  169. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  170. r &= ~MUSB_ULPI_REG_CMPLT;
  171. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  172. out:
  173. pm_runtime_put(phy->io_dev);
  174. return ret;
  175. }
  176. static struct usb_phy_io_ops musb_ulpi_access = {
  177. .read = musb_ulpi_read,
  178. .write = musb_ulpi_write,
  179. };
  180. /*-------------------------------------------------------------------------*/
  181. static u32 musb_default_fifo_offset(u8 epnum)
  182. {
  183. return 0x20 + (epnum * 4);
  184. }
  185. /* "flat" mapping: each endpoint has its own i/o address */
  186. static void musb_flat_ep_select(void __iomem *mbase, u8 epnum)
  187. {
  188. }
  189. static u32 musb_flat_ep_offset(u8 epnum, u16 offset)
  190. {
  191. return 0x100 + (0x10 * epnum) + offset;
  192. }
  193. /* "indexed" mapping: INDEX register controls register bank select */
  194. static void musb_indexed_ep_select(void __iomem *mbase, u8 epnum)
  195. {
  196. musb_writeb(mbase, MUSB_INDEX, epnum);
  197. }
  198. static u32 musb_indexed_ep_offset(u8 epnum, u16 offset)
  199. {
  200. return 0x10 + offset;
  201. }
  202. static u32 musb_default_busctl_offset(u8 epnum, u16 offset)
  203. {
  204. return 0x80 + (0x08 * epnum) + offset;
  205. }
  206. static u8 musb_default_readb(const void __iomem *addr, unsigned offset)
  207. {
  208. u8 data = __raw_readb(addr + offset);
  209. trace_musb_readb(__builtin_return_address(0), addr, offset, data);
  210. return data;
  211. }
  212. static void musb_default_writeb(void __iomem *addr, unsigned offset, u8 data)
  213. {
  214. trace_musb_writeb(__builtin_return_address(0), addr, offset, data);
  215. __raw_writeb(data, addr + offset);
  216. }
  217. static u16 musb_default_readw(const void __iomem *addr, unsigned offset)
  218. {
  219. u16 data = __raw_readw(addr + offset);
  220. trace_musb_readw(__builtin_return_address(0), addr, offset, data);
  221. return data;
  222. }
  223. static void musb_default_writew(void __iomem *addr, unsigned offset, u16 data)
  224. {
  225. trace_musb_writew(__builtin_return_address(0), addr, offset, data);
  226. __raw_writew(data, addr + offset);
  227. }
  228. /*
  229. * Load an endpoint's FIFO
  230. */
  231. static void musb_default_write_fifo(struct musb_hw_ep *hw_ep, u16 len,
  232. const u8 *src)
  233. {
  234. struct musb *musb = hw_ep->musb;
  235. void __iomem *fifo = hw_ep->fifo;
  236. if (unlikely(len == 0))
  237. return;
  238. prefetch((u8 *)src);
  239. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  240. 'T', hw_ep->epnum, fifo, len, src);
  241. /* we can't assume unaligned reads work */
  242. if (likely((0x01 & (unsigned long) src) == 0)) {
  243. u16 index = 0;
  244. /* best case is 32bit-aligned source address */
  245. if ((0x02 & (unsigned long) src) == 0) {
  246. if (len >= 4) {
  247. iowrite32_rep(fifo, src + index, len >> 2);
  248. index += len & ~0x03;
  249. }
  250. if (len & 0x02) {
  251. __raw_writew(*(u16 *)&src[index], fifo);
  252. index += 2;
  253. }
  254. } else {
  255. if (len >= 2) {
  256. iowrite16_rep(fifo, src + index, len >> 1);
  257. index += len & ~0x01;
  258. }
  259. }
  260. if (len & 0x01)
  261. __raw_writeb(src[index], fifo);
  262. } else {
  263. /* byte aligned */
  264. iowrite8_rep(fifo, src, len);
  265. }
  266. }
  267. /*
  268. * Unload an endpoint's FIFO
  269. */
  270. static void musb_default_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  271. {
  272. struct musb *musb = hw_ep->musb;
  273. void __iomem *fifo = hw_ep->fifo;
  274. if (unlikely(len == 0))
  275. return;
  276. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  277. 'R', hw_ep->epnum, fifo, len, dst);
  278. /* we can't assume unaligned writes work */
  279. if (likely((0x01 & (unsigned long) dst) == 0)) {
  280. u16 index = 0;
  281. /* best case is 32bit-aligned destination address */
  282. if ((0x02 & (unsigned long) dst) == 0) {
  283. if (len >= 4) {
  284. ioread32_rep(fifo, dst, len >> 2);
  285. index = len & ~0x03;
  286. }
  287. if (len & 0x02) {
  288. *(u16 *)&dst[index] = __raw_readw(fifo);
  289. index += 2;
  290. }
  291. } else {
  292. if (len >= 2) {
  293. ioread16_rep(fifo, dst, len >> 1);
  294. index = len & ~0x01;
  295. }
  296. }
  297. if (len & 0x01)
  298. dst[index] = __raw_readb(fifo);
  299. } else {
  300. /* byte aligned */
  301. ioread8_rep(fifo, dst, len);
  302. }
  303. }
  304. /*
  305. * Old style IO functions
  306. */
  307. u8 (*musb_readb)(const void __iomem *addr, unsigned offset);
  308. EXPORT_SYMBOL_GPL(musb_readb);
  309. void (*musb_writeb)(void __iomem *addr, unsigned offset, u8 data);
  310. EXPORT_SYMBOL_GPL(musb_writeb);
  311. u16 (*musb_readw)(const void __iomem *addr, unsigned offset);
  312. EXPORT_SYMBOL_GPL(musb_readw);
  313. void (*musb_writew)(void __iomem *addr, unsigned offset, u16 data);
  314. EXPORT_SYMBOL_GPL(musb_writew);
  315. u32 musb_readl(const void __iomem *addr, unsigned offset)
  316. {
  317. u32 data = __raw_readl(addr + offset);
  318. trace_musb_readl(__builtin_return_address(0), addr, offset, data);
  319. return data;
  320. }
  321. EXPORT_SYMBOL_GPL(musb_readl);
  322. void musb_writel(void __iomem *addr, unsigned offset, u32 data)
  323. {
  324. trace_musb_writel(__builtin_return_address(0), addr, offset, data);
  325. __raw_writel(data, addr + offset);
  326. }
  327. EXPORT_SYMBOL_GPL(musb_writel);
  328. #ifndef CONFIG_MUSB_PIO_ONLY
  329. struct dma_controller *
  330. (*musb_dma_controller_create)(struct musb *musb, void __iomem *base);
  331. EXPORT_SYMBOL(musb_dma_controller_create);
  332. void (*musb_dma_controller_destroy)(struct dma_controller *c);
  333. EXPORT_SYMBOL(musb_dma_controller_destroy);
  334. #endif
  335. /*
  336. * New style IO functions
  337. */
  338. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  339. {
  340. return hw_ep->musb->io.read_fifo(hw_ep, len, dst);
  341. }
  342. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  343. {
  344. return hw_ep->musb->io.write_fifo(hw_ep, len, src);
  345. }
  346. /*-------------------------------------------------------------------------*/
  347. /* for high speed test mode; see USB 2.0 spec 7.1.20 */
  348. static const u8 musb_test_packet[53] = {
  349. /* implicit SYNC then DATA0 to start */
  350. /* JKJKJKJK x9 */
  351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  352. /* JJKKJJKK x8 */
  353. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  354. /* JJJJKKKK x8 */
  355. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  356. /* JJJJJJJKKKKKKK x8 */
  357. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  358. /* JJJJJJJK x8 */
  359. 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
  360. /* JKKKKKKK x10, JK */
  361. 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
  362. /* implicit CRC16 then EOP to end */
  363. };
  364. void musb_load_testpacket(struct musb *musb)
  365. {
  366. void __iomem *regs = musb->endpoints[0].regs;
  367. musb_ep_select(musb->mregs, 0);
  368. musb_write_fifo(musb->control_ep,
  369. sizeof(musb_test_packet), musb_test_packet);
  370. musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
  371. }
  372. /*-------------------------------------------------------------------------*/
  373. /*
  374. * Handles OTG hnp timeouts, such as b_ase0_brst
  375. */
  376. static void musb_otg_timer_func(struct timer_list *t)
  377. {
  378. struct musb *musb = from_timer(musb, t, otg_timer);
  379. unsigned long flags;
  380. spin_lock_irqsave(&musb->lock, flags);
  381. switch (musb->xceiv->otg->state) {
  382. case OTG_STATE_B_WAIT_ACON:
  383. musb_dbg(musb,
  384. "HNP: b_wait_acon timeout; back to b_peripheral");
  385. musb_g_disconnect(musb);
  386. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  387. musb->is_active = 0;
  388. break;
  389. case OTG_STATE_A_SUSPEND:
  390. case OTG_STATE_A_WAIT_BCON:
  391. musb_dbg(musb, "HNP: %s timeout",
  392. usb_otg_state_string(musb->xceiv->otg->state));
  393. musb_platform_set_vbus(musb, 0);
  394. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
  395. break;
  396. default:
  397. musb_dbg(musb, "HNP: Unhandled mode %s",
  398. usb_otg_state_string(musb->xceiv->otg->state));
  399. }
  400. spin_unlock_irqrestore(&musb->lock, flags);
  401. }
  402. /*
  403. * Stops the HNP transition. Caller must take care of locking.
  404. */
  405. void musb_hnp_stop(struct musb *musb)
  406. {
  407. struct usb_hcd *hcd = musb->hcd;
  408. void __iomem *mbase = musb->mregs;
  409. u8 reg;
  410. musb_dbg(musb, "HNP: stop from %s",
  411. usb_otg_state_string(musb->xceiv->otg->state));
  412. switch (musb->xceiv->otg->state) {
  413. case OTG_STATE_A_PERIPHERAL:
  414. musb_g_disconnect(musb);
  415. musb_dbg(musb, "HNP: back to %s",
  416. usb_otg_state_string(musb->xceiv->otg->state));
  417. break;
  418. case OTG_STATE_B_HOST:
  419. musb_dbg(musb, "HNP: Disabling HR");
  420. if (hcd)
  421. hcd->self.is_b_host = 0;
  422. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  423. MUSB_DEV_MODE(musb);
  424. reg = musb_readb(mbase, MUSB_POWER);
  425. reg |= MUSB_POWER_SUSPENDM;
  426. musb_writeb(mbase, MUSB_POWER, reg);
  427. /* REVISIT: Start SESSION_REQUEST here? */
  428. break;
  429. default:
  430. musb_dbg(musb, "HNP: Stopping in unknown state %s",
  431. usb_otg_state_string(musb->xceiv->otg->state));
  432. }
  433. /*
  434. * When returning to A state after HNP, avoid hub_port_rebounce(),
  435. * which cause occasional OPT A "Did not receive reset after connect"
  436. * errors.
  437. */
  438. musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
  439. }
  440. static void musb_recover_from_babble(struct musb *musb);
  441. #if 0
  442. static void musb_handle_intr_resume(struct musb *musb, u8 devctl)
  443. {
  444. musb_dbg(musb, "RESUME (%s)",
  445. usb_otg_state_string(musb->xceiv->otg->state));
  446. if (devctl & MUSB_DEVCTL_HM) {
  447. switch (musb->xceiv->otg->state) {
  448. case OTG_STATE_A_SUSPEND:
  449. /* remote wakeup? */
  450. musb->port1_status |=
  451. (USB_PORT_STAT_C_SUSPEND << 16)
  452. | MUSB_PORT_STAT_RESUME;
  453. musb->rh_timer = jiffies
  454. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  455. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  456. musb->is_active = 1;
  457. musb_host_resume_root_hub(musb);
  458. schedule_delayed_work(&musb->finish_resume_work,
  459. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  460. break;
  461. case OTG_STATE_B_WAIT_ACON:
  462. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  463. musb->is_active = 1;
  464. MUSB_DEV_MODE(musb);
  465. break;
  466. default:
  467. WARNING("bogus %s RESUME (%s)\n",
  468. "host",
  469. usb_otg_state_string(musb->xceiv->otg->state));
  470. }
  471. } else {
  472. switch (musb->xceiv->otg->state) {
  473. case OTG_STATE_A_SUSPEND:
  474. /* possibly DISCONNECT is upcoming */
  475. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  476. musb_host_resume_root_hub(musb);
  477. break;
  478. case OTG_STATE_B_WAIT_ACON:
  479. case OTG_STATE_B_PERIPHERAL:
  480. /* disconnect while suspended? we may
  481. * not get a disconnect irq...
  482. */
  483. if ((devctl & MUSB_DEVCTL_VBUS)
  484. != (3 << MUSB_DEVCTL_VBUS_SHIFT)
  485. ) {
  486. musb->int_usb |= MUSB_INTR_DISCONNECT;
  487. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  488. break;
  489. }
  490. musb_g_resume(musb);
  491. break;
  492. case OTG_STATE_B_IDLE:
  493. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  494. break;
  495. default:
  496. WARNING("bogus %s RESUME (%s)\n",
  497. "peripheral",
  498. usb_otg_state_string(musb->xceiv->otg->state));
  499. }
  500. }
  501. }
  502. /* return IRQ_HANDLED to tell the caller to return immediately */
  503. static irqreturn_t musb_handle_intr_sessreq(struct musb *musb, u8 devctl)
  504. {
  505. void __iomem *mbase = musb->mregs;
  506. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
  507. && (devctl & MUSB_DEVCTL_BDEVICE)) {
  508. musb_dbg(musb, "SessReq while on B state");
  509. return IRQ_HANDLED;
  510. }
  511. musb_dbg(musb, "SESSION_REQUEST (%s)",
  512. usb_otg_state_string(musb->xceiv->otg->state));
  513. /* IRQ arrives from ID pin sense or (later, if VBUS power
  514. * is removed) SRP. responses are time critical:
  515. * - turn on VBUS (with silicon-specific mechanism)
  516. * - go through A_WAIT_VRISE
  517. * - ... to A_WAIT_BCON.
  518. * a_wait_vrise_tmout triggers VBUS_ERROR transitions
  519. */
  520. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  521. musb->ep0_stage = MUSB_EP0_START;
  522. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  523. MUSB_HST_MODE(musb);
  524. musb_platform_set_vbus(musb, 1);
  525. return IRQ_NONE;
  526. }
  527. static void musb_handle_intr_vbuserr(struct musb *musb, u8 devctl)
  528. {
  529. int ignore = 0;
  530. /* During connection as an A-Device, we may see a short
  531. * current spikes causing voltage drop, because of cable
  532. * and peripheral capacitance combined with vbus draw.
  533. * (So: less common with truly self-powered devices, where
  534. * vbus doesn't act like a power supply.)
  535. *
  536. * Such spikes are short; usually less than ~500 usec, max
  537. * of ~2 msec. That is, they're not sustained overcurrent
  538. * errors, though they're reported using VBUSERROR irqs.
  539. *
  540. * Workarounds: (a) hardware: use self powered devices.
  541. * (b) software: ignore non-repeated VBUS errors.
  542. *
  543. * REVISIT: do delays from lots of DEBUG_KERNEL checks
  544. * make trouble here, keeping VBUS < 4.4V ?
  545. */
  546. switch (musb->xceiv->otg->state) {
  547. case OTG_STATE_A_HOST:
  548. /* recovery is dicey once we've gotten past the
  549. * initial stages of enumeration, but if VBUS
  550. * stayed ok at the other end of the link, and
  551. * another reset is due (at least for high speed,
  552. * to redo the chirp etc), it might work OK...
  553. */
  554. case OTG_STATE_A_WAIT_BCON:
  555. case OTG_STATE_A_WAIT_VRISE:
  556. if (musb->vbuserr_retry) {
  557. void __iomem *mbase = musb->mregs;
  558. musb->vbuserr_retry--;
  559. ignore = 1;
  560. devctl |= MUSB_DEVCTL_SESSION;
  561. musb_writeb(mbase, MUSB_DEVCTL, devctl);
  562. } else {
  563. musb->port1_status |=
  564. USB_PORT_STAT_OVERCURRENT
  565. | (USB_PORT_STAT_C_OVERCURRENT << 16);
  566. }
  567. break;
  568. default:
  569. break;
  570. }
  571. dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
  572. "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
  573. usb_otg_state_string(musb->xceiv->otg->state),
  574. devctl,
  575. ({ char *s;
  576. switch (devctl & MUSB_DEVCTL_VBUS) {
  577. case 0 << MUSB_DEVCTL_VBUS_SHIFT:
  578. s = "<SessEnd"; break;
  579. case 1 << MUSB_DEVCTL_VBUS_SHIFT:
  580. s = "<AValid"; break;
  581. case 2 << MUSB_DEVCTL_VBUS_SHIFT:
  582. s = "<VBusValid"; break;
  583. /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
  584. default:
  585. s = "VALID"; break;
  586. } s; }),
  587. VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
  588. musb->port1_status);
  589. /* go through A_WAIT_VFALL then start a new session */
  590. if (!ignore)
  591. musb_platform_set_vbus(musb, 0);
  592. }
  593. static void musb_handle_intr_suspend(struct musb *musb, u8 devctl)
  594. {
  595. musb_dbg(musb, "SUSPEND (%s) devctl %02x",
  596. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  597. switch (musb->xceiv->otg->state) {
  598. case OTG_STATE_A_PERIPHERAL:
  599. /* We also come here if the cable is removed, since
  600. * this silicon doesn't report ID-no-longer-grounded.
  601. *
  602. * We depend on T(a_wait_bcon) to shut us down, and
  603. * hope users don't do anything dicey during this
  604. * undesired detour through A_WAIT_BCON.
  605. */
  606. musb_hnp_stop(musb);
  607. musb_host_resume_root_hub(musb);
  608. musb_root_disconnect(musb);
  609. musb_platform_try_idle(musb, jiffies
  610. + msecs_to_jiffies(musb->a_wait_bcon
  611. ? : OTG_TIME_A_WAIT_BCON));
  612. break;
  613. case OTG_STATE_B_IDLE:
  614. if (!musb->is_active)
  615. break;
  616. /* fall through */
  617. case OTG_STATE_B_PERIPHERAL:
  618. musb_g_suspend(musb);
  619. musb->is_active = musb->g.b_hnp_enable;
  620. if (musb->is_active) {
  621. musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
  622. musb_dbg(musb, "HNP: Setting timer for b_ase0_brst");
  623. mod_timer(&musb->otg_timer, jiffies
  624. + msecs_to_jiffies(
  625. OTG_TIME_B_ASE0_BRST));
  626. }
  627. break;
  628. case OTG_STATE_A_WAIT_BCON:
  629. if (musb->a_wait_bcon != 0)
  630. musb_platform_try_idle(musb, jiffies
  631. + msecs_to_jiffies(musb->a_wait_bcon));
  632. break;
  633. case OTG_STATE_A_HOST:
  634. musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
  635. musb->is_active = musb->hcd->self.b_hnp_enable;
  636. break;
  637. case OTG_STATE_B_HOST:
  638. /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
  639. musb_dbg(musb, "REVISIT: SUSPEND as B_HOST");
  640. break;
  641. default:
  642. /* "should not happen" */
  643. musb->is_active = 0;
  644. break;
  645. }
  646. }
  647. static void musb_handle_intr_connect(struct musb *musb, u8 devctl, u8 int_usb)
  648. {
  649. struct usb_hcd *hcd = musb->hcd;
  650. musb->is_active = 1;
  651. musb->ep0_stage = MUSB_EP0_START;
  652. musb->intrtxe = musb->epmask;
  653. musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
  654. musb->intrrxe = musb->epmask & 0xfffe;
  655. musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
  656. musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
  657. musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
  658. |USB_PORT_STAT_HIGH_SPEED
  659. |USB_PORT_STAT_ENABLE
  660. );
  661. musb->port1_status |= USB_PORT_STAT_CONNECTION
  662. |(USB_PORT_STAT_C_CONNECTION << 16);
  663. /* high vs full speed is just a guess until after reset */
  664. if (devctl & MUSB_DEVCTL_LSDEV)
  665. musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
  666. /* indicate new connection to OTG machine */
  667. switch (musb->xceiv->otg->state) {
  668. case OTG_STATE_B_PERIPHERAL:
  669. if (int_usb & MUSB_INTR_SUSPEND) {
  670. musb_dbg(musb, "HNP: SUSPEND+CONNECT, now b_host");
  671. int_usb &= ~MUSB_INTR_SUSPEND;
  672. goto b_host;
  673. } else
  674. musb_dbg(musb, "CONNECT as b_peripheral???");
  675. break;
  676. case OTG_STATE_B_WAIT_ACON:
  677. musb_dbg(musb, "HNP: CONNECT, now b_host");
  678. b_host:
  679. musb->xceiv->otg->state = OTG_STATE_B_HOST;
  680. if (musb->hcd)
  681. musb->hcd->self.is_b_host = 1;
  682. del_timer(&musb->otg_timer);
  683. break;
  684. default:
  685. if ((devctl & MUSB_DEVCTL_VBUS)
  686. == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
  687. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  688. if (hcd)
  689. hcd->self.is_b_host = 0;
  690. }
  691. break;
  692. }
  693. musb_host_poke_root_hub(musb);
  694. musb_dbg(musb, "CONNECT (%s) devctl %02x",
  695. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  696. }
  697. static void musb_handle_intr_disconnect(struct musb *musb, u8 devctl)
  698. {
  699. musb_dbg(musb, "DISCONNECT (%s) as %s, devctl %02x",
  700. usb_otg_state_string(musb->xceiv->otg->state),
  701. MUSB_MODE(musb), devctl);
  702. switch (musb->xceiv->otg->state) {
  703. case OTG_STATE_A_HOST:
  704. case OTG_STATE_A_SUSPEND:
  705. musb_host_resume_root_hub(musb);
  706. musb_root_disconnect(musb);
  707. if (musb->a_wait_bcon != 0)
  708. musb_platform_try_idle(musb, jiffies
  709. + msecs_to_jiffies(musb->a_wait_bcon));
  710. break;
  711. case OTG_STATE_B_HOST:
  712. /* REVISIT this behaves for "real disconnect"
  713. * cases; make sure the other transitions from
  714. * from B_HOST act right too. The B_HOST code
  715. * in hnp_stop() is currently not used...
  716. */
  717. musb_root_disconnect(musb);
  718. if (musb->hcd)
  719. musb->hcd->self.is_b_host = 0;
  720. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  721. MUSB_DEV_MODE(musb);
  722. musb_g_disconnect(musb);
  723. break;
  724. case OTG_STATE_A_PERIPHERAL:
  725. musb_hnp_stop(musb);
  726. musb_root_disconnect(musb);
  727. /* FALLTHROUGH */
  728. case OTG_STATE_B_WAIT_ACON:
  729. /* FALLTHROUGH */
  730. case OTG_STATE_B_PERIPHERAL:
  731. case OTG_STATE_B_IDLE:
  732. musb_g_disconnect(musb);
  733. break;
  734. default:
  735. WARNING("unhandled DISCONNECT transition (%s)\n",
  736. usb_otg_state_string(musb->xceiv->otg->state));
  737. break;
  738. }
  739. }
  740. /*
  741. * mentor saves a bit: bus reset and babble share the same irq.
  742. * only host sees babble; only peripheral sees bus reset.
  743. */
  744. static void musb_handle_intr_reset(struct musb *musb)
  745. {
  746. if (is_host_active(musb)) {
  747. /*
  748. * When BABBLE happens what we can depends on which
  749. * platform MUSB is running, because some platforms
  750. * implemented proprietary means for 'recovering' from
  751. * Babble conditions. One such platform is AM335x. In
  752. * most cases, however, the only thing we can do is
  753. * drop the session.
  754. */
  755. dev_err(musb->controller, "Babble\n");
  756. musb_recover_from_babble(musb);
  757. } else {
  758. musb_dbg(musb, "BUS RESET as %s",
  759. usb_otg_state_string(musb->xceiv->otg->state));
  760. switch (musb->xceiv->otg->state) {
  761. case OTG_STATE_A_SUSPEND:
  762. musb_g_reset(musb);
  763. /* FALLTHROUGH */
  764. case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
  765. /* never use invalid T(a_wait_bcon) */
  766. musb_dbg(musb, "HNP: in %s, %d msec timeout",
  767. usb_otg_state_string(musb->xceiv->otg->state),
  768. TA_WAIT_BCON(musb));
  769. mod_timer(&musb->otg_timer, jiffies
  770. + msecs_to_jiffies(TA_WAIT_BCON(musb)));
  771. break;
  772. case OTG_STATE_A_PERIPHERAL:
  773. del_timer(&musb->otg_timer);
  774. musb_g_reset(musb);
  775. break;
  776. case OTG_STATE_B_WAIT_ACON:
  777. musb_dbg(musb, "HNP: RESET (%s), to b_peripheral",
  778. usb_otg_state_string(musb->xceiv->otg->state));
  779. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  780. musb_g_reset(musb);
  781. break;
  782. case OTG_STATE_B_IDLE:
  783. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  784. /* FALLTHROUGH */
  785. case OTG_STATE_B_PERIPHERAL:
  786. musb_g_reset(musb);
  787. break;
  788. default:
  789. musb_dbg(musb, "Unhandled BUS RESET as %s",
  790. usb_otg_state_string(musb->xceiv->otg->state));
  791. }
  792. }
  793. }
  794. #endif
  795. /*
  796. * Interrupt Service Routine to record USB "global" interrupts.
  797. * Since these do not happen often and signify things of
  798. * paramount importance, it seems OK to check them individually;
  799. * the order of the tests is specified in the manual
  800. *
  801. * @param musb instance pointer
  802. * @param int_usb register contents
  803. * @param devctl
  804. * @param power
  805. */
  806. #if NICHOLAS_ADD
  807. extern void musb_dma_channel_release(struct musb *musb);
  808. #endif
  809. static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
  810. u8 devctl)
  811. {
  812. irqreturn_t handled = IRQ_NONE;
  813. musb_dbg(musb, "<== DevCtl=%02x, int_usb=0x%x", devctl, int_usb);
  814. /* in host mode, the peripheral may issue remote wakeup.
  815. * in peripheral mode, the host may resume the link.
  816. * spurious RESUME irqs happen too, paired with SUSPEND.
  817. */
  818. if (int_usb & MUSB_INTR_RESUME) {
  819. handled = IRQ_HANDLED;
  820. musb_dbg(musb, "RESUME (%s)",
  821. usb_otg_state_string(musb->xceiv->otg->state));
  822. if (devctl & MUSB_DEVCTL_HM) {
  823. switch (musb->xceiv->otg->state) {
  824. case OTG_STATE_A_SUSPEND:
  825. /* remote wakeup? */
  826. musb->port1_status |=
  827. (USB_PORT_STAT_C_SUSPEND << 16)
  828. | MUSB_PORT_STAT_RESUME;
  829. musb->rh_timer = jiffies
  830. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  831. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  832. musb->is_active = 1;
  833. musb_host_resume_root_hub(musb);
  834. schedule_delayed_work(&musb->finish_resume_work,
  835. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  836. break;
  837. case OTG_STATE_B_WAIT_ACON:
  838. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  839. musb->is_active = 1;
  840. MUSB_DEV_MODE(musb);
  841. break;
  842. default:
  843. WARNING("bogus %s RESUME (%s)\n",
  844. "host",
  845. usb_otg_state_string(musb->xceiv->otg->state));
  846. }
  847. } else {
  848. switch (musb->xceiv->otg->state) {
  849. case OTG_STATE_A_SUSPEND:
  850. /* possibly DISCONNECT is upcoming */
  851. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  852. musb_host_resume_root_hub(musb);
  853. break;
  854. case OTG_STATE_B_WAIT_ACON:
  855. case OTG_STATE_B_PERIPHERAL:
  856. /* disconnect while suspended? we may
  857. * not get a disconnect irq...
  858. */
  859. if ((devctl & MUSB_DEVCTL_VBUS)
  860. != (3 << MUSB_DEVCTL_VBUS_SHIFT)
  861. ) {
  862. musb->int_usb |= MUSB_INTR_DISCONNECT;
  863. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  864. break;
  865. }
  866. musb_g_resume(musb);
  867. break;
  868. case OTG_STATE_B_IDLE:
  869. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  870. break;
  871. default:
  872. WARNING("bogus %s RESUME (%s)\n",
  873. "peripheral",
  874. usb_otg_state_string(musb->xceiv->otg->state));
  875. }
  876. }
  877. }
  878. /* see manual for the order of the tests */
  879. if (int_usb & MUSB_INTR_SESSREQ) {
  880. void __iomem *mbase = musb->mregs;
  881. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
  882. && (devctl & MUSB_DEVCTL_BDEVICE)) {
  883. musb_dbg(musb, "SessReq while on B state");
  884. return IRQ_HANDLED;
  885. }
  886. musb_dbg(musb, "SESSION_REQUEST (%s)",
  887. usb_otg_state_string(musb->xceiv->otg->state));
  888. /* IRQ arrives from ID pin sense or (later, if VBUS power
  889. * is removed) SRP. responses are time critical:
  890. * - turn on VBUS (with silicon-specific mechanism)
  891. * - go through A_WAIT_VRISE
  892. * - ... to A_WAIT_BCON.
  893. * a_wait_vrise_tmout triggers VBUS_ERROR transitions
  894. */
  895. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  896. musb->ep0_stage = MUSB_EP0_START;
  897. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  898. MUSB_HST_MODE(musb);
  899. musb_platform_set_vbus(musb, 1);
  900. handled = IRQ_HANDLED;
  901. }
  902. if (int_usb & MUSB_INTR_VBUSERROR) {
  903. int ignore = 0;
  904. /* During connection as an A-Device, we may see a short
  905. * current spikes causing voltage drop, because of cable
  906. * and peripheral capacitance combined with vbus draw.
  907. * (So: less common with truly self-powered devices, where
  908. * vbus doesn't act like a power supply.)
  909. *
  910. * Such spikes are short; usually less than ~500 usec, max
  911. * of ~2 msec. That is, they're not sustained overcurrent
  912. * errors, though they're reported using VBUSERROR irqs.
  913. *
  914. * Workarounds: (a) hardware: use self powered devices.
  915. * (b) software: ignore non-repeated VBUS errors.
  916. *
  917. * REVISIT: do delays from lots of DEBUG_KERNEL checks
  918. * make trouble here, keeping VBUS < 4.4V ?
  919. */
  920. switch (musb->xceiv->otg->state) {
  921. case OTG_STATE_A_HOST:
  922. /* recovery is dicey once we've gotten past the
  923. * initial stages of enumeration, but if VBUS
  924. * stayed ok at the other end of the link, and
  925. * another reset is due (at least for high speed,
  926. * to redo the chirp etc), it might work OK...
  927. */
  928. case OTG_STATE_A_WAIT_BCON:
  929. case OTG_STATE_A_WAIT_VRISE:
  930. if (musb->vbuserr_retry) {
  931. void __iomem *mbase = musb->mregs;
  932. musb->vbuserr_retry--;
  933. ignore = 1;
  934. devctl |= MUSB_DEVCTL_SESSION;
  935. musb_writeb(mbase, MUSB_DEVCTL, devctl);
  936. } else {
  937. musb->port1_status |=
  938. USB_PORT_STAT_OVERCURRENT
  939. | (USB_PORT_STAT_C_OVERCURRENT << 16);
  940. }
  941. break;
  942. default:
  943. break;
  944. }
  945. dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
  946. "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
  947. usb_otg_state_string(musb->xceiv->otg->state),
  948. devctl,
  949. ({ char *s;
  950. switch (devctl & MUSB_DEVCTL_VBUS) {
  951. case 0 << MUSB_DEVCTL_VBUS_SHIFT:
  952. s = "<SessEnd"; break;
  953. case 1 << MUSB_DEVCTL_VBUS_SHIFT:
  954. s = "<AValid"; break;
  955. case 2 << MUSB_DEVCTL_VBUS_SHIFT:
  956. s = "<VBusValid"; break;
  957. /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
  958. default:
  959. s = "VALID"; break;
  960. } s; }),
  961. VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
  962. musb->port1_status);
  963. /* go through A_WAIT_VFALL then start a new session */
  964. if (!ignore)
  965. musb_platform_set_vbus(musb, 0);
  966. handled = IRQ_HANDLED;
  967. }
  968. if (int_usb & MUSB_INTR_SUSPEND) {
  969. musb_dbg(musb, "SUSPEND (%s) devctl %02x",
  970. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  971. handled = IRQ_HANDLED;
  972. switch (musb->xceiv->otg->state) {
  973. case OTG_STATE_A_PERIPHERAL:
  974. /* We also come here if the cable is removed, since
  975. * this silicon doesn't report ID-no-longer-grounded.
  976. *
  977. * We depend on T(a_wait_bcon) to shut us down, and
  978. * hope users don't do anything dicey during this
  979. * undesired detour through A_WAIT_BCON.
  980. */
  981. musb_hnp_stop(musb);
  982. musb_host_resume_root_hub(musb);
  983. musb_root_disconnect(musb);
  984. musb_platform_try_idle(musb, jiffies
  985. + msecs_to_jiffies(musb->a_wait_bcon
  986. ? : OTG_TIME_A_WAIT_BCON));
  987. break;
  988. case OTG_STATE_B_IDLE:
  989. if (!musb->is_active)
  990. break;
  991. /* fall through */
  992. case OTG_STATE_B_PERIPHERAL:
  993. musb_g_suspend(musb);
  994. musb->is_active = musb->g.b_hnp_enable;
  995. if (musb->is_active) {
  996. musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
  997. musb_dbg(musb, "HNP: Setting timer for b_ase0_brst");
  998. mod_timer(&musb->otg_timer, jiffies
  999. + msecs_to_jiffies(
  1000. OTG_TIME_B_ASE0_BRST));
  1001. }
  1002. break;
  1003. case OTG_STATE_A_WAIT_BCON:
  1004. if (musb->a_wait_bcon != 0)
  1005. musb_platform_try_idle(musb, jiffies
  1006. + msecs_to_jiffies(musb->a_wait_bcon));
  1007. break;
  1008. case OTG_STATE_A_HOST:
  1009. musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
  1010. musb->is_active = musb->hcd->self.b_hnp_enable;
  1011. break;
  1012. case OTG_STATE_B_HOST:
  1013. /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
  1014. musb_dbg(musb, "REVISIT: SUSPEND as B_HOST");
  1015. break;
  1016. default:
  1017. /* "should not happen" */
  1018. musb->is_active = 0;
  1019. break;
  1020. }
  1021. }
  1022. if (int_usb & MUSB_INTR_CONNECT) {
  1023. struct usb_hcd *hcd = musb->hcd;
  1024. #if NICHOLAS_ADD
  1025. musb_dma_channel_release(musb);
  1026. #endif
  1027. handled = IRQ_HANDLED;
  1028. musb->is_active = 1;
  1029. musb->ep0_stage = MUSB_EP0_START;
  1030. musb->intrtxe = musb->epmask;
  1031. musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
  1032. musb->intrrxe = musb->epmask & 0xfffe;
  1033. musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
  1034. musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
  1035. musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
  1036. |USB_PORT_STAT_HIGH_SPEED
  1037. |USB_PORT_STAT_ENABLE
  1038. );
  1039. musb->port1_status |= USB_PORT_STAT_CONNECTION
  1040. |(USB_PORT_STAT_C_CONNECTION << 16);
  1041. /* high vs full speed is just a guess until after reset */
  1042. if (devctl & MUSB_DEVCTL_LSDEV)
  1043. musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
  1044. /* indicate new connection to OTG machine */
  1045. switch (musb->xceiv->otg->state) {
  1046. case OTG_STATE_B_PERIPHERAL:
  1047. if (int_usb & MUSB_INTR_SUSPEND) {
  1048. musb_dbg(musb, "HNP: SUSPEND+CONNECT, now b_host");
  1049. int_usb &= ~MUSB_INTR_SUSPEND;
  1050. goto b_host;
  1051. } else
  1052. musb_dbg(musb, "CONNECT as b_peripheral???");
  1053. break;
  1054. case OTG_STATE_B_WAIT_ACON:
  1055. musb_dbg(musb, "HNP: CONNECT, now b_host");
  1056. b_host:
  1057. musb->xceiv->otg->state = OTG_STATE_B_HOST;
  1058. if (musb->hcd)
  1059. musb->hcd->self.is_b_host = 1;
  1060. del_timer(&musb->otg_timer);
  1061. break;
  1062. default:
  1063. if ((devctl & MUSB_DEVCTL_VBUS)
  1064. == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
  1065. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  1066. if (hcd)
  1067. hcd->self.is_b_host = 0;
  1068. }
  1069. break;
  1070. }
  1071. musb_host_poke_root_hub(musb);
  1072. musb_dbg(musb, "CONNECT (%s) devctl %02x",
  1073. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  1074. }
  1075. if (int_usb & MUSB_INTR_DISCONNECT) {
  1076. musb_dbg(musb, "DISCONNECT (%s) as %s, devctl %02x",
  1077. usb_otg_state_string(musb->xceiv->otg->state),
  1078. MUSB_MODE(musb), devctl);
  1079. handled = IRQ_HANDLED;
  1080. switch (musb->xceiv->otg->state) {
  1081. case OTG_STATE_A_HOST:
  1082. //case OTG_STATE_A_WAIT_BCON:
  1083. case OTG_STATE_A_SUSPEND:
  1084. musb_host_resume_root_hub(musb);
  1085. musb_root_disconnect(musb);
  1086. if (musb->a_wait_bcon != 0)
  1087. musb_platform_try_idle(musb, jiffies
  1088. + msecs_to_jiffies(musb->a_wait_bcon));
  1089. break;
  1090. case OTG_STATE_B_HOST:
  1091. /* REVISIT this behaves for "real disconnect"
  1092. * cases; make sure the other transitions from
  1093. * from B_HOST act right too. The B_HOST code
  1094. * in hnp_stop() is currently not used...
  1095. */
  1096. musb_root_disconnect(musb);
  1097. if (musb->hcd)
  1098. musb->hcd->self.is_b_host = 0;
  1099. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  1100. MUSB_DEV_MODE(musb);
  1101. musb_g_disconnect(musb);
  1102. break;
  1103. case OTG_STATE_A_PERIPHERAL:
  1104. musb_hnp_stop(musb);
  1105. musb_root_disconnect(musb);
  1106. /* FALLTHROUGH */
  1107. case OTG_STATE_B_WAIT_ACON:
  1108. /* FALLTHROUGH */
  1109. case OTG_STATE_B_PERIPHERAL:
  1110. case OTG_STATE_B_IDLE:
  1111. musb_g_disconnect(musb);
  1112. break;
  1113. default:
  1114. WARNING("unhandled DISCONNECT transition (%s)\n",
  1115. usb_otg_state_string(musb->xceiv->otg->state));
  1116. break;
  1117. }
  1118. }
  1119. /* mentor saves a bit: bus reset and babble share the same irq.
  1120. * only host sees babble; only peripheral sees bus reset.
  1121. */
  1122. if (int_usb & MUSB_INTR_RESET) {
  1123. handled = IRQ_HANDLED;
  1124. if (is_host_active(musb)) {
  1125. /*
  1126. * When BABBLE happens what we can depends on which
  1127. * platform MUSB is running, because some platforms
  1128. * implemented proprietary means for 'recovering' from
  1129. * Babble conditions. One such platform is AM335x. In
  1130. * most cases, however, the only thing we can do is
  1131. * drop the session.
  1132. */
  1133. dev_err(musb->controller, "Babble\n");
  1134. musb_recover_from_babble(musb);
  1135. } else {
  1136. musb_dbg(musb, "BUS RESET as %s",
  1137. usb_otg_state_string(musb->xceiv->otg->state));
  1138. switch (musb->xceiv->otg->state) {
  1139. case OTG_STATE_A_SUSPEND:
  1140. musb_g_reset(musb);
  1141. /* FALLTHROUGH */
  1142. case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
  1143. /* never use invalid T(a_wait_bcon) */
  1144. musb_dbg(musb, "HNP: in %s, %d msec timeout",
  1145. usb_otg_state_string(musb->xceiv->otg->state),
  1146. TA_WAIT_BCON(musb));
  1147. mod_timer(&musb->otg_timer, jiffies
  1148. + msecs_to_jiffies(TA_WAIT_BCON(musb)));
  1149. break;
  1150. case OTG_STATE_A_PERIPHERAL:
  1151. del_timer(&musb->otg_timer);
  1152. musb_g_reset(musb);
  1153. break;
  1154. case OTG_STATE_B_WAIT_ACON:
  1155. musb_dbg(musb, "HNP: RESET (%s), to b_peripheral",
  1156. usb_otg_state_string(musb->xceiv->otg->state));
  1157. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  1158. musb_g_reset(musb);
  1159. break;
  1160. case OTG_STATE_B_IDLE:
  1161. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  1162. /* FALLTHROUGH */
  1163. case OTG_STATE_B_PERIPHERAL:
  1164. musb_g_reset(musb);
  1165. break;
  1166. default:
  1167. musb_dbg(musb, "Unhandled BUS RESET as %s",
  1168. usb_otg_state_string(musb->xceiv->otg->state));
  1169. }
  1170. }
  1171. }
  1172. #if 0
  1173. /* REVISIT ... this would be for multiplexing periodic endpoints, or
  1174. * supporting transfer phasing to prevent exceeding ISO bandwidth
  1175. * limits of a given frame or microframe.
  1176. *
  1177. * It's not needed for peripheral side, which dedicates endpoints;
  1178. * though it _might_ use SOF irqs for other purposes.
  1179. *
  1180. * And it's not currently needed for host side, which also dedicates
  1181. * endpoints, relies on TX/RX interval registers, and isn't claimed
  1182. * to support ISO transfers yet.
  1183. */
  1184. if (int_usb & MUSB_INTR_SOF) {
  1185. void __iomem *mbase = musb->mregs;
  1186. struct musb_hw_ep *ep;
  1187. u8 epnum;
  1188. u16 frame;
  1189. dev_dbg(musb->controller, "START_OF_FRAME\n");
  1190. handled = IRQ_HANDLED;
  1191. /* start any periodic Tx transfers waiting for current frame */
  1192. frame = musb_readw(mbase, MUSB_FRAME);
  1193. ep = musb->endpoints;
  1194. for (epnum = 1; (epnum < musb->nr_endpoints)
  1195. && (musb->epmask >= (1 << epnum));
  1196. epnum++, ep++) {
  1197. /*
  1198. * FIXME handle framecounter wraps (12 bits)
  1199. * eliminate duplicated StartUrb logic
  1200. */
  1201. if (ep->dwWaitFrame >= frame) {
  1202. ep->dwWaitFrame = 0;
  1203. pr_debug("SOF --> periodic TX%s on %d\n",
  1204. ep->tx_channel ? " DMA" : "",
  1205. epnum);
  1206. if (!ep->tx_channel)
  1207. musb_h_tx_start(musb, epnum);
  1208. else
  1209. cppi_hostdma_start(musb, epnum);
  1210. }
  1211. } /* end of for loop */
  1212. }
  1213. #endif
  1214. if((int_usb&MUSB_INTR_VBUSERROR)&&(musb->xceiv->otg->state == OTG_STATE_A_HOST))
  1215. {
  1216. g_usb_abnormal = 1;
  1217. printk("\r\n ...............MUSB_INTR_VBUSERROR................. \r\n");
  1218. }
  1219. if((int_usb&MUSB_INTR_BABBLE)&&(musb->xceiv->otg->state == OTG_STATE_A_HOST))
  1220. {
  1221. g_usb_abnormal = 1;
  1222. printk("\r\n ...............MUSB_INTR_BABBLE................. \r\n");
  1223. }
  1224. if((devctl == 0x98) && (musb->xceiv->otg->state != OTG_STATE_B_PERIPHERAL))
  1225. {
  1226. g_usb_abnormal = 1;
  1227. printk("\r\n ...............DEVCTL_ERROR................. \r\n");
  1228. }
  1229. //schedule_delayed_work(&musb->irq_work, 0);
  1230. schedule_work(&musb->irq_work.work);
  1231. return handled;
  1232. }
  1233. /*-------------------------------------------------------------------------*/
  1234. static void musb_disable_interrupts(struct musb *musb)
  1235. {
  1236. void __iomem *mbase = musb->mregs;
  1237. u16 temp;
  1238. /* disable interrupts */
  1239. musb_writeb(mbase, MUSB_INTRUSBE, 0);
  1240. musb->intrtxe = 0;
  1241. musb_writew(mbase, MUSB_INTRTXE, 0);
  1242. musb->intrrxe = 0;
  1243. musb_writew(mbase, MUSB_INTRRXE, 0);
  1244. /* flush pending interrupts */
  1245. temp = musb_readb(mbase, MUSB_INTRUSB);
  1246. temp = musb_readw(mbase, MUSB_INTRTX);
  1247. temp = musb_readw(mbase, MUSB_INTRRX);
  1248. }
  1249. static void musb_enable_interrupts(struct musb *musb)
  1250. {
  1251. void __iomem *regs = musb->mregs;
  1252. /* Set INT enable registers, enable interrupts */
  1253. musb->intrtxe = musb->epmask;
  1254. musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
  1255. musb->intrrxe = musb->epmask & 0xfffe;
  1256. musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
  1257. musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
  1258. }
  1259. /*
  1260. * Program the HDRC to start (enable interrupts, dma, etc.).
  1261. */
  1262. void musb_start(struct musb *musb)
  1263. {
  1264. void __iomem *regs = musb->mregs;
  1265. u8 devctl = musb_readb(regs, MUSB_DEVCTL);
  1266. u8 power;
  1267. musb_dbg(musb, "<== devctl %02x", devctl);
  1268. musb_enable_interrupts(musb);
  1269. musb_writeb(regs, MUSB_TESTMODE, 0);
  1270. power = MUSB_POWER_ISOUPDATE;
  1271. /*
  1272. * treating UNKNOWN as unspecified maximum speed, in which case
  1273. * we will default to high-speed.
  1274. */
  1275. if (musb->config->maximum_speed == USB_SPEED_HIGH ||
  1276. musb->config->maximum_speed == USB_SPEED_UNKNOWN)
  1277. power |= MUSB_POWER_HSENAB;
  1278. musb_writeb(regs, MUSB_POWER, power);
  1279. musb->is_active = 0;
  1280. devctl = musb_readb(regs, MUSB_DEVCTL);
  1281. devctl &= ~MUSB_DEVCTL_SESSION;
  1282. /* session started after:
  1283. * (a) ID-grounded irq, host mode;
  1284. * (b) vbus present/connect IRQ, peripheral mode;
  1285. * (c) peripheral initiates, using SRP
  1286. */
  1287. if (musb->port_mode != MUSB_HOST &&
  1288. musb->xceiv->otg->state != OTG_STATE_A_WAIT_BCON &&
  1289. (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
  1290. musb->is_active = 1;
  1291. } else {
  1292. devctl |= MUSB_DEVCTL_SESSION;
  1293. }
  1294. musb_platform_enable(musb);
  1295. musb_writeb(regs, MUSB_DEVCTL, devctl);
  1296. }
  1297. /*
  1298. * Make the HDRC stop (disable interrupts, etc.);
  1299. * reversible by musb_start
  1300. * called on gadget driver unregister
  1301. * with controller locked, irqs blocked
  1302. * acts as a NOP unless some role activated the hardware
  1303. */
  1304. void musb_stop(struct musb *musb)
  1305. {
  1306. /* stop IRQs, timers, ... */
  1307. musb_platform_disable(musb);
  1308. musb_disable_interrupts(musb);
  1309. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  1310. /* FIXME
  1311. * - mark host and/or peripheral drivers unusable/inactive
  1312. * - disable DMA (and enable it in HdrcStart)
  1313. * - make sure we can musb_start() after musb_stop(); with
  1314. * OTG mode, gadget driver module rmmod/modprobe cycles that
  1315. * - ...
  1316. */
  1317. musb_platform_try_idle(musb, 0);
  1318. }
  1319. /*-------------------------------------------------------------------------*/
  1320. /*
  1321. * The silicon either has hard-wired endpoint configurations, or else
  1322. * "dynamic fifo" sizing. The driver has support for both, though at this
  1323. * writing only the dynamic sizing is very well tested. Since we switched
  1324. * away from compile-time hardware parameters, we can no longer rely on
  1325. * dead code elimination to leave only the relevant one in the object file.
  1326. *
  1327. * We don't currently use dynamic fifo setup capability to do anything
  1328. * more than selecting one of a bunch of predefined configurations.
  1329. */
  1330. static ushort fifo_mode;
  1331. /* "modprobe ... fifo_mode=1" etc */
  1332. module_param(fifo_mode, ushort, 0);
  1333. MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
  1334. /*
  1335. * tables defining fifo_mode values. define more if you like.
  1336. * for host side, make sure both halves of ep1 are set up.
  1337. */
  1338. /* mode 0 - fits in 2KB */
  1339. static struct musb_fifo_cfg mode_0_cfg[] = {
  1340. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1341. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1342. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
  1343. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1344. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1345. };
  1346. /* mode 1 - fits in 4KB */
  1347. static struct musb_fifo_cfg mode_1_cfg[] = {
  1348. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1349. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1350. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1351. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1352. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1353. };
  1354. /* mode 2 - fits in 4KB */
  1355. static struct musb_fifo_cfg mode_2_cfg[] = {
  1356. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1357. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1358. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1359. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1360. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 960, },
  1361. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 1024, },
  1362. };
  1363. /* mode 3 - fits in 4KB */
  1364. static struct musb_fifo_cfg mode_3_cfg[] = {
  1365. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1366. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1367. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1368. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1369. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1370. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1371. };
  1372. /* mode 4 - fits in 16KB */
  1373. static struct musb_fifo_cfg mode_4_cfg[] = {
  1374. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1375. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1376. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1377. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1378. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1379. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1380. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1381. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1382. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1383. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1384. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
  1385. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
  1386. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
  1387. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
  1388. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
  1389. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
  1390. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
  1391. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
  1392. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
  1393. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
  1394. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
  1395. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
  1396. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
  1397. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
  1398. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
  1399. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1400. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1401. };
  1402. /* mode 5 - fits in 8KB */
  1403. static struct musb_fifo_cfg mode_5_cfg[] = {
  1404. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1405. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1406. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1407. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1408. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1409. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1410. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1411. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1412. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1413. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1414. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
  1415. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
  1416. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
  1417. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
  1418. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
  1419. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
  1420. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
  1421. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
  1422. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
  1423. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
  1424. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
  1425. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
  1426. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
  1427. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
  1428. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
  1429. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1430. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1431. };
  1432. /*
  1433. * configure a fifo; for non-shared endpoints, this may be called
  1434. * once for a tx fifo and once for an rx fifo.
  1435. *
  1436. * returns negative errno or offset for next fifo.
  1437. */
  1438. static int
  1439. fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
  1440. const struct musb_fifo_cfg *cfg, u16 offset)
  1441. {
  1442. void __iomem *mbase = musb->mregs;
  1443. int size = 0;
  1444. u16 maxpacket = cfg->maxpacket;
  1445. u16 c_off = offset >> 3;
  1446. u8 c_size;
  1447. /* expect hw_ep has already been zero-initialized */
  1448. size = ffs(max(maxpacket, (u16) 8)) - 1;
  1449. maxpacket = 1 << size;
  1450. c_size = size - 3;
  1451. if (cfg->mode == BUF_DOUBLE) {
  1452. if ((offset + (maxpacket << 1)) >
  1453. (1 << (musb->config->ram_bits + 2)))
  1454. return -EMSGSIZE;
  1455. c_size |= MUSB_FIFOSZ_DPB;
  1456. } else {
  1457. if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
  1458. return -EMSGSIZE;
  1459. }
  1460. /* configure the FIFO */
  1461. musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
  1462. /* EP0 reserved endpoint for control, bidirectional;
  1463. * EP1 reserved for bulk, two unidirectional halves.
  1464. */
  1465. if (hw_ep->epnum == 1)
  1466. musb->bulk_ep = hw_ep;
  1467. /* REVISIT error check: be sure ep0 can both rx and tx ... */
  1468. switch (cfg->style) {
  1469. case FIFO_TX:
  1470. musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
  1471. musb_writew(mbase, MUSB_TXFIFOADD, c_off);
  1472. hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1473. hw_ep->max_packet_sz_tx = maxpacket;
  1474. break;
  1475. case FIFO_RX:
  1476. musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
  1477. musb_writew(mbase, MUSB_RXFIFOADD, c_off);
  1478. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1479. hw_ep->max_packet_sz_rx = maxpacket;
  1480. break;
  1481. case FIFO_RXTX:
  1482. musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
  1483. musb_writew(mbase, MUSB_TXFIFOADD, c_off);
  1484. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1485. hw_ep->max_packet_sz_rx = maxpacket;
  1486. musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
  1487. musb_writew(mbase, MUSB_RXFIFOADD, c_off);
  1488. hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
  1489. hw_ep->max_packet_sz_tx = maxpacket;
  1490. hw_ep->is_shared_fifo = true;
  1491. break;
  1492. }
  1493. /* NOTE rx and tx endpoint irqs aren't managed separately,
  1494. * which happens to be ok
  1495. */
  1496. musb->epmask |= (1 << hw_ep->epnum);
  1497. return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
  1498. }
  1499. static struct musb_fifo_cfg ep0_cfg = {
  1500. .style = FIFO_RXTX, .maxpacket = 64,
  1501. };
  1502. static int ep_config_from_table(struct musb *musb)
  1503. {
  1504. const struct musb_fifo_cfg *cfg;
  1505. unsigned i, n;
  1506. int offset;
  1507. struct musb_hw_ep *hw_ep = musb->endpoints;
  1508. if (musb->config->fifo_cfg) {
  1509. cfg = musb->config->fifo_cfg;
  1510. n = musb->config->fifo_cfg_size;
  1511. goto done;
  1512. }
  1513. switch (fifo_mode) {
  1514. default:
  1515. fifo_mode = 0;
  1516. /* FALLTHROUGH */
  1517. case 0:
  1518. cfg = mode_0_cfg;
  1519. n = ARRAY_SIZE(mode_0_cfg);
  1520. break;
  1521. case 1:
  1522. cfg = mode_1_cfg;
  1523. n = ARRAY_SIZE(mode_1_cfg);
  1524. break;
  1525. case 2:
  1526. cfg = mode_2_cfg;
  1527. n = ARRAY_SIZE(mode_2_cfg);
  1528. break;
  1529. case 3:
  1530. cfg = mode_3_cfg;
  1531. n = ARRAY_SIZE(mode_3_cfg);
  1532. break;
  1533. case 4:
  1534. cfg = mode_4_cfg;
  1535. n = ARRAY_SIZE(mode_4_cfg);
  1536. break;
  1537. case 5:
  1538. cfg = mode_5_cfg;
  1539. n = ARRAY_SIZE(mode_5_cfg);
  1540. break;
  1541. }
  1542. pr_debug("%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
  1543. done:
  1544. offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
  1545. /* assert(offset > 0) */
  1546. /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
  1547. * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
  1548. */
  1549. for (i = 0; i < n; i++) {
  1550. u8 epn = cfg->hw_ep_num;
  1551. if (epn >= musb->config->num_eps) {
  1552. pr_debug("%s: invalid ep %d\n",
  1553. musb_driver_name, epn);
  1554. return -EINVAL;
  1555. }
  1556. offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
  1557. if (offset < 0) {
  1558. pr_debug("%s: mem overrun, ep %d\n",
  1559. musb_driver_name, epn);
  1560. return offset;
  1561. }
  1562. epn++;
  1563. musb->nr_endpoints = max(epn, musb->nr_endpoints);
  1564. }
  1565. pr_debug("%s: %d/%d max ep, %d/%d memory\n",
  1566. musb_driver_name,
  1567. n + 1, musb->config->num_eps * 2 - 1,
  1568. offset, (1 << (musb->config->ram_bits + 2)));
  1569. if (!musb->bulk_ep) {
  1570. pr_debug("%s: missing bulk\n", musb_driver_name);
  1571. return -EINVAL;
  1572. }
  1573. return 0;
  1574. }
  1575. /*
  1576. * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
  1577. * @param musb the controller
  1578. */
  1579. static int ep_config_from_hw(struct musb *musb)
  1580. {
  1581. u8 epnum = 0;
  1582. struct musb_hw_ep *hw_ep;
  1583. void __iomem *mbase = musb->mregs;
  1584. int ret = 0;
  1585. musb_dbg(musb, "<== static silicon ep config");
  1586. /* FIXME pick up ep0 maxpacket size */
  1587. for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
  1588. musb_ep_select(mbase, epnum);
  1589. hw_ep = musb->endpoints + epnum;
  1590. ret = musb_read_fifosize(musb, hw_ep, epnum);
  1591. if (ret < 0)
  1592. break;
  1593. /* FIXME set up hw_ep->{rx,tx}_double_buffered */
  1594. /* pick an RX/TX endpoint for bulk */
  1595. if (hw_ep->max_packet_sz_tx < 512
  1596. || hw_ep->max_packet_sz_rx < 512)
  1597. continue;
  1598. /* REVISIT: this algorithm is lazy, we should at least
  1599. * try to pick a double buffered endpoint.
  1600. */
  1601. if (musb->bulk_ep)
  1602. continue;
  1603. musb->bulk_ep = hw_ep;
  1604. }
  1605. if (!musb->bulk_ep) {
  1606. pr_debug("%s: missing bulk\n", musb_driver_name);
  1607. return -EINVAL;
  1608. }
  1609. return 0;
  1610. }
  1611. enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
  1612. /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
  1613. * configure endpoints, or take their config from silicon
  1614. */
  1615. static int musb_core_init(u16 musb_type, struct musb *musb)
  1616. {
  1617. u8 reg;
  1618. char *type;
  1619. char aInfo[90];
  1620. void __iomem *mbase = musb->mregs;
  1621. int status = 0;
  1622. int i;
  1623. /* log core options (read using indexed model) */
  1624. reg = musb_read_configdata(mbase);
  1625. strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
  1626. if (reg & MUSB_CONFIGDATA_DYNFIFO) {
  1627. strcat(aInfo, ", dyn FIFOs");
  1628. musb->dyn_fifo = true;
  1629. }
  1630. if (reg & MUSB_CONFIGDATA_MPRXE) {
  1631. strcat(aInfo, ", bulk combine");
  1632. musb->bulk_combine = true;
  1633. }
  1634. if (reg & MUSB_CONFIGDATA_MPTXE) {
  1635. strcat(aInfo, ", bulk split");
  1636. musb->bulk_split = true;
  1637. }
  1638. if (reg & MUSB_CONFIGDATA_HBRXE) {
  1639. strcat(aInfo, ", HB-ISO Rx");
  1640. musb->hb_iso_rx = true;
  1641. }
  1642. if (reg & MUSB_CONFIGDATA_HBTXE) {
  1643. strcat(aInfo, ", HB-ISO Tx");
  1644. musb->hb_iso_tx = true;
  1645. }
  1646. if (reg & MUSB_CONFIGDATA_SOFTCONE)
  1647. strcat(aInfo, ", SoftConn");
  1648. pr_debug("%s: ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
  1649. if (MUSB_CONTROLLER_MHDRC == musb_type) {
  1650. musb->is_multipoint = 1;
  1651. type = "M";
  1652. } else {
  1653. musb->is_multipoint = 0;
  1654. type = "";
  1655. #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
  1656. pr_err("%s: kernel must blacklist external hubs\n",
  1657. musb_driver_name);
  1658. #endif
  1659. }
  1660. /* log release info */
  1661. musb->hwvers = musb_readw(mbase, MUSB_HWVERS);
  1662. pr_debug("%s: %sHDRC RTL version %d.%d%s\n",
  1663. musb_driver_name, type, MUSB_HWVERS_MAJOR(musb->hwvers),
  1664. MUSB_HWVERS_MINOR(musb->hwvers),
  1665. (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
  1666. /* configure ep0 */
  1667. musb_configure_ep0(musb);
  1668. /* discover endpoint configuration */
  1669. musb->nr_endpoints = 1;
  1670. musb->epmask = 1;
  1671. if (musb->dyn_fifo)
  1672. status = ep_config_from_table(musb);
  1673. else
  1674. status = ep_config_from_hw(musb);
  1675. if (status < 0)
  1676. return status;
  1677. /* finish init, and print endpoint config */
  1678. for (i = 0; i < musb->nr_endpoints; i++) {
  1679. struct musb_hw_ep *hw_ep = musb->endpoints + i;
  1680. hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
  1681. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  1682. if (musb->ops->quirks & MUSB_IN_TUSB) {
  1683. hw_ep->fifo_async = musb->async + 0x400 +
  1684. musb->io.fifo_offset(i);
  1685. hw_ep->fifo_sync = musb->sync + 0x400 +
  1686. musb->io.fifo_offset(i);
  1687. hw_ep->fifo_sync_va =
  1688. musb->sync_va + 0x400 + musb->io.fifo_offset(i);
  1689. if (i == 0)
  1690. hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
  1691. else
  1692. hw_ep->conf = mbase + 0x400 +
  1693. (((i - 1) & 0xf) << 2);
  1694. }
  1695. #endif
  1696. hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
  1697. hw_ep->rx_reinit = 1;
  1698. hw_ep->tx_reinit = 1;
  1699. if (hw_ep->max_packet_sz_tx) {
  1700. musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
  1701. musb_driver_name, i,
  1702. hw_ep->is_shared_fifo ? "shared" : "tx",
  1703. hw_ep->tx_double_buffered
  1704. ? "doublebuffer, " : "",
  1705. hw_ep->max_packet_sz_tx);
  1706. }
  1707. if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
  1708. musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
  1709. musb_driver_name, i,
  1710. "rx",
  1711. hw_ep->rx_double_buffered
  1712. ? "doublebuffer, " : "",
  1713. hw_ep->max_packet_sz_rx);
  1714. }
  1715. if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
  1716. musb_dbg(musb, "hw_ep %d not configured", i);
  1717. }
  1718. return 0;
  1719. }
  1720. /*-------------------------------------------------------------------------*/
  1721. /*
  1722. * handle all the irqs defined by the HDRC core. for now we expect: other
  1723. * irq sources (phy, dma, etc) will be handled first, musb->int_* values
  1724. * will be assigned, and the irq will already have been acked.
  1725. *
  1726. * called in irq context with spinlock held, irqs blocked
  1727. */
  1728. irqreturn_t musb_interrupt(struct musb *musb)
  1729. {
  1730. irqreturn_t retval = IRQ_NONE;
  1731. unsigned long status;
  1732. unsigned long epnum;
  1733. u8 devctl;
  1734. if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
  1735. return IRQ_NONE;
  1736. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1737. trace_musb_isr(musb);
  1738. /**
  1739. * According to Mentor Graphics' documentation, flowchart on page 98,
  1740. * IRQ should be handled as follows:
  1741. *
  1742. * . Resume IRQ
  1743. * . Session Request IRQ
  1744. * . VBUS Error IRQ
  1745. * . Suspend IRQ
  1746. * . Connect IRQ
  1747. * . Disconnect IRQ
  1748. * . Reset/Babble IRQ
  1749. * . SOF IRQ (we're not using this one)
  1750. * . Endpoint 0 IRQ
  1751. * . TX Endpoints
  1752. * . RX Endpoints
  1753. *
  1754. * We will be following that flowchart in order to avoid any problems
  1755. * that might arise with internal Finite State Machine.
  1756. */
  1757. if (musb->int_usb)
  1758. retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
  1759. if (musb->int_tx & 1) {
  1760. if (is_host_active(musb))
  1761. retval |= musb_h_ep0_irq(musb);
  1762. else
  1763. retval |= musb_g_ep0_irq(musb);
  1764. /* we have just handled endpoint 0 IRQ, clear it */
  1765. musb->int_tx &= ~BIT(0);
  1766. }
  1767. status = musb->int_tx;
  1768. for_each_set_bit(epnum, &status, 16) {
  1769. retval = IRQ_HANDLED;
  1770. if (is_host_active(musb))
  1771. musb_host_tx(musb, epnum);
  1772. else
  1773. musb_g_tx(musb, epnum);
  1774. }
  1775. status = musb->int_rx;
  1776. for_each_set_bit(epnum, &status, 16) {
  1777. retval = IRQ_HANDLED;
  1778. if (is_host_active(musb))
  1779. musb_host_rx(musb, epnum);
  1780. else
  1781. musb_g_rx(musb, epnum);
  1782. }
  1783. return retval;
  1784. }
  1785. EXPORT_SYMBOL_GPL(musb_interrupt);
  1786. #ifndef CONFIG_MUSB_PIO_ONLY
  1787. static bool use_dma = 1;
  1788. /* "modprobe ... use_dma=0" etc */
  1789. module_param(use_dma, bool, 0644);
  1790. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  1791. void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
  1792. {
  1793. /* called with controller lock already held */
  1794. if (!epnum) {
  1795. if (!is_cppi_enabled(musb)) {
  1796. /* endpoint 0 */
  1797. if (is_host_active(musb))
  1798. musb_h_ep0_irq(musb);
  1799. else
  1800. musb_g_ep0_irq(musb);
  1801. }
  1802. } else {
  1803. /* endpoints 1..15 */
  1804. if (transmit) {
  1805. if (is_host_active(musb))
  1806. musb_host_tx(musb, epnum);
  1807. else
  1808. musb_g_tx(musb, epnum);
  1809. } else {
  1810. /* receive */
  1811. if (is_host_active(musb))
  1812. musb_host_rx(musb, epnum);
  1813. else
  1814. musb_g_rx(musb, epnum);
  1815. }
  1816. }
  1817. }
  1818. EXPORT_SYMBOL_GPL(musb_dma_completion);
  1819. #else
  1820. #define use_dma 0
  1821. #endif
  1822. static int (*musb_phy_callback)(enum musb_vbus_id_status status);
  1823. /*
  1824. * musb_mailbox - optional phy notifier function
  1825. * @status phy state change
  1826. *
  1827. * Optionally gets called from the USB PHY. Note that the USB PHY must be
  1828. * disabled at the point the phy_callback is registered or unregistered.
  1829. */
  1830. int musb_mailbox(enum musb_vbus_id_status status)
  1831. {
  1832. if (musb_phy_callback)
  1833. return musb_phy_callback(status);
  1834. return -ENODEV;
  1835. };
  1836. EXPORT_SYMBOL_GPL(musb_mailbox);
  1837. /*-------------------------------------------------------------------------*/
  1838. static ssize_t
  1839. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1840. {
  1841. struct musb *musb = dev_to_musb(dev);
  1842. unsigned long flags;
  1843. int ret = -EINVAL;
  1844. spin_lock_irqsave(&musb->lock, flags);
  1845. ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->otg->state));
  1846. spin_unlock_irqrestore(&musb->lock, flags);
  1847. return ret;
  1848. }
  1849. static ssize_t
  1850. mode_store(struct device *dev, struct device_attribute *attr,
  1851. const char *buf, size_t n)
  1852. {
  1853. struct musb *musb = dev_to_musb(dev);
  1854. unsigned long flags;
  1855. int status;
  1856. spin_lock_irqsave(&musb->lock, flags);
  1857. if (sysfs_streq(buf, "host"))
  1858. status = musb_platform_set_mode(musb, MUSB_HOST);
  1859. else if (sysfs_streq(buf, "peripheral"))
  1860. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1861. else if (sysfs_streq(buf, "otg"))
  1862. status = musb_platform_set_mode(musb, MUSB_OTG);
  1863. else
  1864. status = -EINVAL;
  1865. spin_unlock_irqrestore(&musb->lock, flags);
  1866. return (status == 0) ? n : status;
  1867. }
  1868. static DEVICE_ATTR_RW(mode);
  1869. static ssize_t
  1870. vbus_store(struct device *dev, struct device_attribute *attr,
  1871. const char *buf, size_t n)
  1872. {
  1873. struct musb *musb = dev_to_musb(dev);
  1874. unsigned long flags;
  1875. unsigned long val;
  1876. if (sscanf(buf, "%lu", &val) < 1) {
  1877. dev_err(dev, "Invalid VBUS timeout ms value\n");
  1878. return -EINVAL;
  1879. }
  1880. spin_lock_irqsave(&musb->lock, flags);
  1881. /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
  1882. musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
  1883. if (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)
  1884. musb->is_active = 0;
  1885. musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
  1886. spin_unlock_irqrestore(&musb->lock, flags);
  1887. return n;
  1888. }
  1889. static ssize_t
  1890. vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
  1891. {
  1892. struct musb *musb = dev_to_musb(dev);
  1893. unsigned long flags;
  1894. unsigned long val;
  1895. int vbus;
  1896. u8 devctl;
  1897. pm_runtime_get_sync(dev);
  1898. spin_lock_irqsave(&musb->lock, flags);
  1899. val = musb->a_wait_bcon;
  1900. vbus = musb_platform_get_vbus_status(musb);
  1901. if (vbus < 0) {
  1902. /* Use default MUSB method by means of DEVCTL register */
  1903. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1904. if ((devctl & MUSB_DEVCTL_VBUS)
  1905. == (3 << MUSB_DEVCTL_VBUS_SHIFT))
  1906. vbus = 1;
  1907. else
  1908. vbus = 0;
  1909. }
  1910. spin_unlock_irqrestore(&musb->lock, flags);
  1911. pm_runtime_put_sync(dev);
  1912. return sprintf(buf, "Vbus %s, timeout %lu msec\n",
  1913. vbus ? "on" : "off", val);
  1914. }
  1915. static DEVICE_ATTR_RW(vbus);
  1916. /* Gadget drivers can't know that a host is connected so they might want
  1917. * to start SRP, but users can. This allows userspace to trigger SRP.
  1918. */
  1919. static ssize_t srp_store(struct device *dev, struct device_attribute *attr,
  1920. const char *buf, size_t n)
  1921. {
  1922. struct musb *musb = dev_to_musb(dev);
  1923. unsigned short srp;
  1924. if (sscanf(buf, "%hu", &srp) != 1
  1925. || (srp != 1)) {
  1926. dev_err(dev, "SRP: Value must be 1\n");
  1927. return -EINVAL;
  1928. }
  1929. if (srp == 1)
  1930. musb_g_wakeup(musb);
  1931. return n;
  1932. }
  1933. static DEVICE_ATTR_WO(srp);
  1934. static struct attribute *musb_attributes[] = {
  1935. &dev_attr_mode.attr,
  1936. &dev_attr_vbus.attr,
  1937. &dev_attr_srp.attr,
  1938. NULL
  1939. };
  1940. static const struct attribute_group musb_attr_group = {
  1941. .attrs = musb_attributes,
  1942. };
  1943. #define MUSB_QUIRK_B_INVALID_VBUS_91 (MUSB_DEVCTL_BDEVICE | \
  1944. (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
  1945. MUSB_DEVCTL_SESSION)
  1946. #define MUSB_QUIRK_B_DISCONNECT_99 (MUSB_DEVCTL_BDEVICE | \
  1947. (3 << MUSB_DEVCTL_VBUS_SHIFT) | \
  1948. MUSB_DEVCTL_SESSION)
  1949. #define MUSB_QUIRK_A_DISCONNECT_19 ((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
  1950. MUSB_DEVCTL_SESSION)
  1951. /*
  1952. * Check the musb devctl session bit to determine if we want to
  1953. * allow PM runtime for the device. In general, we want to keep things
  1954. * active when the session bit is set except after host disconnect.
  1955. *
  1956. * Only called from musb_irq_work. If this ever needs to get called
  1957. * elsewhere, proper locking must be implemented for musb->session.
  1958. */
  1959. static void musb_pm_runtime_check_session(struct musb *musb)
  1960. {
  1961. u8 devctl, s;
  1962. int error;
  1963. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1964. /* Handle session status quirks first */
  1965. s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
  1966. MUSB_DEVCTL_HR;
  1967. switch (devctl & ~s) {
  1968. case MUSB_QUIRK_B_DISCONNECT_99:
  1969. if (musb->quirk_retries && !musb->flush_irq_work) {
  1970. musb_dbg(musb, "Poll devctl in case of suspend after disconnect\n");
  1971. schedule_delayed_work(&musb->irq_work,
  1972. msecs_to_jiffies(1000));
  1973. musb->quirk_retries--;
  1974. break;
  1975. }
  1976. /* fall through */
  1977. case MUSB_QUIRK_B_INVALID_VBUS_91:
  1978. if (musb->quirk_retries && !musb->flush_irq_work) {
  1979. musb_dbg(musb,
  1980. "Poll devctl on invalid vbus, assume no session");
  1981. //schedule_delayed_work(&musb->irq_work,
  1982. // msecs_to_jiffies(1000));
  1983. schedule_work(&musb->irq_work.work);
  1984. musb->quirk_retries--;
  1985. return;
  1986. }
  1987. /* fall through */
  1988. case MUSB_QUIRK_A_DISCONNECT_19:
  1989. if (musb->quirk_retries && !musb->flush_irq_work) {
  1990. musb_dbg(musb,
  1991. "Poll devctl on possible host mode disconnect");
  1992. //schedule_delayed_work(&musb->irq_work,
  1993. // msecs_to_jiffies(1000));
  1994. schedule_work(&musb->irq_work.work);
  1995. musb->quirk_retries--;
  1996. return;
  1997. }
  1998. if (!musb->session)
  1999. break;
  2000. musb_dbg(musb, "Allow PM on possible host mode disconnect");
  2001. pm_runtime_mark_last_busy(musb->controller);
  2002. pm_runtime_put_autosuspend(musb->controller);
  2003. musb->session = false;
  2004. return;
  2005. default:
  2006. break;
  2007. }
  2008. /* No need to do anything if session has not changed */
  2009. s = devctl & MUSB_DEVCTL_SESSION;
  2010. if (s == musb->session)
  2011. return;
  2012. /* Block PM or allow PM? */
  2013. if (s) {
  2014. musb_dbg(musb, "Block PM on active session: %02x", devctl);
  2015. error = pm_runtime_get_sync(musb->controller);
  2016. if (error < 0)
  2017. dev_err(musb->controller, "Could not enable: %i\n",
  2018. error);
  2019. musb->quirk_retries = 3;
  2020. } else {
  2021. musb_dbg(musb, "Allow PM with no session: %02x", devctl);
  2022. pm_runtime_mark_last_busy(musb->controller);
  2023. pm_runtime_put_autosuspend(musb->controller);
  2024. }
  2025. musb->session = s;
  2026. }
  2027. /* Only used to provide driver mode change events */
  2028. static void musb_irq_work(struct work_struct *data)
  2029. {
  2030. struct musb *musb = container_of(data, struct musb, irq_work.work);
  2031. int error;
  2032. if(g_usb_abnormal) {
  2033. g_usb_abnormal = 0;
  2034. musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
  2035. usb_hcd_resume_root_hub(musb_to_hcd(musb));
  2036. musb_root_disconnect(musb);
  2037. if (musb->a_wait_bcon != 0 && (musb->port_mode == MUSB_OTG)/*is_otg_enabled(musb)*/)
  2038. musb_platform_try_idle(musb, jiffies
  2039. + msecs_to_jiffies(musb->a_wait_bcon));
  2040. if (musb->ops->set_mode) {
  2041. musb->ops->set_mode(musb, MUSB_PERIPHERAL);
  2042. msleep(1000);
  2043. musb->ops->set_mode(musb, MUSB_OTG);
  2044. }
  2045. }
  2046. error = pm_runtime_get_sync(musb->controller);
  2047. if (error < 0) {
  2048. dev_err(musb->controller, "Could not enable: %i\n", error);
  2049. return;
  2050. }
  2051. musb_pm_runtime_check_session(musb);
  2052. if (musb->xceiv->otg->state != musb->xceiv_old_state) {
  2053. musb->xceiv_old_state = musb->xceiv->otg->state;
  2054. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  2055. }
  2056. pm_runtime_mark_last_busy(musb->controller);
  2057. pm_runtime_put_autosuspend(musb->controller);
  2058. }
  2059. static void musb_recover_from_babble(struct musb *musb)
  2060. {
  2061. int ret;
  2062. u8 devctl;
  2063. musb_disable_interrupts(musb);
  2064. /*
  2065. * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
  2066. * it some slack and wait for 10us.
  2067. */
  2068. udelay(10);
  2069. ret = musb_platform_recover(musb);
  2070. if (ret) {
  2071. musb_enable_interrupts(musb);
  2072. return;
  2073. }
  2074. /* drop session bit */
  2075. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2076. devctl &= ~MUSB_DEVCTL_SESSION;
  2077. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  2078. /* tell usbcore about it */
  2079. musb_root_disconnect(musb);
  2080. /*
  2081. * When a babble condition occurs, the musb controller
  2082. * removes the session bit and the endpoint config is lost.
  2083. */
  2084. if (musb->dyn_fifo)
  2085. ret = ep_config_from_table(musb);
  2086. else
  2087. ret = ep_config_from_hw(musb);
  2088. /* restart session */
  2089. if (ret == 0)
  2090. musb_start(musb);
  2091. }
  2092. /* --------------------------------------------------------------------------
  2093. * Init support
  2094. */
  2095. static struct musb *allocate_instance(struct device *dev,
  2096. const struct musb_hdrc_config *config, void __iomem *mbase)
  2097. {
  2098. struct musb *musb;
  2099. struct musb_hw_ep *ep;
  2100. int epnum;
  2101. int ret;
  2102. musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
  2103. if (!musb)
  2104. return NULL;
  2105. INIT_LIST_HEAD(&musb->control);
  2106. INIT_LIST_HEAD(&musb->in_bulk);
  2107. INIT_LIST_HEAD(&musb->out_bulk);
  2108. INIT_LIST_HEAD(&musb->pending_list);
  2109. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  2110. musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
  2111. musb->mregs = mbase;
  2112. musb->ctrl_base = mbase;
  2113. musb->nIrq = -ENODEV;
  2114. musb->config = config;
  2115. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  2116. for (epnum = 0, ep = musb->endpoints;
  2117. epnum < musb->config->num_eps;
  2118. epnum++, ep++) {
  2119. ep->musb = musb;
  2120. ep->epnum = epnum;
  2121. }
  2122. musb->controller = dev;
  2123. ret = musb_host_alloc(musb);
  2124. if (ret < 0)
  2125. goto err_free;
  2126. dev_set_drvdata(dev, musb);
  2127. return musb;
  2128. err_free:
  2129. return NULL;
  2130. }
  2131. static void musb_free(struct musb *musb)
  2132. {
  2133. /* this has multiple entry modes. it handles fault cleanup after
  2134. * probe(), where things may be partially set up, as well as rmmod
  2135. * cleanup after everything's been de-activated.
  2136. */
  2137. #ifdef CONFIG_SYSFS
  2138. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  2139. #endif
  2140. if (musb->nIrq >= 0) {
  2141. if (musb->irq_wake)
  2142. disable_irq_wake(musb->nIrq);
  2143. free_irq(musb->nIrq, musb);
  2144. }
  2145. musb_host_free(musb);
  2146. }
  2147. struct musb_pending_work {
  2148. int (*callback)(struct musb *musb, void *data);
  2149. void *data;
  2150. struct list_head node;
  2151. };
  2152. #ifdef CONFIG_PM
  2153. /*
  2154. * Called from musb_runtime_resume(), musb_resume(), and
  2155. * musb_queue_resume_work(). Callers must take musb->lock.
  2156. */
  2157. static int musb_run_resume_work(struct musb *musb)
  2158. {
  2159. struct musb_pending_work *w, *_w;
  2160. unsigned long flags;
  2161. int error = 0;
  2162. spin_lock_irqsave(&musb->list_lock, flags);
  2163. list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
  2164. if (w->callback) {
  2165. error = w->callback(musb, w->data);
  2166. if (error < 0) {
  2167. dev_err(musb->controller,
  2168. "resume callback %p failed: %i\n",
  2169. w->callback, error);
  2170. }
  2171. }
  2172. list_del(&w->node);
  2173. devm_kfree(musb->controller, w);
  2174. }
  2175. spin_unlock_irqrestore(&musb->list_lock, flags);
  2176. return error;
  2177. }
  2178. #endif
  2179. /*
  2180. * Called to run work if device is active or else queue the work to happen
  2181. * on resume. Caller must take musb->lock and must hold an RPM reference.
  2182. *
  2183. * Note that we cowardly refuse queuing work after musb PM runtime
  2184. * resume is done calling musb_run_resume_work() and return -EINPROGRESS
  2185. * instead.
  2186. */
  2187. int musb_queue_resume_work(struct musb *musb,
  2188. int (*callback)(struct musb *musb, void *data),
  2189. void *data)
  2190. {
  2191. struct musb_pending_work *w;
  2192. unsigned long flags;
  2193. bool is_suspended;
  2194. int error;
  2195. if (WARN_ON(!callback))
  2196. return -EINVAL;
  2197. spin_lock_irqsave(&musb->list_lock, flags);
  2198. is_suspended = musb->is_runtime_suspended;
  2199. if (is_suspended) {
  2200. w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
  2201. if (!w) {
  2202. error = -ENOMEM;
  2203. goto out_unlock;
  2204. }
  2205. w->callback = callback;
  2206. w->data = data;
  2207. list_add_tail(&w->node, &musb->pending_list);
  2208. error = 0;
  2209. }
  2210. out_unlock:
  2211. spin_unlock_irqrestore(&musb->list_lock, flags);
  2212. if (!is_suspended)
  2213. error = callback(musb, data);
  2214. return error;
  2215. }
  2216. EXPORT_SYMBOL_GPL(musb_queue_resume_work);
  2217. static void musb_deassert_reset(struct work_struct *work)
  2218. {
  2219. struct musb *musb;
  2220. unsigned long flags;
  2221. musb = container_of(work, struct musb, deassert_reset_work.work);
  2222. spin_lock_irqsave(&musb->lock, flags);
  2223. if (musb->port1_status & USB_PORT_STAT_RESET)
  2224. musb_port_reset(musb, false);
  2225. spin_unlock_irqrestore(&musb->lock, flags);
  2226. }
  2227. static void musb_recovery_usb_proc(struct work_struct *work)
  2228. {
  2229. struct musb *musb = NULL;
  2230. if (NULL == work)
  2231. return;
  2232. musb = container_of(work, struct musb, recovery_usb_work);
  2233. if (NULL == musb)
  2234. return;
  2235. if (musb->ops->set_mode)
  2236. musb->ops->set_mode(musb, MUSB_PERIPHERAL);
  2237. msleep(1000);
  2238. if (musb->ops->set_mode)
  2239. musb->ops->set_mode(musb, MUSB_OTG);
  2240. }
  2241. static void musb_reset_timer_handler(struct timer_list *t)
  2242. {
  2243. struct musb *musb = NULL;
  2244. if (NULL == t)
  2245. return;
  2246. musb = container_of(t, struct musb, musb_reset_timer);
  2247. if (NULL == musb)
  2248. return;
  2249. schedule_work(&musb->recovery_usb_work);
  2250. }
  2251. /*
  2252. * Perform generic per-controller initialization.
  2253. *
  2254. * @dev: the controller (already clocked, etc)
  2255. * @nIrq: IRQ number
  2256. * @ctrl: virtual address of controller registers,
  2257. * not yet corrected for platform-specific offsets
  2258. */
  2259. static int
  2260. musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  2261. {
  2262. int status;
  2263. struct musb *musb;
  2264. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  2265. /* The driver might handle more features than the board; OK.
  2266. * Fail when the board needs a feature that's not enabled.
  2267. */
  2268. if (!plat) {
  2269. dev_err(dev, "no platform_data?\n");
  2270. status = -ENODEV;
  2271. goto fail0;
  2272. }
  2273. /* allocate */
  2274. musb = allocate_instance(dev, plat->config, ctrl);
  2275. if (!musb) {
  2276. status = -ENOMEM;
  2277. goto fail0;
  2278. }
  2279. spin_lock_init(&musb->lock);
  2280. spin_lock_init(&musb->list_lock);
  2281. musb->board_set_power = plat->set_power;
  2282. musb->min_power = plat->min_power;
  2283. musb->ops = plat->platform_ops;
  2284. musb->port_mode = plat->mode;
  2285. /*
  2286. * Initialize the default IO functions. At least omap2430 needs
  2287. * these early. We initialize the platform specific IO functions
  2288. * later on.
  2289. */
  2290. musb_readb = musb_default_readb;
  2291. musb_writeb = musb_default_writeb;
  2292. musb_readw = musb_default_readw;
  2293. musb_writew = musb_default_writew;
  2294. /* The musb_platform_init() call:
  2295. * - adjusts musb->mregs
  2296. * - sets the musb->isr
  2297. * - may initialize an integrated transceiver
  2298. * - initializes musb->xceiv, usually by otg_get_phy()
  2299. * - stops powering VBUS
  2300. *
  2301. * There are various transceiver configurations.
  2302. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  2303. * external/discrete ones in various flavors (twl4030 family,
  2304. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  2305. */
  2306. status = musb_platform_init(musb);
  2307. if (status < 0)
  2308. goto fail1;
  2309. if (!musb->isr) {
  2310. status = -ENODEV;
  2311. goto fail2;
  2312. }
  2313. /* Most devices use indexed offset or flat offset */
  2314. if (musb->ops->quirks & MUSB_INDEXED_EP) {
  2315. musb->io.ep_offset = musb_indexed_ep_offset;
  2316. musb->io.ep_select = musb_indexed_ep_select;
  2317. } else {
  2318. musb->io.ep_offset = musb_flat_ep_offset;
  2319. musb->io.ep_select = musb_flat_ep_select;
  2320. }
  2321. if (musb->ops->quirks & MUSB_G_NO_SKB_RESERVE)
  2322. musb->g.quirk_avoids_skb_reserve = 1;
  2323. /* At least tusb6010 has its own offsets */
  2324. if (musb->ops->ep_offset)
  2325. musb->io.ep_offset = musb->ops->ep_offset;
  2326. if (musb->ops->ep_select)
  2327. musb->io.ep_select = musb->ops->ep_select;
  2328. if (musb->ops->fifo_mode)
  2329. fifo_mode = musb->ops->fifo_mode;
  2330. else
  2331. fifo_mode = 4;
  2332. if (musb->ops->fifo_offset)
  2333. musb->io.fifo_offset = musb->ops->fifo_offset;
  2334. else
  2335. musb->io.fifo_offset = musb_default_fifo_offset;
  2336. if (musb->ops->busctl_offset)
  2337. musb->io.busctl_offset = musb->ops->busctl_offset;
  2338. else
  2339. musb->io.busctl_offset = musb_default_busctl_offset;
  2340. if (musb->ops->readb)
  2341. musb_readb = musb->ops->readb;
  2342. if (musb->ops->writeb)
  2343. musb_writeb = musb->ops->writeb;
  2344. if (musb->ops->readw)
  2345. musb_readw = musb->ops->readw;
  2346. if (musb->ops->writew)
  2347. musb_writew = musb->ops->writew;
  2348. #ifndef CONFIG_MUSB_PIO_ONLY
  2349. if (!musb->ops->dma_init || !musb->ops->dma_exit) {
  2350. dev_err(dev, "DMA controller not set\n");
  2351. status = -ENODEV;
  2352. goto fail2;
  2353. }
  2354. musb_dma_controller_create = musb->ops->dma_init;
  2355. musb_dma_controller_destroy = musb->ops->dma_exit;
  2356. #endif
  2357. if (musb->ops->read_fifo)
  2358. musb->io.read_fifo = musb->ops->read_fifo;
  2359. else
  2360. musb->io.read_fifo = musb_default_read_fifo;
  2361. if (musb->ops->write_fifo)
  2362. musb->io.write_fifo = musb->ops->write_fifo;
  2363. else
  2364. musb->io.write_fifo = musb_default_write_fifo;
  2365. if (!musb->xceiv->io_ops) {
  2366. musb->xceiv->io_dev = musb->controller;
  2367. musb->xceiv->io_priv = musb->mregs;
  2368. musb->xceiv->io_ops = &musb_ulpi_access;
  2369. }
  2370. if (musb->ops->phy_callback)
  2371. musb_phy_callback = musb->ops->phy_callback;
  2372. /*
  2373. * We need musb_read/write functions initialized for PM.
  2374. * Note that at least 2430 glue needs autosuspend delay
  2375. * somewhere above 300 ms for the hardware to idle properly
  2376. * after disconnecting the cable in host mode. Let's use
  2377. * 500 ms for some margin.
  2378. */
  2379. pm_runtime_use_autosuspend(musb->controller);
  2380. pm_runtime_set_autosuspend_delay(musb->controller, 500);
  2381. pm_runtime_enable(musb->controller);
  2382. pm_runtime_get_sync(musb->controller);
  2383. status = usb_phy_init(musb->xceiv);
  2384. if (status < 0)
  2385. goto err_usb_phy_init;
  2386. if (use_dma && dev->dma_mask) {
  2387. musb->dma_controller =
  2388. musb_dma_controller_create(musb, musb->mregs);
  2389. if (IS_ERR(musb->dma_controller)) {
  2390. status = PTR_ERR(musb->dma_controller);
  2391. goto fail2_5;
  2392. }
  2393. }
  2394. /* be sure interrupts are disabled before connecting ISR */
  2395. musb_platform_disable(musb);
  2396. musb_disable_interrupts(musb);
  2397. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2398. /* MUSB_POWER_SOFTCONN might be already set, JZ4740 does this. */
  2399. musb_writeb(musb->mregs, MUSB_POWER, 0);
  2400. /* Init IRQ workqueue before request_irq */
  2401. INIT_DELAYED_WORK(&musb->irq_work, musb_irq_work);
  2402. INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
  2403. INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
  2404. INIT_WORK(&musb->recovery_usb_work, musb_recovery_usb_proc);
  2405. timer_setup(&musb->musb_reset_timer, musb_reset_timer_handler, 0);
  2406. /* setup musb parts of the core (especially endpoints) */
  2407. status = musb_core_init(plat->config->multipoint
  2408. ? MUSB_CONTROLLER_MHDRC
  2409. : MUSB_CONTROLLER_HDRC, musb);
  2410. if (status < 0)
  2411. goto fail3;
  2412. timer_setup(&musb->otg_timer, musb_otg_timer_func, 0);
  2413. /* attach to the IRQ */
  2414. if (request_irq(nIrq, musb->isr, IRQF_SHARED, dev_name(dev), musb)) {
  2415. dev_err(dev, "request_irq %d failed!\n", nIrq);
  2416. status = -ENODEV;
  2417. goto fail3;
  2418. }
  2419. musb->nIrq = nIrq;
  2420. /* FIXME this handles wakeup irqs wrong */
  2421. if (enable_irq_wake(nIrq) == 0) {
  2422. musb->irq_wake = 1;
  2423. device_init_wakeup(dev, 1);
  2424. } else {
  2425. musb->irq_wake = 0;
  2426. }
  2427. /* program PHY to use external vBus if required */
  2428. if (plat->extvbus) {
  2429. u8 busctl = musb_readb(musb->mregs, MUSB_ULPI_BUSCONTROL);
  2430. busctl |= MUSB_ULPI_USE_EXTVBUS;
  2431. musb_writeb(musb->mregs, MUSB_ULPI_BUSCONTROL, busctl);
  2432. }
  2433. MUSB_DEV_MODE(musb);
  2434. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  2435. switch (musb->port_mode) {
  2436. case MUSB_HOST:
  2437. status = musb_host_setup(musb, plat->power);
  2438. if (status < 0)
  2439. goto fail3;
  2440. status = musb_platform_set_mode(musb, MUSB_HOST);
  2441. break;
  2442. case MUSB_PERIPHERAL:
  2443. status = musb_gadget_setup(musb);
  2444. if (status < 0)
  2445. goto fail3;
  2446. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  2447. break;
  2448. case MUSB_OTG:
  2449. status = musb_host_setup(musb, plat->power);
  2450. if (status < 0)
  2451. goto fail3;
  2452. status = musb_gadget_setup(musb);
  2453. if (status) {
  2454. musb_host_cleanup(musb);
  2455. goto fail3;
  2456. }
  2457. status = musb_platform_set_mode(musb, MUSB_OTG);
  2458. break;
  2459. default:
  2460. dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
  2461. break;
  2462. }
  2463. if (status < 0)
  2464. goto fail3;
  2465. musb_init_debugfs(musb);
  2466. status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  2467. if (status)
  2468. goto fail5;
  2469. musb->is_initialized = 1;
  2470. pm_runtime_mark_last_busy(musb->controller);
  2471. pm_runtime_put_autosuspend(musb->controller);
  2472. return 0;
  2473. fail5:
  2474. musb_exit_debugfs(musb);
  2475. musb_gadget_cleanup(musb);
  2476. musb_host_cleanup(musb);
  2477. fail3:
  2478. cancel_delayed_work_sync(&musb->irq_work);
  2479. cancel_delayed_work_sync(&musb->finish_resume_work);
  2480. cancel_delayed_work_sync(&musb->deassert_reset_work);
  2481. if (musb->dma_controller)
  2482. musb_dma_controller_destroy(musb->dma_controller);
  2483. fail2_5:
  2484. usb_phy_shutdown(musb->xceiv);
  2485. err_usb_phy_init:
  2486. pm_runtime_dont_use_autosuspend(musb->controller);
  2487. pm_runtime_put_sync(musb->controller);
  2488. pm_runtime_disable(musb->controller);
  2489. fail2:
  2490. if (musb->irq_wake)
  2491. device_init_wakeup(dev, 0);
  2492. musb_platform_exit(musb);
  2493. fail1:
  2494. if (status != -EPROBE_DEFER)
  2495. dev_err(musb->controller,
  2496. "%s failed with status %d\n", __func__, status);
  2497. musb_free(musb);
  2498. fail0:
  2499. return status;
  2500. }
  2501. /*-------------------------------------------------------------------------*/
  2502. /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  2503. * bridge to a platform device; this driver then suffices.
  2504. */
  2505. static int musb_probe(struct platform_device *pdev)
  2506. {
  2507. struct device *dev = &pdev->dev;
  2508. int irq = platform_get_irq_byname(pdev, "mc");
  2509. struct resource *iomem;
  2510. void __iomem *base;
  2511. if (irq <= 0)
  2512. return -ENODEV;
  2513. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2514. base = devm_ioremap_resource(dev, iomem);
  2515. if (IS_ERR(base))
  2516. return PTR_ERR(base);
  2517. return musb_init_controller(dev, irq, base);
  2518. }
  2519. static int musb_remove(struct platform_device *pdev)
  2520. {
  2521. struct device *dev = &pdev->dev;
  2522. struct musb *musb = dev_to_musb(dev);
  2523. unsigned long flags;
  2524. /* this gets called on rmmod.
  2525. * - Host mode: host may still be active
  2526. * - Peripheral mode: peripheral is deactivated (or never-activated)
  2527. * - OTG mode: both roles are deactivated (or never-activated)
  2528. */
  2529. musb_exit_debugfs(musb);
  2530. cancel_delayed_work_sync(&musb->irq_work);
  2531. cancel_delayed_work_sync(&musb->finish_resume_work);
  2532. cancel_delayed_work_sync(&musb->deassert_reset_work);
  2533. pm_runtime_get_sync(musb->controller);
  2534. musb_host_cleanup(musb);
  2535. musb_gadget_cleanup(musb);
  2536. musb_platform_disable(musb);
  2537. spin_lock_irqsave(&musb->lock, flags);
  2538. musb_disable_interrupts(musb);
  2539. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2540. spin_unlock_irqrestore(&musb->lock, flags);
  2541. musb_platform_exit(musb);
  2542. pm_runtime_dont_use_autosuspend(musb->controller);
  2543. pm_runtime_put_sync(musb->controller);
  2544. pm_runtime_disable(musb->controller);
  2545. musb_phy_callback = NULL;
  2546. if (musb->dma_controller)
  2547. musb_dma_controller_destroy(musb->dma_controller);
  2548. usb_phy_shutdown(musb->xceiv);
  2549. musb_free(musb);
  2550. device_init_wakeup(dev, 0);
  2551. return 0;
  2552. }
  2553. #ifdef CONFIG_PM
  2554. static void musb_save_context(struct musb *musb)
  2555. {
  2556. int i;
  2557. void __iomem *musb_base = musb->mregs;
  2558. void __iomem *epio;
  2559. musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
  2560. musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
  2561. musb->context.busctl = musb_readb(musb_base, MUSB_ULPI_BUSCONTROL);
  2562. musb->context.power = musb_readb(musb_base, MUSB_POWER);
  2563. musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
  2564. musb->context.index = musb_readb(musb_base, MUSB_INDEX);
  2565. musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
  2566. for (i = 0; i < musb->config->num_eps; ++i) {
  2567. struct musb_hw_ep *hw_ep;
  2568. hw_ep = &musb->endpoints[i];
  2569. if (!hw_ep)
  2570. continue;
  2571. epio = hw_ep->regs;
  2572. if (!epio)
  2573. continue;
  2574. musb_writeb(musb_base, MUSB_INDEX, i);
  2575. musb->context.index_regs[i].txmaxp =
  2576. musb_readw(epio, MUSB_TXMAXP);
  2577. musb->context.index_regs[i].txcsr =
  2578. musb_readw(epio, MUSB_TXCSR);
  2579. musb->context.index_regs[i].rxmaxp =
  2580. musb_readw(epio, MUSB_RXMAXP);
  2581. musb->context.index_regs[i].rxcsr =
  2582. musb_readw(epio, MUSB_RXCSR);
  2583. if (musb->dyn_fifo) {
  2584. musb->context.index_regs[i].txfifoadd =
  2585. musb_readw(musb_base, MUSB_TXFIFOADD);
  2586. musb->context.index_regs[i].rxfifoadd =
  2587. musb_readw(musb_base, MUSB_RXFIFOADD);
  2588. musb->context.index_regs[i].txfifosz =
  2589. musb_readb(musb_base, MUSB_TXFIFOSZ);
  2590. musb->context.index_regs[i].rxfifosz =
  2591. musb_readb(musb_base, MUSB_RXFIFOSZ);
  2592. }
  2593. musb->context.index_regs[i].txtype =
  2594. musb_readb(epio, MUSB_TXTYPE);
  2595. musb->context.index_regs[i].txinterval =
  2596. musb_readb(epio, MUSB_TXINTERVAL);
  2597. musb->context.index_regs[i].rxtype =
  2598. musb_readb(epio, MUSB_RXTYPE);
  2599. musb->context.index_regs[i].rxinterval =
  2600. musb_readb(epio, MUSB_RXINTERVAL);
  2601. musb->context.index_regs[i].txfunaddr =
  2602. musb_read_txfunaddr(musb, i);
  2603. musb->context.index_regs[i].txhubaddr =
  2604. musb_read_txhubaddr(musb, i);
  2605. musb->context.index_regs[i].txhubport =
  2606. musb_read_txhubport(musb, i);
  2607. musb->context.index_regs[i].rxfunaddr =
  2608. musb_read_rxfunaddr(musb, i);
  2609. musb->context.index_regs[i].rxhubaddr =
  2610. musb_read_rxhubaddr(musb, i);
  2611. musb->context.index_regs[i].rxhubport =
  2612. musb_read_rxhubport(musb, i);
  2613. }
  2614. }
  2615. static void musb_restore_context(struct musb *musb)
  2616. {
  2617. int i;
  2618. void __iomem *musb_base = musb->mregs;
  2619. void __iomem *epio;
  2620. u8 power;
  2621. musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
  2622. musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
  2623. musb_writeb(musb_base, MUSB_ULPI_BUSCONTROL, musb->context.busctl);
  2624. /* Don't affect SUSPENDM/RESUME bits in POWER reg */
  2625. power = musb_readb(musb_base, MUSB_POWER);
  2626. power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
  2627. musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
  2628. power |= musb->context.power;
  2629. musb_writeb(musb_base, MUSB_POWER, power);
  2630. musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
  2631. musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
  2632. musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
  2633. if (musb->context.devctl & MUSB_DEVCTL_SESSION)
  2634. musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
  2635. for (i = 0; i < musb->config->num_eps; ++i) {
  2636. struct musb_hw_ep *hw_ep;
  2637. hw_ep = &musb->endpoints[i];
  2638. if (!hw_ep)
  2639. continue;
  2640. epio = hw_ep->regs;
  2641. if (!epio)
  2642. continue;
  2643. musb_writeb(musb_base, MUSB_INDEX, i);
  2644. musb_writew(epio, MUSB_TXMAXP,
  2645. musb->context.index_regs[i].txmaxp);
  2646. musb_writew(epio, MUSB_TXCSR,
  2647. musb->context.index_regs[i].txcsr);
  2648. musb_writew(epio, MUSB_RXMAXP,
  2649. musb->context.index_regs[i].rxmaxp);
  2650. musb_writew(epio, MUSB_RXCSR,
  2651. musb->context.index_regs[i].rxcsr);
  2652. if (musb->dyn_fifo) {
  2653. musb_writeb(musb_base, MUSB_TXFIFOSZ,
  2654. musb->context.index_regs[i].txfifosz);
  2655. musb_writeb(musb_base, MUSB_RXFIFOSZ,
  2656. musb->context.index_regs[i].rxfifosz);
  2657. musb_writew(musb_base, MUSB_TXFIFOADD,
  2658. musb->context.index_regs[i].txfifoadd);
  2659. musb_writew(musb_base, MUSB_RXFIFOADD,
  2660. musb->context.index_regs[i].rxfifoadd);
  2661. }
  2662. musb_writeb(epio, MUSB_TXTYPE,
  2663. musb->context.index_regs[i].txtype);
  2664. musb_writeb(epio, MUSB_TXINTERVAL,
  2665. musb->context.index_regs[i].txinterval);
  2666. musb_writeb(epio, MUSB_RXTYPE,
  2667. musb->context.index_regs[i].rxtype);
  2668. musb_writeb(epio, MUSB_RXINTERVAL,
  2669. musb->context.index_regs[i].rxinterval);
  2670. musb_write_txfunaddr(musb, i,
  2671. musb->context.index_regs[i].txfunaddr);
  2672. musb_write_txhubaddr(musb, i,
  2673. musb->context.index_regs[i].txhubaddr);
  2674. musb_write_txhubport(musb, i,
  2675. musb->context.index_regs[i].txhubport);
  2676. musb_write_rxfunaddr(musb, i,
  2677. musb->context.index_regs[i].rxfunaddr);
  2678. musb_write_rxhubaddr(musb, i,
  2679. musb->context.index_regs[i].rxhubaddr);
  2680. musb_write_rxhubport(musb, i,
  2681. musb->context.index_regs[i].rxhubport);
  2682. }
  2683. musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
  2684. }
  2685. static int musb_suspend(struct device *dev)
  2686. {
  2687. struct musb *musb = dev_to_musb(dev);
  2688. unsigned long flags;
  2689. int ret;
  2690. ret = pm_runtime_get_sync(dev);
  2691. if (ret < 0) {
  2692. pm_runtime_put_noidle(dev);
  2693. return ret;
  2694. }
  2695. musb_platform_disable(musb);
  2696. musb_disable_interrupts(musb);
  2697. musb->flush_irq_work = true;
  2698. while (flush_delayed_work(&musb->irq_work))
  2699. ;
  2700. musb->flush_irq_work = false;
  2701. if (!(musb->ops->quirks & MUSB_PRESERVE_SESSION))
  2702. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2703. WARN_ON(!list_empty(&musb->pending_list));
  2704. spin_lock_irqsave(&musb->lock, flags);
  2705. if (is_peripheral_active(musb)) {
  2706. /* FIXME force disconnect unless we know USB will wake
  2707. * the system up quickly enough to respond ...
  2708. */
  2709. } else if (is_host_active(musb)) {
  2710. /* we know all the children are suspended; sometimes
  2711. * they will even be wakeup-enabled.
  2712. */
  2713. }
  2714. musb_save_context(musb);
  2715. spin_unlock_irqrestore(&musb->lock, flags);
  2716. return 0;
  2717. }
  2718. static int musb_resume(struct device *dev)
  2719. {
  2720. struct musb *musb = dev_to_musb(dev);
  2721. unsigned long flags;
  2722. int error;
  2723. u8 devctl;
  2724. u8 mask;
  2725. /*
  2726. * For static cmos like DaVinci, register values were preserved
  2727. * unless for some reason the whole soc powered down or the USB
  2728. * module got reset through the PSC (vs just being disabled).
  2729. *
  2730. * For the DSPS glue layer though, a full register restore has to
  2731. * be done. As it shouldn't harm other platforms, we do it
  2732. * unconditionally.
  2733. */
  2734. musb_restore_context(musb);
  2735. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2736. mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
  2737. if ((devctl & mask) != (musb->context.devctl & mask))
  2738. musb->port1_status = 0;
  2739. musb_enable_interrupts(musb);
  2740. musb_platform_enable(musb);
  2741. /* session might be disabled in suspend */
  2742. if (musb->port_mode == MUSB_HOST &&
  2743. !(musb->ops->quirks & MUSB_PRESERVE_SESSION)) {
  2744. devctl |= MUSB_DEVCTL_SESSION;
  2745. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  2746. }
  2747. spin_lock_irqsave(&musb->lock, flags);
  2748. error = musb_run_resume_work(musb);
  2749. if (error)
  2750. dev_err(musb->controller, "resume work failed with %i\n",
  2751. error);
  2752. spin_unlock_irqrestore(&musb->lock, flags);
  2753. pm_runtime_mark_last_busy(dev);
  2754. pm_runtime_put_autosuspend(dev);
  2755. return 0;
  2756. }
  2757. static int musb_runtime_suspend(struct device *dev)
  2758. {
  2759. struct musb *musb = dev_to_musb(dev);
  2760. musb_save_context(musb);
  2761. musb->is_runtime_suspended = 1;
  2762. return 0;
  2763. }
  2764. static int musb_runtime_resume(struct device *dev)
  2765. {
  2766. struct musb *musb = dev_to_musb(dev);
  2767. unsigned long flags;
  2768. int error;
  2769. /*
  2770. * When pm_runtime_get_sync called for the first time in driver
  2771. * init, some of the structure is still not initialized which is
  2772. * used in restore function. But clock needs to be
  2773. * enabled before any register access, so
  2774. * pm_runtime_get_sync has to be called.
  2775. * Also context restore without save does not make
  2776. * any sense
  2777. */
  2778. if (!musb->is_initialized)
  2779. return 0;
  2780. musb_restore_context(musb);
  2781. spin_lock_irqsave(&musb->lock, flags);
  2782. error = musb_run_resume_work(musb);
  2783. if (error)
  2784. dev_err(musb->controller, "resume work failed with %i\n",
  2785. error);
  2786. musb->is_runtime_suspended = 0;
  2787. spin_unlock_irqrestore(&musb->lock, flags);
  2788. return 0;
  2789. }
  2790. static const struct dev_pm_ops musb_dev_pm_ops = {
  2791. .suspend = musb_suspend,
  2792. .resume = musb_resume,
  2793. .runtime_suspend = musb_runtime_suspend,
  2794. .runtime_resume = musb_runtime_resume,
  2795. };
  2796. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2797. #else
  2798. #define MUSB_DEV_PM_OPS NULL
  2799. #endif
  2800. static struct platform_driver musb_driver = {
  2801. .driver = {
  2802. .name = (char *)musb_driver_name,
  2803. .bus = &platform_bus_type,
  2804. .pm = MUSB_DEV_PM_OPS,
  2805. },
  2806. .probe = musb_probe,
  2807. .remove = musb_remove,
  2808. };
  2809. module_platform_driver(musb_driver);