blob: f5b27ccf31d68d0d0264ca0083f8ec32f6615c8b [file] [log] [blame]
Alexey Brodkin83fd3122015-12-14 17:18:50 +03001/*
2 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Patrice Chotard37a197a2017-07-18 11:57:12 +02008#include <clk.h>
Alexey Brodkin83fd3122015-12-14 17:18:50 +03009#include <dm.h>
Patrice Chotard37a197a2017-07-18 11:57:12 +020010#include <dm/ofnode.h>
Alexey Brodkin83fd3122015-12-14 17:18:50 +030011#include "ohci.h"
12
13#if !defined(CONFIG_USB_OHCI_NEW)
14# error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW"
15#endif
16
17struct generic_ohci {
18 ohci_t ohci;
Patrice Chotard37a197a2017-07-18 11:57:12 +020019 struct clk *clocks; /* clock list */
20 int clock_count; /* number of clock in clock list */
Alexey Brodkin83fd3122015-12-14 17:18:50 +030021};
22
23static int ohci_usb_probe(struct udevice *dev)
24{
Simon Glassba1dea42017-05-17 17:18:05 -060025 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Patrice Chotard37a197a2017-07-18 11:57:12 +020026 struct generic_ohci *priv = dev_get_priv(dev);
27 int i, err, ret, clock_nb;
Alexey Brodkin83fd3122015-12-14 17:18:50 +030028
Patrice Chotard37a197a2017-07-18 11:57:12 +020029 err = 0;
30 priv->clock_count = 0;
31 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
32 if (clock_nb > 0) {
33 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
34 GFP_KERNEL);
35 if (!priv->clocks)
36 return -ENOMEM;
37
38 for (i = 0; i < clock_nb; i++) {
39 err = clk_get_by_index(dev, i, &priv->clocks[i]);
40 if (err < 0)
41 break;
42
43 err = clk_enable(&priv->clocks[i]);
44 if (err) {
45 error("failed to enable clock %d\n", i);
46 clk_free(&priv->clocks[i]);
47 goto clk_err;
48 }
49 priv->clock_count++;
50 }
51 } else if (clock_nb != -ENOENT) {
52 error("failed to get clock phandle(%d)\n", clock_nb);
53 return clock_nb;
54 }
55
56 err = ohci_register(dev, regs);
57 if (err)
58 goto clk_err;
59
60 return 0;
61
62clk_err:
63 ret = clk_release_all(priv->clocks, priv->clock_count);
64 if (ret)
65 error("failed to disable all clocks\n");
66
67 return err;
Alexey Brodkin83fd3122015-12-14 17:18:50 +030068}
69
70static int ohci_usb_remove(struct udevice *dev)
71{
Patrice Chotard37a197a2017-07-18 11:57:12 +020072 struct generic_ohci *priv = dev_get_priv(dev);
73 int ret;
74
75 ret = ohci_deregister(dev);
76 if (ret)
77 return ret;
78
79 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkin83fd3122015-12-14 17:18:50 +030080}
81
82static const struct udevice_id ohci_usb_ids[] = {
83 { .compatible = "generic-ohci" },
84 { }
85};
86
87U_BOOT_DRIVER(ohci_generic) = {
88 .name = "ohci_generic",
89 .id = UCLASS_USB,
90 .of_match = ohci_usb_ids,
91 .probe = ohci_usb_probe,
92 .remove = ohci_usb_remove,
93 .ops = &ohci_usb_ops,
94 .priv_auto_alloc_size = sizeof(struct generic_ohci),
95 .flags = DM_FLAG_ALLOC_PRIV_DMA,
96};