musb_core.c 67 KB

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