Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 2 | /* |
| 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 Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <asm/arch/clock.h> |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <dm.h> |
| 15 | #include <usb.h> |
| 16 | #include "ohci.h" |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 17 | #include <generic-phy.h> |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +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 | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 30 | struct ohci_sunxi_cfg { |
| 31 | bool has_reset; |
| 32 | u32 extra_ahb_gate_mask; |
Vasily Khoruzhick | 11bb627 | 2018-06-07 19:23:41 -0700 | [diff] [blame] | 33 | u32 extra_usb_gate_mask; |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 34 | u32 reset0_cfg_offset; |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 37 | struct ohci_sunxi_priv { |
Vasily Khoruzhick | ebbc23a | 2018-06-17 09:13:42 -0700 | [diff] [blame] | 38 | ohci_t ohci; |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 39 | struct sunxi_ccm_reg *ccm; |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 40 | u32 *reset0_cfg; |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 41 | 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 Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 43 | struct phy phy; |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 44 | const struct ohci_sunxi_cfg *cfg; |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Andre Przywara | 0bc846a | 2018-07-05 00:57:48 +0100 | [diff] [blame] | 47 | static fdt_addr_t last_ohci_addr = 0; |
| 48 | |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 49 | static int ohci_usb_probe(struct udevice *dev) |
| 50 | { |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 51 | struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev); |
| 52 | struct ohci_sunxi_priv *priv = dev_get_priv(dev); |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 53 | struct ohci_regs *regs = (struct ohci_regs *)devfdt_get_addr(dev); |
Hans de Goede | fb3bfbb | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 54 | int extra_ahb_gate_mask = 0; |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame] | 55 | u8 reg_mask = 0; |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 56 | int phys, ret; |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 57 | |
Andre Przywara | 0bc846a | 2018-07-05 00:57:48 +0100 | [diff] [blame] | 58 | if ((fdt_addr_t)regs > last_ohci_addr) |
| 59 | last_ohci_addr = (fdt_addr_t)regs; |
| 60 | |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 61 | priv->cfg = (const struct ohci_sunxi_cfg *)dev_get_driver_data(dev); |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 62 | priv->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 63 | if (IS_ERR(priv->ccm)) |
| 64 | return PTR_ERR(priv->ccm); |
| 65 | |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 66 | priv->reset0_cfg = (void *)priv->ccm + |
| 67 | priv->cfg->reset0_cfg_offset; |
| 68 | |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 69 | 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 | |
| 93 | no_phy: |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 94 | 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 Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame] | 100 | reg_mask = ((uintptr_t)regs - (SUNXI_USB1_BASE + 0x400)) / BASE_DIST; |
Jelle van der Waa | dc44fd8 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 101 | priv->ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_OHCI0; |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 102 | extra_ahb_gate_mask = priv->cfg->extra_ahb_gate_mask; |
Jelle van der Waa | dc44fd8 | 2016-02-09 23:59:33 +0100 | [diff] [blame] | 103 | priv->usb_gate_mask = CCM_USB_CTRL_OHCI0_CLK; |
Jagan Teki | 9c22aec | 2018-06-28 19:40:46 +0530 | [diff] [blame] | 104 | 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 Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 107 | |
Jagan Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 108 | setbits_le32(&priv->ccm->ahb_gate0, |
Hans de Goede | fb3bfbb | 2016-04-09 15:00:52 +0200 | [diff] [blame] | 109 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Vasily Khoruzhick | 11bb627 | 2018-06-07 19:23:41 -0700 | [diff] [blame] | 110 | setbits_le32(&priv->ccm->usb_clk_cfg, |
| 111 | priv->usb_gate_mask | priv->cfg->extra_usb_gate_mask); |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 112 | if (priv->cfg->has_reset) |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 113 | setbits_le32(priv->reset0_cfg, |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 114 | priv->ahb_gate_mask | extra_ahb_gate_mask); |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 115 | |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 116 | return ohci_register(dev, regs); |
| 117 | } |
| 118 | |
| 119 | static int ohci_usb_remove(struct udevice *dev) |
| 120 | { |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 121 | struct ohci_sunxi_priv *priv = dev_get_priv(dev); |
Andre Przywara | 0bc846a | 2018-07-05 00:57:48 +0100 | [diff] [blame] | 122 | fdt_addr_t base_addr = devfdt_get_addr(dev); |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 123 | int ret; |
| 124 | |
Jagan Teki | dd32281 | 2018-05-07 13:03:38 +0530 | [diff] [blame] | 125 | 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 Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 133 | ret = ohci_deregister(dev); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 137 | if (priv->cfg->has_reset) |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 138 | clrbits_le32(priv->reset0_cfg, priv->ahb_gate_mask); |
Andre Przywara | 0bc846a | 2018-07-05 00:57:48 +0100 | [diff] [blame] | 139 | /* |
| 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 Teki | 831cc98 | 2018-05-07 13:03:17 +0530 | [diff] [blame] | 151 | clrbits_le32(&priv->ccm->ahb_gate0, priv->ahb_gate_mask); |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 156 | static const struct ohci_sunxi_cfg sun4i_a10_cfg = { |
| 157 | .has_reset = false, |
| 158 | }; |
| 159 | |
| 160 | static const struct ohci_sunxi_cfg sun6i_a31_cfg = { |
| 161 | .has_reset = true, |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 162 | .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET, |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static const struct ohci_sunxi_cfg sun8i_h3_cfg = { |
| 166 | .has_reset = true, |
| 167 | .extra_ahb_gate_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0, |
Vasily Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 168 | .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET, |
| 169 | }; |
| 170 | |
| 171 | static const struct ohci_sunxi_cfg sun9i_a80_cfg = { |
| 172 | .has_reset = true, |
| 173 | .reset0_cfg_offset = SUN9I_AHB_RESET0_CFG_OFFSET, |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Vasily Khoruzhick | 11bb627 | 2018-06-07 19:23:41 -0700 | [diff] [blame] | 176 | static 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 Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 180 | .reset0_cfg_offset = SUN6I_AHB_RESET0_CFG_OFFSET, |
Vasily Khoruzhick | 11bb627 | 2018-06-07 19:23:41 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 183 | static const struct udevice_id ohci_usb_ids[] = { |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 184 | { |
| 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 Khoruzhick | b9f3475 | 2018-06-13 23:19:34 -0700 | [diff] [blame] | 214 | .data = (ulong)&sun9i_a80_cfg, |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 215 | }, |
| 216 | { |
| 217 | .compatible = "allwinner,sun50i-a64-ohci", |
Vasily Khoruzhick | 11bb627 | 2018-06-07 19:23:41 -0700 | [diff] [blame] | 218 | .data = (ulong)&sun50i_a64_cfg, |
Vasily Khoruzhick | 56830ce | 2018-06-07 19:23:40 -0700 | [diff] [blame] | 219 | }, |
| 220 | { /* sentinel */ } |
Hans de Goede | 6a72e80 | 2015-05-10 14:10:27 +0200 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | U_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 | }; |