host.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * host.c - ChipIdea USB host controller driver
  4. *
  5. * Copyright (c) 2012 Intel Corporation
  6. *
  7. * Author: Alexander Shishkin
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <linux/usb.h>
  12. #include <linux/usb/hcd.h>
  13. #include <linux/usb/chipidea.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include "../host/ehci.h"
  17. #include "ci.h"
  18. #include "bits.h"
  19. #include "host.h"
  20. static struct hc_driver __read_mostly ci_ehci_hc_driver;
  21. static int (*orig_bus_suspend)(struct usb_hcd *hcd);
  22. struct ehci_ci_priv {
  23. struct regulator *reg_vbus;
  24. bool enabled;
  25. };
  26. struct ci_hdrc_dma_aligned_buffer {
  27. void *original_buffer;
  28. u8 data[];
  29. };
  30. static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
  31. {
  32. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  33. struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
  34. struct device *dev = hcd->self.controller;
  35. struct ci_hdrc *ci = dev_get_drvdata(dev);
  36. int ret = 0;
  37. int port = HCS_N_PORTS(ehci->hcs_params);
  38. if (priv->reg_vbus && enable != priv->enabled) {
  39. if (port > 1) {
  40. dev_warn(dev,
  41. "Not support multi-port regulator control\n");
  42. return 0;
  43. }
  44. if (enable)
  45. ret = regulator_enable(priv->reg_vbus);
  46. else
  47. ret = regulator_disable(priv->reg_vbus);
  48. if (ret) {
  49. dev_err(dev,
  50. "Failed to %s vbus regulator, ret=%d\n",
  51. enable ? "enable" : "disable", ret);
  52. return ret;
  53. }
  54. priv->enabled = enable;
  55. }
  56. if (ci->platdata->flags & CI_HDRC_PHY_VBUS_CONTROL) {
  57. if (enable)
  58. usb_phy_vbus_on(ci->usb_phy);
  59. else
  60. usb_phy_vbus_off(ci->usb_phy);
  61. }
  62. if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
  63. /*
  64. * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
  65. * As HSIC is always HS, this should be safe for others.
  66. */
  67. hw_port_test_set(ci, 5);
  68. hw_port_test_set(ci, 0);
  69. }
  70. return 0;
  71. };
  72. static int ehci_ci_reset(struct usb_hcd *hcd)
  73. {
  74. struct device *dev = hcd->self.controller;
  75. struct ci_hdrc *ci = dev_get_drvdata(dev);
  76. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  77. int ret;
  78. ret = ehci_setup(hcd);
  79. if (ret)
  80. return ret;
  81. ehci->need_io_watchdog = 0;
  82. if (ci->platdata->notify_event) {
  83. ret = ci->platdata->notify_event(ci,
  84. CI_HDRC_CONTROLLER_RESET_EVENT);
  85. if (ret)
  86. return ret;
  87. }
  88. ci_platform_configure(ci);
  89. return ret;
  90. }
  91. static const struct ehci_driver_overrides ehci_ci_overrides = {
  92. .extra_priv_size = sizeof(struct ehci_ci_priv),
  93. .port_power = ehci_ci_portpower,
  94. .reset = ehci_ci_reset,
  95. };
  96. static irqreturn_t host_irq(struct ci_hdrc *ci)
  97. {
  98. return usb_hcd_irq(ci->irq, ci->hcd);
  99. }
  100. static int host_start(struct ci_hdrc *ci)
  101. {
  102. struct usb_hcd *hcd;
  103. struct ehci_hcd *ehci;
  104. struct ehci_ci_priv *priv;
  105. int ret;
  106. if (usb_disabled())
  107. return -ENODEV;
  108. hcd = __usb_create_hcd(&ci_ehci_hc_driver, ci->dev->parent,
  109. ci->dev, dev_name(ci->dev), NULL);
  110. if (!hcd)
  111. return -ENOMEM;
  112. dev_set_drvdata(ci->dev, ci);
  113. hcd->rsrc_start = ci->hw_bank.phys;
  114. hcd->rsrc_len = ci->hw_bank.size;
  115. hcd->regs = ci->hw_bank.abs;
  116. hcd->has_tt = 1;
  117. hcd->power_budget = ci->platdata->power_budget;
  118. hcd->tpl_support = ci->platdata->tpl_support;
  119. if (ci->phy || ci->usb_phy) {
  120. hcd->skip_phy_initialization = 1;
  121. if (ci->usb_phy)
  122. hcd->usb_phy = ci->usb_phy;
  123. }
  124. ehci = hcd_to_ehci(hcd);
  125. ehci->caps = ci->hw_bank.cap;
  126. ehci->has_hostpc = ci->hw_bank.lpm;
  127. ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
  128. ehci->imx28_write_fix = ci->imx28_write_fix;
  129. ehci->has_ci_pec_bug = ci->has_portsc_pec_bug;
  130. priv = (struct ehci_ci_priv *)ehci->priv;
  131. priv->reg_vbus = NULL;
  132. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
  133. if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
  134. ret = regulator_enable(ci->platdata->reg_vbus);
  135. if (ret) {
  136. dev_err(ci->dev,
  137. "Failed to enable vbus regulator, ret=%d\n",
  138. ret);
  139. goto put_hcd;
  140. }
  141. } else {
  142. priv->reg_vbus = ci->platdata->reg_vbus;
  143. }
  144. }
  145. if (ci->platdata->pins_host)
  146. pinctrl_select_state(ci->platdata->pctl,
  147. ci->platdata->pins_host);
  148. ci->hcd = hcd;
  149. ret = usb_add_hcd(hcd, 0, 0);
  150. if (ret) {
  151. ci->hcd = NULL;
  152. goto disable_reg;
  153. } else {
  154. struct usb_otg *otg = &ci->otg;
  155. if (ci_otg_is_fsm_mode(ci)) {
  156. otg->host = &hcd->self;
  157. hcd->self.otg_port = 1;
  158. }
  159. if (ci->platdata->notify_event &&
  160. (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC))
  161. ci->platdata->notify_event
  162. (ci, CI_HDRC_IMX_HSIC_ACTIVE_EVENT);
  163. }
  164. return ret;
  165. disable_reg:
  166. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  167. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  168. regulator_disable(ci->platdata->reg_vbus);
  169. put_hcd:
  170. usb_put_hcd(hcd);
  171. return ret;
  172. }
  173. static void host_stop(struct ci_hdrc *ci)
  174. {
  175. struct usb_hcd *hcd = ci->hcd;
  176. if (hcd) {
  177. if (ci->platdata->notify_event)
  178. ci->platdata->notify_event(ci,
  179. CI_HDRC_CONTROLLER_STOPPED_EVENT);
  180. usb_remove_hcd(hcd);
  181. ci->role = CI_ROLE_END;
  182. synchronize_irq(ci->irq);
  183. usb_put_hcd(hcd);
  184. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  185. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  186. regulator_disable(ci->platdata->reg_vbus);
  187. }
  188. ci->hcd = NULL;
  189. ci->otg.host = NULL;
  190. if (ci->platdata->pins_host && ci->platdata->pins_default)
  191. pinctrl_select_state(ci->platdata->pctl,
  192. ci->platdata->pins_default);
  193. }
  194. void ci_hdrc_host_destroy(struct ci_hdrc *ci)
  195. {
  196. if (ci->role == CI_ROLE_HOST && ci->hcd)
  197. host_stop(ci);
  198. }
  199. /* The below code is based on tegra ehci driver */
  200. static int ci_ehci_hub_control(
  201. struct usb_hcd *hcd,
  202. u16 typeReq,
  203. u16 wValue,
  204. u16 wIndex,
  205. char *buf,
  206. u16 wLength
  207. )
  208. {
  209. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  210. unsigned int ports = HCS_N_PORTS(ehci->hcs_params);
  211. u32 __iomem *status_reg;
  212. u32 temp, port_index;
  213. unsigned long flags;
  214. int retval = 0;
  215. bool done = false;
  216. struct device *dev = hcd->self.controller;
  217. struct ci_hdrc *ci = dev_get_drvdata(dev);
  218. port_index = wIndex & 0xff;
  219. port_index -= (port_index > 0);
  220. status_reg = &ehci->regs->port_status[port_index];
  221. spin_lock_irqsave(&ehci->lock, flags);
  222. if (ci->platdata->hub_control) {
  223. retval = ci->platdata->hub_control(ci, typeReq, wValue, wIndex,
  224. buf, wLength, &done, &flags);
  225. if (done)
  226. goto done;
  227. }
  228. if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
  229. if (!wIndex || wIndex > ports) {
  230. retval = -EPIPE;
  231. goto done;
  232. }
  233. temp = ehci_readl(ehci, status_reg);
  234. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
  235. retval = -EPIPE;
  236. goto done;
  237. }
  238. temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
  239. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  240. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  241. /*
  242. * If a transaction is in progress, there may be a delay in
  243. * suspending the port. Poll until the port is suspended.
  244. */
  245. if (ehci_handshake(ehci, status_reg, PORT_SUSPEND,
  246. PORT_SUSPEND, 5000))
  247. ehci_err(ehci, "timeout waiting for SUSPEND\n");
  248. if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
  249. if (ci->platdata->notify_event)
  250. ci->platdata->notify_event(ci,
  251. CI_HDRC_IMX_HSIC_SUSPEND_EVENT);
  252. temp = ehci_readl(ehci, status_reg);
  253. temp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
  254. ehci_writel(ehci, temp, status_reg);
  255. }
  256. set_bit(port_index, &ehci->suspended_ports);
  257. goto done;
  258. }
  259. /*
  260. * After resume has finished, it needs do some post resume
  261. * operation for some SoCs.
  262. */
  263. else if (typeReq == ClearPortFeature &&
  264. wValue == USB_PORT_FEAT_C_SUSPEND) {
  265. /* Make sure the resume has finished, it should be finished */
  266. if (ehci_handshake(ehci, status_reg, PORT_RESUME, 0, 25000))
  267. ehci_err(ehci, "timeout waiting for resume\n");
  268. }
  269. spin_unlock_irqrestore(&ehci->lock, flags);
  270. /* Handle the hub control events here */
  271. return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  272. done:
  273. spin_unlock_irqrestore(&ehci->lock, flags);
  274. return retval;
  275. }
  276. static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
  277. {
  278. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  279. struct device *dev = hcd->self.controller;
  280. struct ci_hdrc *ci = dev_get_drvdata(dev);
  281. int port;
  282. u32 tmp;
  283. int ret = orig_bus_suspend(hcd);
  284. if (ret)
  285. return ret;
  286. port = HCS_N_PORTS(ehci->hcs_params);
  287. while (port--) {
  288. u32 __iomem *reg = &ehci->regs->port_status[port];
  289. u32 portsc = ehci_readl(ehci, reg);
  290. if (portsc & PORT_CONNECT) {
  291. /*
  292. * For chipidea, the resume signal will be ended
  293. * automatically, so for remote wakeup case, the
  294. * usbcmd.rs may not be set before the resume has
  295. * ended if other resume paths consumes too much
  296. * time (~24ms), in that case, the SOF will not
  297. * send out within 3ms after resume ends, then the
  298. * high speed device will enter full speed mode.
  299. */
  300. tmp = ehci_readl(ehci, &ehci->regs->command);
  301. tmp |= CMD_RUN;
  302. ehci_writel(ehci, tmp, &ehci->regs->command);
  303. /*
  304. * It needs a short delay between set RS bit and PHCD.
  305. */
  306. usleep_range(150, 200);
  307. /*
  308. * Need to clear WKCN and WKOC for imx HSIC,
  309. * otherwise, there will be wakeup event.
  310. */
  311. if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
  312. tmp = ehci_readl(ehci, reg);
  313. tmp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
  314. ehci_writel(ehci, tmp, reg);
  315. }
  316. break;
  317. }
  318. }
  319. return 0;
  320. }
  321. static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb, bool copy_back)
  322. {
  323. struct ci_hdrc_dma_aligned_buffer *temp;
  324. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  325. return;
  326. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  327. temp = container_of(urb->transfer_buffer,
  328. struct ci_hdrc_dma_aligned_buffer, data);
  329. urb->transfer_buffer = temp->original_buffer;
  330. if (copy_back && usb_urb_dir_in(urb)) {
  331. size_t length;
  332. if (usb_pipeisoc(urb->pipe))
  333. length = urb->transfer_buffer_length;
  334. else
  335. length = urb->actual_length;
  336. memcpy(temp->original_buffer, temp->data, length);
  337. }
  338. kfree(temp);
  339. }
  340. static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  341. {
  342. struct ci_hdrc_dma_aligned_buffer *temp;
  343. if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0)
  344. return 0;
  345. if (IS_ALIGNED((uintptr_t)urb->transfer_buffer, 4)
  346. && IS_ALIGNED(urb->transfer_buffer_length, 4))
  347. return 0;
  348. temp = kmalloc(sizeof(*temp) + ALIGN(urb->transfer_buffer_length, 4), mem_flags);
  349. if (!temp)
  350. return -ENOMEM;
  351. if (usb_urb_dir_out(urb))
  352. memcpy(temp->data, urb->transfer_buffer,
  353. urb->transfer_buffer_length);
  354. temp->original_buffer = urb->transfer_buffer;
  355. urb->transfer_buffer = temp->data;
  356. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  357. return 0;
  358. }
  359. static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  360. gfp_t mem_flags)
  361. {
  362. int ret;
  363. ret = ci_hdrc_alloc_dma_aligned_buffer(urb, mem_flags);
  364. if (ret)
  365. return ret;
  366. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  367. if (ret)
  368. ci_hdrc_free_dma_aligned_buffer(urb, false);
  369. return ret;
  370. }
  371. static void ci_hdrc_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  372. {
  373. usb_hcd_unmap_urb_for_dma(hcd, urb);
  374. ci_hdrc_free_dma_aligned_buffer(urb, true);
  375. }
  376. #ifdef CONFIG_PM_SLEEP
  377. static void ci_hdrc_host_suspend(struct ci_hdrc *ci)
  378. {
  379. ehci_suspend(ci->hcd, device_may_wakeup(ci->dev));
  380. }
  381. static void ci_hdrc_host_resume(struct ci_hdrc *ci, bool power_lost)
  382. {
  383. ehci_resume(ci->hcd, power_lost);
  384. }
  385. #endif
  386. int ci_hdrc_host_init(struct ci_hdrc *ci)
  387. {
  388. struct ci_role_driver *rdrv;
  389. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
  390. return -ENXIO;
  391. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  392. if (!rdrv)
  393. return -ENOMEM;
  394. rdrv->start = host_start;
  395. rdrv->stop = host_stop;
  396. #ifdef CONFIG_PM_SLEEP
  397. rdrv->suspend = ci_hdrc_host_suspend;
  398. rdrv->resume = ci_hdrc_host_resume;
  399. #endif
  400. rdrv->irq = host_irq;
  401. rdrv->name = "host";
  402. ci->roles[CI_ROLE_HOST] = rdrv;
  403. if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA) {
  404. ci_ehci_hc_driver.map_urb_for_dma = ci_hdrc_map_urb_for_dma;
  405. ci_ehci_hc_driver.unmap_urb_for_dma = ci_hdrc_unmap_urb_for_dma;
  406. }
  407. return 0;
  408. }
  409. void ci_hdrc_host_driver_init(void)
  410. {
  411. ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
  412. orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
  413. ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
  414. ci_ehci_hc_driver.hub_control = ci_ehci_hub_control;
  415. }