blob: 04d5fdb2a8676796b3f5d67deefdcdb90612d420 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkinfee331f2015-12-14 17:18:50 +03002/*
3 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03004 */
5
6#include <common.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +02007#include <clk.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +03008#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070010#include <dm/devres.h>
Patrice Chotard155d9f62017-07-18 11:57:12 +020011#include <dm/ofnode.h>
Patrice Chotard28df1cf2017-07-18 11:57:14 +020012#include <generic-phy.h>
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020013#include <reset.h>
Alexey Brodkinfee331f2015-12-14 17:18:50 +030014#include "ohci.h"
15
16#if !defined(CONFIG_USB_OHCI_NEW)
17# error "Generic OHCI driver requires CONFIG_USB_OHCI_NEW"
18#endif
19
20struct generic_ohci {
21 ohci_t ohci;
Patrice Chotard155d9f62017-07-18 11:57:12 +020022 struct clk *clocks; /* clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020023 struct reset_ctl *resets; /* reset list */
Patrice Chotard28df1cf2017-07-18 11:57:14 +020024 struct phy phy;
Patrice Chotard155d9f62017-07-18 11:57:12 +020025 int clock_count; /* number of clock in clock list */
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020026 int reset_count; /* number of reset in reset list */
Alexey Brodkinfee331f2015-12-14 17:18:50 +030027};
28
Patrice Chotardcab4d482018-03-14 17:48:57 +010029static int ohci_setup_phy(struct udevice *dev, int index)
30{
31 struct generic_ohci *priv = dev_get_priv(dev);
32 int ret;
33
34 ret = generic_phy_get_by_index(dev, index, &priv->phy);
35 if (ret) {
36 if (ret != -ENOENT) {
37 dev_err(dev, "failed to get usb phy\n");
38 return ret;
39 }
40 } else {
41 ret = generic_phy_init(&priv->phy);
42 if (ret) {
43 dev_err(dev, "failed to init usb phy\n");
44 return ret;
45 }
46
47 ret = generic_phy_power_on(&priv->phy);
48 if (ret) {
49 dev_err(dev, "failed to power on usb phy\n");
50 return generic_phy_exit(&priv->phy);
51 }
52 }
53
54 return 0;
55}
56
57static int ohci_shutdown_phy(struct udevice *dev)
58{
59 struct generic_ohci *priv = dev_get_priv(dev);
60 int ret = 0;
61
62 if (generic_phy_valid(&priv->phy)) {
63 ret = generic_phy_power_off(&priv->phy);
64 if (ret) {
65 dev_err(dev, "failed to power off usb phy\n");
66 return ret;
67 }
68
69 ret = generic_phy_exit(&priv->phy);
70 if (ret) {
71 dev_err(dev, "failed to power off usb phy\n");
72 return ret;
73 }
74 }
75
76 return 0;
77}
78
Alexey Brodkinfee331f2015-12-14 17:18:50 +030079static int ohci_usb_probe(struct udevice *dev)
80{
Simon Glassa821c4a2017-05-17 17:18:05 -060081 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Patrice Chotard155d9f62017-07-18 11:57:12 +020082 struct generic_ohci *priv = dev_get_priv(dev);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +020083 int i, err, ret, clock_nb, reset_nb;
Alexey Brodkinfee331f2015-12-14 17:18:50 +030084
Patrice Chotard155d9f62017-07-18 11:57:12 +020085 err = 0;
86 priv->clock_count = 0;
87 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
88 if (clock_nb > 0) {
89 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
90 GFP_KERNEL);
91 if (!priv->clocks)
92 return -ENOMEM;
93
94 for (i = 0; i < clock_nb; i++) {
95 err = clk_get_by_index(dev, i, &priv->clocks[i]);
96 if (err < 0)
97 break;
98
99 err = clk_enable(&priv->clocks[i]);
Kever Yang6578db82019-08-28 16:23:47 +0800100 if (err && err != -ENOSYS) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100101 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200102 clk_free(&priv->clocks[i]);
103 goto clk_err;
104 }
105 priv->clock_count++;
106 }
107 } else if (clock_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100108 dev_err(dev, "failed to get clock phandle(%d)\n", clock_nb);
Patrice Chotard155d9f62017-07-18 11:57:12 +0200109 return clock_nb;
110 }
111
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200112 priv->reset_count = 0;
113 reset_nb = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
114 if (reset_nb > 0) {
115 priv->resets = devm_kcalloc(dev, reset_nb,
116 sizeof(struct reset_ctl),
117 GFP_KERNEL);
118 if (!priv->resets)
119 return -ENOMEM;
120
121 for (i = 0; i < reset_nb; i++) {
122 err = reset_get_by_index(dev, i, &priv->resets[i]);
123 if (err < 0)
124 break;
125
126 err = reset_deassert(&priv->resets[i]);
127 if (err) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100128 dev_err(dev, "failed to deassert reset %d\n", i);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200129 reset_free(&priv->resets[i]);
130 goto reset_err;
131 }
132 priv->reset_count++;
133 }
134 } else if (reset_nb != -ENOENT) {
Patrice Chotard6048d422018-03-14 17:48:58 +0100135 dev_err(dev, "failed to get reset phandle(%d)\n", reset_nb);
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200136 goto clk_err;
137 }
138
Patrice Chotardcab4d482018-03-14 17:48:57 +0100139 err = ohci_setup_phy(dev, 0);
140 if (err)
Patrice Chotardcab4d482018-03-14 17:48:57 +0100141 goto reset_err;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200142
Patrice Chotard155d9f62017-07-18 11:57:12 +0200143 err = ohci_register(dev, regs);
144 if (err)
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200145 goto phy_err;
Patrice Chotard155d9f62017-07-18 11:57:12 +0200146
147 return 0;
148
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200149phy_err:
Patrice Chotardcab4d482018-03-14 17:48:57 +0100150 ret = ohci_shutdown_phy(dev);
151 if (ret)
152 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200153
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200154reset_err:
155 ret = reset_release_all(priv->resets, priv->reset_count);
156 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100157 dev_err(dev, "failed to assert all resets\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200158clk_err:
159 ret = clk_release_all(priv->clocks, priv->clock_count);
160 if (ret)
Patrice Chotard6048d422018-03-14 17:48:58 +0100161 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotard155d9f62017-07-18 11:57:12 +0200162
163 return err;
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300164}
165
166static int ohci_usb_remove(struct udevice *dev)
167{
Patrice Chotard155d9f62017-07-18 11:57:12 +0200168 struct generic_ohci *priv = dev_get_priv(dev);
169 int ret;
170
171 ret = ohci_deregister(dev);
172 if (ret)
173 return ret;
174
Patrice Chotardcab4d482018-03-14 17:48:57 +0100175 ret = ohci_shutdown_phy(dev);
176 if (ret)
177 return ret;
Patrice Chotard28df1cf2017-07-18 11:57:14 +0200178
Patrice Chotard8a51b4b2017-07-18 11:57:13 +0200179 ret = reset_release_all(priv->resets, priv->reset_count);
180 if (ret)
181 return ret;
182
Patrice Chotard155d9f62017-07-18 11:57:12 +0200183 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkinfee331f2015-12-14 17:18:50 +0300184}
185
186static const struct udevice_id ohci_usb_ids[] = {
187 { .compatible = "generic-ohci" },
188 { }
189};
190
191U_BOOT_DRIVER(ohci_generic) = {
192 .name = "ohci_generic",
193 .id = UCLASS_USB,
194 .of_match = ohci_usb_ids,
195 .probe = ohci_usb_probe,
196 .remove = ohci_usb_remove,
197 .ops = &ohci_usb_ops,
198 .priv_auto_alloc_size = sizeof(struct generic_ohci),
199 .flags = DM_FLAG_ALLOC_PRIV_DMA,
200};