musb_core.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213
  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_OTG);
  2042. }
  2043. error = pm_runtime_get_sync(musb->controller);
  2044. if (error < 0) {
  2045. dev_err(musb->controller, "Could not enable: %i\n", error);
  2046. return;
  2047. }
  2048. musb_pm_runtime_check_session(musb);
  2049. if (musb->xceiv->otg->state != musb->xceiv_old_state) {
  2050. musb->xceiv_old_state = musb->xceiv->otg->state;
  2051. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  2052. }
  2053. pm_runtime_mark_last_busy(musb->controller);
  2054. pm_runtime_put_autosuspend(musb->controller);
  2055. }
  2056. static void musb_recover_from_babble(struct musb *musb)
  2057. {
  2058. int ret;
  2059. u8 devctl;
  2060. musb_disable_interrupts(musb);
  2061. /*
  2062. * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
  2063. * it some slack and wait for 10us.
  2064. */
  2065. udelay(10);
  2066. ret = musb_platform_recover(musb);
  2067. if (ret) {
  2068. musb_enable_interrupts(musb);
  2069. return;
  2070. }
  2071. /* drop session bit */
  2072. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2073. devctl &= ~MUSB_DEVCTL_SESSION;
  2074. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  2075. /* tell usbcore about it */
  2076. musb_root_disconnect(musb);
  2077. /*
  2078. * When a babble condition occurs, the musb controller
  2079. * removes the session bit and the endpoint config is lost.
  2080. */
  2081. if (musb->dyn_fifo)
  2082. ret = ep_config_from_table(musb);
  2083. else
  2084. ret = ep_config_from_hw(musb);
  2085. /* restart session */
  2086. if (ret == 0)
  2087. musb_start(musb);
  2088. }
  2089. /* --------------------------------------------------------------------------
  2090. * Init support
  2091. */
  2092. static struct musb *allocate_instance(struct device *dev,
  2093. const struct musb_hdrc_config *config, void __iomem *mbase)
  2094. {
  2095. struct musb *musb;
  2096. struct musb_hw_ep *ep;
  2097. int epnum;
  2098. int ret;
  2099. musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
  2100. if (!musb)
  2101. return NULL;
  2102. INIT_LIST_HEAD(&musb->control);
  2103. INIT_LIST_HEAD(&musb->in_bulk);
  2104. INIT_LIST_HEAD(&musb->out_bulk);
  2105. INIT_LIST_HEAD(&musb->pending_list);
  2106. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  2107. musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
  2108. musb->mregs = mbase;
  2109. musb->ctrl_base = mbase;
  2110. musb->nIrq = -ENODEV;
  2111. musb->config = config;
  2112. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  2113. for (epnum = 0, ep = musb->endpoints;
  2114. epnum < musb->config->num_eps;
  2115. epnum++, ep++) {
  2116. ep->musb = musb;
  2117. ep->epnum = epnum;
  2118. }
  2119. musb->controller = dev;
  2120. ret = musb_host_alloc(musb);
  2121. if (ret < 0)
  2122. goto err_free;
  2123. dev_set_drvdata(dev, musb);
  2124. return musb;
  2125. err_free:
  2126. return NULL;
  2127. }
  2128. static void musb_free(struct musb *musb)
  2129. {
  2130. /* this has multiple entry modes. it handles fault cleanup after
  2131. * probe(), where things may be partially set up, as well as rmmod
  2132. * cleanup after everything's been de-activated.
  2133. */
  2134. #ifdef CONFIG_SYSFS
  2135. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  2136. #endif
  2137. if (musb->nIrq >= 0) {
  2138. if (musb->irq_wake)
  2139. disable_irq_wake(musb->nIrq);
  2140. free_irq(musb->nIrq, musb);
  2141. }
  2142. musb_host_free(musb);
  2143. }
  2144. struct musb_pending_work {
  2145. int (*callback)(struct musb *musb, void *data);
  2146. void *data;
  2147. struct list_head node;
  2148. };
  2149. #ifdef CONFIG_PM
  2150. /*
  2151. * Called from musb_runtime_resume(), musb_resume(), and
  2152. * musb_queue_resume_work(). Callers must take musb->lock.
  2153. */
  2154. static int musb_run_resume_work(struct musb *musb)
  2155. {
  2156. struct musb_pending_work *w, *_w;
  2157. unsigned long flags;
  2158. int error = 0;
  2159. spin_lock_irqsave(&musb->list_lock, flags);
  2160. list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
  2161. if (w->callback) {
  2162. error = w->callback(musb, w->data);
  2163. if (error < 0) {
  2164. dev_err(musb->controller,
  2165. "resume callback %p failed: %i\n",
  2166. w->callback, error);
  2167. }
  2168. }
  2169. list_del(&w->node);
  2170. devm_kfree(musb->controller, w);
  2171. }
  2172. spin_unlock_irqrestore(&musb->list_lock, flags);
  2173. return error;
  2174. }
  2175. #endif
  2176. /*
  2177. * Called to run work if device is active or else queue the work to happen
  2178. * on resume. Caller must take musb->lock and must hold an RPM reference.
  2179. *
  2180. * Note that we cowardly refuse queuing work after musb PM runtime
  2181. * resume is done calling musb_run_resume_work() and return -EINPROGRESS
  2182. * instead.
  2183. */
  2184. int musb_queue_resume_work(struct musb *musb,
  2185. int (*callback)(struct musb *musb, void *data),
  2186. void *data)
  2187. {
  2188. struct musb_pending_work *w;
  2189. unsigned long flags;
  2190. bool is_suspended;
  2191. int error;
  2192. if (WARN_ON(!callback))
  2193. return -EINVAL;
  2194. spin_lock_irqsave(&musb->list_lock, flags);
  2195. is_suspended = musb->is_runtime_suspended;
  2196. if (is_suspended) {
  2197. w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
  2198. if (!w) {
  2199. error = -ENOMEM;
  2200. goto out_unlock;
  2201. }
  2202. w->callback = callback;
  2203. w->data = data;
  2204. list_add_tail(&w->node, &musb->pending_list);
  2205. error = 0;
  2206. }
  2207. out_unlock:
  2208. spin_unlock_irqrestore(&musb->list_lock, flags);
  2209. if (!is_suspended)
  2210. error = callback(musb, data);
  2211. return error;
  2212. }
  2213. EXPORT_SYMBOL_GPL(musb_queue_resume_work);
  2214. static void musb_deassert_reset(struct work_struct *work)
  2215. {
  2216. struct musb *musb;
  2217. unsigned long flags;
  2218. musb = container_of(work, struct musb, deassert_reset_work.work);
  2219. spin_lock_irqsave(&musb->lock, flags);
  2220. if (musb->port1_status & USB_PORT_STAT_RESET)
  2221. musb_port_reset(musb, false);
  2222. spin_unlock_irqrestore(&musb->lock, flags);
  2223. }
  2224. /*
  2225. * Perform generic per-controller initialization.
  2226. *
  2227. * @dev: the controller (already clocked, etc)
  2228. * @nIrq: IRQ number
  2229. * @ctrl: virtual address of controller registers,
  2230. * not yet corrected for platform-specific offsets
  2231. */
  2232. static int
  2233. musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  2234. {
  2235. int status;
  2236. struct musb *musb;
  2237. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  2238. /* The driver might handle more features than the board; OK.
  2239. * Fail when the board needs a feature that's not enabled.
  2240. */
  2241. if (!plat) {
  2242. dev_err(dev, "no platform_data?\n");
  2243. status = -ENODEV;
  2244. goto fail0;
  2245. }
  2246. /* allocate */
  2247. musb = allocate_instance(dev, plat->config, ctrl);
  2248. if (!musb) {
  2249. status = -ENOMEM;
  2250. goto fail0;
  2251. }
  2252. spin_lock_init(&musb->lock);
  2253. spin_lock_init(&musb->list_lock);
  2254. musb->board_set_power = plat->set_power;
  2255. musb->min_power = plat->min_power;
  2256. musb->ops = plat->platform_ops;
  2257. musb->port_mode = plat->mode;
  2258. /*
  2259. * Initialize the default IO functions. At least omap2430 needs
  2260. * these early. We initialize the platform specific IO functions
  2261. * later on.
  2262. */
  2263. musb_readb = musb_default_readb;
  2264. musb_writeb = musb_default_writeb;
  2265. musb_readw = musb_default_readw;
  2266. musb_writew = musb_default_writew;
  2267. /* The musb_platform_init() call:
  2268. * - adjusts musb->mregs
  2269. * - sets the musb->isr
  2270. * - may initialize an integrated transceiver
  2271. * - initializes musb->xceiv, usually by otg_get_phy()
  2272. * - stops powering VBUS
  2273. *
  2274. * There are various transceiver configurations.
  2275. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  2276. * external/discrete ones in various flavors (twl4030 family,
  2277. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  2278. */
  2279. status = musb_platform_init(musb);
  2280. if (status < 0)
  2281. goto fail1;
  2282. if (!musb->isr) {
  2283. status = -ENODEV;
  2284. goto fail2;
  2285. }
  2286. /* Most devices use indexed offset or flat offset */
  2287. if (musb->ops->quirks & MUSB_INDEXED_EP) {
  2288. musb->io.ep_offset = musb_indexed_ep_offset;
  2289. musb->io.ep_select = musb_indexed_ep_select;
  2290. } else {
  2291. musb->io.ep_offset = musb_flat_ep_offset;
  2292. musb->io.ep_select = musb_flat_ep_select;
  2293. }
  2294. if (musb->ops->quirks & MUSB_G_NO_SKB_RESERVE)
  2295. musb->g.quirk_avoids_skb_reserve = 1;
  2296. /* At least tusb6010 has its own offsets */
  2297. if (musb->ops->ep_offset)
  2298. musb->io.ep_offset = musb->ops->ep_offset;
  2299. if (musb->ops->ep_select)
  2300. musb->io.ep_select = musb->ops->ep_select;
  2301. if (musb->ops->fifo_mode)
  2302. fifo_mode = musb->ops->fifo_mode;
  2303. else
  2304. fifo_mode = 4;
  2305. if (musb->ops->fifo_offset)
  2306. musb->io.fifo_offset = musb->ops->fifo_offset;
  2307. else
  2308. musb->io.fifo_offset = musb_default_fifo_offset;
  2309. if (musb->ops->busctl_offset)
  2310. musb->io.busctl_offset = musb->ops->busctl_offset;
  2311. else
  2312. musb->io.busctl_offset = musb_default_busctl_offset;
  2313. if (musb->ops->readb)
  2314. musb_readb = musb->ops->readb;
  2315. if (musb->ops->writeb)
  2316. musb_writeb = musb->ops->writeb;
  2317. if (musb->ops->readw)
  2318. musb_readw = musb->ops->readw;
  2319. if (musb->ops->writew)
  2320. musb_writew = musb->ops->writew;
  2321. #ifndef CONFIG_MUSB_PIO_ONLY
  2322. if (!musb->ops->dma_init || !musb->ops->dma_exit) {
  2323. dev_err(dev, "DMA controller not set\n");
  2324. status = -ENODEV;
  2325. goto fail2;
  2326. }
  2327. musb_dma_controller_create = musb->ops->dma_init;
  2328. musb_dma_controller_destroy = musb->ops->dma_exit;
  2329. #endif
  2330. if (musb->ops->read_fifo)
  2331. musb->io.read_fifo = musb->ops->read_fifo;
  2332. else
  2333. musb->io.read_fifo = musb_default_read_fifo;
  2334. if (musb->ops->write_fifo)
  2335. musb->io.write_fifo = musb->ops->write_fifo;
  2336. else
  2337. musb->io.write_fifo = musb_default_write_fifo;
  2338. if (!musb->xceiv->io_ops) {
  2339. musb->xceiv->io_dev = musb->controller;
  2340. musb->xceiv->io_priv = musb->mregs;
  2341. musb->xceiv->io_ops = &musb_ulpi_access;
  2342. }
  2343. if (musb->ops->phy_callback)
  2344. musb_phy_callback = musb->ops->phy_callback;
  2345. /*
  2346. * We need musb_read/write functions initialized for PM.
  2347. * Note that at least 2430 glue needs autosuspend delay
  2348. * somewhere above 300 ms for the hardware to idle properly
  2349. * after disconnecting the cable in host mode. Let's use
  2350. * 500 ms for some margin.
  2351. */
  2352. pm_runtime_use_autosuspend(musb->controller);
  2353. pm_runtime_set_autosuspend_delay(musb->controller, 500);
  2354. pm_runtime_enable(musb->controller);
  2355. pm_runtime_get_sync(musb->controller);
  2356. status = usb_phy_init(musb->xceiv);
  2357. if (status < 0)
  2358. goto err_usb_phy_init;
  2359. if (use_dma && dev->dma_mask) {
  2360. musb->dma_controller =
  2361. musb_dma_controller_create(musb, musb->mregs);
  2362. if (IS_ERR(musb->dma_controller)) {
  2363. status = PTR_ERR(musb->dma_controller);
  2364. goto fail2_5;
  2365. }
  2366. }
  2367. /* be sure interrupts are disabled before connecting ISR */
  2368. musb_platform_disable(musb);
  2369. musb_disable_interrupts(musb);
  2370. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2371. /* MUSB_POWER_SOFTCONN might be already set, JZ4740 does this. */
  2372. musb_writeb(musb->mregs, MUSB_POWER, 0);
  2373. /* Init IRQ workqueue before request_irq */
  2374. INIT_DELAYED_WORK(&musb->irq_work, musb_irq_work);
  2375. INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
  2376. INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
  2377. /* setup musb parts of the core (especially endpoints) */
  2378. status = musb_core_init(plat->config->multipoint
  2379. ? MUSB_CONTROLLER_MHDRC
  2380. : MUSB_CONTROLLER_HDRC, musb);
  2381. if (status < 0)
  2382. goto fail3;
  2383. timer_setup(&musb->otg_timer, musb_otg_timer_func, 0);
  2384. /* attach to the IRQ */
  2385. if (request_irq(nIrq, musb->isr, IRQF_SHARED, dev_name(dev), musb)) {
  2386. dev_err(dev, "request_irq %d failed!\n", nIrq);
  2387. status = -ENODEV;
  2388. goto fail3;
  2389. }
  2390. musb->nIrq = nIrq;
  2391. /* FIXME this handles wakeup irqs wrong */
  2392. if (enable_irq_wake(nIrq) == 0) {
  2393. musb->irq_wake = 1;
  2394. device_init_wakeup(dev, 1);
  2395. } else {
  2396. musb->irq_wake = 0;
  2397. }
  2398. /* program PHY to use external vBus if required */
  2399. if (plat->extvbus) {
  2400. u8 busctl = musb_readb(musb->mregs, MUSB_ULPI_BUSCONTROL);
  2401. busctl |= MUSB_ULPI_USE_EXTVBUS;
  2402. musb_writeb(musb->mregs, MUSB_ULPI_BUSCONTROL, busctl);
  2403. }
  2404. MUSB_DEV_MODE(musb);
  2405. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  2406. switch (musb->port_mode) {
  2407. case MUSB_HOST:
  2408. status = musb_host_setup(musb, plat->power);
  2409. if (status < 0)
  2410. goto fail3;
  2411. status = musb_platform_set_mode(musb, MUSB_HOST);
  2412. break;
  2413. case MUSB_PERIPHERAL:
  2414. status = musb_gadget_setup(musb);
  2415. if (status < 0)
  2416. goto fail3;
  2417. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  2418. break;
  2419. case MUSB_OTG:
  2420. status = musb_host_setup(musb, plat->power);
  2421. if (status < 0)
  2422. goto fail3;
  2423. status = musb_gadget_setup(musb);
  2424. if (status) {
  2425. musb_host_cleanup(musb);
  2426. goto fail3;
  2427. }
  2428. status = musb_platform_set_mode(musb, MUSB_OTG);
  2429. break;
  2430. default:
  2431. dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
  2432. break;
  2433. }
  2434. if (status < 0)
  2435. goto fail3;
  2436. musb_init_debugfs(musb);
  2437. status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  2438. if (status)
  2439. goto fail5;
  2440. musb->is_initialized = 1;
  2441. pm_runtime_mark_last_busy(musb->controller);
  2442. pm_runtime_put_autosuspend(musb->controller);
  2443. return 0;
  2444. fail5:
  2445. musb_exit_debugfs(musb);
  2446. musb_gadget_cleanup(musb);
  2447. musb_host_cleanup(musb);
  2448. fail3:
  2449. cancel_delayed_work_sync(&musb->irq_work);
  2450. cancel_delayed_work_sync(&musb->finish_resume_work);
  2451. cancel_delayed_work_sync(&musb->deassert_reset_work);
  2452. if (musb->dma_controller)
  2453. musb_dma_controller_destroy(musb->dma_controller);
  2454. fail2_5:
  2455. usb_phy_shutdown(musb->xceiv);
  2456. err_usb_phy_init:
  2457. pm_runtime_dont_use_autosuspend(musb->controller);
  2458. pm_runtime_put_sync(musb->controller);
  2459. pm_runtime_disable(musb->controller);
  2460. fail2:
  2461. if (musb->irq_wake)
  2462. device_init_wakeup(dev, 0);
  2463. musb_platform_exit(musb);
  2464. fail1:
  2465. if (status != -EPROBE_DEFER)
  2466. dev_err(musb->controller,
  2467. "%s failed with status %d\n", __func__, status);
  2468. musb_free(musb);
  2469. fail0:
  2470. return status;
  2471. }
  2472. /*-------------------------------------------------------------------------*/
  2473. /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  2474. * bridge to a platform device; this driver then suffices.
  2475. */
  2476. static int musb_probe(struct platform_device *pdev)
  2477. {
  2478. struct device *dev = &pdev->dev;
  2479. int irq = platform_get_irq_byname(pdev, "mc");
  2480. struct resource *iomem;
  2481. void __iomem *base;
  2482. if (irq <= 0)
  2483. return -ENODEV;
  2484. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2485. base = devm_ioremap_resource(dev, iomem);
  2486. if (IS_ERR(base))
  2487. return PTR_ERR(base);
  2488. return musb_init_controller(dev, irq, base);
  2489. }
  2490. static int musb_remove(struct platform_device *pdev)
  2491. {
  2492. struct device *dev = &pdev->dev;
  2493. struct musb *musb = dev_to_musb(dev);
  2494. unsigned long flags;
  2495. /* this gets called on rmmod.
  2496. * - Host mode: host may still be active
  2497. * - Peripheral mode: peripheral is deactivated (or never-activated)
  2498. * - OTG mode: both roles are deactivated (or never-activated)
  2499. */
  2500. musb_exit_debugfs(musb);
  2501. cancel_delayed_work_sync(&musb->irq_work);
  2502. cancel_delayed_work_sync(&musb->finish_resume_work);
  2503. cancel_delayed_work_sync(&musb->deassert_reset_work);
  2504. pm_runtime_get_sync(musb->controller);
  2505. musb_host_cleanup(musb);
  2506. musb_gadget_cleanup(musb);
  2507. musb_platform_disable(musb);
  2508. spin_lock_irqsave(&musb->lock, flags);
  2509. musb_disable_interrupts(musb);
  2510. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2511. spin_unlock_irqrestore(&musb->lock, flags);
  2512. musb_platform_exit(musb);
  2513. pm_runtime_dont_use_autosuspend(musb->controller);
  2514. pm_runtime_put_sync(musb->controller);
  2515. pm_runtime_disable(musb->controller);
  2516. musb_phy_callback = NULL;
  2517. if (musb->dma_controller)
  2518. musb_dma_controller_destroy(musb->dma_controller);
  2519. usb_phy_shutdown(musb->xceiv);
  2520. musb_free(musb);
  2521. device_init_wakeup(dev, 0);
  2522. return 0;
  2523. }
  2524. #ifdef CONFIG_PM
  2525. static void musb_save_context(struct musb *musb)
  2526. {
  2527. int i;
  2528. void __iomem *musb_base = musb->mregs;
  2529. void __iomem *epio;
  2530. musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
  2531. musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
  2532. musb->context.busctl = musb_readb(musb_base, MUSB_ULPI_BUSCONTROL);
  2533. musb->context.power = musb_readb(musb_base, MUSB_POWER);
  2534. musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
  2535. musb->context.index = musb_readb(musb_base, MUSB_INDEX);
  2536. musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
  2537. for (i = 0; i < musb->config->num_eps; ++i) {
  2538. struct musb_hw_ep *hw_ep;
  2539. hw_ep = &musb->endpoints[i];
  2540. if (!hw_ep)
  2541. continue;
  2542. epio = hw_ep->regs;
  2543. if (!epio)
  2544. continue;
  2545. musb_writeb(musb_base, MUSB_INDEX, i);
  2546. musb->context.index_regs[i].txmaxp =
  2547. musb_readw(epio, MUSB_TXMAXP);
  2548. musb->context.index_regs[i].txcsr =
  2549. musb_readw(epio, MUSB_TXCSR);
  2550. musb->context.index_regs[i].rxmaxp =
  2551. musb_readw(epio, MUSB_RXMAXP);
  2552. musb->context.index_regs[i].rxcsr =
  2553. musb_readw(epio, MUSB_RXCSR);
  2554. if (musb->dyn_fifo) {
  2555. musb->context.index_regs[i].txfifoadd =
  2556. musb_readw(musb_base, MUSB_TXFIFOADD);
  2557. musb->context.index_regs[i].rxfifoadd =
  2558. musb_readw(musb_base, MUSB_RXFIFOADD);
  2559. musb->context.index_regs[i].txfifosz =
  2560. musb_readb(musb_base, MUSB_TXFIFOSZ);
  2561. musb->context.index_regs[i].rxfifosz =
  2562. musb_readb(musb_base, MUSB_RXFIFOSZ);
  2563. }
  2564. musb->context.index_regs[i].txtype =
  2565. musb_readb(epio, MUSB_TXTYPE);
  2566. musb->context.index_regs[i].txinterval =
  2567. musb_readb(epio, MUSB_TXINTERVAL);
  2568. musb->context.index_regs[i].rxtype =
  2569. musb_readb(epio, MUSB_RXTYPE);
  2570. musb->context.index_regs[i].rxinterval =
  2571. musb_readb(epio, MUSB_RXINTERVAL);
  2572. musb->context.index_regs[i].txfunaddr =
  2573. musb_read_txfunaddr(musb, i);
  2574. musb->context.index_regs[i].txhubaddr =
  2575. musb_read_txhubaddr(musb, i);
  2576. musb->context.index_regs[i].txhubport =
  2577. musb_read_txhubport(musb, i);
  2578. musb->context.index_regs[i].rxfunaddr =
  2579. musb_read_rxfunaddr(musb, i);
  2580. musb->context.index_regs[i].rxhubaddr =
  2581. musb_read_rxhubaddr(musb, i);
  2582. musb->context.index_regs[i].rxhubport =
  2583. musb_read_rxhubport(musb, i);
  2584. }
  2585. }
  2586. static void musb_restore_context(struct musb *musb)
  2587. {
  2588. int i;
  2589. void __iomem *musb_base = musb->mregs;
  2590. void __iomem *epio;
  2591. u8 power;
  2592. musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
  2593. musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
  2594. musb_writeb(musb_base, MUSB_ULPI_BUSCONTROL, musb->context.busctl);
  2595. /* Don't affect SUSPENDM/RESUME bits in POWER reg */
  2596. power = musb_readb(musb_base, MUSB_POWER);
  2597. power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
  2598. musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
  2599. power |= musb->context.power;
  2600. musb_writeb(musb_base, MUSB_POWER, power);
  2601. musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
  2602. musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
  2603. musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
  2604. if (musb->context.devctl & MUSB_DEVCTL_SESSION)
  2605. musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
  2606. for (i = 0; i < musb->config->num_eps; ++i) {
  2607. struct musb_hw_ep *hw_ep;
  2608. hw_ep = &musb->endpoints[i];
  2609. if (!hw_ep)
  2610. continue;
  2611. epio = hw_ep->regs;
  2612. if (!epio)
  2613. continue;
  2614. musb_writeb(musb_base, MUSB_INDEX, i);
  2615. musb_writew(epio, MUSB_TXMAXP,
  2616. musb->context.index_regs[i].txmaxp);
  2617. musb_writew(epio, MUSB_TXCSR,
  2618. musb->context.index_regs[i].txcsr);
  2619. musb_writew(epio, MUSB_RXMAXP,
  2620. musb->context.index_regs[i].rxmaxp);
  2621. musb_writew(epio, MUSB_RXCSR,
  2622. musb->context.index_regs[i].rxcsr);
  2623. if (musb->dyn_fifo) {
  2624. musb_writeb(musb_base, MUSB_TXFIFOSZ,
  2625. musb->context.index_regs[i].txfifosz);
  2626. musb_writeb(musb_base, MUSB_RXFIFOSZ,
  2627. musb->context.index_regs[i].rxfifosz);
  2628. musb_writew(musb_base, MUSB_TXFIFOADD,
  2629. musb->context.index_regs[i].txfifoadd);
  2630. musb_writew(musb_base, MUSB_RXFIFOADD,
  2631. musb->context.index_regs[i].rxfifoadd);
  2632. }
  2633. musb_writeb(epio, MUSB_TXTYPE,
  2634. musb->context.index_regs[i].txtype);
  2635. musb_writeb(epio, MUSB_TXINTERVAL,
  2636. musb->context.index_regs[i].txinterval);
  2637. musb_writeb(epio, MUSB_RXTYPE,
  2638. musb->context.index_regs[i].rxtype);
  2639. musb_writeb(epio, MUSB_RXINTERVAL,
  2640. musb->context.index_regs[i].rxinterval);
  2641. musb_write_txfunaddr(musb, i,
  2642. musb->context.index_regs[i].txfunaddr);
  2643. musb_write_txhubaddr(musb, i,
  2644. musb->context.index_regs[i].txhubaddr);
  2645. musb_write_txhubport(musb, i,
  2646. musb->context.index_regs[i].txhubport);
  2647. musb_write_rxfunaddr(musb, i,
  2648. musb->context.index_regs[i].rxfunaddr);
  2649. musb_write_rxhubaddr(musb, i,
  2650. musb->context.index_regs[i].rxhubaddr);
  2651. musb_write_rxhubport(musb, i,
  2652. musb->context.index_regs[i].rxhubport);
  2653. }
  2654. musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
  2655. }
  2656. static int musb_suspend(struct device *dev)
  2657. {
  2658. struct musb *musb = dev_to_musb(dev);
  2659. unsigned long flags;
  2660. int ret;
  2661. ret = pm_runtime_get_sync(dev);
  2662. if (ret < 0) {
  2663. pm_runtime_put_noidle(dev);
  2664. return ret;
  2665. }
  2666. musb_platform_disable(musb);
  2667. musb_disable_interrupts(musb);
  2668. musb->flush_irq_work = true;
  2669. while (flush_delayed_work(&musb->irq_work))
  2670. ;
  2671. musb->flush_irq_work = false;
  2672. if (!(musb->ops->quirks & MUSB_PRESERVE_SESSION))
  2673. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2674. WARN_ON(!list_empty(&musb->pending_list));
  2675. spin_lock_irqsave(&musb->lock, flags);
  2676. if (is_peripheral_active(musb)) {
  2677. /* FIXME force disconnect unless we know USB will wake
  2678. * the system up quickly enough to respond ...
  2679. */
  2680. } else if (is_host_active(musb)) {
  2681. /* we know all the children are suspended; sometimes
  2682. * they will even be wakeup-enabled.
  2683. */
  2684. }
  2685. musb_save_context(musb);
  2686. spin_unlock_irqrestore(&musb->lock, flags);
  2687. return 0;
  2688. }
  2689. static int musb_resume(struct device *dev)
  2690. {
  2691. struct musb *musb = dev_to_musb(dev);
  2692. unsigned long flags;
  2693. int error;
  2694. u8 devctl;
  2695. u8 mask;
  2696. /*
  2697. * For static cmos like DaVinci, register values were preserved
  2698. * unless for some reason the whole soc powered down or the USB
  2699. * module got reset through the PSC (vs just being disabled).
  2700. *
  2701. * For the DSPS glue layer though, a full register restore has to
  2702. * be done. As it shouldn't harm other platforms, we do it
  2703. * unconditionally.
  2704. */
  2705. musb_restore_context(musb);
  2706. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2707. mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
  2708. if ((devctl & mask) != (musb->context.devctl & mask))
  2709. musb->port1_status = 0;
  2710. musb_enable_interrupts(musb);
  2711. musb_platform_enable(musb);
  2712. /* session might be disabled in suspend */
  2713. if (musb->port_mode == MUSB_HOST &&
  2714. !(musb->ops->quirks & MUSB_PRESERVE_SESSION)) {
  2715. devctl |= MUSB_DEVCTL_SESSION;
  2716. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  2717. }
  2718. spin_lock_irqsave(&musb->lock, flags);
  2719. error = musb_run_resume_work(musb);
  2720. if (error)
  2721. dev_err(musb->controller, "resume work failed with %i\n",
  2722. error);
  2723. spin_unlock_irqrestore(&musb->lock, flags);
  2724. pm_runtime_mark_last_busy(dev);
  2725. pm_runtime_put_autosuspend(dev);
  2726. return 0;
  2727. }
  2728. static int musb_runtime_suspend(struct device *dev)
  2729. {
  2730. struct musb *musb = dev_to_musb(dev);
  2731. musb_save_context(musb);
  2732. musb->is_runtime_suspended = 1;
  2733. return 0;
  2734. }
  2735. static int musb_runtime_resume(struct device *dev)
  2736. {
  2737. struct musb *musb = dev_to_musb(dev);
  2738. unsigned long flags;
  2739. int error;
  2740. /*
  2741. * When pm_runtime_get_sync called for the first time in driver
  2742. * init, some of the structure is still not initialized which is
  2743. * used in restore function. But clock needs to be
  2744. * enabled before any register access, so
  2745. * pm_runtime_get_sync has to be called.
  2746. * Also context restore without save does not make
  2747. * any sense
  2748. */
  2749. if (!musb->is_initialized)
  2750. return 0;
  2751. musb_restore_context(musb);
  2752. spin_lock_irqsave(&musb->lock, flags);
  2753. error = musb_run_resume_work(musb);
  2754. if (error)
  2755. dev_err(musb->controller, "resume work failed with %i\n",
  2756. error);
  2757. musb->is_runtime_suspended = 0;
  2758. spin_unlock_irqrestore(&musb->lock, flags);
  2759. return 0;
  2760. }
  2761. static const struct dev_pm_ops musb_dev_pm_ops = {
  2762. .suspend = musb_suspend,
  2763. .resume = musb_resume,
  2764. .runtime_suspend = musb_runtime_suspend,
  2765. .runtime_resume = musb_runtime_resume,
  2766. };
  2767. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2768. #else
  2769. #define MUSB_DEV_PM_OPS NULL
  2770. #endif
  2771. static struct platform_driver musb_driver = {
  2772. .driver = {
  2773. .name = (char *)musb_driver_name,
  2774. .bus = &platform_bus_type,
  2775. .pm = MUSB_DEV_PM_OPS,
  2776. },
  2777. .probe = musb_probe,
  2778. .remove = musb_remove,
  2779. };
  2780. module_platform_driver(musb_driver);