msm72k_otg.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /* Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/err.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/ioport.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/usb/ulpi.h>
  35. #include <linux/usb/gadget.h>
  36. #include <linux/usb/hcd.h>
  37. #include <linux/usb/msm_hsusb.h>
  38. #include <linux/usb/msm_hsusb_hw.h>
  39. #include <mach/clk.h>
  40. #define MSM_USB_BASE (motg->regs)
  41. #define DRIVER_NAME "msm_otg"
  42. #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
  43. static int ulpi_read(struct otg_transceiver *otg, u32 reg)
  44. {
  45. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  46. int cnt = 0;
  47. /* initiate read operation */
  48. writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
  49. USB_ULPI_VIEWPORT);
  50. /* wait for completion */
  51. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  52. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  53. break;
  54. udelay(1);
  55. cnt++;
  56. }
  57. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  58. dev_err(otg->dev, "ulpi_read: timeout %08x\n",
  59. readl(USB_ULPI_VIEWPORT));
  60. return -ETIMEDOUT;
  61. }
  62. return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
  63. }
  64. static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
  65. {
  66. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  67. int cnt = 0;
  68. /* initiate write operation */
  69. writel(ULPI_RUN | ULPI_WRITE |
  70. ULPI_ADDR(reg) | ULPI_DATA(val),
  71. USB_ULPI_VIEWPORT);
  72. /* wait for completion */
  73. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  74. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  75. break;
  76. udelay(1);
  77. cnt++;
  78. }
  79. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  80. dev_err(otg->dev, "ulpi_write: timeout\n");
  81. return -ETIMEDOUT;
  82. }
  83. return 0;
  84. }
  85. static struct otg_io_access_ops msm_otg_io_ops = {
  86. .read = ulpi_read,
  87. .write = ulpi_write,
  88. };
  89. static void ulpi_init(struct msm_otg *motg)
  90. {
  91. struct msm_otg_platform_data *pdata = motg->pdata;
  92. int *seq = pdata->phy_init_seq;
  93. if (!seq)
  94. return;
  95. while (seq[0] >= 0) {
  96. dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
  97. seq[0], seq[1]);
  98. ulpi_write(&motg->otg, seq[0], seq[1]);
  99. seq += 2;
  100. }
  101. }
  102. static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
  103. {
  104. int ret;
  105. if (assert) {
  106. ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
  107. if (ret)
  108. dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
  109. } else {
  110. ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
  111. if (ret)
  112. dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
  113. }
  114. return ret;
  115. }
  116. static int msm_otg_phy_clk_reset(struct msm_otg *motg)
  117. {
  118. int ret;
  119. ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
  120. if (ret) {
  121. dev_err(motg->otg.dev, "usb phy clk assert failed\n");
  122. return ret;
  123. }
  124. usleep_range(10000, 12000);
  125. ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
  126. if (ret)
  127. dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
  128. return ret;
  129. }
  130. static int msm_otg_phy_reset(struct msm_otg *motg)
  131. {
  132. u32 val;
  133. int ret;
  134. int retries;
  135. ret = msm_otg_link_clk_reset(motg, 1);
  136. if (ret)
  137. return ret;
  138. ret = msm_otg_phy_clk_reset(motg);
  139. if (ret)
  140. return ret;
  141. ret = msm_otg_link_clk_reset(motg, 0);
  142. if (ret)
  143. return ret;
  144. val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
  145. writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
  146. for (retries = 3; retries > 0; retries--) {
  147. ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
  148. ULPI_CLR(ULPI_FUNC_CTRL));
  149. if (!ret)
  150. break;
  151. ret = msm_otg_phy_clk_reset(motg);
  152. if (ret)
  153. return ret;
  154. }
  155. if (!retries)
  156. return -ETIMEDOUT;
  157. /* This reset calibrates the phy, if the above write succeeded */
  158. ret = msm_otg_phy_clk_reset(motg);
  159. if (ret)
  160. return ret;
  161. for (retries = 3; retries > 0; retries--) {
  162. ret = ulpi_read(&motg->otg, ULPI_DEBUG);
  163. if (ret != -ETIMEDOUT)
  164. break;
  165. ret = msm_otg_phy_clk_reset(motg);
  166. if (ret)
  167. return ret;
  168. }
  169. if (!retries)
  170. return -ETIMEDOUT;
  171. dev_info(motg->otg.dev, "phy_reset: success\n");
  172. return 0;
  173. }
  174. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  175. static int msm_otg_reset(struct otg_transceiver *otg)
  176. {
  177. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  178. struct msm_otg_platform_data *pdata = motg->pdata;
  179. int cnt = 0;
  180. int ret;
  181. u32 val = 0;
  182. u32 ulpi_val = 0;
  183. ret = msm_otg_phy_reset(motg);
  184. if (ret) {
  185. dev_err(otg->dev, "phy_reset failed\n");
  186. return ret;
  187. }
  188. ulpi_init(motg);
  189. writel(USBCMD_RESET, USB_USBCMD);
  190. while (cnt < LINK_RESET_TIMEOUT_USEC) {
  191. if (!(readl(USB_USBCMD) & USBCMD_RESET))
  192. break;
  193. udelay(1);
  194. cnt++;
  195. }
  196. if (cnt >= LINK_RESET_TIMEOUT_USEC)
  197. return -ETIMEDOUT;
  198. /* select ULPI phy */
  199. writel(0x80000000, USB_PORTSC);
  200. msleep(100);
  201. writel(0x0, USB_AHBBURST);
  202. writel(0x00, USB_AHBMODE);
  203. if (pdata->otg_control == OTG_PHY_CONTROL) {
  204. val = readl(USB_OTGSC);
  205. if (pdata->mode == USB_OTG) {
  206. ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
  207. val |= OTGSC_IDIE | OTGSC_BSVIE;
  208. } else if (pdata->mode == USB_PERIPHERAL) {
  209. ulpi_val = ULPI_INT_SESS_VALID;
  210. val |= OTGSC_BSVIE;
  211. }
  212. writel(val, USB_OTGSC);
  213. ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
  214. ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
  215. }
  216. return 0;
  217. }
  218. #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
  219. #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
  220. #ifdef CONFIG_PM_SLEEP
  221. static int msm_otg_suspend(struct msm_otg *motg)
  222. {
  223. struct otg_transceiver *otg = &motg->otg;
  224. struct usb_bus *bus = otg->host;
  225. struct msm_otg_platform_data *pdata = motg->pdata;
  226. int cnt = 0;
  227. if (atomic_read(&motg->in_lpm))
  228. return 0;
  229. disable_irq(motg->irq);
  230. /*
  231. * Interrupt Latch Register auto-clear feature is not present
  232. * in all PHY versions. Latch register is clear on read type.
  233. * Clear latch register to avoid spurious wakeup from
  234. * low power mode (LPM).
  235. */
  236. ulpi_read(otg, 0x14);
  237. /*
  238. * PHY comparators are disabled when PHY enters into low power
  239. * mode (LPM). Keep PHY comparators ON in LPM only when we expect
  240. * VBUS/Id notifications from USB PHY. Otherwise turn off USB
  241. * PHY comparators. This save significant amount of power.
  242. */
  243. if (pdata->otg_control == OTG_PHY_CONTROL)
  244. ulpi_write(otg, 0x01, 0x30);
  245. /*
  246. * PLL is not turned off when PHY enters into low power mode (LPM).
  247. * Disable PLL for maximum power savings.
  248. */
  249. ulpi_write(otg, 0x08, 0x09);
  250. /*
  251. * PHY may take some time or even fail to enter into low power
  252. * mode (LPM). Hence poll for 500 msec and reset the PHY and link
  253. * in failure case.
  254. */
  255. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  256. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  257. if (readl(USB_PORTSC) & PORTSC_PHCD)
  258. break;
  259. udelay(1);
  260. cnt++;
  261. }
  262. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
  263. dev_err(otg->dev, "Unable to suspend PHY\n");
  264. msm_otg_reset(otg);
  265. enable_irq(motg->irq);
  266. return -ETIMEDOUT;
  267. }
  268. /*
  269. * PHY has capability to generate interrupt asynchronously in low
  270. * power mode (LPM). This interrupt is level triggered. So USB IRQ
  271. * line must be disabled till async interrupt enable bit is cleared
  272. * in USBCMD register. Assert STP (ULPI interface STOP signal) to
  273. * block data communication from PHY.
  274. */
  275. writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
  276. clk_disable(motg->pclk);
  277. clk_disable(motg->clk);
  278. if (motg->core_clk)
  279. clk_disable(motg->core_clk);
  280. if (device_may_wakeup(otg->dev))
  281. enable_irq_wake(motg->irq);
  282. if (bus)
  283. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  284. atomic_set(&motg->in_lpm, 1);
  285. enable_irq(motg->irq);
  286. dev_info(otg->dev, "USB in low power mode\n");
  287. return 0;
  288. }
  289. static int msm_otg_resume(struct msm_otg *motg)
  290. {
  291. struct otg_transceiver *otg = &motg->otg;
  292. struct usb_bus *bus = otg->host;
  293. int cnt = 0;
  294. unsigned temp;
  295. if (!atomic_read(&motg->in_lpm))
  296. return 0;
  297. clk_enable(motg->pclk);
  298. clk_enable(motg->clk);
  299. if (motg->core_clk)
  300. clk_enable(motg->core_clk);
  301. temp = readl(USB_USBCMD);
  302. temp &= ~ASYNC_INTR_CTRL;
  303. temp &= ~ULPI_STP_CTRL;
  304. writel(temp, USB_USBCMD);
  305. /*
  306. * PHY comes out of low power mode (LPM) in case of wakeup
  307. * from asynchronous interrupt.
  308. */
  309. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  310. goto skip_phy_resume;
  311. writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
  312. while (cnt < PHY_RESUME_TIMEOUT_USEC) {
  313. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  314. break;
  315. udelay(1);
  316. cnt++;
  317. }
  318. if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
  319. /*
  320. * This is a fatal error. Reset the link and
  321. * PHY. USB state can not be restored. Re-insertion
  322. * of USB cable is the only way to get USB working.
  323. */
  324. dev_err(otg->dev, "Unable to resume USB."
  325. "Re-plugin the cable\n");
  326. msm_otg_reset(otg);
  327. }
  328. skip_phy_resume:
  329. if (device_may_wakeup(otg->dev))
  330. disable_irq_wake(motg->irq);
  331. if (bus)
  332. set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  333. if (motg->async_int) {
  334. motg->async_int = 0;
  335. pm_runtime_put(otg->dev);
  336. enable_irq(motg->irq);
  337. }
  338. atomic_set(&motg->in_lpm, 0);
  339. dev_info(otg->dev, "USB exited from low power mode\n");
  340. return 0;
  341. }
  342. #endif
  343. static void msm_otg_start_host(struct otg_transceiver *otg, int on)
  344. {
  345. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  346. struct msm_otg_platform_data *pdata = motg->pdata;
  347. struct usb_hcd *hcd;
  348. if (!otg->host)
  349. return;
  350. hcd = bus_to_hcd(otg->host);
  351. if (on) {
  352. dev_dbg(otg->dev, "host on\n");
  353. if (pdata->vbus_power)
  354. pdata->vbus_power(1);
  355. /*
  356. * Some boards have a switch cotrolled by gpio
  357. * to enable/disable internal HUB. Enable internal
  358. * HUB before kicking the host.
  359. */
  360. if (pdata->setup_gpio)
  361. pdata->setup_gpio(OTG_STATE_A_HOST);
  362. #ifdef CONFIG_USB
  363. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  364. #endif
  365. } else {
  366. dev_dbg(otg->dev, "host off\n");
  367. #ifdef CONFIG_USB
  368. usb_remove_hcd(hcd);
  369. #endif
  370. if (pdata->setup_gpio)
  371. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  372. if (pdata->vbus_power)
  373. pdata->vbus_power(0);
  374. }
  375. }
  376. static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  377. {
  378. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  379. struct usb_hcd *hcd;
  380. /*
  381. * Fail host registration if this board can support
  382. * only peripheral configuration.
  383. */
  384. if (motg->pdata->mode == USB_PERIPHERAL) {
  385. dev_info(otg->dev, "Host mode is not supported\n");
  386. return -ENODEV;
  387. }
  388. if (!host) {
  389. if (otg->state == OTG_STATE_A_HOST) {
  390. pm_runtime_get_sync(otg->dev);
  391. msm_otg_start_host(otg, 0);
  392. otg->host = NULL;
  393. otg->state = OTG_STATE_UNDEFINED;
  394. schedule_work(&motg->sm_work);
  395. } else {
  396. otg->host = NULL;
  397. }
  398. return 0;
  399. }
  400. hcd = bus_to_hcd(host);
  401. hcd->power_budget = motg->pdata->power_budget;
  402. otg->host = host;
  403. dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
  404. /*
  405. * Kick the state machine work, if peripheral is not supported
  406. * or peripheral is already registered with us.
  407. */
  408. if (motg->pdata->mode == USB_HOST || otg->gadget) {
  409. pm_runtime_get_sync(otg->dev);
  410. schedule_work(&motg->sm_work);
  411. }
  412. return 0;
  413. }
  414. static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
  415. {
  416. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  417. struct msm_otg_platform_data *pdata = motg->pdata;
  418. if (!otg->gadget)
  419. return;
  420. if (on) {
  421. dev_dbg(otg->dev, "gadget on\n");
  422. /*
  423. * Some boards have a switch cotrolled by gpio
  424. * to enable/disable internal HUB. Disable internal
  425. * HUB before kicking the gadget.
  426. */
  427. if (pdata->setup_gpio)
  428. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  429. usb_gadget_vbus_connect(otg->gadget);
  430. } else {
  431. dev_dbg(otg->dev, "gadget off\n");
  432. usb_gadget_vbus_disconnect(otg->gadget);
  433. if (pdata->setup_gpio)
  434. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  435. }
  436. }
  437. static int msm_otg_set_peripheral(struct otg_transceiver *otg,
  438. struct usb_gadget *gadget)
  439. {
  440. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  441. /*
  442. * Fail peripheral registration if this board can support
  443. * only host configuration.
  444. */
  445. if (motg->pdata->mode == USB_HOST) {
  446. dev_info(otg->dev, "Peripheral mode is not supported\n");
  447. return -ENODEV;
  448. }
  449. if (!gadget) {
  450. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  451. pm_runtime_get_sync(otg->dev);
  452. msm_otg_start_peripheral(otg, 0);
  453. otg->gadget = NULL;
  454. otg->state = OTG_STATE_UNDEFINED;
  455. schedule_work(&motg->sm_work);
  456. } else {
  457. otg->gadget = NULL;
  458. }
  459. return 0;
  460. }
  461. otg->gadget = gadget;
  462. dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
  463. /*
  464. * Kick the state machine work, if host is not supported
  465. * or host is already registered with us.
  466. */
  467. if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
  468. pm_runtime_get_sync(otg->dev);
  469. schedule_work(&motg->sm_work);
  470. }
  471. return 0;
  472. }
  473. /*
  474. * We support OTG, Peripheral only and Host only configurations. In case
  475. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  476. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  477. * enabled when switch is controlled by user and default mode is supplied
  478. * by board file, which can be changed by userspace later.
  479. */
  480. static void msm_otg_init_sm(struct msm_otg *motg)
  481. {
  482. struct msm_otg_platform_data *pdata = motg->pdata;
  483. u32 otgsc = readl(USB_OTGSC);
  484. switch (pdata->mode) {
  485. case USB_OTG:
  486. if (pdata->otg_control == OTG_PHY_CONTROL) {
  487. if (otgsc & OTGSC_ID)
  488. set_bit(ID, &motg->inputs);
  489. else
  490. clear_bit(ID, &motg->inputs);
  491. if (otgsc & OTGSC_BSV)
  492. set_bit(B_SESS_VLD, &motg->inputs);
  493. else
  494. clear_bit(B_SESS_VLD, &motg->inputs);
  495. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  496. if (pdata->default_mode == USB_HOST) {
  497. clear_bit(ID, &motg->inputs);
  498. } else if (pdata->default_mode == USB_PERIPHERAL) {
  499. set_bit(ID, &motg->inputs);
  500. set_bit(B_SESS_VLD, &motg->inputs);
  501. } else {
  502. set_bit(ID, &motg->inputs);
  503. clear_bit(B_SESS_VLD, &motg->inputs);
  504. }
  505. }
  506. break;
  507. case USB_HOST:
  508. clear_bit(ID, &motg->inputs);
  509. break;
  510. case USB_PERIPHERAL:
  511. set_bit(ID, &motg->inputs);
  512. if (otgsc & OTGSC_BSV)
  513. set_bit(B_SESS_VLD, &motg->inputs);
  514. else
  515. clear_bit(B_SESS_VLD, &motg->inputs);
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. static void msm_otg_sm_work(struct work_struct *w)
  522. {
  523. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  524. struct otg_transceiver *otg = &motg->otg;
  525. switch (otg->state) {
  526. case OTG_STATE_UNDEFINED:
  527. dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
  528. msm_otg_reset(otg);
  529. msm_otg_init_sm(motg);
  530. otg->state = OTG_STATE_B_IDLE;
  531. /* FALL THROUGH */
  532. case OTG_STATE_B_IDLE:
  533. dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
  534. if (!test_bit(ID, &motg->inputs) && otg->host) {
  535. /* disable BSV bit */
  536. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  537. msm_otg_start_host(otg, 1);
  538. otg->state = OTG_STATE_A_HOST;
  539. } else if (test_bit(B_SESS_VLD, &motg->inputs) && otg->gadget) {
  540. msm_otg_start_peripheral(otg, 1);
  541. otg->state = OTG_STATE_B_PERIPHERAL;
  542. }
  543. pm_runtime_put_sync(otg->dev);
  544. break;
  545. case OTG_STATE_B_PERIPHERAL:
  546. dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
  547. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  548. !test_bit(ID, &motg->inputs)) {
  549. msm_otg_start_peripheral(otg, 0);
  550. otg->state = OTG_STATE_B_IDLE;
  551. msm_otg_reset(otg);
  552. schedule_work(w);
  553. }
  554. break;
  555. case OTG_STATE_A_HOST:
  556. dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
  557. if (test_bit(ID, &motg->inputs)) {
  558. msm_otg_start_host(otg, 0);
  559. otg->state = OTG_STATE_B_IDLE;
  560. msm_otg_reset(otg);
  561. schedule_work(w);
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. }
  568. static irqreturn_t msm_otg_irq(int irq, void *data)
  569. {
  570. struct msm_otg *motg = data;
  571. struct otg_transceiver *otg = &motg->otg;
  572. u32 otgsc = 0;
  573. if (atomic_read(&motg->in_lpm)) {
  574. disable_irq_nosync(irq);
  575. motg->async_int = 1;
  576. pm_runtime_get(otg->dev);
  577. return IRQ_HANDLED;
  578. }
  579. otgsc = readl(USB_OTGSC);
  580. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  581. return IRQ_NONE;
  582. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  583. if (otgsc & OTGSC_ID)
  584. set_bit(ID, &motg->inputs);
  585. else
  586. clear_bit(ID, &motg->inputs);
  587. dev_dbg(otg->dev, "ID set/clear\n");
  588. pm_runtime_get_noresume(otg->dev);
  589. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  590. if (otgsc & OTGSC_BSV)
  591. set_bit(B_SESS_VLD, &motg->inputs);
  592. else
  593. clear_bit(B_SESS_VLD, &motg->inputs);
  594. dev_dbg(otg->dev, "BSV set/clear\n");
  595. pm_runtime_get_noresume(otg->dev);
  596. }
  597. writel(otgsc, USB_OTGSC);
  598. schedule_work(&motg->sm_work);
  599. return IRQ_HANDLED;
  600. }
  601. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  602. {
  603. struct msm_otg *motg = s->private;
  604. struct otg_transceiver *otg = &motg->otg;
  605. switch (otg->state) {
  606. case OTG_STATE_A_HOST:
  607. seq_printf(s, "host\n");
  608. break;
  609. case OTG_STATE_B_PERIPHERAL:
  610. seq_printf(s, "peripheral\n");
  611. break;
  612. default:
  613. seq_printf(s, "none\n");
  614. break;
  615. }
  616. return 0;
  617. }
  618. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  619. {
  620. return single_open(file, msm_otg_mode_show, inode->i_private);
  621. }
  622. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  623. size_t count, loff_t *ppos)
  624. {
  625. struct msm_otg *motg = file->private_data;
  626. char buf[16];
  627. struct otg_transceiver *otg = &motg->otg;
  628. int status = count;
  629. enum usb_mode_type req_mode;
  630. memset(buf, 0x00, sizeof(buf));
  631. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  632. status = -EFAULT;
  633. goto out;
  634. }
  635. if (!strncmp(buf, "host", 4)) {
  636. req_mode = USB_HOST;
  637. } else if (!strncmp(buf, "peripheral", 10)) {
  638. req_mode = USB_PERIPHERAL;
  639. } else if (!strncmp(buf, "none", 4)) {
  640. req_mode = USB_NONE;
  641. } else {
  642. status = -EINVAL;
  643. goto out;
  644. }
  645. switch (req_mode) {
  646. case USB_NONE:
  647. switch (otg->state) {
  648. case OTG_STATE_A_HOST:
  649. case OTG_STATE_B_PERIPHERAL:
  650. set_bit(ID, &motg->inputs);
  651. clear_bit(B_SESS_VLD, &motg->inputs);
  652. break;
  653. default:
  654. goto out;
  655. }
  656. break;
  657. case USB_PERIPHERAL:
  658. switch (otg->state) {
  659. case OTG_STATE_B_IDLE:
  660. case OTG_STATE_A_HOST:
  661. set_bit(ID, &motg->inputs);
  662. set_bit(B_SESS_VLD, &motg->inputs);
  663. break;
  664. default:
  665. goto out;
  666. }
  667. break;
  668. case USB_HOST:
  669. switch (otg->state) {
  670. case OTG_STATE_B_IDLE:
  671. case OTG_STATE_B_PERIPHERAL:
  672. clear_bit(ID, &motg->inputs);
  673. break;
  674. default:
  675. goto out;
  676. }
  677. break;
  678. default:
  679. goto out;
  680. }
  681. pm_runtime_get_sync(otg->dev);
  682. schedule_work(&motg->sm_work);
  683. out:
  684. return status;
  685. }
  686. const struct file_operations msm_otg_mode_fops = {
  687. .open = msm_otg_mode_open,
  688. .read = seq_read,
  689. .write = msm_otg_mode_write,
  690. .llseek = seq_lseek,
  691. .release = single_release,
  692. };
  693. static struct dentry *msm_otg_dbg_root;
  694. static struct dentry *msm_otg_dbg_mode;
  695. static int msm_otg_debugfs_init(struct msm_otg *motg)
  696. {
  697. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  698. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  699. return -ENODEV;
  700. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  701. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  702. if (!msm_otg_dbg_mode) {
  703. debugfs_remove(msm_otg_dbg_root);
  704. msm_otg_dbg_root = NULL;
  705. return -ENODEV;
  706. }
  707. return 0;
  708. }
  709. static void msm_otg_debugfs_cleanup(void)
  710. {
  711. debugfs_remove(msm_otg_dbg_mode);
  712. debugfs_remove(msm_otg_dbg_root);
  713. }
  714. static int __init msm_otg_probe(struct platform_device *pdev)
  715. {
  716. int ret = 0;
  717. struct resource *res;
  718. struct msm_otg *motg;
  719. struct otg_transceiver *otg;
  720. dev_info(&pdev->dev, "msm_otg probe\n");
  721. if (!pdev->dev.platform_data) {
  722. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  723. return -ENODEV;
  724. }
  725. motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
  726. if (!motg) {
  727. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  728. return -ENOMEM;
  729. }
  730. motg->pdata = pdev->dev.platform_data;
  731. otg = &motg->otg;
  732. otg->dev = &pdev->dev;
  733. motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
  734. if (IS_ERR(motg->phy_reset_clk)) {
  735. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  736. ret = PTR_ERR(motg->phy_reset_clk);
  737. goto free_motg;
  738. }
  739. motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
  740. if (IS_ERR(motg->clk)) {
  741. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  742. ret = PTR_ERR(motg->clk);
  743. goto put_phy_reset_clk;
  744. }
  745. motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
  746. if (IS_ERR(motg->pclk)) {
  747. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  748. ret = PTR_ERR(motg->pclk);
  749. goto put_clk;
  750. }
  751. /*
  752. * USB core clock is not present on all MSM chips. This
  753. * clock is introduced to remove the dependency on AXI
  754. * bus frequency.
  755. */
  756. motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
  757. if (IS_ERR(motg->core_clk))
  758. motg->core_clk = NULL;
  759. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  760. if (!res) {
  761. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  762. ret = -ENODEV;
  763. goto put_core_clk;
  764. }
  765. motg->regs = ioremap(res->start, resource_size(res));
  766. if (!motg->regs) {
  767. dev_err(&pdev->dev, "ioremap failed\n");
  768. ret = -ENOMEM;
  769. goto put_core_clk;
  770. }
  771. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  772. motg->irq = platform_get_irq(pdev, 0);
  773. if (!motg->irq) {
  774. dev_err(&pdev->dev, "platform_get_irq failed\n");
  775. ret = -ENODEV;
  776. goto free_regs;
  777. }
  778. clk_enable(motg->clk);
  779. clk_enable(motg->pclk);
  780. if (motg->core_clk)
  781. clk_enable(motg->core_clk);
  782. writel(0, USB_USBINTR);
  783. writel(0, USB_OTGSC);
  784. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  785. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  786. "msm_otg", motg);
  787. if (ret) {
  788. dev_err(&pdev->dev, "request irq failed\n");
  789. goto disable_clks;
  790. }
  791. otg->init = msm_otg_reset;
  792. otg->set_host = msm_otg_set_host;
  793. otg->set_peripheral = msm_otg_set_peripheral;
  794. otg->io_ops = &msm_otg_io_ops;
  795. ret = otg_set_transceiver(&motg->otg);
  796. if (ret) {
  797. dev_err(&pdev->dev, "otg_set_transceiver failed\n");
  798. goto free_irq;
  799. }
  800. platform_set_drvdata(pdev, motg);
  801. device_init_wakeup(&pdev->dev, 1);
  802. if (motg->pdata->mode == USB_OTG &&
  803. motg->pdata->otg_control == OTG_USER_CONTROL) {
  804. ret = msm_otg_debugfs_init(motg);
  805. if (ret)
  806. dev_dbg(&pdev->dev, "mode debugfs file is"
  807. "not available\n");
  808. }
  809. pm_runtime_set_active(&pdev->dev);
  810. pm_runtime_enable(&pdev->dev);
  811. return 0;
  812. free_irq:
  813. free_irq(motg->irq, motg);
  814. disable_clks:
  815. clk_disable(motg->pclk);
  816. clk_disable(motg->clk);
  817. free_regs:
  818. iounmap(motg->regs);
  819. put_core_clk:
  820. if (motg->core_clk)
  821. clk_put(motg->core_clk);
  822. clk_put(motg->pclk);
  823. put_clk:
  824. clk_put(motg->clk);
  825. put_phy_reset_clk:
  826. clk_put(motg->phy_reset_clk);
  827. free_motg:
  828. kfree(motg);
  829. return ret;
  830. }
  831. static int __devexit msm_otg_remove(struct platform_device *pdev)
  832. {
  833. struct msm_otg *motg = platform_get_drvdata(pdev);
  834. struct otg_transceiver *otg = &motg->otg;
  835. int cnt = 0;
  836. if (otg->host || otg->gadget)
  837. return -EBUSY;
  838. msm_otg_debugfs_cleanup();
  839. cancel_work_sync(&motg->sm_work);
  840. pm_runtime_resume(&pdev->dev);
  841. device_init_wakeup(&pdev->dev, 0);
  842. pm_runtime_disable(&pdev->dev);
  843. otg_set_transceiver(NULL);
  844. free_irq(motg->irq, motg);
  845. /*
  846. * Put PHY in low power mode.
  847. */
  848. ulpi_read(otg, 0x14);
  849. ulpi_write(otg, 0x08, 0x09);
  850. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  851. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  852. if (readl(USB_PORTSC) & PORTSC_PHCD)
  853. break;
  854. udelay(1);
  855. cnt++;
  856. }
  857. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  858. dev_err(otg->dev, "Unable to suspend PHY\n");
  859. clk_disable(motg->pclk);
  860. clk_disable(motg->clk);
  861. if (motg->core_clk)
  862. clk_disable(motg->core_clk);
  863. iounmap(motg->regs);
  864. pm_runtime_set_suspended(&pdev->dev);
  865. clk_put(motg->phy_reset_clk);
  866. clk_put(motg->pclk);
  867. clk_put(motg->clk);
  868. if (motg->core_clk)
  869. clk_put(motg->core_clk);
  870. kfree(motg);
  871. return 0;
  872. }
  873. #ifdef CONFIG_PM_RUNTIME
  874. static int msm_otg_runtime_idle(struct device *dev)
  875. {
  876. struct msm_otg *motg = dev_get_drvdata(dev);
  877. struct otg_transceiver *otg = &motg->otg;
  878. dev_dbg(dev, "OTG runtime idle\n");
  879. /*
  880. * It is observed some times that a spurious interrupt
  881. * comes when PHY is put into LPM immediately after PHY reset.
  882. * This 1 sec delay also prevents entering into LPM immediately
  883. * after asynchronous interrupt.
  884. */
  885. if (otg->state != OTG_STATE_UNDEFINED)
  886. pm_schedule_suspend(dev, 1000);
  887. return -EAGAIN;
  888. }
  889. static int msm_otg_runtime_suspend(struct device *dev)
  890. {
  891. struct msm_otg *motg = dev_get_drvdata(dev);
  892. dev_dbg(dev, "OTG runtime suspend\n");
  893. return msm_otg_suspend(motg);
  894. }
  895. static int msm_otg_runtime_resume(struct device *dev)
  896. {
  897. struct msm_otg *motg = dev_get_drvdata(dev);
  898. dev_dbg(dev, "OTG runtime resume\n");
  899. return msm_otg_resume(motg);
  900. }
  901. #endif
  902. #ifdef CONFIG_PM_SLEEP
  903. static int msm_otg_pm_suspend(struct device *dev)
  904. {
  905. struct msm_otg *motg = dev_get_drvdata(dev);
  906. dev_dbg(dev, "OTG PM suspend\n");
  907. return msm_otg_suspend(motg);
  908. }
  909. static int msm_otg_pm_resume(struct device *dev)
  910. {
  911. struct msm_otg *motg = dev_get_drvdata(dev);
  912. int ret;
  913. dev_dbg(dev, "OTG PM resume\n");
  914. ret = msm_otg_resume(motg);
  915. if (ret)
  916. return ret;
  917. /*
  918. * Runtime PM Documentation recommends bringing the
  919. * device to full powered state upon resume.
  920. */
  921. pm_runtime_disable(dev);
  922. pm_runtime_set_active(dev);
  923. pm_runtime_enable(dev);
  924. return 0;
  925. }
  926. #endif
  927. #ifdef CONFIG_PM
  928. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  929. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  930. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  931. msm_otg_runtime_idle)
  932. };
  933. #endif
  934. static struct platform_driver msm_otg_driver = {
  935. .remove = __devexit_p(msm_otg_remove),
  936. .driver = {
  937. .name = DRIVER_NAME,
  938. .owner = THIS_MODULE,
  939. #ifdef CONFIG_PM
  940. .pm = &msm_otg_dev_pm_ops,
  941. #endif
  942. },
  943. };
  944. static int __init msm_otg_init(void)
  945. {
  946. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  947. }
  948. static void __exit msm_otg_exit(void)
  949. {
  950. platform_driver_unregister(&msm_otg_driver);
  951. }
  952. module_init(msm_otg_init);
  953. module_exit(msm_otg_exit);
  954. MODULE_LICENSE("GPL v2");
  955. MODULE_DESCRIPTION("MSM USB transceiver driver");