GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / usb / host / ehci-mxc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/io.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/usb/otg.h>
14 #include <linux/usb/ulpi.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17 #include <linux/usb/hcd.h>
18 #include <linux/platform_data/usb-ehci-mxc.h>
19 #include "ehci.h"
20
21 #define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
22
23 static const char hcd_name[] = "ehci-mxc";
24
25 #define ULPI_VIEWPORT_OFFSET    0x170
26
27 struct ehci_mxc_priv {
28         struct clk *usbclk, *ahbclk, *phyclk;
29 };
30
31 static struct hc_driver __read_mostly ehci_mxc_hc_driver;
32
33 static const struct ehci_driver_overrides ehci_mxc_overrides __initconst = {
34         .extra_priv_size =      sizeof(struct ehci_mxc_priv),
35 };
36
37 static int ehci_mxc_drv_probe(struct platform_device *pdev)
38 {
39         struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
40         struct usb_hcd *hcd;
41         struct resource *res;
42         int irq, ret;
43         struct ehci_mxc_priv *priv;
44         struct device *dev = &pdev->dev;
45         struct ehci_hcd *ehci;
46
47         if (!pdata) {
48                 dev_err(dev, "No platform data given, bailing out.\n");
49                 return -EINVAL;
50         }
51
52         irq = platform_get_irq(pdev, 0);
53         if (irq < 0)
54                 return irq;
55
56         hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
57         if (!hcd)
58                 return -ENOMEM;
59
60         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
61         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
62         if (IS_ERR(hcd->regs)) {
63                 ret = PTR_ERR(hcd->regs);
64                 goto err_alloc;
65         }
66         hcd->rsrc_start = res->start;
67         hcd->rsrc_len = resource_size(res);
68
69         hcd->has_tt = 1;
70         ehci = hcd_to_ehci(hcd);
71         priv = (struct ehci_mxc_priv *) ehci->priv;
72
73         /* enable clocks */
74         priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
75         if (IS_ERR(priv->usbclk)) {
76                 ret = PTR_ERR(priv->usbclk);
77                 goto err_alloc;
78         }
79         clk_prepare_enable(priv->usbclk);
80
81         priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
82         if (IS_ERR(priv->ahbclk)) {
83                 ret = PTR_ERR(priv->ahbclk);
84                 goto err_clk_ahb;
85         }
86         clk_prepare_enable(priv->ahbclk);
87
88         /* "dr" device has its own clock on i.MX51 */
89         priv->phyclk = devm_clk_get(&pdev->dev, "phy");
90         if (IS_ERR(priv->phyclk))
91                 priv->phyclk = NULL;
92         if (priv->phyclk)
93                 clk_prepare_enable(priv->phyclk);
94
95
96         /* call platform specific init function */
97         if (pdata->init) {
98                 ret = pdata->init(pdev);
99                 if (ret) {
100                         dev_err(dev, "platform init failed\n");
101                         goto err_init;
102                 }
103                 /* platforms need some time to settle changed IO settings */
104                 mdelay(10);
105         }
106
107         /* EHCI registers start at offset 0x100 */
108         ehci->caps = hcd->regs + 0x100;
109         ehci->regs = hcd->regs + 0x100 +
110                 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
111
112         /* set up the PORTSCx register */
113         ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
114
115         /* is this really needed? */
116         msleep(10);
117
118         /* Initialize the transceiver */
119         if (pdata->otg) {
120                 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
121                 ret = usb_phy_init(pdata->otg);
122                 if (ret) {
123                         dev_err(dev, "unable to init transceiver, probably missing\n");
124                         ret = -ENODEV;
125                         goto err_add;
126                 }
127                 ret = otg_set_vbus(pdata->otg->otg, 1);
128                 if (ret) {
129                         dev_err(dev, "unable to enable vbus on transceiver\n");
130                         goto err_add;
131                 }
132         }
133
134         platform_set_drvdata(pdev, hcd);
135
136         ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
137         if (ret)
138                 goto err_add;
139
140         device_wakeup_enable(hcd->self.controller);
141         return 0;
142
143 err_add:
144         if (pdata && pdata->exit)
145                 pdata->exit(pdev);
146 err_init:
147         if (priv->phyclk)
148                 clk_disable_unprepare(priv->phyclk);
149
150         clk_disable_unprepare(priv->ahbclk);
151 err_clk_ahb:
152         clk_disable_unprepare(priv->usbclk);
153 err_alloc:
154         usb_put_hcd(hcd);
155         return ret;
156 }
157
158 static int ehci_mxc_drv_remove(struct platform_device *pdev)
159 {
160         struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
161         struct usb_hcd *hcd = platform_get_drvdata(pdev);
162         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
163         struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
164
165         usb_remove_hcd(hcd);
166
167         if (pdata && pdata->exit)
168                 pdata->exit(pdev);
169
170         if (pdata && pdata->otg)
171                 usb_phy_shutdown(pdata->otg);
172
173         clk_disable_unprepare(priv->usbclk);
174         clk_disable_unprepare(priv->ahbclk);
175
176         if (priv->phyclk)
177                 clk_disable_unprepare(priv->phyclk);
178
179         usb_put_hcd(hcd);
180         return 0;
181 }
182
183 MODULE_ALIAS("platform:mxc-ehci");
184
185 static struct platform_driver ehci_mxc_driver = {
186         .probe = ehci_mxc_drv_probe,
187         .remove = ehci_mxc_drv_remove,
188         .shutdown = usb_hcd_platform_shutdown,
189         .driver = {
190                    .name = "mxc-ehci",
191         },
192 };
193
194 static int __init ehci_mxc_init(void)
195 {
196         if (usb_disabled())
197                 return -ENODEV;
198
199         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
200
201         ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
202         return platform_driver_register(&ehci_mxc_driver);
203 }
204 module_init(ehci_mxc_init);
205
206 static void __exit ehci_mxc_cleanup(void)
207 {
208         platform_driver_unregister(&ehci_mxc_driver);
209 }
210 module_exit(ehci_mxc_cleanup);
211
212 MODULE_DESCRIPTION(DRIVER_DESC);
213 MODULE_AUTHOR("Sascha Hauer");
214 MODULE_LICENSE("GPL");