musb_core.c 71 KB

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