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/gpio/consumer.h>
13 #include <linux/io.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/phy/phy.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <linux/unaligned.h>
25
26 #include "ohci.h"
27
28 #define DRIVER_DESC "DA8XX"
29 #define DRV_NAME "ohci-da8xx"
30
31 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
32
33 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
34 u16 wValue, u16 wIndex, char *buf, u16 wLength);
35 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
36
37 struct da8xx_ohci_hcd {
38 struct usb_hcd *hcd;
39 struct clk *usb11_clk;
40 struct phy *usb11_phy;
41 struct regulator *vbus_reg;
42 struct notifier_block nb;
43 struct gpio_desc *oc_gpio;
44 };
45
46 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
47
48 /* Over-current indicator change bitmask */
49 static volatile u16 ocic_mask;
50
ohci_da8xx_enable(struct usb_hcd * hcd)51 static int ohci_da8xx_enable(struct usb_hcd *hcd)
52 {
53 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
54 int ret;
55
56 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
57 if (ret)
58 return ret;
59
60 ret = phy_init(da8xx_ohci->usb11_phy);
61 if (ret)
62 goto err_phy_init;
63
64 ret = phy_power_on(da8xx_ohci->usb11_phy);
65 if (ret)
66 goto err_phy_power_on;
67
68 return 0;
69
70 err_phy_power_on:
71 phy_exit(da8xx_ohci->usb11_phy);
72 err_phy_init:
73 clk_disable_unprepare(da8xx_ohci->usb11_clk);
74
75 return ret;
76 }
77
ohci_da8xx_disable(struct usb_hcd * hcd)78 static void ohci_da8xx_disable(struct usb_hcd *hcd)
79 {
80 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
81
82 phy_power_off(da8xx_ohci->usb11_phy);
83 phy_exit(da8xx_ohci->usb11_phy);
84 clk_disable_unprepare(da8xx_ohci->usb11_clk);
85 }
86
ohci_da8xx_set_power(struct usb_hcd * hcd,int on)87 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
88 {
89 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
90 struct device *dev = hcd->self.controller;
91 int ret;
92
93 if (!da8xx_ohci->vbus_reg)
94 return 0;
95
96 if (on) {
97 ret = regulator_enable(da8xx_ohci->vbus_reg);
98 if (ret) {
99 dev_err(dev, "Failed to enable regulator: %d\n", ret);
100 return ret;
101 }
102 } else {
103 ret = regulator_disable(da8xx_ohci->vbus_reg);
104 if (ret) {
105 dev_err(dev, "Failed to disable regulator: %d\n", ret);
106 return ret;
107 }
108 }
109
110 return 0;
111 }
112
ohci_da8xx_get_power(struct usb_hcd * hcd)113 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
114 {
115 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
116
117 if (da8xx_ohci->vbus_reg)
118 return regulator_is_enabled(da8xx_ohci->vbus_reg);
119
120 return 1;
121 }
122
ohci_da8xx_get_oci(struct usb_hcd * hcd)123 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
124 {
125 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
126 unsigned int flags;
127 int ret;
128
129 if (da8xx_ohci->oc_gpio)
130 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
131
132 if (!da8xx_ohci->vbus_reg)
133 return 0;
134
135 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
136 if (ret)
137 return ret;
138
139 if (flags & REGULATOR_ERROR_OVER_CURRENT)
140 return 1;
141
142 return 0;
143 }
144
ohci_da8xx_has_set_power(struct usb_hcd * hcd)145 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
146 {
147 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
148
149 if (da8xx_ohci->vbus_reg)
150 return 1;
151
152 return 0;
153 }
154
ohci_da8xx_has_oci(struct usb_hcd * hcd)155 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
156 {
157 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
158
159 if (da8xx_ohci->oc_gpio)
160 return 1;
161
162 if (da8xx_ohci->vbus_reg)
163 return 1;
164
165 return 0;
166 }
167
ohci_da8xx_regulator_event(struct notifier_block * nb,unsigned long event,void * data)168 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
169 unsigned long event, void *data)
170 {
171 struct da8xx_ohci_hcd *da8xx_ohci =
172 container_of(nb, struct da8xx_ohci_hcd, nb);
173
174 if (event & REGULATOR_EVENT_OVER_CURRENT) {
175 ocic_mask |= 1 << 1;
176 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
177 }
178
179 return 0;
180 }
181
ohci_da8xx_oc_thread(int irq,void * data)182 static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
183 {
184 struct da8xx_ohci_hcd *da8xx_ohci = data;
185 struct device *dev = da8xx_ohci->hcd->self.controller;
186 int ret;
187
188 if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio) &&
189 da8xx_ohci->vbus_reg) {
190 ret = regulator_disable(da8xx_ohci->vbus_reg);
191 if (ret)
192 dev_err(dev, "Failed to disable regulator: %d\n", ret);
193 }
194
195 return IRQ_HANDLED;
196 }
197
ohci_da8xx_register_notify(struct usb_hcd * hcd)198 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
199 {
200 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
201 struct device *dev = hcd->self.controller;
202 int ret = 0;
203
204 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
205 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
206 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
207 &da8xx_ohci->nb);
208 }
209
210 if (ret)
211 dev_err(dev, "Failed to register notifier: %d\n", ret);
212
213 return ret;
214 }
215
ohci_da8xx_reset(struct usb_hcd * hcd)216 static int ohci_da8xx_reset(struct usb_hcd *hcd)
217 {
218 struct device *dev = hcd->self.controller;
219 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
220 int result;
221 u32 rh_a;
222
223 dev_dbg(dev, "starting USB controller\n");
224
225 result = ohci_da8xx_enable(hcd);
226 if (result < 0)
227 return result;
228
229 /*
230 * DA8xx only have 1 port connected to the pins but the HC root hub
231 * register A reports 2 ports, thus we'll have to override it...
232 */
233 ohci->num_ports = 1;
234
235 result = ohci_setup(hcd);
236 if (result < 0) {
237 ohci_da8xx_disable(hcd);
238 return result;
239 }
240
241 /*
242 * Since we're providing a board-specific root hub port power control
243 * and over-current reporting, we have to override the HC root hub A
244 * register's default value, so that ohci_hub_control() could return
245 * the correct hub descriptor...
246 */
247 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
248 if (ohci_da8xx_has_set_power(hcd)) {
249 rh_a &= ~RH_A_NPS;
250 rh_a |= RH_A_PSM;
251 }
252 if (ohci_da8xx_has_oci(hcd)) {
253 rh_a &= ~RH_A_NOCP;
254 rh_a |= RH_A_OCPM;
255 }
256 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
257
258 return result;
259 }
260
261 /*
262 * Update the status data from the hub with the over-current indicator change.
263 */
ohci_da8xx_hub_status_data(struct usb_hcd * hcd,char * buf)264 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
265 {
266 int length = orig_ohci_hub_status_data(hcd, buf);
267
268 /* See if we have OCIC bit set on port 1 */
269 if (ocic_mask & (1 << 1)) {
270 dev_dbg(hcd->self.controller, "over-current indicator change "
271 "on port 1\n");
272
273 if (!length)
274 length = 1;
275
276 buf[0] |= 1 << 1;
277 }
278 return length;
279 }
280
281 /*
282 * Look at the control requests to the root hub and see if we need to override.
283 */
ohci_da8xx_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)284 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
285 u16 wIndex, char *buf, u16 wLength)
286 {
287 struct device *dev = hcd->self.controller;
288 int temp;
289
290 switch (typeReq) {
291 case GetPortStatus:
292 /* Check the port number */
293 if (wIndex != 1)
294 break;
295
296 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
297
298 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
299
300 /* The port power status (PPS) bit defaults to 1 */
301 if (!ohci_da8xx_get_power(hcd))
302 temp &= ~RH_PS_PPS;
303
304 /* The port over-current indicator (POCI) bit is always 0 */
305 if (ohci_da8xx_get_oci(hcd) > 0)
306 temp |= RH_PS_POCI;
307
308 /* The over-current indicator change (OCIC) bit is 0 too */
309 if (ocic_mask & (1 << wIndex))
310 temp |= RH_PS_OCIC;
311
312 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
313 return 0;
314 case SetPortFeature:
315 temp = 1;
316 goto check_port;
317 case ClearPortFeature:
318 temp = 0;
319
320 check_port:
321 /* Check the port number */
322 if (wIndex != 1)
323 break;
324
325 switch (wValue) {
326 case USB_PORT_FEAT_POWER:
327 dev_dbg(dev, "%sPortFeature(%u): %s\n",
328 temp ? "Set" : "Clear", wIndex, "POWER");
329
330 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
331 case USB_PORT_FEAT_C_OVER_CURRENT:
332 dev_dbg(dev, "%sPortFeature(%u): %s\n",
333 temp ? "Set" : "Clear", wIndex,
334 "C_OVER_CURRENT");
335
336 if (temp)
337 ocic_mask |= 1 << wIndex;
338 else
339 ocic_mask &= ~(1 << wIndex);
340 return 0;
341 }
342 }
343
344 return orig_ohci_hub_control(hcd, typeReq, wValue,
345 wIndex, buf, wLength);
346 }
347
348 /*-------------------------------------------------------------------------*/
349 #ifdef CONFIG_OF
350 static const struct of_device_id da8xx_ohci_ids[] = {
351 { .compatible = "ti,da830-ohci" },
352 { }
353 };
354 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
355 #endif
356
ohci_da8xx_probe(struct platform_device * pdev)357 static int ohci_da8xx_probe(struct platform_device *pdev)
358 {
359 struct da8xx_ohci_hcd *da8xx_ohci;
360 struct device *dev = &pdev->dev;
361 int error, hcd_irq, oc_irq;
362 struct usb_hcd *hcd;
363 struct resource *mem;
364
365 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
366 if (!hcd)
367 return -ENOMEM;
368
369 da8xx_ohci = to_da8xx_ohci(hcd);
370 da8xx_ohci->hcd = hcd;
371
372 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
373 if (IS_ERR(da8xx_ohci->usb11_clk)) {
374 error = PTR_ERR(da8xx_ohci->usb11_clk);
375 if (error != -EPROBE_DEFER)
376 dev_err(dev, "Failed to get clock.\n");
377 goto err;
378 }
379
380 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
381 if (IS_ERR(da8xx_ohci->usb11_phy)) {
382 error = PTR_ERR(da8xx_ohci->usb11_phy);
383 if (error != -EPROBE_DEFER)
384 dev_err(dev, "Failed to get phy.\n");
385 goto err;
386 }
387
388 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
389 if (IS_ERR(da8xx_ohci->vbus_reg)) {
390 error = PTR_ERR(da8xx_ohci->vbus_reg);
391 if (error == -ENODEV) {
392 da8xx_ohci->vbus_reg = NULL;
393 } else if (error == -EPROBE_DEFER) {
394 goto err;
395 } else {
396 dev_err(dev, "Failed to get regulator\n");
397 goto err;
398 }
399 }
400
401 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
402 if (IS_ERR(da8xx_ohci->oc_gpio)) {
403 error = PTR_ERR(da8xx_ohci->oc_gpio);
404 goto err;
405 }
406
407 if (da8xx_ohci->oc_gpio) {
408 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
409 if (oc_irq < 0) {
410 error = oc_irq;
411 goto err;
412 }
413
414 error = devm_request_threaded_irq(dev, oc_irq, NULL,
415 ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
416 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
417 "OHCI over-current indicator", da8xx_ohci);
418 if (error)
419 goto err;
420 }
421
422 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
423 if (IS_ERR(hcd->regs)) {
424 error = PTR_ERR(hcd->regs);
425 goto err;
426 }
427 hcd->rsrc_start = mem->start;
428 hcd->rsrc_len = resource_size(mem);
429
430 hcd_irq = platform_get_irq(pdev, 0);
431 if (hcd_irq < 0) {
432 error = -ENODEV;
433 goto err;
434 }
435
436 error = usb_add_hcd(hcd, hcd_irq, 0);
437 if (error)
438 goto err;
439
440 device_wakeup_enable(hcd->self.controller);
441
442 error = ohci_da8xx_register_notify(hcd);
443 if (error)
444 goto err_remove_hcd;
445
446 return 0;
447
448 err_remove_hcd:
449 usb_remove_hcd(hcd);
450 err:
451 usb_put_hcd(hcd);
452 return error;
453 }
454
ohci_da8xx_remove(struct platform_device * pdev)455 static void ohci_da8xx_remove(struct platform_device *pdev)
456 {
457 struct usb_hcd *hcd = platform_get_drvdata(pdev);
458
459 usb_remove_hcd(hcd);
460 usb_put_hcd(hcd);
461 }
462
463 #ifdef CONFIG_PM
ohci_da8xx_suspend(struct platform_device * pdev,pm_message_t message)464 static int ohci_da8xx_suspend(struct platform_device *pdev,
465 pm_message_t message)
466 {
467 struct usb_hcd *hcd = platform_get_drvdata(pdev);
468 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
469 bool do_wakeup = device_may_wakeup(&pdev->dev);
470 int ret;
471
472
473 if (time_before(jiffies, ohci->next_statechange))
474 msleep(5);
475 ohci->next_statechange = jiffies;
476
477 ret = ohci_suspend(hcd, do_wakeup);
478 if (ret)
479 return ret;
480
481 ohci_da8xx_disable(hcd);
482 hcd->state = HC_STATE_SUSPENDED;
483
484 return ret;
485 }
486
ohci_da8xx_resume(struct platform_device * dev)487 static int ohci_da8xx_resume(struct platform_device *dev)
488 {
489 struct usb_hcd *hcd = platform_get_drvdata(dev);
490 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
491 int ret;
492
493 if (time_before(jiffies, ohci->next_statechange))
494 msleep(5);
495 ohci->next_statechange = jiffies;
496
497 ret = ohci_da8xx_enable(hcd);
498 if (ret)
499 return ret;
500
501 ohci_resume(hcd, false);
502
503 return 0;
504 }
505 #endif
506
507 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
508 .reset = ohci_da8xx_reset,
509 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
510 };
511
512 /*
513 * Driver definition to register with platform structure.
514 */
515 static struct platform_driver ohci_hcd_da8xx_driver = {
516 .probe = ohci_da8xx_probe,
517 .remove = ohci_da8xx_remove,
518 .shutdown = usb_hcd_platform_shutdown,
519 #ifdef CONFIG_PM
520 .suspend = ohci_da8xx_suspend,
521 .resume = ohci_da8xx_resume,
522 #endif
523 .driver = {
524 .name = DRV_NAME,
525 .of_match_table = of_match_ptr(da8xx_ohci_ids),
526 },
527 };
528
ohci_da8xx_init(void)529 static int __init ohci_da8xx_init(void)
530 {
531
532 if (usb_disabled())
533 return -ENODEV;
534
535 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
536
537 /*
538 * The Davinci da8xx HW has some unusual quirks, which require
539 * da8xx-specific workarounds. We override certain hc_driver
540 * functions here to achieve that. We explicitly do not enhance
541 * ohci_driver_overrides to allow this more easily, since this
542 * is an unusual case, and we don't want to encourage others to
543 * override these functions by making it too easy.
544 */
545
546 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
547 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
548
549 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
550 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
551
552 return platform_driver_register(&ohci_hcd_da8xx_driver);
553 }
554 module_init(ohci_da8xx_init);
555
ohci_da8xx_exit(void)556 static void __exit ohci_da8xx_exit(void)
557 {
558 platform_driver_unregister(&ohci_hcd_da8xx_driver);
559 }
560 module_exit(ohci_da8xx_exit);
561 MODULE_DESCRIPTION(DRIVER_DESC);
562 MODULE_LICENSE("GPL");
563 MODULE_ALIAS("platform:" DRV_NAME);
564