host.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. priv = (struct ehci_ci_priv *)ehci->priv;
  130. priv->reg_vbus = NULL;
  131. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
  132. if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
  133. ret = regulator_enable(ci->platdata->reg_vbus);
  134. if (ret) {
  135. dev_err(ci->dev,
  136. "Failed to enable vbus regulator, ret=%d\n",
  137. ret);
  138. goto put_hcd;
  139. }
  140. } else {
  141. priv->reg_vbus = ci->platdata->reg_vbus;
  142. }
  143. }
  144. if (ci->platdata->pins_host)
  145. pinctrl_select_state(ci->platdata->pctl,
  146. ci->platdata->pins_host);
  147. ci->hcd = hcd;
  148. ret = usb_add_hcd(hcd, 0, 0);
  149. if (ret) {
  150. ci->hcd = NULL;
  151. goto disable_reg;
  152. } else {
  153. struct usb_otg *otg = &ci->otg;
  154. if (ci_otg_is_fsm_mode(ci)) {
  155. otg->host = &hcd->self;
  156. hcd->self.otg_port = 1;
  157. }
  158. if (ci->platdata->notify_event &&
  159. (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC))
  160. ci->platdata->notify_event
  161. (ci, CI_HDRC_IMX_HSIC_ACTIVE_EVENT);
  162. }
  163. return ret;
  164. disable_reg:
  165. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  166. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  167. regulator_disable(ci->platdata->reg_vbus);
  168. put_hcd:
  169. usb_put_hcd(hcd);
  170. return ret;
  171. }
  172. static void host_stop(struct ci_hdrc *ci)
  173. {
  174. struct usb_hcd *hcd = ci->hcd;
  175. if (hcd) {
  176. if (ci->platdata->notify_event)
  177. ci->platdata->notify_event(ci,
  178. CI_HDRC_CONTROLLER_STOPPED_EVENT);
  179. usb_remove_hcd(hcd);
  180. ci->role = CI_ROLE_END;
  181. synchronize_irq(ci->irq);
  182. usb_put_hcd(hcd);
  183. if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
  184. (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
  185. regulator_disable(ci->platdata->reg_vbus);
  186. }
  187. ci->hcd = NULL;
  188. ci->otg.host = NULL;
  189. if (ci->platdata->pins_host && ci->platdata->pins_default)
  190. pinctrl_select_state(ci->platdata->pctl,
  191. ci->platdata->pins_default);
  192. }
  193. void ci_hdrc_host_destroy(struct ci_hdrc *ci)
  194. {
  195. if (ci->role == CI_ROLE_HOST && ci->hcd)
  196. host_stop(ci);
  197. }
  198. /* The below code is based on tegra ehci driver */
  199. static int ci_ehci_hub_control(
  200. struct usb_hcd *hcd,
  201. u16 typeReq,
  202. u16 wValue,
  203. u16 wIndex,
  204. char *buf,
  205. u16 wLength
  206. )
  207. {
  208. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  209. unsigned int ports = HCS_N_PORTS(ehci->hcs_params);
  210. u32 __iomem *status_reg;
  211. u32 temp, port_index;
  212. unsigned long flags;
  213. int retval = 0;
  214. bool done = false;
  215. struct device *dev = hcd->self.controller;
  216. struct ci_hdrc *ci = dev_get_drvdata(dev);
  217. port_index = wIndex & 0xff;
  218. port_index -= (port_index > 0);
  219. status_reg = &ehci->regs->port_status[port_index];
  220. spin_lock_irqsave(&ehci->lock, flags);
  221. if (ci->platdata->hub_control) {
  222. retval = ci->platdata->hub_control(ci, typeReq, wValue, wIndex,
  223. buf, wLength, &done, &flags);
  224. if (done)
  225. goto done;
  226. }
  227. if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
  228. if (!wIndex || wIndex > ports) {
  229. retval = -EPIPE;
  230. goto done;
  231. }
  232. temp = ehci_readl(ehci, status_reg);
  233. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
  234. retval = -EPIPE;
  235. goto done;
  236. }
  237. temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
  238. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  239. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  240. /*
  241. * If a transaction is in progress, there may be a delay in
  242. * suspending the port. Poll until the port is suspended.
  243. */
  244. if (ehci_handshake(ehci, status_reg, PORT_SUSPEND,
  245. PORT_SUSPEND, 5000))
  246. ehci_err(ehci, "timeout waiting for SUSPEND\n");
  247. if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
  248. if (ci->platdata->notify_event)
  249. ci->platdata->notify_event(ci,
  250. CI_HDRC_IMX_HSIC_SUSPEND_EVENT);
  251. temp = ehci_readl(ehci, status_reg);
  252. temp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
  253. ehci_writel(ehci, temp, status_reg);
  254. }
  255. set_bit(port_index, &ehci->suspended_ports);
  256. goto done;
  257. }
  258. /*
  259. * After resume has finished, it needs do some post resume
  260. * operation for some SoCs.
  261. */
  262. else if (typeReq == ClearPortFeature &&
  263. wValue == USB_PORT_FEAT_C_SUSPEND) {
  264. /* Make sure the resume has finished, it should be finished */
  265. if (ehci_handshake(ehci, status_reg, PORT_RESUME, 0, 25000))
  266. ehci_err(ehci, "timeout waiting for resume\n");
  267. }
  268. spin_unlock_irqrestore(&ehci->lock, flags);
  269. /* Handle the hub control events here */
  270. return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  271. done:
  272. spin_unlock_irqrestore(&ehci->lock, flags);
  273. return retval;
  274. }
  275. static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
  276. {
  277. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  278. struct device *dev = hcd->self.controller;
  279. struct ci_hdrc *ci = dev_get_drvdata(dev);
  280. int port;
  281. u32 tmp;
  282. int ret = orig_bus_suspend(hcd);
  283. if (ret)
  284. return ret;
  285. port = HCS_N_PORTS(ehci->hcs_params);
  286. while (port--) {
  287. u32 __iomem *reg = &ehci->regs->port_status[port];
  288. u32 portsc = ehci_readl(ehci, reg);
  289. if (portsc & PORT_CONNECT) {
  290. /*
  291. * For chipidea, the resume signal will be ended
  292. * automatically, so for remote wakeup case, the
  293. * usbcmd.rs may not be set before the resume has
  294. * ended if other resume paths consumes too much
  295. * time (~24ms), in that case, the SOF will not
  296. * send out within 3ms after resume ends, then the
  297. * high speed device will enter full speed mode.
  298. */
  299. tmp = ehci_readl(ehci, &ehci->regs->command);
  300. tmp |= CMD_RUN;
  301. ehci_writel(ehci, tmp, &ehci->regs->command);
  302. /*
  303. * It needs a short delay between set RS bit and PHCD.
  304. */
  305. usleep_range(150, 200);
  306. /*
  307. * Need to clear WKCN and WKOC for imx HSIC,
  308. * otherwise, there will be wakeup event.
  309. */
  310. if (ci->platdata->flags & CI_HDRC_IMX_IS_HSIC) {
  311. tmp = ehci_readl(ehci, reg);
  312. tmp &= ~(PORT_WKDISC_E | PORT_WKCONN_E);
  313. ehci_writel(ehci, tmp, reg);
  314. }
  315. break;
  316. }
  317. }
  318. return 0;
  319. }
  320. static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb, bool copy_back)
  321. {
  322. struct ci_hdrc_dma_aligned_buffer *temp;
  323. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  324. return;
  325. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  326. temp = container_of(urb->transfer_buffer,
  327. struct ci_hdrc_dma_aligned_buffer, data);
  328. urb->transfer_buffer = temp->original_buffer;
  329. if (copy_back && usb_urb_dir_in(urb)) {
  330. size_t length;
  331. if (usb_pipeisoc(urb->pipe))
  332. length = urb->transfer_buffer_length;
  333. else
  334. length = urb->actual_length;
  335. memcpy(temp->original_buffer, temp->data, length);
  336. }
  337. kfree(temp);
  338. }
  339. static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  340. {
  341. struct ci_hdrc_dma_aligned_buffer *temp;
  342. if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0)
  343. return 0;
  344. if (IS_ALIGNED((uintptr_t)urb->transfer_buffer, 4)
  345. && IS_ALIGNED(urb->transfer_buffer_length, 4))
  346. return 0;
  347. temp = kmalloc(sizeof(*temp) + ALIGN(urb->transfer_buffer_length, 4), mem_flags);
  348. if (!temp)
  349. return -ENOMEM;
  350. if (usb_urb_dir_out(urb))
  351. memcpy(temp->data, urb->transfer_buffer,
  352. urb->transfer_buffer_length);
  353. temp->original_buffer = urb->transfer_buffer;
  354. urb->transfer_buffer = temp->data;
  355. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  356. return 0;
  357. }
  358. static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  359. gfp_t mem_flags)
  360. {
  361. int ret;
  362. ret = ci_hdrc_alloc_dma_aligned_buffer(urb, mem_flags);
  363. if (ret)
  364. return ret;
  365. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  366. if (ret)
  367. ci_hdrc_free_dma_aligned_buffer(urb, false);
  368. return ret;
  369. }
  370. static void ci_hdrc_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  371. {
  372. usb_hcd_unmap_urb_for_dma(hcd, urb);
  373. ci_hdrc_free_dma_aligned_buffer(urb, true);
  374. }
  375. int ci_hdrc_host_init(struct ci_hdrc *ci)
  376. {
  377. struct ci_role_driver *rdrv;
  378. if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
  379. return -ENXIO;
  380. rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
  381. if (!rdrv)
  382. return -ENOMEM;
  383. rdrv->start = host_start;
  384. rdrv->stop = host_stop;
  385. rdrv->irq = host_irq;
  386. rdrv->name = "host";
  387. ci->roles[CI_ROLE_HOST] = rdrv;
  388. if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA) {
  389. ci_ehci_hc_driver.map_urb_for_dma = ci_hdrc_map_urb_for_dma;
  390. ci_ehci_hc_driver.unmap_urb_for_dma = ci_hdrc_unmap_urb_for_dma;
  391. }
  392. return 0;
  393. }
  394. void ci_hdrc_host_driver_init(void)
  395. {
  396. ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
  397. orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
  398. ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
  399. ci_ehci_hc_driver.hub_control = ci_ehci_hub_control;
  400. }