blob: 8600317bd60b6619065d175ec7eeaf9d58ada47c [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
Srikanth Srinivasan4f534252008-07-02 02:14:33 -0500236#ifdef CONFIG_PPC_85xx
237 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x00000008);
238 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000080);
239#else
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800240 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
241 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
Srikanth Srinivasan4f534252008-07-02 02:14:33 -0500242#endif
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800243 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
244}
245
246/* called after powerup, by probe or system-pm "wakeup" */
247static int ehci_fsl_reinit(struct ehci_hcd *ehci)
248{
249 mpc83xx_usb_setup(ehci_to_hcd(ehci));
250 ehci_port_power(ehci, 0);
251
252 return 0;
253}
254
255/* called during probe() after chip reset completes */
256static int ehci_fsl_setup(struct usb_hcd *hcd)
257{
258 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
259 int retval;
260
261 /* EHCI registers start at offset 0x100 */
262 ehci->caps = hcd->regs + 0x100;
263 ehci->regs = hcd->regs + 0x100 +
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100264 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800265 dbg_hcs_params(ehci, "reset");
266 dbg_hcc_params(ehci, "reset");
267
268 /* cache this readonly data; minimize chip reads */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100269 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800270
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200271 hcd->has_tt = 1;
272
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800273 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
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800282 ehci->sbrn = 0x20;
283
284 ehci_reset(ehci);
285
286 retval = ehci_fsl_reinit(ehci);
287 return retval;
288}
289
Anton Vorontsov1af10772009-12-14 18:41:12 +0300290struct ehci_fsl {
291 struct ehci_hcd ehci;
292
293#ifdef CONFIG_PM
294 /* Saved USB PHY settings, need to restore after deep sleep. */
295 u32 usb_ctrl;
296#endif
297};
298
299#ifdef CONFIG_PM
300
301static struct ehci_fsl *hcd_to_ehci_fsl(struct usb_hcd *hcd)
302{
303 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
304
305 return container_of(ehci, struct ehci_fsl, ehci);
306}
307
308static int ehci_fsl_drv_suspend(struct device *dev)
309{
310 struct usb_hcd *hcd = dev_get_drvdata(dev);
311 struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
312 void __iomem *non_ehci = hcd->regs;
313
Alan Stern41472002010-06-25 14:02:14 -0400314 ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd),
315 device_may_wakeup(dev));
Anton Vorontsov1af10772009-12-14 18:41:12 +0300316 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
Alan Stern16032c42010-05-12 18:21:35 -0400330 ehci_prepare_ports_for_controller_resume(ehci);
Anton Vorontsov1af10772009-12-14 18:41:12 +0300331 if (!fsl_deep_sleep())
332 return 0;
333
334 usb_root_hub_lost_power(hcd->self.root_hub);
335
336 /* Restore USB PHY settings and enable the controller. */
337 out_be32(non_ehci + FSL_SOC_USB_CTRL, ehci_fsl->usb_ctrl);
338
339 ehci_reset(ehci);
340 ehci_fsl_reinit(ehci);
341
342 return 0;
343}
344
345static int ehci_fsl_drv_restore(struct device *dev)
346{
347 struct usb_hcd *hcd = dev_get_drvdata(dev);
348
349 usb_root_hub_lost_power(hcd->self.root_hub);
350 return 0;
351}
352
353static struct dev_pm_ops ehci_fsl_pm_ops = {
354 .suspend = ehci_fsl_drv_suspend,
355 .resume = ehci_fsl_drv_resume,
356 .restore = ehci_fsl_drv_restore,
357};
358
359#define EHCI_FSL_PM_OPS (&ehci_fsl_pm_ops)
360#else
361#define EHCI_FSL_PM_OPS NULL
362#endif /* CONFIG_PM */
363
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800364static const struct hc_driver ehci_fsl_hc_driver = {
365 .description = hcd_name,
366 .product_desc = "Freescale On-Chip EHCI Host Controller",
Anton Vorontsov1af10772009-12-14 18:41:12 +0300367 .hcd_priv_size = sizeof(struct ehci_fsl),
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800368
369 /*
370 * generic hardware linkage
371 */
372 .irq = ehci_irq,
373 .flags = HCD_USB2,
374
375 /*
376 * basic lifecycle operations
377 */
378 .reset = ehci_fsl_setup,
379 .start = ehci_run,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800380 .stop = ehci_stop,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700381 .shutdown = ehci_shutdown,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800382
383 /*
384 * managing i/o requests and associated device resources
385 */
386 .urb_enqueue = ehci_urb_enqueue,
387 .urb_dequeue = ehci_urb_dequeue,
388 .endpoint_disable = ehci_endpoint_disable,
Alan Sternb18ffd42009-05-27 18:21:56 -0400389 .endpoint_reset = ehci_endpoint_reset,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800390
391 /*
392 * scheduling support
393 */
394 .get_frame_number = ehci_get_frame,
395
396 /*
397 * root hub support
398 */
399 .hub_status_data = ehci_hub_status_data,
400 .hub_control = ehci_hub_control,
401 .bus_suspend = ehci_bus_suspend,
402 .bus_resume = ehci_bus_resume,
Balaji Rao90da0962007-11-22 01:58:14 +0530403 .relinquish_port = ehci_relinquish_port,
Alan Stern3a311552008-05-20 16:58:29 -0400404 .port_handed_over = ehci_port_handed_over,
Alan Stern914b7012009-06-29 10:47:30 -0400405
406 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800407};
408
409static int ehci_fsl_drv_probe(struct platform_device *pdev)
410{
411 if (usb_disabled())
412 return -ENODEV;
413
David Brownell135db042008-02-11 18:40:46 -0800414 /* FIXME we only want one one probe() not two */
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800415 return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
416}
417
418static int ehci_fsl_drv_remove(struct platform_device *pdev)
419{
420 struct usb_hcd *hcd = platform_get_drvdata(pdev);
421
David Brownell135db042008-02-11 18:40:46 -0800422 /* FIXME we only want one one remove() not two */
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800423 usb_hcd_fsl_remove(hcd, pdev);
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800424 return 0;
425}
426
David Brownell135db042008-02-11 18:40:46 -0800427MODULE_ALIAS("platform:fsl-ehci");
Kumar Gala01cced22006-04-11 10:07:16 -0500428
429static struct platform_driver ehci_fsl_driver = {
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800430 .probe = ehci_fsl_drv_probe,
431 .remove = ehci_fsl_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700432 .shutdown = usb_hcd_platform_shutdown,
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800433 .driver = {
Anton Vorontsov1af10772009-12-14 18:41:12 +0300434 .name = "fsl-ehci",
435 .pm = EHCI_FSL_PM_OPS,
David Brownell135db042008-02-11 18:40:46 -0800436 },
Randy Vinson80cb9ae2006-01-20 13:53:38 -0800437};