musb_core.c 65 KB

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