musb_core.c 75 KB

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