musb_virthub.c 11 KB

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