musb_core.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  19. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. /*
  29. * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
  30. *
  31. * This consists of a Host Controller Driver (HCD) and a peripheral
  32. * controller driver implementing the "Gadget" API; OTG support is
  33. * in the works. These are normal Linux-USB controller drivers which
  34. * use IRQs and have no dedicated thread.
  35. *
  36. * This version of the driver has only been used with products from
  37. * Texas Instruments. Those products integrate the Inventra logic
  38. * with other DMA, IRQ, and bus modules, as well as other logic that
  39. * needs to be reflected in this driver.
  40. *
  41. *
  42. * NOTE: the original Mentor code here was pretty much a collection
  43. * of mechanisms that don't seem to have been fully integrated/working
  44. * for *any* Linux kernel version. This version aims at Linux 2.6.now,
  45. * Key open issues include:
  46. *
  47. * - Lack of host-side transaction scheduling, for all transfer types.
  48. * The hardware doesn't do it; instead, software must.
  49. *
  50. * This is not an issue for OTG devices that don't support external
  51. * hubs, but for more "normal" USB hosts it's a user issue that the
  52. * "multipoint" support doesn't scale in the expected ways. That
  53. * includes DaVinci EVM in a common non-OTG mode.
  54. *
  55. * * Control and bulk use dedicated endpoints, and there's as
  56. * yet no mechanism to either (a) reclaim the hardware when
  57. * peripherals are NAKing, which gets complicated with bulk
  58. * endpoints, or (b) use more than a single bulk endpoint in
  59. * each direction.
  60. *
  61. * RESULT: one device may be perceived as blocking another one.
  62. *
  63. * * Interrupt and isochronous will dynamically allocate endpoint
  64. * hardware, but (a) there's no record keeping for bandwidth;
  65. * (b) in the common case that few endpoints are available, there
  66. * is no mechanism to reuse endpoints to talk to multiple devices.
  67. *
  68. * RESULT: At one extreme, bandwidth can be overcommitted in
  69. * some hardware configurations, no faults will be reported.
  70. * At the other extreme, the bandwidth capabilities which do
  71. * exist tend to be severely undercommitted. You can't yet hook
  72. * up both a keyboard and a mouse to an external USB hub.
  73. */
  74. /* Sanity CR check in */
  75. /*
  76. * This gets many kinds of configuration information:
  77. * - Kconfig for everything user-configurable
  78. * - platform_device for addressing, irq, and platform_data
  79. * - platform_data is mostly for board-specific informarion
  80. * (plus recentrly, SOC or family details)
  81. *
  82. * Most of the conditional compilation will (someday) vanish.
  83. */
  84. #include <linux/module.h>
  85. #include <linux/kernel.h>
  86. #include <linux/sched.h>
  87. #include <linux/slab.h>
  88. #include <linux/init.h>
  89. #include <linux/list.h>
  90. #include <linux/kobject.h>
  91. #include <linux/prefetch.h>
  92. #include <linux/platform_device.h>
  93. #include <linux/io.h>
  94. #include <linux/wakelock.h>
  95. #include <mu3phy/mtk-phy.h>
  96. #include <mu3phy/mtk-phy-asic.h> /* for fc_iddig, rm later */
  97. #include "musb_core.h"
  98. /* #include "mu3d_hal_osal.h" */
  99. #include "mu3d_hal_usb_drv.h"
  100. #include "mu3d_hal_hw.h"
  101. #include "mu3d_hal_qmu_drv.h"
  102. #include "ssusb_io.h"
  103. /* #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON) */
  104. #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
  105. #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
  106. #define MUSB_VERSION "6.0"
  107. #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
  108. const char musb_driver_name[] = MUSB_DRIVER_NAME;
  109. struct musb *_mu3d_musb;
  110. u32 debug_level = K_ALET | K_CRIT | K_ERR | K_WARNIN | K_NOTICE;
  111. module_param(debug_level, int, 0644);
  112. MODULE_PARM_DESC(debug_level, "Debug Print Log Lvl");
  113. MODULE_DESCRIPTION(DRIVER_INFO);
  114. MODULE_AUTHOR(DRIVER_AUTHOR);
  115. MODULE_LICENSE("GPL");
  116. MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
  117. #define U3D_FIFO_START_ADDRESS 0
  118. static void print_buf(char *verb, const char *buf, int len)
  119. {
  120. }
  121. /*
  122. * Load an endpoint's FIFO
  123. */
  124. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  125. {
  126. unsigned int residue;
  127. unsigned int temp;
  128. void __iomem *fifo = hw_ep->fifo; /* (void __iomem *)USB_FIFO(hw_ep->epnum); */
  129. mu3d_dbg(K_DEBUG, "%s epnum=%d, len=%d, buf=%p\n", __func__, hw_ep->epnum, len, src);
  130. residue = len;
  131. print_buf("write-fifo", src, len);
  132. while (residue > 0) {
  133. if (residue == 1) {
  134. temp = ((*src) & 0xFF);
  135. writeb(temp, fifo);
  136. src += 1;
  137. residue -= 1;
  138. } else if (residue == 2) {
  139. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00);
  140. writew(temp, fifo);
  141. src += 2;
  142. residue -= 2;
  143. } else if (residue == 3) {
  144. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00);
  145. writew(temp, fifo);
  146. src += 2;
  147. temp = ((*src) & 0xFF);
  148. writeb(temp, fifo);
  149. src += 1;
  150. residue -= 3;
  151. } else {
  152. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00) +
  153. (((*(src + 2)) << 16) & 0xFF0000) + (((*(src + 3)) << 24) & 0xFF000000);
  154. writel(temp, fifo);
  155. src += 4;
  156. residue -= 4;
  157. }
  158. }
  159. }
  160. /*
  161. * Unload an endpoint's FIFO
  162. */
  163. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  164. {
  165. u16 residue;
  166. unsigned int temp;
  167. u8 *dst_bg = dst;
  168. void __iomem *fifo = hw_ep->fifo; /* (void __iomem *)USB_FIFO(hw_ep->epnum); */
  169. mu3d_dbg(K_DEBUG, "%s %cX ep%d fifo %p count %d buf %p\n",
  170. __func__, 'R', hw_ep->epnum, fifo, len, dst);
  171. residue = len;
  172. while (residue > 0) {
  173. temp = readl(fifo);
  174. /*Store the first byte */
  175. *dst = temp & 0xFF;
  176. /*Store the 2nd byte, If have */
  177. if (residue > 1)
  178. *(dst + 1) = (temp >> 8) & 0xFF;
  179. /*Store the 3rd byte, If have */
  180. if (residue > 2)
  181. *(dst + 2) = (temp >> 16) & 0xFF;
  182. /*Store the 4th byte, If have */
  183. if (residue > 3)
  184. *(dst + 3) = (temp >> 24) & 0xFF;
  185. if (residue > 4) {
  186. dst = dst + 4;
  187. residue = residue - 4;
  188. } else {
  189. residue = 0;
  190. }
  191. }
  192. print_buf("read-fifo", dst_bg, len);
  193. }
  194. /*-------------------------------------------------------------------------*/
  195. /* for high speed test mode; see USB 2.0 spec 7.1.20 */
  196. static const u8 musb_test_packet[53] = {
  197. /* implicit SYNC then DATA0 to start */
  198. /* JKJKJKJK x9 */
  199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. /* JJKKJJKK x8 */
  201. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  202. /* JJJJKKKK x8 */
  203. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  204. /* JJJJJJJKKKKKKK x8 */
  205. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  206. /* JJJJJJJK x8 */
  207. 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
  208. /* JKKKKKKK x10, JK */
  209. 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
  210. /* implicit CRC16 then EOP to end */
  211. };
  212. void musb_load_testpacket(struct musb *musb)
  213. {
  214. u32 maxp;
  215. maxp = musb->endpoints->max_packet_sz_tx;
  216. mu3d_hal_write_fifo(&musb->endpoints[0], 0, sizeof(musb_test_packet),
  217. (u8 *) musb_test_packet, maxp);
  218. }
  219. /*-------------------------------------------------------------------------*/
  220. /*
  221. * Stops the HNP transition. Caller must take care of locking.
  222. */
  223. /* void musb_hnp_stop(struct musb *musb){} */
  224. /*
  225. * Interrupt Service Routine to record USB "global" interrupts.
  226. * Since these do not happen often and signify things of
  227. * paramount importance, it seems OK to check them individually;
  228. * the order of the tests is specified in the manual
  229. *
  230. * @param musb instance pointer
  231. * @param int_usb register contents
  232. * @param devctl
  233. * @param power
  234. */
  235. static irqreturn_t musb_stage0_irq(struct musb *musb, u32 int_usb, u32 devctl, u32 power)
  236. {
  237. struct usb_otg *otg = musb->xceiv->otg;
  238. irqreturn_t handled = IRQ_NONE;
  239. dev_notice(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
  240. int_usb);
  241. /* in host mode, the peripheral may issue remote wakeup.
  242. * in peripheral mode, the host may resume the link.
  243. * spurious RESUME irqs happen too, paired with SUSPEND.
  244. */
  245. if (int_usb & RESUME_INTR) {
  246. handled = IRQ_HANDLED;
  247. dev_notice(musb->controller, "RESUME (%s)\n",
  248. usb_otg_state_string(musb->xceiv->state));
  249. /* We implement device mode only. */
  250. switch (musb->xceiv->state) {
  251. case OTG_STATE_B_PERIPHERAL:
  252. /* disconnect while suspended? we may
  253. * not get a disconnect irq...
  254. */
  255. if ((devctl & VBUS) != USB_DEVCTL_VBUSVALID) {
  256. musb->int_usb |= DISCONN_INTR;
  257. musb->int_usb &= ~SUSPEND_INTR;
  258. break;
  259. }
  260. musb_g_resume(musb);
  261. break;
  262. case OTG_STATE_B_IDLE:
  263. musb->int_usb &= ~SUSPEND_INTR;
  264. break;
  265. default:
  266. mu3d_dbg(K_ERR, "bogus %s RESUME (%s)\n",
  267. "peripheral", usb_otg_state_string(musb->xceiv->state));
  268. }
  269. }
  270. /* see manual for the order of the tests */
  271. if (int_usb & SESSION_REQ_INTR) { /* no use anymore is ssusb */
  272. handled = IRQ_HANDLED;
  273. }
  274. if (int_usb & VBUSERR_INTR) { /* only for a-device, but now we only work as b-peripheral */
  275. handled = IRQ_HANDLED;
  276. }
  277. if (int_usb & SUSPEND_INTR) {
  278. dev_notice(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
  279. usb_otg_state_string(musb->xceiv->state), devctl, power);
  280. handled = IRQ_HANDLED;
  281. switch (musb->xceiv->state) {
  282. case OTG_STATE_B_IDLE:
  283. if (!musb->is_active)
  284. break;
  285. case OTG_STATE_B_PERIPHERAL:
  286. musb_g_suspend(musb);
  287. musb->is_active = is_otg_enabled(musb) && otg->gadget->b_hnp_enable;
  288. /* if (musb->is_active) { */
  289. /* musb->xceiv->state = OTG_STATE_B_WAIT_ACON; */
  290. /* dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n"); */
  291. /* mod_timer(&musb->otg_timer, jiffies */
  292. /* + msecs_to_jiffies(OTG_TIME_B_ASE0_BRST)); */
  293. /* } */
  294. break;
  295. default:
  296. /* "should not happen" */
  297. musb->is_active = 0;
  298. dev_dbg(musb->controller, "REVISIT: SUSPEND as %s\n",
  299. usb_otg_state_string(musb->xceiv->state));
  300. break;
  301. }
  302. }
  303. #if 0 /* host only ,check it */
  304. if (int_usb & CONN_INTR) {
  305. dev_notice(musb->controller, "CONNECT (%s) devctl %02x\n",
  306. usb_otg_state_string(musb->xceiv->state), devctl);
  307. }
  308. if (int_usb & DISCONN_INTR) {
  309. dev_notice(musb->controller, "DISCONNECT (%s), devctl %02x\n",
  310. usb_otg_state_string(musb->xceiv->state), devctl);
  311. handled = IRQ_HANDLED;
  312. switch (musb->xceiv->state) {
  313. case OTG_STATE_B_PERIPHERAL:
  314. case OTG_STATE_B_IDLE:
  315. musb_g_disconnect(musb);
  316. break;
  317. default:
  318. WARNING("unhandled DISCONNECT transition (%s)\n",
  319. usb_otg_state_string(musb->xceiv->state));
  320. break;
  321. }
  322. }
  323. #endif
  324. /* mentor saves a bit: bus reset and babble share the same irq.
  325. * only host sees babble; only peripheral sees bus reset.
  326. */
  327. if (int_usb & RESET_INTR) {
  328. handled = IRQ_HANDLED;
  329. dev_notice(musb->controller, "BUS RESET as %s\n",
  330. usb_otg_state_string(musb->xceiv->state));
  331. mu3d_dbg(K_INFO, "BUS RESET\n");
  332. switch (musb->xceiv->state) {
  333. case OTG_STATE_B_IDLE:
  334. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  335. /* FALLTHROUGH */
  336. case OTG_STATE_B_PERIPHERAL:
  337. musb_g_reset(musb);
  338. break;
  339. default:
  340. dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
  341. usb_otg_state_string(musb->xceiv->state));
  342. }
  343. }
  344. queue_work(musb->wq, &musb->otg_event_work);
  345. return handled;
  346. }
  347. static void musb_restore_context(struct musb *musb);
  348. static void musb_save_context(struct musb *musb);
  349. static void print_regs(void __iomem *mbase, int offset, int len, char *name)
  350. {
  351. int i;
  352. int length = ((len >> 2) + 3) >> 2;
  353. void __iomem *base = mbase + offset;
  354. mu3d_dbg(K_ERR, "%s(%d)- %s regs:\n", __func__, __LINE__, name);
  355. for (i = 0; i < length; i++) {
  356. mu3d_dbg(K_ERR, "%p: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  357. base, mu3d_readl(base, 0), mu3d_readl(base + 4, 0),
  358. mu3d_readl(base + 8, 0), mu3d_readl(base + 12, 0));
  359. base = base + 16;
  360. }
  361. mu3d_dbg(K_ERR, "\n");
  362. }
  363. void print_mu3d_regs(struct musb *musb)
  364. {
  365. print_regs(musb->mac_base, SSUSB_DEV_BASE, 0xd00, "SSUSB_DEV_BASE");
  366. print_regs(musb->sif_base, SSUSB_SIFSLV_IPPC_BASE, 0x100, "SSUSB_SIFSLV_IPPC_BASE");
  367. print_regs(musb->sif_base, SSUSB_SIFSLV_U2PHY_COM_BASE, 0x100,
  368. "SSUSB_SIFSLV_U2PHY_COM_BASE");
  369. }
  370. /*-------------------------------------------------------------------------*/
  371. void musb_dev_on_off(struct musb *musb, int is_on)
  372. {
  373. mu3d_dbg(K_DEBUG, "%s\n", __func__);
  374. if (is_on) {
  375. #ifdef SUPPORT_U3
  376. mu3d_hal_u3dev_en(musb);
  377. #else
  378. mu3d_hal_u2dev_connect(musb);
  379. #endif
  380. } else {
  381. #ifdef SUPPORT_U3
  382. mu3d_hal_u3dev_dis(musb);
  383. #endif
  384. mu3d_hal_u2dev_disconn(musb);
  385. }
  386. dev_notice(musb->controller, "gadget pullup D%s\n",
  387. is_on ? "+ (soft-connect)" : "- (soft-disconnect)");
  388. }
  389. /*
  390. * Program the HDRC to start (enable interrupts, dma, etc.).
  391. */
  392. void musb_start(struct musb *musb)
  393. {
  394. void __iomem *mbase = musb->mac_base;
  395. u8 devctl = (u8) mu3d_readl(mbase, U3D_DEVICE_CONTROL);
  396. dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
  397. mu3d_dbg(K_INFO, "%s\n", __func__);
  398. /* otg//if (musb->is_clk_on == 0) { */
  399. #ifndef CONFIG_MTK_FPGA
  400. /* Recovert PHY. And turn on CLK. */
  401. /* otg//u3phy->u3p_ops->usb_phy_recover(u3phy, musb->is_clk_on); */
  402. musb->is_clk_on = 1;
  403. /* USB 2.0 slew rate calibration */
  404. /* otg//u3phy->u3p_ops->u2_slew_rate_calibration(u3phy); */
  405. #endif
  406. /* mu3d_setmsk(musb->sif_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST); */
  407. /* mdelay(2); */
  408. ssusb_power_restore(musb->ssusb);
  409. /* disable IP reset and power down, disable U2/U3 ip power down */
  410. mu3d_hal_ssusb_en(musb);
  411. /* reset U3D all dev module. */
  412. mu3d_hal_rst_dev(musb);
  413. musb_restore_context(musb);
  414. /* otg//} */
  415. /*Enable Level 1 interrupt (BMU, QMU, MAC3, DMA, MAC2, EPCTL) */
  416. mu3d_writel(mbase, U3D_LV1IESR, 0xFFFFFFFF);
  417. /* Initialize the default interrupts */
  418. mu3d_hal_system_intr_en(musb);
  419. #ifdef USB_GADGET_SUPERSPEED
  420. /* HS/FS detected by HW */
  421. /* USB2.0 controller will negotiate for HS mode when the device is reset by the host */
  422. mu3d_setmsk(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  423. /* Accept LGO_U1/U2 */
  424. mu3d_setmsk(mbase, U3D_LINK_POWER_CONTROL, SW_U1_ACCEPT_ENABLE | SW_U2_ACCEPT_ENABLE);
  425. /* device responses to u3_exit from host automatically */
  426. mu3d_clrmsk(mbase, U3D_LTSSM_CTRL, SOFT_U3_EXIT_EN);
  427. #else
  428. #ifdef USB_GADGET_DUALSPEED
  429. /* HS/FS detected by HW */
  430. mu3d_setmsk(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  431. #else
  432. /* FS only */
  433. mu3d_clrmsk(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  434. #endif
  435. /* disable U3 port */
  436. mu3d_hal_u3dev_dis(musb);
  437. #endif
  438. /* delay about 0.1us from detecting reset to send chirp-K */
  439. mu3d_clrmsk(mbase, U3D_LINK_RESET_INFO, WTCHRP);
  440. if (need_vbus_chg_int(musb))
  441. mu3d_clrmsk(mbase, U3D_MISC_CTRL, VBUS_FRC_EN | VBUS_ON);
  442. /* U2/U3 detected by HW */
  443. mu3d_writel(mbase, U3D_DEVICE_CONF, 0);
  444. /* OTG switch by iddig */
  445. ssusb_otg_plug_out(musb->ssusb);
  446. musb->is_active = 1;
  447. musb_platform_enable(musb);
  448. if (musb->softconnect)
  449. musb_dev_on_off(musb, 1);
  450. /* mu3d_hal_u3dev_en(musb); */
  451. /* print_mu3d_regs(musb); */
  452. }
  453. static void musb_generic_disable(struct musb *musb)
  454. {
  455. /*Disable interrupts */
  456. mu3d_hal_initr_dis(musb);
  457. /*Clear all interrupt status */
  458. mu3d_hal_clear_intr(musb);
  459. }
  460. /*
  461. * 1. Disable U2 & U3 function
  462. * 2. Notify disconnect event to upper
  463. */
  464. static void gadget_stop(struct musb *musb)
  465. {
  466. /* Disable U2 detect */
  467. /* mu3d_hal_u3dev_dis(musb); */
  468. /* mu3d_hal_u2dev_disconn(musb); */
  469. musb_dev_on_off(musb, 0);
  470. /* notify gadget driver */
  471. if (musb->g.speed != USB_SPEED_UNKNOWN) {
  472. if (musb->gadget_driver && musb->gadget_driver->disconnect)
  473. musb->gadget_driver->disconnect(&musb->g);
  474. musb->g.speed = USB_SPEED_UNKNOWN;
  475. }
  476. }
  477. /*
  478. * Make the HDRC stop (disable interrupts, etc.);
  479. * reversible by musb_start
  480. * called on gadget driver unregister
  481. * with controller locked, irqs blocked
  482. * acts as a NOP unless some role activated the hardware
  483. */
  484. void musb_stop(struct musb *musb)
  485. {
  486. mu3d_dbg(K_INFO, "musb_stop\n");
  487. /* stop IRQs, timers, ... */
  488. musb_platform_disable(musb);
  489. musb_generic_disable(musb);
  490. /*Added by M */
  491. gadget_stop(musb);
  492. musb->is_active = 0;
  493. /*Added by M */
  494. dev_dbg(musb->controller, "HDRC disabled\n");
  495. if (musb->active_ep == 0)
  496. queue_work(musb->wq, &musb->suspend_work);
  497. /* Move to suspend work queue */
  498. #ifdef NEVER
  499. /*
  500. * Note: When reset the SSUSB IP, All MAC regs can _NOT_ be accessed and be reset to the default value.
  501. * So save the MUST-SAVED reg in the context structure before set SSUSB_IP_SW_RST.
  502. */
  503. musb_save_context(musb);
  504. /* Set SSUSB_IP_SW_RST to avoid power leakage */
  505. os_setmsk(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  506. #ifndef CONFIG_MTK_FPGA
  507. /* Let PHY enter savecurrent mode. And turn off CLK. */
  508. usb_phy_savecurrent(musb->is_clk_on);
  509. musb->is_clk_on = 0;
  510. #endif
  511. #endif /* NEVER */
  512. /* FIXME
  513. * - mark host and/or peripheral drivers unusable/inactive
  514. * - disable DMA (and enable it in HdrcStart)
  515. * - make sure we can musb_start() after musb_stop(); with
  516. * OTG mode, gadget driver module rmmod/modprobe cycles that
  517. * - ...
  518. */
  519. musb_platform_try_idle(musb, 0);
  520. }
  521. #if 0
  522. static void musb_shutdown(struct platform_device *pdev)
  523. {
  524. struct musb *musb = dev_to_musb(&pdev->dev);
  525. unsigned long flags;
  526. pm_runtime_get_sync(musb->controller);
  527. spin_lock_irqsave(&musb->lock, flags);
  528. musb_platform_disable(musb);
  529. musb_generic_disable(musb);
  530. spin_unlock_irqrestore(&musb->lock, flags);
  531. mu3d_writel(musb->mac_base, U3D_DEVICE_CONTROL, 0);
  532. musb_platform_exit(musb);
  533. pm_runtime_put(musb->controller);
  534. /* FIXME power down */
  535. }
  536. #endif
  537. /*-------------------------------------------------------------------------*/
  538. /*
  539. * The silicon either has hard-wired endpoint configurations, or else
  540. * "dynamic fifo" sizing. The driver has support for both, though at this
  541. * writing only the dynamic sizing is very well tested. Since we switched
  542. * away from compile-time hardware parameters, we can no longer rely on
  543. * dead code elimination to leave only the relevant one in the object file.
  544. *
  545. * We don't currently use dynamic fifo setup capability to do anything
  546. * more than selecting one of a bunch of predefined configurations.
  547. */
  548. /* static ushort __initdata fifo_mode = 2; */
  549. void ep0_setup(struct musb *musb, struct musb_hw_ep *hw_ep0, const struct musb_fifo_cfg *cfg)
  550. {
  551. u32 ep0csr_val;
  552. mu3d_dbg(K_INFO, "ep0_setup maxpacket: %d\n", cfg->maxpacket);
  553. hw_ep0->fifoaddr_rx = 0;
  554. hw_ep0->fifoaddr_tx = 0;
  555. hw_ep0->is_shared_fifo = true;
  556. hw_ep0->fifo = musb->mac_base + MU3D_FIFO_OFFSET(0);
  557. /* for U2 */
  558. hw_ep0->max_packet_sz_tx = cfg->maxpacket;
  559. hw_ep0->max_packet_sz_rx = cfg->maxpacket;
  560. /* Defines the maximum amount of data that can be transferred through EP0 in a single operation. */
  561. /* os_writelmskumsk((void __iomem *)U3D_EP0CSR, hw_ep0->max_packet_sz_tx, EP0_MAXPKTSZ0, EP0_W1C_BITS); */
  562. ep0csr_val = mu3d_readl(musb->mac_base, U3D_EP0CSR);
  563. ep0csr_val &= ~EP0_MAXPKTSZ0;
  564. ep0csr_val |= (hw_ep0->max_packet_sz_tx & EP0_MAXPKTSZ0);
  565. ep0csr_val &= EP0_W1C_BITS;
  566. mu3d_writel(musb->mac_base, U3D_EP0CSR, ep0csr_val);
  567. /* Enable EP0 interrupt */
  568. mu3d_writel(musb->mac_base, U3D_EPIESR, EP0ISR);
  569. }
  570. /*
  571. * configure a fifo; for non-shared endpoints, this may be called
  572. * once for a tx fifo and once for an rx fifo.
  573. *
  574. * returns negative errno or offset for next fifo.
  575. */
  576. static int
  577. fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep, const struct musb_fifo_cfg *cfg, u16 offset)
  578. {
  579. u16 maxpacket = cfg->maxpacket;
  580. /* u16 c_off = offset >> 3; */
  581. u16 ret_offset = 0;
  582. u32 maxpreg = 0;
  583. u8 mult = 0;
  584. /* calculate mult. added for ssusb. */
  585. if (maxpacket > 1024) {
  586. maxpreg = 1024;
  587. mult = (maxpacket / 1024) - 1;
  588. } else {
  589. maxpreg = maxpacket;
  590. /* set EP0 TX/RX slot to 3 by default */
  591. /* REVISIT-J: WHY? CHECK! */
  592. /* if (hw_ep->epnum == 1) */
  593. /* mult = 3; */
  594. }
  595. /*REVISIT-J: WHY? CHECK! EP1 as BULK EP */
  596. /* EP0 reserved endpoint for control, bidirectional;
  597. * EP1 reserved for bulk, two unidirection halves.
  598. */
  599. /* if (hw_ep->epnum == 1) */
  600. /* musb->bulk_ep = hw_ep; */
  601. /* REVISIT error check: be sure ep0 can both rx and tx ... */
  602. if ((cfg->style == FIFO_TX) || (cfg->style == FIFO_RXTX)) {
  603. hw_ep->max_packet_sz_tx = maxpreg;
  604. hw_ep->mult_tx = mult;
  605. hw_ep->fifoaddr_tx = musb->txfifoadd_offset;
  606. if (maxpacket == 1023)
  607. musb->txfifoadd_offset += (1024 * (hw_ep->mult_tx + 1));
  608. else
  609. musb->txfifoadd_offset += (maxpacket * (hw_ep->mult_tx + 1));
  610. ret_offset = musb->txfifoadd_offset;
  611. }
  612. if ((cfg->style == FIFO_RX) || (cfg->style == FIFO_RXTX)) {
  613. hw_ep->max_packet_sz_rx = maxpreg;
  614. hw_ep->mult_rx = mult;
  615. hw_ep->fifoaddr_rx = musb->rxfifoadd_offset;
  616. if (maxpacket == 1023)
  617. musb->rxfifoadd_offset += (1024 * (hw_ep->mult_rx + 1));
  618. else
  619. musb->rxfifoadd_offset += (maxpacket * (hw_ep->mult_rx + 1));
  620. ret_offset = musb->rxfifoadd_offset;
  621. }
  622. /* NOTE rx and tx endpoint irqs aren't managed separately,
  623. * which happens to be ok
  624. */
  625. musb->epmask |= (1 << hw_ep->epnum);
  626. return ret_offset;
  627. }
  628. struct musb_fifo_cfg ep0_cfg_u3 = {
  629. .style = FIFO_RXTX, .maxpacket = 512,
  630. };
  631. struct musb_fifo_cfg ep0_cfg_u2 = {
  632. .style = FIFO_RXTX, .maxpacket = 64,
  633. };
  634. static int ep_config_from_table(struct musb *musb)
  635. {
  636. const struct musb_fifo_cfg *cfg = NULL;
  637. unsigned i, n = 0;
  638. int offset = 0;
  639. struct musb_hw_ep *hw_ep = musb->endpoints;
  640. if (musb->config->fifo_cfg) {
  641. cfg = musb->config->fifo_cfg;
  642. n = musb->config->fifo_cfg_size;
  643. mu3d_dbg(K_DEBUG, "%s: usb pre-cfg fifo_mode cfg=%p sz=%d\n", musb_driver_name,
  644. cfg, n);
  645. } else {
  646. mu3d_dbg(K_ERR, "%s: SHOULD provide usb ep config table\n", musb_driver_name);
  647. return -EINVAL;
  648. }
  649. #ifdef USB_GADGET_SUPERSPEED /* SS */
  650. /* use SS EP0 as default; it may be changed later */
  651. mu3d_dbg(K_INFO, "%s ep_config_from_table ep0_cfg_u3\n", __func__);
  652. ep0_setup(musb, hw_ep, &ep0_cfg_u3);
  653. #else /* HS, FS */
  654. mu3d_dbg(K_INFO, "%s ep_config_from_table ep0_cfg_u2\n", __func__);
  655. ep0_setup(musb, hw_ep, &ep0_cfg_u2);
  656. #endif
  657. /* assert(offset > 0) */
  658. /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
  659. * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
  660. */
  661. for (i = 0; i < n; i++) {
  662. u8 epn = cfg->hw_ep_num;
  663. if (epn >= musb->config->num_eps) {
  664. mu3d_dbg(K_ERR, "%s: invalid ep %d\n", musb_driver_name, epn);
  665. return -EINVAL;
  666. }
  667. offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
  668. if (offset < 0) {
  669. mu3d_dbg(K_ERR, "%s: mem overrun, ep %d\n", musb_driver_name, epn);
  670. return -EINVAL;
  671. }
  672. epn++;
  673. musb->nr_endpoints = max(epn, musb->nr_endpoints);
  674. }
  675. mu3d_dbg(K_INFO, "%s: %d/%d max ep, %d/%d memory\n",
  676. musb_driver_name,
  677. n + 1, musb->config->num_eps * 2 - 1, offset, (1 << (musb->config->ram_bits + 2)));
  678. /* if (!musb->bulk_ep) { */
  679. /* pr_debug("%s: missing bulk\n", musb_driver_name); */
  680. /* return -EINVAL; */
  681. /* } */
  682. return 0;
  683. }
  684. /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
  685. * configure endpoints, or take their config from silicon
  686. */
  687. static int musb_core_init(struct musb *musb)
  688. {
  689. /* u8 reg; */
  690. /* char *type; */
  691. /* char aInfo[90], aRevision[32], aDate[12]; */
  692. void __iomem *mbase = musb->mac_base;
  693. u32 cap_epinfo;
  694. int status = 0;
  695. int i;
  696. mu3d_dbg(K_INFO, "%s() in (%#x)\n", __func__, U3D_SSUSB_HW_ID);
  697. musb->hwvers = mu3d_readl(musb->sif_base, U3D_SSUSB_HW_ID);
  698. mu3d_dbg(K_INFO, "%s: HDC version 0x%x\n", musb_driver_name, musb->hwvers);
  699. /* add for U3D */
  700. musb->txfifoadd_offset = U3D_FIFO_START_ADDRESS;
  701. musb->rxfifoadd_offset = U3D_FIFO_START_ADDRESS;
  702. mu3d_dbg(K_INFO, "%s EPnFIFOSZ Tx=%x, Rx=%x\n", __func__,
  703. mu3d_readl(mbase, U3D_CAP_EPNTXFFSZ), mu3d_readl(mbase, U3D_CAP_EPNRXFFSZ));
  704. cap_epinfo = mu3d_readl(mbase, U3D_CAP_EPINFO);
  705. mu3d_dbg(K_INFO, "%s EPnNum Tx=%x, Rx=%d\n", __func__, cap_epinfo & 0x1F,
  706. (cap_epinfo >> 8) & 0x1F);
  707. /* if (os_readl(U3D_CAP_EPNTXFFSZ) && os_readl(U3D_CAP_EPNRXFFSZ)) */
  708. /* musb->dyn_fifo = true; */
  709. /* else */
  710. /* musb->dyn_fifo = false; */
  711. /* discover endpoint configuration */
  712. musb->nr_endpoints = 1;
  713. musb->epmask = 1;
  714. /* status = ep_config_from_table(musb); */
  715. /* if (musb->dyn_fifo) */
  716. status = ep_config_from_table(musb);
  717. /* else */
  718. /* status = ep_config_from_hw(musb); */
  719. if (status < 0)
  720. return status;
  721. /* finish init, and print endpoint config */
  722. for (i = 0; i < musb->nr_endpoints; i++) {
  723. struct musb_hw_ep *hw_ep = musb->endpoints + i;
  724. hw_ep->fifo = mbase + MU3D_FIFO_OFFSET(i);
  725. /* change data structure for ssusb */
  726. if (i > 0) {
  727. hw_ep->addr_txcsr0 = MU3D_EP_TXCR0_OFFSET(i, mbase);
  728. hw_ep->addr_txcsr1 = MU3D_EP_TXCR1_OFFSET(i, mbase);
  729. hw_ep->addr_txcsr2 = MU3D_EP_TXCR2_OFFSET(i, mbase);
  730. hw_ep->addr_rxcsr0 = MU3D_EP_RXCR0_OFFSET(i, mbase);
  731. hw_ep->addr_rxcsr1 = MU3D_EP_RXCR1_OFFSET(i, mbase);
  732. hw_ep->addr_rxcsr2 = MU3D_EP_RXCR2_OFFSET(i, mbase);
  733. hw_ep->addr_rxcsr3 = MU3D_EP_RXCR3_OFFSET(i, mbase);
  734. }
  735. /* hw_ep->target_regs = musb_read_target_reg_base(i, mbase); */
  736. /* hw_ep->rx_reinit = 1; */
  737. /* hw_ep->tx_reinit = 1; */
  738. if (hw_ep->max_packet_sz_tx) {
  739. dev_dbg(musb->controller,
  740. "%s: hw_ep %d%s, %smax %d\n",
  741. musb_driver_name, i,
  742. hw_ep->is_shared_fifo ? "shared" : "tx",
  743. "", hw_ep->max_packet_sz_tx);
  744. }
  745. if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
  746. dev_dbg(musb->controller,
  747. "%s: hw_ep %d%s, %smax %d\n",
  748. musb_driver_name, i, "rx", "", hw_ep->max_packet_sz_rx);
  749. }
  750. if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
  751. dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
  752. }
  753. #ifdef USE_SSUSB_QMU
  754. /* Allocate GBD and BD */
  755. mu3d_hal_alloc_qmu_mem(musb);
  756. /* Iniital QMU */
  757. mu3d_hal_init_qmu(musb);
  758. musb_save_context(musb);
  759. #endif
  760. return 0;
  761. }
  762. /*
  763. * handle all the irqs defined by the HDRC core. for now we expect: other
  764. * irq sources (phy, dma, etc) will be handled first, musb->int_* values
  765. * will be assigned, and the irq will already have been acked.
  766. *
  767. * called in irq context with spinlock held, irqs blocked
  768. */
  769. irqreturn_t musb_interrupt(struct musb *musb)
  770. {
  771. irqreturn_t retval = IRQ_NONE;
  772. u32 devctl, power = 0;
  773. #ifndef USE_SSUSB_QMU
  774. u32 reg = 0, ep_num = 0;
  775. #endif
  776. devctl = mu3d_readl(musb->mac_base, U3D_DEVICE_CONTROL);
  777. power = mu3d_readl(musb->mac_base, U3D_POWER_MANAGEMENT);
  778. /* dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n", */
  779. mu3d_dbg(K_DEBUG, "IRQ usb%04x tx%04x rx%04x\n",
  780. /*(devctl & HOSTMODE) ? "host" : "peripheral", */
  781. musb->int_usb, musb->int_tx, musb->int_rx);
  782. /* the core can interrupt us for multiple reasons; docs have
  783. * a generic interrupt flowchart to follow
  784. */
  785. if (musb->int_usb)
  786. retval |= musb_stage0_irq(musb, musb->int_usb, devctl, power);
  787. /* "stage 1" is handling endpoint irqs */
  788. /* handle endpoint 0 first */
  789. if (musb->int_tx & 1)
  790. retval |= musb_g_ep0_irq(musb);
  791. #ifndef USE_SSUSB_QMU
  792. /* RX on endpoints 1-15 */
  793. reg = musb->int_rx >> 1;
  794. ep_num = 1;
  795. while (reg) {
  796. if (reg & 1) {
  797. /* musb_ep_select(musb->mregs, ep_num); */
  798. /* REVISIT just retval = ep->rx_irq(...) */
  799. retval = IRQ_HANDLED;
  800. musb_g_rx(musb, ep_num);
  801. }
  802. reg >>= 1;
  803. ep_num++;
  804. }
  805. /* TX on endpoints 1-15 */
  806. reg = musb->int_tx >> 1;
  807. ep_num = 1;
  808. while (reg) {
  809. if (reg & 1) {
  810. /* musb_ep_select(musb->mregs, ep_num); */
  811. /* REVISIT just retval |= ep->tx_irq(...) */
  812. retval = IRQ_HANDLED;
  813. musb_g_tx(musb, ep_num);
  814. }
  815. reg >>= 1;
  816. ep_num++;
  817. }
  818. #endif
  819. return retval;
  820. }
  821. /* EXPORT_SYMBOL_GPL(musb_interrupt); */
  822. /*-------------------------------------------------------------------------*/
  823. static void musb_save_context(struct musb *musb)
  824. {
  825. int i;
  826. for (i = 0; i < musb->config->num_eps; ++i) {
  827. mu3d_dbg(K_DEBUG, "%s EP%d\n", __func__, i);
  828. #ifdef USE_SSUSB_QMU
  829. /* Save TXQ/RXQ starting address. Those would be reset to 0 after reset SSUSB IP. */
  830. musb->context.index_regs[i].txqmuaddr =
  831. mu3d_readl(musb->mac_base, USB_QMU_TQSAR(i + 1));
  832. mu3d_dbg(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  833. musb->context.index_regs[i].txqmuaddr);
  834. musb->context.index_regs[i].rxqmuaddr =
  835. mu3d_readl(musb->mac_base, USB_QMU_RQSAR(i + 1));
  836. mu3d_dbg(K_DEBUG, "%s RQSAR[%d]=%x\n", __func__, i,
  837. musb->context.index_regs[i].rxqmuaddr);
  838. #endif
  839. }
  840. }
  841. static void musb_restore_context(struct musb *musb)
  842. {
  843. int i;
  844. for (i = 0; i < musb->config->num_eps; ++i) {
  845. #ifdef USE_SSUSB_QMU
  846. void __iomem *mbase = musb->mac_base;
  847. mu3d_writel(mbase, USB_QMU_TQSAR(i + 1), musb->context.index_regs[i].txqmuaddr);
  848. mu3d_writel(mbase, USB_QMU_RQSAR(i + 1), musb->context.index_regs[i].rxqmuaddr);
  849. mu3d_dbg(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  850. mu3d_readl(mbase, USB_QMU_TQSAR(i + 1)));
  851. mu3d_dbg(K_DEBUG, "%s RQSAR[%d]=%x\n", __func__, i,
  852. mu3d_readl(mbase, USB_QMU_RQSAR(i + 1)));
  853. #endif
  854. }
  855. }
  856. static void musb_suspend_work(struct work_struct *data)
  857. {
  858. struct musb *musb = container_of(data, struct musb, suspend_work);
  859. mu3d_dbg(K_INFO, "%s active_ep=%d, clk_on=%d\n", __func__, musb->active_ep,
  860. musb->is_clk_on);
  861. /* otg//if (musb->is_clk_on == 1) { */
  862. /*
  863. * Note: musb_save_context() _MUST_ be called _BEFORE_ mtu3d_suspend_noirq().
  864. * Because when mtu3d_suspend_noirq() resets the SSUSB IP, All MAC regs can _NOT_ be read and be reset to
  865. * the default value. So save the MUST-SAVED reg in the context structure.
  866. */
  867. musb_save_context(musb);
  868. /* Set SSUSB_IP_SW_RST to avoid power leakage */
  869. /* otg//mu3d_setmsk(musb->sif_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST); */
  870. /* it will disable xhci at the same time, maybe its not we want, pdw device only here */
  871. mu3d_hal_ssusb_dis(musb);
  872. ssusb_power_save(musb->ssusb);
  873. #ifndef CONFIG_MTK_FPGA
  874. /* Let PHY enter savecurrent mode. And turn off CLK. */
  875. /* otg//u3phy->u3p_ops->usb_phy_savecurrent(u3phy, musb->is_clk_on); */
  876. musb->is_clk_on = 0;
  877. #endif
  878. /* otg//} */
  879. }
  880. /* Only used to provide driver mode change events */
  881. static void musb_otg_event_work(struct work_struct *data)
  882. {
  883. struct musb *musb = container_of(data, struct musb, otg_event_work);
  884. static int old_state;
  885. mu3d_dbg(K_INFO, "%s [%d]=[%d]\n", __func__, musb->xceiv->state, old_state);
  886. if (musb->xceiv->state != old_state) {
  887. old_state = musb->xceiv->state;
  888. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  889. }
  890. }
  891. /* --------------------------------------------------------------------------
  892. * Init support
  893. */
  894. #if 0
  895. static void __iomem *get_regs_base(struct platform_device *pdev, int index)
  896. {
  897. struct resource *res;
  898. void __iomem *regs;
  899. res = platform_get_resource(pdev, IORESOURCE_MEM, index);
  900. if (NULL == res) {
  901. mu3d_dbg(K_ERR, "error to get rsrc %d\n", index);
  902. return NULL;
  903. }
  904. regs = ioremap_nocache(res->start, resource_size(res));
  905. if (NULL == regs) {
  906. mu3d_dbg(K_ERR, "error mapping memory for %d\n", index);
  907. return NULL;
  908. }
  909. mu3d_dbg(K_INFO, "index:%d - iomap:0x%p, res:%#lx, len:%#lx\n", index, regs,
  910. (unsigned long)res->start, (unsigned long)resource_size(res));
  911. return regs;
  912. }
  913. #endif
  914. static struct musb *allocate_instance(struct device *dev, struct musb_hdrc_config *config)
  915. {
  916. struct musb *musb;
  917. struct musb_hw_ep *ep;
  918. int epnum;
  919. musb = kzalloc(sizeof(struct musb), GFP_KERNEL);
  920. if (NULL == musb)
  921. return NULL;
  922. /* INIT_LIST_HEAD(&musb->control); */
  923. /* INIT_LIST_HEAD(&musb->in_bulk); */
  924. /* INIT_LIST_HEAD(&musb->out_bulk); */
  925. spin_lock_init(&musb->lock);
  926. /* dev_set_drvdata(dev, musb); rm musb layer */
  927. musb->config = config;
  928. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  929. for (epnum = 0, ep = musb->endpoints; epnum < musb->config->num_eps; epnum++, ep++) {
  930. ep->musb = musb;
  931. ep->epnum = epnum;
  932. }
  933. musb->controller = dev;
  934. /* added for ssusb: */
  935. /* musb->xceiv = kzalloc(sizeof(struct otg_transceiver), GFP_KERNEL); */
  936. /* memset(musb->xceiv, 0, sizeof(struct otg_transceiver)); */
  937. /* musb->xceiv->state = OTG_STATE_B_IDLE; //initial its value */
  938. return musb;
  939. }
  940. static void musb_free(struct musb *musb)
  941. {
  942. /* this has multiple entry modes. it handles fault cleanup after
  943. * probe(), where things may be partially set up, as well as rmmod
  944. * cleanup after everything's been de-activated.
  945. */
  946. ssusb_sysfs_exit(musb);
  947. musb_gadget_cleanup(musb);
  948. if (musb->irq >= 0) {
  949. if (musb->irq_wake)
  950. disable_irq_wake(musb->irq);
  951. free_irq(musb->irq, musb);
  952. }
  953. cancel_work_sync(&musb->otg_event_work);
  954. cancel_delayed_work_sync(&musb->connection_work);
  955. /* cancel_delayed_work_sync(&musb->check_ltssm_work); */
  956. cancel_work_sync(&musb->suspend_work);
  957. #ifdef USE_SSUSB_QMU
  958. tasklet_kill(&musb->qmu_done);
  959. mu3d_hal_free_qmu_mem(musb);
  960. #endif
  961. wake_lock_destroy(&musb->usb_wakelock);
  962. /* added for ssusb: */
  963. kfree(musb->xceiv); /* free the instance allocated in allocate_instance */
  964. musb->xceiv = NULL;
  965. kfree(musb);
  966. }
  967. /*
  968. * Perform generic per-controller initialization.
  969. *
  970. * @pDevice: the controller (already clocked, etc)
  971. * @irq: irq number
  972. * @mregs: virtual address of controller registers,
  973. * not yet corrected for platform-specific offsets
  974. */
  975. static int musb_init_controller(struct musb *musb, struct device *dev, int irq)
  976. {
  977. struct musb_hdrc_platform_data *plat = dev->platform_data;
  978. int status;
  979. /* The driver might handle more features than the board; OK.
  980. * Fail when the board needs a feature that's not enabled.
  981. */
  982. mu3d_dbg(K_INFO, "[MU3D]%s\n", __func__);
  983. musb->board_mode = plat->drv_mode;
  984. /* musb->board_mode = MUSB_PERIPHERAL; for test , rm later */
  985. /* musb->board_set_power = plat->set_power; */
  986. /* musb->min_power = plat->min_power; */
  987. musb->ops = plat->platform_ops;
  988. musb->usb_mode = 1;
  989. _mu3d_musb = musb;
  990. wake_lock_init(&musb->usb_wakelock, WAKE_LOCK_SUSPEND, "USB.lock");
  991. musb->wq = create_singlethread_workqueue("usb_work");
  992. if (!musb->wq) {
  993. status = -ENOMEM;
  994. goto fail0;
  995. }
  996. INIT_DELAYED_WORK(&musb->connection_work, connection_work);
  997. mu3d_dbg(K_INFO, "[MU3D]%s %d\n", __func__, __LINE__);
  998. /* The musb_platform_init() call:
  999. * - adjusts musb->mregs and musb->isr if needed,
  1000. * - may initialize an integrated tranceiver
  1001. * - initializes musb->xceiv, usually by otg_get_transceiver()
  1002. * - stops powering VBUS
  1003. *
  1004. * There are various transceiver configurations. Blackfin,
  1005. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  1006. * external/discrete ones in various flavors (twl4030 family,
  1007. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  1008. */
  1009. /*move to musb_init.c */
  1010. /*musb->isr = generic_interrupt; */
  1011. status = musb_platform_init(musb);
  1012. if (status < 0)
  1013. goto fail1;
  1014. if (!musb->isr) {
  1015. status = -ENODEV;
  1016. goto fail3;
  1017. }
  1018. pm_runtime_get_sync(musb->controller);
  1019. /* ideally this would be abstracted in platform setup */
  1020. /* if (!is_dma_capable() || !musb->dma_controller) */
  1021. /* dev->dma_mask = NULL; */
  1022. /* be sure interrupts are disabled before connecting ISR */
  1023. musb_platform_disable(musb);
  1024. musb_generic_disable(musb);
  1025. /* setup musb parts of the core (especially endpoints) */
  1026. status = musb_core_init(musb);
  1027. if (status < 0)
  1028. goto fail3;
  1029. /* REVISIT-J: Do _NOT_ support OTG functionality */
  1030. /* setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb); */
  1031. INIT_WORK(&musb->otg_event_work, musb_otg_event_work);
  1032. INIT_WORK(&musb->suspend_work, musb_suspend_work);
  1033. #ifdef USE_SSUSB_QMU
  1034. tasklet_init(&musb->qmu_done, qmu_done_tasklet, (unsigned long)musb);
  1035. #endif
  1036. /* attach to the IRQ */
  1037. mu3d_dbg(K_INFO, "%s -> %d irq%d, isr:%p, %s, musb:%p\n", __func__, __LINE__,
  1038. irq, musb->isr, dev_name(dev), musb);
  1039. if (request_irq(irq, musb->isr, IRQF_TRIGGER_LOW, dev_name(dev), musb)) {
  1040. dev_err(dev, "request_irq %d failed!\n", irq);
  1041. status = -ENODEV;
  1042. goto fail3;
  1043. }
  1044. musb->irq = irq;
  1045. /* FIXME this handles wakeup irqs wrong */
  1046. if (enable_irq_wake(irq) == 0) {
  1047. musb->irq_wake = 1;
  1048. device_init_wakeup(dev, 1);
  1049. } else {
  1050. musb->irq_wake = 0;
  1051. }
  1052. /* MUSB_DEV_MODE(musb); */
  1053. musb->xceiv->otg->default_a = 0;
  1054. musb->xceiv->state = OTG_STATE_B_IDLE;
  1055. status = musb_gadget_setup(musb);
  1056. if (status < 0)
  1057. goto fail4;
  1058. status = ssusb_sysfs_init(musb);
  1059. if (status)
  1060. goto fail5;
  1061. pm_runtime_put(musb->controller);
  1062. dev_info(dev, "MU3D using %s, IRQ %d\n", (is_dma_capable()) ? "QMU" : "PIO", musb->irq);
  1063. return 0;
  1064. fail5:
  1065. musb_gadget_cleanup(musb);
  1066. fail4:
  1067. if (musb->irq_wake)
  1068. device_init_wakeup(dev, 0);
  1069. free_irq(musb->irq, musb);
  1070. fail3:
  1071. musb_platform_exit(musb);
  1072. fail1:
  1073. destroy_workqueue(musb->wq);
  1074. fail0:
  1075. _mu3d_musb = NULL;
  1076. mu3d_dbg(K_ERR, "%s() failed with status %d\n", __func__, status);
  1077. return status;
  1078. }
  1079. /*-------------------------------------------------------------------------*/
  1080. int ssusb_gadget_init(struct ssusb_mtk *ssusb)
  1081. {
  1082. struct musb_hdrc_platform_data *pdata = ssusb->pdata;
  1083. struct device *dev = ssusb->dev;
  1084. struct musb *musb = NULL;
  1085. int retval = -ENOMEM;
  1086. musb = allocate_instance(dev, pdata->config);
  1087. if (NULL == musb) {
  1088. mu3d_dbg(K_ERR, "fail to alloc musb\n");
  1089. retval = -ENOMEM;
  1090. goto musb_err;
  1091. }
  1092. musb->mac_base = ssusb->mac_base;
  1093. musb->sif_base = ssusb->sif_base;
  1094. /* musb->sif2_base = ssusb->sif2_base; */
  1095. musb->start_mu3d = ssusb->start_mu3d;
  1096. ssusb->mu3di = musb;
  1097. musb->ssusb = ssusb;
  1098. mu3d_dbg(K_INFO, "mac_base=0x%p, sif_base=0x%p\n", musb->mac_base, musb->sif_base);
  1099. retval = musb_init_controller(musb, dev, ssusb->mu3d_irq);
  1100. if (retval < 0)
  1101. goto mi_err;
  1102. mu3d_dbg(K_INFO, " %s() success...\n", __func__);
  1103. return 0;
  1104. mi_err:
  1105. kfree(musb);
  1106. ssusb->mu3di = NULL;
  1107. musb_err:
  1108. mu3d_dbg(K_ERR, " %s() fail...\n", __func__);
  1109. return retval;
  1110. }
  1111. void ssusb_gadget_exit(struct ssusb_mtk *ssusb)
  1112. {
  1113. struct musb *musb = ssusb->mu3di;
  1114. /* this gets called on rmmod.
  1115. * - Host mode: host may still be active
  1116. * - Peripheral mode: peripheral is deactivated (or never-activated)
  1117. * - OTG mode: both roles are deactivated (or never-activated)
  1118. */
  1119. pm_runtime_get_sync(musb->controller);
  1120. mu3d_writel(musb->mac_base, U3D_DEVICE_CONTROL, 0);
  1121. musb_platform_exit(musb);
  1122. pm_runtime_put(musb->controller);
  1123. musb_free(musb);
  1124. _mu3d_musb = NULL;
  1125. device_init_wakeup(ssusb->dev, 0);
  1126. }