1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-1.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * OHCI HCD (Host Controller Driver) for USB.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2004 SAN People (Pty) Ltd.
6*4882a593Smuzhiyun * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * AT91 Bus Glue
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Based on fragments of 2.4 driver by Rick Bronson.
11*4882a593Smuzhiyun * Based on ohci-omap.c
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This file is licenced under the GPL.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/clk.h>
17*4882a593Smuzhiyun #include <linux/dma-mapping.h>
18*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
19*4882a593Smuzhiyun #include <linux/of_platform.h>
20*4882a593Smuzhiyun #include <linux/platform_device.h>
21*4882a593Smuzhiyun #include <linux/platform_data/atmel.h>
22*4882a593Smuzhiyun #include <linux/io.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
26*4882a593Smuzhiyun #include <linux/regmap.h>
27*4882a593Smuzhiyun #include <linux/usb.h>
28*4882a593Smuzhiyun #include <linux/usb/hcd.h>
29*4882a593Smuzhiyun #include <soc/at91/atmel-sfr.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include "ohci.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
34*4882a593Smuzhiyun #define at91_for_each_port(index) \
35*4882a593Smuzhiyun for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* interface, function and usb clocks; sometimes also an AHB clock */
38*4882a593Smuzhiyun #define hcd_to_ohci_at91_priv(h) \
39*4882a593Smuzhiyun ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define AT91_MAX_USBH_PORTS 3
42*4882a593Smuzhiyun struct at91_usbh_data {
43*4882a593Smuzhiyun struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
44*4882a593Smuzhiyun struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
45*4882a593Smuzhiyun u8 ports; /* number of ports on root hub */
46*4882a593Smuzhiyun u8 overcurrent_supported;
47*4882a593Smuzhiyun u8 overcurrent_status[AT91_MAX_USBH_PORTS];
48*4882a593Smuzhiyun u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct ohci_at91_priv {
52*4882a593Smuzhiyun struct clk *iclk;
53*4882a593Smuzhiyun struct clk *fclk;
54*4882a593Smuzhiyun struct clk *hclk;
55*4882a593Smuzhiyun bool clocked;
56*4882a593Smuzhiyun bool wakeup; /* Saved wake-up state for resume */
57*4882a593Smuzhiyun struct regmap *sfr_regmap;
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun /* interface and function clocks; sometimes also an AHB clock */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define DRIVER_DESC "OHCI Atmel driver"
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun static const char hcd_name[] = "ohci-atmel";
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun static struct hc_driver __read_mostly ohci_at91_hc_driver;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
68*4882a593Smuzhiyun .extra_priv_size = sizeof(struct ohci_at91_priv),
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
72*4882a593Smuzhiyun
at91_start_clock(struct ohci_at91_priv * ohci_at91)73*4882a593Smuzhiyun static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun if (ohci_at91->clocked)
76*4882a593Smuzhiyun return;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun clk_set_rate(ohci_at91->fclk, 48000000);
79*4882a593Smuzhiyun clk_prepare_enable(ohci_at91->hclk);
80*4882a593Smuzhiyun clk_prepare_enable(ohci_at91->iclk);
81*4882a593Smuzhiyun clk_prepare_enable(ohci_at91->fclk);
82*4882a593Smuzhiyun ohci_at91->clocked = true;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
at91_stop_clock(struct ohci_at91_priv * ohci_at91)85*4882a593Smuzhiyun static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun if (!ohci_at91->clocked)
88*4882a593Smuzhiyun return;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun clk_disable_unprepare(ohci_at91->fclk);
91*4882a593Smuzhiyun clk_disable_unprepare(ohci_at91->iclk);
92*4882a593Smuzhiyun clk_disable_unprepare(ohci_at91->hclk);
93*4882a593Smuzhiyun ohci_at91->clocked = false;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
at91_start_hc(struct platform_device * pdev)96*4882a593Smuzhiyun static void at91_start_hc(struct platform_device *pdev)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(pdev);
99*4882a593Smuzhiyun struct ohci_regs __iomem *regs = hcd->regs;
100*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun dev_dbg(&pdev->dev, "start\n");
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * Start the USB clocks.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun at91_start_clock(ohci_at91);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * The USB host controller must remain in reset.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun writel(0, ®s->control);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
at91_stop_hc(struct platform_device * pdev)115*4882a593Smuzhiyun static void at91_stop_hc(struct platform_device *pdev)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(pdev);
118*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun dev_dbg(&pdev->dev, "stop\n");
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * Put the USB host controller into reset.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun usb_hcd_platform_shutdown(pdev);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Stop the USB clocks.
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun at91_stop_clock(ohci_at91);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
137*4882a593Smuzhiyun
at91_dt_syscon_sfr(void)138*4882a593Smuzhiyun static struct regmap *at91_dt_syscon_sfr(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun struct regmap *regmap;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
143*4882a593Smuzhiyun if (IS_ERR(regmap)) {
144*4882a593Smuzhiyun regmap = syscon_regmap_lookup_by_compatible("microchip,sam9x60-sfr");
145*4882a593Smuzhiyun if (IS_ERR(regmap))
146*4882a593Smuzhiyun regmap = NULL;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return regmap;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* configure so an HC device and id are always provided */
153*4882a593Smuzhiyun /* always called with process context; sleeping is OK */
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun * usb_hcd_at91_probe - initialize AT91-based HCDs
158*4882a593Smuzhiyun * Context: !in_interrupt()
159*4882a593Smuzhiyun *
160*4882a593Smuzhiyun * Allocates basic resources for this USB host controller, and
161*4882a593Smuzhiyun * then invokes the start() method for the HCD associated with it
162*4882a593Smuzhiyun * through the hotplug entry's driver_data.
163*4882a593Smuzhiyun */
usb_hcd_at91_probe(const struct hc_driver * driver,struct platform_device * pdev)164*4882a593Smuzhiyun static int usb_hcd_at91_probe(const struct hc_driver *driver,
165*4882a593Smuzhiyun struct platform_device *pdev)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun struct at91_usbh_data *board;
168*4882a593Smuzhiyun struct ohci_hcd *ohci;
169*4882a593Smuzhiyun int retval;
170*4882a593Smuzhiyun struct usb_hcd *hcd;
171*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91;
172*4882a593Smuzhiyun struct device *dev = &pdev->dev;
173*4882a593Smuzhiyun struct resource *res;
174*4882a593Smuzhiyun int irq;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
177*4882a593Smuzhiyun if (irq < 0) {
178*4882a593Smuzhiyun dev_dbg(dev, "hcd probe: missing irq resource\n");
179*4882a593Smuzhiyun return irq;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun hcd = usb_create_hcd(driver, dev, "at91");
183*4882a593Smuzhiyun if (!hcd)
184*4882a593Smuzhiyun return -ENOMEM;
185*4882a593Smuzhiyun ohci_at91 = hcd_to_ohci_at91_priv(hcd);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188*4882a593Smuzhiyun hcd->regs = devm_ioremap_resource(dev, res);
189*4882a593Smuzhiyun if (IS_ERR(hcd->regs)) {
190*4882a593Smuzhiyun retval = PTR_ERR(hcd->regs);
191*4882a593Smuzhiyun goto err;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun hcd->rsrc_start = res->start;
194*4882a593Smuzhiyun hcd->rsrc_len = resource_size(res);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
197*4882a593Smuzhiyun if (IS_ERR(ohci_at91->iclk)) {
198*4882a593Smuzhiyun dev_err(dev, "failed to get ohci_clk\n");
199*4882a593Smuzhiyun retval = PTR_ERR(ohci_at91->iclk);
200*4882a593Smuzhiyun goto err;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun ohci_at91->fclk = devm_clk_get(dev, "uhpck");
203*4882a593Smuzhiyun if (IS_ERR(ohci_at91->fclk)) {
204*4882a593Smuzhiyun dev_err(dev, "failed to get uhpck\n");
205*4882a593Smuzhiyun retval = PTR_ERR(ohci_at91->fclk);
206*4882a593Smuzhiyun goto err;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun ohci_at91->hclk = devm_clk_get(dev, "hclk");
209*4882a593Smuzhiyun if (IS_ERR(ohci_at91->hclk)) {
210*4882a593Smuzhiyun dev_err(dev, "failed to get hclk\n");
211*4882a593Smuzhiyun retval = PTR_ERR(ohci_at91->hclk);
212*4882a593Smuzhiyun goto err;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
216*4882a593Smuzhiyun if (!ohci_at91->sfr_regmap)
217*4882a593Smuzhiyun dev_dbg(dev, "failed to find sfr node\n");
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun board = hcd->self.controller->platform_data;
220*4882a593Smuzhiyun ohci = hcd_to_ohci(hcd);
221*4882a593Smuzhiyun ohci->num_ports = board->ports;
222*4882a593Smuzhiyun at91_start_hc(pdev);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun * The RemoteWakeupConnected bit has to be set explicitly
226*4882a593Smuzhiyun * before calling ohci_run. The reset value of this bit is 0.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun ohci->hc_control = OHCI_CTRL_RWC;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
231*4882a593Smuzhiyun if (retval == 0) {
232*4882a593Smuzhiyun device_wakeup_enable(hcd->self.controller);
233*4882a593Smuzhiyun return retval;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /* Error handling */
237*4882a593Smuzhiyun at91_stop_hc(pdev);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun err:
240*4882a593Smuzhiyun usb_put_hcd(hcd);
241*4882a593Smuzhiyun return retval;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* may be called with controller, bus, and devices active */
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
249*4882a593Smuzhiyun * Context: !in_interrupt()
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * Reverses the effect of usb_hcd_at91_probe(), first invoking
252*4882a593Smuzhiyun * the HCD's stop() method. It is always called from a thread
253*4882a593Smuzhiyun * context, "rmmod" or something similar.
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun */
usb_hcd_at91_remove(struct usb_hcd * hcd,struct platform_device * pdev)256*4882a593Smuzhiyun static void usb_hcd_at91_remove(struct usb_hcd *hcd,
257*4882a593Smuzhiyun struct platform_device *pdev)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun usb_remove_hcd(hcd);
260*4882a593Smuzhiyun at91_stop_hc(pdev);
261*4882a593Smuzhiyun usb_put_hcd(hcd);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
ohci_at91_usb_set_power(struct at91_usbh_data * pdata,int port,int enable)265*4882a593Smuzhiyun static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun if (!valid_port(port))
268*4882a593Smuzhiyun return;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun gpiod_set_value(pdata->vbus_pin[port], enable);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
ohci_at91_usb_get_power(struct at91_usbh_data * pdata,int port)273*4882a593Smuzhiyun static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun if (!valid_port(port))
276*4882a593Smuzhiyun return -EINVAL;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return gpiod_get_value(pdata->vbus_pin[port]);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /*
282*4882a593Smuzhiyun * Update the status data from the hub with the over-current indicator change.
283*4882a593Smuzhiyun */
ohci_at91_hub_status_data(struct usb_hcd * hcd,char * buf)284*4882a593Smuzhiyun static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
287*4882a593Smuzhiyun int length = ohci_hub_status_data(hcd, buf);
288*4882a593Smuzhiyun int port;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun at91_for_each_port(port) {
291*4882a593Smuzhiyun if (pdata->overcurrent_changed[port]) {
292*4882a593Smuzhiyun if (!length)
293*4882a593Smuzhiyun length = 1;
294*4882a593Smuzhiyun buf[0] |= 1 << (port + 1);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun return length;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
ohci_at91_port_suspend(struct regmap * regmap,u8 set)301*4882a593Smuzhiyun static int ohci_at91_port_suspend(struct regmap *regmap, u8 set)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun u32 regval;
304*4882a593Smuzhiyun int ret;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (!regmap)
307*4882a593Smuzhiyun return 0;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun ret = regmap_read(regmap, AT91_SFR_OHCIICR, ®val);
310*4882a593Smuzhiyun if (ret)
311*4882a593Smuzhiyun return ret;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun if (set)
314*4882a593Smuzhiyun regval |= AT91_OHCIICR_USB_SUSPEND;
315*4882a593Smuzhiyun else
316*4882a593Smuzhiyun regval &= ~AT91_OHCIICR_USB_SUSPEND;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun regmap_write(regmap, AT91_SFR_OHCIICR, regval);
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /*
324*4882a593Smuzhiyun * Look at the control requests to the root hub and see if we need to override.
325*4882a593Smuzhiyun */
ohci_at91_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)326*4882a593Smuzhiyun static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
327*4882a593Smuzhiyun u16 wIndex, char *buf, u16 wLength)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
330*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
331*4882a593Smuzhiyun struct usb_hub_descriptor *desc;
332*4882a593Smuzhiyun int ret = -EINVAL;
333*4882a593Smuzhiyun u32 *data = (u32 *)buf;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun dev_dbg(hcd->self.controller,
336*4882a593Smuzhiyun "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
337*4882a593Smuzhiyun hcd, typeReq, wValue, wIndex, buf, wLength);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun wIndex--;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun switch (typeReq) {
342*4882a593Smuzhiyun case SetPortFeature:
343*4882a593Smuzhiyun switch (wValue) {
344*4882a593Smuzhiyun case USB_PORT_FEAT_POWER:
345*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
346*4882a593Smuzhiyun if (valid_port(wIndex)) {
347*4882a593Smuzhiyun ohci_at91_usb_set_power(pdata, wIndex, 1);
348*4882a593Smuzhiyun ret = 0;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun goto out;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun case USB_PORT_FEAT_SUSPEND:
354*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
355*4882a593Smuzhiyun if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
356*4882a593Smuzhiyun ohci_at91_port_suspend(ohci_at91->sfr_regmap,
357*4882a593Smuzhiyun 1);
358*4882a593Smuzhiyun return 0;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun break;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun break;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun case ClearPortFeature:
365*4882a593Smuzhiyun switch (wValue) {
366*4882a593Smuzhiyun case USB_PORT_FEAT_C_OVER_CURRENT:
367*4882a593Smuzhiyun dev_dbg(hcd->self.controller,
368*4882a593Smuzhiyun "ClearPortFeature: C_OVER_CURRENT\n");
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun if (valid_port(wIndex)) {
371*4882a593Smuzhiyun pdata->overcurrent_changed[wIndex] = 0;
372*4882a593Smuzhiyun pdata->overcurrent_status[wIndex] = 0;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun goto out;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun case USB_PORT_FEAT_OVER_CURRENT:
378*4882a593Smuzhiyun dev_dbg(hcd->self.controller,
379*4882a593Smuzhiyun "ClearPortFeature: OVER_CURRENT\n");
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun if (valid_port(wIndex))
382*4882a593Smuzhiyun pdata->overcurrent_status[wIndex] = 0;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun goto out;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun case USB_PORT_FEAT_POWER:
387*4882a593Smuzhiyun dev_dbg(hcd->self.controller,
388*4882a593Smuzhiyun "ClearPortFeature: POWER\n");
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (valid_port(wIndex)) {
391*4882a593Smuzhiyun ohci_at91_usb_set_power(pdata, wIndex, 0);
392*4882a593Smuzhiyun return 0;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun break;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun case USB_PORT_FEAT_SUSPEND:
397*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
398*4882a593Smuzhiyun if (valid_port(wIndex) && ohci_at91->sfr_regmap) {
399*4882a593Smuzhiyun ohci_at91_port_suspend(ohci_at91->sfr_regmap,
400*4882a593Smuzhiyun 0);
401*4882a593Smuzhiyun return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun break;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun break;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
409*4882a593Smuzhiyun if (ret)
410*4882a593Smuzhiyun goto out;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun switch (typeReq) {
413*4882a593Smuzhiyun case GetHubDescriptor:
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* update the hub's descriptor */
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun desc = (struct usb_hub_descriptor *)buf;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
420*4882a593Smuzhiyun desc->wHubCharacteristics);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* remove the old configurations for power-switching, and
423*4882a593Smuzhiyun * over-current protection, and insert our new configuration
424*4882a593Smuzhiyun */
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
427*4882a593Smuzhiyun desc->wHubCharacteristics |=
428*4882a593Smuzhiyun cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (pdata->overcurrent_supported) {
431*4882a593Smuzhiyun desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
432*4882a593Smuzhiyun desc->wHubCharacteristics |=
433*4882a593Smuzhiyun cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
437*4882a593Smuzhiyun desc->wHubCharacteristics);
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun return ret;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun case GetPortStatus:
442*4882a593Smuzhiyun /* check port status */
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun if (valid_port(wIndex)) {
447*4882a593Smuzhiyun if (!ohci_at91_usb_get_power(pdata, wIndex))
448*4882a593Smuzhiyun *data &= ~cpu_to_le32(RH_PS_PPS);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (pdata->overcurrent_changed[wIndex])
451*4882a593Smuzhiyun *data |= cpu_to_le32(RH_PS_OCIC);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (pdata->overcurrent_status[wIndex])
454*4882a593Smuzhiyun *data |= cpu_to_le32(RH_PS_POCI);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun out:
459*4882a593Smuzhiyun return ret;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
463*4882a593Smuzhiyun
ohci_hcd_at91_overcurrent_irq(int irq,void * data)464*4882a593Smuzhiyun static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun struct platform_device *pdev = data;
467*4882a593Smuzhiyun struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
468*4882a593Smuzhiyun int val, port;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun /* From the GPIO notifying the over-current situation, find
471*4882a593Smuzhiyun * out the corresponding port */
472*4882a593Smuzhiyun at91_for_each_port(port) {
473*4882a593Smuzhiyun if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
474*4882a593Smuzhiyun break;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun if (port == AT91_MAX_USBH_PORTS) {
478*4882a593Smuzhiyun dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
479*4882a593Smuzhiyun return IRQ_HANDLED;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun val = gpiod_get_value(pdata->overcurrent_pin[port]);
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /* When notified of an over-current situation, disable power
485*4882a593Smuzhiyun on the corresponding port, and mark this port in
486*4882a593Smuzhiyun over-current. */
487*4882a593Smuzhiyun if (!val) {
488*4882a593Smuzhiyun ohci_at91_usb_set_power(pdata, port, 0);
489*4882a593Smuzhiyun pdata->overcurrent_status[port] = 1;
490*4882a593Smuzhiyun pdata->overcurrent_changed[port] = 1;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun dev_dbg(& pdev->dev, "overcurrent situation %s\n",
494*4882a593Smuzhiyun val ? "exited" : "notified");
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun return IRQ_HANDLED;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun static const struct of_device_id at91_ohci_dt_ids[] = {
500*4882a593Smuzhiyun { .compatible = "atmel,at91rm9200-ohci" },
501*4882a593Smuzhiyun { /* sentinel */ }
502*4882a593Smuzhiyun };
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
507*4882a593Smuzhiyun
ohci_hcd_at91_drv_probe(struct platform_device * pdev)508*4882a593Smuzhiyun static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun struct device_node *np = pdev->dev.of_node;
511*4882a593Smuzhiyun struct at91_usbh_data *pdata;
512*4882a593Smuzhiyun int i;
513*4882a593Smuzhiyun int ret;
514*4882a593Smuzhiyun int err;
515*4882a593Smuzhiyun u32 ports;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /* Right now device-tree probed devices don't get dma_mask set.
518*4882a593Smuzhiyun * Since shared usb code relies on it, set it here for now.
519*4882a593Smuzhiyun * Once we have dma capability bindings this can go away.
520*4882a593Smuzhiyun */
521*4882a593Smuzhiyun ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
522*4882a593Smuzhiyun if (ret)
523*4882a593Smuzhiyun return ret;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
526*4882a593Smuzhiyun if (!pdata)
527*4882a593Smuzhiyun return -ENOMEM;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun pdev->dev.platform_data = pdata;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun if (!of_property_read_u32(np, "num-ports", &ports))
532*4882a593Smuzhiyun pdata->ports = ports;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun at91_for_each_port(i) {
535*4882a593Smuzhiyun if (i >= pdata->ports)
536*4882a593Smuzhiyun break;
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun pdata->vbus_pin[i] =
539*4882a593Smuzhiyun devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
540*4882a593Smuzhiyun i, GPIOD_OUT_HIGH);
541*4882a593Smuzhiyun if (IS_ERR(pdata->vbus_pin[i])) {
542*4882a593Smuzhiyun err = PTR_ERR(pdata->vbus_pin[i]);
543*4882a593Smuzhiyun dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
544*4882a593Smuzhiyun continue;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun at91_for_each_port(i) {
549*4882a593Smuzhiyun if (i >= pdata->ports)
550*4882a593Smuzhiyun break;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun pdata->overcurrent_pin[i] =
553*4882a593Smuzhiyun devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
554*4882a593Smuzhiyun i, GPIOD_IN);
555*4882a593Smuzhiyun if (!pdata->overcurrent_pin[i])
556*4882a593Smuzhiyun continue;
557*4882a593Smuzhiyun if (IS_ERR(pdata->overcurrent_pin[i])) {
558*4882a593Smuzhiyun err = PTR_ERR(pdata->overcurrent_pin[i]);
559*4882a593Smuzhiyun dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
560*4882a593Smuzhiyun continue;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev,
564*4882a593Smuzhiyun gpiod_to_irq(pdata->overcurrent_pin[i]),
565*4882a593Smuzhiyun ohci_hcd_at91_overcurrent_irq,
566*4882a593Smuzhiyun IRQF_SHARED,
567*4882a593Smuzhiyun "ohci_overcurrent", pdev);
568*4882a593Smuzhiyun if (ret)
569*4882a593Smuzhiyun dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, 1);
573*4882a593Smuzhiyun return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
ohci_hcd_at91_drv_remove(struct platform_device * pdev)576*4882a593Smuzhiyun static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
579*4882a593Smuzhiyun int i;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun if (pdata) {
582*4882a593Smuzhiyun at91_for_each_port(i)
583*4882a593Smuzhiyun ohci_at91_usb_set_power(pdata, i, 0);
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, 0);
587*4882a593Smuzhiyun usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
588*4882a593Smuzhiyun return 0;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun static int __maybe_unused
ohci_hcd_at91_drv_suspend(struct device * dev)592*4882a593Smuzhiyun ohci_hcd_at91_drv_suspend(struct device *dev)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
595*4882a593Smuzhiyun struct ohci_hcd *ohci = hcd_to_ohci(hcd);
596*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
597*4882a593Smuzhiyun int ret;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /*
600*4882a593Smuzhiyun * Disable wakeup if we are going to sleep with slow clock mode
601*4882a593Smuzhiyun * enabled.
602*4882a593Smuzhiyun */
603*4882a593Smuzhiyun ohci_at91->wakeup = device_may_wakeup(dev)
604*4882a593Smuzhiyun && !at91_suspend_entering_slow_clock();
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun if (ohci_at91->wakeup)
607*4882a593Smuzhiyun enable_irq_wake(hcd->irq);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun ret = ohci_suspend(hcd, ohci_at91->wakeup);
610*4882a593Smuzhiyun if (ret) {
611*4882a593Smuzhiyun if (ohci_at91->wakeup)
612*4882a593Smuzhiyun disable_irq_wake(hcd->irq);
613*4882a593Smuzhiyun return ret;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun /*
616*4882a593Smuzhiyun * The integrated transceivers seem unable to notice disconnect,
617*4882a593Smuzhiyun * reconnect, or wakeup without the 48 MHz clock active. so for
618*4882a593Smuzhiyun * correctness, always discard connection state (using reset).
619*4882a593Smuzhiyun *
620*4882a593Smuzhiyun * REVISIT: some boards will be able to turn VBUS off...
621*4882a593Smuzhiyun */
622*4882a593Smuzhiyun if (!ohci_at91->wakeup) {
623*4882a593Smuzhiyun ohci->rh_state = OHCI_RH_HALTED;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun /* flush the writes */
626*4882a593Smuzhiyun (void) ohci_readl (ohci, &ohci->regs->control);
627*4882a593Smuzhiyun msleep(1);
628*4882a593Smuzhiyun ohci_at91_port_suspend(ohci_at91->sfr_regmap, 1);
629*4882a593Smuzhiyun at91_stop_clock(ohci_at91);
630*4882a593Smuzhiyun } else {
631*4882a593Smuzhiyun ohci_at91_port_suspend(ohci_at91->sfr_regmap, 1);
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun return ret;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun static int __maybe_unused
ohci_hcd_at91_drv_resume(struct device * dev)638*4882a593Smuzhiyun ohci_hcd_at91_drv_resume(struct device *dev)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
641*4882a593Smuzhiyun struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun ohci_at91_port_suspend(ohci_at91->sfr_regmap, 0);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun if (ohci_at91->wakeup)
646*4882a593Smuzhiyun disable_irq_wake(hcd->irq);
647*4882a593Smuzhiyun else
648*4882a593Smuzhiyun at91_start_clock(ohci_at91);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun ohci_resume(hcd, false);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun return 0;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
656*4882a593Smuzhiyun ohci_hcd_at91_drv_resume);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun static struct platform_driver ohci_hcd_at91_driver = {
659*4882a593Smuzhiyun .probe = ohci_hcd_at91_drv_probe,
660*4882a593Smuzhiyun .remove = ohci_hcd_at91_drv_remove,
661*4882a593Smuzhiyun .shutdown = usb_hcd_platform_shutdown,
662*4882a593Smuzhiyun .driver = {
663*4882a593Smuzhiyun .name = "at91_ohci",
664*4882a593Smuzhiyun .pm = &ohci_hcd_at91_pm_ops,
665*4882a593Smuzhiyun .of_match_table = at91_ohci_dt_ids,
666*4882a593Smuzhiyun },
667*4882a593Smuzhiyun };
668*4882a593Smuzhiyun
ohci_at91_init(void)669*4882a593Smuzhiyun static int __init ohci_at91_init(void)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun if (usb_disabled())
672*4882a593Smuzhiyun return -ENODEV;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun pr_info("%s: " DRIVER_DESC "\n", hcd_name);
675*4882a593Smuzhiyun ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * The Atmel HW has some unusual quirks, which require Atmel-specific
679*4882a593Smuzhiyun * workarounds. We override certain hc_driver functions here to
680*4882a593Smuzhiyun * achieve that. We explicitly do not enhance ohci_driver_overrides to
681*4882a593Smuzhiyun * allow this more easily, since this is an unusual case, and we don't
682*4882a593Smuzhiyun * want to encourage others to override these functions by making it
683*4882a593Smuzhiyun * too easy.
684*4882a593Smuzhiyun */
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
687*4882a593Smuzhiyun ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun return platform_driver_register(&ohci_hcd_at91_driver);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun module_init(ohci_at91_init);
692*4882a593Smuzhiyun
ohci_at91_cleanup(void)693*4882a593Smuzhiyun static void __exit ohci_at91_cleanup(void)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun platform_driver_unregister(&ohci_hcd_at91_driver);
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun module_exit(ohci_at91_cleanup);
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
700*4882a593Smuzhiyun MODULE_LICENSE("GPL");
701*4882a593Smuzhiyun MODULE_ALIAS("platform:at91_ohci");
702