musb_core.c 67 KB

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