musb_virthub.c 11 KB

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