blob: bb3c2475df9a61155bb00e9bcdc00efa9ddeb673 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hans de Goede6a72e802015-05-10 14:10:27 +02002/*
3 * Sunxi ohci glue
4 *
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
Hans de Goede6a72e802015-05-10 14:10:27 +02009 */
10
11#include <common.h>
12#include <asm/arch/clock.h>
Hans de Goede6a72e802015-05-10 14:10:27 +020013#include <asm/io.h>
14#include <dm.h>
15#include <usb.h>
16#include "ohci.h"
Jagan Tekidd322812018-05-07 13:03:38 +053017#include <generic-phy.h>
Hans de Goede6a72e802015-05-10 14:10:27 +020018
Hans de Goede948603d2016-03-21 14:44:35 +010019#ifdef CONFIG_SUNXI_GEN_SUN4I
Jagan Teki9c22aec2018-06-28 19:40:46 +053020#define BASE_DIST 0x8000
Hans de Goede948603d2016-03-21 14:44:35 +010021#define AHB_CLK_DIST 2
22#else
Jagan Teki9c22aec2018-06-28 19:40:46 +053023#define BASE_DIST 0x1000
Hans de Goede948603d2016-03-21 14:44:35 +010024#define AHB_CLK_DIST 1
25#endif
26
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -070027#define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0
28#define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0
29
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -070030struct ohci_sunxi_cfg {
31 bool has_reset;
32 u32 extra_ahb_gate_mask;
Vasily Khoruzhick11bb6272018-06-07 19:23:41 -070033 u32 extra_usb_gate_mask;
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -070034 u32 reset0_cfg_offset;
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -070035};
36
Hans de Goede6a72e802015-05-10 14:10:27 +020037struct ohci_sunxi_priv {
Vasily Khoruzhickebbc23a2018-06-17 09:13:42 -070038 ohci_t ohci;
Jagan Teki831cc982018-05-07 13:03:17 +053039 struct sunxi_ccm_reg *ccm;
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -070040 u32 *reset0_cfg;
Hans de Goede6a72e802015-05-10 14:10:27 +020041 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
42 int usb_gate_mask; /* Mask of usb_clk_cfg clk gate bits for this hcd */
Jagan Tekidd322812018-05-07 13:03:38 +053043 struct phy phy;
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -070044 const struct ohci_sunxi_cfg *cfg;
Hans de Goede6a72e802015-05-10 14:10:27 +020045};
46
Andre Przywara0bc846a2018-07-05 00:57:48 +010047static fdt_addr_t last_ohci_addr = 0;
48
Hans de Goede6a72e802015-05-10 14:10:27 +020049static int ohci_usb_probe(struct udevice *dev)
50{
Hans de Goede6a72e802015-05-10 14:10:27 +020051 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
52 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
Simon Glassa821c4a2017-05-17 17:18:05 -060053 struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev);
Hans de Goedefb3bfbb2016-04-09 15:00:52 +020054 int extra_ahb_gate_mask = 0;
Jagan Teki9c22aec2018-06-28 19:40:46 +053055 u8 reg_mask = 0;
Jagan Tekidd322812018-05-07 13:03:38 +053056 int phys, ret;
Hans de Goede6a72e802015-05-10 14:10:27 +020057
Andre Przywara0bc846a2018-07-05 00:57:48 +010058 if ((fdt_addr_t)regs > last_ohci_addr)
59 last_ohci_addr = (fdt_addr_t)regs;
60
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -070061 priv->cfg = (const struct ohci_sunxi_cfg *)dev_get_driver_data(dev);
Jagan Teki831cc982018-05-07 13:03:17 +053062 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
63 if (IS_ERR(priv->ccm))
64 return PTR_ERR(priv->ccm);
65
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -070066 priv->reset0_cfg = (void *)priv->ccm +
67 priv->cfg->reset0_cfg_offset;
68
Jagan Tekidd322812018-05-07 13:03:38 +053069 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
70 if (phys < 0) {
71 phys = 0;
72 goto no_phy;
73 }
74
75 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
76 if (ret) {
77 pr_err("failed to get %s usb PHY\n", dev->name);
78 return ret;
79 }
80
81 ret = generic_phy_init(&priv->phy);
82 if (ret) {
83 pr_err("failed to init %s USB PHY\n", dev->name);
84 return ret;
85 }
86
87 ret = generic_phy_power_on(&priv->phy);
88 if (ret) {
89 pr_err("failed to power on %s USB PHY\n", dev->name);
90 return ret;
91 }
92
93no_phy:
Hans de Goede6a72e802015-05-10 14:10:27 +020094 bus_priv->companion = true;
95
96 /*
97 * This should go away once we've moved to the driver model for
98 * clocks resp. phys.
99 */
Jagan Teki9c22aec2018-06-28 19:40:46 +0530100 reg_mask = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST;
Jelle van der Waadc44fd82016-02-09 23:59:33 +0100101 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0;
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700102 extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
Jelle van der Waadc44fd82016-02-09 23:59:33 +0100103 priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK;
Jagan Teki9c22aec2018-06-28 19:40:46 +0530104 priv->ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
105 extra_ahb_gate_mask <<= reg_mask * AHB_CLK_DIST;
106 priv->usb_gate_mask <<= reg_mask;
Hans de Goede6a72e802015-05-10 14:10:27 +0200107
Jagan Teki831cc982018-05-07 13:03:17 +0530108 setbits_le32(&priv->ccm->ahb_gate0,
Hans de Goedefb3bfbb2016-04-09 15:00:52 +0200109 priv->ahb_gate_mask | extra_ahb_gate_mask);
Vasily Khoruzhick11bb6272018-06-07 19:23:41 -0700110 setbits_le32(&priv->ccm->usb_clk_cfg,
111 priv->usb_gate_mask | priv->cfg->extra_usb_gate_mask);
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700112 if (priv->cfg->has_reset)
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700113 setbits_le32(priv->reset0_cfg,
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700114 priv->ahb_gate_mask | extra_ahb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +0200115
Hans de Goede6a72e802015-05-10 14:10:27 +0200116 return ohci_register(dev, regs);
117}
118
119static int ohci_usb_remove(struct udevice *dev)
120{
Hans de Goede6a72e802015-05-10 14:10:27 +0200121 struct ohci_sunxi_priv *priv = dev_get_priv(dev);
Andre Przywara0bc846a2018-07-05 00:57:48 +0100122 fdt_addr_t base_addr = devfdt_get_addr(dev);
Hans de Goede6a72e802015-05-10 14:10:27 +0200123 int ret;
124
Jagan Tekidd322812018-05-07 13:03:38 +0530125 if (generic_phy_valid(&priv->phy)) {
126 ret = generic_phy_exit(&priv->phy);
127 if (ret) {
128 pr_err("failed to exit %s USB PHY\n", dev->name);
129 return ret;
130 }
131 }
132
Hans de Goede6a72e802015-05-10 14:10:27 +0200133 ret = ohci_deregister(dev);
134 if (ret)
135 return ret;
136
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700137 if (priv->cfg->has_reset)
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700138 clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
Andre Przywara0bc846a2018-07-05 00:57:48 +0100139 /*
140 * On the A64 CLK_USB_OHCI0 is the parent of CLK_USB_OHCI1, so
141 * we have to wait with bringing down any clock until the last
142 * OHCI controller is removed.
143 */
144 if (!priv->cfg->extra_usb_gate_mask || base_addr == last_ohci_addr) {
145 u32 usb_gate_mask = priv->usb_gate_mask;
146
147 usb_gate_mask |= priv->cfg->extra_usb_gate_mask;
148 clrbits_le32(&priv->ccm->usb_clk_cfg, usb_gate_mask);
149 }
150
Jagan Teki831cc982018-05-07 13:03:17 +0530151 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
Hans de Goede6a72e802015-05-10 14:10:27 +0200152
153 return 0;
154}
155
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700156static const struct ohci_sunxi_cfg sun4i_a10_cfg = {
157 .has_reset = false,
158};
159
160static const struct ohci_sunxi_cfg sun6i_a31_cfg = {
161 .has_reset = true,
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700162 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700163};
164
165static const struct ohci_sunxi_cfg sun8i_h3_cfg = {
166 .has_reset = true,
167 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700168 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
169};
170
171static const struct ohci_sunxi_cfg sun9i_a80_cfg = {
172 .has_reset = true,
173 .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700174};
175
Vasily Khoruzhick11bb6272018-06-07 19:23:41 -0700176static const struct ohci_sunxi_cfg sun50i_a64_cfg = {
177 .has_reset = true,
178 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
179 .extra_usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK,
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700180 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
Vasily Khoruzhick11bb6272018-06-07 19:23:41 -0700181};
182
Hans de Goede6a72e802015-05-10 14:10:27 +0200183static const struct udevice_id ohci_usb_ids[] = {
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700184 {
185 .compatible = "allwinner,sun4i-a10-ohci",
186 .data = (ulong)&sun4i_a10_cfg,
187 },
188 {
189 .compatible = "allwinner,sun5i-a13-ohci",
190 .data = (ulong)&sun4i_a10_cfg,
191 },
192 {
193 .compatible = "allwinner,sun6i-a31-ohci",
194 .data = (ulong)&sun6i_a31_cfg,
195 },
196 {
197 .compatible = "allwinner,sun7i-a20-ohci",
198 .data = (ulong)&sun4i_a10_cfg,
199 },
200 {
201 .compatible = "allwinner,sun8i-a23-ohci",
202 .data = (ulong)&sun6i_a31_cfg,
203 },
204 {
205 .compatible = "allwinner,sun8i-a83t-ohci",
206 .data = (ulong)&sun6i_a31_cfg,
207 },
208 {
209 .compatible = "allwinner,sun8i-h3-ohci",
210 .data = (ulong)&sun8i_h3_cfg,
211 },
212 {
213 .compatible = "allwinner,sun9i-a80-ohci",
Vasily Khoruzhickb9f34752018-06-13 23:19:34 -0700214 .data = (ulong)&sun9i_a80_cfg,
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700215 },
216 {
217 .compatible = "allwinner,sun50i-a64-ohci",
Vasily Khoruzhick11bb6272018-06-07 19:23:41 -0700218 .data = (ulong)&sun50i_a64_cfg,
Vasily Khoruzhick56830ce2018-06-07 19:23:40 -0700219 },
220 { /* sentinel */ }
Hans de Goede6a72e802015-05-10 14:10:27 +0200221};
222
223U_BOOT_DRIVER(usb_ohci) = {
224 .name = "ohci_sunxi",
225 .id = UCLASS_USB,
226 .of_match = ohci_usb_ids,
227 .probe = ohci_usb_probe,
228 .remove = ohci_usb_remove,
229 .ops = &ohci_usb_ops,
230 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
231 .priv_auto_alloc_size = sizeof(struct ohci_sunxi_priv),
232 .flags = DM_FLAG_ALLOC_PRIV_DMA,
233};