musb_core.c 67 KB

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