musb_virthub.c 11 KB

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