blob: 9249039055a01ec89a6e04908cc0b28b6f409f93 [file] [log] [blame]
Alexey Brodkinfee331f2015-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 Chotard155d9f62017-07-18 11:57:12 +02008#include <clk.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03009#include <dm.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +020010#include <dm/ofnode.h>
Patrice Chotard28df1cf2017-07-18 11:57:14 +020011#include <generic-phy.h>
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020012#include <reset.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +030013#include "ohci.h"
14
15#if !defined(CONFIG_USB_OHCI_NEW)
16# error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW"
17#endif
18
19struct generic_ohci {
20 ohci_t ohci;
Patrice Chotard155d9f62017-07-18 11:57:12 +020021 struct clk *clocks; /* clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020022 struct reset_ctl *resets; /* reset list */
Patrice Chotard28df1cf2017-07-18 11:57:14 +020023 struct phy phy;
Patrice Chotard155d9f62017-07-18 11:57:12 +020024 int clock_count; /* number of clock in clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020025 int reset_count; /* number of reset in reset list */
Alexey Brodkinfee331f2015-12-14 17:18:50 +030026};
27
28static int ohci_usb_probe(struct udevice *dev)
29{
Simon Glassa821c4a2017-05-17 17:18:05 -060030 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Patrice Chotard155d9f62017-07-18 11:57:12 +020031 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020032 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkinfee331f2015-12-14 17:18:50 +030033
Patrice Chotard155d9f62017-07-18 11:57:12 +020034 err = 0;
35 priv->clock_count = 0;
36 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
37 if (clock_nb > 0) {
38 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
39 GFP_KERNEL);
40 if (!priv->clocks)
41 return -ENOMEM;
42
43 for (i = 0; i < clock_nb; i++) {
44 err = clk_get_by_index(dev, i, &priv->clocks[i]);
45 if (err < 0)
46 break;
47
48 err = clk_enable(&priv->clocks[i]);
49 if (err) {
50 error("failed to enable clock %d\n", i);
51 clk_free(&priv->clocks[i]);
52 goto clk_err;
53 }
54 priv->clock_count++;
55 }
56 } else if (clock_nb != -ENOENT) {
57 error("failed to get clock phandle(%d)\n", clock_nb);
58 return clock_nb;
59 }
60
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020061 priv->reset_count = 0;
62 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
63 if (reset_nb > 0) {
64 priv->resets = devm_kcalloc(dev, reset_nb,
65 sizeof(struct reset_ctl),
66 GFP_KERNEL);
67 if (!priv->resets)
68 return -ENOMEM;
69
70 for (i = 0; i < reset_nb; i++) {
71 err = reset_get_by_index(dev, i, &priv->resets[i]);
72 if (err < 0)
73 break;
74
75 err = reset_deassert(&priv->resets[i]);
76 if (err) {
77 error("failed to deassert reset %d\n", i);
78 reset_free(&priv->resets[i]);
79 goto reset_err;
80 }
81 priv->reset_count++;
82 }
83 } else if (reset_nb != -ENOENT) {
84 error("failed to get reset phandle(%d)\n", reset_nb);
85 goto clk_err;
86 }
87
Patrice Chotard28df1cf2017-07-18 11:57:14 +020088 err = generic_phy_get_by_index(dev, 0, &priv->phy);
89 if (err) {
90 if (err != -ENOENT) {
91 error("failed to get usb phy\n");
92 goto reset_err;
93 }
94 }
95
96 err = generic_phy_init(&priv->phy);
97 if (err) {
98 error("failed to init usb phy\n");
99 goto reset_err;
100 }
101
Patrice Chotard155d9f62017-07-18 11:57:12 +0200102 err = ohci_register(dev, regs);
103 if (err)
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200104 goto phy_err;
Patrice Chotard155d9f62017-07-18 11:57:12 +0200105
106 return 0;
107
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200108phy_err:
109 if (generic_phy_valid(&priv->phy)) {
110 ret = generic_phy_exit(&priv->phy);
111 if (ret)
112 error("failed to release phy\n");
113 }
114
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200115reset_err:
116 ret = reset_release_all(priv->resets, priv->reset_count);
117 if (ret)
118 error("failed to assert all resets\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200119clk_err:
120 ret = clk_release_all(priv->clocks, priv->clock_count);
121 if (ret)
122 error("failed to disable all clocks\n");
123
124 return err;
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300125}
126
127static int ohci_usb_remove(struct udevice *dev)
128{
Patrice Chotard155d9f62017-07-18 11:57:12 +0200129 struct generic_ohci *priv = dev_get_priv(dev);
130 int ret;
131
132 ret = ohci_deregister(dev);
133 if (ret)
134 return ret;
135
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200136 if (generic_phy_valid(&priv->phy)) {
137 ret = generic_phy_exit(&priv->phy);
138 if (ret)
139 return ret;
140 }
141
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200142 ret = reset_release_all(priv->resets, priv->reset_count);
143 if (ret)
144 return ret;
145
Patrice Chotard155d9f62017-07-18 11:57:12 +0200146 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300147}
148
149static const struct udevice_id ohci_usb_ids[] = {
150 { .compatible = "generic-ohci" },
151 { }
152};
153
154U_BOOT_DRIVER(ohci_generic) = {
155 .name = "ohci_generic",
156 .id = UCLASS_USB,
157 .of_match = ohci_usb_ids,
158 .probe = ohci_usb_probe,
159 .remove = ohci_usb_remove,
160 .ops = &ohci_usb_ops,
161 .priv_auto_alloc_size = sizeof(struct generic_ohci),
162 .flags = DM_FLAG_ALLOC_PRIV_DMA,
163};