musb_core.c 69 KB

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