blob: deea5d1d6394f3015de6a8a728061a0d3cfedc5a [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
eric miaoa8bcf412007-12-12 08:53:25 +080022#include <linux/clk.h>
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +053023#include <linux/device.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
Daniel Mack1139b452012-07-30 09:29:12 +020027#include <linux/of_platform.h>
28#include <linux/of_gpio.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>
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +053031#include <linux/platform_device.h>
32#include <linux/signal.h>
33#include <linux/usb.h>
34#include <linux/usb/hcd.h>
35#include <linux/usb/otg.h>
36
37#include <mach/hardware.h>
38
39#include "ohci.h"
40
41#define DRIVER_DESC "OHCI PXA27x/PXA3x driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Eric Miao596050b2008-09-27 16:12:32 +080043/*
44 * UHC: USB Host Controller (OHCI-like) register definitions
45 */
Eric Miao0c392ed2008-10-04 12:57:21 +080046#define UHCREV (0x0000) /* UHC HCI Spec Revision */
47#define UHCHCON (0x0004) /* UHC Host Control Register */
48#define UHCCOMS (0x0008) /* UHC Command Status Register */
49#define UHCINTS (0x000C) /* UHC Interrupt Status Register */
50#define UHCINTE (0x0010) /* UHC Interrupt Enable */
51#define UHCINTD (0x0014) /* UHC Interrupt Disable */
52#define UHCHCCA (0x0018) /* UHC Host Controller Comm. Area */
53#define UHCPCED (0x001C) /* UHC Period Current Endpt Descr */
54#define UHCCHED (0x0020) /* UHC Control Head Endpt Descr */
55#define UHCCCED (0x0024) /* UHC Control Current Endpt Descr */
56#define UHCBHED (0x0028) /* UHC Bulk Head Endpt Descr */
57#define UHCBCED (0x002C) /* UHC Bulk Current Endpt Descr */
58#define UHCDHEAD (0x0030) /* UHC Done Head */
59#define UHCFMI (0x0034) /* UHC Frame Interval */
60#define UHCFMR (0x0038) /* UHC Frame Remaining */
61#define UHCFMN (0x003C) /* UHC Frame Number */
62#define UHCPERS (0x0040) /* UHC Periodic Start */
63#define UHCLS (0x0044) /* UHC Low Speed Threshold */
Eric Miao596050b2008-09-27 16:12:32 +080064
Eric Miao0c392ed2008-10-04 12:57:21 +080065#define UHCRHDA (0x0048) /* UHC Root Hub Descriptor A */
Eric Miao596050b2008-09-27 16:12:32 +080066#define UHCRHDA_NOCP (1 << 12) /* No over current protection */
67#define UHCRHDA_OCPM (1 << 11) /* Over Current Protection Mode */
68#define UHCRHDA_POTPGT(x) \
69 (((x) & 0xff) << 24) /* Power On To Power Good Time */
70
Eric Miao0c392ed2008-10-04 12:57:21 +080071#define UHCRHDB (0x004C) /* UHC Root Hub Descriptor B */
72#define UHCRHS (0x0050) /* UHC Root Hub Status */
73#define UHCRHPS1 (0x0054) /* UHC Root Hub Port 1 Status */
74#define UHCRHPS2 (0x0058) /* UHC Root Hub Port 2 Status */
75#define UHCRHPS3 (0x005C) /* UHC Root Hub Port 3 Status */
Eric Miao596050b2008-09-27 16:12:32 +080076
Eric Miao0c392ed2008-10-04 12:57:21 +080077#define UHCSTAT (0x0060) /* UHC Status Register */
Eric Miao596050b2008-09-27 16:12:32 +080078#define UHCSTAT_UPS3 (1 << 16) /* USB Power Sense Port3 */
79#define UHCSTAT_SBMAI (1 << 15) /* System Bus Master Abort Interrupt*/
80#define UHCSTAT_SBTAI (1 << 14) /* System Bus Target Abort Interrupt*/
81#define UHCSTAT_UPRI (1 << 13) /* USB Port Resume Interrupt */
82#define UHCSTAT_UPS2 (1 << 12) /* USB Power Sense Port 2 */
83#define UHCSTAT_UPS1 (1 << 11) /* USB Power Sense Port 1 */
84#define UHCSTAT_HTA (1 << 10) /* HCI Target Abort */
85#define UHCSTAT_HBA (1 << 8) /* HCI Buffer Active */
86#define UHCSTAT_RWUE (1 << 7) /* HCI Remote Wake Up Event */
87
Eric Miao0c392ed2008-10-04 12:57:21 +080088#define UHCHR (0x0064) /* UHC Reset Register */
Eric Miao596050b2008-09-27 16:12:32 +080089#define UHCHR_SSEP3 (1 << 11) /* Sleep Standby Enable for Port3 */
90#define UHCHR_SSEP2 (1 << 10) /* Sleep Standby Enable for Port2 */
91#define UHCHR_SSEP1 (1 << 9) /* Sleep Standby Enable for Port1 */
92#define UHCHR_PCPL (1 << 7) /* Power control polarity low */
93#define UHCHR_PSPL (1 << 6) /* Power sense polarity low */
94#define UHCHR_SSE (1 << 5) /* Sleep Standby Enable */
95#define UHCHR_UIT (1 << 4) /* USB Interrupt Test */
96#define UHCHR_SSDC (1 << 3) /* Simulation Scale Down Clock */
97#define UHCHR_CGR (1 << 2) /* Clock Generation Reset */
98#define UHCHR_FHR (1 << 1) /* Force Host Controller Reset */
99#define UHCHR_FSBIR (1 << 0) /* Force System Bus Iface Reset */
100
Eric Miao0c392ed2008-10-04 12:57:21 +0800101#define UHCHIE (0x0068) /* UHC Interrupt Enable Register*/
Eric Miao596050b2008-09-27 16:12:32 +0800102#define UHCHIE_UPS3IE (1 << 14) /* Power Sense Port3 IntEn */
103#define UHCHIE_UPRIE (1 << 13) /* Port Resume IntEn */
104#define UHCHIE_UPS2IE (1 << 12) /* Power Sense Port2 IntEn */
105#define UHCHIE_UPS1IE (1 << 11) /* Power Sense Port1 IntEn */
106#define UHCHIE_TAIE (1 << 10) /* HCI Interface Transfer Abort
107 Interrupt Enable*/
108#define UHCHIE_HBAIE (1 << 8) /* HCI Buffer Active IntEn */
109#define UHCHIE_RWIE (1 << 7) /* Remote Wake-up IntEn */
110
Eric Miao0c392ed2008-10-04 12:57:21 +0800111#define UHCHIT (0x006C) /* UHC Interrupt Test register */
Eric Miao596050b2008-09-27 16:12:32 +0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define PXA_UHC_MAX_PORTNUM 3
114
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530115static const char hcd_name[] = "ohci-pxa27x";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530117static struct hc_driver __read_mostly ohci_pxa27x_hc_driver;
118
119struct pxa27x_ohci {
Eric Miao0c392ed2008-10-04 12:57:21 +0800120 struct clk *clk;
121 void __iomem *mmio_base;
122};
123
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530124#define to_pxa27x_ohci(hcd) (struct pxa27x_ohci *)(hcd_to_ohci(hcd)->priv)
eric miaoa8bcf412007-12-12 08:53:25 +0800125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 PMM_NPS_MODE -- PMM Non-power switching mode
128 Ports are powered continuously.
129
130 PMM_GLOBAL_MODE -- PMM global switching mode
131 All ports are powered at the same time.
132
133 PMM_PERPORT_MODE -- PMM per port switching mode
134 Ports are powered individually.
135 */
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530136static int pxa27x_ohci_select_pmm(struct pxa27x_ohci *pxa_ohci, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530138 uint32_t uhcrhda = __raw_readl(pxa_ohci->mmio_base + UHCRHDA);
139 uint32_t uhcrhdb = __raw_readl(pxa_ohci->mmio_base + UHCRHDB);
Eric Miao0c392ed2008-10-04 12:57:21 +0800140
141 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 case PMM_NPS_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800143 uhcrhda |= RH_A_NPS;
David Brownelldd9048a2006-12-05 03:18:31 -0800144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case PMM_GLOBAL_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800146 uhcrhda &= ~(RH_A_NPS & RH_A_PSM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 case PMM_PERPORT_MODE:
Eric Miao0c392ed2008-10-04 12:57:21 +0800149 uhcrhda &= ~(RH_A_NPS);
150 uhcrhda |= RH_A_PSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* Set port power control mask bits, only 3 ports. */
Eric Miao0c392ed2008-10-04 12:57:21 +0800153 uhcrhdb |= (0x7<<17);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155 default:
156 printk( KERN_ERR
David Brownelldd9048a2006-12-05 03:18:31 -0800157 "Invalid mode %d, set to non-power switch mode.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 mode );
159
Eric Miao0c392ed2008-10-04 12:57:21 +0800160 uhcrhda |= RH_A_NPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530163 __raw_writel(uhcrhda, pxa_ohci->mmio_base + UHCRHDA);
164 __raw_writel(uhcrhdb, pxa_ohci->mmio_base + UHCRHDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*-------------------------------------------------------------------------*/
169
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530170static inline void pxa27x_setup_hc(struct pxa27x_ohci *pxa_ohci,
Eric Miao0c392ed2008-10-04 12:57:21 +0800171 struct pxaohci_platform_data *inf)
Eric Miao097b5332008-09-27 15:49:57 +0800172{
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530173 uint32_t uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR);
174 uint32_t uhcrhda = __raw_readl(pxa_ohci->mmio_base + UHCRHDA);
Eric Miao097b5332008-09-27 15:49:57 +0800175
176 if (inf->flags & ENABLE_PORT1)
177 uhchr &= ~UHCHR_SSEP1;
178
179 if (inf->flags & ENABLE_PORT2)
180 uhchr &= ~UHCHR_SSEP2;
181
182 if (inf->flags & ENABLE_PORT3)
183 uhchr &= ~UHCHR_SSEP3;
184
185 if (inf->flags & POWER_CONTROL_LOW)
186 uhchr |= UHCHR_PCPL;
187
188 if (inf->flags & POWER_SENSE_LOW)
189 uhchr |= UHCHR_PSPL;
190
191 if (inf->flags & NO_OC_PROTECTION)
192 uhcrhda |= UHCRHDA_NOCP;
Aric Blumer7b4361f2009-07-30 13:26:58 -0400193 else
194 uhcrhda &= ~UHCRHDA_NOCP;
Eric Miao097b5332008-09-27 15:49:57 +0800195
196 if (inf->flags & OC_MODE_PERPORT)
197 uhcrhda |= UHCRHDA_OCPM;
Aric Blumer7b4361f2009-07-30 13:26:58 -0400198 else
199 uhcrhda &= ~UHCRHDA_OCPM;
Eric Miao097b5332008-09-27 15:49:57 +0800200
201 if (inf->power_on_delay) {
202 uhcrhda &= ~UHCRHDA_POTPGT(0xff);
203 uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
204 }
205
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530206 __raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
207 __raw_writel(uhcrhda, pxa_ohci->mmio_base + UHCRHDA);
Eric Miao0c392ed2008-10-04 12:57:21 +0800208}
209
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530210static inline void pxa27x_reset_hc(struct pxa27x_ohci *pxa_ohci)
Eric Miao0c392ed2008-10-04 12:57:21 +0800211{
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530212 uint32_t uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR);
Eric Miao0c392ed2008-10-04 12:57:21 +0800213
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530214 __raw_writel(uhchr | UHCHR_FHR, pxa_ohci->mmio_base + UHCHR);
Eric Miao0c392ed2008-10-04 12:57:21 +0800215 udelay(11);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530216 __raw_writel(uhchr & ~UHCHR_FHR, pxa_ohci->mmio_base + UHCHR);
Eric Miao097b5332008-09-27 15:49:57 +0800217}
218
Eric Miao59376cc2010-07-14 21:17:25 +0800219#ifdef CONFIG_PXA27x
Eric Miao0cb0b0d2008-10-04 12:45:39 +0800220extern void pxa27x_clear_otgph(void);
221#else
222#define pxa27x_clear_otgph() do {} while (0)
223#endif
224
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530225static int pxa27x_start_hc(struct pxa27x_ohci *pxa_ohci, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Richard Purdie81f280e2005-11-12 14:22:11 +0000227 int retval = 0;
228 struct pxaohci_platform_data *inf;
Eric Miao0c392ed2008-10-04 12:57:21 +0800229 uint32_t uhchr;
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530230 struct usb_hcd *hcd = dev_get_drvdata(dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000231
Jingoo Hand4f09e22013-07-30 19:59:40 +0900232 inf = dev_get_platdata(dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000233
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530234 clk_prepare_enable(pxa_ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530236 pxa27x_reset_hc(pxa_ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530238 uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR) | UHCHR_FSBIR;
239 __raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
Eric Miao0c392ed2008-10-04 12:57:21 +0800240
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530241 while (__raw_readl(pxa_ohci->mmio_base + UHCHR) & UHCHR_FSBIR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 cpu_relax();
243
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530244 pxa27x_setup_hc(pxa_ohci, inf);
Eric Miao097b5332008-09-27 15:49:57 +0800245
Richard Purdie81f280e2005-11-12 14:22:11 +0000246 if (inf->init)
247 retval = inf->init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Richard Purdie81f280e2005-11-12 14:22:11 +0000249 if (retval < 0)
250 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300252 if (cpu_is_pxa3xx())
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530253 pxa3xx_u2d_start_hc(&hcd->self);
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300254
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530255 uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR) & ~UHCHR_SSE;
256 __raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
257 __raw_writel(UHCHIE_UPRIE | UHCHIE_RWIE, pxa_ohci->mmio_base + UHCHIE);
David Brownell155faf52005-08-31 11:54:09 -0700258
259 /* Clear any OTG Pin Hold */
Eric Miao0cb0b0d2008-10-04 12:45:39 +0800260 pxa27x_clear_otgph();
Richard Purdie81f280e2005-11-12 14:22:11 +0000261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530264static void pxa27x_stop_hc(struct pxa27x_ohci *pxa_ohci, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Richard Purdie81f280e2005-11-12 14:22:11 +0000266 struct pxaohci_platform_data *inf;
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530267 struct usb_hcd *hcd = dev_get_drvdata(dev);
Eric Miao0c392ed2008-10-04 12:57:21 +0800268 uint32_t uhccoms;
Richard Purdie81f280e2005-11-12 14:22:11 +0000269
Jingoo Hand4f09e22013-07-30 19:59:40 +0900270 inf = dev_get_platdata(dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000271
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300272 if (cpu_is_pxa3xx())
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530273 pxa3xx_u2d_stop_hc(&hcd->self);
Igor Grinberg6dc3ae82010-07-27 15:06:59 +0300274
Richard Purdie81f280e2005-11-12 14:22:11 +0000275 if (inf->exit)
276 inf->exit(dev);
277
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530278 pxa27x_reset_hc(pxa_ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Eric Miao0c392ed2008-10-04 12:57:21 +0800280 /* Host Controller Reset */
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530281 uhccoms = __raw_readl(pxa_ohci->mmio_base + UHCCOMS) | 0x01;
282 __raw_writel(uhccoms, pxa_ohci->mmio_base + UHCCOMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 udelay(10);
284
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530285 clk_disable_unprepare(pxa_ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Daniel Mack1139b452012-07-30 09:29:12 +0200288#ifdef CONFIG_OF
289static const struct of_device_id pxa_ohci_dt_ids[] = {
290 { .compatible = "marvell,pxa-ohci" },
291 { }
292};
293
294MODULE_DEVICE_TABLE(of, pxa_ohci_dt_ids);
295
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500296static int ohci_pxa_of_init(struct platform_device *pdev)
Daniel Mack1139b452012-07-30 09:29:12 +0200297{
298 struct device_node *np = pdev->dev.of_node;
299 struct pxaohci_platform_data *pdata;
300 u32 tmp;
301
302 if (!np)
303 return 0;
304
305 /* Right now device-tree probed devices don't get dma_mask set.
306 * Since shared usb code relies on it, set it here for now.
307 * Once we have dma capability bindings this can go away.
308 */
309 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -0600310 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
311 if (!pdev->dev.coherent_dma_mask)
312 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Daniel Mack1139b452012-07-30 09:29:12 +0200313
314 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
315 if (!pdata)
316 return -ENOMEM;
317
318 if (of_get_property(np, "marvell,enable-port1", NULL))
319 pdata->flags |= ENABLE_PORT1;
320 if (of_get_property(np, "marvell,enable-port2", NULL))
321 pdata->flags |= ENABLE_PORT2;
322 if (of_get_property(np, "marvell,enable-port3", NULL))
323 pdata->flags |= ENABLE_PORT3;
324 if (of_get_property(np, "marvell,port-sense-low", NULL))
325 pdata->flags |= POWER_SENSE_LOW;
326 if (of_get_property(np, "marvell,power-control-low", NULL))
327 pdata->flags |= POWER_CONTROL_LOW;
328 if (of_get_property(np, "marvell,no-oc-protection", NULL))
329 pdata->flags |= NO_OC_PROTECTION;
330 if (of_get_property(np, "marvell,oc-mode-perport", NULL))
331 pdata->flags |= OC_MODE_PERPORT;
332 if (!of_property_read_u32(np, "marvell,power-on-delay", &tmp))
333 pdata->power_on_delay = tmp;
334 if (!of_property_read_u32(np, "marvell,port-mode", &tmp))
335 pdata->port_mode = tmp;
336 if (!of_property_read_u32(np, "marvell,power-budget", &tmp))
337 pdata->power_budget = tmp;
338
339 pdev->dev.platform_data = pdata;
340
341 return 0;
342}
343#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500344static int ohci_pxa_of_init(struct platform_device *pdev)
Daniel Mack1139b452012-07-30 09:29:12 +0200345{
346 return 0;
347}
348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/*-------------------------------------------------------------------------*/
351
352/* configure so an HC device and id are always provided */
353/* always called with process context; sleeping is OK */
354
355
356/**
357 * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
358 * Context: !in_interrupt()
359 *
360 * Allocates basic resources for this USB host controller, and
361 * then invokes the start() method for the HCD associated with it
362 * through the hotplug entry's driver_data.
363 *
364 */
Richard Purdie81f280e2005-11-12 14:22:11 +0000365int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Eric Miao84bab732008-10-03 22:37:21 +0800367 int retval, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct usb_hcd *hcd;
Richard Purdie81f280e2005-11-12 14:22:11 +0000369 struct pxaohci_platform_data *inf;
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530370 struct pxa27x_ohci *pxa_ohci;
371 struct ohci_hcd *ohci;
Eric Miao84bab732008-10-03 22:37:21 +0800372 struct resource *r;
Eric Miao0c392ed2008-10-04 12:57:21 +0800373 struct clk *usb_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Daniel Mack1139b452012-07-30 09:29:12 +0200375 retval = ohci_pxa_of_init(pdev);
376 if (retval)
377 return retval;
378
Jingoo Hand4f09e22013-07-30 19:59:40 +0900379 inf = dev_get_platdata(&pdev->dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000380
381 if (!inf)
382 return -ENODEV;
383
Eric Miao84bab732008-10-03 22:37:21 +0800384 irq = platform_get_irq(pdev, 0);
385 if (irq < 0) {
386 pr_err("no resource of IORESOURCE_IRQ");
387 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Russell Kinge0d8b132008-11-11 17:52:32 +0000390 usb_clk = clk_get(&pdev->dev, NULL);
eric miaoa8bcf412007-12-12 08:53:25 +0800391 if (IS_ERR(usb_clk))
392 return PTR_ERR(usb_clk);
393
Richard Purdie81f280e2005-11-12 14:22:11 +0000394 hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
Julia Lawall7febe2b2011-06-01 19:10:06 +0200395 if (!hcd) {
396 retval = -ENOMEM;
397 goto err0;
398 }
Eric Miao84bab732008-10-03 22:37:21 +0800399
400 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
401 if (!r) {
402 pr_err("no resource of IORESOURCE_MEM");
403 retval = -ENXIO;
404 goto err1;
405 }
406
407 hcd->rsrc_start = r->start;
408 hcd->rsrc_len = resource_size(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
411 pr_debug("request_mem_region failed");
412 retval = -EBUSY;
413 goto err1;
414 }
415
416 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
417 if (!hcd->regs) {
418 pr_debug("ioremap failed");
419 retval = -ENOMEM;
420 goto err2;
421 }
422
Eric Miao0c392ed2008-10-04 12:57:21 +0800423 /* initialize "struct pxa27x_ohci" */
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530424 pxa_ohci = to_pxa27x_ohci(hcd);
425 pxa_ohci->clk = usb_clk;
426 pxa_ohci->mmio_base = (void __iomem *)hcd->regs;
Eric Miao0c392ed2008-10-04 12:57:21 +0800427
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530428 retval = pxa27x_start_hc(pxa_ohci, &pdev->dev);
429 if (retval < 0) {
Richard Purdie81f280e2005-11-12 14:22:11 +0000430 pr_debug("pxa27x_start_hc failed");
431 goto err3;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* Select Power Management Mode */
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530435 pxa27x_ohci_select_pmm(pxa_ohci, inf->port_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Richard Purdie0c27c5d2006-06-08 22:44:07 +0100437 if (inf->power_budget)
438 hcd->power_budget = inf->power_budget;
439
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530440 /* The value of NDP in roothub_a is incorrect on this hardware */
441 ohci = hcd_to_ohci(hcd);
442 ohci->num_ports = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800444 retval = usb_add_hcd(hcd, irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (retval == 0)
446 return retval;
447
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530448 pxa27x_stop_hc(pxa_ohci, &pdev->dev);
Richard Purdie81f280e2005-11-12 14:22:11 +0000449 err3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 iounmap(hcd->regs);
451 err2:
452 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
453 err1:
454 usb_put_hcd(hcd);
Julia Lawall7febe2b2011-06-01 19:10:06 +0200455 err0:
eric miaoa8bcf412007-12-12 08:53:25 +0800456 clk_put(usb_clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return retval;
458}
459
460
461/* may be called without controller electrically present */
462/* may be called with controller, bus, and devices active */
463
464/**
465 * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
466 * @dev: USB Host Controller being removed
467 * Context: !in_interrupt()
468 *
469 * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
470 * the HCD's stop() method. It is always called from a thread
471 * context, normally "rmmod", "apmd", or something similar.
472 *
473 */
Richard Purdie81f280e2005-11-12 14:22:11 +0000474void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530476 struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
Eric Miao0c392ed2008-10-04 12:57:21 +0800477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 usb_remove_hcd(hcd);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530479 pxa27x_stop_hc(pxa_ohci, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 iounmap(hcd->regs);
481 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530482 clk_put(pxa_ohci->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 usb_put_hcd(hcd);
484}
485
486/*-------------------------------------------------------------------------*/
487
Russell King3ae5eae2005-11-09 22:32:44 +0000488static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 pr_debug ("In ohci_hcd_pxa27x_drv_probe");
491
492 if (usb_disabled())
493 return -ENODEV;
494
Richard Purdie81f280e2005-11-12 14:22:11 +0000495 return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Russell King3ae5eae2005-11-09 22:32:44 +0000498static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Russell King3ae5eae2005-11-09 22:32:44 +0000500 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 usb_hcd_pxa27x_remove(hcd, pdev);
503 return 0;
504}
505
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300506#ifdef CONFIG_PM
507static int ohci_hcd_pxa27x_drv_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300509 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530510 struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
511 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Manjunath Goudar4ceaa892013-10-04 09:58:15 +0530512 bool do_wakeup = device_may_wakeup(dev);
513 int ret;
514
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000515
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530516 if (time_before(jiffies, ohci->next_statechange))
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000517 msleep(5);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530518 ohci->next_statechange = jiffies;
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000519
Manjunath Goudar4ceaa892013-10-04 09:58:15 +0530520 ret = ohci_suspend(hcd, do_wakeup);
521 if (ret)
522 return ret;
523
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530524 pxa27x_stop_hc(pxa_ohci, dev);
Manjunath Goudar4ceaa892013-10-04 09:58:15 +0530525 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300528static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300530 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530531 struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900532 struct pxaohci_platform_data *inf = dev_get_platdata(dev);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530533 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000534 int status;
535
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530536 if (time_before(jiffies, ohci->next_statechange))
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000537 msleep(5);
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530538 ohci->next_statechange = jiffies;
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000539
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530540 status = pxa27x_start_hc(pxa_ohci, dev);
541 if (status < 0)
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000542 return status;
543
Aric D. Blumera75d0482009-07-30 13:42:30 -0400544 /* Select Power Management Mode */
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530545 pxa27x_ohci_select_pmm(pxa_ohci, inf->port_mode);
Aric D. Blumera75d0482009-07-30 13:42:30 -0400546
Florian Fainellicfa49b42012-10-08 15:11:29 +0200547 ohci_resume(hcd, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300550
Alexey Dobriyan47145212009-12-14 18:00:08 -0800551static const struct dev_pm_ops ohci_hcd_pxa27x_pm_ops = {
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300552 .suspend = ohci_hcd_pxa27x_drv_suspend,
553 .resume = ohci_hcd_pxa27x_drv_resume,
554};
Richard Purdie2e1dcc12005-11-12 14:22:14 +0000555#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Russell King3ae5eae2005-11-09 22:32:44 +0000557static struct platform_driver ohci_hcd_pxa27x_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .probe = ohci_hcd_pxa27x_drv_probe,
559 .remove = ohci_hcd_pxa27x_drv_remove,
David Brownelldd9048a2006-12-05 03:18:31 -0800560 .shutdown = usb_hcd_platform_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000561 .driver = {
562 .name = "pxa27x-ohci",
Kay Sieversf4fce612008-04-10 21:29:22 -0700563 .owner = THIS_MODULE,
Daniel Mack1139b452012-07-30 09:29:12 +0200564 .of_match_table = of_match_ptr(pxa_ohci_dt_ids),
Mike Rapoportb7f3f592009-07-21 17:49:39 +0300565#ifdef CONFIG_PM
566 .pm = &ohci_hcd_pxa27x_pm_ops,
567#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000568 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Manjunath Goudarb8ad5c32013-09-21 16:38:46 +0530571static const struct ohci_driver_overrides pxa27x_overrides __initconst = {
572 .extra_priv_size = sizeof(struct pxa27x_ohci),
573};
574
575static int __init ohci_pxa27x_init(void)
576{
577 if (usb_disabled())
578 return -ENODEV;
579
580 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
581 ohci_init_driver(&ohci_pxa27x_hc_driver, &pxa27x_overrides);
582 return platform_driver_register(&ohci_hcd_pxa27x_driver);
583}
584module_init(ohci_pxa27x_init);
585
586static void __exit ohci_pxa27x_cleanup(void)
587{
588 platform_driver_unregister(&ohci_hcd_pxa27x_driver);
589}
590module_exit(ohci_pxa27x_cleanup);
591
592MODULE_DESCRIPTION(DRIVER_DESC);
593MODULE_LICENSE("GPL");
594MODULE_ALIAS("platform:pxa27x-ohci");