musb_core.c 71 KB

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