blob: c1edcc9b13cece07c9dc0e00c27a7f0848f4875c [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02002/*
3 * xhci-plat.c - xHCI host controller driver platform Bus Glue.
4 *
Alexander A. Klimovd6c3c6c2020-07-11 15:58:13 +02005 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02006 * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 *
8 * A lot of code borrowed from the Linux xHCI driver.
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02009 */
10
Gregory CLEMENT4718c172014-05-15 12:17:32 +020011#include <linux/clk.h>
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +030012#include <linux/dma-mapping.h>
Gregory CLEMENT48157bb2014-05-15 12:17:31 +020013#include <linux/module.h>
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +080014#include <linux/pci.h>
Gregory CLEMENT48157bb2014-05-15 12:17:31 +020015#include <linux/of.h>
Geert Uytterhoeven2847d242017-10-05 11:21:49 +030016#include <linux/of_device.h>
Gregory CLEMENT48157bb2014-05-15 12:17:31 +020017#include <linux/platform_device.h>
Maxime Ripard7b8ef222015-03-17 18:32:22 +020018#include <linux/usb/phy.h>
Gregory CLEMENT48157bb2014-05-15 12:17:31 +020019#include <linux/slab.h>
Duc Dang579085b2015-10-09 13:30:12 +030020#include <linux/acpi.h>
Peter Chenc94d41e2018-09-20 19:13:36 +030021#include <linux/usb/of.h>
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020022
23#include "xhci.h"
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +020024#include "xhci-plat.h"
Gregory CLEMENT97374792014-05-15 12:17:33 +020025#include "xhci-mvebu.h"
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +090026#include "xhci-rcar.h"
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020027
Andrew Bresticker1885d9a2014-10-03 11:35:26 +030028static struct hc_driver __read_mostly xhci_plat_hc_driver;
29
Roger Quadroscd33a322015-05-29 17:01:46 +030030static int xhci_plat_setup(struct usb_hcd *hcd);
31static int xhci_plat_start(struct usb_hcd *hcd);
32
33static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +020034 .extra_priv_size = sizeof(struct xhci_plat_priv),
Roger Quadroscd33a322015-05-29 17:01:46 +030035 .reset = xhci_plat_setup,
36 .start = xhci_plat_start,
37};
38
Felipe Balbib1c127ae2016-04-22 13:17:16 +030039static void xhci_priv_plat_start(struct usb_hcd *hcd)
40{
41 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
42
43 if (priv->plat_start)
44 priv->plat_start(hcd);
45}
46
Pali Rohár32419292021-02-01 16:08:03 +010047static int xhci_priv_plat_setup(struct usb_hcd *hcd)
48{
49 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
50
51 if (!priv->plat_setup)
52 return 0;
53
54 return priv->plat_setup(hcd);
55}
56
Felipe Balbib1c127ae2016-04-22 13:17:16 +030057static int xhci_priv_init_quirk(struct usb_hcd *hcd)
58{
59 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
60
61 if (!priv->init_quirk)
62 return 0;
63
64 return priv->init_quirk(hcd);
65}
66
Peter Chen5e0e54f2020-09-18 16:17:44 +030067static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
68{
69 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
70
71 if (!priv->suspend_quirk)
72 return 0;
73
74 return priv->suspend_quirk(hcd);
75}
76
Yoshihiro Shimoda98c0a3f2017-04-19 16:55:47 +030077static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
78{
79 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
80
81 if (!priv->resume_quirk)
82 return 0;
83
84 return priv->resume_quirk(hcd);
85}
86
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020087static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
88{
Yoshihiro Shimoda77d8f112019-09-02 21:01:36 +090089 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
90
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020091 /*
92 * As of now platform drivers don't provide MSI support so we ensure
93 * here that the generic code does not try to make a pci_dev from our
94 * dev struct in order to setup MSI
95 */
Yoshihiro Shimoda77d8f112019-09-02 21:01:36 +090096 xhci->quirks |= XHCI_PLAT | priv->quirks;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +020097}
98
99/* called during probe() after chip reset completes */
100static int xhci_plat_setup(struct usb_hcd *hcd)
101{
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +0900102 int ret;
103
Yoshihiro Shimoda4ac89182014-07-09 10:08:52 +0900104
Felipe Balbib1c127ae2016-04-22 13:17:16 +0300105 ret = xhci_priv_init_quirk(hcd);
106 if (ret)
107 return ret;
Felipe Balbi3bdb2632016-04-22 13:17:14 +0300108
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200109 return xhci_gen_setup(hcd, xhci_plat_quirks);
110}
111
Yoshihiro Shimoda94adcdc2014-05-28 20:22:58 +0900112static int xhci_plat_start(struct usb_hcd *hcd)
113{
Felipe Balbib1c127ae2016-04-22 13:17:16 +0300114 xhci_priv_plat_start(hcd);
Yoshihiro Shimoda94adcdc2014-05-28 20:22:58 +0900115 return xhci_run(hcd);
116}
117
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200118#ifdef CONFIG_OF
119static const struct xhci_plat_priv xhci_plat_marvell_armada = {
Felipe Balbib1c127ae2016-04-22 13:17:16 +0300120 .init_quirk = xhci_mvebu_mbus_init_quirk,
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200121};
122
Ofer Heifetz12453a82019-01-29 10:23:41 +0100123static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
Pali Rohár32419292021-02-01 16:08:03 +0100124 .plat_setup = xhci_mvebu_a3700_plat_setup,
Ofer Heifetz12453a82019-01-29 10:23:41 +0100125 .init_quirk = xhci_mvebu_a3700_init_quirk,
126};
127
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200128static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
Yoshihiro Shimodaed056202019-09-02 21:01:37 +0900129 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200130};
131
Yoshihiro Shimoda526a2402015-11-24 13:09:53 +0200132static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
Yoshihiro Shimodaed056202019-09-02 21:01:37 +0900133 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
Yoshihiro Shimoda3f1dae62016-11-11 15:13:27 +0200134};
135
Al Cooper1036e762020-05-12 11:00:17 -0400136static const struct xhci_plat_priv xhci_plat_brcm = {
137 .quirks = XHCI_RESET_ON_RESUME,
138};
139
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200140static const struct of_device_id usb_xhci_of_match[] = {
141 {
142 .compatible = "generic-xhci",
143 }, {
144 .compatible = "xhci-platform",
145 }, {
146 .compatible = "marvell,armada-375-xhci",
147 .data = &xhci_plat_marvell_armada,
148 }, {
149 .compatible = "marvell,armada-380-xhci",
150 .data = &xhci_plat_marvell_armada,
151 }, {
Ofer Heifetz12453a82019-01-29 10:23:41 +0100152 .compatible = "marvell,armada3700-xhci",
153 .data = &xhci_plat_marvell_armada3700,
154 }, {
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200155 .compatible = "renesas,xhci-r8a7790",
156 .data = &xhci_plat_renesas_rcar_gen2,
157 }, {
158 .compatible = "renesas,xhci-r8a7791",
159 .data = &xhci_plat_renesas_rcar_gen2,
160 }, {
Yoshihiro Shimoda82487b72015-11-24 13:09:52 +0200161 .compatible = "renesas,xhci-r8a7793",
162 .data = &xhci_plat_renesas_rcar_gen2,
163 }, {
Yoshihiro Shimoda526a2402015-11-24 13:09:53 +0200164 .compatible = "renesas,xhci-r8a7795",
165 .data = &xhci_plat_renesas_rcar_gen3,
166 }, {
Yoshihiro Shimoda3f1dae62016-11-11 15:13:27 +0200167 .compatible = "renesas,xhci-r8a7796",
Yoshihiro Shimoda4dd51862017-08-16 14:23:19 +0300168 .data = &xhci_plat_renesas_rcar_gen3,
Yoshihiro Shimoda3f1dae62016-11-11 15:13:27 +0200169 }, {
Simon Hormanf1bbdc32016-02-18 17:55:49 +0200170 .compatible = "renesas,rcar-gen2-xhci",
171 .data = &xhci_plat_renesas_rcar_gen2,
172 }, {
173 .compatible = "renesas,rcar-gen3-xhci",
174 .data = &xhci_plat_renesas_rcar_gen3,
Al Cooper1036e762020-05-12 11:00:17 -0400175 }, {
176 .compatible = "brcm,xhci-brcm-v2",
177 .data = &xhci_plat_brcm,
178 }, {
179 .compatible = "brcm,bcm7445-xhci",
180 .data = &xhci_plat_brcm,
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200181 },
Julia Lawallff075d672016-02-18 17:55:50 +0200182 {},
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200183};
184MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
185#endif
186
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200187static int xhci_plat_probe(struct platform_device *pdev)
188{
Geert Uytterhoeven2847d242017-10-05 11:21:49 +0300189 const struct xhci_plat_priv *priv_match;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200190 const struct hc_driver *driver;
Anurag Kumar Vulisha222471f2018-08-31 17:24:42 +0300191 struct device *sysdev, *tmpdev;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200192 struct xhci_hcd *xhci;
193 struct resource *res;
194 struct usb_hcd *hcd;
195 int ret;
196 int irq;
Peter Chenf768e712020-09-18 16:17:46 +0300197 struct xhci_plat_priv *priv = NULL;
198
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200199
200 if (usb_disabled())
201 return -ENODEV;
202
Andrew Bresticker1885d9a2014-10-03 11:35:26 +0300203 driver = &xhci_plat_hc_driver;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200204
205 irq = platform_get_irq(pdev, 0);
206 if (irq < 0)
Thomas Petazzoni4b148d52017-05-17 18:32:06 +0300207 return irq;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200208
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800209 /*
210 * sysdev must point to a device that is known to the system firmware
211 * or PCI hardware. We handle these three cases here:
212 * 1. xhci_plat comes from firmware
213 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
214 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
215 */
Adam Wallisc6b8e792017-09-18 17:39:16 +0300216 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
217 if (is_of_node(sysdev->fwnode) ||
218 is_acpi_device_node(sysdev->fwnode))
219 break;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800220#ifdef CONFIG_PCI
Adam Wallisc6b8e792017-09-18 17:39:16 +0300221 else if (sysdev->bus == &pci_bus_type)
222 break;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800223#endif
Adam Wallisc6b8e792017-09-18 17:39:16 +0300224 }
225
226 if (!sysdev)
227 sysdev = &pdev->dev;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800228
Duc Dang0ebbe392015-10-09 13:30:11 +0300229 /* Try to set 64-bit DMA first */
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800230 if (WARN_ON(!sysdev->dma_mask))
Duc Dang0ebbe392015-10-09 13:30:11 +0300231 /* Platform did not initialize dma_mask */
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800232 ret = dma_coerce_mask_and_coherent(sysdev,
Duc Dang0ebbe392015-10-09 13:30:11 +0300233 DMA_BIT_MASK(64));
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +0300234 else
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800235 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
Duc Dang0ebbe392015-10-09 13:30:11 +0300236
237 /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
238 if (ret) {
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800239 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
Duc Dang0ebbe392015-10-09 13:30:11 +0300240 if (ret)
241 return ret;
242 }
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +0300243
Baolin Wangb0c69b42017-04-19 16:55:45 +0300244 pm_runtime_set_active(&pdev->dev);
245 pm_runtime_enable(&pdev->dev);
246 pm_runtime_get_noresume(&pdev->dev);
247
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800248 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
249 dev_name(&pdev->dev), NULL);
Baolin Wangb0c69b42017-04-19 16:55:45 +0300250 if (!hcd) {
251 ret = -ENOMEM;
252 goto disable_runtime;
253 }
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200254
Dejin Zhengfb222272020-03-24 00:06:09 +0800255 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
Himangi Saraogifd666342014-06-20 23:11:23 +0530256 if (IS_ERR(hcd->regs)) {
257 ret = PTR_ERR(hcd->regs);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200258 goto put_hcd;
259 }
260
Varka Bhadrame7020f12014-11-04 07:51:27 +0530261 hcd->rsrc_start = res->start;
262 hcd->rsrc_len = resource_size(res);
263
Chunfeng Yun08048c02019-04-17 16:28:17 +0800264 xhci = hcd_to_xhci(hcd);
265
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200266 /*
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300267 * Not all platforms have clks so it is not an error if the
268 * clock do not exist.
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200269 */
Chunfeng Yun08048c02019-04-17 16:28:17 +0800270 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
271 if (IS_ERR(xhci->reg_clk)) {
272 ret = PTR_ERR(xhci->reg_clk);
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300273 goto put_hcd;
274 }
275
Chunfeng Yun08048c02019-04-17 16:28:17 +0800276 ret = clk_prepare_enable(xhci->reg_clk);
277 if (ret)
278 goto put_hcd;
279
280 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
281 if (IS_ERR(xhci->clk)) {
282 ret = PTR_ERR(xhci->clk);
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300283 goto disable_reg_clk;
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200284 }
285
Chunfeng Yun08048c02019-04-17 16:28:17 +0800286 ret = clk_prepare_enable(xhci->clk);
287 if (ret)
288 goto disable_reg_clk;
289
Peter Chen46034a92020-09-18 16:17:43 +0300290 if (pdev->dev.of_node)
291 priv_match = of_device_get_match_data(&pdev->dev);
292 else
293 priv_match = dev_get_platdata(&pdev->dev);
294
Geert Uytterhoeven2847d242017-10-05 11:21:49 +0300295 if (priv_match) {
Peter Chenf768e712020-09-18 16:17:46 +0300296 priv = hcd_to_xhci_priv(hcd);
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200297 /* Just copy data for now */
Peter Chen93cb8f12020-09-18 16:17:45 +0300298 *priv = *priv_match;
Yoshihiro Shimoda4efb2f62015-11-24 13:09:49 +0200299 }
300
Peter Chen4bb4fc02020-09-18 16:17:47 +0300301 device_set_wakeup_capable(&pdev->dev, true);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200302
Roger Quadros9fa733f2015-05-29 17:01:50 +0300303 xhci->main_hcd = hcd;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800304 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200305 dev_name(&pdev->dev), hcd);
306 if (!xhci->shared_hcd) {
307 ret = -ENOMEM;
Roger Quadros4ac53082015-05-29 17:01:47 +0300308 goto disable_clk;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200309 }
310
Adam Wallisab725cb2017-12-08 17:59:13 +0200311 /* imod_interval is the interrupt moderation value in nanoseconds. */
312 xhci->imod_interval = 40000;
Anurag Kumar Vulisha222471f2018-08-31 17:24:42 +0300313
314 /* Iterate over all parent nodes for finding quirks */
315 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
316
317 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
318 xhci->quirks |= XHCI_HW_LPM_DISABLE;
319
320 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
321 xhci->quirks |= XHCI_LPM_SUPPORT;
322
323 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
324 xhci->quirks |= XHCI_BROKEN_PORT_PED;
325
326 device_property_read_u32(tmpdev, "imod-interval-ns",
327 &xhci->imod_interval);
328 }
Adam Wallisab725cb2017-12-08 17:59:13 +0200329
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800330 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
Maxime Ripard7b8ef222015-03-17 18:32:22 +0200331 if (IS_ERR(hcd->usb_phy)) {
332 ret = PTR_ERR(hcd->usb_phy);
333 if (ret == -EPROBE_DEFER)
334 goto put_usb3_hcd;
335 hcd->usb_phy = NULL;
336 } else {
337 ret = usb_phy_init(hcd->usb_phy);
338 if (ret)
339 goto put_usb3_hcd;
340 }
341
Peter Chenc94d41e2018-09-20 19:13:36 +0300342 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
343 xhci->shared_hcd->tpl_support = hcd->tpl_support;
Pali Rohár32419292021-02-01 16:08:03 +0100344
345 if (priv) {
346 ret = xhci_priv_plat_setup(hcd);
347 if (ret)
348 goto disable_usb_phy;
349 }
350
351 if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
Peter Chenf768e712020-09-18 16:17:46 +0300352 hcd->skip_phy_initialization = 1;
353
Tejas Joglekarbac1ec52020-12-08 11:29:08 +0200354 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
355 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
356
Roger Quadros4ac53082015-05-29 17:01:47 +0300357 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200358 if (ret)
Maxime Ripard7b8ef222015-03-17 18:32:22 +0200359 goto disable_usb_phy;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200360
William wu5de4e1e2017-01-17 15:32:07 +0800361 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
362 xhci->shared_hcd->can_do_streams = 1;
363
Roger Quadros4ac53082015-05-29 17:01:47 +0300364 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
365 if (ret)
366 goto dealloc_usb2_hcd;
367
Andrew Brestickerc70a1522017-04-07 17:56:49 +0300368 device_enable_async_suspend(&pdev->dev);
Baolin Wangb0c69b42017-04-19 16:55:45 +0300369 pm_runtime_put_noidle(&pdev->dev);
370
371 /*
372 * Prevent runtime pm from being on as default, users should enable
373 * runtime pm using power/control in sysfs.
374 */
375 pm_runtime_forbid(&pdev->dev);
Andrew Brestickerc70a1522017-04-07 17:56:49 +0300376
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200377 return 0;
378
Roger Quadros4ac53082015-05-29 17:01:47 +0300379
380dealloc_usb2_hcd:
381 usb_remove_hcd(hcd);
382
Maxime Ripard7b8ef222015-03-17 18:32:22 +0200383disable_usb_phy:
384 usb_phy_shutdown(hcd->usb_phy);
385
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200386put_usb3_hcd:
387 usb_put_hcd(xhci->shared_hcd);
388
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200389disable_clk:
Chunfeng Yun08048c02019-04-17 16:28:17 +0800390 clk_disable_unprepare(xhci->clk);
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200391
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300392disable_reg_clk:
Chunfeng Yun08048c02019-04-17 16:28:17 +0800393 clk_disable_unprepare(xhci->reg_clk);
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300394
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200395put_hcd:
396 usb_put_hcd(hcd);
397
Baolin Wangb0c69b42017-04-19 16:55:45 +0300398disable_runtime:
399 pm_runtime_put_noidle(&pdev->dev);
400 pm_runtime_disable(&pdev->dev);
401
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200402 return ret;
403}
404
405static int xhci_plat_remove(struct platform_device *dev)
406{
407 struct usb_hcd *hcd = platform_get_drvdata(dev);
408 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Gregory CLEMENT4718c172014-05-15 12:17:32 +0200409 struct clk *clk = xhci->clk;
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300410 struct clk *reg_clk = xhci->reg_clk;
Mathias Nymanf0680902018-11-09 17:21:17 +0200411 struct usb_hcd *shared_hcd = xhci->shared_hcd;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200412
Li Jun1449cb22020-05-14 14:04:32 +0300413 pm_runtime_get_sync(&dev->dev);
Guenter Roeckdcc76202017-03-09 15:39:37 +0200414 xhci->xhc_state |= XHCI_STATE_REMOVING;
415
Mathias Nymanf0680902018-11-09 17:21:17 +0200416 usb_remove_hcd(shared_hcd);
417 xhci->shared_hcd = NULL;
Maxime Ripard7b8ef222015-03-17 18:32:22 +0200418 usb_phy_shutdown(hcd->usb_phy);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200419
420 usb_remove_hcd(hcd);
Mathias Nymanf0680902018-11-09 17:21:17 +0200421 usb_put_hcd(shared_hcd);
Roger Quadros4ac53082015-05-29 17:01:47 +0300422
Gregory CLEMENT2d796092018-04-20 16:52:51 +0300423 clk_disable_unprepare(clk);
Gregory CLEMENT3ae2da72018-04-20 16:52:52 +0300424 clk_disable_unprepare(reg_clk);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200425 usb_put_hcd(hcd);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200426
Baolin Wangb0c69b42017-04-19 16:55:45 +0300427 pm_runtime_disable(&dev->dev);
Li Jun1449cb22020-05-14 14:04:32 +0300428 pm_runtime_put_noidle(&dev->dev);
429 pm_runtime_set_suspended(&dev->dev);
Baolin Wangb0c69b42017-04-19 16:55:45 +0300430
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200431 return 0;
432}
433
Arnd Bergmannd852ed92017-04-21 23:42:54 +0200434static int __maybe_unused xhci_plat_suspend(struct device *dev)
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200435{
436 struct usb_hcd *hcd = dev_get_drvdata(dev);
437 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Peter Chen5e0e54f2020-09-18 16:17:44 +0300438 int ret;
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200439
Peter Chen5e0e54f2020-09-18 16:17:44 +0300440 ret = xhci_priv_suspend_quirk(hcd);
441 if (ret)
442 return ret;
Lu Baolua1377e52014-11-18 11:27:14 +0200443 /*
444 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
Peter Chen9cdda282020-09-18 16:17:48 +0300445 * to do wakeup during suspend.
Lu Baolua1377e52014-11-18 11:27:14 +0200446 */
Yoshihiro Shimodad56e57c2018-03-08 17:17:16 +0200447 return xhci_suspend(xhci, device_may_wakeup(dev));
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200448}
449
Arnd Bergmannd852ed92017-04-21 23:42:54 +0200450static int __maybe_unused xhci_plat_resume(struct device *dev)
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200451{
452 struct usb_hcd *hcd = dev_get_drvdata(dev);
453 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Yoshihiro Shimoda98c0a3f2017-04-19 16:55:47 +0300454 int ret;
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200455
Yoshihiro Shimoda98c0a3f2017-04-19 16:55:47 +0300456 ret = xhci_priv_resume_quirk(hcd);
457 if (ret)
458 return ret;
459
Qais Yousef79112cc2020-05-18 16:49:30 +0100460 ret = xhci_resume(xhci, 0);
461 if (ret)
462 return ret;
463
464 pm_runtime_disable(dev);
465 pm_runtime_set_active(dev);
466 pm_runtime_enable(dev);
467
468 return 0;
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200469}
Baolin Wangb0c69b42017-04-19 16:55:45 +0300470
Arnd Bergmannd852ed92017-04-21 23:42:54 +0200471static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
Baolin Wangb0c69b42017-04-19 16:55:45 +0300472{
473 struct usb_hcd *hcd = dev_get_drvdata(dev);
474 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Peter Chen5e0e54f2020-09-18 16:17:44 +0300475 int ret;
476
477 ret = xhci_priv_suspend_quirk(hcd);
478 if (ret)
479 return ret;
Baolin Wangb0c69b42017-04-19 16:55:45 +0300480
481 return xhci_suspend(xhci, true);
482}
483
Arnd Bergmannd852ed92017-04-21 23:42:54 +0200484static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
Baolin Wangb0c69b42017-04-19 16:55:45 +0300485{
486 struct usb_hcd *hcd = dev_get_drvdata(dev);
487 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
488
489 return xhci_resume(xhci, 0);
490}
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200491
492static const struct dev_pm_ops xhci_plat_pm_ops = {
493 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
Baolin Wangb0c69b42017-04-19 16:55:45 +0300494
495 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
496 xhci_plat_runtime_resume,
497 NULL)
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200498};
Vikas Sajjan57d04eb2013-02-11 12:58:00 +0200499
Lee Jonesf535ad62020-07-06 14:33:41 +0100500#ifdef CONFIG_ACPI
Duc Dang579085b2015-10-09 13:30:12 +0300501static const struct acpi_device_id usb_xhci_acpi_match[] = {
502 /* XHCI-compliant USB Controller */
503 { "PNP0D10", },
504 { }
505};
506MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
Lee Jonesf535ad62020-07-06 14:33:41 +0100507#endif
Duc Dang579085b2015-10-09 13:30:12 +0300508
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200509static struct platform_driver usb_xhci_driver = {
510 .probe = xhci_plat_probe,
511 .remove = xhci_plat_remove,
Ran Wangb433e342020-03-06 17:23:28 +0800512 .shutdown = usb_hcd_platform_shutdown,
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200513 .driver = {
514 .name = "xhci-hcd",
Baolin Wangb0c69b42017-04-19 16:55:45 +0300515 .pm = &xhci_plat_pm_ops,
Al Cooper1fe6c452013-07-25 19:04:44 -0400516 .of_match_table = of_match_ptr(usb_xhci_of_match),
Duc Dang579085b2015-10-09 13:30:12 +0300517 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200518 },
519};
520MODULE_ALIAS("platform:xhci-hcd");
521
Andrew Bresticker29e409f2014-10-03 11:35:29 +0300522static int __init xhci_plat_init(void)
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200523{
Roger Quadroscd33a322015-05-29 17:01:46 +0300524 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200525 return platform_driver_register(&usb_xhci_driver);
526}
Andrew Bresticker29e409f2014-10-03 11:35:29 +0300527module_init(xhci_plat_init);
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200528
Andrew Bresticker29e409f2014-10-03 11:35:29 +0300529static void __exit xhci_plat_exit(void)
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +0200530{
531 platform_driver_unregister(&usb_xhci_driver);
532}
Andrew Bresticker29e409f2014-10-03 11:35:29 +0300533module_exit(xhci_plat_exit);
534
535MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
536MODULE_LICENSE("GPL");