musb_core.c 78 KB

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