musb_core.c 62 KB

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