musb_virthub.c 12 KB

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