blob: b012d8651f1239c2a4048d3d5369c3eb08e23d4d [file] [log] [blame]
Alexey Brodkin90fbb282015-12-02 12:32:02 +03001/*
2 * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Masahiro Yamada4feefdc2016-01-25 15:00:36 +09008#include <clk.h>
Patrice Chotarda1cee8e2017-07-18 11:57:10 +02009#include <dm/ofnode.h>
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020010#include <generic-phy.h>
Masahiro Yamada8824cfc2016-09-21 11:29:02 +090011#include <reset.h>
Marek Vasut643cacb2016-01-23 21:04:46 +010012#include <asm/io.h>
Alexey Brodkin90fbb282015-12-02 12:32:02 +030013#include <dm.h>
14#include "ehci.h"
15
16/*
17 * Even though here we don't explicitly use "struct ehci_ctrl"
18 * ehci_register() expects it to be the first thing that resides in
19 * device's private data.
20 */
21struct generic_ehci {
22 struct ehci_ctrl ctrl;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020023 struct clk *clocks;
24 struct reset_ctl *resets;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +020025 struct phy phy;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020026 int clock_count;
27 int reset_count;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030028};
29
Patrice Chotard20f06a42018-03-14 17:48:54 +010030static int ehci_setup_phy(struct udevice *dev, int index)
31{
32 struct generic_ehci *priv = dev_get_priv(dev);
33 int ret;
34
35 ret = generic_phy_get_by_index(dev, index, &priv->phy);
36 if (ret) {
37 if (ret != -ENOENT) {
38 dev_err(dev, "failed to get usb phy\n");
39 return ret;
40 }
41 } else {
42 ret = generic_phy_init(&priv->phy);
43 if (ret) {
44 dev_err(dev, "failed to init usb phy\n");
45 return ret;
46 }
47
48 ret = generic_phy_power_on(&priv->phy);
49 if (ret) {
50 dev_err(dev, "failed to power on usb phy\n");
51 return generic_phy_exit(&priv->phy);
52 }
53 }
54
55 return 0;
56}
57
58static int ehci_shutdown_phy(struct udevice *dev)
59{
60 struct generic_ehci *priv = dev_get_priv(dev);
61 int ret = 0;
62
63 if (generic_phy_valid(&priv->phy)) {
64 ret = generic_phy_power_off(&priv->phy);
65 if (ret) {
66 dev_err(dev, "failed to power off usb phy\n");
67 return ret;
68 }
69
70 ret = generic_phy_exit(&priv->phy);
71 if (ret) {
72 dev_err(dev, "failed to power off usb phy\n");
73 return ret;
74 }
75 }
76
77 return 0;
78}
79
Alexey Brodkin90fbb282015-12-02 12:32:02 +030080static int ehci_usb_probe(struct udevice *dev)
81{
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020082 struct generic_ehci *priv = dev_get_priv(dev);
Marek Vasut643cacb2016-01-23 21:04:46 +010083 struct ehci_hccr *hccr;
Alexey Brodkin90fbb282015-12-02 12:32:02 +030084 struct ehci_hcor *hcor;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020085 int i, err, ret, clock_nb, reset_nb;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090086
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020087 err = 0;
88 priv->clock_count = 0;
89 clock_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "clocks",
90 "#clock-cells");
91 if (clock_nb > 0) {
92 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
93 GFP_KERNEL);
94 if (!priv->clocks)
95 return -ENOMEM;
Masahiro Yamada4feefdc2016-01-25 15:00:36 +090096
Patrice Chotarda1cee8e2017-07-18 11:57:10 +020097 for (i = 0; i < clock_nb; i++) {
98 err = clk_get_by_index(dev, i, &priv->clocks[i]);
99
100 if (err < 0)
101 break;
102 err = clk_enable(&priv->clocks[i]);
103 if (err) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100104 dev_err(dev, "failed to enable clock %d\n", i);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200105 clk_free(&priv->clocks[i]);
106 goto clk_err;
107 }
108 priv->clock_count++;
109 }
110 } else {
111 if (clock_nb != -ENOENT) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100112 dev_err(dev, "failed to get clock phandle(%d)\n",
113 clock_nb);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200114 return clock_nb;
115 }
Masahiro Yamada4feefdc2016-01-25 15:00:36 +0900116 }
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300117
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200118 priv->reset_count = 0;
119 reset_nb = ofnode_count_phandle_with_args(dev_ofnode(dev), "resets",
120 "#reset-cells");
121 if (reset_nb > 0) {
122 priv->resets = devm_kcalloc(dev, reset_nb,
123 sizeof(struct reset_ctl),
124 GFP_KERNEL);
125 if (!priv->resets)
126 return -ENOMEM;
Masahiro Yamada8824cfc2016-09-21 11:29:02 +0900127
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200128 for (i = 0; i < reset_nb; i++) {
129 err = reset_get_by_index(dev, i, &priv->resets[i]);
130 if (err < 0)
131 break;
132
133 if (reset_deassert(&priv->resets[i])) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100134 dev_err(dev, "failed to deassert reset %d\n",
135 i);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200136 reset_free(&priv->resets[i]);
137 goto reset_err;
138 }
139 priv->reset_count++;
140 }
141 } else {
142 if (reset_nb != -ENOENT) {
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100143 dev_err(dev, "failed to get reset phandle(%d)\n",
144 reset_nb);
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200145 goto clk_err;
146 }
Masahiro Yamada8824cfc2016-09-21 11:29:02 +0900147 }
148
Patrice Chotard20f06a42018-03-14 17:48:54 +0100149 err = ehci_setup_phy(dev, 0);
150 if (err)
Patrice Chotard20f06a42018-03-14 17:48:54 +0100151 goto reset_err;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200152
Philipp Tomsich6e652e32017-09-12 17:32:28 +0200153 hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300154 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
155 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
156
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200157 err = ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
158 if (err)
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200159 goto phy_err;
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200160
161 return 0;
162
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200163phy_err:
Patrice Chotard20f06a42018-03-14 17:48:54 +0100164 ret = ehci_shutdown_phy(dev);
165 if (ret)
166 dev_err(dev, "failed to shutdown usb phy\n");
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200167
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200168reset_err:
169 ret = reset_release_all(priv->resets, priv->reset_count);
170 if (ret)
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100171 dev_err(dev, "failed to assert all resets\n");
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200172clk_err:
173 ret = clk_release_all(priv->clocks, priv->clock_count);
174 if (ret)
Patrice Chotarddf7777a2018-03-14 17:48:55 +0100175 dev_err(dev, "failed to disable all clocks\n");
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200176
177 return err;
178}
179
180static int ehci_usb_remove(struct udevice *dev)
181{
182 struct generic_ehci *priv = dev_get_priv(dev);
183 int ret;
184
185 ret = ehci_deregister(dev);
186 if (ret)
187 return ret;
188
Patrice Chotard20f06a42018-03-14 17:48:54 +0100189 ret = ehci_shutdown_phy(dev);
190 if (ret)
191 return ret;
Patrice Chotard0d0ba1a2017-07-18 11:57:11 +0200192
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200193 ret = reset_release_all(priv->resets, priv->reset_count);
194 if (ret)
195 return ret;
196
197 return clk_release_all(priv->clocks, priv->clock_count);
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300198}
199
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300200static const struct udevice_id ehci_usb_ids[] = {
201 { .compatible = "generic-ehci" },
202 { }
203};
204
205U_BOOT_DRIVER(ehci_generic) = {
206 .name = "ehci_generic",
207 .id = UCLASS_USB,
208 .of_match = ehci_usb_ids,
209 .probe = ehci_usb_probe,
Patrice Chotarda1cee8e2017-07-18 11:57:10 +0200210 .remove = ehci_usb_remove,
Alexey Brodkin90fbb282015-12-02 12:32:02 +0300211 .ops = &ehci_usb_ops,
212 .priv_auto_alloc_size = sizeof(struct generic_ehci),
213 .flags = DM_FLAG_ALLOC_PRIV_DMA,
214};