musb_virthub.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * MUSB OTG driver virtual root hub support
  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. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/sched.h>
  37. #include <linux/errno.h>
  38. #include <linux/time.h>
  39. #include <linux/timer.h>
  40. #include <asm/unaligned.h>
  41. #include "musb_core.h"
  42. void musb_host_finish_resume(struct work_struct *work)
  43. {
  44. struct musb *musb;
  45. unsigned long flags;
  46. u8 power;
  47. musb = container_of(work, struct musb, finish_resume_work.work);
  48. spin_lock_irqsave(&musb->lock, flags);
  49. power = musb_readb(musb->mregs, MUSB_POWER);
  50. power &= ~MUSB_POWER_RESUME;
  51. musb_dbg(musb, "root port resume stopped, power %02x", power);
  52. musb_writeb(musb->mregs, MUSB_POWER, power);
  53. /*
  54. * ISSUE: DaVinci (RTL 1.300) disconnects after
  55. * resume of high speed peripherals (but not full
  56. * speed ones).
  57. */
  58. musb->is_active = 1;
  59. musb->port1_status &= ~(USB_PORT_STAT_SUSPEND | MUSB_PORT_STAT_RESUME);
  60. musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
  61. usb_hcd_poll_rh_status(musb->hcd);
  62. /* NOTE: it might really be A_WAIT_BCON ... */
  63. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  64. spin_unlock_irqrestore(&musb->lock, flags);
  65. }
  66. int musb_port_suspend(struct musb *musb, bool do_suspend)
  67. {
  68. struct usb_otg *otg = musb->xceiv->otg;
  69. u8 power;
  70. void __iomem *mbase = musb->mregs;
  71. if (!is_host_active(musb))
  72. return 0;
  73. /* NOTE: this doesn't necessarily put PHY into low power mode,
  74. * turning off its clock; that's a function of PHY integration and
  75. * MUSB_POWER_ENSUSPEND. PHY may need a clock (sigh) to detect
  76. * SE0 changing to connect (J) or wakeup (K) states.
  77. */
  78. power = musb_readb(mbase, MUSB_POWER);
  79. if (do_suspend) {
  80. int retries = 10000;
  81. if (power & MUSB_POWER_RESUME)
  82. return -EBUSY;
  83. if (!(power & MUSB_POWER_SUSPENDM)) {
  84. power |= MUSB_POWER_SUSPENDM;
  85. musb_writeb(mbase, MUSB_POWER, power);
  86. /* Needed for OPT A tests */
  87. power = musb_readb(mbase, MUSB_POWER);
  88. while (power & MUSB_POWER_SUSPENDM) {
  89. power = musb_readb(mbase, MUSB_POWER);
  90. if (retries-- < 1)
  91. break;
  92. }
  93. }
  94. musb_dbg(musb, "Root port suspended, power %02x", power);
  95. musb->port1_status |= USB_PORT_STAT_SUSPEND;
  96. switch (musb->xceiv->otg->state) {
  97. case OTG_STATE_A_HOST:
  98. musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
  99. musb->is_active = otg->host->b_hnp_enable;
  100. if (musb->is_active)
  101. mod_timer(&musb->otg_timer, jiffies
  102. + msecs_to_jiffies(
  103. OTG_TIME_A_AIDL_BDIS));
  104. musb_platform_try_idle(musb, 0);
  105. break;
  106. case OTG_STATE_B_HOST:
  107. musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
  108. musb->is_active = otg->host->b_hnp_enable;
  109. musb_platform_try_idle(musb, 0);
  110. break;
  111. default:
  112. musb_dbg(musb, "bogus rh suspend? %s",
  113. usb_otg_state_string(musb->xceiv->otg->state));
  114. }
  115. } else if (power & MUSB_POWER_SUSPENDM) {
  116. power &= ~MUSB_POWER_SUSPENDM;
  117. power |= MUSB_POWER_RESUME;
  118. musb_writeb(mbase, MUSB_POWER, power);
  119. musb_dbg(musb, "Root port resuming, power %02x", power);
  120. /* later, GetPortStatus will stop RESUME signaling */
  121. musb->port1_status |= MUSB_PORT_STAT_RESUME;
  122. schedule_delayed_work(&musb->finish_resume_work,
  123. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  124. }
  125. return 0;
  126. }
  127. void musb_port_reset(struct musb *musb, bool do_reset)
  128. {
  129. u8 power;
  130. void __iomem *mbase = musb->mregs;
  131. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE) {
  132. musb_dbg(musb, "HNP: Returning from HNP; no hub reset from b_idle");
  133. musb->port1_status &= ~USB_PORT_STAT_RESET;
  134. return;
  135. }
  136. if (!is_host_active(musb))
  137. return;
  138. /* NOTE: caller guarantees it will turn off the reset when
  139. * the appropriate amount of time has passed
  140. */
  141. power = musb_readb(mbase, MUSB_POWER);
  142. if (do_reset) {
  143. /*
  144. * If RESUME is set, we must make sure it stays minimum 20 ms.
  145. * Then we must clear RESUME and wait a bit to let musb start
  146. * generating SOFs. If we don't do this, OPT HS A 6.8 tests
  147. * fail with "Error! Did not receive an SOF before suspend
  148. * detected".
  149. */
  150. if (power & MUSB_POWER_RESUME) {
  151. long remain = (unsigned long) musb->rh_timer - jiffies;
  152. if (musb->rh_timer > 0 && remain > 0) {
  153. /* take into account the minimum delay after resume */
  154. schedule_delayed_work(
  155. &musb->deassert_reset_work, remain);
  156. return;
  157. }
  158. musb_writeb(mbase, MUSB_POWER,
  159. power & ~MUSB_POWER_RESUME);
  160. /* Give the core 1 ms to clear MUSB_POWER_RESUME */
  161. schedule_delayed_work(&musb->deassert_reset_work,
  162. msecs_to_jiffies(1));
  163. return;
  164. }
  165. power &= 0xf0;
  166. musb_writeb(mbase, MUSB_POWER,
  167. power | MUSB_POWER_RESET);
  168. musb->port1_status |= USB_PORT_STAT_RESET;
  169. musb->port1_status &= ~USB_PORT_STAT_ENABLE;
  170. schedule_delayed_work(&musb->deassert_reset_work,
  171. msecs_to_jiffies(50));
  172. } else {
  173. musb_dbg(musb, "root port reset stopped");
  174. musb_platform_pre_root_reset_end(musb);
  175. musb_writeb(mbase, MUSB_POWER,
  176. power & ~MUSB_POWER_RESET);
  177. musb_platform_post_root_reset_end(musb);
  178. power = musb_readb(mbase, MUSB_POWER);
  179. if (power & MUSB_POWER_HSMODE) {
  180. musb_dbg(musb, "high-speed device connected");
  181. musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
  182. }
  183. musb->port1_status &= ~USB_PORT_STAT_RESET;
  184. musb->port1_status |= USB_PORT_STAT_ENABLE
  185. | (USB_PORT_STAT_C_RESET << 16)
  186. | (USB_PORT_STAT_C_ENABLE << 16);
  187. usb_hcd_poll_rh_status(musb->hcd);
  188. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  189. }
  190. }
  191. void musb_root_disconnect(struct musb *musb)
  192. {
  193. struct usb_otg *otg = musb->xceiv->otg;
  194. musb->port1_status = USB_PORT_STAT_POWER
  195. | (USB_PORT_STAT_C_CONNECTION << 16);
  196. usb_hcd_poll_rh_status(musb->hcd);
  197. musb->is_active = 0;
  198. switch (musb->xceiv->otg->state) {
  199. case OTG_STATE_A_SUSPEND:
  200. if (otg->host->b_hnp_enable) {
  201. musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
  202. musb->g.is_a_peripheral = 1;
  203. break;
  204. }
  205. /* FALLTHROUGH */
  206. case OTG_STATE_A_HOST:
  207. musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
  208. musb->is_active = 0;
  209. break;
  210. case OTG_STATE_A_WAIT_VFALL:
  211. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  212. break;
  213. default:
  214. musb_dbg(musb, "host disconnect (%s)",
  215. usb_otg_state_string(musb->xceiv->otg->state));
  216. }
  217. }
  218. EXPORT_SYMBOL_GPL(musb_root_disconnect);
  219. /*---------------------------------------------------------------------*/
  220. /* Caller may or may not hold musb->lock */
  221. int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
  222. {
  223. struct musb *musb = hcd_to_musb(hcd);
  224. int retval = 0;
  225. /* called in_irq() via usb_hcd_poll_rh_status() */
  226. if (musb->port1_status & 0xffff0000) {
  227. *buf = 0x02;
  228. retval = 1;
  229. }
  230. return retval;
  231. }
  232. static int musb_has_gadget(struct musb *musb)
  233. {
  234. /*
  235. * In host-only mode we start a connection right away. In OTG mode
  236. * we have to wait until we loaded a gadget. We don't really need a
  237. * gadget if we operate as a host but we should not start a session
  238. * as a device without a gadget or else we explode.
  239. */
  240. #ifdef CONFIG_USB_MUSB_HOST
  241. return 1;
  242. #else
  243. return musb->port_mode == MUSB_PORT_MODE_HOST;
  244. #endif
  245. }
  246. int musb_hub_control(
  247. struct usb_hcd *hcd,
  248. u16 typeReq,
  249. u16 wValue,
  250. u16 wIndex,
  251. char *buf,
  252. u16 wLength)
  253. {
  254. struct musb *musb = hcd_to_musb(hcd);
  255. u32 temp;
  256. int retval = 0;
  257. unsigned long flags;
  258. bool start_musb = false;
  259. spin_lock_irqsave(&musb->lock, flags);
  260. if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
  261. spin_unlock_irqrestore(&musb->lock, flags);
  262. return -ESHUTDOWN;
  263. }
  264. /* hub features: always zero, setting is a NOP
  265. * port features: reported, sometimes updated when host is active
  266. * no indicators
  267. */
  268. switch (typeReq) {
  269. case ClearHubFeature:
  270. case SetHubFeature:
  271. switch (wValue) {
  272. case C_HUB_OVER_CURRENT:
  273. case C_HUB_LOCAL_POWER:
  274. break;
  275. default:
  276. goto error;
  277. }
  278. break;
  279. case ClearPortFeature:
  280. if ((wIndex & 0xff) != 1)
  281. goto error;
  282. switch (wValue) {
  283. case USB_PORT_FEAT_ENABLE:
  284. break;
  285. case USB_PORT_FEAT_SUSPEND:
  286. musb_port_suspend(musb, false);
  287. break;
  288. case USB_PORT_FEAT_POWER:
  289. if (!hcd->self.is_b_host)
  290. musb_platform_set_vbus(musb, 0);
  291. break;
  292. case USB_PORT_FEAT_C_CONNECTION:
  293. case USB_PORT_FEAT_C_ENABLE:
  294. case USB_PORT_FEAT_C_OVER_CURRENT:
  295. case USB_PORT_FEAT_C_RESET:
  296. case USB_PORT_FEAT_C_SUSPEND:
  297. break;
  298. default:
  299. goto error;
  300. }
  301. musb_dbg(musb, "clear feature %d", wValue);
  302. musb->port1_status &= ~(1 << wValue);
  303. break;
  304. case GetHubDescriptor:
  305. {
  306. struct usb_hub_descriptor *desc = (void *)buf;
  307. desc->bDescLength = 9;
  308. desc->bDescriptorType = USB_DT_HUB;
  309. desc->bNbrPorts = 1;
  310. desc->wHubCharacteristics = cpu_to_le16(
  311. HUB_CHAR_INDV_PORT_LPSM /* per-port power switching */
  312. | HUB_CHAR_NO_OCPM /* no overcurrent reporting */
  313. );
  314. desc->bPwrOn2PwrGood = 5; /* msec/2 */
  315. desc->bHubContrCurrent = 0;
  316. /* workaround bogus struct definition */
  317. desc->u.hs.DeviceRemovable[0] = 0x02; /* port 1 */
  318. desc->u.hs.DeviceRemovable[1] = 0xff;
  319. }
  320. break;
  321. case GetHubStatus:
  322. temp = 0;
  323. *(__le32 *) buf = cpu_to_le32(temp);
  324. break;
  325. case GetPortStatus:
  326. if (wIndex != 1)
  327. goto error;
  328. put_unaligned(cpu_to_le32(musb->port1_status
  329. & ~MUSB_PORT_STAT_RESUME),
  330. (__le32 *) buf);
  331. /* port change status is more interesting */
  332. musb_dbg(musb, "port status %08x", musb->port1_status);
  333. break;
  334. case SetPortFeature:
  335. if ((wIndex & 0xff) != 1)
  336. goto error;
  337. switch (wValue) {
  338. case USB_PORT_FEAT_POWER:
  339. /* NOTE: this controller has a strange state machine
  340. * that involves "requesting sessions" according to
  341. * magic side effects from incompletely-described
  342. * rules about startup...
  343. *
  344. * This call is what really starts the host mode; be
  345. * very careful about side effects if you reorder any
  346. * initialization logic, e.g. for OTG, or change any
  347. * logic relating to VBUS power-up.
  348. */
  349. if (!hcd->self.is_b_host && musb_has_gadget(musb))
  350. start_musb = true;
  351. break;
  352. case USB_PORT_FEAT_RESET:
  353. musb_port_reset(musb, true);
  354. break;
  355. case USB_PORT_FEAT_SUSPEND:
  356. musb_port_suspend(musb, true);
  357. break;
  358. case USB_PORT_FEAT_TEST:
  359. if (unlikely(is_host_active(musb)))
  360. goto error;
  361. wIndex >>= 8;
  362. switch (wIndex) {
  363. case 1:
  364. pr_debug("TEST_J\n");
  365. temp = MUSB_TEST_J;
  366. break;
  367. case 2:
  368. pr_debug("TEST_K\n");
  369. temp = MUSB_TEST_K;
  370. break;
  371. case 3:
  372. pr_debug("TEST_SE0_NAK\n");
  373. temp = MUSB_TEST_SE0_NAK;
  374. break;
  375. case 4:
  376. pr_debug("TEST_PACKET\n");
  377. temp = MUSB_TEST_PACKET;
  378. musb_load_testpacket(musb);
  379. break;
  380. case 5:
  381. pr_debug("TEST_FORCE_ENABLE\n");
  382. temp = MUSB_TEST_FORCE_HOST
  383. | MUSB_TEST_FORCE_HS;
  384. musb_writeb(musb->mregs, MUSB_DEVCTL,
  385. MUSB_DEVCTL_SESSION);
  386. break;
  387. case 6:
  388. pr_debug("TEST_FIFO_ACCESS\n");
  389. temp = MUSB_TEST_FIFO_ACCESS;
  390. break;
  391. default:
  392. goto error;
  393. }
  394. musb_writeb(musb->mregs, MUSB_TESTMODE, temp);
  395. break;
  396. default:
  397. goto error;
  398. }
  399. musb_dbg(musb, "set feature %d", wValue);
  400. musb->port1_status |= 1 << wValue;
  401. break;
  402. default:
  403. error:
  404. /* "protocol stall" on error */
  405. retval = -EPIPE;
  406. }
  407. spin_unlock_irqrestore(&musb->lock, flags);
  408. if (start_musb)
  409. musb_start(musb);
  410. return retval;
  411. }