Loading...
1/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * TI DA8xx (OMAP-L1x) Bus Glue
5 *
6 * Derived from: ohci-omap.c and ohci-s3c2410.c
7 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 */
13
14#include <linux/interrupt.h>
15#include <linux/jiffies.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18
19#include <mach/da8xx.h>
20#include <mach/usb.h>
21
22#ifndef CONFIG_ARCH_DAVINCI_DA8XX
23#error "This file is DA8xx bus glue. Define CONFIG_ARCH_DAVINCI_DA8XX."
24#endif
25
26#define CFGCHIP2 DA8XX_SYSCFG0_VIRT(DA8XX_CFGCHIP2_REG)
27
28static struct clk *usb11_clk;
29static struct clk *usb20_clk;
30
31/* Over-current indicator change bitmask */
32static volatile u16 ocic_mask;
33
34static void ohci_da8xx_clock(int on)
35{
36 u32 cfgchip2;
37
38 cfgchip2 = __raw_readl(CFGCHIP2);
39 if (on) {
40 clk_enable(usb11_clk);
41
42 /*
43 * If USB 1.1 reference clock is sourced from USB 2.0 PHY, we
44 * need to enable the USB 2.0 module clocking, start its PHY,
45 * and not allow it to stop the clock during USB 2.0 suspend.
46 */
47 if (!(cfgchip2 & CFGCHIP2_USB1PHYCLKMUX)) {
48 clk_enable(usb20_clk);
49
50 cfgchip2 &= ~(CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN);
51 cfgchip2 |= CFGCHIP2_PHY_PLLON;
52 __raw_writel(cfgchip2, CFGCHIP2);
53
54 pr_info("Waiting for USB PHY clock good...\n");
55 while (!(__raw_readl(CFGCHIP2) & CFGCHIP2_PHYCLKGD))
56 cpu_relax();
57 }
58
59 /* Enable USB 1.1 PHY */
60 cfgchip2 |= CFGCHIP2_USB1SUSPENDM;
61 } else {
62 clk_disable(usb11_clk);
63 if (!(cfgchip2 & CFGCHIP2_USB1PHYCLKMUX))
64 clk_disable(usb20_clk);
65
66 /* Disable USB 1.1 PHY */
67 cfgchip2 &= ~CFGCHIP2_USB1SUSPENDM;
68 }
69 __raw_writel(cfgchip2, CFGCHIP2);
70}
71
72/*
73 * Handle the port over-current indicator change.
74 */
75static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub *hub,
76 unsigned port)
77{
78 ocic_mask |= 1 << port;
79
80 /* Once over-current is detected, the port needs to be powered down */
81 if (hub->get_oci(port) > 0)
82 hub->set_power(port, 0);
83}
84
85static int ohci_da8xx_init(struct usb_hcd *hcd)
86{
87 struct device *dev = hcd->self.controller;
88 struct da8xx_ohci_root_hub *hub = dev->platform_data;
89 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
90 int result;
91 u32 rh_a;
92
93 dev_dbg(dev, "starting USB controller\n");
94
95 ohci_da8xx_clock(1);
96
97 /*
98 * DA8xx only have 1 port connected to the pins but the HC root hub
99 * register A reports 2 ports, thus we'll have to override it...
100 */
101 ohci->num_ports = 1;
102
103 result = ohci_init(ohci);
104 if (result < 0)
105 return result;
106
107 /*
108 * Since we're providing a board-specific root hub port power control
109 * and over-current reporting, we have to override the HC root hub A
110 * register's default value, so that ohci_hub_control() could return
111 * the correct hub descriptor...
112 */
113 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
114 if (hub->set_power) {
115 rh_a &= ~RH_A_NPS;
116 rh_a |= RH_A_PSM;
117 }
118 if (hub->get_oci) {
119 rh_a &= ~RH_A_NOCP;
120 rh_a |= RH_A_OCPM;
121 }
122 rh_a &= ~RH_A_POTPGT;
123 rh_a |= hub->potpgt << 24;
124 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
125
126 return result;
127}
128
129static void ohci_da8xx_stop(struct usb_hcd *hcd)
130{
131 ohci_stop(hcd);
132 ohci_da8xx_clock(0);
133}
134
135static int ohci_da8xx_start(struct usb_hcd *hcd)
136{
137 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
138 int result;
139
140 result = ohci_run(ohci);
141 if (result < 0)
142 ohci_da8xx_stop(hcd);
143
144 return result;
145}
146
147/*
148 * Update the status data from the hub with the over-current indicator change.
149 */
150static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
151{
152 int length = ohci_hub_status_data(hcd, buf);
153
154 /* See if we have OCIC bit set on port 1 */
155 if (ocic_mask & (1 << 1)) {
156 dev_dbg(hcd->self.controller, "over-current indicator change "
157 "on port 1\n");
158
159 if (!length)
160 length = 1;
161
162 buf[0] |= 1 << 1;
163 }
164 return length;
165}
166
167/*
168 * Look at the control requests to the root hub and see if we need to override.
169 */
170static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
171 u16 wIndex, char *buf, u16 wLength)
172{
173 struct device *dev = hcd->self.controller;
174 struct da8xx_ohci_root_hub *hub = dev->platform_data;
175 int temp;
176
177 switch (typeReq) {
178 case GetPortStatus:
179 /* Check the port number */
180 if (wIndex != 1)
181 break;
182
183 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
184
185 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
186
187 /* The port power status (PPS) bit defaults to 1 */
188 if (hub->get_power && hub->get_power(wIndex) == 0)
189 temp &= ~RH_PS_PPS;
190
191 /* The port over-current indicator (POCI) bit is always 0 */
192 if (hub->get_oci && hub->get_oci(wIndex) > 0)
193 temp |= RH_PS_POCI;
194
195 /* The over-current indicator change (OCIC) bit is 0 too */
196 if (ocic_mask & (1 << wIndex))
197 temp |= RH_PS_OCIC;
198
199 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
200 return 0;
201 case SetPortFeature:
202 temp = 1;
203 goto check_port;
204 case ClearPortFeature:
205 temp = 0;
206
207check_port:
208 /* Check the port number */
209 if (wIndex != 1)
210 break;
211
212 switch (wValue) {
213 case USB_PORT_FEAT_POWER:
214 dev_dbg(dev, "%sPortFeature(%u): %s\n",
215 temp ? "Set" : "Clear", wIndex, "POWER");
216
217 if (!hub->set_power)
218 return -EPIPE;
219
220 return hub->set_power(wIndex, temp) ? -EPIPE : 0;
221 case USB_PORT_FEAT_C_OVER_CURRENT:
222 dev_dbg(dev, "%sPortFeature(%u): %s\n",
223 temp ? "Set" : "Clear", wIndex,
224 "C_OVER_CURRENT");
225
226 if (temp)
227 ocic_mask |= 1 << wIndex;
228 else
229 ocic_mask &= ~(1 << wIndex);
230 return 0;
231 }
232 }
233
234 return ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
235}
236
237static const struct hc_driver ohci_da8xx_hc_driver = {
238 .description = hcd_name,
239 .product_desc = "DA8xx OHCI",
240 .hcd_priv_size = sizeof(struct ohci_hcd),
241
242 /*
243 * generic hardware linkage
244 */
245 .irq = ohci_irq,
246 .flags = HCD_USB11 | HCD_MEMORY,
247
248 /*
249 * basic lifecycle operations
250 */
251 .reset = ohci_da8xx_init,
252 .start = ohci_da8xx_start,
253 .stop = ohci_da8xx_stop,
254 .shutdown = ohci_shutdown,
255
256 /*
257 * managing i/o requests and associated device resources
258 */
259 .urb_enqueue = ohci_urb_enqueue,
260 .urb_dequeue = ohci_urb_dequeue,
261 .endpoint_disable = ohci_endpoint_disable,
262
263 /*
264 * scheduling support
265 */
266 .get_frame_number = ohci_get_frame,
267
268 /*
269 * root hub support
270 */
271 .hub_status_data = ohci_da8xx_hub_status_data,
272 .hub_control = ohci_da8xx_hub_control,
273
274#ifdef CONFIG_PM
275 .bus_suspend = ohci_bus_suspend,
276 .bus_resume = ohci_bus_resume,
277#endif
278 .start_port_reset = ohci_start_port_reset,
279};
280
281/*-------------------------------------------------------------------------*/
282
283
284/**
285 * usb_hcd_da8xx_probe - initialize DA8xx-based HCDs
286 * Context: !in_interrupt()
287 *
288 * Allocates basic resources for this USB host controller, and
289 * then invokes the start() method for the HCD associated with it
290 * through the hotplug entry's driver_data.
291 */
292static int usb_hcd_da8xx_probe(const struct hc_driver *driver,
293 struct platform_device *pdev)
294{
295 struct da8xx_ohci_root_hub *hub = pdev->dev.platform_data;
296 struct usb_hcd *hcd;
297 struct resource *mem;
298 int error, irq;
299
300 if (hub == NULL)
301 return -ENODEV;
302
303 usb11_clk = clk_get(&pdev->dev, "usb11");
304 if (IS_ERR(usb11_clk))
305 return PTR_ERR(usb11_clk);
306
307 usb20_clk = clk_get(&pdev->dev, "usb20");
308 if (IS_ERR(usb20_clk)) {
309 error = PTR_ERR(usb20_clk);
310 goto err0;
311 }
312
313 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
314 if (!hcd) {
315 error = -ENOMEM;
316 goto err1;
317 }
318
319 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
320 if (!mem) {
321 error = -ENODEV;
322 goto err2;
323 }
324 hcd->rsrc_start = mem->start;
325 hcd->rsrc_len = resource_size(mem);
326
327 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
328 dev_dbg(&pdev->dev, "request_mem_region failed\n");
329 error = -EBUSY;
330 goto err2;
331 }
332
333 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
334 if (!hcd->regs) {
335 dev_err(&pdev->dev, "ioremap failed\n");
336 error = -ENOMEM;
337 goto err3;
338 }
339
340 ohci_hcd_init(hcd_to_ohci(hcd));
341
342 irq = platform_get_irq(pdev, 0);
343 if (irq < 0) {
344 error = -ENODEV;
345 goto err4;
346 }
347 error = usb_add_hcd(hcd, irq, IRQF_DISABLED);
348 if (error)
349 goto err4;
350
351 if (hub->ocic_notify) {
352 error = hub->ocic_notify(ohci_da8xx_ocic_handler);
353 if (!error)
354 return 0;
355 }
356
357 usb_remove_hcd(hcd);
358err4:
359 iounmap(hcd->regs);
360err3:
361 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
362err2:
363 usb_put_hcd(hcd);
364err1:
365 clk_put(usb20_clk);
366err0:
367 clk_put(usb11_clk);
368 return error;
369}
370
371/**
372 * usb_hcd_da8xx_remove - shutdown processing for DA8xx-based HCDs
373 * @dev: USB Host Controller being removed
374 * Context: !in_interrupt()
375 *
376 * Reverses the effect of usb_hcd_da8xx_probe(), first invoking
377 * the HCD's stop() method. It is always called from a thread
378 * context, normally "rmmod", "apmd", or something similar.
379 */
380static inline void
381usb_hcd_da8xx_remove(struct usb_hcd *hcd, struct platform_device *pdev)
382{
383 struct da8xx_ohci_root_hub *hub = pdev->dev.platform_data;
384
385 hub->ocic_notify(NULL);
386 usb_remove_hcd(hcd);
387 iounmap(hcd->regs);
388 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
389 usb_put_hcd(hcd);
390 clk_put(usb20_clk);
391 clk_put(usb11_clk);
392}
393
394static int ohci_hcd_da8xx_drv_probe(struct platform_device *dev)
395{
396 return usb_hcd_da8xx_probe(&ohci_da8xx_hc_driver, dev);
397}
398
399static int ohci_hcd_da8xx_drv_remove(struct platform_device *dev)
400{
401 struct usb_hcd *hcd = platform_get_drvdata(dev);
402
403 usb_hcd_da8xx_remove(hcd, dev);
404 platform_set_drvdata(dev, NULL);
405
406 return 0;
407}
408
409#ifdef CONFIG_PM
410static int ohci_da8xx_suspend(struct platform_device *dev, pm_message_t message)
411{
412 struct usb_hcd *hcd = platform_get_drvdata(dev);
413 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
414
415 if (time_before(jiffies, ohci->next_statechange))
416 msleep(5);
417 ohci->next_statechange = jiffies;
418
419 ohci_da8xx_clock(0);
420 hcd->state = HC_STATE_SUSPENDED;
421 dev->dev.power.power_state = PMSG_SUSPEND;
422 return 0;
423}
424
425static int ohci_da8xx_resume(struct platform_device *dev)
426{
427 struct usb_hcd *hcd = platform_get_drvdata(dev);
428 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
429
430 if (time_before(jiffies, ohci->next_statechange))
431 msleep(5);
432 ohci->next_statechange = jiffies;
433
434 ohci_da8xx_clock(1);
435 dev->dev.power.power_state = PMSG_ON;
436 usb_hcd_resume_root_hub(hcd);
437 return 0;
438}
439#endif
440
441/*
442 * Driver definition to register with platform structure.
443 */
444static struct platform_driver ohci_hcd_da8xx_driver = {
445 .probe = ohci_hcd_da8xx_drv_probe,
446 .remove = ohci_hcd_da8xx_drv_remove,
447 .shutdown = usb_hcd_platform_shutdown,
448#ifdef CONFIG_PM
449 .suspend = ohci_da8xx_suspend,
450 .resume = ohci_da8xx_resume,
451#endif
452 .driver = {
453 .owner = THIS_MODULE,
454 .name = "ohci",
455 },
456};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * OHCI HCD (Host Controller Driver) for USB.
4 *
5 * TI DA8xx (OMAP-L1x) Bus Glue
6 *
7 * Derived from: ohci-omap.c and ohci-s3c2410.c
8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9 */
10
11#include <linux/clk.h>
12#include <linux/io.h>
13#include <linux/interrupt.h>
14#include <linux/jiffies.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/phy/phy.h>
19#include <linux/platform_data/usb-davinci.h>
20#include <linux/regulator/consumer.h>
21#include <linux/usb.h>
22#include <linux/usb/hcd.h>
23#include <asm/unaligned.h>
24
25#include "ohci.h"
26
27#define DRIVER_DESC "DA8XX"
28#define DRV_NAME "ohci-da8xx"
29
30static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
31
32static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
33 u16 wValue, u16 wIndex, char *buf, u16 wLength);
34static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
35
36struct da8xx_ohci_hcd {
37 struct usb_hcd *hcd;
38 struct clk *usb11_clk;
39 struct phy *usb11_phy;
40 struct regulator *vbus_reg;
41 struct notifier_block nb;
42 unsigned int reg_enabled;
43};
44
45#define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
46
47/* Over-current indicator change bitmask */
48static volatile u16 ocic_mask;
49
50static int ohci_da8xx_enable(struct usb_hcd *hcd)
51{
52 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
53 int ret;
54
55 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
56 if (ret)
57 return ret;
58
59 ret = phy_init(da8xx_ohci->usb11_phy);
60 if (ret)
61 goto err_phy_init;
62
63 ret = phy_power_on(da8xx_ohci->usb11_phy);
64 if (ret)
65 goto err_phy_power_on;
66
67 return 0;
68
69err_phy_power_on:
70 phy_exit(da8xx_ohci->usb11_phy);
71err_phy_init:
72 clk_disable_unprepare(da8xx_ohci->usb11_clk);
73
74 return ret;
75}
76
77static void ohci_da8xx_disable(struct usb_hcd *hcd)
78{
79 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
80
81 phy_power_off(da8xx_ohci->usb11_phy);
82 phy_exit(da8xx_ohci->usb11_phy);
83 clk_disable_unprepare(da8xx_ohci->usb11_clk);
84}
85
86static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
87{
88 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
89 struct device *dev = hcd->self.controller;
90 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
91 int ret;
92
93 if (hub && hub->set_power)
94 return hub->set_power(1, on);
95
96 if (!da8xx_ohci->vbus_reg)
97 return 0;
98
99 if (on && !da8xx_ohci->reg_enabled) {
100 ret = regulator_enable(da8xx_ohci->vbus_reg);
101 if (ret) {
102 dev_err(dev, "Failed to enable regulator: %d\n", ret);
103 return ret;
104 }
105 da8xx_ohci->reg_enabled = 1;
106
107 } else if (!on && da8xx_ohci->reg_enabled) {
108 ret = regulator_disable(da8xx_ohci->vbus_reg);
109 if (ret) {
110 dev_err(dev, "Failed to disable regulator: %d\n", ret);
111 return ret;
112 }
113 da8xx_ohci->reg_enabled = 0;
114 }
115
116 return 0;
117}
118
119static int ohci_da8xx_get_power(struct usb_hcd *hcd)
120{
121 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
122 struct device *dev = hcd->self.controller;
123 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
124
125 if (hub && hub->get_power)
126 return hub->get_power(1);
127
128 if (da8xx_ohci->vbus_reg)
129 return regulator_is_enabled(da8xx_ohci->vbus_reg);
130
131 return 1;
132}
133
134static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
135{
136 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
137 struct device *dev = hcd->self.controller;
138 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
139 unsigned int flags;
140 int ret;
141
142 if (hub && hub->get_oci)
143 return hub->get_oci(1);
144
145 if (!da8xx_ohci->vbus_reg)
146 return 0;
147
148 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
149 if (ret)
150 return ret;
151
152 if (flags & REGULATOR_ERROR_OVER_CURRENT)
153 return 1;
154
155 return 0;
156}
157
158static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
159{
160 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
161 struct device *dev = hcd->self.controller;
162 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
163
164 if (hub && hub->set_power)
165 return 1;
166
167 if (da8xx_ohci->vbus_reg)
168 return 1;
169
170 return 0;
171}
172
173static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
174{
175 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
176 struct device *dev = hcd->self.controller;
177 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
178
179 if (hub && hub->get_oci)
180 return 1;
181
182 if (da8xx_ohci->vbus_reg)
183 return 1;
184
185 return 0;
186}
187
188static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
189{
190 struct device *dev = hcd->self.controller;
191 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
192
193 if (hub && hub->potpgt)
194 return 1;
195
196 return 0;
197}
198
199/*
200 * Handle the port over-current indicator change.
201 */
202static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub *hub,
203 unsigned port)
204{
205 ocic_mask |= 1 << port;
206
207 /* Once over-current is detected, the port needs to be powered down */
208 if (hub->get_oci(port) > 0)
209 hub->set_power(port, 0);
210}
211
212static int ohci_da8xx_regulator_event(struct notifier_block *nb,
213 unsigned long event, void *data)
214{
215 struct da8xx_ohci_hcd *da8xx_ohci =
216 container_of(nb, struct da8xx_ohci_hcd, nb);
217
218 if (event & REGULATOR_EVENT_OVER_CURRENT) {
219 ocic_mask |= 1 << 1;
220 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
221 }
222
223 return 0;
224}
225
226static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
227{
228 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
229 struct device *dev = hcd->self.controller;
230 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
231 int ret = 0;
232
233 if (hub && hub->ocic_notify) {
234 ret = hub->ocic_notify(ohci_da8xx_ocic_handler);
235 } else if (da8xx_ohci->vbus_reg) {
236 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
237 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
238 &da8xx_ohci->nb);
239 }
240
241 if (ret)
242 dev_err(dev, "Failed to register notifier: %d\n", ret);
243
244 return ret;
245}
246
247static void ohci_da8xx_unregister_notify(struct usb_hcd *hcd)
248{
249 struct device *dev = hcd->self.controller;
250 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
251
252 if (hub && hub->ocic_notify)
253 hub->ocic_notify(NULL);
254}
255
256static int ohci_da8xx_reset(struct usb_hcd *hcd)
257{
258 struct device *dev = hcd->self.controller;
259 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
260 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
261 int result;
262 u32 rh_a;
263
264 dev_dbg(dev, "starting USB controller\n");
265
266 result = ohci_da8xx_enable(hcd);
267 if (result < 0)
268 return result;
269
270 /*
271 * DA8xx only have 1 port connected to the pins but the HC root hub
272 * register A reports 2 ports, thus we'll have to override it...
273 */
274 ohci->num_ports = 1;
275
276 result = ohci_setup(hcd);
277 if (result < 0) {
278 ohci_da8xx_disable(hcd);
279 return result;
280 }
281
282 /*
283 * Since we're providing a board-specific root hub port power control
284 * and over-current reporting, we have to override the HC root hub A
285 * register's default value, so that ohci_hub_control() could return
286 * the correct hub descriptor...
287 */
288 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
289 if (ohci_da8xx_has_set_power(hcd)) {
290 rh_a &= ~RH_A_NPS;
291 rh_a |= RH_A_PSM;
292 }
293 if (ohci_da8xx_has_oci(hcd)) {
294 rh_a &= ~RH_A_NOCP;
295 rh_a |= RH_A_OCPM;
296 }
297 if (ohci_da8xx_has_potpgt(hcd)) {
298 rh_a &= ~RH_A_POTPGT;
299 rh_a |= hub->potpgt << 24;
300 }
301 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
302
303 return result;
304}
305
306/*
307 * Update the status data from the hub with the over-current indicator change.
308 */
309static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
310{
311 int length = orig_ohci_hub_status_data(hcd, buf);
312
313 /* See if we have OCIC bit set on port 1 */
314 if (ocic_mask & (1 << 1)) {
315 dev_dbg(hcd->self.controller, "over-current indicator change "
316 "on port 1\n");
317
318 if (!length)
319 length = 1;
320
321 buf[0] |= 1 << 1;
322 }
323 return length;
324}
325
326/*
327 * Look at the control requests to the root hub and see if we need to override.
328 */
329static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
330 u16 wIndex, char *buf, u16 wLength)
331{
332 struct device *dev = hcd->self.controller;
333 int temp;
334
335 switch (typeReq) {
336 case GetPortStatus:
337 /* Check the port number */
338 if (wIndex != 1)
339 break;
340
341 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
342
343 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
344
345 /* The port power status (PPS) bit defaults to 1 */
346 if (!ohci_da8xx_get_power(hcd))
347 temp &= ~RH_PS_PPS;
348
349 /* The port over-current indicator (POCI) bit is always 0 */
350 if (ohci_da8xx_get_oci(hcd) > 0)
351 temp |= RH_PS_POCI;
352
353 /* The over-current indicator change (OCIC) bit is 0 too */
354 if (ocic_mask & (1 << wIndex))
355 temp |= RH_PS_OCIC;
356
357 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
358 return 0;
359 case SetPortFeature:
360 temp = 1;
361 goto check_port;
362 case ClearPortFeature:
363 temp = 0;
364
365check_port:
366 /* Check the port number */
367 if (wIndex != 1)
368 break;
369
370 switch (wValue) {
371 case USB_PORT_FEAT_POWER:
372 dev_dbg(dev, "%sPortFeature(%u): %s\n",
373 temp ? "Set" : "Clear", wIndex, "POWER");
374
375 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
376 case USB_PORT_FEAT_C_OVER_CURRENT:
377 dev_dbg(dev, "%sPortFeature(%u): %s\n",
378 temp ? "Set" : "Clear", wIndex,
379 "C_OVER_CURRENT");
380
381 if (temp)
382 ocic_mask |= 1 << wIndex;
383 else
384 ocic_mask &= ~(1 << wIndex);
385 return 0;
386 }
387 }
388
389 return orig_ohci_hub_control(hcd, typeReq, wValue,
390 wIndex, buf, wLength);
391}
392
393/*-------------------------------------------------------------------------*/
394#ifdef CONFIG_OF
395static const struct of_device_id da8xx_ohci_ids[] = {
396 { .compatible = "ti,da830-ohci" },
397 { }
398};
399MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
400#endif
401
402static int ohci_da8xx_probe(struct platform_device *pdev)
403{
404 struct da8xx_ohci_hcd *da8xx_ohci;
405 struct usb_hcd *hcd;
406 struct resource *mem;
407 int error, irq;
408 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, &pdev->dev,
409 dev_name(&pdev->dev));
410 if (!hcd)
411 return -ENOMEM;
412
413 da8xx_ohci = to_da8xx_ohci(hcd);
414 da8xx_ohci->hcd = hcd;
415
416 da8xx_ohci->usb11_clk = devm_clk_get(&pdev->dev, NULL);
417 if (IS_ERR(da8xx_ohci->usb11_clk)) {
418 error = PTR_ERR(da8xx_ohci->usb11_clk);
419 if (error != -EPROBE_DEFER)
420 dev_err(&pdev->dev, "Failed to get clock.\n");
421 goto err;
422 }
423
424 da8xx_ohci->usb11_phy = devm_phy_get(&pdev->dev, "usb-phy");
425 if (IS_ERR(da8xx_ohci->usb11_phy)) {
426 error = PTR_ERR(da8xx_ohci->usb11_phy);
427 if (error != -EPROBE_DEFER)
428 dev_err(&pdev->dev, "Failed to get phy.\n");
429 goto err;
430 }
431
432 da8xx_ohci->vbus_reg = devm_regulator_get_optional(&pdev->dev, "vbus");
433 if (IS_ERR(da8xx_ohci->vbus_reg)) {
434 error = PTR_ERR(da8xx_ohci->vbus_reg);
435 if (error == -ENODEV) {
436 da8xx_ohci->vbus_reg = NULL;
437 } else if (error == -EPROBE_DEFER) {
438 goto err;
439 } else {
440 dev_err(&pdev->dev, "Failed to get regulator\n");
441 goto err;
442 }
443 }
444
445 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 hcd->regs = devm_ioremap_resource(&pdev->dev, mem);
447 if (IS_ERR(hcd->regs)) {
448 error = PTR_ERR(hcd->regs);
449 goto err;
450 }
451 hcd->rsrc_start = mem->start;
452 hcd->rsrc_len = resource_size(mem);
453
454 irq = platform_get_irq(pdev, 0);
455 if (irq < 0) {
456 error = -ENODEV;
457 goto err;
458 }
459
460 error = usb_add_hcd(hcd, irq, 0);
461 if (error)
462 goto err;
463
464 device_wakeup_enable(hcd->self.controller);
465
466 error = ohci_da8xx_register_notify(hcd);
467 if (error)
468 goto err_remove_hcd;
469
470 return 0;
471
472err_remove_hcd:
473 usb_remove_hcd(hcd);
474err:
475 usb_put_hcd(hcd);
476 return error;
477}
478
479static int ohci_da8xx_remove(struct platform_device *pdev)
480{
481 struct usb_hcd *hcd = platform_get_drvdata(pdev);
482
483 ohci_da8xx_unregister_notify(hcd);
484 usb_remove_hcd(hcd);
485 usb_put_hcd(hcd);
486
487 return 0;
488}
489
490#ifdef CONFIG_PM
491static int ohci_da8xx_suspend(struct platform_device *pdev,
492 pm_message_t message)
493{
494 struct usb_hcd *hcd = platform_get_drvdata(pdev);
495 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
496 bool do_wakeup = device_may_wakeup(&pdev->dev);
497 int ret;
498
499
500 if (time_before(jiffies, ohci->next_statechange))
501 msleep(5);
502 ohci->next_statechange = jiffies;
503
504 ret = ohci_suspend(hcd, do_wakeup);
505 if (ret)
506 return ret;
507
508 ohci_da8xx_disable(hcd);
509 hcd->state = HC_STATE_SUSPENDED;
510
511 return ret;
512}
513
514static int ohci_da8xx_resume(struct platform_device *dev)
515{
516 struct usb_hcd *hcd = platform_get_drvdata(dev);
517 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
518 int ret;
519
520 if (time_before(jiffies, ohci->next_statechange))
521 msleep(5);
522 ohci->next_statechange = jiffies;
523
524 ret = ohci_da8xx_enable(hcd);
525 if (ret)
526 return ret;
527
528 ohci_resume(hcd, false);
529
530 return 0;
531}
532#endif
533
534static const struct ohci_driver_overrides da8xx_overrides __initconst = {
535 .reset = ohci_da8xx_reset,
536 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
537};
538
539/*
540 * Driver definition to register with platform structure.
541 */
542static struct platform_driver ohci_hcd_da8xx_driver = {
543 .probe = ohci_da8xx_probe,
544 .remove = ohci_da8xx_remove,
545 .shutdown = usb_hcd_platform_shutdown,
546#ifdef CONFIG_PM
547 .suspend = ohci_da8xx_suspend,
548 .resume = ohci_da8xx_resume,
549#endif
550 .driver = {
551 .name = DRV_NAME,
552 .of_match_table = of_match_ptr(da8xx_ohci_ids),
553 },
554};
555
556static int __init ohci_da8xx_init(void)
557{
558
559 if (usb_disabled())
560 return -ENODEV;
561
562 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
563 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
564
565 /*
566 * The Davinci da8xx HW has some unusual quirks, which require
567 * da8xx-specific workarounds. We override certain hc_driver
568 * functions here to achieve that. We explicitly do not enhance
569 * ohci_driver_overrides to allow this more easily, since this
570 * is an unusual case, and we don't want to encourage others to
571 * override these functions by making it too easy.
572 */
573
574 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
575 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
576
577 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
578 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
579
580 return platform_driver_register(&ohci_hcd_da8xx_driver);
581}
582module_init(ohci_da8xx_init);
583
584static void __exit ohci_da8xx_exit(void)
585{
586 platform_driver_unregister(&ohci_hcd_da8xx_driver);
587}
588module_exit(ohci_da8xx_exit);
589MODULE_DESCRIPTION(DRIVER_DESC);
590MODULE_LICENSE("GPL");
591MODULE_ALIAS("platform:" DRV_NAME);