musb_core.c 75 KB

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