musb_core.c 66 KB

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