musb_core.c 66 KB

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