Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * STiH407 family DWC3 specific Glue layer |
| 4 | * |
Patrice Chotard | 9e21624 | 2017-10-23 09:53:57 +0200 | [diff] [blame] | 5 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
Patrice Chotard | 5d9950d | 2020-12-02 18:47:30 +0100 | [diff] [blame] | 6 | * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics. |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 14 | #include <dm/lists.h> |
| 15 | #include <regmap.h> |
| 16 | #include <reset-uclass.h> |
| 17 | #include <syscon.h> |
| 18 | #include <usb.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 19 | #include <linux/printk.h> |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 20 | |
| 21 | #include <linux/usb/dwc3.h> |
| 22 | #include <linux/usb/otg.h> |
| 23 | #include <dwc3-sti-glue.h> |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
| 27 | /* |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 28 | * struct sti_dwc3_glue_plat - dwc3 STi glue driver private structure |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 29 | * @syscfg_base: addr for the glue syscfg |
| 30 | * @glue_base: addr for the glue registers |
| 31 | * @syscfg_offset: usb syscfg control offset |
| 32 | * @powerdown_ctl: rest controller for powerdown signal |
| 33 | * @softreset_ctl: reset controller for softreset signal |
| 34 | * @mode: drd static host/device config |
| 35 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 36 | struct sti_dwc3_glue_plat { |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 37 | phys_addr_t syscfg_base; |
| 38 | phys_addr_t glue_base; |
| 39 | phys_addr_t syscfg_offset; |
| 40 | struct reset_ctl powerdown_ctl; |
| 41 | struct reset_ctl softreset_ctl; |
| 42 | enum usb_dr_mode mode; |
| 43 | }; |
| 44 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 45 | static int sti_dwc3_glue_drd_init(struct sti_dwc3_glue_plat *plat) |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 46 | { |
| 47 | unsigned long val; |
| 48 | |
| 49 | val = readl(plat->syscfg_base + plat->syscfg_offset); |
| 50 | |
| 51 | val &= USB3_CONTROL_MASK; |
| 52 | |
| 53 | switch (plat->mode) { |
| 54 | case USB_DR_MODE_PERIPHERAL: |
| 55 | val &= ~(USB3_DELAY_VBUSVALID |
| 56 | | USB3_SEL_FORCE_OPMODE | USB3_FORCE_OPMODE(0x3) |
| 57 | | USB3_SEL_FORCE_DPPULLDOWN2 | USB3_FORCE_DPPULLDOWN2 |
| 58 | | USB3_SEL_FORCE_DMPULLDOWN2 | USB3_FORCE_DMPULLDOWN2); |
| 59 | |
| 60 | val |= USB3_DEVICE_NOT_HOST | USB3_FORCE_VBUSVALID; |
| 61 | break; |
| 62 | |
| 63 | case USB_DR_MODE_HOST: |
| 64 | val &= ~(USB3_DEVICE_NOT_HOST | USB3_FORCE_VBUSVALID |
| 65 | | USB3_SEL_FORCE_OPMODE | USB3_FORCE_OPMODE(0x3) |
| 66 | | USB3_SEL_FORCE_DPPULLDOWN2 | USB3_FORCE_DPPULLDOWN2 |
| 67 | | USB3_SEL_FORCE_DMPULLDOWN2 | USB3_FORCE_DMPULLDOWN2); |
| 68 | |
| 69 | val |= USB3_DELAY_VBUSVALID; |
| 70 | break; |
| 71 | |
| 72 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 73 | pr_err("Unsupported mode of operation %d\n", plat->mode); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | } |
| 76 | writel(val, plat->syscfg_base + plat->syscfg_offset); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 81 | static void sti_dwc3_glue_init(struct sti_dwc3_glue_plat *plat) |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 82 | { |
| 83 | unsigned long reg; |
| 84 | |
| 85 | reg = readl(plat->glue_base + CLKRST_CTRL); |
| 86 | |
| 87 | reg |= AUX_CLK_EN | EXT_CFG_RESET_N | XHCI_REVISION; |
| 88 | reg &= ~SW_PIPEW_RESET_N; |
| 89 | |
| 90 | writel(reg, plat->glue_base + CLKRST_CTRL); |
| 91 | |
| 92 | /* configure mux for vbus, powerpresent and bvalid signals */ |
| 93 | reg = readl(plat->glue_base + USB2_VBUS_MNGMNT_SEL1); |
| 94 | |
| 95 | reg |= SEL_OVERRIDE_VBUSVALID(USB2_VBUS_UTMIOTG) | |
| 96 | SEL_OVERRIDE_POWERPRESENT(USB2_VBUS_UTMIOTG) | |
| 97 | SEL_OVERRIDE_BVALID(USB2_VBUS_UTMIOTG); |
| 98 | |
| 99 | writel(reg, plat->glue_base + USB2_VBUS_MNGMNT_SEL1); |
| 100 | |
| 101 | setbits_le32(plat->glue_base + CLKRST_CTRL, SW_PIPEW_RESET_N); |
| 102 | } |
| 103 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 104 | static int sti_dwc3_glue_of_to_plat(struct udevice *dev) |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 105 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 106 | struct sti_dwc3_glue_plat *plat = dev_get_plat(dev); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 107 | struct udevice *syscon; |
| 108 | struct regmap *regmap; |
| 109 | int ret; |
| 110 | u32 reg[4]; |
| 111 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 112 | ret = ofnode_read_u32_array(dev_ofnode(dev), "reg", reg, |
| 113 | ARRAY_SIZE(reg)); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 114 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 115 | pr_err("unable to find st,stih407-dwc3 reg property(%d)\n", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | plat->glue_base = reg[0]; |
| 120 | plat->syscfg_offset = reg[2]; |
| 121 | |
| 122 | /* get corresponding syscon phandle */ |
| 123 | ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "st,syscfg", |
| 124 | &syscon); |
| 125 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 126 | pr_err("unable to find syscon device (%d)\n", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* get syscfg-reg base address */ |
| 131 | regmap = syscon_get_regmap(syscon); |
| 132 | if (!regmap) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 133 | pr_err("unable to find regmap\n"); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 134 | return -ENODEV; |
| 135 | } |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 136 | plat->syscfg_base = regmap->ranges[0].start; |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 137 | |
| 138 | /* get powerdown reset */ |
| 139 | ret = reset_get_by_name(dev, "powerdown", &plat->powerdown_ctl); |
| 140 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 141 | pr_err("can't get powerdown reset for %s (%d)", dev->name, ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* get softreset reset */ |
| 146 | ret = reset_get_by_name(dev, "softreset", &plat->softreset_ctl); |
| 147 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 148 | pr_err("can't get soft reset for %s (%d)", dev->name, ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 149 | |
| 150 | return ret; |
| 151 | }; |
| 152 | |
| 153 | static int sti_dwc3_glue_bind(struct udevice *dev) |
| 154 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 155 | struct sti_dwc3_glue_plat *plat = dev_get_plat(dev); |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 156 | ofnode node, dwc3_node; |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 157 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 158 | /* Find snps,dwc3 node from subnode */ |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 159 | ofnode_for_each_subnode(node, dev_ofnode(dev)) { |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 160 | if (ofnode_device_is_compatible(node, "snps,dwc3")) |
| 161 | dwc3_node = node; |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Patrice Chotard | 7c7640a | 2020-06-29 11:19:02 +0200 | [diff] [blame] | 164 | if (!ofnode_valid(dwc3_node)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 165 | pr_err("Can't find dwc3 subnode for %s\n", dev->name); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 166 | return -ENODEV; |
| 167 | } |
| 168 | |
| 169 | /* retrieve the DWC3 dual role mode */ |
| 170 | plat->mode = usb_get_dr_mode(dwc3_node); |
| 171 | if (plat->mode == USB_DR_MODE_UNKNOWN) |
| 172 | /* by default set dual role mode to HOST */ |
| 173 | plat->mode = USB_DR_MODE_HOST; |
| 174 | |
| 175 | return dm_scan_fdt_dev(dev); |
| 176 | } |
| 177 | |
| 178 | static int sti_dwc3_glue_probe(struct udevice *dev) |
| 179 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 180 | struct sti_dwc3_glue_plat *plat = dev_get_plat(dev); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 181 | int ret; |
| 182 | |
| 183 | /* deassert both powerdown and softreset */ |
| 184 | ret = reset_deassert(&plat->powerdown_ctl); |
| 185 | if (ret < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 186 | pr_err("DWC3 powerdown reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | ret = reset_deassert(&plat->softreset_ctl); |
| 191 | if (ret < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 192 | pr_err("DWC3 soft reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 193 | goto softreset_err; |
| 194 | } |
| 195 | |
| 196 | ret = sti_dwc3_glue_drd_init(plat); |
| 197 | if (ret) |
| 198 | goto init_err; |
| 199 | |
| 200 | sti_dwc3_glue_init(plat); |
| 201 | |
| 202 | return 0; |
| 203 | |
| 204 | init_err: |
| 205 | ret = reset_assert(&plat->softreset_ctl); |
| 206 | if (ret < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 207 | pr_err("DWC3 soft reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | softreset_err: |
| 212 | ret = reset_assert(&plat->powerdown_ctl); |
| 213 | if (ret < 0) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 214 | pr_err("DWC3 powerdown reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int sti_dwc3_glue_remove(struct udevice *dev) |
| 220 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 221 | struct sti_dwc3_glue_plat *plat = dev_get_plat(dev); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 222 | int ret; |
| 223 | |
| 224 | /* assert both powerdown and softreset */ |
| 225 | ret = reset_assert(&plat->powerdown_ctl); |
| 226 | if (ret < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 227 | pr_err("DWC3 powerdown reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | ret = reset_assert(&plat->softreset_ctl); |
| 232 | if (ret < 0) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 233 | pr_err("DWC3 soft reset deassert failed: %d", ret); |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static const struct udevice_id sti_dwc3_glue_ids[] = { |
| 239 | { .compatible = "st,stih407-dwc3" }, |
| 240 | { } |
| 241 | }; |
| 242 | |
| 243 | U_BOOT_DRIVER(dwc3_sti_glue) = { |
| 244 | .name = "dwc3_sti_glue", |
Patrice Chotard | 190ee83 | 2020-04-28 13:49:50 +0200 | [diff] [blame] | 245 | .id = UCLASS_NOP, |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 246 | .of_match = sti_dwc3_glue_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 247 | .of_to_plat = sti_dwc3_glue_of_to_plat, |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 248 | .probe = sti_dwc3_glue_probe, |
| 249 | .remove = sti_dwc3_glue_remove, |
| 250 | .bind = sti_dwc3_glue_bind, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 251 | .plat_auto = sizeof(struct sti_dwc3_glue_plat), |
Patrice Chotard | f2505b1 | 2017-09-05 11:04:24 +0200 | [diff] [blame] | 252 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 253 | }; |