musb_core.c 71 KB

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