musb_core.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  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. musb->xceiv->otg->state != OTG_STATE_A_WAIT_BCON &&
  920. (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
  921. musb->is_active = 1;
  922. } else {
  923. devctl |= MUSB_DEVCTL_SESSION;
  924. }
  925. musb_platform_enable(musb);
  926. musb_writeb(regs, MUSB_DEVCTL, devctl);
  927. }
  928. /*
  929. * Make the HDRC stop (disable interrupts, etc.);
  930. * reversible by musb_start
  931. * called on gadget driver unregister
  932. * with controller locked, irqs blocked
  933. * acts as a NOP unless some role activated the hardware
  934. */
  935. void musb_stop(struct musb *musb)
  936. {
  937. /* stop IRQs, timers, ... */
  938. musb_platform_disable(musb);
  939. musb_generic_disable(musb);
  940. dev_dbg(musb->controller, "HDRC disabled\n");
  941. /* FIXME
  942. * - mark host and/or peripheral drivers unusable/inactive
  943. * - disable DMA (and enable it in HdrcStart)
  944. * - make sure we can musb_start() after musb_stop(); with
  945. * OTG mode, gadget driver module rmmod/modprobe cycles that
  946. * - ...
  947. */
  948. musb_platform_try_idle(musb, 0);
  949. }
  950. static void musb_shutdown(struct platform_device *pdev)
  951. {
  952. struct musb *musb = dev_to_musb(&pdev->dev);
  953. unsigned long flags;
  954. pm_runtime_get_sync(musb->controller);
  955. musb_host_cleanup(musb);
  956. musb_gadget_cleanup(musb);
  957. spin_lock_irqsave(&musb->lock, flags);
  958. musb_platform_disable(musb);
  959. musb_generic_disable(musb);
  960. spin_unlock_irqrestore(&musb->lock, flags);
  961. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  962. musb_platform_exit(musb);
  963. pm_runtime_put(musb->controller);
  964. /* FIXME power down */
  965. }
  966. /*-------------------------------------------------------------------------*/
  967. /*
  968. * The silicon either has hard-wired endpoint configurations, or else
  969. * "dynamic fifo" sizing. The driver has support for both, though at this
  970. * writing only the dynamic sizing is very well tested. Since we switched
  971. * away from compile-time hardware parameters, we can no longer rely on
  972. * dead code elimination to leave only the relevant one in the object file.
  973. *
  974. * We don't currently use dynamic fifo setup capability to do anything
  975. * more than selecting one of a bunch of predefined configurations.
  976. */
  977. static ushort fifo_mode;
  978. /* "modprobe ... fifo_mode=1" etc */
  979. module_param(fifo_mode, ushort, 0);
  980. MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
  981. /*
  982. * tables defining fifo_mode values. define more if you like.
  983. * for host side, make sure both halves of ep1 are set up.
  984. */
  985. /* mode 0 - fits in 2KB */
  986. static struct musb_fifo_cfg mode_0_cfg[] = {
  987. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  988. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  989. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
  990. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  991. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  992. };
  993. /* mode 1 - fits in 4KB */
  994. static struct musb_fifo_cfg mode_1_cfg[] = {
  995. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  996. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  997. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  998. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  999. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1000. };
  1001. /* mode 2 - fits in 4KB */
  1002. static struct musb_fifo_cfg mode_2_cfg[] = {
  1003. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1004. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1005. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1006. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1007. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1008. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1009. };
  1010. /* mode 3 - fits in 4KB */
  1011. static struct musb_fifo_cfg mode_3_cfg[] = {
  1012. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1013. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1014. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1015. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1016. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1017. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1018. };
  1019. /* mode 4 - fits in 16KB */
  1020. static struct musb_fifo_cfg mode_4_cfg[] = {
  1021. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1022. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1023. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1024. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1025. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1026. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1027. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1028. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1029. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1030. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1031. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
  1032. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
  1033. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
  1034. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
  1035. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
  1036. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
  1037. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
  1038. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
  1039. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
  1040. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
  1041. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
  1042. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
  1043. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
  1044. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
  1045. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
  1046. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1047. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1048. };
  1049. /* mode 5 - fits in 8KB */
  1050. static struct musb_fifo_cfg mode_5_cfg[] = {
  1051. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1052. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1053. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1054. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1055. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1056. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1057. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1058. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1059. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1060. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1061. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
  1062. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
  1063. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
  1064. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
  1065. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
  1066. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
  1067. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
  1068. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
  1069. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
  1070. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
  1071. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
  1072. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
  1073. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
  1074. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
  1075. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
  1076. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1077. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1078. };
  1079. /*
  1080. * configure a fifo; for non-shared endpoints, this may be called
  1081. * once for a tx fifo and once for an rx fifo.
  1082. *
  1083. * returns negative errno or offset for next fifo.
  1084. */
  1085. static int
  1086. fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
  1087. const struct musb_fifo_cfg *cfg, u16 offset)
  1088. {
  1089. void __iomem *mbase = musb->mregs;
  1090. int size = 0;
  1091. u16 maxpacket = cfg->maxpacket;
  1092. u16 c_off = offset >> 3;
  1093. u8 c_size;
  1094. /* expect hw_ep has already been zero-initialized */
  1095. size = ffs(max(maxpacket, (u16) 8)) - 1;
  1096. maxpacket = 1 << size;
  1097. c_size = size - 3;
  1098. if (cfg->mode == BUF_DOUBLE) {
  1099. if ((offset + (maxpacket << 1)) >
  1100. (1 << (musb->config->ram_bits + 2)))
  1101. return -EMSGSIZE;
  1102. c_size |= MUSB_FIFOSZ_DPB;
  1103. } else {
  1104. if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
  1105. return -EMSGSIZE;
  1106. }
  1107. /* configure the FIFO */
  1108. musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
  1109. /* EP0 reserved endpoint for control, bidirectional;
  1110. * EP1 reserved for bulk, two unidirectional halves.
  1111. */
  1112. if (hw_ep->epnum == 1)
  1113. musb->bulk_ep = hw_ep;
  1114. /* REVISIT error check: be sure ep0 can both rx and tx ... */
  1115. switch (cfg->style) {
  1116. case FIFO_TX:
  1117. musb_write_txfifosz(mbase, c_size);
  1118. musb_write_txfifoadd(mbase, c_off);
  1119. hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1120. hw_ep->max_packet_sz_tx = maxpacket;
  1121. break;
  1122. case FIFO_RX:
  1123. musb_write_rxfifosz(mbase, c_size);
  1124. musb_write_rxfifoadd(mbase, c_off);
  1125. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1126. hw_ep->max_packet_sz_rx = maxpacket;
  1127. break;
  1128. case FIFO_RXTX:
  1129. musb_write_txfifosz(mbase, c_size);
  1130. musb_write_txfifoadd(mbase, c_off);
  1131. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1132. hw_ep->max_packet_sz_rx = maxpacket;
  1133. musb_write_rxfifosz(mbase, c_size);
  1134. musb_write_rxfifoadd(mbase, c_off);
  1135. hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
  1136. hw_ep->max_packet_sz_tx = maxpacket;
  1137. hw_ep->is_shared_fifo = true;
  1138. break;
  1139. }
  1140. /* NOTE rx and tx endpoint irqs aren't managed separately,
  1141. * which happens to be ok
  1142. */
  1143. musb->epmask |= (1 << hw_ep->epnum);
  1144. return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
  1145. }
  1146. static struct musb_fifo_cfg ep0_cfg = {
  1147. .style = FIFO_RXTX, .maxpacket = 64,
  1148. };
  1149. static int ep_config_from_table(struct musb *musb)
  1150. {
  1151. const struct musb_fifo_cfg *cfg;
  1152. unsigned i, n;
  1153. int offset;
  1154. struct musb_hw_ep *hw_ep = musb->endpoints;
  1155. if (musb->config->fifo_cfg) {
  1156. cfg = musb->config->fifo_cfg;
  1157. n = musb->config->fifo_cfg_size;
  1158. goto done;
  1159. }
  1160. switch (fifo_mode) {
  1161. default:
  1162. fifo_mode = 0;
  1163. /* FALLTHROUGH */
  1164. case 0:
  1165. cfg = mode_0_cfg;
  1166. n = ARRAY_SIZE(mode_0_cfg);
  1167. break;
  1168. case 1:
  1169. cfg = mode_1_cfg;
  1170. n = ARRAY_SIZE(mode_1_cfg);
  1171. break;
  1172. case 2:
  1173. cfg = mode_2_cfg;
  1174. n = ARRAY_SIZE(mode_2_cfg);
  1175. break;
  1176. case 3:
  1177. cfg = mode_3_cfg;
  1178. n = ARRAY_SIZE(mode_3_cfg);
  1179. break;
  1180. case 4:
  1181. cfg = mode_4_cfg;
  1182. n = ARRAY_SIZE(mode_4_cfg);
  1183. break;
  1184. case 5:
  1185. cfg = mode_5_cfg;
  1186. n = ARRAY_SIZE(mode_5_cfg);
  1187. break;
  1188. }
  1189. printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
  1190. musb_driver_name, fifo_mode);
  1191. done:
  1192. offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
  1193. /* assert(offset > 0) */
  1194. /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
  1195. * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
  1196. */
  1197. for (i = 0; i < n; i++) {
  1198. u8 epn = cfg->hw_ep_num;
  1199. if (epn >= musb->config->num_eps) {
  1200. pr_debug("%s: invalid ep %d\n",
  1201. musb_driver_name, epn);
  1202. return -EINVAL;
  1203. }
  1204. offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
  1205. if (offset < 0) {
  1206. pr_debug("%s: mem overrun, ep %d\n",
  1207. musb_driver_name, epn);
  1208. return offset;
  1209. }
  1210. epn++;
  1211. musb->nr_endpoints = max(epn, musb->nr_endpoints);
  1212. }
  1213. printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
  1214. musb_driver_name,
  1215. n + 1, musb->config->num_eps * 2 - 1,
  1216. offset, (1 << (musb->config->ram_bits + 2)));
  1217. if (!musb->bulk_ep) {
  1218. pr_debug("%s: missing bulk\n", musb_driver_name);
  1219. return -EINVAL;
  1220. }
  1221. return 0;
  1222. }
  1223. /*
  1224. * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
  1225. * @param musb the controller
  1226. */
  1227. static int ep_config_from_hw(struct musb *musb)
  1228. {
  1229. u8 epnum = 0;
  1230. struct musb_hw_ep *hw_ep;
  1231. void __iomem *mbase = musb->mregs;
  1232. int ret = 0;
  1233. dev_dbg(musb->controller, "<== static silicon ep config\n");
  1234. /* FIXME pick up ep0 maxpacket size */
  1235. for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
  1236. musb_ep_select(mbase, epnum);
  1237. hw_ep = musb->endpoints + epnum;
  1238. ret = musb_read_fifosize(musb, hw_ep, epnum);
  1239. if (ret < 0)
  1240. break;
  1241. /* FIXME set up hw_ep->{rx,tx}_double_buffered */
  1242. /* pick an RX/TX endpoint for bulk */
  1243. if (hw_ep->max_packet_sz_tx < 512
  1244. || hw_ep->max_packet_sz_rx < 512)
  1245. continue;
  1246. /* REVISIT: this algorithm is lazy, we should at least
  1247. * try to pick a double buffered endpoint.
  1248. */
  1249. if (musb->bulk_ep)
  1250. continue;
  1251. musb->bulk_ep = hw_ep;
  1252. }
  1253. if (!musb->bulk_ep) {
  1254. pr_debug("%s: missing bulk\n", musb_driver_name);
  1255. return -EINVAL;
  1256. }
  1257. return 0;
  1258. }
  1259. enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
  1260. /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
  1261. * configure endpoints, or take their config from silicon
  1262. */
  1263. static int musb_core_init(u16 musb_type, struct musb *musb)
  1264. {
  1265. u8 reg;
  1266. char *type;
  1267. char aInfo[90], aRevision[32], aDate[12];
  1268. void __iomem *mbase = musb->mregs;
  1269. int status = 0;
  1270. int i;
  1271. /* log core options (read using indexed model) */
  1272. reg = musb_read_configdata(mbase);
  1273. strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
  1274. if (reg & MUSB_CONFIGDATA_DYNFIFO) {
  1275. strcat(aInfo, ", dyn FIFOs");
  1276. musb->dyn_fifo = true;
  1277. }
  1278. if (reg & MUSB_CONFIGDATA_MPRXE) {
  1279. strcat(aInfo, ", bulk combine");
  1280. musb->bulk_combine = true;
  1281. }
  1282. if (reg & MUSB_CONFIGDATA_MPTXE) {
  1283. strcat(aInfo, ", bulk split");
  1284. musb->bulk_split = true;
  1285. }
  1286. if (reg & MUSB_CONFIGDATA_HBRXE) {
  1287. strcat(aInfo, ", HB-ISO Rx");
  1288. musb->hb_iso_rx = true;
  1289. }
  1290. if (reg & MUSB_CONFIGDATA_HBTXE) {
  1291. strcat(aInfo, ", HB-ISO Tx");
  1292. musb->hb_iso_tx = true;
  1293. }
  1294. if (reg & MUSB_CONFIGDATA_SOFTCONE)
  1295. strcat(aInfo, ", SoftConn");
  1296. printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
  1297. musb_driver_name, reg, aInfo);
  1298. aDate[0] = 0;
  1299. if (MUSB_CONTROLLER_MHDRC == musb_type) {
  1300. musb->is_multipoint = 1;
  1301. type = "M";
  1302. } else {
  1303. musb->is_multipoint = 0;
  1304. type = "";
  1305. #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
  1306. printk(KERN_ERR
  1307. "%s: kernel must blacklist external hubs\n",
  1308. musb_driver_name);
  1309. #endif
  1310. }
  1311. /* log release info */
  1312. musb->hwvers = musb_read_hwvers(mbase);
  1313. snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
  1314. MUSB_HWVERS_MINOR(musb->hwvers),
  1315. (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
  1316. printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
  1317. musb_driver_name, type, aRevision, aDate);
  1318. /* configure ep0 */
  1319. musb_configure_ep0(musb);
  1320. /* discover endpoint configuration */
  1321. musb->nr_endpoints = 1;
  1322. musb->epmask = 1;
  1323. if (musb->dyn_fifo)
  1324. status = ep_config_from_table(musb);
  1325. else
  1326. status = ep_config_from_hw(musb);
  1327. if (status < 0)
  1328. return status;
  1329. /* finish init, and print endpoint config */
  1330. for (i = 0; i < musb->nr_endpoints; i++) {
  1331. struct musb_hw_ep *hw_ep = musb->endpoints + i;
  1332. hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
  1333. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  1334. if (musb->io.quirks & MUSB_IN_TUSB) {
  1335. hw_ep->fifo_async = musb->async + 0x400 +
  1336. musb->io.fifo_offset(i);
  1337. hw_ep->fifo_sync = musb->sync + 0x400 +
  1338. musb->io.fifo_offset(i);
  1339. hw_ep->fifo_sync_va =
  1340. musb->sync_va + 0x400 + musb->io.fifo_offset(i);
  1341. if (i == 0)
  1342. hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
  1343. else
  1344. hw_ep->conf = mbase + 0x400 +
  1345. (((i - 1) & 0xf) << 2);
  1346. }
  1347. #endif
  1348. hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
  1349. hw_ep->rx_reinit = 1;
  1350. hw_ep->tx_reinit = 1;
  1351. if (hw_ep->max_packet_sz_tx) {
  1352. dev_dbg(musb->controller,
  1353. "%s: hw_ep %d%s, %smax %d\n",
  1354. musb_driver_name, i,
  1355. hw_ep->is_shared_fifo ? "shared" : "tx",
  1356. hw_ep->tx_double_buffered
  1357. ? "doublebuffer, " : "",
  1358. hw_ep->max_packet_sz_tx);
  1359. }
  1360. if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
  1361. dev_dbg(musb->controller,
  1362. "%s: hw_ep %d%s, %smax %d\n",
  1363. musb_driver_name, i,
  1364. "rx",
  1365. hw_ep->rx_double_buffered
  1366. ? "doublebuffer, " : "",
  1367. hw_ep->max_packet_sz_rx);
  1368. }
  1369. if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
  1370. dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
  1371. }
  1372. return 0;
  1373. }
  1374. /*-------------------------------------------------------------------------*/
  1375. /*
  1376. * handle all the irqs defined by the HDRC core. for now we expect: other
  1377. * irq sources (phy, dma, etc) will be handled first, musb->int_* values
  1378. * will be assigned, and the irq will already have been acked.
  1379. *
  1380. * called in irq context with spinlock held, irqs blocked
  1381. */
  1382. irqreturn_t musb_interrupt(struct musb *musb)
  1383. {
  1384. irqreturn_t retval = IRQ_NONE;
  1385. unsigned long status;
  1386. unsigned long epnum;
  1387. u8 devctl;
  1388. if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
  1389. return IRQ_NONE;
  1390. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1391. dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
  1392. is_host_active(musb) ? "host" : "peripheral",
  1393. musb->int_usb, musb->int_tx, musb->int_rx);
  1394. /**
  1395. * According to Mentor Graphics' documentation, flowchart on page 98,
  1396. * IRQ should be handled as follows:
  1397. *
  1398. * . Resume IRQ
  1399. * . Session Request IRQ
  1400. * . VBUS Error IRQ
  1401. * . Suspend IRQ
  1402. * . Connect IRQ
  1403. * . Disconnect IRQ
  1404. * . Reset/Babble IRQ
  1405. * . SOF IRQ (we're not using this one)
  1406. * . Endpoint 0 IRQ
  1407. * . TX Endpoints
  1408. * . RX Endpoints
  1409. *
  1410. * We will be following that flowchart in order to avoid any problems
  1411. * that might arise with internal Finite State Machine.
  1412. */
  1413. if (musb->int_usb)
  1414. retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
  1415. if (musb->int_tx & 1) {
  1416. if (is_host_active(musb))
  1417. retval |= musb_h_ep0_irq(musb);
  1418. else
  1419. retval |= musb_g_ep0_irq(musb);
  1420. /* we have just handled endpoint 0 IRQ, clear it */
  1421. musb->int_tx &= ~BIT(0);
  1422. }
  1423. status = musb->int_tx;
  1424. for_each_set_bit(epnum, &status, 16) {
  1425. retval = IRQ_HANDLED;
  1426. if (is_host_active(musb))
  1427. musb_host_tx(musb, epnum);
  1428. else
  1429. musb_g_tx(musb, epnum);
  1430. }
  1431. status = musb->int_rx;
  1432. for_each_set_bit(epnum, &status, 16) {
  1433. retval = IRQ_HANDLED;
  1434. if (is_host_active(musb))
  1435. musb_host_rx(musb, epnum);
  1436. else
  1437. musb_g_rx(musb, epnum);
  1438. }
  1439. return retval;
  1440. }
  1441. EXPORT_SYMBOL_GPL(musb_interrupt);
  1442. #ifndef CONFIG_MUSB_PIO_ONLY
  1443. static bool use_dma = 1;
  1444. /* "modprobe ... use_dma=0" etc */
  1445. module_param(use_dma, bool, 0);
  1446. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  1447. void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
  1448. {
  1449. /* called with controller lock already held */
  1450. if (!epnum) {
  1451. if (!is_cppi_enabled(musb)) {
  1452. /* endpoint 0 */
  1453. if (is_host_active(musb))
  1454. musb_h_ep0_irq(musb);
  1455. else
  1456. musb_g_ep0_irq(musb);
  1457. }
  1458. } else {
  1459. /* endpoints 1..15 */
  1460. if (transmit) {
  1461. if (is_host_active(musb))
  1462. musb_host_tx(musb, epnum);
  1463. else
  1464. musb_g_tx(musb, epnum);
  1465. } else {
  1466. /* receive */
  1467. if (is_host_active(musb))
  1468. musb_host_rx(musb, epnum);
  1469. else
  1470. musb_g_rx(musb, epnum);
  1471. }
  1472. }
  1473. }
  1474. EXPORT_SYMBOL_GPL(musb_dma_completion);
  1475. #else
  1476. #define use_dma 0
  1477. #endif
  1478. /*-------------------------------------------------------------------------*/
  1479. static ssize_t
  1480. musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1481. {
  1482. struct musb *musb = dev_to_musb(dev);
  1483. unsigned long flags;
  1484. int ret = -EINVAL;
  1485. spin_lock_irqsave(&musb->lock, flags);
  1486. ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->otg->state));
  1487. spin_unlock_irqrestore(&musb->lock, flags);
  1488. return ret;
  1489. }
  1490. static ssize_t
  1491. musb_mode_store(struct device *dev, struct device_attribute *attr,
  1492. const char *buf, size_t n)
  1493. {
  1494. struct musb *musb = dev_to_musb(dev);
  1495. unsigned long flags;
  1496. int status;
  1497. spin_lock_irqsave(&musb->lock, flags);
  1498. if (sysfs_streq(buf, "host"))
  1499. status = musb_platform_set_mode(musb, MUSB_HOST);
  1500. else if (sysfs_streq(buf, "peripheral"))
  1501. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1502. else if (sysfs_streq(buf, "otg"))
  1503. status = musb_platform_set_mode(musb, MUSB_OTG);
  1504. else
  1505. status = -EINVAL;
  1506. spin_unlock_irqrestore(&musb->lock, flags);
  1507. return (status == 0) ? n : status;
  1508. }
  1509. static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
  1510. static ssize_t
  1511. musb_vbus_store(struct device *dev, struct device_attribute *attr,
  1512. const char *buf, size_t n)
  1513. {
  1514. struct musb *musb = dev_to_musb(dev);
  1515. unsigned long flags;
  1516. unsigned long val;
  1517. if (sscanf(buf, "%lu", &val) < 1) {
  1518. dev_err(dev, "Invalid VBUS timeout ms value\n");
  1519. return -EINVAL;
  1520. }
  1521. spin_lock_irqsave(&musb->lock, flags);
  1522. /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
  1523. musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
  1524. if (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)
  1525. musb->is_active = 0;
  1526. musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
  1527. spin_unlock_irqrestore(&musb->lock, flags);
  1528. return n;
  1529. }
  1530. static ssize_t
  1531. musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
  1532. {
  1533. struct musb *musb = dev_to_musb(dev);
  1534. unsigned long flags;
  1535. unsigned long val;
  1536. int vbus;
  1537. spin_lock_irqsave(&musb->lock, flags);
  1538. val = musb->a_wait_bcon;
  1539. /* FIXME get_vbus_status() is normally #defined as false...
  1540. * and is effectively TUSB-specific.
  1541. */
  1542. vbus = musb_platform_get_vbus_status(musb);
  1543. spin_unlock_irqrestore(&musb->lock, flags);
  1544. return sprintf(buf, "Vbus %s, timeout %lu msec\n",
  1545. vbus ? "on" : "off", val);
  1546. }
  1547. static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
  1548. /* Gadget drivers can't know that a host is connected so they might want
  1549. * to start SRP, but users can. This allows userspace to trigger SRP.
  1550. */
  1551. static ssize_t
  1552. musb_srp_store(struct device *dev, struct device_attribute *attr,
  1553. const char *buf, size_t n)
  1554. {
  1555. struct musb *musb = dev_to_musb(dev);
  1556. unsigned short srp;
  1557. if (sscanf(buf, "%hu", &srp) != 1
  1558. || (srp != 1)) {
  1559. dev_err(dev, "SRP: Value must be 1\n");
  1560. return -EINVAL;
  1561. }
  1562. if (srp == 1)
  1563. musb_g_wakeup(musb);
  1564. return n;
  1565. }
  1566. static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
  1567. static struct attribute *musb_attributes[] = {
  1568. &dev_attr_mode.attr,
  1569. &dev_attr_vbus.attr,
  1570. &dev_attr_srp.attr,
  1571. NULL
  1572. };
  1573. static const struct attribute_group musb_attr_group = {
  1574. .attrs = musb_attributes,
  1575. };
  1576. /* Only used to provide driver mode change events */
  1577. static void musb_irq_work(struct work_struct *data)
  1578. {
  1579. struct musb *musb = container_of(data, struct musb, irq_work);
  1580. if (musb->xceiv->otg->state != musb->xceiv_old_state) {
  1581. musb->xceiv_old_state = musb->xceiv->otg->state;
  1582. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  1583. }
  1584. }
  1585. static void musb_recover_from_babble(struct musb *musb)
  1586. {
  1587. int ret;
  1588. u8 devctl;
  1589. musb_disable_interrupts(musb);
  1590. /*
  1591. * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
  1592. * it some slack and wait for 10us.
  1593. */
  1594. udelay(10);
  1595. ret = musb_platform_recover(musb);
  1596. if (ret) {
  1597. musb_enable_interrupts(musb);
  1598. return;
  1599. }
  1600. /* drop session bit */
  1601. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1602. devctl &= ~MUSB_DEVCTL_SESSION;
  1603. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  1604. /* tell usbcore about it */
  1605. musb_root_disconnect(musb);
  1606. /*
  1607. * When a babble condition occurs, the musb controller
  1608. * removes the session bit and the endpoint config is lost.
  1609. */
  1610. if (musb->dyn_fifo)
  1611. ret = ep_config_from_table(musb);
  1612. else
  1613. ret = ep_config_from_hw(musb);
  1614. /* restart session */
  1615. if (ret == 0)
  1616. musb_start(musb);
  1617. }
  1618. /* --------------------------------------------------------------------------
  1619. * Init support
  1620. */
  1621. static struct musb *allocate_instance(struct device *dev,
  1622. struct musb_hdrc_config *config, void __iomem *mbase)
  1623. {
  1624. struct musb *musb;
  1625. struct musb_hw_ep *ep;
  1626. int epnum;
  1627. int ret;
  1628. musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
  1629. if (!musb)
  1630. return NULL;
  1631. INIT_LIST_HEAD(&musb->control);
  1632. INIT_LIST_HEAD(&musb->in_bulk);
  1633. INIT_LIST_HEAD(&musb->out_bulk);
  1634. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  1635. musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
  1636. musb->mregs = mbase;
  1637. musb->ctrl_base = mbase;
  1638. musb->nIrq = -ENODEV;
  1639. musb->config = config;
  1640. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  1641. for (epnum = 0, ep = musb->endpoints;
  1642. epnum < musb->config->num_eps;
  1643. epnum++, ep++) {
  1644. ep->musb = musb;
  1645. ep->epnum = epnum;
  1646. }
  1647. musb->controller = dev;
  1648. ret = musb_host_alloc(musb);
  1649. if (ret < 0)
  1650. goto err_free;
  1651. dev_set_drvdata(dev, musb);
  1652. return musb;
  1653. err_free:
  1654. return NULL;
  1655. }
  1656. static void musb_free(struct musb *musb)
  1657. {
  1658. /* this has multiple entry modes. it handles fault cleanup after
  1659. * probe(), where things may be partially set up, as well as rmmod
  1660. * cleanup after everything's been de-activated.
  1661. */
  1662. #ifdef CONFIG_SYSFS
  1663. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  1664. #endif
  1665. if (musb->nIrq >= 0) {
  1666. if (musb->irq_wake)
  1667. disable_irq_wake(musb->nIrq);
  1668. free_irq(musb->nIrq, musb);
  1669. }
  1670. musb_host_free(musb);
  1671. }
  1672. static void musb_deassert_reset(struct work_struct *work)
  1673. {
  1674. struct musb *musb;
  1675. unsigned long flags;
  1676. musb = container_of(work, struct musb, deassert_reset_work.work);
  1677. spin_lock_irqsave(&musb->lock, flags);
  1678. if (musb->port1_status & USB_PORT_STAT_RESET)
  1679. musb_port_reset(musb, false);
  1680. spin_unlock_irqrestore(&musb->lock, flags);
  1681. }
  1682. /*
  1683. * Perform generic per-controller initialization.
  1684. *
  1685. * @dev: the controller (already clocked, etc)
  1686. * @nIrq: IRQ number
  1687. * @ctrl: virtual address of controller registers,
  1688. * not yet corrected for platform-specific offsets
  1689. */
  1690. static int
  1691. musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  1692. {
  1693. int status;
  1694. struct musb *musb;
  1695. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  1696. /* The driver might handle more features than the board; OK.
  1697. * Fail when the board needs a feature that's not enabled.
  1698. */
  1699. if (!plat) {
  1700. dev_dbg(dev, "no platform_data?\n");
  1701. status = -ENODEV;
  1702. goto fail0;
  1703. }
  1704. /* allocate */
  1705. musb = allocate_instance(dev, plat->config, ctrl);
  1706. if (!musb) {
  1707. status = -ENOMEM;
  1708. goto fail0;
  1709. }
  1710. spin_lock_init(&musb->lock);
  1711. musb->board_set_power = plat->set_power;
  1712. musb->min_power = plat->min_power;
  1713. musb->ops = plat->platform_ops;
  1714. musb->port_mode = plat->mode;
  1715. /*
  1716. * Initialize the default IO functions. At least omap2430 needs
  1717. * these early. We initialize the platform specific IO functions
  1718. * later on.
  1719. */
  1720. musb_readb = musb_default_readb;
  1721. musb_writeb = musb_default_writeb;
  1722. musb_readw = musb_default_readw;
  1723. musb_writew = musb_default_writew;
  1724. musb_readl = musb_default_readl;
  1725. musb_writel = musb_default_writel;
  1726. /* We need musb_read/write functions initialized for PM */
  1727. pm_runtime_use_autosuspend(musb->controller);
  1728. pm_runtime_set_autosuspend_delay(musb->controller, 200);
  1729. pm_runtime_irq_safe(musb->controller);
  1730. pm_runtime_enable(musb->controller);
  1731. /* The musb_platform_init() call:
  1732. * - adjusts musb->mregs
  1733. * - sets the musb->isr
  1734. * - may initialize an integrated transceiver
  1735. * - initializes musb->xceiv, usually by otg_get_phy()
  1736. * - stops powering VBUS
  1737. *
  1738. * There are various transceiver configurations. Blackfin,
  1739. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  1740. * external/discrete ones in various flavors (twl4030 family,
  1741. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  1742. */
  1743. status = musb_platform_init(musb);
  1744. if (status < 0)
  1745. goto fail1;
  1746. if (!musb->isr) {
  1747. status = -ENODEV;
  1748. goto fail2;
  1749. }
  1750. if (musb->ops->quirks)
  1751. musb->io.quirks = musb->ops->quirks;
  1752. /* Most devices use indexed offset or flat offset */
  1753. if (musb->io.quirks & MUSB_INDEXED_EP) {
  1754. musb->io.ep_offset = musb_indexed_ep_offset;
  1755. musb->io.ep_select = musb_indexed_ep_select;
  1756. } else {
  1757. musb->io.ep_offset = musb_flat_ep_offset;
  1758. musb->io.ep_select = musb_flat_ep_select;
  1759. }
  1760. /* And override them with platform specific ops if specified. */
  1761. if (musb->ops->ep_offset)
  1762. musb->io.ep_offset = musb->ops->ep_offset;
  1763. if (musb->ops->ep_select)
  1764. musb->io.ep_select = musb->ops->ep_select;
  1765. /* At least tusb6010 has its own offsets */
  1766. if (musb->ops->ep_offset)
  1767. musb->io.ep_offset = musb->ops->ep_offset;
  1768. if (musb->ops->ep_select)
  1769. musb->io.ep_select = musb->ops->ep_select;
  1770. if (musb->ops->fifo_mode)
  1771. fifo_mode = musb->ops->fifo_mode;
  1772. else
  1773. fifo_mode = 4;
  1774. if (musb->ops->fifo_offset)
  1775. musb->io.fifo_offset = musb->ops->fifo_offset;
  1776. else
  1777. musb->io.fifo_offset = musb_default_fifo_offset;
  1778. if (musb->ops->busctl_offset)
  1779. musb->io.busctl_offset = musb->ops->busctl_offset;
  1780. else
  1781. musb->io.busctl_offset = musb_default_busctl_offset;
  1782. if (musb->ops->readb)
  1783. musb_readb = musb->ops->readb;
  1784. if (musb->ops->writeb)
  1785. musb_writeb = musb->ops->writeb;
  1786. if (musb->ops->readw)
  1787. musb_readw = musb->ops->readw;
  1788. if (musb->ops->writew)
  1789. musb_writew = musb->ops->writew;
  1790. if (musb->ops->readl)
  1791. musb_readl = musb->ops->readl;
  1792. if (musb->ops->writel)
  1793. musb_writel = musb->ops->writel;
  1794. #ifndef CONFIG_MUSB_PIO_ONLY
  1795. if (!musb->ops->dma_init || !musb->ops->dma_exit) {
  1796. dev_err(dev, "DMA controller not set\n");
  1797. goto fail2;
  1798. }
  1799. musb_dma_controller_create = musb->ops->dma_init;
  1800. musb_dma_controller_destroy = musb->ops->dma_exit;
  1801. #endif
  1802. if (musb->ops->read_fifo)
  1803. musb->io.read_fifo = musb->ops->read_fifo;
  1804. else
  1805. musb->io.read_fifo = musb_default_read_fifo;
  1806. if (musb->ops->write_fifo)
  1807. musb->io.write_fifo = musb->ops->write_fifo;
  1808. else
  1809. musb->io.write_fifo = musb_default_write_fifo;
  1810. if (!musb->xceiv->io_ops) {
  1811. musb->xceiv->io_dev = musb->controller;
  1812. musb->xceiv->io_priv = musb->mregs;
  1813. musb->xceiv->io_ops = &musb_ulpi_access;
  1814. }
  1815. pm_runtime_get_sync(musb->controller);
  1816. if (use_dma && dev->dma_mask) {
  1817. musb->dma_controller =
  1818. musb_dma_controller_create(musb, musb->mregs);
  1819. if (IS_ERR(musb->dma_controller)) {
  1820. status = PTR_ERR(musb->dma_controller);
  1821. goto fail2_5;
  1822. }
  1823. }
  1824. /* be sure interrupts are disabled before connecting ISR */
  1825. musb_platform_disable(musb);
  1826. musb_generic_disable(musb);
  1827. /* Init IRQ workqueue before request_irq */
  1828. INIT_WORK(&musb->irq_work, musb_irq_work);
  1829. INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
  1830. INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
  1831. /* setup musb parts of the core (especially endpoints) */
  1832. status = musb_core_init(plat->config->multipoint
  1833. ? MUSB_CONTROLLER_MHDRC
  1834. : MUSB_CONTROLLER_HDRC, musb);
  1835. if (status < 0)
  1836. goto fail3;
  1837. setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
  1838. /* attach to the IRQ */
  1839. if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
  1840. dev_err(dev, "request_irq %d failed!\n", nIrq);
  1841. status = -ENODEV;
  1842. goto fail3;
  1843. }
  1844. musb->nIrq = nIrq;
  1845. /* FIXME this handles wakeup irqs wrong */
  1846. if (enable_irq_wake(nIrq) == 0) {
  1847. musb->irq_wake = 1;
  1848. device_init_wakeup(dev, 1);
  1849. } else {
  1850. musb->irq_wake = 0;
  1851. }
  1852. /* program PHY to use external vBus if required */
  1853. if (plat->extvbus) {
  1854. u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
  1855. busctl |= MUSB_ULPI_USE_EXTVBUS;
  1856. musb_write_ulpi_buscontrol(musb->mregs, busctl);
  1857. }
  1858. if (musb->xceiv->otg->default_a) {
  1859. MUSB_HST_MODE(musb);
  1860. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  1861. } else {
  1862. MUSB_DEV_MODE(musb);
  1863. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  1864. }
  1865. switch (musb->port_mode) {
  1866. case MUSB_PORT_MODE_HOST:
  1867. status = musb_host_setup(musb, plat->power);
  1868. if (status < 0)
  1869. goto fail3;
  1870. status = musb_platform_set_mode(musb, MUSB_HOST);
  1871. break;
  1872. case MUSB_PORT_MODE_GADGET:
  1873. status = musb_gadget_setup(musb);
  1874. if (status < 0)
  1875. goto fail3;
  1876. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1877. break;
  1878. case MUSB_PORT_MODE_DUAL_ROLE:
  1879. status = musb_host_setup(musb, plat->power);
  1880. if (status < 0)
  1881. goto fail3;
  1882. status = musb_gadget_setup(musb);
  1883. if (status) {
  1884. musb_host_cleanup(musb);
  1885. goto fail3;
  1886. }
  1887. status = musb_platform_set_mode(musb, MUSB_OTG);
  1888. break;
  1889. default:
  1890. dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
  1891. break;
  1892. }
  1893. if (status < 0)
  1894. goto fail3;
  1895. status = musb_init_debugfs(musb);
  1896. if (status < 0)
  1897. goto fail4;
  1898. status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  1899. if (status)
  1900. goto fail5;
  1901. pm_runtime_put(musb->controller);
  1902. return 0;
  1903. fail5:
  1904. musb_exit_debugfs(musb);
  1905. fail4:
  1906. musb_gadget_cleanup(musb);
  1907. musb_host_cleanup(musb);
  1908. fail3:
  1909. cancel_work_sync(&musb->irq_work);
  1910. cancel_delayed_work_sync(&musb->finish_resume_work);
  1911. cancel_delayed_work_sync(&musb->deassert_reset_work);
  1912. if (musb->dma_controller)
  1913. musb_dma_controller_destroy(musb->dma_controller);
  1914. fail2_5:
  1915. pm_runtime_put_sync(musb->controller);
  1916. fail2:
  1917. if (musb->irq_wake)
  1918. device_init_wakeup(dev, 0);
  1919. musb_platform_exit(musb);
  1920. fail1:
  1921. pm_runtime_disable(musb->controller);
  1922. dev_err(musb->controller,
  1923. "musb_init_controller failed with status %d\n", status);
  1924. musb_free(musb);
  1925. fail0:
  1926. return status;
  1927. }
  1928. /*-------------------------------------------------------------------------*/
  1929. /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  1930. * bridge to a platform device; this driver then suffices.
  1931. */
  1932. static int musb_probe(struct platform_device *pdev)
  1933. {
  1934. struct device *dev = &pdev->dev;
  1935. int irq = platform_get_irq_byname(pdev, "mc");
  1936. struct resource *iomem;
  1937. void __iomem *base;
  1938. if (irq <= 0)
  1939. return -ENODEV;
  1940. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1941. base = devm_ioremap_resource(dev, iomem);
  1942. if (IS_ERR(base))
  1943. return PTR_ERR(base);
  1944. return musb_init_controller(dev, irq, base);
  1945. }
  1946. static int musb_remove(struct platform_device *pdev)
  1947. {
  1948. struct device *dev = &pdev->dev;
  1949. struct musb *musb = dev_to_musb(dev);
  1950. /* this gets called on rmmod.
  1951. * - Host mode: host may still be active
  1952. * - Peripheral mode: peripheral is deactivated (or never-activated)
  1953. * - OTG mode: both roles are deactivated (or never-activated)
  1954. */
  1955. musb_exit_debugfs(musb);
  1956. musb_shutdown(pdev);
  1957. if (musb->dma_controller)
  1958. musb_dma_controller_destroy(musb->dma_controller);
  1959. cancel_work_sync(&musb->irq_work);
  1960. cancel_delayed_work_sync(&musb->finish_resume_work);
  1961. cancel_delayed_work_sync(&musb->deassert_reset_work);
  1962. musb_free(musb);
  1963. device_init_wakeup(dev, 0);
  1964. return 0;
  1965. }
  1966. #ifdef CONFIG_PM
  1967. static void musb_save_context(struct musb *musb)
  1968. {
  1969. int i;
  1970. void __iomem *musb_base = musb->mregs;
  1971. void __iomem *epio;
  1972. musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
  1973. musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
  1974. musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
  1975. musb->context.power = musb_readb(musb_base, MUSB_POWER);
  1976. musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
  1977. musb->context.index = musb_readb(musb_base, MUSB_INDEX);
  1978. musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
  1979. for (i = 0; i < musb->config->num_eps; ++i) {
  1980. struct musb_hw_ep *hw_ep;
  1981. hw_ep = &musb->endpoints[i];
  1982. if (!hw_ep)
  1983. continue;
  1984. epio = hw_ep->regs;
  1985. if (!epio)
  1986. continue;
  1987. musb_writeb(musb_base, MUSB_INDEX, i);
  1988. musb->context.index_regs[i].txmaxp =
  1989. musb_readw(epio, MUSB_TXMAXP);
  1990. musb->context.index_regs[i].txcsr =
  1991. musb_readw(epio, MUSB_TXCSR);
  1992. musb->context.index_regs[i].rxmaxp =
  1993. musb_readw(epio, MUSB_RXMAXP);
  1994. musb->context.index_regs[i].rxcsr =
  1995. musb_readw(epio, MUSB_RXCSR);
  1996. if (musb->dyn_fifo) {
  1997. musb->context.index_regs[i].txfifoadd =
  1998. musb_read_txfifoadd(musb_base);
  1999. musb->context.index_regs[i].rxfifoadd =
  2000. musb_read_rxfifoadd(musb_base);
  2001. musb->context.index_regs[i].txfifosz =
  2002. musb_read_txfifosz(musb_base);
  2003. musb->context.index_regs[i].rxfifosz =
  2004. musb_read_rxfifosz(musb_base);
  2005. }
  2006. musb->context.index_regs[i].txtype =
  2007. musb_readb(epio, MUSB_TXTYPE);
  2008. musb->context.index_regs[i].txinterval =
  2009. musb_readb(epio, MUSB_TXINTERVAL);
  2010. musb->context.index_regs[i].rxtype =
  2011. musb_readb(epio, MUSB_RXTYPE);
  2012. musb->context.index_regs[i].rxinterval =
  2013. musb_readb(epio, MUSB_RXINTERVAL);
  2014. musb->context.index_regs[i].txfunaddr =
  2015. musb_read_txfunaddr(musb, i);
  2016. musb->context.index_regs[i].txhubaddr =
  2017. musb_read_txhubaddr(musb, i);
  2018. musb->context.index_regs[i].txhubport =
  2019. musb_read_txhubport(musb, i);
  2020. musb->context.index_regs[i].rxfunaddr =
  2021. musb_read_rxfunaddr(musb, i);
  2022. musb->context.index_regs[i].rxhubaddr =
  2023. musb_read_rxhubaddr(musb, i);
  2024. musb->context.index_regs[i].rxhubport =
  2025. musb_read_rxhubport(musb, i);
  2026. }
  2027. }
  2028. static void musb_restore_context(struct musb *musb)
  2029. {
  2030. int i;
  2031. void __iomem *musb_base = musb->mregs;
  2032. void __iomem *epio;
  2033. u8 power;
  2034. musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
  2035. musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
  2036. musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
  2037. /* Don't affect SUSPENDM/RESUME bits in POWER reg */
  2038. power = musb_readb(musb_base, MUSB_POWER);
  2039. power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
  2040. musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
  2041. power |= musb->context.power;
  2042. musb_writeb(musb_base, MUSB_POWER, power);
  2043. musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
  2044. musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
  2045. musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
  2046. musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
  2047. for (i = 0; i < musb->config->num_eps; ++i) {
  2048. struct musb_hw_ep *hw_ep;
  2049. hw_ep = &musb->endpoints[i];
  2050. if (!hw_ep)
  2051. continue;
  2052. epio = hw_ep->regs;
  2053. if (!epio)
  2054. continue;
  2055. musb_writeb(musb_base, MUSB_INDEX, i);
  2056. musb_writew(epio, MUSB_TXMAXP,
  2057. musb->context.index_regs[i].txmaxp);
  2058. musb_writew(epio, MUSB_TXCSR,
  2059. musb->context.index_regs[i].txcsr);
  2060. musb_writew(epio, MUSB_RXMAXP,
  2061. musb->context.index_regs[i].rxmaxp);
  2062. musb_writew(epio, MUSB_RXCSR,
  2063. musb->context.index_regs[i].rxcsr);
  2064. if (musb->dyn_fifo) {
  2065. musb_write_txfifosz(musb_base,
  2066. musb->context.index_regs[i].txfifosz);
  2067. musb_write_rxfifosz(musb_base,
  2068. musb->context.index_regs[i].rxfifosz);
  2069. musb_write_txfifoadd(musb_base,
  2070. musb->context.index_regs[i].txfifoadd);
  2071. musb_write_rxfifoadd(musb_base,
  2072. musb->context.index_regs[i].rxfifoadd);
  2073. }
  2074. musb_writeb(epio, MUSB_TXTYPE,
  2075. musb->context.index_regs[i].txtype);
  2076. musb_writeb(epio, MUSB_TXINTERVAL,
  2077. musb->context.index_regs[i].txinterval);
  2078. musb_writeb(epio, MUSB_RXTYPE,
  2079. musb->context.index_regs[i].rxtype);
  2080. musb_writeb(epio, MUSB_RXINTERVAL,
  2081. musb->context.index_regs[i].rxinterval);
  2082. musb_write_txfunaddr(musb, i,
  2083. musb->context.index_regs[i].txfunaddr);
  2084. musb_write_txhubaddr(musb, i,
  2085. musb->context.index_regs[i].txhubaddr);
  2086. musb_write_txhubport(musb, i,
  2087. musb->context.index_regs[i].txhubport);
  2088. musb_write_rxfunaddr(musb, i,
  2089. musb->context.index_regs[i].rxfunaddr);
  2090. musb_write_rxhubaddr(musb, i,
  2091. musb->context.index_regs[i].rxhubaddr);
  2092. musb_write_rxhubport(musb, i,
  2093. musb->context.index_regs[i].rxhubport);
  2094. }
  2095. musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
  2096. }
  2097. static int musb_suspend(struct device *dev)
  2098. {
  2099. struct musb *musb = dev_to_musb(dev);
  2100. unsigned long flags;
  2101. musb_platform_disable(musb);
  2102. musb_generic_disable(musb);
  2103. spin_lock_irqsave(&musb->lock, flags);
  2104. if (is_peripheral_active(musb)) {
  2105. /* FIXME force disconnect unless we know USB will wake
  2106. * the system up quickly enough to respond ...
  2107. */
  2108. } else if (is_host_active(musb)) {
  2109. /* we know all the children are suspended; sometimes
  2110. * they will even be wakeup-enabled.
  2111. */
  2112. }
  2113. musb_save_context(musb);
  2114. spin_unlock_irqrestore(&musb->lock, flags);
  2115. return 0;
  2116. }
  2117. static int musb_resume(struct device *dev)
  2118. {
  2119. struct musb *musb = dev_to_musb(dev);
  2120. u8 devctl;
  2121. u8 mask;
  2122. /*
  2123. * For static cmos like DaVinci, register values were preserved
  2124. * unless for some reason the whole soc powered down or the USB
  2125. * module got reset through the PSC (vs just being disabled).
  2126. *
  2127. * For the DSPS glue layer though, a full register restore has to
  2128. * be done. As it shouldn't harm other platforms, we do it
  2129. * unconditionally.
  2130. */
  2131. musb_restore_context(musb);
  2132. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2133. mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
  2134. if ((devctl & mask) != (musb->context.devctl & mask))
  2135. musb->port1_status = 0;
  2136. if (musb->need_finish_resume) {
  2137. musb->need_finish_resume = 0;
  2138. schedule_delayed_work(&musb->finish_resume_work,
  2139. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  2140. }
  2141. /*
  2142. * The USB HUB code expects the device to be in RPM_ACTIVE once it came
  2143. * out of suspend
  2144. */
  2145. pm_runtime_disable(dev);
  2146. pm_runtime_set_active(dev);
  2147. pm_runtime_enable(dev);
  2148. musb_start(musb);
  2149. return 0;
  2150. }
  2151. static int musb_runtime_suspend(struct device *dev)
  2152. {
  2153. struct musb *musb = dev_to_musb(dev);
  2154. musb_save_context(musb);
  2155. return 0;
  2156. }
  2157. static int musb_runtime_resume(struct device *dev)
  2158. {
  2159. struct musb *musb = dev_to_musb(dev);
  2160. static int first = 1;
  2161. /*
  2162. * When pm_runtime_get_sync called for the first time in driver
  2163. * init, some of the structure is still not initialized which is
  2164. * used in restore function. But clock needs to be
  2165. * enabled before any register access, so
  2166. * pm_runtime_get_sync has to be called.
  2167. * Also context restore without save does not make
  2168. * any sense
  2169. */
  2170. if (!first)
  2171. musb_restore_context(musb);
  2172. first = 0;
  2173. if (musb->need_finish_resume) {
  2174. musb->need_finish_resume = 0;
  2175. schedule_delayed_work(&musb->finish_resume_work,
  2176. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  2177. }
  2178. return 0;
  2179. }
  2180. static const struct dev_pm_ops musb_dev_pm_ops = {
  2181. .suspend = musb_suspend,
  2182. .resume = musb_resume,
  2183. .runtime_suspend = musb_runtime_suspend,
  2184. .runtime_resume = musb_runtime_resume,
  2185. };
  2186. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2187. #else
  2188. #define MUSB_DEV_PM_OPS NULL
  2189. #endif
  2190. static struct platform_driver musb_driver = {
  2191. .driver = {
  2192. .name = (char *)musb_driver_name,
  2193. .bus = &platform_bus_type,
  2194. .pm = MUSB_DEV_PM_OPS,
  2195. },
  2196. .probe = musb_probe,
  2197. .remove = musb_remove,
  2198. .shutdown = musb_shutdown,
  2199. };
  2200. module_platform_driver(musb_driver);