musb_core.c 67 KB

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