musb_core.c 66 KB

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