Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 2 | /* |
Hans de Goede | 7b79865 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 3 | * Sunxi ehci glue |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 4 | * |
Hans de Goede | 7b79865 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 5 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> |
| 6 | * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com> |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 7 | * |
| 8 | * Based on code from |
| 9 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 12 | #include <common.h> |
Hans de Goede | 375de01 | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 13 | #include <asm/arch/clock.h> |
Hans de Goede | 375de01 | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 15 | #include <dm.h> |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 16 | #include "ehci.h" |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 17 | #include <generic-phy.h> |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 18 | |
Hans de Goede | 948603d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_SUNXI_GEN_SUN4I |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame^] | 20 | #define BASE_DIST 0x8000 |
Hans de Goede | 948603d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 21 | #define AHB_CLK_DIST 2 |
| 22 | #else |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame^] | 23 | #define BASE_DIST 0x1000 |
Hans de Goede | 948603d | 2016-03-21 14:44:35 +0100 | [diff] [blame] | 24 | #define AHB_CLK_DIST 1 |
| 25 | #endif |
| 26 | |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 27 | #define SUN6I_AHB_RESET0_CFG_OFFSET 0x2c0 |
| 28 | #define SUN9I_AHB_RESET0_CFG_OFFSET 0x5a0 |
| 29 | |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 30 | struct ehci_sunxi_cfg { |
| 31 | bool has_reset; |
| 32 | u32 extra_ahb_gate_mask; |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 33 | u32 reset0_cfg_offset; |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 36 | struct ehci_sunxi_priv { |
| 37 | struct ehci_ctrl ehci; |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 38 | struct sunxi_ccm_reg *ccm; |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 39 | u32 *reset0_cfg; |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 40 | int ahb_gate_mask; /* Mask of ahb_gate0 clk gate bits for this hcd */ |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 41 | struct phy phy; |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 42 | const struct ehci_sunxi_cfg *cfg; |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static int ehci_usb_probe(struct udevice *dev) |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 46 | { |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 47 | struct usb_platdata *plat = dev_get_platdata(dev); |
| 48 | struct ehci_sunxi_priv *priv = dev_get_priv(dev); |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 49 | struct ehci_hccr *hccr = (struct ehci_hccr *)devfdt_get_addr(dev); |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 50 | struct ehci_hcor *hcor; |
Hans de Goede | fb3bfbb | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 51 | int extra_ahb_gate_mask = 0; |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame^] | 52 | u8 reg_mask = 0; |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 53 | int phys, ret; |
Hans de Goede | 115200c | 2014-11-07 16:09:00 +0100 | [diff] [blame] | 54 | |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 55 | priv->cfg = (const struct ehci_sunxi_cfg *)dev_get_driver_data(dev); |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 56 | priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 57 | if (IS_ERR(priv->ccm)) |
| 58 | return PTR_ERR(priv->ccm); |
| 59 | |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 60 | priv->reset0_cfg = (void *)priv->ccm + |
| 61 | priv->cfg->reset0_cfg_offset; |
| 62 | |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 63 | phys = dev_count_phandle_with_args(dev, "phys", "#phy-cells"); |
| 64 | if (phys < 0) { |
| 65 | phys = 0; |
| 66 | goto no_phy; |
| 67 | } |
| 68 | |
| 69 | ret = generic_phy_get_by_name(dev, "usb", &priv->phy); |
| 70 | if (ret) { |
| 71 | pr_err("failed to get %s usb PHY\n", dev->name); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | ret = generic_phy_init(&priv->phy); |
| 76 | if (ret) { |
| 77 | pr_err("failed to init %s USB PHY\n", dev->name); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | ret = generic_phy_power_on(&priv->phy); |
| 82 | if (ret) { |
| 83 | pr_err("failed to power on %s USB PHY\n", dev->name); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | no_phy: |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 88 | /* |
| 89 | * This should go away once we've moved to the driver model for |
| 90 | * clocks resp. phys. |
| 91 | */ |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame^] | 92 | reg_mask = ((uintptr_t)hccr - SUNXI_USB1_BASE) / BASE_DIST; |
Jelle van der Waa | dc44fd8 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 93 | priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0; |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 94 | extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask; |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame^] | 95 | priv->ahb_gate_mask <<= reg_mask * AHB_CLK_DIST; |
| 96 | extra_ahb_gate_mask <<= reg_mask * AHB_CLK_DIST; |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 97 | |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 98 | setbits_le32(&priv->ccm->ahb_gate0, |
Hans de Goede | fb3bfbb | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 99 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 100 | if (priv->cfg->has_reset) |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 101 | setbits_le32(priv->reset0_cfg, |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 102 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Hans de Goede | 375de01 | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 103 | |
Amit Singh Tomar | 9d6c9d9 | 2016-10-21 02:24:30 +0100 | [diff] [blame] | 104 | hcor = (struct ehci_hcor *)((uintptr_t)hccr + |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 105 | HC_LENGTH(ehci_readl(&hccr->cr_capbase))); |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 106 | |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 107 | return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type); |
| 108 | } |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 109 | |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 110 | static int ehci_usb_remove(struct udevice *dev) |
| 111 | { |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 112 | struct ehci_sunxi_priv *priv = dev_get_priv(dev); |
| 113 | int ret; |
| 114 | |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 115 | if (generic_phy_valid(&priv->phy)) { |
| 116 | ret = generic_phy_exit(&priv->phy); |
| 117 | if (ret) { |
| 118 | pr_err("failed to exit %s USB PHY\n", dev->name); |
| 119 | return ret; |
| 120 | } |
| 121 | } |
| 122 | |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 123 | ret = ehci_deregister(dev); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 127 | if (priv->cfg->has_reset) |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 128 | clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask); |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 129 | clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask); |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 130 | |
Roman Byshko | 8d15400 | 2014-07-27 19:32:44 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 134 | static const struct ehci_sunxi_cfg sun4i_a10_cfg = { |
| 135 | .has_reset = false, |
| 136 | }; |
| 137 | |
| 138 | static const struct ehci_sunxi_cfg sun6i_a31_cfg = { |
| 139 | .has_reset = true, |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 140 | .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET, |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static const struct ehci_sunxi_cfg sun8i_h3_cfg = { |
| 144 | .has_reset = true, |
| 145 | .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0, |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 146 | .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET, |
| 147 | }; |
| 148 | |
| 149 | static const struct ehci_sunxi_cfg sun9i_a80_cfg = { |
| 150 | .has_reset = true, |
| 151 | .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET, |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 154 | static const struct udevice_id ehci_usb_ids[] = { |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 155 | { |
| 156 | .compatible = "allwinner,sun4i-a10-ehci", |
| 157 | .data = (ulong)&sun4i_a10_cfg, |
| 158 | }, |
| 159 | { |
| 160 | .compatible = "allwinner,sun5i-a13-ehci", |
| 161 | .data = (ulong)&sun4i_a10_cfg, |
| 162 | }, |
| 163 | { |
| 164 | .compatible = "allwinner,sun6i-a31-ehci", |
| 165 | .data = (ulong)&sun6i_a31_cfg, |
| 166 | }, |
| 167 | { |
| 168 | .compatible = "allwinner,sun7i-a20-ehci", |
| 169 | .data = (ulong)&sun4i_a10_cfg, |
| 170 | }, |
| 171 | { |
| 172 | .compatible = "allwinner,sun8i-a23-ehci", |
| 173 | .data = (ulong)&sun6i_a31_cfg, |
| 174 | }, |
| 175 | { |
| 176 | .compatible = "allwinner,sun8i-a83t-ehci", |
| 177 | .data = (ulong)&sun6i_a31_cfg, |
| 178 | }, |
| 179 | { |
| 180 | .compatible = "allwinner,sun8i-h3-ehci", |
| 181 | .data = (ulong)&sun8i_h3_cfg, |
| 182 | }, |
| 183 | { |
| 184 | .compatible = "allwinner,sun9i-a80-ehci", |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 185 | .data = (ulong)&sun9i_a80_cfg, |
Vasily Khoruzhick | 1ed9c11 | 2018-06-07 19:23:39 -0700 | [diff] [blame] | 186 | }, |
| 187 | { |
| 188 | .compatible = "allwinner,sun50i-a64-ehci", |
| 189 | .data = (ulong)&sun8i_h3_cfg, |
| 190 | }, |
| 191 | { /* sentinel */ } |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 192 | }; |
Hans de Goede | 375de01 | 2015-04-27 11:44:22 +0200 | [diff] [blame] | 193 | |
Marek Vasut | 40c9208 | 2015-11-30 18:15:33 +0100 | [diff] [blame] | 194 | U_BOOT_DRIVER(ehci_sunxi) = { |
Hans de Goede | 8d837a1 | 2015-05-10 14:10:26 +0200 | [diff] [blame] | 195 | .name = "ehci_sunxi", |
| 196 | .id = UCLASS_USB, |
| 197 | .of_match = ehci_usb_ids, |
| 198 | .probe = ehci_usb_probe, |
| 199 | .remove = ehci_usb_remove, |
| 200 | .ops = &ehci_usb_ops, |
| 201 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 202 | .priv_auto_alloc_size = sizeof(struct ehci_sunxi_priv), |
| 203 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 204 | }; |