musb_core.c 70 KB

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