Commit | Line | Data |
---|---|---|
7e8d5cd9 DM |
1 | /* |
2 | * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix | |
3 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software Foundation, | |
17 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
dba63b2f AS |
20 | #include <linux/kernel.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/io.h> | |
7e8d5cd9 DM |
23 | #include <linux/platform_device.h> |
24 | #include <linux/clk.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/usb/otg.h> | |
a464dc4d | 27 | #include <linux/usb/ulpi.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
dba63b2f AS |
29 | #include <linux/usb.h> |
30 | #include <linux/usb/hcd.h> | |
7e8d5cd9 | 31 | |
82906b13 | 32 | #include <linux/platform_data/usb-ehci-mxc.h> |
7e8d5cd9 | 33 | |
a464dc4d APR |
34 | #include <asm/mach-types.h> |
35 | ||
dba63b2f AS |
36 | #include "ehci.h" |
37 | ||
38 | #define DRIVER_DESC "Freescale On-Chip EHCI Host driver" | |
39 | ||
40 | static const char hcd_name[] = "ehci-mxc"; | |
41 | ||
7e8d5cd9 | 42 | #define ULPI_VIEWPORT_OFFSET 0x170 |
7e8d5cd9 DM |
43 | |
44 | struct ehci_mxc_priv { | |
c943740c | 45 | struct clk *usbclk, *ahbclk, *phyclk; |
7e8d5cd9 DM |
46 | }; |
47 | ||
dba63b2f | 48 | static struct hc_driver __read_mostly ehci_mxc_hc_driver; |
7e8d5cd9 | 49 | |
dba63b2f AS |
50 | static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = { |
51 | .extra_priv_size = sizeof(struct ehci_mxc_priv), | |
7e8d5cd9 DM |
52 | }; |
53 | ||
54 | static int ehci_mxc_drv_probe(struct platform_device *pdev) | |
55 | { | |
56 | struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data; | |
57 | struct usb_hcd *hcd; | |
58 | struct resource *res; | |
724c8525 | 59 | int irq, ret; |
7e8d5cd9 DM |
60 | struct ehci_mxc_priv *priv; |
61 | struct device *dev = &pdev->dev; | |
0247a7bc | 62 | struct ehci_hcd *ehci; |
7e8d5cd9 DM |
63 | |
64 | dev_info(&pdev->dev, "initializing i.MX USB Controller\n"); | |
65 | ||
66 | if (!pdata) { | |
67 | dev_err(dev, "No platform data given, bailing out.\n"); | |
68 | return -EINVAL; | |
69 | } | |
70 | ||
71 | irq = platform_get_irq(pdev, 0); | |
72 | ||
73 | hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev)); | |
74 | if (!hcd) | |
75 | return -ENOMEM; | |
76 | ||
7e8d5cd9 DM |
77 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
78 | if (!res) { | |
79 | dev_err(dev, "Found HC with no register addr. Check setup!\n"); | |
80 | ret = -ENODEV; | |
af09c060 | 81 | goto err_alloc; |
7e8d5cd9 DM |
82 | } |
83 | ||
84 | hcd->rsrc_start = res->start; | |
85 | hcd->rsrc_len = resource_size(res); | |
86 | ||
148e1134 TR |
87 | hcd->regs = devm_ioremap_resource(&pdev->dev, res); |
88 | if (IS_ERR(hcd->regs)) { | |
89 | ret = PTR_ERR(hcd->regs); | |
af09c060 | 90 | goto err_alloc; |
7e8d5cd9 DM |
91 | } |
92 | ||
dba63b2f AS |
93 | hcd->has_tt = 1; |
94 | ehci = hcd_to_ehci(hcd); | |
95 | priv = (struct ehci_mxc_priv *) ehci->priv; | |
96 | ||
7e8d5cd9 | 97 | /* enable clocks */ |
af09c060 | 98 | priv->usbclk = devm_clk_get(&pdev->dev, "ipg"); |
7e8d5cd9 DM |
99 | if (IS_ERR(priv->usbclk)) { |
100 | ret = PTR_ERR(priv->usbclk); | |
af09c060 | 101 | goto err_alloc; |
7e8d5cd9 | 102 | } |
198ad2ce | 103 | clk_prepare_enable(priv->usbclk); |
7e8d5cd9 | 104 | |
af09c060 | 105 | priv->ahbclk = devm_clk_get(&pdev->dev, "ahb"); |
c943740c SH |
106 | if (IS_ERR(priv->ahbclk)) { |
107 | ret = PTR_ERR(priv->ahbclk); | |
108 | goto err_clk_ahb; | |
7e8d5cd9 | 109 | } |
c943740c | 110 | clk_prepare_enable(priv->ahbclk); |
7e8d5cd9 | 111 | |
3bb8029a | 112 | /* "dr" device has its own clock on i.MX51 */ |
af09c060 | 113 | priv->phyclk = devm_clk_get(&pdev->dev, "phy"); |
c943740c SH |
114 | if (IS_ERR(priv->phyclk)) |
115 | priv->phyclk = NULL; | |
116 | if (priv->phyclk) | |
117 | clk_prepare_enable(priv->phyclk); | |
711669e5 APR |
118 | |
119 | ||
120 | /* call platform specific init function */ | |
121 | if (pdata->init) { | |
122 | ret = pdata->init(pdev); | |
123 | if (ret) { | |
124 | dev_err(dev, "platform init failed\n"); | |
125 | goto err_init; | |
126 | } | |
127 | /* platforms need some time to settle changed IO settings */ | |
128 | mdelay(10); | |
129 | } | |
130 | ||
0247a7bc FE |
131 | /* EHCI registers start at offset 0x100 */ |
132 | ehci->caps = hcd->regs + 0x100; | |
133 | ehci->regs = hcd->regs + 0x100 + | |
c430131a | 134 | HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase)); |
0247a7bc FE |
135 | |
136 | /* set up the PORTSCx register */ | |
137 | ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]); | |
138 | ||
139 | /* is this really needed? */ | |
140 | msleep(10); | |
141 | ||
7e8d5cd9 DM |
142 | /* Initialize the transceiver */ |
143 | if (pdata->otg) { | |
144 | pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET; | |
b96d3b08 | 145 | ret = usb_phy_init(pdata->otg); |
4c9715de WS |
146 | if (ret) { |
147 | dev_err(dev, "unable to init transceiver, probably missing\n"); | |
148 | ret = -ENODEV; | |
149 | goto err_add; | |
150 | } | |
6e13c650 | 151 | ret = otg_set_vbus(pdata->otg->otg, 1); |
4c9715de | 152 | if (ret) { |
7e8d5cd9 | 153 | dev_err(dev, "unable to enable vbus on transceiver\n"); |
4c9715de WS |
154 | goto err_add; |
155 | } | |
7e8d5cd9 DM |
156 | } |
157 | ||
dba63b2f | 158 | platform_set_drvdata(pdev, hcd); |
7e8d5cd9 | 159 | |
b5dd18d8 | 160 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); |
7e8d5cd9 DM |
161 | if (ret) |
162 | goto err_add; | |
163 | ||
164 | return 0; | |
165 | ||
166 | err_add: | |
167 | if (pdata && pdata->exit) | |
168 | pdata->exit(pdev); | |
169 | err_init: | |
af09c060 | 170 | if (priv->phyclk) |
c943740c | 171 | clk_disable_unprepare(priv->phyclk); |
c943740c SH |
172 | |
173 | clk_disable_unprepare(priv->ahbclk); | |
7e8d5cd9 | 174 | err_clk_ahb: |
198ad2ce | 175 | clk_disable_unprepare(priv->usbclk); |
7e8d5cd9 DM |
176 | err_alloc: |
177 | usb_put_hcd(hcd); | |
178 | return ret; | |
179 | } | |
180 | ||
181 | static int __exit ehci_mxc_drv_remove(struct platform_device *pdev) | |
182 | { | |
183 | struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data; | |
dba63b2f AS |
184 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
185 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); | |
186 | struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv; | |
187 | ||
188 | usb_remove_hcd(hcd); | |
7e8d5cd9 DM |
189 | |
190 | if (pdata && pdata->exit) | |
191 | pdata->exit(pdev); | |
192 | ||
193 | if (pdata->otg) | |
b96d3b08 | 194 | usb_phy_shutdown(pdata->otg); |
7e8d5cd9 | 195 | |
198ad2ce | 196 | clk_disable_unprepare(priv->usbclk); |
c943740c | 197 | clk_disable_unprepare(priv->ahbclk); |
c943740c | 198 | |
af09c060 | 199 | if (priv->phyclk) |
c943740c | 200 | clk_disable_unprepare(priv->phyclk); |
7e8d5cd9 | 201 | |
dba63b2f AS |
202 | usb_put_hcd(hcd); |
203 | platform_set_drvdata(pdev, NULL); | |
7e8d5cd9 DM |
204 | return 0; |
205 | } | |
206 | ||
207 | static void ehci_mxc_drv_shutdown(struct platform_device *pdev) | |
208 | { | |
dba63b2f | 209 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
7e8d5cd9 DM |
210 | |
211 | if (hcd->driver->shutdown) | |
212 | hcd->driver->shutdown(hcd); | |
213 | } | |
214 | ||
215 | MODULE_ALIAS("platform:mxc-ehci"); | |
216 | ||
217 | static struct platform_driver ehci_mxc_driver = { | |
218 | .probe = ehci_mxc_drv_probe, | |
dba63b2f | 219 | .remove = ehci_mxc_drv_remove, |
7e8d5cd9 DM |
220 | .shutdown = ehci_mxc_drv_shutdown, |
221 | .driver = { | |
222 | .name = "mxc-ehci", | |
223 | }, | |
224 | }; | |
dba63b2f AS |
225 | |
226 | static int __init ehci_mxc_init(void) | |
227 | { | |
228 | if (usb_disabled()) | |
229 | return -ENODEV; | |
230 | ||
231 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); | |
232 | ||
233 | ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides); | |
234 | return platform_driver_register(&ehci_mxc_driver); | |
235 | } | |
236 | module_init(ehci_mxc_init); | |
237 | ||
238 | static void __exit ehci_mxc_cleanup(void) | |
239 | { | |
240 | platform_driver_unregister(&ehci_mxc_driver); | |
241 | } | |
242 | module_exit(ehci_mxc_cleanup); | |
243 | ||
244 | MODULE_DESCRIPTION(DRIVER_DESC); | |
245 | MODULE_AUTHOR("Sascha Hauer"); | |
246 | MODULE_LICENSE("GPL"); |