xref: /openbmc/linux/drivers/usb/host/ohci-pxa27x.c (revision 545e4006)
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6  * (C) Copyright 2002 Hewlett-Packard Company
7  *
8  * Bus Glue for pxa27x
9  *
10  * Written by Christopher Hoover <ch@hpl.hp.com>
11  * Based on fragments of previous driver by Russell King et al.
12  *
13  * Modified for LH7A404 from ohci-sa1111.c
14  *  by Durgesh Pattamatta <pattamattad@sharpsec.com>
15  *
16  * Modified for pxa27x from ohci-lh7a404.c
17  *  by Nick Bane <nick@cecomputing.co.uk> 26-8-2004
18  *
19  * This file is licenced under the GPL.
20  */
21 
22 #include <linux/device.h>
23 #include <linux/signal.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 
27 #include <asm/mach-types.h>
28 #include <asm/hardware.h>
29 #include <asm/arch/pxa-regs.h>
30 #include <asm/arch/pxa2xx-regs.h> /* FIXME: for PSSR */
31 #include <asm/arch/ohci.h>
32 
33 #define PXA_UHC_MAX_PORTNUM    3
34 
35 #define UHCRHPS(x)              __REG2( 0x4C000050, (x)<<2 )
36 
37 static struct clk *usb_clk;
38 
39 /*
40   PMM_NPS_MODE -- PMM Non-power switching mode
41       Ports are powered continuously.
42 
43   PMM_GLOBAL_MODE -- PMM global switching mode
44       All ports are powered at the same time.
45 
46   PMM_PERPORT_MODE -- PMM per port switching mode
47       Ports are powered individually.
48  */
49 static int pxa27x_ohci_select_pmm( int mode )
50 {
51 	switch ( mode ) {
52 	case PMM_NPS_MODE:
53 		UHCRHDA |= RH_A_NPS;
54 		break;
55 	case PMM_GLOBAL_MODE:
56 		UHCRHDA &= ~(RH_A_NPS & RH_A_PSM);
57 		break;
58 	case PMM_PERPORT_MODE:
59 		UHCRHDA &= ~(RH_A_NPS);
60 		UHCRHDA |= RH_A_PSM;
61 
62 		/* Set port power control mask bits, only 3 ports. */
63 		UHCRHDB |= (0x7<<17);
64 		break;
65 	default:
66 		printk( KERN_ERR
67 			"Invalid mode %d, set to non-power switch mode.\n",
68 			mode );
69 
70 		UHCRHDA |= RH_A_NPS;
71 	}
72 
73 	return 0;
74 }
75 
76 extern int usb_disabled(void);
77 
78 /*-------------------------------------------------------------------------*/
79 
80 static int pxa27x_start_hc(struct device *dev)
81 {
82 	int retval = 0;
83 	struct pxaohci_platform_data *inf;
84 
85 	inf = dev->platform_data;
86 
87 	clk_enable(usb_clk);
88 
89 	UHCHR |= UHCHR_FHR;
90 	udelay(11);
91 	UHCHR &= ~UHCHR_FHR;
92 
93 	UHCHR |= UHCHR_FSBIR;
94 	while (UHCHR & UHCHR_FSBIR)
95 		cpu_relax();
96 
97 	if (inf->init)
98 		retval = inf->init(dev);
99 
100 	if (retval < 0)
101 		return retval;
102 
103 	UHCHR &= ~UHCHR_SSE;
104 
105 	UHCHIE = (UHCHIE_UPRIE | UHCHIE_RWIE);
106 
107 	/* Clear any OTG Pin Hold */
108 	if (cpu_is_pxa27x() && (PSSR & PSSR_OTGPH))
109 		PSSR |= PSSR_OTGPH;
110 
111 	return 0;
112 }
113 
114 static void pxa27x_stop_hc(struct device *dev)
115 {
116 	struct pxaohci_platform_data *inf;
117 
118 	inf = dev->platform_data;
119 
120 	if (inf->exit)
121 		inf->exit(dev);
122 
123 	UHCHR |= UHCHR_FHR;
124 	udelay(11);
125 	UHCHR &= ~UHCHR_FHR;
126 
127 	UHCCOMS |= 1;
128 	udelay(10);
129 
130 	clk_disable(usb_clk);
131 }
132 
133 
134 /*-------------------------------------------------------------------------*/
135 
136 /* configure so an HC device and id are always provided */
137 /* always called with process context; sleeping is OK */
138 
139 
140 /**
141  * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
142  * Context: !in_interrupt()
143  *
144  * Allocates basic resources for this USB host controller, and
145  * then invokes the start() method for the HCD associated with it
146  * through the hotplug entry's driver_data.
147  *
148  */
149 int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
150 {
151 	int retval;
152 	struct usb_hcd *hcd;
153 	struct pxaohci_platform_data *inf;
154 
155 	inf = pdev->dev.platform_data;
156 
157 	if (!inf)
158 		return -ENODEV;
159 
160 	if (pdev->resource[1].flags != IORESOURCE_IRQ) {
161 		pr_debug ("resource[1] is not IORESOURCE_IRQ");
162 		return -ENOMEM;
163 	}
164 
165 	usb_clk = clk_get(&pdev->dev, "USBCLK");
166 	if (IS_ERR(usb_clk))
167 		return PTR_ERR(usb_clk);
168 
169 	hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
170 	if (!hcd)
171 		return -ENOMEM;
172 	hcd->rsrc_start = pdev->resource[0].start;
173 	hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
174 
175 	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
176 		pr_debug("request_mem_region failed");
177 		retval = -EBUSY;
178 		goto err1;
179 	}
180 
181 	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
182 	if (!hcd->regs) {
183 		pr_debug("ioremap failed");
184 		retval = -ENOMEM;
185 		goto err2;
186 	}
187 
188 	if ((retval = pxa27x_start_hc(&pdev->dev)) < 0) {
189 		pr_debug("pxa27x_start_hc failed");
190 		goto err3;
191 	}
192 
193 	/* Select Power Management Mode */
194 	pxa27x_ohci_select_pmm(inf->port_mode);
195 
196 	if (inf->power_budget)
197 		hcd->power_budget = inf->power_budget;
198 
199 	ohci_hcd_init(hcd_to_ohci(hcd));
200 
201 	retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
202 	if (retval == 0)
203 		return retval;
204 
205 	pxa27x_stop_hc(&pdev->dev);
206  err3:
207 	iounmap(hcd->regs);
208  err2:
209 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
210  err1:
211 	usb_put_hcd(hcd);
212 	clk_put(usb_clk);
213 	return retval;
214 }
215 
216 
217 /* may be called without controller electrically present */
218 /* may be called with controller, bus, and devices active */
219 
220 /**
221  * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
222  * @dev: USB Host Controller being removed
223  * Context: !in_interrupt()
224  *
225  * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
226  * the HCD's stop() method.  It is always called from a thread
227  * context, normally "rmmod", "apmd", or something similar.
228  *
229  */
230 void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
231 {
232 	usb_remove_hcd(hcd);
233 	pxa27x_stop_hc(&pdev->dev);
234 	iounmap(hcd->regs);
235 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
236 	usb_put_hcd(hcd);
237 	clk_put(usb_clk);
238 }
239 
240 /*-------------------------------------------------------------------------*/
241 
242 static int __devinit
243 ohci_pxa27x_start (struct usb_hcd *hcd)
244 {
245 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
246 	int		ret;
247 
248 	ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
249 
250 	/* The value of NDP in roothub_a is incorrect on this hardware */
251 	ohci->num_ports = 3;
252 
253 	if ((ret = ohci_init(ohci)) < 0)
254 		return ret;
255 
256 	if ((ret = ohci_run (ohci)) < 0) {
257 		err ("can't start %s", hcd->self.bus_name);
258 		ohci_stop (hcd);
259 		return ret;
260 	}
261 
262 	return 0;
263 }
264 
265 /*-------------------------------------------------------------------------*/
266 
267 static const struct hc_driver ohci_pxa27x_hc_driver = {
268 	.description =		hcd_name,
269 	.product_desc =		"PXA27x OHCI",
270 	.hcd_priv_size =	sizeof(struct ohci_hcd),
271 
272 	/*
273 	 * generic hardware linkage
274 	 */
275 	.irq =			ohci_irq,
276 	.flags =		HCD_USB11 | HCD_MEMORY,
277 
278 	/*
279 	 * basic lifecycle operations
280 	 */
281 	.start =		ohci_pxa27x_start,
282 	.stop =			ohci_stop,
283 	.shutdown =		ohci_shutdown,
284 
285 	/*
286 	 * managing i/o requests and associated device resources
287 	 */
288 	.urb_enqueue =		ohci_urb_enqueue,
289 	.urb_dequeue =		ohci_urb_dequeue,
290 	.endpoint_disable =	ohci_endpoint_disable,
291 
292 	/*
293 	 * scheduling support
294 	 */
295 	.get_frame_number =	ohci_get_frame,
296 
297 	/*
298 	 * root hub support
299 	 */
300 	.hub_status_data =	ohci_hub_status_data,
301 	.hub_control =		ohci_hub_control,
302 	.hub_irq_enable =	ohci_rhsc_enable,
303 #ifdef  CONFIG_PM
304 	.bus_suspend =		ohci_bus_suspend,
305 	.bus_resume =		ohci_bus_resume,
306 #endif
307 	.start_port_reset =	ohci_start_port_reset,
308 };
309 
310 /*-------------------------------------------------------------------------*/
311 
312 static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
313 {
314 	pr_debug ("In ohci_hcd_pxa27x_drv_probe");
315 
316 	if (usb_disabled())
317 		return -ENODEV;
318 
319 	return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
320 }
321 
322 static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
323 {
324 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
325 
326 	usb_hcd_pxa27x_remove(hcd, pdev);
327 	platform_set_drvdata(pdev, NULL);
328 	return 0;
329 }
330 
331 #ifdef	CONFIG_PM
332 static int ohci_hcd_pxa27x_drv_suspend(struct platform_device *pdev, pm_message_t state)
333 {
334 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
335 	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
336 
337 	if (time_before(jiffies, ohci->next_statechange))
338 		msleep(5);
339 	ohci->next_statechange = jiffies;
340 
341 	pxa27x_stop_hc(&pdev->dev);
342 	hcd->state = HC_STATE_SUSPENDED;
343 
344 	return 0;
345 }
346 
347 static int ohci_hcd_pxa27x_drv_resume(struct platform_device *pdev)
348 {
349 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
350 	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
351 	int status;
352 
353 	if (time_before(jiffies, ohci->next_statechange))
354 		msleep(5);
355 	ohci->next_statechange = jiffies;
356 
357 	if ((status = pxa27x_start_hc(&pdev->dev)) < 0)
358 		return status;
359 
360 	ohci_finish_controller_resume(hcd);
361 	return 0;
362 }
363 #endif
364 
365 /* work with hotplug and coldplug */
366 MODULE_ALIAS("platform:pxa27x-ohci");
367 
368 static struct platform_driver ohci_hcd_pxa27x_driver = {
369 	.probe		= ohci_hcd_pxa27x_drv_probe,
370 	.remove		= ohci_hcd_pxa27x_drv_remove,
371 	.shutdown	= usb_hcd_platform_shutdown,
372 #ifdef CONFIG_PM
373 	.suspend	= ohci_hcd_pxa27x_drv_suspend,
374 	.resume		= ohci_hcd_pxa27x_drv_resume,
375 #endif
376 	.driver		= {
377 		.name	= "pxa27x-ohci",
378 		.owner	= THIS_MODULE,
379 	},
380 };
381 
382