xref: /openbmc/linux/drivers/usb/host/ohci-da8xx.c (revision d327330185f192411be80563a3c8398f4538cdb2)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2efe7daf2SSergei Shtylyov /*
3efe7daf2SSergei Shtylyov  * OHCI HCD (Host Controller Driver) for USB.
4efe7daf2SSergei Shtylyov  *
5efe7daf2SSergei Shtylyov  * TI DA8xx (OMAP-L1x) Bus Glue
6efe7daf2SSergei Shtylyov  *
7efe7daf2SSergei Shtylyov  * Derived from: ohci-omap.c and ohci-s3c2410.c
8efe7daf2SSergei Shtylyov  * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9efe7daf2SSergei Shtylyov  */
10efe7daf2SSergei Shtylyov 
116c21caa3SManjunath Goudar #include <linux/clk.h>
12d193abf1SBartosz Golaszewski #include <linux/gpio/consumer.h>
136c21caa3SManjunath Goudar #include <linux/io.h>
14efe7daf2SSergei Shtylyov #include <linux/interrupt.h>
15efe7daf2SSergei Shtylyov #include <linux/jiffies.h>
166c21caa3SManjunath Goudar #include <linux/kernel.h>
176c21caa3SManjunath Goudar #include <linux/module.h>
18efe7daf2SSergei Shtylyov #include <linux/platform_device.h>
196110c425SDavid Lechner #include <linux/phy/phy.h>
20ec2a0833SArnd Bergmann #include <linux/platform_data/usb-davinci.h>
21c844ff74SAxel Haslam #include <linux/regulator/consumer.h>
226c21caa3SManjunath Goudar #include <linux/usb.h>
236c21caa3SManjunath Goudar #include <linux/usb/hcd.h>
246c21caa3SManjunath Goudar #include <asm/unaligned.h>
25efe7daf2SSergei Shtylyov 
266c21caa3SManjunath Goudar #include "ohci.h"
276c21caa3SManjunath Goudar 
286c21caa3SManjunath Goudar #define DRIVER_DESC "DA8XX"
29eacae5d2SAxel Haslam #define DRV_NAME "ohci-da8xx"
306c21caa3SManjunath Goudar 
316c21caa3SManjunath Goudar static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
326c21caa3SManjunath Goudar 
336c21caa3SManjunath Goudar static int (*orig_ohci_hub_control)(struct usb_hcd  *hcd, u16 typeReq,
346c21caa3SManjunath Goudar 			u16 wValue, u16 wIndex, char *buf, u16 wLength);
356c21caa3SManjunath Goudar static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
36efe7daf2SSergei Shtylyov 
37c7a4f9f3SAxel Haslam struct da8xx_ohci_hcd {
38c844ff74SAxel Haslam 	struct usb_hcd *hcd;
39c7a4f9f3SAxel Haslam 	struct clk *usb11_clk;
40c7a4f9f3SAxel Haslam 	struct phy *usb11_phy;
41c844ff74SAxel Haslam 	struct regulator *vbus_reg;
42c844ff74SAxel Haslam 	struct notifier_block nb;
43d193abf1SBartosz Golaszewski 	struct gpio_desc *vbus_gpio;
44d193abf1SBartosz Golaszewski 	struct gpio_desc *oc_gpio;
45c7a4f9f3SAxel Haslam };
46c7a4f9f3SAxel Haslam 
47c7a4f9f3SAxel Haslam #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
48efe7daf2SSergei Shtylyov 
49efe7daf2SSergei Shtylyov /* Over-current indicator change bitmask */
50efe7daf2SSergei Shtylyov static volatile u16 ocic_mask;
51efe7daf2SSergei Shtylyov 
52c7a4f9f3SAxel Haslam static int ohci_da8xx_enable(struct usb_hcd *hcd)
53efe7daf2SSergei Shtylyov {
54c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
556110c425SDavid Lechner 	int ret;
56efe7daf2SSergei Shtylyov 
57c7a4f9f3SAxel Haslam 	ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
586110c425SDavid Lechner 	if (ret)
596110c425SDavid Lechner 		return ret;
60efe7daf2SSergei Shtylyov 
61c7a4f9f3SAxel Haslam 	ret = phy_init(da8xx_ohci->usb11_phy);
626110c425SDavid Lechner 	if (ret)
636110c425SDavid Lechner 		goto err_phy_init;
64efe7daf2SSergei Shtylyov 
65c7a4f9f3SAxel Haslam 	ret = phy_power_on(da8xx_ohci->usb11_phy);
666110c425SDavid Lechner 	if (ret)
676110c425SDavid Lechner 		goto err_phy_power_on;
68efe7daf2SSergei Shtylyov 
696110c425SDavid Lechner 	return 0;
706110c425SDavid Lechner 
716110c425SDavid Lechner err_phy_power_on:
72c7a4f9f3SAxel Haslam 	phy_exit(da8xx_ohci->usb11_phy);
736110c425SDavid Lechner err_phy_init:
74c7a4f9f3SAxel Haslam 	clk_disable_unprepare(da8xx_ohci->usb11_clk);
756110c425SDavid Lechner 
766110c425SDavid Lechner 	return ret;
77efe7daf2SSergei Shtylyov }
78efe7daf2SSergei Shtylyov 
79c7a4f9f3SAxel Haslam static void ohci_da8xx_disable(struct usb_hcd *hcd)
806110c425SDavid Lechner {
81c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
82c7a4f9f3SAxel Haslam 
83c7a4f9f3SAxel Haslam 	phy_power_off(da8xx_ohci->usb11_phy);
84c7a4f9f3SAxel Haslam 	phy_exit(da8xx_ohci->usb11_phy);
85c7a4f9f3SAxel Haslam 	clk_disable_unprepare(da8xx_ohci->usb11_clk);
86efe7daf2SSergei Shtylyov }
87efe7daf2SSergei Shtylyov 
88f3c56fb3SAxel Haslam static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
89f3c56fb3SAxel Haslam {
90c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
91f3c56fb3SAxel Haslam 	struct device *dev = hcd->self.controller;
92c844ff74SAxel Haslam 	int ret;
93f3c56fb3SAxel Haslam 
94d193abf1SBartosz Golaszewski 	if (da8xx_ohci->vbus_gpio) {
95d193abf1SBartosz Golaszewski 		gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, on);
96d193abf1SBartosz Golaszewski 		return 0;
97d193abf1SBartosz Golaszewski 	}
98f3c56fb3SAxel Haslam 
99c844ff74SAxel Haslam 	if (!da8xx_ohci->vbus_reg)
100c844ff74SAxel Haslam 		return 0;
101c844ff74SAxel Haslam 
1028e2f5eaeSBartosz Golaszewski 	if (on) {
103c844ff74SAxel Haslam 		ret = regulator_enable(da8xx_ohci->vbus_reg);
104c844ff74SAxel Haslam 		if (ret) {
105c844ff74SAxel Haslam 			dev_err(dev, "Failed to enable regulator: %d\n", ret);
106c844ff74SAxel Haslam 			return ret;
107c844ff74SAxel Haslam 		}
1088e2f5eaeSBartosz Golaszewski 	} else {
109c844ff74SAxel Haslam 		ret = regulator_disable(da8xx_ohci->vbus_reg);
110c844ff74SAxel Haslam 		if (ret) {
111c844ff74SAxel Haslam 			dev_err(dev, "Failed  to disable regulator: %d\n", ret);
112c844ff74SAxel Haslam 			return ret;
113c844ff74SAxel Haslam 		}
114c844ff74SAxel Haslam 	}
115c844ff74SAxel Haslam 
116f3c56fb3SAxel Haslam 	return 0;
117f3c56fb3SAxel Haslam }
118f3c56fb3SAxel Haslam 
119f3c56fb3SAxel Haslam static int ohci_da8xx_get_power(struct usb_hcd *hcd)
120f3c56fb3SAxel Haslam {
121c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
122f3c56fb3SAxel Haslam 
123d193abf1SBartosz Golaszewski 	if (da8xx_ohci->vbus_gpio)
124d193abf1SBartosz Golaszewski 		return gpiod_get_value_cansleep(da8xx_ohci->vbus_gpio);
125f3c56fb3SAxel Haslam 
126c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
127c844ff74SAxel Haslam 		return regulator_is_enabled(da8xx_ohci->vbus_reg);
128c844ff74SAxel Haslam 
129f3c56fb3SAxel Haslam 	return 1;
130f3c56fb3SAxel Haslam }
131f3c56fb3SAxel Haslam 
132f3c56fb3SAxel Haslam static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
133f3c56fb3SAxel Haslam {
134c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
135c844ff74SAxel Haslam 	unsigned int flags;
136c844ff74SAxel Haslam 	int ret;
137f3c56fb3SAxel Haslam 
138d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio)
139d193abf1SBartosz Golaszewski 		return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
140f3c56fb3SAxel Haslam 
141c844ff74SAxel Haslam 	if (!da8xx_ohci->vbus_reg)
142c844ff74SAxel Haslam 		return 0;
143c844ff74SAxel Haslam 
144c844ff74SAxel Haslam 	ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
145c844ff74SAxel Haslam 	if (ret)
146c844ff74SAxel Haslam 		return ret;
147c844ff74SAxel Haslam 
148c844ff74SAxel Haslam 	if (flags & REGULATOR_ERROR_OVER_CURRENT)
149c844ff74SAxel Haslam 		return 1;
150c844ff74SAxel Haslam 
151f3c56fb3SAxel Haslam 	return 0;
152f3c56fb3SAxel Haslam }
153f3c56fb3SAxel Haslam 
154f3c56fb3SAxel Haslam static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
155f3c56fb3SAxel Haslam {
156c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
157f3c56fb3SAxel Haslam 
158d193abf1SBartosz Golaszewski 	if (da8xx_ohci->vbus_gpio)
159f3c56fb3SAxel Haslam 		return 1;
160f3c56fb3SAxel Haslam 
161c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
162c844ff74SAxel Haslam 		return 1;
163c844ff74SAxel Haslam 
164f3c56fb3SAxel Haslam 	return 0;
165f3c56fb3SAxel Haslam }
166f3c56fb3SAxel Haslam 
167f3c56fb3SAxel Haslam static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
168f3c56fb3SAxel Haslam {
169c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
170f3c56fb3SAxel Haslam 
171d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio)
172f3c56fb3SAxel Haslam 		return 1;
173f3c56fb3SAxel Haslam 
174c844ff74SAxel Haslam 	if (da8xx_ohci->vbus_reg)
175c844ff74SAxel Haslam 		return 1;
176c844ff74SAxel Haslam 
177f3c56fb3SAxel Haslam 	return 0;
178f3c56fb3SAxel Haslam }
179f3c56fb3SAxel Haslam 
180f3c56fb3SAxel Haslam static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
181f3c56fb3SAxel Haslam {
182f3c56fb3SAxel Haslam 	struct device *dev		= hcd->self.controller;
183f3c56fb3SAxel Haslam 	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
184f3c56fb3SAxel Haslam 
185f3c56fb3SAxel Haslam 	if (hub && hub->potpgt)
186f3c56fb3SAxel Haslam 		return 1;
187f3c56fb3SAxel Haslam 
188f3c56fb3SAxel Haslam 	return 0;
189f3c56fb3SAxel Haslam }
190f3c56fb3SAxel Haslam 
191c844ff74SAxel Haslam static int ohci_da8xx_regulator_event(struct notifier_block *nb,
192c844ff74SAxel Haslam 				unsigned long event, void *data)
193f3c56fb3SAxel Haslam {
194c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci =
195c844ff74SAxel Haslam 				container_of(nb, struct da8xx_ohci_hcd, nb);
196f3c56fb3SAxel Haslam 
197c844ff74SAxel Haslam 	if (event & REGULATOR_EVENT_OVER_CURRENT) {
198c844ff74SAxel Haslam 		ocic_mask |= 1 << 1;
199c844ff74SAxel Haslam 		ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
200c844ff74SAxel Haslam 	}
201f3c56fb3SAxel Haslam 
202f3c56fb3SAxel Haslam 	return 0;
203f3c56fb3SAxel Haslam }
204f3c56fb3SAxel Haslam 
205*d3273301SBartosz Golaszewski static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
206d193abf1SBartosz Golaszewski {
207d193abf1SBartosz Golaszewski 	struct da8xx_ohci_hcd *da8xx_ohci = data;
208*d3273301SBartosz Golaszewski 	struct device *dev = da8xx_ohci->hcd->self.controller;
209*d3273301SBartosz Golaszewski 	int ret;
210d193abf1SBartosz Golaszewski 
211*d3273301SBartosz Golaszewski 	if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio)) {
212*d3273301SBartosz Golaszewski 		if (da8xx_ohci->vbus_gpio) {
213*d3273301SBartosz Golaszewski 			gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, 0);
214*d3273301SBartosz Golaszewski 		} else if (da8xx_ohci->vbus_reg) {
215*d3273301SBartosz Golaszewski 			ret = regulator_disable(da8xx_ohci->vbus_reg);
216*d3273301SBartosz Golaszewski 			if (ret)
217*d3273301SBartosz Golaszewski 				dev_err(dev,
218*d3273301SBartosz Golaszewski 					"Failed to disable regulator: %d\n",
219*d3273301SBartosz Golaszewski 					ret);
220*d3273301SBartosz Golaszewski 		}
221*d3273301SBartosz Golaszewski 	}
222d193abf1SBartosz Golaszewski 
223d193abf1SBartosz Golaszewski 	return IRQ_HANDLED;
224d193abf1SBartosz Golaszewski }
225d193abf1SBartosz Golaszewski 
226c844ff74SAxel Haslam static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
227c844ff74SAxel Haslam {
228c844ff74SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
229c844ff74SAxel Haslam 	struct device *dev		= hcd->self.controller;
230c844ff74SAxel Haslam 	int ret = 0;
231c844ff74SAxel Haslam 
232d193abf1SBartosz Golaszewski 	if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
233c844ff74SAxel Haslam 		da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
234c844ff74SAxel Haslam 		ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
235c844ff74SAxel Haslam 						&da8xx_ohci->nb);
236c844ff74SAxel Haslam 	}
237c844ff74SAxel Haslam 
238c844ff74SAxel Haslam 	if (ret)
239c844ff74SAxel Haslam 		dev_err(dev, "Failed to register notifier: %d\n", ret);
240c844ff74SAxel Haslam 
241c844ff74SAxel Haslam 	return ret;
242c844ff74SAxel Haslam }
243c844ff74SAxel Haslam 
2446c21caa3SManjunath Goudar static int ohci_da8xx_reset(struct usb_hcd *hcd)
245efe7daf2SSergei Shtylyov {
246efe7daf2SSergei Shtylyov 	struct device *dev		= hcd->self.controller;
247d4f09e28SJingoo Han 	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
248efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci		= hcd_to_ohci(hcd);
249efe7daf2SSergei Shtylyov 	int result;
250efe7daf2SSergei Shtylyov 	u32 rh_a;
251efe7daf2SSergei Shtylyov 
252efe7daf2SSergei Shtylyov 	dev_dbg(dev, "starting USB controller\n");
253efe7daf2SSergei Shtylyov 
254c7a4f9f3SAxel Haslam 	result = ohci_da8xx_enable(hcd);
2556110c425SDavid Lechner 	if (result < 0)
2566110c425SDavid Lechner 		return result;
257efe7daf2SSergei Shtylyov 
258efe7daf2SSergei Shtylyov 	/*
259efe7daf2SSergei Shtylyov 	 * DA8xx only have 1 port connected to the pins but the HC root hub
260efe7daf2SSergei Shtylyov 	 * register A reports 2 ports, thus we'll have to override it...
261efe7daf2SSergei Shtylyov 	 */
262efe7daf2SSergei Shtylyov 	ohci->num_ports = 1;
263efe7daf2SSergei Shtylyov 
2646c21caa3SManjunath Goudar 	result = ohci_setup(hcd);
2656110c425SDavid Lechner 	if (result < 0) {
266c7a4f9f3SAxel Haslam 		ohci_da8xx_disable(hcd);
267efe7daf2SSergei Shtylyov 		return result;
2686110c425SDavid Lechner 	}
269efe7daf2SSergei Shtylyov 
270efe7daf2SSergei Shtylyov 	/*
271efe7daf2SSergei Shtylyov 	 * Since we're providing a board-specific root hub port power control
272efe7daf2SSergei Shtylyov 	 * and over-current reporting, we have to override the HC root hub A
273efe7daf2SSergei Shtylyov 	 * register's default value, so that ohci_hub_control() could return
274efe7daf2SSergei Shtylyov 	 * the correct hub descriptor...
275efe7daf2SSergei Shtylyov 	 */
276efe7daf2SSergei Shtylyov 	rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
277f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_set_power(hcd)) {
278efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_NPS;
279efe7daf2SSergei Shtylyov 		rh_a |=  RH_A_PSM;
280efe7daf2SSergei Shtylyov 	}
281f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_oci(hcd)) {
282efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_NOCP;
283efe7daf2SSergei Shtylyov 		rh_a |=  RH_A_OCPM;
284efe7daf2SSergei Shtylyov 	}
285f3c56fb3SAxel Haslam 	if (ohci_da8xx_has_potpgt(hcd)) {
286efe7daf2SSergei Shtylyov 		rh_a &= ~RH_A_POTPGT;
287efe7daf2SSergei Shtylyov 		rh_a |= hub->potpgt << 24;
288f3c56fb3SAxel Haslam 	}
289efe7daf2SSergei Shtylyov 	ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
290efe7daf2SSergei Shtylyov 
291efe7daf2SSergei Shtylyov 	return result;
292efe7daf2SSergei Shtylyov }
293efe7daf2SSergei Shtylyov 
294efe7daf2SSergei Shtylyov /*
295efe7daf2SSergei Shtylyov  * Update the status data from the hub with the over-current indicator change.
296efe7daf2SSergei Shtylyov  */
297efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
298efe7daf2SSergei Shtylyov {
2996c21caa3SManjunath Goudar 	int length		= orig_ohci_hub_status_data(hcd, buf);
300efe7daf2SSergei Shtylyov 
301efe7daf2SSergei Shtylyov 	/* See if we have OCIC bit set on port 1 */
302efe7daf2SSergei Shtylyov 	if (ocic_mask & (1 << 1)) {
303efe7daf2SSergei Shtylyov 		dev_dbg(hcd->self.controller, "over-current indicator change "
304efe7daf2SSergei Shtylyov 			"on port 1\n");
305efe7daf2SSergei Shtylyov 
306efe7daf2SSergei Shtylyov 		if (!length)
307efe7daf2SSergei Shtylyov 			length = 1;
308efe7daf2SSergei Shtylyov 
309efe7daf2SSergei Shtylyov 		buf[0] |= 1 << 1;
310efe7daf2SSergei Shtylyov 	}
311efe7daf2SSergei Shtylyov 	return length;
312efe7daf2SSergei Shtylyov }
313efe7daf2SSergei Shtylyov 
314efe7daf2SSergei Shtylyov /*
315efe7daf2SSergei Shtylyov  * Look at the control requests to the root hub and see if we need to override.
316efe7daf2SSergei Shtylyov  */
317efe7daf2SSergei Shtylyov static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
318efe7daf2SSergei Shtylyov 				  u16 wIndex, char *buf, u16 wLength)
319efe7daf2SSergei Shtylyov {
320efe7daf2SSergei Shtylyov 	struct device *dev		= hcd->self.controller;
321efe7daf2SSergei Shtylyov 	int temp;
322efe7daf2SSergei Shtylyov 
323efe7daf2SSergei Shtylyov 	switch (typeReq) {
324efe7daf2SSergei Shtylyov 	case GetPortStatus:
325efe7daf2SSergei Shtylyov 		/* Check the port number */
326efe7daf2SSergei Shtylyov 		if (wIndex != 1)
327efe7daf2SSergei Shtylyov 			break;
328efe7daf2SSergei Shtylyov 
329efe7daf2SSergei Shtylyov 		dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
330efe7daf2SSergei Shtylyov 
331efe7daf2SSergei Shtylyov 		temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
332efe7daf2SSergei Shtylyov 
333efe7daf2SSergei Shtylyov 		/* The port power status (PPS) bit defaults to 1 */
334f3c56fb3SAxel Haslam 		if (!ohci_da8xx_get_power(hcd))
335efe7daf2SSergei Shtylyov 			temp &= ~RH_PS_PPS;
336efe7daf2SSergei Shtylyov 
337efe7daf2SSergei Shtylyov 		/* The port over-current indicator (POCI) bit is always 0 */
338f3c56fb3SAxel Haslam 		if (ohci_da8xx_get_oci(hcd) > 0)
339efe7daf2SSergei Shtylyov 			temp |=  RH_PS_POCI;
340efe7daf2SSergei Shtylyov 
341efe7daf2SSergei Shtylyov 		/* The over-current indicator change (OCIC) bit is 0 too */
342efe7daf2SSergei Shtylyov 		if (ocic_mask & (1 << wIndex))
343efe7daf2SSergei Shtylyov 			temp |=  RH_PS_OCIC;
344efe7daf2SSergei Shtylyov 
345efe7daf2SSergei Shtylyov 		put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
346efe7daf2SSergei Shtylyov 		return 0;
347efe7daf2SSergei Shtylyov 	case SetPortFeature:
348efe7daf2SSergei Shtylyov 		temp = 1;
349efe7daf2SSergei Shtylyov 		goto check_port;
350efe7daf2SSergei Shtylyov 	case ClearPortFeature:
351efe7daf2SSergei Shtylyov 		temp = 0;
352efe7daf2SSergei Shtylyov 
353efe7daf2SSergei Shtylyov check_port:
354efe7daf2SSergei Shtylyov 		/* Check the port number */
355efe7daf2SSergei Shtylyov 		if (wIndex != 1)
356efe7daf2SSergei Shtylyov 			break;
357efe7daf2SSergei Shtylyov 
358efe7daf2SSergei Shtylyov 		switch (wValue) {
359efe7daf2SSergei Shtylyov 		case USB_PORT_FEAT_POWER:
360efe7daf2SSergei Shtylyov 			dev_dbg(dev, "%sPortFeature(%u): %s\n",
361efe7daf2SSergei Shtylyov 				temp ? "Set" : "Clear", wIndex, "POWER");
362efe7daf2SSergei Shtylyov 
363f3c56fb3SAxel Haslam 			return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
364efe7daf2SSergei Shtylyov 		case USB_PORT_FEAT_C_OVER_CURRENT:
365efe7daf2SSergei Shtylyov 			dev_dbg(dev, "%sPortFeature(%u): %s\n",
366efe7daf2SSergei Shtylyov 				temp ? "Set" : "Clear", wIndex,
367efe7daf2SSergei Shtylyov 				"C_OVER_CURRENT");
368efe7daf2SSergei Shtylyov 
369efe7daf2SSergei Shtylyov 			if (temp)
370efe7daf2SSergei Shtylyov 				ocic_mask |= 1 << wIndex;
371efe7daf2SSergei Shtylyov 			else
372efe7daf2SSergei Shtylyov 				ocic_mask &= ~(1 << wIndex);
373efe7daf2SSergei Shtylyov 			return 0;
374efe7daf2SSergei Shtylyov 		}
375efe7daf2SSergei Shtylyov 	}
376efe7daf2SSergei Shtylyov 
3776c21caa3SManjunath Goudar 	return orig_ohci_hub_control(hcd, typeReq, wValue,
3786c21caa3SManjunath Goudar 			wIndex, buf, wLength);
379efe7daf2SSergei Shtylyov }
380efe7daf2SSergei Shtylyov 
381efe7daf2SSergei Shtylyov /*-------------------------------------------------------------------------*/
382190534f6SAxel Haslam #ifdef CONFIG_OF
383190534f6SAxel Haslam static const struct of_device_id da8xx_ohci_ids[] = {
384190534f6SAxel Haslam 	{ .compatible = "ti,da830-ohci" },
385190534f6SAxel Haslam 	{ }
386190534f6SAxel Haslam };
387190534f6SAxel Haslam MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
388190534f6SAxel Haslam #endif
389efe7daf2SSergei Shtylyov 
3906c21caa3SManjunath Goudar static int ohci_da8xx_probe(struct platform_device *pdev)
391efe7daf2SSergei Shtylyov {
392c7a4f9f3SAxel Haslam 	struct da8xx_ohci_hcd *da8xx_ohci;
3933d2ab9f3SBartosz Golaszewski 	struct device *dev = &pdev->dev;
394d193abf1SBartosz Golaszewski 	int error, hcd_irq, oc_irq;
395efe7daf2SSergei Shtylyov 	struct usb_hcd	*hcd;
396efe7daf2SSergei Shtylyov 	struct resource *mem;
39708e46f18SBartosz Golaszewski 
3983d2ab9f3SBartosz Golaszewski 	hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
399644db166SJingoo Han 	if (!hcd)
400644db166SJingoo Han 		return -ENOMEM;
401efe7daf2SSergei Shtylyov 
402c7a4f9f3SAxel Haslam 	da8xx_ohci = to_da8xx_ohci(hcd);
403c844ff74SAxel Haslam 	da8xx_ohci->hcd = hcd;
404c7a4f9f3SAxel Haslam 
4053d2ab9f3SBartosz Golaszewski 	da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
406c7a4f9f3SAxel Haslam 	if (IS_ERR(da8xx_ohci->usb11_clk)) {
407c7a4f9f3SAxel Haslam 		error = PTR_ERR(da8xx_ohci->usb11_clk);
408c7a4f9f3SAxel Haslam 		if (error != -EPROBE_DEFER)
4093d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get clock.\n");
410c7a4f9f3SAxel Haslam 		goto err;
411c7a4f9f3SAxel Haslam 	}
412c7a4f9f3SAxel Haslam 
4133d2ab9f3SBartosz Golaszewski 	da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
414c7a4f9f3SAxel Haslam 	if (IS_ERR(da8xx_ohci->usb11_phy)) {
415c7a4f9f3SAxel Haslam 		error = PTR_ERR(da8xx_ohci->usb11_phy);
416c7a4f9f3SAxel Haslam 		if (error != -EPROBE_DEFER)
4173d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get phy.\n");
418c7a4f9f3SAxel Haslam 		goto err;
419c7a4f9f3SAxel Haslam 	}
420c7a4f9f3SAxel Haslam 
4213d2ab9f3SBartosz Golaszewski 	da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
422c844ff74SAxel Haslam 	if (IS_ERR(da8xx_ohci->vbus_reg)) {
423c844ff74SAxel Haslam 		error = PTR_ERR(da8xx_ohci->vbus_reg);
424c844ff74SAxel Haslam 		if (error == -ENODEV) {
425c844ff74SAxel Haslam 			da8xx_ohci->vbus_reg = NULL;
426c844ff74SAxel Haslam 		} else if (error == -EPROBE_DEFER) {
427c844ff74SAxel Haslam 			goto err;
428c844ff74SAxel Haslam 		} else {
4293d2ab9f3SBartosz Golaszewski 			dev_err(dev, "Failed to get regulator\n");
430c844ff74SAxel Haslam 			goto err;
431c844ff74SAxel Haslam 		}
432c844ff74SAxel Haslam 	}
433c844ff74SAxel Haslam 
434d193abf1SBartosz Golaszewski 	da8xx_ohci->vbus_gpio = devm_gpiod_get_optional(dev, "vbus",
435d193abf1SBartosz Golaszewski 							GPIOD_OUT_HIGH);
436d193abf1SBartosz Golaszewski 	if (IS_ERR(da8xx_ohci->vbus_gpio))
437d193abf1SBartosz Golaszewski 		goto err;
438d193abf1SBartosz Golaszewski 
439d193abf1SBartosz Golaszewski 	da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
440d193abf1SBartosz Golaszewski 	if (IS_ERR(da8xx_ohci->oc_gpio))
441d193abf1SBartosz Golaszewski 		goto err;
442d193abf1SBartosz Golaszewski 
443d193abf1SBartosz Golaszewski 	if (da8xx_ohci->oc_gpio) {
444d193abf1SBartosz Golaszewski 		oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
445d193abf1SBartosz Golaszewski 		if (oc_irq < 0)
446d193abf1SBartosz Golaszewski 			goto err;
447d193abf1SBartosz Golaszewski 
448*d3273301SBartosz Golaszewski 		error = devm_request_threaded_irq(dev, oc_irq, NULL,
449*d3273301SBartosz Golaszewski 				ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
450*d3273301SBartosz Golaszewski 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
451d193abf1SBartosz Golaszewski 				"OHCI over-current indicator", da8xx_ohci);
452d193abf1SBartosz Golaszewski 		if (error)
453d193abf1SBartosz Golaszewski 			goto err;
454d193abf1SBartosz Golaszewski 	}
455d193abf1SBartosz Golaszewski 
456efe7daf2SSergei Shtylyov 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4573d2ab9f3SBartosz Golaszewski 	hcd->regs = devm_ioremap_resource(dev, mem);
458644db166SJingoo Han 	if (IS_ERR(hcd->regs)) {
459644db166SJingoo Han 		error = PTR_ERR(hcd->regs);
460644db166SJingoo Han 		goto err;
461efe7daf2SSergei Shtylyov 	}
46254891d74SVarka Bhadram 	hcd->rsrc_start = mem->start;
46354891d74SVarka Bhadram 	hcd->rsrc_len = resource_size(mem);
464efe7daf2SSergei Shtylyov 
465d193abf1SBartosz Golaszewski 	hcd_irq = platform_get_irq(pdev, 0);
466d193abf1SBartosz Golaszewski 	if (hcd_irq < 0) {
467efe7daf2SSergei Shtylyov 		error = -ENODEV;
468644db166SJingoo Han 		goto err;
469efe7daf2SSergei Shtylyov 	}
4706c21caa3SManjunath Goudar 
471d193abf1SBartosz Golaszewski 	error = usb_add_hcd(hcd, hcd_irq, 0);
472efe7daf2SSergei Shtylyov 	if (error)
473644db166SJingoo Han 		goto err;
474efe7daf2SSergei Shtylyov 
4753c9740a1SPeter Chen 	device_wakeup_enable(hcd->self.controller);
4763c9740a1SPeter Chen 
477f3c56fb3SAxel Haslam 	error = ohci_da8xx_register_notify(hcd);
478f3c56fb3SAxel Haslam 	if (error)
479f3c56fb3SAxel Haslam 		goto err_remove_hcd;
480efe7daf2SSergei Shtylyov 
481f3c56fb3SAxel Haslam 	return 0;
482f3c56fb3SAxel Haslam 
483f3c56fb3SAxel Haslam err_remove_hcd:
484efe7daf2SSergei Shtylyov 	usb_remove_hcd(hcd);
485644db166SJingoo Han err:
486efe7daf2SSergei Shtylyov 	usb_put_hcd(hcd);
487efe7daf2SSergei Shtylyov 	return error;
488efe7daf2SSergei Shtylyov }
489efe7daf2SSergei Shtylyov 
4906c21caa3SManjunath Goudar static int ohci_da8xx_remove(struct platform_device *pdev)
491efe7daf2SSergei Shtylyov {
4926c21caa3SManjunath Goudar 	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
493efe7daf2SSergei Shtylyov 
494efe7daf2SSergei Shtylyov 	usb_remove_hcd(hcd);
495efe7daf2SSergei Shtylyov 	usb_put_hcd(hcd);
496efe7daf2SSergei Shtylyov 
497efe7daf2SSergei Shtylyov 	return 0;
498efe7daf2SSergei Shtylyov }
499efe7daf2SSergei Shtylyov 
500efe7daf2SSergei Shtylyov #ifdef CONFIG_PM
501933bb1f0SMajunath Goudar static int ohci_da8xx_suspend(struct platform_device *pdev,
502933bb1f0SMajunath Goudar 				pm_message_t message)
503efe7daf2SSergei Shtylyov {
504933bb1f0SMajunath Goudar 	struct usb_hcd	*hcd	= platform_get_drvdata(pdev);
505efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
506933bb1f0SMajunath Goudar 	bool		do_wakeup	= device_may_wakeup(&pdev->dev);
507933bb1f0SMajunath Goudar 	int		ret;
508933bb1f0SMajunath Goudar 
509efe7daf2SSergei Shtylyov 
510efe7daf2SSergei Shtylyov 	if (time_before(jiffies, ohci->next_statechange))
511efe7daf2SSergei Shtylyov 		msleep(5);
512efe7daf2SSergei Shtylyov 	ohci->next_statechange = jiffies;
513efe7daf2SSergei Shtylyov 
514933bb1f0SMajunath Goudar 	ret = ohci_suspend(hcd, do_wakeup);
515933bb1f0SMajunath Goudar 	if (ret)
516933bb1f0SMajunath Goudar 		return ret;
517933bb1f0SMajunath Goudar 
518c7a4f9f3SAxel Haslam 	ohci_da8xx_disable(hcd);
519efe7daf2SSergei Shtylyov 	hcd->state = HC_STATE_SUSPENDED;
520933bb1f0SMajunath Goudar 
521933bb1f0SMajunath Goudar 	return ret;
522efe7daf2SSergei Shtylyov }
523efe7daf2SSergei Shtylyov 
524efe7daf2SSergei Shtylyov static int ohci_da8xx_resume(struct platform_device *dev)
525efe7daf2SSergei Shtylyov {
526efe7daf2SSergei Shtylyov 	struct usb_hcd	*hcd	= platform_get_drvdata(dev);
527efe7daf2SSergei Shtylyov 	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
5286110c425SDavid Lechner 	int ret;
529efe7daf2SSergei Shtylyov 
530efe7daf2SSergei Shtylyov 	if (time_before(jiffies, ohci->next_statechange))
531efe7daf2SSergei Shtylyov 		msleep(5);
532efe7daf2SSergei Shtylyov 	ohci->next_statechange = jiffies;
533efe7daf2SSergei Shtylyov 
534c7a4f9f3SAxel Haslam 	ret = ohci_da8xx_enable(hcd);
5356110c425SDavid Lechner 	if (ret)
5366110c425SDavid Lechner 		return ret;
5376110c425SDavid Lechner 
538640308b7SAxel Haslam 	ohci_resume(hcd, false);
5396110c425SDavid Lechner 
540efe7daf2SSergei Shtylyov 	return 0;
541efe7daf2SSergei Shtylyov }
542efe7daf2SSergei Shtylyov #endif
543efe7daf2SSergei Shtylyov 
5446c21caa3SManjunath Goudar static const struct ohci_driver_overrides da8xx_overrides __initconst = {
5456c21caa3SManjunath Goudar 	.reset		 = ohci_da8xx_reset,
546c7a4f9f3SAxel Haslam 	.extra_priv_size = sizeof(struct da8xx_ohci_hcd),
5476c21caa3SManjunath Goudar };
5486c21caa3SManjunath Goudar 
549efe7daf2SSergei Shtylyov /*
550efe7daf2SSergei Shtylyov  * Driver definition to register with platform structure.
551efe7daf2SSergei Shtylyov  */
552efe7daf2SSergei Shtylyov static struct platform_driver ohci_hcd_da8xx_driver = {
5536c21caa3SManjunath Goudar 	.probe		= ohci_da8xx_probe,
5546c21caa3SManjunath Goudar 	.remove		= ohci_da8xx_remove,
555efe7daf2SSergei Shtylyov 	.shutdown 	= usb_hcd_platform_shutdown,
556efe7daf2SSergei Shtylyov #ifdef	CONFIG_PM
557efe7daf2SSergei Shtylyov 	.suspend	= ohci_da8xx_suspend,
558efe7daf2SSergei Shtylyov 	.resume		= ohci_da8xx_resume,
559efe7daf2SSergei Shtylyov #endif
560efe7daf2SSergei Shtylyov 	.driver		= {
5616c21caa3SManjunath Goudar 		.name	= DRV_NAME,
562190534f6SAxel Haslam 		.of_match_table = of_match_ptr(da8xx_ohci_ids),
563efe7daf2SSergei Shtylyov 	},
564efe7daf2SSergei Shtylyov };
565ab59ac01SJan Luebbe 
5666c21caa3SManjunath Goudar static int __init ohci_da8xx_init(void)
5676c21caa3SManjunath Goudar {
5686c21caa3SManjunath Goudar 
5696c21caa3SManjunath Goudar 	if (usb_disabled())
5706c21caa3SManjunath Goudar 		return -ENODEV;
5716c21caa3SManjunath Goudar 
5726c21caa3SManjunath Goudar 	pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
5736c21caa3SManjunath Goudar 	ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
5746c21caa3SManjunath Goudar 
5756c21caa3SManjunath Goudar 	/*
5766c21caa3SManjunath Goudar 	 * The Davinci da8xx HW has some unusual quirks, which require
5776c21caa3SManjunath Goudar 	 * da8xx-specific workarounds. We override certain hc_driver
5786c21caa3SManjunath Goudar 	 * functions here to achieve that. We explicitly do not enhance
5796c21caa3SManjunath Goudar 	 * ohci_driver_overrides to allow this more easily, since this
5806c21caa3SManjunath Goudar 	 * is an unusual case, and we don't want to encourage others to
5816c21caa3SManjunath Goudar 	 * override these functions by making it too easy.
5826c21caa3SManjunath Goudar 	 */
5836c21caa3SManjunath Goudar 
5846c21caa3SManjunath Goudar 	orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
5856c21caa3SManjunath Goudar 	orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
5866c21caa3SManjunath Goudar 
5876c21caa3SManjunath Goudar 	ohci_da8xx_hc_driver.hub_status_data     = ohci_da8xx_hub_status_data;
5886c21caa3SManjunath Goudar 	ohci_da8xx_hc_driver.hub_control         = ohci_da8xx_hub_control;
5896c21caa3SManjunath Goudar 
5906c21caa3SManjunath Goudar 	return platform_driver_register(&ohci_hcd_da8xx_driver);
5916c21caa3SManjunath Goudar }
5926c21caa3SManjunath Goudar module_init(ohci_da8xx_init);
5936c21caa3SManjunath Goudar 
5946c21caa3SManjunath Goudar static void __exit ohci_da8xx_exit(void)
5956c21caa3SManjunath Goudar {
5966c21caa3SManjunath Goudar 	platform_driver_unregister(&ohci_hcd_da8xx_driver);
5976c21caa3SManjunath Goudar }
5986c21caa3SManjunath Goudar module_exit(ohci_da8xx_exit);
5996c21caa3SManjunath Goudar MODULE_DESCRIPTION(DRIVER_DESC);
6006c21caa3SManjunath Goudar MODULE_LICENSE("GPL");
6016c21caa3SManjunath Goudar MODULE_ALIAS("platform:" DRV_NAME);
602