xref: /linux/drivers/usb/host/ohci-pxa27x.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * OHCI HCD (Host Controller Driver) for USB.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
61da177e4SLinus Torvalds  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
71da177e4SLinus Torvalds  * (C) Copyright 2002 Hewlett-Packard Company
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Bus Glue for pxa27x
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Written by Christopher Hoover <ch@hpl.hp.com>
121da177e4SLinus Torvalds  * Based on fragments of previous driver by Russell King et al.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Modified for LH7A404 from ohci-sa1111.c
151da177e4SLinus Torvalds  *  by Durgesh Pattamatta <pattamattad@sharpsec.com>
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Modified for pxa27x from ohci-lh7a404.c
181da177e4SLinus Torvalds  *  by Nick Bane <nick@cecomputing.co.uk> 26-8-2004
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * This file is licenced under the GPL.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23a8bcf410Seric miao #include <linux/clk.h>
24b8ad5c37SManjunath Goudar #include <linux/device.h>
259876388eSDaniel Mack #include <linux/dma-mapping.h>
26b8ad5c37SManjunath Goudar #include <linux/io.h>
27b8ad5c37SManjunath Goudar #include <linux/kernel.h>
28b8ad5c37SManjunath Goudar #include <linux/module.h>
291139b451SDaniel Mack #include <linux/of_platform.h>
30293b2da1SArnd Bergmann #include <linux/platform_data/usb-ohci-pxa27x.h>
31a9ae9c52SArnd Bergmann #include <linux/platform_data/pxa2xx_udc.h>
32b8ad5c37SManjunath Goudar #include <linux/platform_device.h>
33cecabe5cSLaurent Pinchart #include <linux/regulator/consumer.h>
34b8ad5c37SManjunath Goudar #include <linux/signal.h>
35b8ad5c37SManjunath Goudar #include <linux/usb.h>
36b8ad5c37SManjunath Goudar #include <linux/usb/hcd.h>
37b8ad5c37SManjunath Goudar #include <linux/usb/otg.h>
3808d3df8cSArnd Bergmann #include <linux/soc/pxa/cpu.h>
39b8ad5c37SManjunath Goudar 
40b8ad5c37SManjunath Goudar #include "ohci.h"
41b8ad5c37SManjunath Goudar 
42b8ad5c37SManjunath Goudar #define DRIVER_DESC "OHCI PXA27x/PXA3x driver"
431da177e4SLinus Torvalds 
44596050bcSEric Miao /*
45596050bcSEric Miao  * UHC: USB Host Controller (OHCI-like) register definitions
46596050bcSEric Miao  */
470c392ed9SEric Miao #define UHCREV		(0x0000) /* UHC HCI Spec Revision */
480c392ed9SEric Miao #define UHCHCON		(0x0004) /* UHC Host Control Register */
490c392ed9SEric Miao #define UHCCOMS		(0x0008) /* UHC Command Status Register */
500c392ed9SEric Miao #define UHCINTS		(0x000C) /* UHC Interrupt Status Register */
510c392ed9SEric Miao #define UHCINTE		(0x0010) /* UHC Interrupt Enable */
520c392ed9SEric Miao #define UHCINTD		(0x0014) /* UHC Interrupt Disable */
530c392ed9SEric Miao #define UHCHCCA		(0x0018) /* UHC Host Controller Comm. Area */
540c392ed9SEric Miao #define UHCPCED		(0x001C) /* UHC Period Current Endpt Descr */
550c392ed9SEric Miao #define UHCCHED		(0x0020) /* UHC Control Head Endpt Descr */
560c392ed9SEric Miao #define UHCCCED		(0x0024) /* UHC Control Current Endpt Descr */
570c392ed9SEric Miao #define UHCBHED		(0x0028) /* UHC Bulk Head Endpt Descr */
580c392ed9SEric Miao #define UHCBCED		(0x002C) /* UHC Bulk Current Endpt Descr */
590c392ed9SEric Miao #define UHCDHEAD	(0x0030) /* UHC Done Head */
600c392ed9SEric Miao #define UHCFMI		(0x0034) /* UHC Frame Interval */
610c392ed9SEric Miao #define UHCFMR		(0x0038) /* UHC Frame Remaining */
620c392ed9SEric Miao #define UHCFMN		(0x003C) /* UHC Frame Number */
630c392ed9SEric Miao #define UHCPERS		(0x0040) /* UHC Periodic Start */
640c392ed9SEric Miao #define UHCLS		(0x0044) /* UHC Low Speed Threshold */
65596050bcSEric Miao 
660c392ed9SEric Miao #define UHCRHDA		(0x0048) /* UHC Root Hub Descriptor A */
67596050bcSEric Miao #define UHCRHDA_NOCP	(1 << 12)	/* No over current protection */
68596050bcSEric Miao #define UHCRHDA_OCPM	(1 << 11)	/* Over Current Protection Mode */
69596050bcSEric Miao #define UHCRHDA_POTPGT(x) \
70596050bcSEric Miao 			(((x) & 0xff) << 24) /* Power On To Power Good Time */
71596050bcSEric Miao 
720c392ed9SEric Miao #define UHCRHDB		(0x004C) /* UHC Root Hub Descriptor B */
730c392ed9SEric Miao #define UHCRHS		(0x0050) /* UHC Root Hub Status */
740c392ed9SEric Miao #define UHCRHPS1	(0x0054) /* UHC Root Hub Port 1 Status */
750c392ed9SEric Miao #define UHCRHPS2	(0x0058) /* UHC Root Hub Port 2 Status */
760c392ed9SEric Miao #define UHCRHPS3	(0x005C) /* UHC Root Hub Port 3 Status */
77596050bcSEric Miao 
780c392ed9SEric Miao #define UHCSTAT		(0x0060) /* UHC Status Register */
79596050bcSEric Miao #define UHCSTAT_UPS3	(1 << 16)	/* USB Power Sense Port3 */
80596050bcSEric Miao #define UHCSTAT_SBMAI	(1 << 15)	/* System Bus Master Abort Interrupt*/
81596050bcSEric Miao #define UHCSTAT_SBTAI	(1 << 14)	/* System Bus Target Abort Interrupt*/
82596050bcSEric Miao #define UHCSTAT_UPRI	(1 << 13)	/* USB Port Resume Interrupt */
83596050bcSEric Miao #define UHCSTAT_UPS2	(1 << 12)	/* USB Power Sense Port 2 */
84596050bcSEric Miao #define UHCSTAT_UPS1	(1 << 11)	/* USB Power Sense Port 1 */
85596050bcSEric Miao #define UHCSTAT_HTA	(1 << 10)	/* HCI Target Abort */
86596050bcSEric Miao #define UHCSTAT_HBA	(1 << 8)	/* HCI Buffer Active */
87596050bcSEric Miao #define UHCSTAT_RWUE	(1 << 7)	/* HCI Remote Wake Up Event */
88596050bcSEric Miao 
890c392ed9SEric Miao #define UHCHR           (0x0064) /* UHC Reset Register */
90596050bcSEric Miao #define UHCHR_SSEP3	(1 << 11)	/* Sleep Standby Enable for Port3 */
91596050bcSEric Miao #define UHCHR_SSEP2	(1 << 10)	/* Sleep Standby Enable for Port2 */
92596050bcSEric Miao #define UHCHR_SSEP1	(1 << 9)	/* Sleep Standby Enable for Port1 */
93596050bcSEric Miao #define UHCHR_PCPL	(1 << 7)	/* Power control polarity low */
94596050bcSEric Miao #define UHCHR_PSPL	(1 << 6)	/* Power sense polarity low */
95596050bcSEric Miao #define UHCHR_SSE	(1 << 5)	/* Sleep Standby Enable */
96596050bcSEric Miao #define UHCHR_UIT	(1 << 4)	/* USB Interrupt Test */
97596050bcSEric Miao #define UHCHR_SSDC	(1 << 3)	/* Simulation Scale Down Clock */
98596050bcSEric Miao #define UHCHR_CGR	(1 << 2)	/* Clock Generation Reset */
99596050bcSEric Miao #define UHCHR_FHR	(1 << 1)	/* Force Host Controller Reset */
100596050bcSEric Miao #define UHCHR_FSBIR	(1 << 0)	/* Force System Bus Iface Reset */
101596050bcSEric Miao 
1020c392ed9SEric Miao #define UHCHIE          (0x0068) /* UHC Interrupt Enable Register*/
103596050bcSEric Miao #define UHCHIE_UPS3IE	(1 << 14)	/* Power Sense Port3 IntEn */
104596050bcSEric Miao #define UHCHIE_UPRIE	(1 << 13)	/* Port Resume IntEn */
105596050bcSEric Miao #define UHCHIE_UPS2IE	(1 << 12)	/* Power Sense Port2 IntEn */
106596050bcSEric Miao #define UHCHIE_UPS1IE	(1 << 11)	/* Power Sense Port1 IntEn */
107596050bcSEric Miao #define UHCHIE_TAIE	(1 << 10)	/* HCI Interface Transfer Abort
108596050bcSEric Miao 					   Interrupt Enable*/
109596050bcSEric Miao #define UHCHIE_HBAIE	(1 << 8)	/* HCI Buffer Active IntEn */
110596050bcSEric Miao #define UHCHIE_RWIE	(1 << 7)	/* Remote Wake-up IntEn */
111596050bcSEric Miao 
1120c392ed9SEric Miao #define UHCHIT          (0x006C) /* UHC Interrupt Test register */
113596050bcSEric Miao 
1141da177e4SLinus Torvalds #define PXA_UHC_MAX_PORTNUM    3
1151da177e4SLinus Torvalds 
116b8ad5c37SManjunath Goudar static struct hc_driver __read_mostly ohci_pxa27x_hc_driver;
117b8ad5c37SManjunath Goudar 
118b8ad5c37SManjunath Goudar struct pxa27x_ohci {
1190c392ed9SEric Miao 	struct clk	*clk;
1200c392ed9SEric Miao 	void __iomem	*mmio_base;
121cecabe5cSLaurent Pinchart 	struct regulator *vbus[3];
122cecabe5cSLaurent Pinchart 	bool		vbus_enabled[3];
1230c392ed9SEric Miao };
1240c392ed9SEric Miao 
125b8ad5c37SManjunath Goudar #define to_pxa27x_ohci(hcd)	(struct pxa27x_ohci *)(hcd_to_ohci(hcd)->priv)
126a8bcf410Seric miao 
1271da177e4SLinus Torvalds /*
1281da177e4SLinus Torvalds   PMM_NPS_MODE -- PMM Non-power switching mode
1291da177e4SLinus Torvalds       Ports are powered continuously.
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds   PMM_GLOBAL_MODE -- PMM global switching mode
1321da177e4SLinus Torvalds       All ports are powered at the same time.
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds   PMM_PERPORT_MODE -- PMM per port switching mode
1351da177e4SLinus Torvalds       Ports are powered individually.
1361da177e4SLinus Torvalds  */
pxa27x_ohci_select_pmm(struct pxa27x_ohci * pxa_ohci,int mode)137b8ad5c37SManjunath Goudar static int pxa27x_ohci_select_pmm(struct pxa27x_ohci *pxa_ohci, int mode)
1381da177e4SLinus Torvalds {
139b8ad5c37SManjunath Goudar 	uint32_t uhcrhda = __raw_readl(pxa_ohci->mmio_base + UHCRHDA);
140b8ad5c37SManjunath Goudar 	uint32_t uhcrhdb = __raw_readl(pxa_ohci->mmio_base + UHCRHDB);
1410c392ed9SEric Miao 
1421da177e4SLinus Torvalds 	switch (mode) {
1431da177e4SLinus Torvalds 	case PMM_NPS_MODE:
1440c392ed9SEric Miao 		uhcrhda |= RH_A_NPS;
1451da177e4SLinus Torvalds 		break;
1461da177e4SLinus Torvalds 	case PMM_GLOBAL_MODE:
1470709831aSDan Carpenter 		uhcrhda &= ~(RH_A_NPS | RH_A_PSM);
1481da177e4SLinus Torvalds 		break;
1491da177e4SLinus Torvalds 	case PMM_PERPORT_MODE:
1500c392ed9SEric Miao 		uhcrhda &= ~(RH_A_NPS);
1510c392ed9SEric Miao 		uhcrhda |= RH_A_PSM;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 		/* Set port power control mask bits, only 3 ports. */
1540c392ed9SEric Miao 		uhcrhdb |= (0x7<<17);
1551da177e4SLinus Torvalds 		break;
1561da177e4SLinus Torvalds 	default:
1571da177e4SLinus Torvalds 		printk( KERN_ERR
1581da177e4SLinus Torvalds 			"Invalid mode %d, set to non-power switch mode.\n",
1591da177e4SLinus Torvalds 			mode );
1601da177e4SLinus Torvalds 
1610c392ed9SEric Miao 		uhcrhda |= RH_A_NPS;
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 
164b8ad5c37SManjunath Goudar 	__raw_writel(uhcrhda, pxa_ohci->mmio_base + UHCRHDA);
165b8ad5c37SManjunath Goudar 	__raw_writel(uhcrhdb, pxa_ohci->mmio_base + UHCRHDB);
1661da177e4SLinus Torvalds 	return 0;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
pxa27x_ohci_set_vbus_power(struct pxa27x_ohci * pxa_ohci,unsigned int port,bool enable)169cecabe5cSLaurent Pinchart static int pxa27x_ohci_set_vbus_power(struct pxa27x_ohci *pxa_ohci,
170cecabe5cSLaurent Pinchart 				      unsigned int port, bool enable)
171cecabe5cSLaurent Pinchart {
172cecabe5cSLaurent Pinchart 	struct regulator *vbus = pxa_ohci->vbus[port];
173cecabe5cSLaurent Pinchart 	int ret = 0;
174cecabe5cSLaurent Pinchart 
175cecabe5cSLaurent Pinchart 	if (IS_ERR_OR_NULL(vbus))
176cecabe5cSLaurent Pinchart 		return 0;
177cecabe5cSLaurent Pinchart 
178cecabe5cSLaurent Pinchart 	if (enable && !pxa_ohci->vbus_enabled[port])
179cecabe5cSLaurent Pinchart 		ret = regulator_enable(vbus);
180cecabe5cSLaurent Pinchart 	else if (!enable && pxa_ohci->vbus_enabled[port])
181cecabe5cSLaurent Pinchart 		ret = regulator_disable(vbus);
182cecabe5cSLaurent Pinchart 
183cecabe5cSLaurent Pinchart 	if (ret < 0)
184cecabe5cSLaurent Pinchart 		return ret;
185cecabe5cSLaurent Pinchart 
186cecabe5cSLaurent Pinchart 	pxa_ohci->vbus_enabled[port] = enable;
187cecabe5cSLaurent Pinchart 
188cecabe5cSLaurent Pinchart 	return 0;
189cecabe5cSLaurent Pinchart }
190cecabe5cSLaurent Pinchart 
pxa27x_ohci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)191cecabe5cSLaurent Pinchart static int pxa27x_ohci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
192cecabe5cSLaurent Pinchart 				   u16 wIndex, char *buf, u16 wLength)
193cecabe5cSLaurent Pinchart {
194cecabe5cSLaurent Pinchart 	struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
195cecabe5cSLaurent Pinchart 	int ret;
196cecabe5cSLaurent Pinchart 
197cecabe5cSLaurent Pinchart 	switch (typeReq) {
198cecabe5cSLaurent Pinchart 	case SetPortFeature:
199cecabe5cSLaurent Pinchart 	case ClearPortFeature:
200cecabe5cSLaurent Pinchart 		if (!wIndex || wIndex > 3)
201cecabe5cSLaurent Pinchart 			return -EPIPE;
202cecabe5cSLaurent Pinchart 
203cecabe5cSLaurent Pinchart 		if (wValue != USB_PORT_FEAT_POWER)
204cecabe5cSLaurent Pinchart 			break;
205cecabe5cSLaurent Pinchart 
206cecabe5cSLaurent Pinchart 		ret = pxa27x_ohci_set_vbus_power(pxa_ohci, wIndex - 1,
207cecabe5cSLaurent Pinchart 						 typeReq == SetPortFeature);
208cecabe5cSLaurent Pinchart 		if (ret)
209cecabe5cSLaurent Pinchart 			return ret;
210cecabe5cSLaurent Pinchart 		break;
211cecabe5cSLaurent Pinchart 	}
212cecabe5cSLaurent Pinchart 
213cecabe5cSLaurent Pinchart 	return ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
214cecabe5cSLaurent Pinchart }
2151da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2161da177e4SLinus Torvalds 
pxa27x_setup_hc(struct pxa27x_ohci * pxa_ohci,struct pxaohci_platform_data * inf)217b8ad5c37SManjunath Goudar static inline void pxa27x_setup_hc(struct pxa27x_ohci *pxa_ohci,
2180c392ed9SEric Miao 				   struct pxaohci_platform_data *inf)
219097b5334SEric Miao {
220b8ad5c37SManjunath Goudar 	uint32_t uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR);
221b8ad5c37SManjunath Goudar 	uint32_t uhcrhda = __raw_readl(pxa_ohci->mmio_base + UHCRHDA);
222097b5334SEric Miao 
223097b5334SEric Miao 	if (inf->flags & ENABLE_PORT1)
224097b5334SEric Miao 		uhchr &= ~UHCHR_SSEP1;
225097b5334SEric Miao 
226097b5334SEric Miao 	if (inf->flags & ENABLE_PORT2)
227097b5334SEric Miao 		uhchr &= ~UHCHR_SSEP2;
228097b5334SEric Miao 
229097b5334SEric Miao 	if (inf->flags & ENABLE_PORT3)
230097b5334SEric Miao 		uhchr &= ~UHCHR_SSEP3;
231097b5334SEric Miao 
232097b5334SEric Miao 	if (inf->flags & POWER_CONTROL_LOW)
233097b5334SEric Miao 		uhchr |= UHCHR_PCPL;
234097b5334SEric Miao 
235097b5334SEric Miao 	if (inf->flags & POWER_SENSE_LOW)
236097b5334SEric Miao 		uhchr |= UHCHR_PSPL;
237097b5334SEric Miao 
238097b5334SEric Miao 	if (inf->flags & NO_OC_PROTECTION)
239097b5334SEric Miao 		uhcrhda |= UHCRHDA_NOCP;
2407b4361f0SAric Blumer 	else
2417b4361f0SAric Blumer 		uhcrhda &= ~UHCRHDA_NOCP;
242097b5334SEric Miao 
243097b5334SEric Miao 	if (inf->flags & OC_MODE_PERPORT)
244097b5334SEric Miao 		uhcrhda |= UHCRHDA_OCPM;
2457b4361f0SAric Blumer 	else
2467b4361f0SAric Blumer 		uhcrhda &= ~UHCRHDA_OCPM;
247097b5334SEric Miao 
248097b5334SEric Miao 	if (inf->power_on_delay) {
249097b5334SEric Miao 		uhcrhda &= ~UHCRHDA_POTPGT(0xff);
250097b5334SEric Miao 		uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
251097b5334SEric Miao 	}
252097b5334SEric Miao 
253b8ad5c37SManjunath Goudar 	__raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
254b8ad5c37SManjunath Goudar 	__raw_writel(uhcrhda, pxa_ohci->mmio_base + UHCRHDA);
2550c392ed9SEric Miao }
2560c392ed9SEric Miao 
pxa27x_reset_hc(struct pxa27x_ohci * pxa_ohci)257b8ad5c37SManjunath Goudar static inline void pxa27x_reset_hc(struct pxa27x_ohci *pxa_ohci)
2580c392ed9SEric Miao {
259b8ad5c37SManjunath Goudar 	uint32_t uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR);
2600c392ed9SEric Miao 
261b8ad5c37SManjunath Goudar 	__raw_writel(uhchr | UHCHR_FHR, pxa_ohci->mmio_base + UHCHR);
2620c392ed9SEric Miao 	udelay(11);
263b8ad5c37SManjunath Goudar 	__raw_writel(uhchr & ~UHCHR_FHR, pxa_ohci->mmio_base + UHCHR);
264097b5334SEric Miao }
265097b5334SEric Miao 
pxa27x_start_hc(struct pxa27x_ohci * pxa_ohci,struct device * dev)266b8ad5c37SManjunath Goudar static int pxa27x_start_hc(struct pxa27x_ohci *pxa_ohci, struct device *dev)
2671da177e4SLinus Torvalds {
268af06d009SArvind Yadav 	int retval;
26981f280e2SRichard Purdie 	struct pxaohci_platform_data *inf;
2700c392ed9SEric Miao 	uint32_t uhchr;
27181f280e2SRichard Purdie 
272d4f09e28SJingoo Han 	inf = dev_get_platdata(dev);
27381f280e2SRichard Purdie 
274af06d009SArvind Yadav 	retval = clk_prepare_enable(pxa_ohci->clk);
275af06d009SArvind Yadav 	if (retval)
276af06d009SArvind Yadav 		return retval;
2771da177e4SLinus Torvalds 
278b8ad5c37SManjunath Goudar 	pxa27x_reset_hc(pxa_ohci);
2791da177e4SLinus Torvalds 
280b8ad5c37SManjunath Goudar 	uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR) | UHCHR_FSBIR;
281b8ad5c37SManjunath Goudar 	__raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
2820c392ed9SEric Miao 
283b8ad5c37SManjunath Goudar 	while (__raw_readl(pxa_ohci->mmio_base + UHCHR) & UHCHR_FSBIR)
2841da177e4SLinus Torvalds 		cpu_relax();
2851da177e4SLinus Torvalds 
286b8ad5c37SManjunath Goudar 	pxa27x_setup_hc(pxa_ohci, inf);
287097b5334SEric Miao 
28881f280e2SRichard Purdie 	if (inf->init)
28981f280e2SRichard Purdie 		retval = inf->init(dev);
2901da177e4SLinus Torvalds 
291af06d009SArvind Yadav 	if (retval < 0) {
292af06d009SArvind Yadav 		clk_disable_unprepare(pxa_ohci->clk);
29381f280e2SRichard Purdie 		return retval;
294af06d009SArvind Yadav 	}
2951da177e4SLinus Torvalds 
296b8ad5c37SManjunath Goudar 	uhchr = __raw_readl(pxa_ohci->mmio_base + UHCHR) & ~UHCHR_SSE;
297b8ad5c37SManjunath Goudar 	__raw_writel(uhchr, pxa_ohci->mmio_base + UHCHR);
298b8ad5c37SManjunath Goudar 	__raw_writel(UHCHIE_UPRIE | UHCHIE_RWIE, pxa_ohci->mmio_base + UHCHIE);
299155faf5eSDavid Brownell 
300155faf5eSDavid Brownell 	/* Clear any OTG Pin Hold */
3010cb0b0d3SEric Miao 	pxa27x_clear_otgph();
30281f280e2SRichard Purdie 	return 0;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
pxa27x_stop_hc(struct pxa27x_ohci * pxa_ohci,struct device * dev)305b8ad5c37SManjunath Goudar static void pxa27x_stop_hc(struct pxa27x_ohci *pxa_ohci, struct device *dev)
3061da177e4SLinus Torvalds {
30781f280e2SRichard Purdie 	struct pxaohci_platform_data *inf;
3080c392ed9SEric Miao 	uint32_t uhccoms;
30981f280e2SRichard Purdie 
310d4f09e28SJingoo Han 	inf = dev_get_platdata(dev);
31181f280e2SRichard Purdie 
31281f280e2SRichard Purdie 	if (inf->exit)
31381f280e2SRichard Purdie 		inf->exit(dev);
31481f280e2SRichard Purdie 
315b8ad5c37SManjunath Goudar 	pxa27x_reset_hc(pxa_ohci);
3161da177e4SLinus Torvalds 
3170c392ed9SEric Miao 	/* Host Controller Reset */
318b8ad5c37SManjunath Goudar 	uhccoms = __raw_readl(pxa_ohci->mmio_base + UHCCOMS) | 0x01;
319b8ad5c37SManjunath Goudar 	__raw_writel(uhccoms, pxa_ohci->mmio_base + UHCCOMS);
3201da177e4SLinus Torvalds 	udelay(10);
3211da177e4SLinus Torvalds 
322b8ad5c37SManjunath Goudar 	clk_disable_unprepare(pxa_ohci->clk);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251139b451SDaniel Mack #ifdef CONFIG_OF
3261139b451SDaniel Mack static const struct of_device_id pxa_ohci_dt_ids[] = {
3271139b451SDaniel Mack 	{ .compatible = "marvell,pxa-ohci" },
3281139b451SDaniel Mack 	{ }
3291139b451SDaniel Mack };
3301139b451SDaniel Mack 
3311139b451SDaniel Mack MODULE_DEVICE_TABLE(of, pxa_ohci_dt_ids);
3321139b451SDaniel Mack 
ohci_pxa_of_init(struct platform_device * pdev)33341ac7b3aSBill Pemberton static int ohci_pxa_of_init(struct platform_device *pdev)
3341139b451SDaniel Mack {
3351139b451SDaniel Mack 	struct device_node *np = pdev->dev.of_node;
3361139b451SDaniel Mack 	struct pxaohci_platform_data *pdata;
3371139b451SDaniel Mack 	u32 tmp;
33822d9d8e8SRussell King 	int ret;
3391139b451SDaniel Mack 
3401139b451SDaniel Mack 	if (!np)
3411139b451SDaniel Mack 		return 0;
3421139b451SDaniel Mack 
3431139b451SDaniel Mack 	/* Right now device-tree probed devices don't get dma_mask set.
3441139b451SDaniel Mack 	 * Since shared usb code relies on it, set it here for now.
3451139b451SDaniel Mack 	 * Once we have dma capability bindings this can go away.
3461139b451SDaniel Mack 	 */
347e1fd7341SRussell King 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
34822d9d8e8SRussell King 	if (ret)
34922d9d8e8SRussell King 		return ret;
3501139b451SDaniel Mack 
3511139b451SDaniel Mack 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3521139b451SDaniel Mack 	if (!pdata)
3531139b451SDaniel Mack 		return -ENOMEM;
3541139b451SDaniel Mack 
355144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,enable-port1"))
3561139b451SDaniel Mack 		pdata->flags |= ENABLE_PORT1;
357144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,enable-port2"))
3581139b451SDaniel Mack 		pdata->flags |= ENABLE_PORT2;
359144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,enable-port3"))
3601139b451SDaniel Mack 		pdata->flags |= ENABLE_PORT3;
361144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,port-sense-low"))
3621139b451SDaniel Mack 		pdata->flags |= POWER_SENSE_LOW;
363144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,power-control-low"))
3641139b451SDaniel Mack 		pdata->flags |= POWER_CONTROL_LOW;
365144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,no-oc-protection"))
3661139b451SDaniel Mack 		pdata->flags |= NO_OC_PROTECTION;
367144e38c6SSaurabh Sengar 	if (of_property_read_bool(np, "marvell,oc-mode-perport"))
3681139b451SDaniel Mack 		pdata->flags |= OC_MODE_PERPORT;
3691139b451SDaniel Mack 	if (!of_property_read_u32(np, "marvell,power-on-delay", &tmp))
3701139b451SDaniel Mack 		pdata->power_on_delay = tmp;
3711139b451SDaniel Mack 	if (!of_property_read_u32(np, "marvell,port-mode", &tmp))
3721139b451SDaniel Mack 		pdata->port_mode = tmp;
3731139b451SDaniel Mack 	if (!of_property_read_u32(np, "marvell,power-budget", &tmp))
3741139b451SDaniel Mack 		pdata->power_budget = tmp;
3751139b451SDaniel Mack 
3761139b451SDaniel Mack 	pdev->dev.platform_data = pdata;
3771139b451SDaniel Mack 
3781139b451SDaniel Mack 	return 0;
3791139b451SDaniel Mack }
3801139b451SDaniel Mack #else
ohci_pxa_of_init(struct platform_device * pdev)38141ac7b3aSBill Pemberton static int ohci_pxa_of_init(struct platform_device *pdev)
3821139b451SDaniel Mack {
3831139b451SDaniel Mack 	return 0;
3841139b451SDaniel Mack }
3851139b451SDaniel Mack #endif
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /* configure so an HC device and id are always provided */
3901da177e4SLinus Torvalds /* always called with process context; sleeping is OK */
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /**
394c9e82b07SManjunath Goudar  * ohci_hcd_pxa27x_probe - initialize pxa27x-based HCDs
39519220bacSAhmed S. Darwish  * @pdev:	USB Host controller to probe
39619220bacSAhmed S. Darwish  *
39719220bacSAhmed S. Darwish  * Context: task context, might sleep
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * Allocates basic resources for this USB host controller, and
4001da177e4SLinus Torvalds  * then invokes the start() method for the HCD associated with it
4011da177e4SLinus Torvalds  * through the hotplug entry's driver_data.
4021da177e4SLinus Torvalds  */
ohci_hcd_pxa27x_probe(struct platform_device * pdev)403c9e82b07SManjunath Goudar static int ohci_hcd_pxa27x_probe(struct platform_device *pdev)
4041da177e4SLinus Torvalds {
40584bab739SEric Miao 	int retval, irq;
4061da177e4SLinus Torvalds 	struct usb_hcd *hcd;
40781f280e2SRichard Purdie 	struct pxaohci_platform_data *inf;
408b8ad5c37SManjunath Goudar 	struct pxa27x_ohci *pxa_ohci;
409b8ad5c37SManjunath Goudar 	struct ohci_hcd *ohci;
41084bab739SEric Miao 	struct resource *r;
4110c392ed9SEric Miao 	struct clk *usb_clk;
412cecabe5cSLaurent Pinchart 	unsigned int i;
4131da177e4SLinus Torvalds 
4141139b451SDaniel Mack 	retval = ohci_pxa_of_init(pdev);
4151139b451SDaniel Mack 	if (retval)
4161139b451SDaniel Mack 		return retval;
4171139b451SDaniel Mack 
418d4f09e28SJingoo Han 	inf = dev_get_platdata(&pdev->dev);
41981f280e2SRichard Purdie 
42081f280e2SRichard Purdie 	if (!inf)
42181f280e2SRichard Purdie 		return -ENODEV;
42281f280e2SRichard Purdie 
42384bab739SEric Miao 	irq = platform_get_irq(pdev, 0);
42484bab739SEric Miao 	if (irq < 0) {
42584bab739SEric Miao 		pr_err("no resource of IORESOURCE_IRQ");
426c3853d5aSRobert Jarzmik 		return irq;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
429f1080e4dSJingoo Han 	usb_clk = devm_clk_get(&pdev->dev, NULL);
430a8bcf410Seric miao 	if (IS_ERR(usb_clk))
431a8bcf410Seric miao 		return PTR_ERR(usb_clk);
432a8bcf410Seric miao 
433c9e82b07SManjunath Goudar 	hcd = usb_create_hcd(&ohci_pxa27x_hc_driver, &pdev->dev, "pxa27x");
434f1080e4dSJingoo Han 	if (!hcd)
435f1080e4dSJingoo Han 		return -ENOMEM;
43684bab739SEric Miao 
437*e873efdbSYangtao Li 	hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
438f1080e4dSJingoo Han 	if (IS_ERR(hcd->regs)) {
439f1080e4dSJingoo Han 		retval = PTR_ERR(hcd->regs);
440f1080e4dSJingoo Han 		goto err;
4411da177e4SLinus Torvalds 	}
44215c85d9cSVarka Bhadram 	hcd->rsrc_start = r->start;
44315c85d9cSVarka Bhadram 	hcd->rsrc_len = resource_size(r);
4441da177e4SLinus Torvalds 
4450c392ed9SEric Miao 	/* initialize "struct pxa27x_ohci" */
446b8ad5c37SManjunath Goudar 	pxa_ohci = to_pxa27x_ohci(hcd);
447b8ad5c37SManjunath Goudar 	pxa_ohci->clk = usb_clk;
448b8ad5c37SManjunath Goudar 	pxa_ohci->mmio_base = (void __iomem *)hcd->regs;
4490c392ed9SEric Miao 
450cecabe5cSLaurent Pinchart 	for (i = 0; i < 3; ++i) {
451cecabe5cSLaurent Pinchart 		char name[6];
452cecabe5cSLaurent Pinchart 
453cecabe5cSLaurent Pinchart 		if (!(inf->flags & (ENABLE_PORT1 << i)))
454cecabe5cSLaurent Pinchart 			continue;
455cecabe5cSLaurent Pinchart 
456cecabe5cSLaurent Pinchart 		sprintf(name, "vbus%u", i + 1);
457cecabe5cSLaurent Pinchart 		pxa_ohci->vbus[i] = devm_regulator_get(&pdev->dev, name);
458cecabe5cSLaurent Pinchart 	}
459cecabe5cSLaurent Pinchart 
460b8ad5c37SManjunath Goudar 	retval = pxa27x_start_hc(pxa_ohci, &pdev->dev);
461b8ad5c37SManjunath Goudar 	if (retval < 0) {
46281f280e2SRichard Purdie 		pr_debug("pxa27x_start_hc failed");
463f1080e4dSJingoo Han 		goto err;
46481f280e2SRichard Purdie 	}
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	/* Select Power Management Mode */
467b8ad5c37SManjunath Goudar 	pxa27x_ohci_select_pmm(pxa_ohci, inf->port_mode);
4681da177e4SLinus Torvalds 
4690c27c5d5SRichard Purdie 	if (inf->power_budget)
4700c27c5d5SRichard Purdie 		hcd->power_budget = inf->power_budget;
4710c27c5d5SRichard Purdie 
472b8ad5c37SManjunath Goudar 	/* The value of NDP in roothub_a is incorrect on this hardware */
473b8ad5c37SManjunath Goudar 	ohci = hcd_to_ohci(hcd);
474b8ad5c37SManjunath Goudar 	ohci->num_ports = 3;
4751da177e4SLinus Torvalds 
476b5dd18d8SYong Zhang 	retval = usb_add_hcd(hcd, irq, 0);
4773c9740a1SPeter Chen 	if (retval == 0) {
4783c9740a1SPeter Chen 		device_wakeup_enable(hcd->self.controller);
4791da177e4SLinus Torvalds 		return retval;
4803c9740a1SPeter Chen 	}
4811da177e4SLinus Torvalds 
482b8ad5c37SManjunath Goudar 	pxa27x_stop_hc(pxa_ohci, &pdev->dev);
483f1080e4dSJingoo Han  err:
4841da177e4SLinus Torvalds 	usb_put_hcd(hcd);
4851da177e4SLinus Torvalds 	return retval;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds /* may be called without controller electrically present */
4901da177e4SLinus Torvalds /* may be called with controller, bus, and devices active */
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /**
493c9e82b07SManjunath Goudar  * ohci_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
49419220bacSAhmed S. Darwish  * @pdev: USB Host Controller being removed
49519220bacSAhmed S. Darwish  *
49619220bacSAhmed S. Darwish  * Context: task context, might sleep
4971da177e4SLinus Torvalds  *
498c9e82b07SManjunath Goudar  * Reverses the effect of ohci_hcd_pxa27x_probe(), first invoking
4991da177e4SLinus Torvalds  * the HCD's stop() method.  It is always called from a thread
5001da177e4SLinus Torvalds  * context, normally "rmmod", "apmd", or something similar.
5011da177e4SLinus Torvalds  */
ohci_hcd_pxa27x_remove(struct platform_device * pdev)5028c5f41acSUwe Kleine-König static void ohci_hcd_pxa27x_remove(struct platform_device *pdev)
5031da177e4SLinus Torvalds {
504c9e82b07SManjunath Goudar 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
505b8ad5c37SManjunath Goudar 	struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
506cecabe5cSLaurent Pinchart 	unsigned int i;
5070c392ed9SEric Miao 
5081da177e4SLinus Torvalds 	usb_remove_hcd(hcd);
509b8ad5c37SManjunath Goudar 	pxa27x_stop_hc(pxa_ohci, &pdev->dev);
510cecabe5cSLaurent Pinchart 
511cecabe5cSLaurent Pinchart 	for (i = 0; i < 3; ++i)
512cecabe5cSLaurent Pinchart 		pxa27x_ohci_set_vbus_power(pxa_ohci, i, false);
513cecabe5cSLaurent Pinchart 
5141da177e4SLinus Torvalds 	usb_put_hcd(hcd);
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5181da177e4SLinus Torvalds 
5192e1dcc16SRichard Purdie #ifdef CONFIG_PM
ohci_hcd_pxa27x_drv_suspend(struct device * dev)520b7f3f59bSMike Rapoport static int ohci_hcd_pxa27x_drv_suspend(struct device *dev)
5211da177e4SLinus Torvalds {
522b7f3f59bSMike Rapoport 	struct usb_hcd *hcd = dev_get_drvdata(dev);
523b8ad5c37SManjunath Goudar 	struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
524b8ad5c37SManjunath Goudar 	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
5254ceaa893SManjunath Goudar 	bool do_wakeup = device_may_wakeup(dev);
5264ceaa893SManjunath Goudar 	int ret;
5274ceaa893SManjunath Goudar 
5282e1dcc16SRichard Purdie 
529b8ad5c37SManjunath Goudar 	if (time_before(jiffies, ohci->next_statechange))
5302e1dcc16SRichard Purdie 		msleep(5);
531b8ad5c37SManjunath Goudar 	ohci->next_statechange = jiffies;
5322e1dcc16SRichard Purdie 
5334ceaa893SManjunath Goudar 	ret = ohci_suspend(hcd, do_wakeup);
5344ceaa893SManjunath Goudar 	if (ret)
5354ceaa893SManjunath Goudar 		return ret;
5364ceaa893SManjunath Goudar 
537b8ad5c37SManjunath Goudar 	pxa27x_stop_hc(pxa_ohci, dev);
5384ceaa893SManjunath Goudar 	return ret;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
ohci_hcd_pxa27x_drv_resume(struct device * dev)541b7f3f59bSMike Rapoport static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
5421da177e4SLinus Torvalds {
543b7f3f59bSMike Rapoport 	struct usb_hcd *hcd = dev_get_drvdata(dev);
544b8ad5c37SManjunath Goudar 	struct pxa27x_ohci *pxa_ohci = to_pxa27x_ohci(hcd);
545d4f09e28SJingoo Han 	struct pxaohci_platform_data *inf = dev_get_platdata(dev);
546b8ad5c37SManjunath Goudar 	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
5472e1dcc16SRichard Purdie 	int status;
5482e1dcc16SRichard Purdie 
549b8ad5c37SManjunath Goudar 	if (time_before(jiffies, ohci->next_statechange))
5502e1dcc16SRichard Purdie 		msleep(5);
551b8ad5c37SManjunath Goudar 	ohci->next_statechange = jiffies;
5522e1dcc16SRichard Purdie 
553b8ad5c37SManjunath Goudar 	status = pxa27x_start_hc(pxa_ohci, dev);
554b8ad5c37SManjunath Goudar 	if (status < 0)
5552e1dcc16SRichard Purdie 		return status;
5562e1dcc16SRichard Purdie 
557a75d048eSAric D. Blumer 	/* Select Power Management Mode */
558b8ad5c37SManjunath Goudar 	pxa27x_ohci_select_pmm(pxa_ohci, inf->port_mode);
559a75d048eSAric D. Blumer 
560cfa49b4bSFlorian Fainelli 	ohci_resume(hcd, false);
5611da177e4SLinus Torvalds 	return 0;
5621da177e4SLinus Torvalds }
563b7f3f59bSMike Rapoport 
56447145210SAlexey Dobriyan static const struct dev_pm_ops ohci_hcd_pxa27x_pm_ops = {
565b7f3f59bSMike Rapoport 	.suspend	= ohci_hcd_pxa27x_drv_suspend,
566b7f3f59bSMike Rapoport 	.resume		= ohci_hcd_pxa27x_drv_resume,
567b7f3f59bSMike Rapoport };
5682e1dcc16SRichard Purdie #endif
5691da177e4SLinus Torvalds 
5703ae5eaecSRussell King static struct platform_driver ohci_hcd_pxa27x_driver = {
571c9e82b07SManjunath Goudar 	.probe		= ohci_hcd_pxa27x_probe,
5728c5f41acSUwe Kleine-König 	.remove_new	= ohci_hcd_pxa27x_remove,
57364a21d02SAleksey Gorelov 	.shutdown	= usb_hcd_platform_shutdown,
5743ae5eaecSRussell King 	.driver		= {
5753ae5eaecSRussell King 		.name	= "pxa27x-ohci",
5761139b451SDaniel Mack 		.of_match_table = of_match_ptr(pxa_ohci_dt_ids),
577b7f3f59bSMike Rapoport #ifdef CONFIG_PM
578b7f3f59bSMike Rapoport 		.pm	= &ohci_hcd_pxa27x_pm_ops,
579b7f3f59bSMike Rapoport #endif
5803ae5eaecSRussell King 	},
5811da177e4SLinus Torvalds };
5821da177e4SLinus Torvalds 
583b8ad5c37SManjunath Goudar static const struct ohci_driver_overrides pxa27x_overrides __initconst = {
584b8ad5c37SManjunath Goudar 	.extra_priv_size =      sizeof(struct pxa27x_ohci),
585b8ad5c37SManjunath Goudar };
586b8ad5c37SManjunath Goudar 
ohci_pxa27x_init(void)587b8ad5c37SManjunath Goudar static int __init ohci_pxa27x_init(void)
588b8ad5c37SManjunath Goudar {
589b8ad5c37SManjunath Goudar 	if (usb_disabled())
590b8ad5c37SManjunath Goudar 		return -ENODEV;
591b8ad5c37SManjunath Goudar 
592b8ad5c37SManjunath Goudar 	ohci_init_driver(&ohci_pxa27x_hc_driver, &pxa27x_overrides);
593cecabe5cSLaurent Pinchart 	ohci_pxa27x_hc_driver.hub_control = pxa27x_ohci_hub_control;
594cecabe5cSLaurent Pinchart 
595b8ad5c37SManjunath Goudar 	return platform_driver_register(&ohci_hcd_pxa27x_driver);
596b8ad5c37SManjunath Goudar }
597b8ad5c37SManjunath Goudar module_init(ohci_pxa27x_init);
598b8ad5c37SManjunath Goudar 
ohci_pxa27x_cleanup(void)599b8ad5c37SManjunath Goudar static void __exit ohci_pxa27x_cleanup(void)
600b8ad5c37SManjunath Goudar {
601b8ad5c37SManjunath Goudar 	platform_driver_unregister(&ohci_hcd_pxa27x_driver);
602b8ad5c37SManjunath Goudar }
603b8ad5c37SManjunath Goudar module_exit(ohci_pxa27x_cleanup);
604b8ad5c37SManjunath Goudar 
605b8ad5c37SManjunath Goudar MODULE_DESCRIPTION(DRIVER_DESC);
606b8ad5c37SManjunath Goudar MODULE_LICENSE("GPL");
607b8ad5c37SManjunath Goudar MODULE_ALIAS("platform:pxa27x-ohci");
608