musb_core.c 69 KB

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