musb_core.c 65 KB

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