developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2019 MediaTek, Inc. |
| 4 | * Authors: Chunfeng Yun <chunfeng.yun@mediatek.com> |
| 5 | */ |
| 6 | |
| 7 | #include <clk.h> |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 11 | #include <dm/devres.h> |
| 12 | #include <generic-phy.h> |
| 13 | #include <malloc.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 14 | #include <power/regulator.h> |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 16 | #include <usb/xhci.h> |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/compat.h> |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 19 | #include <linux/iopoll.h> |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 20 | |
| 21 | /* IPPC (IP Port Control) registers */ |
| 22 | #define IPPC_IP_PW_CTRL0 0x00 |
| 23 | #define CTRL0_IP_SW_RST BIT(0) |
| 24 | |
| 25 | #define IPPC_IP_PW_CTRL1 0x04 |
| 26 | #define CTRL1_IP_HOST_PDN BIT(0) |
| 27 | |
| 28 | #define IPPC_IP_PW_STS1 0x10 |
| 29 | #define STS1_IP_SLEEP_STS BIT(30) |
| 30 | #define STS1_U3_MAC_RST BIT(16) |
| 31 | #define STS1_XHCI_RST BIT(11) |
| 32 | #define STS1_SYS125_RST BIT(10) |
| 33 | #define STS1_REF_RST BIT(8) |
| 34 | #define STS1_SYSPLL_STABLE BIT(0) |
| 35 | |
| 36 | #define IPPC_IP_XHCI_CAP 0x24 |
| 37 | #define CAP_U3_PORT_NUM(p) ((p) & 0xff) |
| 38 | #define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff) |
| 39 | |
| 40 | #define IPPC_U3_CTRL_0P 0x30 |
| 41 | #define CTRL_U3_PORT_HOST_SEL BIT(2) |
| 42 | #define CTRL_U3_PORT_PDN BIT(1) |
| 43 | #define CTRL_U3_PORT_DIS BIT(0) |
| 44 | |
| 45 | #define IPPC_U2_CTRL_0P 0x50 |
| 46 | #define CTRL_U2_PORT_HOST_SEL BIT(2) |
| 47 | #define CTRL_U2_PORT_PDN BIT(1) |
| 48 | #define CTRL_U2_PORT_DIS BIT(0) |
| 49 | |
| 50 | #define IPPC_U3_CTRL(p) (IPPC_U3_CTRL_0P + ((p) * 0x08)) |
| 51 | #define IPPC_U2_CTRL(p) (IPPC_U2_CTRL_0P + ((p) * 0x08)) |
| 52 | |
| 53 | struct mtk_xhci { |
| 54 | struct xhci_ctrl ctrl; /* Needs to come first in this struct! */ |
| 55 | struct xhci_hccr *hcd; |
| 56 | void __iomem *ippc; |
| 57 | struct udevice *dev; |
| 58 | struct udevice *vusb33_supply; |
| 59 | struct udevice *vbus_supply; |
| 60 | struct clk_bulk clks; |
| 61 | struct phy_bulk phys; |
| 62 | int num_u2ports; |
| 63 | int num_u3ports; |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 64 | u32 u3p_dis_msk; |
| 65 | u32 u2p_dis_msk; |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static int xhci_mtk_host_enable(struct mtk_xhci *mtk) |
| 69 | { |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 70 | int u3_ports_disabed = 0; |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 71 | u32 value; |
| 72 | u32 check_val; |
| 73 | int ret; |
| 74 | int i; |
| 75 | |
| 76 | /* power on host ip */ |
| 77 | clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN); |
| 78 | |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 79 | /* power on and enable u3 ports except skipped ones */ |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 80 | for (i = 0; i < mtk->num_u3ports; i++) { |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 81 | if (BIT(i) & mtk->u3p_dis_msk) { |
| 82 | u3_ports_disabed++; |
| 83 | continue; |
| 84 | } |
| 85 | |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 86 | clrsetbits_le32(mtk->ippc + IPPC_U3_CTRL(i), |
| 87 | CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS, |
| 88 | CTRL_U3_PORT_HOST_SEL); |
| 89 | } |
| 90 | |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 91 | /* power on and enable u2 ports except skipped ones */ |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 92 | for (i = 0; i < mtk->num_u2ports; i++) { |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 93 | if (BIT(i) & mtk->u2p_dis_msk) |
| 94 | continue; |
| 95 | |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 96 | clrsetbits_le32(mtk->ippc + IPPC_U2_CTRL(i), |
| 97 | CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS, |
| 98 | CTRL_U2_PORT_HOST_SEL); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * wait for clocks to be stable, and clock domains reset to |
| 103 | * be inactive after power on and enable ports |
| 104 | */ |
| 105 | check_val = STS1_SYSPLL_STABLE | STS1_REF_RST | |
| 106 | STS1_SYS125_RST | STS1_XHCI_RST; |
| 107 | |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 108 | if (mtk->num_u3ports > u3_ports_disabed) |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 109 | check_val |= STS1_U3_MAC_RST; |
| 110 | |
| 111 | ret = readl_poll_timeout(mtk->ippc + IPPC_IP_PW_STS1, value, |
| 112 | (check_val == (value & check_val)), 20000); |
| 113 | if (ret) |
| 114 | dev_err(mtk->dev, "clocks are not stable 0x%x!\n", value); |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int xhci_mtk_host_disable(struct mtk_xhci *mtk) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | /* power down all u3 ports */ |
| 124 | for (i = 0; i < mtk->num_u3ports; i++) |
developer | 683cde3 | 2022-05-27 09:52:09 +0800 | [diff] [blame] | 125 | setbits_le32(mtk->ippc + IPPC_U3_CTRL(i), |
| 126 | CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS); |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 127 | |
| 128 | /* power down all u2 ports */ |
| 129 | for (i = 0; i < mtk->num_u2ports; i++) |
developer | 683cde3 | 2022-05-27 09:52:09 +0800 | [diff] [blame] | 130 | setbits_le32(mtk->ippc + IPPC_U2_CTRL(i), |
| 131 | CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS); |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 132 | |
| 133 | /* power down host ip */ |
| 134 | setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int xhci_mtk_ssusb_init(struct mtk_xhci *mtk) |
| 140 | { |
| 141 | u32 value; |
| 142 | |
| 143 | /* reset whole ip */ |
| 144 | setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST); |
| 145 | udelay(1); |
| 146 | clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST); |
| 147 | |
| 148 | value = readl(mtk->ippc + IPPC_IP_XHCI_CAP); |
| 149 | mtk->num_u3ports = CAP_U3_PORT_NUM(value); |
| 150 | mtk->num_u2ports = CAP_U2_PORT_NUM(value); |
| 151 | dev_info(mtk->dev, "u2p:%d, u3p:%d\n", |
| 152 | mtk->num_u2ports, mtk->num_u3ports); |
| 153 | |
| 154 | return xhci_mtk_host_enable(mtk); |
| 155 | } |
| 156 | |
| 157 | static int xhci_mtk_ofdata_get(struct mtk_xhci *mtk) |
| 158 | { |
| 159 | struct udevice *dev = mtk->dev; |
| 160 | int ret = 0; |
| 161 | |
| 162 | mtk->hcd = devfdt_remap_addr_name(dev, "mac"); |
| 163 | if (!mtk->hcd) { |
| 164 | dev_err(dev, "failed to get xHCI base address\n"); |
| 165 | return -ENXIO; |
| 166 | } |
| 167 | |
| 168 | mtk->ippc = devfdt_remap_addr_name(dev, "ippc"); |
| 169 | if (!mtk->ippc) { |
| 170 | dev_err(dev, "failed to get IPPC base address\n"); |
| 171 | return -ENXIO; |
| 172 | } |
| 173 | |
| 174 | dev_info(dev, "hcd: 0x%p, ippc: 0x%p\n", mtk->hcd, mtk->ippc); |
| 175 | |
| 176 | ret = clk_get_bulk(dev, &mtk->clks); |
| 177 | if (ret) { |
| 178 | dev_err(dev, "failed to get clocks %d!\n", ret); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | ret = device_get_supply_regulator(dev, "vusb33-supply", |
| 183 | &mtk->vusb33_supply); |
| 184 | if (ret) |
| 185 | debug("can't get vusb33 regulator %d!\n", ret); |
| 186 | |
| 187 | ret = device_get_supply_regulator(dev, "vbus-supply", |
| 188 | &mtk->vbus_supply); |
| 189 | if (ret) |
| 190 | debug("can't get vbus regulator %d!\n", ret); |
| 191 | |
developer | 37b8328 | 2020-12-23 09:52:20 +0800 | [diff] [blame] | 192 | /* optional properties to disable ports, ignore the error */ |
| 193 | dev_read_u32(dev, "mediatek,u3p-dis-msk", &mtk->u3p_dis_msk); |
| 194 | dev_read_u32(dev, "mediatek,u2p-dis-msk", &mtk->u2p_dis_msk); |
| 195 | dev_info(dev, "ports disabled mask: u3p-0x%x, u2p-0x%x\n", |
| 196 | mtk->u3p_dis_msk, mtk->u2p_dis_msk); |
| 197 | |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int xhci_mtk_ldos_enable(struct mtk_xhci *mtk) |
| 202 | { |
| 203 | int ret; |
| 204 | |
| 205 | ret = regulator_set_enable(mtk->vusb33_supply, true); |
| 206 | if (ret < 0 && ret != -ENOSYS) { |
| 207 | dev_err(mtk->dev, "failed to enable vusb33 %d!\n", ret); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | ret = regulator_set_enable(mtk->vbus_supply, true); |
| 212 | if (ret < 0 && ret != -ENOSYS) { |
| 213 | dev_err(mtk->dev, "failed to enable vbus %d!\n", ret); |
| 214 | regulator_set_enable(mtk->vusb33_supply, false); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static void xhci_mtk_ldos_disable(struct mtk_xhci *mtk) |
| 222 | { |
| 223 | regulator_set_enable(mtk->vbus_supply, false); |
| 224 | regulator_set_enable(mtk->vusb33_supply, false); |
| 225 | } |
| 226 | |
| 227 | static int xhci_mtk_phy_setup(struct mtk_xhci *mtk) |
| 228 | { |
| 229 | struct udevice *dev = mtk->dev; |
| 230 | struct phy_bulk *phys = &mtk->phys; |
| 231 | int ret; |
| 232 | |
| 233 | ret = generic_phy_get_bulk(dev, phys); |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | |
| 237 | ret = generic_phy_init_bulk(phys); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | |
| 241 | ret = generic_phy_power_on_bulk(phys); |
| 242 | if (ret) |
| 243 | generic_phy_exit_bulk(phys); |
| 244 | |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static void xhci_mtk_phy_shutdown(struct mtk_xhci *mtk) |
| 249 | { |
| 250 | generic_phy_power_off_bulk(&mtk->phys); |
| 251 | generic_phy_exit_bulk(&mtk->phys); |
| 252 | } |
| 253 | |
| 254 | static int xhci_mtk_probe(struct udevice *dev) |
| 255 | { |
| 256 | struct mtk_xhci *mtk = dev_get_priv(dev); |
| 257 | struct xhci_hcor *hcor; |
| 258 | int ret; |
| 259 | |
| 260 | mtk->dev = dev; |
| 261 | ret = xhci_mtk_ofdata_get(mtk); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | ret = xhci_mtk_ldos_enable(mtk); |
| 266 | if (ret) |
| 267 | goto ldos_err; |
| 268 | |
| 269 | ret = clk_enable_bulk(&mtk->clks); |
| 270 | if (ret) |
| 271 | goto clks_err; |
| 272 | |
| 273 | ret = xhci_mtk_phy_setup(mtk); |
| 274 | if (ret) |
| 275 | goto phys_err; |
| 276 | |
| 277 | ret = xhci_mtk_ssusb_init(mtk); |
| 278 | if (ret) |
| 279 | goto ssusb_init_err; |
| 280 | |
developer | 8039053 | 2020-09-08 18:59:57 +0200 | [diff] [blame] | 281 | mtk->ctrl.quirks = XHCI_MTK_HOST; |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 282 | hcor = (struct xhci_hcor *)((uintptr_t)mtk->hcd + |
| 283 | HC_LENGTH(xhci_readl(&mtk->hcd->cr_capbase))); |
| 284 | |
| 285 | return xhci_register(dev, mtk->hcd, hcor); |
| 286 | |
| 287 | ssusb_init_err: |
| 288 | xhci_mtk_phy_shutdown(mtk); |
| 289 | phys_err: |
| 290 | clk_disable_bulk(&mtk->clks); |
| 291 | clks_err: |
| 292 | xhci_mtk_ldos_disable(mtk); |
| 293 | ldos_err: |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int xhci_mtk_remove(struct udevice *dev) |
| 298 | { |
| 299 | struct mtk_xhci *mtk = dev_get_priv(dev); |
| 300 | |
| 301 | xhci_deregister(dev); |
| 302 | xhci_mtk_host_disable(mtk); |
| 303 | xhci_mtk_ldos_disable(mtk); |
| 304 | clk_disable_bulk(&mtk->clks); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static const struct udevice_id xhci_mtk_ids[] = { |
| 310 | { .compatible = "mediatek,mtk-xhci" }, |
| 311 | { } |
| 312 | }; |
| 313 | |
| 314 | U_BOOT_DRIVER(usb_xhci) = { |
| 315 | .name = "xhci-mtk", |
| 316 | .id = UCLASS_USB, |
| 317 | .of_match = xhci_mtk_ids, |
| 318 | .probe = xhci_mtk_probe, |
| 319 | .remove = xhci_mtk_remove, |
| 320 | .ops = &xhci_usb_ops, |
| 321 | .bind = dm_scan_fdt_dev, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 322 | .priv_auto = sizeof(struct mtk_xhci), |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 323 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 324 | }; |