USB: usb peripheral controller driver oops avoidance
[deliverable/linux.git] / drivers / usb / host / ehci-fsl.c
CommitLineData
80cb9aee
RV
1/*
2 * (C) Copyright David Brownell 2000-2002
3 * Copyright (c) 2005 MontaVista Software
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 * Ported to 834x by Randy Vinson <rvinson@mvista.com> using code provided
20 * by Hunter Wu.
21 */
22
23#include <linux/platform_device.h>
24#include <linux/fsl_devices.h>
25
26#include "ehci-fsl.h"
27
28/* FIXME: Power Managment is un-ported so temporarily disable it */
29#undef CONFIG_PM
30
31/* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
32
33/* configure so an HC device and id are always provided */
34/* always called with process context; sleeping is OK */
35
36/**
37 * usb_hcd_fsl_probe - initialize FSL-based HCDs
38 * @drvier: Driver to be used for this HCD
39 * @pdev: USB Host Controller being probed
40 * Context: !in_interrupt()
41 *
42 * Allocates basic resources for this USB host controller.
43 *
44 */
45int usb_hcd_fsl_probe(const struct hc_driver *driver,
46 struct platform_device *pdev)
47{
48 struct fsl_usb2_platform_data *pdata;
49 struct usb_hcd *hcd;
50 struct resource *res;
51 int irq;
52 int retval;
53 unsigned int temp;
54
55 pr_debug("initializing FSL-SOC USB Controller\n");
56
57 /* Need platform data for setup */
58 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
59 if (!pdata) {
60 dev_err(&pdev->dev,
61 "No platform data for %s.\n", pdev->dev.bus_id);
62 return -ENODEV;
63 }
64
65 /*
66 * This is a host mode driver, verify that we're supposed to be
67 * in host mode.
68 */
69 if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
ba02978a
LY
70 (pdata->operating_mode == FSL_USB2_MPH_HOST) ||
71 (pdata->operating_mode == FSL_USB2_DR_OTG))) {
80cb9aee
RV
72 dev_err(&pdev->dev,
73 "Non Host Mode configured for %s. Wrong driver linked.\n",
74 pdev->dev.bus_id);
75 return -ENODEV;
76 }
77
78 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
79 if (!res) {
80 dev_err(&pdev->dev,
81 "Found HC with no IRQ. Check %s setup!\n",
82 pdev->dev.bus_id);
83 return -ENODEV;
84 }
85 irq = res->start;
86
87 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id);
88 if (!hcd) {
89 retval = -ENOMEM;
90 goto err1;
91 }
92
93 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 if (!res) {
95 dev_err(&pdev->dev,
96 "Found HC with no register addr. Check %s setup!\n",
97 pdev->dev.bus_id);
98 retval = -ENODEV;
99 goto err2;
100 }
101 hcd->rsrc_start = res->start;
102 hcd->rsrc_len = res->end - res->start + 1;
103 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
104 driver->description)) {
105 dev_dbg(&pdev->dev, "controller already in use\n");
106 retval = -EBUSY;
107 goto err2;
108 }
109 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
110
111 if (hcd->regs == NULL) {
112 dev_dbg(&pdev->dev, "error mapping memory\n");
113 retval = -EFAULT;
114 goto err3;
115 }
116
117 /* Enable USB controller */
118 temp = in_be32(hcd->regs + 0x500);
119 out_be32(hcd->regs + 0x500, temp | 0x4);
120
121 /* Set to Host mode */
122 temp = in_le32(hcd->regs + 0x1a8);
123 out_le32(hcd->regs + 0x1a8, temp | 0x3);
124
442258e2 125 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
80cb9aee
RV
126 if (retval != 0)
127 goto err4;
128 return retval;
129
130 err4:
131 iounmap(hcd->regs);
132 err3:
133 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
134 err2:
135 usb_put_hcd(hcd);
136 err1:
137 dev_err(&pdev->dev, "init %s fail, %d\n", pdev->dev.bus_id, retval);
138 return retval;
139}
140
141/* may be called without controller electrically present */
142/* may be called with controller, bus, and devices active */
143
144/**
145 * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
146 * @dev: USB Host Controller being removed
147 * Context: !in_interrupt()
148 *
149 * Reverses the effect of usb_hcd_fsl_probe().
150 *
151 */
152void usb_hcd_fsl_remove(struct usb_hcd *hcd, struct platform_device *pdev)
153{
154 usb_remove_hcd(hcd);
155 iounmap(hcd->regs);
156 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
157 usb_put_hcd(hcd);
158}
159
160static void mpc83xx_setup_phy(struct ehci_hcd *ehci,
161 enum fsl_usb2_phy_modes phy_mode,
162 unsigned int port_offset)
163{
499003e8 164 u32 portsc = 0;
80cb9aee
RV
165 switch (phy_mode) {
166 case FSL_USB2_PHY_ULPI:
167 portsc |= PORT_PTS_ULPI;
168 break;
169 case FSL_USB2_PHY_SERIAL:
170 portsc |= PORT_PTS_SERIAL;
171 break;
172 case FSL_USB2_PHY_UTMI_WIDE:
173 portsc |= PORT_PTS_PTW;
174 /* fall through */
175 case FSL_USB2_PHY_UTMI:
176 portsc |= PORT_PTS_UTMI;
177 break;
178 case FSL_USB2_PHY_NONE:
179 break;
180 }
083522d7 181 ehci_writel(ehci, portsc, &ehci->regs->port_status[port_offset]);
80cb9aee
RV
182}
183
184static void mpc83xx_usb_setup(struct usb_hcd *hcd)
185{
186 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
187 struct fsl_usb2_platform_data *pdata;
188 void __iomem *non_ehci = hcd->regs;
ba02978a 189 u32 temp;
80cb9aee
RV
190
191 pdata =
192 (struct fsl_usb2_platform_data *)hcd->self.controller->
193 platform_data;
194 /* Enable PHY interface in the control reg. */
ba02978a
LY
195 temp = in_be32(non_ehci + FSL_SOC_USB_CTRL);
196 out_be32(non_ehci + FSL_SOC_USB_CTRL, temp | 0x00000004);
80cb9aee
RV
197 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0000001b);
198
40acc095
LY
199#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
200 /*
201 * Turn on cache snooping hardware, since some PowerPC platforms
202 * wholly rely on hardware to deal with cache coherent
203 */
204
205 /* Setup Snooping for all the 4GB space */
206 /* SNOOP1 starts from 0x0, size 2G */
207 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0 | SNOOP_SIZE_2GB);
208 /* SNOOP2 starts from 0x80000000, size 2G */
209 out_be32(non_ehci + FSL_SOC_USB_SNOOP2, 0x80000000 | SNOOP_SIZE_2GB);
210#endif
211
ba02978a
LY
212 if ((pdata->operating_mode == FSL_USB2_DR_HOST) ||
213 (pdata->operating_mode == FSL_USB2_DR_OTG))
80cb9aee
RV
214 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
215
216 if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
8cd42e97
KG
217 unsigned int chip, rev, svr;
218
219 svr = mfspr(SPRN_SVR);
220 chip = svr >> 16;
221 rev = (svr >> 4) & 0xf;
222
223 /* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
224 if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
225 ehci->has_fsl_port_bug = 1;
226
80cb9aee
RV
227 if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
228 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
229 if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
230 mpc83xx_setup_phy(ehci, pdata->phy_mode, 1);
231 }
232
233 /* put controller in host mode. */
083522d7 234 ehci_writel(ehci, 0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
80cb9aee
RV
235 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
236 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
237 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
238}
239
240/* called after powerup, by probe or system-pm "wakeup" */
241static int ehci_fsl_reinit(struct ehci_hcd *ehci)
242{
243 mpc83xx_usb_setup(ehci_to_hcd(ehci));
244 ehci_port_power(ehci, 0);
245
246 return 0;
247}
248
249/* called during probe() after chip reset completes */
250static int ehci_fsl_setup(struct usb_hcd *hcd)
251{
252 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
253 int retval;
254
255 /* EHCI registers start at offset 0x100 */
256 ehci->caps = hcd->regs + 0x100;
257 ehci->regs = hcd->regs + 0x100 +
083522d7 258 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
80cb9aee
RV
259 dbg_hcs_params(ehci, "reset");
260 dbg_hcc_params(ehci, "reset");
261
262 /* cache this readonly data; minimize chip reads */
083522d7 263 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
80cb9aee
RV
264
265 retval = ehci_halt(ehci);
266 if (retval)
267 return retval;
268
269 /* data structure init */
270 retval = ehci_init(hcd);
271 if (retval)
272 return retval;
273
274 ehci->is_tdi_rh_tt = 1;
275
276 ehci->sbrn = 0x20;
277
278 ehci_reset(ehci);
279
280 retval = ehci_fsl_reinit(ehci);
281 return retval;
282}
283
284static const struct hc_driver ehci_fsl_hc_driver = {
285 .description = hcd_name,
286 .product_desc = "Freescale On-Chip EHCI Host Controller",
287 .hcd_priv_size = sizeof(struct ehci_hcd),
288
289 /*
290 * generic hardware linkage
291 */
292 .irq = ehci_irq,
293 .flags = HCD_USB2,
294
295 /*
296 * basic lifecycle operations
297 */
298 .reset = ehci_fsl_setup,
299 .start = ehci_run,
300#ifdef CONFIG_PM
301 .suspend = ehci_bus_suspend,
302 .resume = ehci_bus_resume,
303#endif
304 .stop = ehci_stop,
64a21d02 305 .shutdown = ehci_shutdown,
80cb9aee
RV
306
307 /*
308 * managing i/o requests and associated device resources
309 */
310 .urb_enqueue = ehci_urb_enqueue,
311 .urb_dequeue = ehci_urb_dequeue,
312 .endpoint_disable = ehci_endpoint_disable,
313
314 /*
315 * scheduling support
316 */
317 .get_frame_number = ehci_get_frame,
318
319 /*
320 * root hub support
321 */
322 .hub_status_data = ehci_hub_status_data,
323 .hub_control = ehci_hub_control,
324 .bus_suspend = ehci_bus_suspend,
325 .bus_resume = ehci_bus_resume,
326};
327
328static int ehci_fsl_drv_probe(struct platform_device *pdev)
329{
330 if (usb_disabled())
331 return -ENODEV;
332
333 return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
334}
335
336static int ehci_fsl_drv_remove(struct platform_device *pdev)
337{
338 struct usb_hcd *hcd = platform_get_drvdata(pdev);
339
340 usb_hcd_fsl_remove(hcd, pdev);
341
342 return 0;
343}
344
01cced25 345MODULE_ALIAS("fsl-ehci");
80cb9aee 346
01cced25 347static struct platform_driver ehci_fsl_driver = {
80cb9aee
RV
348 .probe = ehci_fsl_drv_probe,
349 .remove = ehci_fsl_drv_remove,
64a21d02 350 .shutdown = usb_hcd_platform_shutdown,
80cb9aee 351 .driver = {
01cced25 352 .name = "fsl-ehci",
80cb9aee
RV
353 },
354};
This page took 0.248307 seconds and 5 git commands to generate.