musb_core.c 65 KB

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