musb_core.c 87 KB

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