blob: 97d06d597582062422e9f03d441928d61f9645f5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Roman Byshko637fbb62014-07-27 19:32:44 +02002/*
Hans de Goede86979092015-04-27 14:54:47 +02003 * Sunxi ehci glue
Roman Byshko637fbb62014-07-27 19:32:44 +02004 *
Hans de Goede86979092015-04-27 14:54:47 +02005 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
Roman Byshko637fbb62014-07-27 19:32:44 +02007 *
8 * Based on code from
9 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
Roman Byshko637fbb62014-07-27 19:32:44 +020010 */
11
Roman Byshko637fbb62014-07-27 19:32:44 +020012#include <common.h>
Hans de Goede7e5aabd2015-04-27 11:44:22 +020013#include <asm/arch/clock.h>
Hans de Goede7e5aabd2015-04-27 11:44:22 +020014#include <asm/io.h>
Hans de Goede4dba1082015-05-10 14:10:26 +020015#include <dm.h>
Roman Byshko637fbb62014-07-27 19:32:44 +020016#include "ehci.h"
Jagan Teki137fc752018-05-07 13:03:38 +053017#include <generic-phy.h>
Roman Byshko637fbb62014-07-27 19:32:44 +020018
Hans de Goede8b6ab7d2016-03-21 14:44:35 +010019#ifdef CONFIG_SUNXI_GEN_SUN4I
Hans de Goede8b6ab7d2016-03-21 14:44:35 +010020#define AHB_CLK_DIST 2
21#else
Hans de Goede8b6ab7d2016-03-21 14:44:35 +010022#define AHB_CLK_DIST 1
23#endif
24
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -070025#define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0
26#define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0
27
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070028struct ehci_sunxi_cfg {
29 bool has_reset;
30 u32 extra_ahb_gate_mask;
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -070031 u32 reset0_cfg_offset;
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070032};
33
Hans de Goede4dba1082015-05-10 14:10:26 +020034struct ehci_sunxi_priv {
35 struct ehci_ctrl ehci;
Jagan Tekifda15692018-05-07 13:03:17 +053036 struct sunxi_ccm_reg *ccm;
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -070037 u32 *reset0_cfg;
Hans de Goede4dba1082015-05-10 14:10:26 +020038 int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */
Jagan Teki137fc752018-05-07 13:03:38 +053039 struct phy phy;
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070040 const struct ehci_sunxi_cfg *cfg;
Hans de Goede4dba1082015-05-10 14:10:26 +020041};
42
43static int ehci_usb_probe(struct udevice *dev)
Roman Byshko637fbb62014-07-27 19:32:44 +020044{
Hans de Goede4dba1082015-05-10 14:10:26 +020045 struct usb_platdata *plat = dev_get_platdata(dev);
46 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
Simon Glassba1dea42017-05-17 17:18:05 -060047 struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev);
Hans de Goede4dba1082015-05-10 14:10:26 +020048 struct ehci_hcor *hcor;
Hans de Goede52b5e5a2016-04-09 15:00:52 +020049 int extra_ahb_gate_mask = 0;
Jagan Teki137fc752018-05-07 13:03:38 +053050 int phys, ret;
Hans de Goede4dba1082015-05-10 14:10:26 +020051
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070052 priv->cfg = (const struct ehci_sunxi_cfg *)dev_get_driver_data(dev);
Jagan Tekifda15692018-05-07 13:03:17 +053053 priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
54 if (IS_ERR(priv->ccm))
55 return PTR_ERR(priv->ccm);
56
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -070057 priv->reset0_cfg = (void *)priv->ccm +
58 priv->cfg->reset0_cfg_offset;
59
Jagan Teki137fc752018-05-07 13:03:38 +053060 phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
61 if (phys < 0) {
62 phys = 0;
63 goto no_phy;
64 }
65
66 ret = generic_phy_get_by_name(dev, "usb", &priv->phy);
67 if (ret) {
68 pr_err("failed to get %s usb PHY\n", dev->name);
69 return ret;
70 }
71
72 ret = generic_phy_init(&priv->phy);
73 if (ret) {
74 pr_err("failed to init %s USB PHY\n", dev->name);
75 return ret;
76 }
77
78 ret = generic_phy_power_on(&priv->phy);
79 if (ret) {
80 pr_err("failed to power on %s USB PHY\n", dev->name);
81 return ret;
82 }
83
84no_phy:
Hans de Goede4dba1082015-05-10 14:10:26 +020085 /*
86 * This should go away once we've moved to the driver model for
87 * clocks resp. phys.
88 */
Jelle van der Waaa1f5d112016-02-09 23:59:33 +010089 priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0;
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070090 extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask;
Jagan Teki137fc752018-05-07 13:03:38 +053091 priv->ahb_gate_mask <<= phys * AHB_CLK_DIST;
92 extra_ahb_gate_mask <<= phys * AHB_CLK_DIST;
Hans de Goedeaf4273b2014-11-07 16:09:00 +010093
Jagan Tekifda15692018-05-07 13:03:17 +053094 setbits_le32(&priv->ccm->ahb_gate0,
Hans de Goede52b5e5a2016-04-09 15:00:52 +020095 priv->ahb_gate_mask | extra_ahb_gate_mask);
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070096 if (priv->cfg->has_reset)
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -070097 setbits_le32(priv->reset0_cfg,
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -070098 priv->ahb_gate_mask | extra_ahb_gate_mask);
Hans de Goede7e5aabd2015-04-27 11:44:22 +020099
Amit Singh Tomarbd732d02016-10-21 02:24:30 +0100100 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
Hans de Goede4dba1082015-05-10 14:10:26 +0200101 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Roman Byshko637fbb62014-07-27 19:32:44 +0200102
Hans de Goede4dba1082015-05-10 14:10:26 +0200103 return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
Roman Byshko637fbb62014-07-27 19:32:44 +0200104}
105
Hans de Goede4dba1082015-05-10 14:10:26 +0200106static int ehci_usb_remove(struct udevice *dev)
Roman Byshko637fbb62014-07-27 19:32:44 +0200107{
Hans de Goede4dba1082015-05-10 14:10:26 +0200108 struct ehci_sunxi_priv *priv = dev_get_priv(dev);
109 int ret;
110
Jagan Teki137fc752018-05-07 13:03:38 +0530111 if (generic_phy_valid(&priv->phy)) {
112 ret = generic_phy_exit(&priv->phy);
113 if (ret) {
114 pr_err("failed to exit %s USB PHY\n", dev->name);
115 return ret;
116 }
117 }
118
Hans de Goede4dba1082015-05-10 14:10:26 +0200119 ret = ehci_deregister(dev);
120 if (ret)
121 return ret;
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200122
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700123 if (priv->cfg->has_reset)
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -0700124 clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask);
Jagan Tekifda15692018-05-07 13:03:17 +0530125 clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask);
Hans de Goede7e5aabd2015-04-27 11:44:22 +0200126
Hans de Goede1168e092015-04-27 16:50:04 +0200127 return 0;
Roman Byshko637fbb62014-07-27 19:32:44 +0200128}
Hans de Goede4dba1082015-05-10 14:10:26 +0200129
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700130static const struct ehci_sunxi_cfg sun4i_a10_cfg = {
131 .has_reset = false,
132};
133
134static const struct ehci_sunxi_cfg sun6i_a31_cfg = {
135 .has_reset = true,
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -0700136 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700137};
138
139static const struct ehci_sunxi_cfg sun8i_h3_cfg = {
140 .has_reset = true,
141 .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0,
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -0700142 .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET,
143};
144
145static const struct ehci_sunxi_cfg sun9i_a80_cfg = {
146 .has_reset = true,
147 .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET,
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700148};
149
Hans de Goede4dba1082015-05-10 14:10:26 +0200150static const struct udevice_id ehci_usb_ids[] = {
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700151 {
152 .compatible = "allwinner,sun4i-a10-ehci",
153 .data = (ulong)&sun4i_a10_cfg,
154 },
155 {
156 .compatible = "allwinner,sun5i-a13-ehci",
157 .data = (ulong)&sun4i_a10_cfg,
158 },
159 {
160 .compatible = "allwinner,sun6i-a31-ehci",
161 .data = (ulong)&sun6i_a31_cfg,
162 },
163 {
164 .compatible = "allwinner,sun7i-a20-ehci",
165 .data = (ulong)&sun4i_a10_cfg,
166 },
167 {
168 .compatible = "allwinner,sun8i-a23-ehci",
169 .data = (ulong)&sun6i_a31_cfg,
170 },
171 {
172 .compatible = "allwinner,sun8i-a83t-ehci",
173 .data = (ulong)&sun6i_a31_cfg,
174 },
175 {
176 .compatible = "allwinner,sun8i-h3-ehci",
177 .data = (ulong)&sun8i_h3_cfg,
178 },
179 {
180 .compatible = "allwinner,sun9i-a80-ehci",
Vasily Khoruzhick63400ad2018-06-13 23:19:34 -0700181 .data = (ulong)&sun9i_a80_cfg,
Vasily Khoruzhick9f9c6cf2018-06-07 19:23:39 -0700182 },
183 {
184 .compatible = "allwinner,sun50i-a64-ehci",
185 .data = (ulong)&sun8i_h3_cfg,
186 },
187 { /* sentinel */ }
Hans de Goede4dba1082015-05-10 14:10:26 +0200188};
189
Marek Vasutad693ce2015-11-30 18:15:33 +0100190U_BOOT_DRIVER(ehci_sunxi) = {
Hans de Goede4dba1082015-05-10 14:10:26 +0200191 .name = "ehci_sunxi",
192 .id = UCLASS_USB,
193 .of_match = ehci_usb_ids,
194 .probe = ehci_usb_probe,
195 .remove = ehci_usb_remove,
196 .ops = &ehci_usb_ops,
197 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
198 .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv),
199 .flags = DM_FLAG_ALLOC_PRIV_DMA,
200};