musb_core.c 62 KB

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