musb_virthub.c 11 KB

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