blob: 0e26aa13f1580c308351c208e3d8eb3bdc8ef566 [file] [log] [blame]
Randy Vinson80cb9ae2006-01-20 13:53:38 -08001/*
Anton Vorontsov1af10772009-12-14 18:41:12 +03002 * Copyright 2005-2009 MontaVista Software, Inc.
3 * Copyright 2008 Freescale Semiconductor, Inc.
Randy Vinson80cb9ae2006-01-20 13:53:38 -08004 *
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.
Anton Vorontsov1af10772009-12-14 18:41:12 +030021 * Power Management support by Dave Liu <daveliu@freescale.com>,
22 * Jerry Huang <Chang-Ming.Huang@freescale.com> and
23 * Anton Vorontsov <avorontsov@ru.mvista.com>.
Randy Vinson80cb9ae2006-01-20 13:53:38 -080024 */
25
Anton Vorontsov1af10772009-12-14 18:41:12 +030026#include <linux/kernel.h>
27#include <linux/types.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
Randy Vinson80cb9ae2006-01-20 13:53:38 -080030#include <linux/platform_device.h>
31#include <linux/fsl_devices.h>
32
33#include "ehci-fsl.h"
34
Randy Vinson80cb9ae2006-01-20 13:53:38 -080035/* configure so an HC device and id are always provided */
36/* always called with process context; sleeping is OK */
37
38/**
39 * usb_hcd_fsl_probe - initialize FSL-based HCDs
40 * @drvier: Driver to be used for this HCD
41 * @pdev: USB Host Controller being probed
42 * Context: !in_interrupt()
43 *
44 * Allocates basic resources for this USB host controller.
45 *
46 */
Anton Vorontsovdad38432009-12-14 18:41:05 +030047static int usb_hcd_fsl_probe(const struct hc_driver *driver,
48 struct platform_device *pdev)
Randy Vinson80cb9ae2006-01-20 13:53:38 -080049{
50 struct fsl_usb2_platform_data *pdata;
51 struct usb_hcd *hcd;
52 struct resource *res;
53 int irq;
54 int retval;
55 unsigned int temp;
56
57 pr_debug("initializing FSL-SOC USB Controller\n");
58
59 /* Need platform data for setup */
60 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
61 if (!pdata) {
62 dev_err(&pdev->dev,
Kay Sievers7071a3c2008-05-02 06:02:41 +020063 "No platform data for %s.\n", dev_name(&pdev->dev));
Randy Vinson80cb9ae2006-01-20 13:53:38 -080064 return -ENODEV;
65 }
66
67 /*
68 * This is a host mode driver, verify that we're supposed to be
69 * in host mode.
70 */
71 if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
Li Yangba029782007-05-11 17:09:55 +080072 (pdata->operating_mode == FSL_USB2_MPH_HOST) ||
73 (pdata->operating_mode == FSL_USB2_DR_OTG))) {
Randy Vinson80cb9ae2006-01-20 13:53:38 -080074 dev_err(&pdev->dev,
75 "Non Host Mode configured for %s. Wrong driver linked.\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +020076 dev_name(&pdev->dev));
Randy Vinson80cb9ae2006-01-20 13:53:38 -080077 return -ENODEV;
78 }
79
80 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
81 if (!res) {
82 dev_err(&pdev->dev,
83 "Found HC with no IRQ. Check %s setup!\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +020084 dev_name(&pdev->dev));
Randy Vinson80cb9ae2006-01-20 13:53:38 -080085 return -ENODEV;
86 }
87 irq = res->start;
88
Kay Sievers7071a3c2008-05-02 06:02:41 +020089 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
Randy Vinson80cb9ae2006-01-20 13:53:38 -080090 if (!hcd) {
91 retval = -ENOMEM;
92 goto err1;
93 }
94
95 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
96 if (!res) {
97 dev_err(&pdev->dev,
98 "Found HC with no register addr. Check %s setup!\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +020099 dev_name(&pdev->dev));
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800100 retval = -ENODEV;
101 goto err2;
102 }
103 hcd->rsrc_start = res->start;
104 hcd->rsrc_len = res->end - res->start + 1;
105 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
106 driver->description)) {
107 dev_dbg(&pdev->dev, "controller already in use\n");
108 retval = -EBUSY;
109 goto err2;
110 }
111 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
112
113 if (hcd->regs == NULL) {
114 dev_dbg(&pdev->dev, "error mapping memory\n");
115 retval = -EFAULT;
116 goto err3;
117 }
118
119 /* Enable USB controller */
120 temp = in_be32(hcd->regs + 0x500);
121 out_be32(hcd->regs + 0x500, temp | 0x4);
122
123 /* Set to Host mode */
124 temp = in_le32(hcd->regs + 0x1a8);
125 out_le32(hcd->regs + 0x1a8, temp | 0x3);
126
Alan Stern442258e2007-12-06 14:47:08 -0500127 retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800128 if (retval != 0)
129 goto err4;
130 return retval;
131
132 err4:
133 iounmap(hcd->regs);
134 err3:
135 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
136 err2:
137 usb_put_hcd(hcd);
138 err1:
Kay Sievers7071a3c2008-05-02 06:02:41 +0200139 dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800140 return retval;
141}
142
143/* may be called without controller electrically present */
144/* may be called with controller, bus, and devices active */
145
146/**
147 * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
148 * @dev: USB Host Controller being removed
149 * Context: !in_interrupt()
150 *
151 * Reverses the effect of usb_hcd_fsl_probe().
152 *
153 */
Anton Vorontsovdad38432009-12-14 18:41:05 +0300154static void usb_hcd_fsl_remove(struct usb_hcd *hcd,
155 struct platform_device *pdev)
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800156{
157 usb_remove_hcd(hcd);
158 iounmap(hcd->regs);
159 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
160 usb_put_hcd(hcd);
161}
162
163static void mpc83xx_setup_phy(struct ehci_hcd *ehci,
164 enum fsl_usb2_phy_modes phy_mode,
165 unsigned int port_offset)
166{
Kumar Gala499003e82006-01-24 08:11:27 -0800167 u32 portsc = 0;
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800168 switch (phy_mode) {
169 case FSL_USB2_PHY_ULPI:
170 portsc |= PORT_PTS_ULPI;
171 break;
172 case FSL_USB2_PHY_SERIAL:
173 portsc |= PORT_PTS_SERIAL;
174 break;
175 case FSL_USB2_PHY_UTMI_WIDE:
176 portsc |= PORT_PTS_PTW;
177 /* fall through */
178 case FSL_USB2_PHY_UTMI:
179 portsc |= PORT_PTS_UTMI;
180 break;
181 case FSL_USB2_PHY_NONE:
182 break;
183 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100184 ehci_writel(ehci, portsc, &ehci->regs->port_status[port_offset]);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800185}
186
187static void mpc83xx_usb_setup(struct usb_hcd *hcd)
188{
189 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
190 struct fsl_usb2_platform_data *pdata;
191 void __iomem *non_ehci = hcd->regs;
Li Yangba029782007-05-11 17:09:55 +0800192 u32 temp;
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800193
194 pdata =
195 (struct fsl_usb2_platform_data *)hcd->self.controller->
196 platform_data;
197 /* Enable PHY interface in the control reg. */
Li Yangba029782007-05-11 17:09:55 +0800198 temp = in_be32(non_ehci + FSL_SOC_USB_CTRL);
199 out_be32(non_ehci + FSL_SOC_USB_CTRL, temp | 0x00000004);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800200 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0000001b);
201
Li Yang40acc092007-05-23 13:58:17 -0700202#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
203 /*
204 * Turn on cache snooping hardware, since some PowerPC platforms
205 * wholly rely on hardware to deal with cache coherent
206 */
207
208 /* Setup Snooping for all the 4GB space */
209 /* SNOOP1 starts from 0x0, size 2G */
210 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0 | SNOOP_SIZE_2GB);
211 /* SNOOP2 starts from 0x80000000, size 2G */
212 out_be32(non_ehci + FSL_SOC_USB_SNOOP2, 0x80000000 | SNOOP_SIZE_2GB);
213#endif
214
Li Yangba029782007-05-11 17:09:55 +0800215 if ((pdata->operating_mode == FSL_USB2_DR_HOST) ||
216 (pdata->operating_mode == FSL_USB2_DR_OTG))
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800217 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
218
219 if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
Kumar Gala8cd42e92006-01-20 13:57:52 -0800220 unsigned int chip, rev, svr;
221
222 svr = mfspr(SPRN_SVR);
223 chip = svr >> 16;
224 rev = (svr >> 4) & 0xf;
225
226 /* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
227 if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
228 ehci->has_fsl_port_bug = 1;
229
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800230 if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
231 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
232 if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
233 mpc83xx_setup_phy(ehci, pdata->phy_mode, 1);
234 }
235
236 /* put controller in host mode. */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100237 ehci_writel(ehci, 0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
Srikanth Srinivasan4f534252008-07-02 02:14:33 -0500238#ifdef CONFIG_PPC_85xx
239 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x00000008);
240 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000080);
241#else
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800242 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
243 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
Srikanth Srinivasan4f534252008-07-02 02:14:33 -0500244#endif
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800245 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
246}
247
248/* called after powerup, by probe or system-pm "wakeup" */
249static int ehci_fsl_reinit(struct ehci_hcd *ehci)
250{
251 mpc83xx_usb_setup(ehci_to_hcd(ehci));
252 ehci_port_power(ehci, 0);
253
254 return 0;
255}
256
257/* called during probe() after chip reset completes */
258static int ehci_fsl_setup(struct usb_hcd *hcd)
259{
260 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
261 int retval;
262
263 /* EHCI registers start at offset 0x100 */
264 ehci->caps = hcd->regs + 0x100;
265 ehci->regs = hcd->regs + 0x100 +
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100266 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800267 dbg_hcs_params(ehci, "reset");
268 dbg_hcc_params(ehci, "reset");
269
270 /* cache this readonly data; minimize chip reads */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100271 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800272
273 retval = ehci_halt(ehci);
274 if (retval)
275 return retval;
276
277 /* data structure init */
278 retval = ehci_init(hcd);
279 if (retval)
280 return retval;
281
Alan Sterna8e51772008-05-20 16:58:11 -0400282 hcd->has_tt = 1;
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800283
284 ehci->sbrn = 0x20;
285
286 ehci_reset(ehci);
287
288 retval = ehci_fsl_reinit(ehci);
289 return retval;
290}
291
Anton Vorontsov1af10772009-12-14 18:41:12 +0300292struct ehci_fsl {
293 struct ehci_hcd ehci;
294
295#ifdef CONFIG_PM
296 /* Saved USB PHY settings, need to restore after deep sleep. */
297 u32 usb_ctrl;
298#endif
299};
300
301#ifdef CONFIG_PM
302
303static struct ehci_fsl *hcd_to_ehci_fsl(struct usb_hcd *hcd)
304{
305 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
306
307 return container_of(ehci, struct ehci_fsl, ehci);
308}
309
310static int ehci_fsl_drv_suspend(struct device *dev)
311{
312 struct usb_hcd *hcd = dev_get_drvdata(dev);
313 struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
314 void __iomem *non_ehci = hcd->regs;
315
316 if (!fsl_deep_sleep())
317 return 0;
318
319 ehci_fsl->usb_ctrl = in_be32(non_ehci + FSL_SOC_USB_CTRL);
320 return 0;
321}
322
323static int ehci_fsl_drv_resume(struct device *dev)
324{
325 struct usb_hcd *hcd = dev_get_drvdata(dev);
326 struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
327 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
328 void __iomem *non_ehci = hcd->regs;
329
330 if (!fsl_deep_sleep())
331 return 0;
332
333 usb_root_hub_lost_power(hcd->self.root_hub);
334
335 /* Restore USB PHY settings and enable the controller. */
336 out_be32(non_ehci + FSL_SOC_USB_CTRL, ehci_fsl->usb_ctrl);
337
338 ehci_reset(ehci);
339 ehci_fsl_reinit(ehci);
340
341 return 0;
342}
343
344static int ehci_fsl_drv_restore(struct device *dev)
345{
346 struct usb_hcd *hcd = dev_get_drvdata(dev);
347
348 usb_root_hub_lost_power(hcd->self.root_hub);
349 return 0;
350}
351
352static struct dev_pm_ops ehci_fsl_pm_ops = {
353 .suspend = ehci_fsl_drv_suspend,
354 .resume = ehci_fsl_drv_resume,
355 .restore = ehci_fsl_drv_restore,
356};
357
358#define EHCI_FSL_PM_OPS (&ehci_fsl_pm_ops)
359#else
360#define EHCI_FSL_PM_OPS NULL
361#endif /* CONFIG_PM */
362
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800363static const struct hc_driver ehci_fsl_hc_driver = {
364 .description = hcd_name,
365 .product_desc = "Freescale On-Chip EHCI Host Controller",
Anton Vorontsov1af10772009-12-14 18:41:12 +0300366 .hcd_priv_size = sizeof(struct ehci_fsl),
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800367
368 /*
369 * generic hardware linkage
370 */
371 .irq = ehci_irq,
372 .flags = HCD_USB2,
373
374 /*
375 * basic lifecycle operations
376 */
377 .reset = ehci_fsl_setup,
378 .start = ehci_run,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800379 .stop = ehci_stop,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700380 .shutdown = ehci_shutdown,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800381
382 /*
383 * managing i/o requests and associated device resources
384 */
385 .urb_enqueue = ehci_urb_enqueue,
386 .urb_dequeue = ehci_urb_dequeue,
387 .endpoint_disable = ehci_endpoint_disable,
Alan Sternb18ffd42009-05-27 18:21:56 -0400388 .endpoint_reset = ehci_endpoint_reset,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800389
390 /*
391 * scheduling support
392 */
393 .get_frame_number = ehci_get_frame,
394
395 /*
396 * root hub support
397 */
398 .hub_status_data = ehci_hub_status_data,
399 .hub_control = ehci_hub_control,
400 .bus_suspend = ehci_bus_suspend,
401 .bus_resume = ehci_bus_resume,
Balaji Rao90da0962007-11-22 01:58:14 +0530402 .relinquish_port = ehci_relinquish_port,
Alan Stern3a311552008-05-20 16:58:29 -0400403 .port_handed_over = ehci_port_handed_over,
Alan Stern914b7012009-06-29 10:47:30 -0400404
405 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800406};
407
408static int ehci_fsl_drv_probe(struct platform_device *pdev)
409{
410 if (usb_disabled())
411 return -ENODEV;
412
David Brownell135db042008-02-11 18:40:46 -0800413 /* FIXME we only want one one probe() not two */
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800414 return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
415}
416
417static int ehci_fsl_drv_remove(struct platform_device *pdev)
418{
419 struct usb_hcd *hcd = platform_get_drvdata(pdev);
420
David Brownell135db042008-02-11 18:40:46 -0800421 /* FIXME we only want one one remove() not two */
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800422 usb_hcd_fsl_remove(hcd, pdev);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800423 return 0;
424}
425
David Brownell135db042008-02-11 18:40:46 -0800426MODULE_ALIAS("platform:fsl-ehci");
Kumar Gala01cced22006-04-11 10:07:16 -0500427
428static struct platform_driver ehci_fsl_driver = {
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800429 .probe = ehci_fsl_drv_probe,
430 .remove = ehci_fsl_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700431 .shutdown = usb_hcd_platform_shutdown,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800432 .driver = {
Anton Vorontsov1af10772009-12-14 18:41:12 +0300433 .name = "fsl-ehci",
434 .pm = EHCI_FSL_PM_OPS,
David Brownell135db042008-02-11 18:40:46 -0800435 },
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800436};