blob: 74696ae29aa6981d26aaa79020765471b6bf8ee4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <linux/signal.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
eric miaoa8bcf412007-12-12 08:53:25 +080025#include <linux/clk.h>
Daniel Mack1139b452012-07-30 09:29:12 +020026#include <linux/of_platform.h>
27#include <linux/of_gpio.h>
Rob Herring60b9bd82012-03-01 21:13:13 -060028#include <mach/hardware.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020029#include <linux/platform_data/usb-ohci-pxa27x.h>
30#include <linux/platform_data/usb-pxa3xx-ulpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Eric Miao596050b2008-09-27 16:12:32 +080032/*
33 * UHC: USB Host Controller (OHCI-like) register definitions
34 */
Eric Miao0c392ed2008-10-04 12:57:21 +080035#define UHCREV (0x0000) /* UHC HCI Spec Revision */
36#define UHCHCON (0x0004) /* UHC Host Control Register */
37#define UHCCOMS (0x0008) /* UHC Command Status Register */
38#define UHCINTS (0x000C) /* UHC Interrupt Status Register */
39#define UHCINTE (0x0010) /* UHC Interrupt Enable */
40#define UHCINTD (0x0014) /* UHC Interrupt Disable */
41#define UHCHCCA (0x0018) /* UHC Host Controller Comm. Area */
42#define UHCPCED (0x001C) /* UHC Period Current Endpt Descr */
43#define UHCCHED (0x0020) /* UHC Control Head Endpt Descr */
44#define UHCCCED (0x0024) /* UHC Control Current Endpt Descr */
45#define UHCBHED (0x0028) /* UHC Bulk Head Endpt Descr */
46#define UHCBCED (0x002C) /* UHC Bulk Current Endpt Descr */
47#define UHCDHEAD (0x0030) /* UHC Done Head */
48#define UHCFMI (0x0034) /* UHC Frame Interval */
49#define UHCFMR (0x0038) /* UHC Frame Remaining */
50#define UHCFMN (0x003C) /* UHC Frame Number */
51#define UHCPERS (0x0040) /* UHC Periodic Start */
52#define UHCLS (0x0044) /* UHC Low Speed Threshold */
Eric Miao596050b2008-09-27 16:12:32 +080053
Eric Miao0c392ed2008-10-04 12:57:21 +080054#define UHCRHDA (0x0048) /* UHC Root Hub Descriptor A */
Eric Miao596050b2008-09-27 16:12:32 +080055#define UHCRHDA_NOCP (1 << 12) /* No over current protection */
56#define UHCRHDA_OCPM (1 << 11) /* Over Current Protection Mode */
57#define UHCRHDA_POTPGT(x) \
58 (((x) & 0xff) << 24) /* Power On To Power Good Time */
59
Eric Miao0c392ed2008-10-04 12:57:21 +080060#define UHCRHDB (0x004C) /* UHC Root Hub Descriptor B */
61#define UHCRHS (0x0050) /* UHC Root Hub Status */
62#define UHCRHPS1 (0x0054) /* UHC Root Hub Port 1 Status */
63#define UHCRHPS2 (0x0058) /* UHC Root Hub Port 2 Status */
64#define UHCRHPS3 (0x005C) /* UHC Root Hub Port 3 Status */
Eric Miao596050b2008-09-27 16:12:32 +080065
Eric Miao0c392ed2008-10-04 12:57:21 +080066#define UHCSTAT (0x0060) /* UHC Status Register */
Eric Miao596050b2008-09-27 16:12:32 +080067#define UHCSTAT_UPS3 (1 << 16) /* USB Power Sense Port3 */
68#define UHCSTAT_SBMAI (1 << 15) /* System Bus Master Abort Interrupt*/
69#define UHCSTAT_SBTAI (1 << 14) /* System Bus Target Abort Interrupt*/
70#define UHCSTAT_UPRI (1 << 13) /* USB Port Resume Interrupt */
71#define UHCSTAT_UPS2 (1 << 12) /* USB Power Sense Port 2 */
72#define UHCSTAT_UPS1 (1 << 11) /* USB Power Sense Port 1 */
73#define UHCSTAT_HTA (1 << 10) /* HCI Target Abort */
74#define UHCSTAT_HBA (1 << 8) /* HCI Buffer Active */
75#define UHCSTAT_RWUE (1 << 7) /* HCI Remote Wake Up Event */
76
Eric Miao0c392ed2008-10-04 12:57:21 +080077#define UHCHR (0x0064) /* UHC Reset Register */
Eric Miao596050b2008-09-27 16:12:32 +080078#define UHCHR_SSEP3 (1 << 11) /* Sleep Standby Enable for Port3 */
79#define UHCHR_SSEP2 (1 << 10) /* Sleep Standby Enable for Port2 */
80#define UHCHR_SSEP1 (1 << 9) /* Sleep Standby Enable for Port1 */
81#define UHCHR_PCPL (1 << 7) /* Power control polarity low */
82#define UHCHR_PSPL (1 << 6) /* Power sense polarity low */
83#define UHCHR_SSE (1 << 5) /* Sleep Standby Enable */
84#define UHCHR_UIT (1 << 4) /* USB Interrupt Test */
85#define UHCHR_SSDC (1 << 3) /* Simulation Scale Down Clock */
86#define UHCHR_CGR (1 << 2) /* Clock Generation Reset */
87#define UHCHR_FHR (1 << 1) /* Force Host Controller Reset */
88#define UHCHR_FSBIR (1 << 0) /* Force System Bus Iface Reset */
89
Eric Miao0c392ed2008-10-04 12:57:21 +080090#define UHCHIE (0x0068) /* UHC Interrupt Enable Register*/
Eric Miao596050b2008-09-27 16:12:32 +080091#define UHCHIE_UPS3IE (1 << 14) /* Power Sense Port3 IntEn */
92#define UHCHIE_UPRIE (1 << 13) /* Port Resume IntEn */
93#define UHCHIE_UPS2IE (1 << 12) /* Power Sense Port2 IntEn */
94#define UHCHIE_UPS1IE (1 << 11) /* Power Sense Port1 IntEn */
95#define UHCHIE_TAIE (1 << 10) /* HCI Interface Transfer Abort
96 Interrupt Enable*/
97#define UHCHIE_HBAIE (1 << 8) /* HCI Buffer Active IntEn */
98#define UHCHIE_RWIE (1 << 7) /* Remote Wake-up IntEn */
99
Eric Miao0c392ed2008-10-04 12:57:21 +0800100#define UHCHIT (0x006C) /* UHC Interrupt Test register */
Eric Miao596050b2008-09-27 16:12:32 +0800101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define PXA_UHC_MAX_PORTNUM 3
103
Eric Miao0c392ed2008-10-04 12:57:21 +0800104struct pxa27x_ohci {
105 /* must be 1st member here for hcd_to_ohci() to work */
106 struct ohci_hcd ohci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Miao0c392ed2008-10-04 12:57:21 +0800108 struct device *dev;
109 struct clk *clk;
110 void __iomem *mmio_base;
111};
112
113#define to_pxa27x_ohci(hcd) (struct pxa27x_ohci *)hcd_to_ohci(hcd)
eric miaoa8bcf412007-12-12 08:53:25 +0800114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 PMM_NPS_MODE -- PMM Non-power switching mode
117 Ports are powered continuously.
118
119 PMM_GLOBAL_MODE -- PMM global switching mode
120 All ports are powered at the same time.
121
122 PMM_PERPORT_MODE -- PMM per port switching mode
123 Ports are powered individually.
124 */
Eric Miao0c392ed2008-10-04 12:57:21 +0800125static int pxa27x_ohci_select_pmm(struct pxa27x_ohci *ohci, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Eric Miao0c392ed2008-10-04 12:57:21 +0800127 uint32_t uhcrhda = __raw_readl(ohci->mmio_base + UHCRHDA);
128 uint32_t uhcrhdb = __raw_readl(ohci->mmio_base + UHCRHDB);
129
130 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 case PMM_NPS_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800132 uhcrhda |= RH_A_NPS;
David Brownelldd9048a2006-12-05 03:18:31 -0800133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 case PMM_GLOBAL_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800135 uhcrhda &= ~(RH_A_NPS & RH_A_PSM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
137 case PMM_PERPORT_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800138 uhcrhda &= ~(RH_A_NPS);
139 uhcrhda |= RH_A_PSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Set port power control mask bits, only 3 ports. */
Eric Miao0c392ed2008-10-04 12:57:21 +0800142 uhcrhdb |= (0x7<<17);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 default:
145 printk( KERN_ERR
David Brownelldd9048a2006-12-05 03:18:31 -0800146 "Invalid mode %d, set to non-power switch mode.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mode );
148
Eric Miao0c392ed2008-10-04 12:57:21 +0800149 uhcrhda |= RH_A_NPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Eric Miao0c392ed2008-10-04 12:57:21 +0800152 __raw_writel(uhcrhda, ohci->mmio_base + UHCRHDA);
153 __raw_writel(uhcrhdb, ohci->mmio_base + UHCRHDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157extern int usb_disabled(void);
158
159/*-------------------------------------------------------------------------*/
160
Eric Miao0c392ed2008-10-04 12:57:21 +0800161static inline void pxa27x_setup_hc(struct pxa27x_ohci *ohci,
162 struct pxaohci_platform_data *inf)
Eric Miao097b5332008-09-27 15:49:57 +0800163{
Eric Miao0c392ed2008-10-04 12:57:21 +0800164 uint32_t uhchr = __raw_readl(ohci->mmio_base + UHCHR);
165 uint32_t uhcrhda = __raw_readl(ohci->mmio_base + UHCRHDA);
Eric Miao097b5332008-09-27 15:49:57 +0800166
167 if (inf->flags & ENABLE_PORT1)
168 uhchr &= ~UHCHR_SSEP1;
169
170 if (inf->flags & ENABLE_PORT2)
171 uhchr &= ~UHCHR_SSEP2;
172
173 if (inf->flags & ENABLE_PORT3)
174 uhchr &= ~UHCHR_SSEP3;
175
176 if (inf->flags & POWER_CONTROL_LOW)
177 uhchr |= UHCHR_PCPL;
178
179 if (inf->flags & POWER_SENSE_LOW)
180 uhchr |= UHCHR_PSPL;
181
182 if (inf->flags & NO_OC_PROTECTION)
183 uhcrhda |= UHCRHDA_NOCP;
Aric Blumer7b4361f2009-07-30 13:26:58 -0400184 else
185 uhcrhda &= ~UHCRHDA_NOCP;
Eric Miao097b5332008-09-27 15:49:57 +0800186
187 if (inf->flags & OC_MODE_PERPORT)
188 uhcrhda |= UHCRHDA_OCPM;
Aric Blumer7b4361f2009-07-30 13:26:58 -0400189 else
190 uhcrhda &= ~UHCRHDA_OCPM;
Eric Miao097b5332008-09-27 15:49:57 +0800191
192 if (inf->power_on_delay) {
193 uhcrhda &= ~UHCRHDA_POTPGT(0xff);
194 uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
195 }
196
Eric Miao0c392ed2008-10-04 12:57:21 +0800197 __raw_writel(uhchr, ohci->mmio_base + UHCHR);
198 __raw_writel(uhcrhda, ohci->mmio_base + UHCRHDA);
199}
200
201static inline void pxa27x_reset_hc(struct pxa27x_ohci *ohci)
202{
203 uint32_t uhchr = __raw_readl(ohci->mmio_base + UHCHR);
204
205 __raw_writel(uhchr | UHCHR_FHR, ohci->mmio_base + UHCHR);
206 udelay(11);
207 __raw_writel(uhchr & ~UHCHR_FHR, ohci->mmio_base + UHCHR);
Eric Miao097b5332008-09-27 15:49:57 +0800208}
209
Eric Miao59376cc2010-07-14 21:17:25 +0800210#ifdef CONFIG_PXA27x
Eric Miao0cb0b0d2008-10-04 12:45:39 +0800211extern void pxa27x_clear_otgph(void);
212#else
213#define pxa27x_clear_otgph() do {} while (0)
214#endif
215
Eric Miao0c392ed2008-10-04 12:57:21 +0800216static int pxa27x_start_hc(struct pxa27x_ohci *ohci, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Richard Purdie81f280e2005-11-12 14:22:11 +0000218 int retval = 0;
219 struct pxaohci_platform_data *inf;
Eric Miao0c392ed2008-10-04 12:57:21 +0800220 uint32_t uhchr;
Richard Purdie81f280e2005-11-12 14:22:11 +0000221
Jingoo Hand4f09e22013-07-30 19:59:40 +0900222 inf = dev_get_platdata(dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000223
Philipp Zabel4bf34b82012-03-15 19:20:24 +0100224 clk_prepare_enable(ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Eric Miao0c392ed2008-10-04 12:57:21 +0800226 pxa27x_reset_hc(ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Miao0c392ed2008-10-04 12:57:21 +0800228 uhchr = __raw_readl(ohci->mmio_base + UHCHR) | UHCHR_FSBIR;
229 __raw_writel(uhchr, ohci->mmio_base + UHCHR);
230
231 while (__raw_readl(ohci->mmio_base + UHCHR) & UHCHR_FSBIR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cpu_relax();
233
Eric Miao0c392ed2008-10-04 12:57:21 +0800234 pxa27x_setup_hc(ohci, inf);
Eric Miao097b5332008-09-27 15:49:57 +0800235
Richard Purdie81f280e2005-11-12 14:22:11 +0000236 if (inf->init)
237 retval = inf->init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Richard Purdie81f280e2005-11-12 14:22:11 +0000239 if (retval < 0)
240 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300242 if (cpu_is_pxa3xx())
243 pxa3xx_u2d_start_hc(&ohci_to_hcd(&ohci->ohci)->self);
244
Eric Miao0c392ed2008-10-04 12:57:21 +0800245 uhchr = __raw_readl(ohci->mmio_base + UHCHR) & ~UHCHR_SSE;
246 __raw_writel(uhchr, ohci->mmio_base + UHCHR);
247 __raw_writel(UHCHIE_UPRIE | UHCHIE_RWIE, ohci->mmio_base + UHCHIE);
David Brownell155faf52005-08-31 11:54:09 -0700248
249 /* Clear any OTG Pin Hold */
Eric Miao0cb0b0d2008-10-04 12:45:39 +0800250 pxa27x_clear_otgph();
Richard Purdie81f280e2005-11-12 14:22:11 +0000251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Eric Miao0c392ed2008-10-04 12:57:21 +0800254static void pxa27x_stop_hc(struct pxa27x_ohci *ohci, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Richard Purdie81f280e2005-11-12 14:22:11 +0000256 struct pxaohci_platform_data *inf;
Eric Miao0c392ed2008-10-04 12:57:21 +0800257 uint32_t uhccoms;
Richard Purdie81f280e2005-11-12 14:22:11 +0000258
Jingoo Hand4f09e22013-07-30 19:59:40 +0900259 inf = dev_get_platdata(dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000260
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300261 if (cpu_is_pxa3xx())
262 pxa3xx_u2d_stop_hc(&ohci_to_hcd(&ohci->ohci)->self);
263
Richard Purdie81f280e2005-11-12 14:22:11 +0000264 if (inf->exit)
265 inf->exit(dev);
266
Eric Miao0c392ed2008-10-04 12:57:21 +0800267 pxa27x_reset_hc(ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Eric Miao0c392ed2008-10-04 12:57:21 +0800269 /* Host Controller Reset */
270 uhccoms = __raw_readl(ohci->mmio_base + UHCCOMS) | 0x01;
271 __raw_writel(uhccoms, ohci->mmio_base + UHCCOMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 udelay(10);
273
Philipp Zabel4bf34b82012-03-15 19:20:24 +0100274 clk_disable_unprepare(ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Daniel Mack1139b452012-07-30 09:29:12 +0200277#ifdef CONFIG_OF
278static const struct of_device_id pxa_ohci_dt_ids[] = {
279 { .compatible = "marvell,pxa-ohci" },
280 { }
281};
282
283MODULE_DEVICE_TABLE(of, pxa_ohci_dt_ids);
284
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500285static int ohci_pxa_of_init(struct platform_device *pdev)
Daniel Mack1139b452012-07-30 09:29:12 +0200286{
287 struct device_node *np = pdev->dev.of_node;
288 struct pxaohci_platform_data *pdata;
289 u32 tmp;
Russell King22d9d8e2013-06-10 16:28:49 +0100290 int ret;
Daniel Mack1139b452012-07-30 09:29:12 +0200291
292 if (!np)
293 return 0;
294
295 /* Right now device-tree probed devices don't get dma_mask set.
296 * Since shared usb code relies on it, set it here for now.
297 * Once we have dma capability bindings this can go away.
298 */
299 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -0600300 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
Russell King22d9d8e2013-06-10 16:28:49 +0100301 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
302 if (ret)
303 return ret;
Daniel Mack1139b452012-07-30 09:29:12 +0200304
305 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
306 if (!pdata)
307 return -ENOMEM;
308
309 if (of_get_property(np, "marvell,enable-port1", NULL))
310 pdata->flags |= ENABLE_PORT1;
311 if (of_get_property(np, "marvell,enable-port2", NULL))
312 pdata->flags |= ENABLE_PORT2;
313 if (of_get_property(np, "marvell,enable-port3", NULL))
314 pdata->flags |= ENABLE_PORT3;
315 if (of_get_property(np, "marvell,port-sense-low", NULL))
316 pdata->flags |= POWER_SENSE_LOW;
317 if (of_get_property(np, "marvell,power-control-low", NULL))
318 pdata->flags |= POWER_CONTROL_LOW;
319 if (of_get_property(np, "marvell,no-oc-protection", NULL))
320 pdata->flags |= NO_OC_PROTECTION;
321 if (of_get_property(np, "marvell,oc-mode-perport", NULL))
322 pdata->flags |= OC_MODE_PERPORT;
323 if (!of_property_read_u32(np, "marvell,power-on-delay", &tmp))
324 pdata->power_on_delay = tmp;
325 if (!of_property_read_u32(np, "marvell,port-mode", &tmp))
326 pdata->port_mode = tmp;
327 if (!of_property_read_u32(np, "marvell,power-budget", &tmp))
328 pdata->power_budget = tmp;
329
330 pdev->dev.platform_data = pdata;
331
332 return 0;
333}
334#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500335static int ohci_pxa_of_init(struct platform_device *pdev)
Daniel Mack1139b452012-07-30 09:29:12 +0200336{
337 return 0;
338}
339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/*-------------------------------------------------------------------------*/
342
343/* configure so an HC device and id are always provided */
344/* always called with process context; sleeping is OK */
345
346
347/**
348 * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
349 * Context: !in_interrupt()
350 *
351 * Allocates basic resources for this USB host controller, and
352 * then invokes the start() method for the HCD associated with it
353 * through the hotplug entry's driver_data.
354 *
355 */
Richard Purdie81f280e2005-11-12 14:22:11 +0000356int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Eric Miao84bab732008-10-03 22:37:21 +0800358 int retval, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct usb_hcd *hcd;
Richard Purdie81f280e2005-11-12 14:22:11 +0000360 struct pxaohci_platform_data *inf;
Eric Miao0c392ed2008-10-04 12:57:21 +0800361 struct pxa27x_ohci *ohci;
Eric Miao84bab732008-10-03 22:37:21 +0800362 struct resource *r;
Eric Miao0c392ed2008-10-04 12:57:21 +0800363 struct clk *usb_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Daniel Mack1139b452012-07-30 09:29:12 +0200365 retval = ohci_pxa_of_init(pdev);
366 if (retval)
367 return retval;
368
Jingoo Hand4f09e22013-07-30 19:59:40 +0900369 inf = dev_get_platdata(&pdev->dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000370
371 if (!inf)
372 return -ENODEV;
373
Eric Miao84bab732008-10-03 22:37:21 +0800374 irq = platform_get_irq(pdev, 0);
375 if (irq < 0) {
376 pr_err("no resource of IORESOURCE_IRQ");
377 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Russell Kinge0d8b132008-11-11 17:52:32 +0000380 usb_clk = clk_get(&pdev->dev, NULL);
eric miaoa8bcf412007-12-12 08:53:25 +0800381 if (IS_ERR(usb_clk))
382 return PTR_ERR(usb_clk);
383
Richard Purdie81f280e2005-11-12 14:22:11 +0000384 hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
Julia Lawall7febe2b2011-06-01 19:10:06 +0200385 if (!hcd) {
386 retval = -ENOMEM;
387 goto err0;
388 }
Eric Miao84bab732008-10-03 22:37:21 +0800389
390 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
391 if (!r) {
392 pr_err("no resource of IORESOURCE_MEM");
393 retval = -ENXIO;
394 goto err1;
395 }
396
397 hcd->rsrc_start = r->start;
398 hcd->rsrc_len = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
401 pr_debug("request_mem_region failed");
402 retval = -EBUSY;
403 goto err1;
404 }
405
406 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
407 if (!hcd->regs) {
408 pr_debug("ioremap failed");
409 retval = -ENOMEM;
410 goto err2;
411 }
412
Eric Miao0c392ed2008-10-04 12:57:21 +0800413 /* initialize "struct pxa27x_ohci" */
414 ohci = (struct pxa27x_ohci *)hcd_to_ohci(hcd);
415 ohci->dev = &pdev->dev;
416 ohci->clk = usb_clk;
417 ohci->mmio_base = (void __iomem *)hcd->regs;
418
419 if ((retval = pxa27x_start_hc(ohci, &pdev->dev)) < 0) {
Richard Purdie81f280e2005-11-12 14:22:11 +0000420 pr_debug("pxa27x_start_hc failed");
421 goto err3;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* Select Power Management Mode */
Eric Miao0c392ed2008-10-04 12:57:21 +0800425 pxa27x_ohci_select_pmm(ohci, inf->port_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Richard Purdie0c27c5d2006-06-08 22:44:07 +0100427 if (inf->power_budget)
428 hcd->power_budget = inf->power_budget;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ohci_hcd_init(hcd_to_ohci(hcd));
431
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800432 retval = usb_add_hcd(hcd, irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (retval == 0)
434 return retval;
435
Eric Miao0c392ed2008-10-04 12:57:21 +0800436 pxa27x_stop_hc(ohci, &pdev->dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000437 err3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 iounmap(hcd->regs);
439 err2:
440 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
441 err1:
442 usb_put_hcd(hcd);
Julia Lawall7febe2b2011-06-01 19:10:06 +0200443 err0:
eric miaoa8bcf412007-12-12 08:53:25 +0800444 clk_put(usb_clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return retval;
446}
447
448
449/* may be called without controller electrically present */
450/* may be called with controller, bus, and devices active */
451
452/**
453 * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
454 * @dev: USB Host Controller being removed
455 * Context: !in_interrupt()
456 *
457 * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
458 * the HCD's stop() method. It is always called from a thread
459 * context, normally "rmmod", "apmd", or something similar.
460 *
461 */
Richard Purdie81f280e2005-11-12 14:22:11 +0000462void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Eric Miao0c392ed2008-10-04 12:57:21 +0800464 struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 usb_remove_hcd(hcd);
Eric Miao0c392ed2008-10-04 12:57:21 +0800467 pxa27x_stop_hc(ohci, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 iounmap(hcd->regs);
469 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
470 usb_put_hcd(hcd);
Eric Miao0c392ed2008-10-04 12:57:21 +0800471 clk_put(ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/*-------------------------------------------------------------------------*/
475
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500476static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477ohci_pxa27x_start (struct usb_hcd *hcd)
478{
479 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
480 int ret;
481
482 ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
483
David Brownellfdd13b32005-08-31 11:52:57 -0700484 /* The value of NDP in roothub_a is incorrect on this hardware */
485 ohci->num_ports = 3;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if ((ret = ohci_init(ohci)) < 0)
488 return ret;
489
490 if ((ret = ohci_run (ohci)) < 0) {
Paul Gortmakerade749a2012-05-01 17:36:54 -0400491 dev_err(hcd->self.controller, "can't start %s",
492 hcd->self.bus_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ohci_stop (hcd);
494 return ret;
495 }
496
497 return 0;
498}
499
500/*-------------------------------------------------------------------------*/
501
502static const struct hc_driver ohci_pxa27x_hc_driver = {
503 .description = hcd_name,
504 .product_desc = "PXA27x OHCI",
Eric Miao0c392ed2008-10-04 12:57:21 +0800505 .hcd_priv_size = sizeof(struct pxa27x_ohci),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /*
508 * generic hardware linkage
509 */
510 .irq = ohci_irq,
511 .flags = HCD_USB11 | HCD_MEMORY,
512
513 /*
514 * basic lifecycle operations
515 */
516 .start = ohci_pxa27x_start,
517 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800518 .shutdown = ohci_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * managing i/o requests and associated device resources
522 */
523 .urb_enqueue = ohci_urb_enqueue,
524 .urb_dequeue = ohci_urb_dequeue,
525 .endpoint_disable = ohci_endpoint_disable,
526
527 /*
528 * scheduling support
529 */
530 .get_frame_number = ohci_get_frame,
531
532 /*
533 * root hub support
534 */
535 .hub_status_data = ohci_hub_status_data,
536 .hub_control = ohci_hub_control,
David Brownell8ad7fe12005-09-13 19:59:11 -0700537#ifdef CONFIG_PM
Alan Stern0c0382e2005-10-13 17:08:02 -0400538 .bus_suspend = ohci_bus_suspend,
539 .bus_resume = ohci_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
David Brownell92936772005-09-22 22:32:11 -0700541 .start_port_reset = ohci_start_port_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
544/*-------------------------------------------------------------------------*/
545
Russell King3ae5eae2005-11-09 22:32:44 +0000546static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 pr_debug ("In ohci_hcd_pxa27x_drv_probe");
549
550 if (usb_disabled())
551 return -ENODEV;
552
Richard Purdie81f280e2005-11-12 14:22:11 +0000553 return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Russell King3ae5eae2005-11-09 22:32:44 +0000556static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Russell King3ae5eae2005-11-09 22:32:44 +0000558 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 usb_hcd_pxa27x_remove(hcd, pdev);
561 return 0;
562}
563
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300564#ifdef CONFIG_PM
565static int ohci_hcd_pxa27x_drv_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300567 struct usb_hcd *hcd = dev_get_drvdata(dev);
Eric Miao0c392ed2008-10-04 12:57:21 +0800568 struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000569
Eric Miao0c392ed2008-10-04 12:57:21 +0800570 if (time_before(jiffies, ohci->ohci.next_statechange))
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000571 msleep(5);
Eric Miao0c392ed2008-10-04 12:57:21 +0800572 ohci->ohci.next_statechange = jiffies;
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000573
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300574 pxa27x_stop_hc(ohci, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576}
577
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300578static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300580 struct usb_hcd *hcd = dev_get_drvdata(dev);
Eric Miao0c392ed2008-10-04 12:57:21 +0800581 struct pxa27x_ohci *ohci = to_pxa27x_ohci(hcd);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900582 struct pxaohci_platform_data *inf = dev_get_platdata(dev);
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000583 int status;
584
Eric Miao0c392ed2008-10-04 12:57:21 +0800585 if (time_before(jiffies, ohci->ohci.next_statechange))
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000586 msleep(5);
Eric Miao0c392ed2008-10-04 12:57:21 +0800587 ohci->ohci.next_statechange = jiffies;
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000588
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300589 if ((status = pxa27x_start_hc(ohci, dev)) < 0)
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000590 return status;
591
Aric D. Blumera75d0482009-07-30 13:42:30 -0400592 /* Select Power Management Mode */
593 pxa27x_ohci_select_pmm(ohci, inf->port_mode);
594
Florian Fainellicfa49b42012-10-08 15:11:29 +0200595 ohci_resume(hcd, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597}
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300598
Alexey Dobriyan47145212009-12-14 18:00:08 -0800599static const struct dev_pm_ops ohci_hcd_pxa27x_pm_ops = {
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300600 .suspend = ohci_hcd_pxa27x_drv_suspend,
601 .resume = ohci_hcd_pxa27x_drv_resume,
602};
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Kay Sieversf4fce612008-04-10 21:29:22 -0700605/* work with hotplug and coldplug */
606MODULE_ALIAS("platform:pxa27x-ohci");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Russell King3ae5eae2005-11-09 22:32:44 +0000608static struct platform_driver ohci_hcd_pxa27x_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .probe = ohci_hcd_pxa27x_drv_probe,
610 .remove = ohci_hcd_pxa27x_drv_remove,
David Brownelldd9048a2006-12-05 03:18:31 -0800611 .shutdown = usb_hcd_platform_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000612 .driver = {
613 .name = "pxa27x-ohci",
Kay Sieversf4fce612008-04-10 21:29:22 -0700614 .owner = THIS_MODULE,
Daniel Mack1139b452012-07-30 09:29:12 +0200615 .of_match_table = of_match_ptr(pxa_ohci_dt_ids),
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300616#ifdef CONFIG_PM
617 .pm = &ohci_hcd_pxa27x_pm_ops,
618#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000619 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620};
621