Merge tag 'tty-5.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[linux-2.6-microblaze.git] / drivers / usb / host / ehci-brcm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Broadcom */
3
4 #include <linux/clk.h>
5 #include <linux/dma-mapping.h>
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 #include <linux/io.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/usb.h>
12 #include <linux/usb/hcd.h>
13 #include <linux/iopoll.h>
14
15 #include "ehci.h"
16
17 #define hcd_to_ehci_priv(h) ((struct brcm_priv *)hcd_to_ehci(h)->priv)
18
19 struct brcm_priv {
20         struct clk *clk;
21 };
22
23 /*
24  * ehci_brcm_wait_for_sof
25  * Wait for start of next microframe, then wait extra delay microseconds
26  */
27 static inline void ehci_brcm_wait_for_sof(struct ehci_hcd *ehci, u32 delay)
28 {
29         u32 frame_idx = ehci_readl(ehci, &ehci->regs->frame_index);
30         u32 val;
31         int res;
32
33         /* Wait for next microframe (every 125 usecs) */
34         res = readl_relaxed_poll_timeout(&ehci->regs->frame_index, val,
35                                          val != frame_idx, 1, 130);
36         if (res)
37                 ehci_err(ehci, "Error waiting for SOF\n");
38         udelay(delay);
39 }
40
41 /*
42  * ehci_brcm_hub_control
43  * The EHCI controller has a bug where it can violate the SOF
44  * interval between the first two SOF's transmitted after resume
45  * if the resume occurs near the end of the microframe. This causees
46  * the controller to detect babble on the suspended port and
47  * will eventually cause the controller to reset the port.
48  * The fix is to Intercept the echi-hcd request to complete RESUME and
49  * align it to the start of the next microframe.
50  * See SWLINUX-1909 for more details
51  */
52 static int ehci_brcm_hub_control(
53         struct usb_hcd  *hcd,
54         u16             typeReq,
55         u16             wValue,
56         u16             wIndex,
57         char            *buf,
58         u16             wLength)
59 {
60         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
61         int             ports = HCS_N_PORTS(ehci->hcs_params);
62         u32 __iomem     *status_reg;
63         unsigned long flags;
64         int retval, irq_disabled = 0;
65
66         status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
67
68         /*
69          * RESUME is cleared when GetPortStatus() is called 20ms after start
70          * of RESUME
71          */
72         if ((typeReq == GetPortStatus) &&
73             (wIndex && wIndex <= ports) &&
74             ehci->reset_done[wIndex-1] &&
75             time_after_eq(jiffies, ehci->reset_done[wIndex-1]) &&
76             (ehci_readl(ehci, status_reg) & PORT_RESUME)) {
77
78                 /*
79                  * to make sure we are not interrupted until RESUME bit
80                  * is cleared, disable interrupts on current CPU
81                  */
82                 ehci_dbg(ehci, "SOF alignment workaround\n");
83                 irq_disabled = 1;
84                 local_irq_save(flags);
85                 ehci_brcm_wait_for_sof(ehci, 5);
86         }
87         retval = ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
88         if (irq_disabled)
89                 local_irq_restore(flags);
90         return retval;
91 }
92
93 static int ehci_brcm_reset(struct usb_hcd *hcd)
94 {
95         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
96         int len;
97
98         ehci->big_endian_mmio = 1;
99
100         ehci->caps = (void __iomem *)hcd->regs;
101         len = HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
102         ehci->regs = (void __iomem *)(hcd->regs + len);
103
104         /* This fixes the lockup during reboot due to prior interrupts */
105         ehci_writel(ehci, CMD_RESET, &ehci->regs->command);
106         mdelay(10);
107
108         /*
109          * SWLINUX-1705: Avoid OUT packet underflows during high memory
110          *   bus usage
111          * port_status[0x0f] = Broadcom-proprietary USB_EHCI_INSNREG00 @ 0x90
112          */
113         ehci_writel(ehci, 0x00800040, &ehci->regs->port_status[0x10]);
114         ehci_writel(ehci, 0x00000001, &ehci->regs->port_status[0x12]);
115
116         return ehci_setup(hcd);
117 }
118
119 static struct hc_driver __read_mostly ehci_brcm_hc_driver;
120
121 static const struct ehci_driver_overrides brcm_overrides __initconst = {
122         .reset = ehci_brcm_reset,
123         .extra_priv_size = sizeof(struct brcm_priv),
124 };
125
126 static int ehci_brcm_probe(struct platform_device *pdev)
127 {
128         struct device *dev = &pdev->dev;
129         struct resource *res_mem;
130         struct brcm_priv *priv;
131         struct usb_hcd *hcd;
132         int irq;
133         int err;
134
135         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
136         if (err)
137                 return err;
138
139         irq = platform_get_irq(pdev, 0);
140         if (irq <= 0)
141                 return irq ? irq : -EINVAL;
142
143         /* Hook the hub control routine to work around a bug */
144         ehci_brcm_hc_driver.hub_control = ehci_brcm_hub_control;
145
146         /* initialize hcd */
147         hcd = usb_create_hcd(&ehci_brcm_hc_driver, dev, dev_name(dev));
148         if (!hcd)
149                 return -ENOMEM;
150
151         platform_set_drvdata(pdev, hcd);
152         priv = hcd_to_ehci_priv(hcd);
153
154         priv->clk = devm_clk_get_optional(dev, NULL);
155         if (IS_ERR(priv->clk)) {
156                 err = PTR_ERR(priv->clk);
157                 goto err_hcd;
158         }
159
160         err = clk_prepare_enable(priv->clk);
161         if (err)
162                 goto err_hcd;
163
164         hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res_mem);
165         if (IS_ERR(hcd->regs)) {
166                 err = PTR_ERR(hcd->regs);
167                 goto err_clk;
168         }
169         hcd->rsrc_start = res_mem->start;
170         hcd->rsrc_len = resource_size(res_mem);
171         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
172         if (err)
173                 goto err_clk;
174
175         device_wakeup_enable(hcd->self.controller);
176         device_enable_async_suspend(hcd->self.controller);
177
178         return 0;
179
180 err_clk:
181         clk_disable_unprepare(priv->clk);
182 err_hcd:
183         usb_put_hcd(hcd);
184
185         return err;
186 }
187
188 static int ehci_brcm_remove(struct platform_device *dev)
189 {
190         struct usb_hcd *hcd = platform_get_drvdata(dev);
191         struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
192
193         usb_remove_hcd(hcd);
194         clk_disable_unprepare(priv->clk);
195         usb_put_hcd(hcd);
196         return 0;
197 }
198
199 static int __maybe_unused ehci_brcm_suspend(struct device *dev)
200 {
201         int ret;
202         struct usb_hcd *hcd = dev_get_drvdata(dev);
203         struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
204         bool do_wakeup = device_may_wakeup(dev);
205
206         ret = ehci_suspend(hcd, do_wakeup);
207         if (ret)
208                 return ret;
209         clk_disable_unprepare(priv->clk);
210         return 0;
211 }
212
213 static int __maybe_unused ehci_brcm_resume(struct device *dev)
214 {
215         struct usb_hcd *hcd = dev_get_drvdata(dev);
216         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
217         struct brcm_priv *priv = hcd_to_ehci_priv(hcd);
218         int err;
219
220         err = clk_prepare_enable(priv->clk);
221         if (err)
222                 return err;
223         /*
224          * SWLINUX-1705: Avoid OUT packet underflows during high memory
225          *   bus usage
226          * port_status[0x0f] = Broadcom-proprietary USB_EHCI_INSNREG00
227          * @ 0x90
228          */
229         ehci_writel(ehci, 0x00800040, &ehci->regs->port_status[0x10]);
230         ehci_writel(ehci, 0x00000001, &ehci->regs->port_status[0x12]);
231
232         ehci_resume(hcd, false);
233
234         pm_runtime_disable(dev);
235         pm_runtime_set_active(dev);
236         pm_runtime_enable(dev);
237
238         return 0;
239 }
240
241 static SIMPLE_DEV_PM_OPS(ehci_brcm_pm_ops, ehci_brcm_suspend,
242                 ehci_brcm_resume);
243
244 static const struct of_device_id brcm_ehci_of_match[] = {
245         { .compatible = "brcm,ehci-brcm-v2", },
246         { .compatible = "brcm,bcm7445-ehci", },
247         {}
248 };
249
250 static struct platform_driver ehci_brcm_driver = {
251         .probe          = ehci_brcm_probe,
252         .remove         = ehci_brcm_remove,
253         .shutdown       = usb_hcd_platform_shutdown,
254         .driver         = {
255                 .name   = "ehci-brcm",
256                 .pm     = &ehci_brcm_pm_ops,
257                 .of_match_table = brcm_ehci_of_match,
258         }
259 };
260
261 static int __init ehci_brcm_init(void)
262 {
263         if (usb_disabled())
264                 return -ENODEV;
265
266         ehci_init_driver(&ehci_brcm_hc_driver, &brcm_overrides);
267         return platform_driver_register(&ehci_brcm_driver);
268 }
269 module_init(ehci_brcm_init);
270
271 static void __exit ehci_brcm_exit(void)
272 {
273         platform_driver_unregister(&ehci_brcm_driver);
274 }
275 module_exit(ehci_brcm_exit);
276
277 MODULE_ALIAS("platform:ehci-brcm");
278 MODULE_DESCRIPTION("EHCI Broadcom STB driver");
279 MODULE_AUTHOR("Al Cooper");
280 MODULE_LICENSE("GPL");