Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 2 | /* |
| 3 | * phy-brcm-usb.c - Broadcom USB Phy Driver |
| 4 | * |
| 5 | * Copyright (C) 2015-2017 Broadcom |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
Rafał Miłecki | d14f4cc | 2020-12-16 15:33:04 +0100 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 15 | #include <linux/phy/phy.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/soc/brcmstb/brcmstb.h> |
| 19 | #include <dt-bindings/phy/phy.h> |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 20 | #include <linux/mfd/syscon.h> |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 21 | #include <linux/suspend.h> |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 22 | |
| 23 | #include "phy-brcm-usb-init.h" |
| 24 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 25 | static DEFINE_MUTEX(sysfs_lock); |
| 26 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 27 | enum brcm_usb_phy_id { |
| 28 | BRCM_USB_PHY_2_0 = 0, |
| 29 | BRCM_USB_PHY_3_0, |
| 30 | BRCM_USB_PHY_ID_MAX |
| 31 | }; |
| 32 | |
| 33 | struct value_to_name_map { |
| 34 | int value; |
| 35 | const char *name; |
| 36 | }; |
| 37 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 38 | struct match_chip_info { |
Rafał Miłecki | 915f1d2 | 2020-12-16 15:33:05 +0100 | [diff] [blame] | 39 | void (*init_func)(struct brcm_usb_init_params *params); |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 40 | u8 required_regs[BRCM_REGS_MAX + 1]; |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 41 | u8 optional_reg; |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 42 | }; |
| 43 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 44 | static const struct value_to_name_map brcm_dr_mode_to_name[] = { |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 45 | { USB_CTLR_MODE_HOST, "host" }, |
| 46 | { USB_CTLR_MODE_DEVICE, "peripheral" }, |
| 47 | { USB_CTLR_MODE_DRD, "drd" }, |
| 48 | { USB_CTLR_MODE_TYPEC_PD, "typec-pd" } |
| 49 | }; |
| 50 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 51 | static const struct value_to_name_map brcm_dual_mode_to_name[] = { |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 52 | { 0, "host" }, |
| 53 | { 1, "device" }, |
| 54 | { 2, "auto" }, |
| 55 | }; |
| 56 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 57 | struct brcm_usb_phy { |
| 58 | struct phy *phy; |
| 59 | unsigned int id; |
| 60 | bool inited; |
| 61 | }; |
| 62 | |
| 63 | struct brcm_usb_phy_data { |
| 64 | struct brcm_usb_init_params ini; |
| 65 | bool has_eohci; |
| 66 | bool has_xhci; |
| 67 | struct clk *usb_20_clk; |
| 68 | struct clk *usb_30_clk; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 69 | struct clk *suspend_clk; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 70 | struct mutex mutex; /* serialize phy init */ |
| 71 | int init_count; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 72 | int wake_irq; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 73 | struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX]; |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 74 | struct notifier_block pm_notifier; |
| 75 | bool pm_active; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 78 | static s8 *node_reg_names[BRCM_REGS_MAX] = { |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 79 | "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec" |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 82 | static int brcm_pm_notifier(struct notifier_block *notifier, |
| 83 | unsigned long pm_event, |
| 84 | void *unused) |
| 85 | { |
| 86 | struct brcm_usb_phy_data *priv = |
| 87 | container_of(notifier, struct brcm_usb_phy_data, pm_notifier); |
| 88 | |
| 89 | switch (pm_event) { |
| 90 | case PM_HIBERNATION_PREPARE: |
| 91 | case PM_SUSPEND_PREPARE: |
| 92 | priv->pm_active = true; |
| 93 | break; |
| 94 | case PM_POST_RESTORE: |
| 95 | case PM_POST_HIBERNATION: |
| 96 | case PM_POST_SUSPEND: |
| 97 | priv->pm_active = false; |
| 98 | break; |
| 99 | } |
| 100 | return NOTIFY_DONE; |
| 101 | } |
| 102 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 103 | static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id) |
| 104 | { |
| 105 | struct phy *gphy = dev_id; |
| 106 | |
| 107 | pm_wakeup_event(&gphy->dev, 0); |
| 108 | |
| 109 | return IRQ_HANDLED; |
| 110 | } |
| 111 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 112 | static int brcm_usb_phy_init(struct phy *gphy) |
| 113 | { |
| 114 | struct brcm_usb_phy *phy = phy_get_drvdata(gphy); |
| 115 | struct brcm_usb_phy_data *priv = |
| 116 | container_of(phy, struct brcm_usb_phy_data, phys[phy->id]); |
| 117 | |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 118 | if (priv->pm_active) |
| 119 | return 0; |
| 120 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 121 | /* |
| 122 | * Use a lock to make sure a second caller waits until |
| 123 | * the base phy is inited before using it. |
| 124 | */ |
| 125 | mutex_lock(&priv->mutex); |
| 126 | if (priv->init_count++ == 0) { |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 127 | clk_prepare_enable(priv->usb_20_clk); |
| 128 | clk_prepare_enable(priv->usb_30_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 129 | clk_prepare_enable(priv->suspend_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 130 | brcm_usb_init_common(&priv->ini); |
| 131 | } |
| 132 | mutex_unlock(&priv->mutex); |
| 133 | if (phy->id == BRCM_USB_PHY_2_0) |
| 134 | brcm_usb_init_eohci(&priv->ini); |
| 135 | else if (phy->id == BRCM_USB_PHY_3_0) |
| 136 | brcm_usb_init_xhci(&priv->ini); |
| 137 | phy->inited = true; |
| 138 | dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id, |
| 139 | priv->init_count); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int brcm_usb_phy_exit(struct phy *gphy) |
| 145 | { |
| 146 | struct brcm_usb_phy *phy = phy_get_drvdata(gphy); |
| 147 | struct brcm_usb_phy_data *priv = |
| 148 | container_of(phy, struct brcm_usb_phy_data, phys[phy->id]); |
| 149 | |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 150 | if (priv->pm_active) |
| 151 | return 0; |
| 152 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 153 | dev_dbg(&gphy->dev, "EXIT\n"); |
| 154 | if (phy->id == BRCM_USB_PHY_2_0) |
| 155 | brcm_usb_uninit_eohci(&priv->ini); |
| 156 | if (phy->id == BRCM_USB_PHY_3_0) |
| 157 | brcm_usb_uninit_xhci(&priv->ini); |
| 158 | |
| 159 | /* If both xhci and eohci are gone, reset everything else */ |
| 160 | mutex_lock(&priv->mutex); |
| 161 | if (--priv->init_count == 0) { |
| 162 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 163 | clk_disable_unprepare(priv->usb_20_clk); |
| 164 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 165 | clk_disable_unprepare(priv->suspend_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 166 | } |
| 167 | mutex_unlock(&priv->mutex); |
| 168 | phy->inited = false; |
| 169 | return 0; |
| 170 | } |
| 171 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 172 | static const struct phy_ops brcm_usb_phy_ops = { |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 173 | .init = brcm_usb_phy_init, |
| 174 | .exit = brcm_usb_phy_exit, |
| 175 | .owner = THIS_MODULE, |
| 176 | }; |
| 177 | |
| 178 | static struct phy *brcm_usb_phy_xlate(struct device *dev, |
| 179 | struct of_phandle_args *args) |
| 180 | { |
| 181 | struct brcm_usb_phy_data *data = dev_get_drvdata(dev); |
| 182 | |
| 183 | /* |
| 184 | * values 0 and 1 are for backward compatibility with |
| 185 | * device tree nodes from older bootloaders. |
| 186 | */ |
| 187 | switch (args->args[0]) { |
| 188 | case 0: |
| 189 | case PHY_TYPE_USB2: |
| 190 | if (data->phys[BRCM_USB_PHY_2_0].phy) |
| 191 | return data->phys[BRCM_USB_PHY_2_0].phy; |
| 192 | dev_warn(dev, "Error, 2.0 Phy not found\n"); |
| 193 | break; |
| 194 | case 1: |
| 195 | case PHY_TYPE_USB3: |
| 196 | if (data->phys[BRCM_USB_PHY_3_0].phy) |
| 197 | return data->phys[BRCM_USB_PHY_3_0].phy; |
| 198 | dev_warn(dev, "Error, 3.0 Phy not found\n"); |
| 199 | break; |
| 200 | } |
| 201 | return ERR_PTR(-ENODEV); |
| 202 | } |
| 203 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 204 | static int name_to_value(const struct value_to_name_map *table, int count, |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 205 | const char *name, int *value) |
| 206 | { |
| 207 | int x; |
| 208 | |
| 209 | *value = 0; |
| 210 | for (x = 0; x < count; x++) { |
| 211 | if (sysfs_streq(name, table[x].name)) { |
| 212 | *value = x; |
| 213 | return 0; |
| 214 | } |
| 215 | } |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 219 | static const char *value_to_name(const struct value_to_name_map *table, int count, |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 220 | int value) |
| 221 | { |
| 222 | if (value >= count) |
| 223 | return "unknown"; |
| 224 | return table[value].name; |
| 225 | } |
| 226 | |
| 227 | static ssize_t dr_mode_show(struct device *dev, |
| 228 | struct device_attribute *attr, |
| 229 | char *buf) |
| 230 | { |
| 231 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 232 | |
| 233 | return sprintf(buf, "%s\n", |
| 234 | value_to_name(&brcm_dr_mode_to_name[0], |
| 235 | ARRAY_SIZE(brcm_dr_mode_to_name), |
| 236 | priv->ini.mode)); |
| 237 | } |
| 238 | static DEVICE_ATTR_RO(dr_mode); |
| 239 | |
| 240 | static ssize_t dual_select_store(struct device *dev, |
| 241 | struct device_attribute *attr, |
| 242 | const char *buf, size_t len) |
| 243 | { |
| 244 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 245 | int value; |
| 246 | int res; |
| 247 | |
| 248 | mutex_lock(&sysfs_lock); |
| 249 | res = name_to_value(&brcm_dual_mode_to_name[0], |
| 250 | ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value); |
| 251 | if (!res) { |
Al Cooper | 94583a4 | 2020-01-03 13:18:03 -0500 | [diff] [blame] | 252 | brcm_usb_set_dual_select(&priv->ini, value); |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 253 | res = len; |
| 254 | } |
| 255 | mutex_unlock(&sysfs_lock); |
| 256 | return res; |
| 257 | } |
| 258 | |
| 259 | static ssize_t dual_select_show(struct device *dev, |
| 260 | struct device_attribute *attr, |
| 261 | char *buf) |
| 262 | { |
| 263 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 264 | int value; |
| 265 | |
| 266 | mutex_lock(&sysfs_lock); |
Al Cooper | 94583a4 | 2020-01-03 13:18:03 -0500 | [diff] [blame] | 267 | value = brcm_usb_get_dual_select(&priv->ini); |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 268 | mutex_unlock(&sysfs_lock); |
| 269 | return sprintf(buf, "%s\n", |
| 270 | value_to_name(&brcm_dual_mode_to_name[0], |
| 271 | ARRAY_SIZE(brcm_dual_mode_to_name), |
| 272 | value)); |
| 273 | } |
| 274 | static DEVICE_ATTR_RW(dual_select); |
| 275 | |
| 276 | static struct attribute *brcm_usb_phy_attrs[] = { |
| 277 | &dev_attr_dr_mode.attr, |
| 278 | &dev_attr_dual_select.attr, |
| 279 | NULL |
| 280 | }; |
| 281 | |
| 282 | static const struct attribute_group brcm_usb_phy_group = { |
| 283 | .attrs = brcm_usb_phy_attrs, |
| 284 | }; |
| 285 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 286 | static const struct match_chip_info chip_info_7216 = { |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 287 | .init_func = &brcm_usb_dvr_init_7216, |
| 288 | .required_regs = { |
| 289 | BRCM_REGS_CTRL, |
| 290 | BRCM_REGS_XHCI_EC, |
| 291 | BRCM_REGS_XHCI_GBL, |
| 292 | -1, |
| 293 | }, |
| 294 | }; |
| 295 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 296 | static const struct match_chip_info chip_info_7211b0 = { |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 297 | .init_func = &brcm_usb_dvr_init_7211b0, |
| 298 | .required_regs = { |
| 299 | BRCM_REGS_CTRL, |
| 300 | BRCM_REGS_XHCI_EC, |
| 301 | BRCM_REGS_XHCI_GBL, |
| 302 | BRCM_REGS_USB_PHY, |
| 303 | BRCM_REGS_USB_MDIO, |
| 304 | -1, |
| 305 | }, |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 306 | .optional_reg = BRCM_REGS_BDC_EC, |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 307 | }; |
| 308 | |
Rikard Falkeborn | c79cc3d | 2020-05-16 14:04:41 +0200 | [diff] [blame] | 309 | static const struct match_chip_info chip_info_7445 = { |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 310 | .init_func = &brcm_usb_dvr_init_7445, |
| 311 | .required_regs = { |
| 312 | BRCM_REGS_CTRL, |
| 313 | BRCM_REGS_XHCI_EC, |
| 314 | -1, |
| 315 | }, |
| 316 | }; |
| 317 | |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 318 | static const struct of_device_id brcm_usb_dt_ids[] = { |
| 319 | { |
Rafał Miłecki | 4b402fa | 2021-01-06 21:58:38 +0100 | [diff] [blame] | 320 | .compatible = "brcm,bcm4908-usb-phy", |
| 321 | .data = &chip_info_7445, |
| 322 | }, |
| 323 | { |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 324 | .compatible = "brcm,bcm7216-usb-phy", |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 325 | .data = &chip_info_7216, |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 326 | }, |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 327 | { |
| 328 | .compatible = "brcm,bcm7211-usb-phy", |
| 329 | .data = &chip_info_7211b0, |
| 330 | }, |
| 331 | { |
| 332 | .compatible = "brcm,brcmstb-usb-phy", |
| 333 | .data = &chip_info_7445, |
| 334 | }, |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 335 | { /* sentinel */ } |
| 336 | }; |
| 337 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 338 | static int brcm_usb_get_regs(struct platform_device *pdev, |
| 339 | enum brcmusb_reg_sel regs, |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 340 | struct brcm_usb_init_params *ini, |
| 341 | bool optional) |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 342 | { |
| 343 | struct resource *res; |
| 344 | |
| 345 | /* Older DT nodes have ctrl and optional xhci_ec by index only */ |
| 346 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 347 | node_reg_names[regs]); |
| 348 | if (res == NULL) { |
| 349 | if (regs == BRCM_REGS_CTRL) { |
| 350 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 351 | } else if (regs == BRCM_REGS_XHCI_EC) { |
| 352 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 353 | /* XHCI_EC registers are optional */ |
| 354 | if (res == NULL) |
| 355 | return 0; |
| 356 | } |
| 357 | if (res == NULL) { |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 358 | if (optional) { |
| 359 | dev_dbg(&pdev->dev, |
| 360 | "Optional reg %s not found\n", |
| 361 | node_reg_names[regs]); |
| 362 | return 0; |
| 363 | } |
| 364 | dev_err(&pdev->dev, "can't get %s base addr\n", |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 365 | node_reg_names[regs]); |
| 366 | return 1; |
| 367 | } |
| 368 | } |
| 369 | ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res); |
| 370 | if (IS_ERR(ini->regs[regs])) { |
| 371 | dev_err(&pdev->dev, "can't map %s register space\n", |
| 372 | node_reg_names[regs]); |
| 373 | return 1; |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 378 | static int brcm_usb_phy_dvr_init(struct platform_device *pdev, |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 379 | struct brcm_usb_phy_data *priv, |
| 380 | struct device_node *dn) |
| 381 | { |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 382 | struct device *dev = &pdev->dev; |
| 383 | struct phy *gphy = NULL; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 384 | int err; |
| 385 | |
| 386 | priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb"); |
| 387 | if (IS_ERR(priv->usb_20_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 388 | if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER) |
| 389 | return -EPROBE_DEFER; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 390 | dev_info(dev, "Clock not found in Device Tree\n"); |
| 391 | priv->usb_20_clk = NULL; |
| 392 | } |
| 393 | err = clk_prepare_enable(priv->usb_20_clk); |
| 394 | if (err) |
| 395 | return err; |
| 396 | |
| 397 | if (priv->has_eohci) { |
| 398 | gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops); |
| 399 | if (IS_ERR(gphy)) { |
| 400 | dev_err(dev, "failed to create EHCI/OHCI PHY\n"); |
| 401 | return PTR_ERR(gphy); |
| 402 | } |
| 403 | priv->phys[BRCM_USB_PHY_2_0].phy = gphy; |
| 404 | priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0; |
| 405 | phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]); |
| 406 | } |
| 407 | |
| 408 | if (priv->has_xhci) { |
| 409 | gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops); |
| 410 | if (IS_ERR(gphy)) { |
| 411 | dev_err(dev, "failed to create XHCI PHY\n"); |
| 412 | return PTR_ERR(gphy); |
| 413 | } |
| 414 | priv->phys[BRCM_USB_PHY_3_0].phy = gphy; |
| 415 | priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0; |
| 416 | phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]); |
| 417 | |
| 418 | priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3"); |
| 419 | if (IS_ERR(priv->usb_30_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 420 | if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER) |
| 421 | return -EPROBE_DEFER; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 422 | dev_info(dev, |
| 423 | "USB3.0 clock not found in Device Tree\n"); |
| 424 | priv->usb_30_clk = NULL; |
| 425 | } |
| 426 | err = clk_prepare_enable(priv->usb_30_clk); |
| 427 | if (err) |
| 428 | return err; |
| 429 | } |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 430 | |
| 431 | priv->suspend_clk = clk_get(dev, "usb0_freerun"); |
| 432 | if (IS_ERR(priv->suspend_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 433 | if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER) |
| 434 | return -EPROBE_DEFER; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 435 | dev_err(dev, "Suspend Clock not found in Device Tree\n"); |
| 436 | priv->suspend_clk = NULL; |
| 437 | } |
| 438 | |
| 439 | priv->wake_irq = platform_get_irq_byname(pdev, "wake"); |
| 440 | if (priv->wake_irq < 0) |
| 441 | priv->wake_irq = platform_get_irq_byname(pdev, "wakeup"); |
| 442 | if (priv->wake_irq >= 0) { |
| 443 | err = devm_request_irq(dev, priv->wake_irq, |
| 444 | brcm_usb_phy_wake_isr, 0, |
| 445 | dev_name(dev), gphy); |
| 446 | if (err < 0) |
| 447 | return err; |
| 448 | device_set_wakeup_capable(dev, 1); |
| 449 | } else { |
| 450 | dev_info(dev, |
| 451 | "Wake interrupt missing, system wake not supported\n"); |
| 452 | } |
| 453 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int brcm_usb_phy_probe(struct platform_device *pdev) |
| 458 | { |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 459 | struct device *dev = &pdev->dev; |
| 460 | struct brcm_usb_phy_data *priv; |
| 461 | struct phy_provider *phy_provider; |
| 462 | struct device_node *dn = pdev->dev.of_node; |
| 463 | int err; |
| 464 | const char *mode; |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 465 | const struct match_chip_info *info; |
| 466 | struct regmap *rmap; |
| 467 | int x; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 468 | |
| 469 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 470 | if (!priv) |
| 471 | return -ENOMEM; |
| 472 | platform_set_drvdata(pdev, priv); |
| 473 | |
| 474 | priv->ini.family_id = brcmstb_get_family_id(); |
| 475 | priv->ini.product_id = brcmstb_get_product_id(); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 476 | |
Rafał Miłecki | d14f4cc | 2020-12-16 15:33:04 +0100 | [diff] [blame] | 477 | info = of_device_get_match_data(&pdev->dev); |
| 478 | if (!info) |
| 479 | return -ENOENT; |
Rafał Miłecki | 915f1d2 | 2020-12-16 15:33:05 +0100 | [diff] [blame] | 480 | |
| 481 | info->init_func(&priv->ini); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 482 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 483 | dev_dbg(dev, "Best mapping table is for %s\n", |
| 484 | priv->ini.family_name); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 485 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 486 | of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp); |
| 487 | of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc); |
| 488 | |
| 489 | priv->ini.mode = USB_CTLR_MODE_HOST; |
| 490 | err = of_property_read_string(dn, "dr_mode", &mode); |
| 491 | if (err == 0) { |
| 492 | name_to_value(&brcm_dr_mode_to_name[0], |
| 493 | ARRAY_SIZE(brcm_dr_mode_to_name), |
| 494 | mode, &priv->ini.mode); |
| 495 | } |
Al Cooper | 5e498ff | 2017-12-27 14:28:48 -0500 | [diff] [blame] | 496 | if (of_property_read_bool(dn, "brcm,has-xhci")) |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 497 | priv->has_xhci = true; |
Al Cooper | 5e498ff | 2017-12-27 14:28:48 -0500 | [diff] [blame] | 498 | if (of_property_read_bool(dn, "brcm,has-eohci")) |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 499 | priv->has_eohci = true; |
| 500 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 501 | for (x = 0; x < BRCM_REGS_MAX; x++) { |
| 502 | if (info->required_regs[x] >= BRCM_REGS_MAX) |
| 503 | break; |
| 504 | |
| 505 | err = brcm_usb_get_regs(pdev, info->required_regs[x], |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame] | 506 | &priv->ini, false); |
| 507 | if (err) |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | if (info->optional_reg) { |
| 511 | err = brcm_usb_get_regs(pdev, info->optional_reg, |
| 512 | &priv->ini, true); |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 513 | if (err) |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 517 | err = brcm_usb_phy_dvr_init(pdev, priv, dn); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 518 | if (err) |
| 519 | return err; |
| 520 | |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 521 | priv->pm_notifier.notifier_call = brcm_pm_notifier; |
| 522 | register_pm_notifier(&priv->pm_notifier); |
| 523 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 524 | mutex_init(&priv->mutex); |
| 525 | |
| 526 | /* make sure invert settings are correct */ |
| 527 | brcm_usb_init_ipp(&priv->ini); |
| 528 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 529 | /* |
| 530 | * Create sysfs entries for mode. |
| 531 | * Remove "dual_select" attribute if not in dual mode |
| 532 | */ |
| 533 | if (priv->ini.mode != USB_CTLR_MODE_DRD) |
| 534 | brcm_usb_phy_attrs[1] = NULL; |
| 535 | err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group); |
| 536 | if (err) |
| 537 | dev_warn(dev, "Error creating sysfs attributes\n"); |
| 538 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 539 | /* Get piarbctl syscon if it exists */ |
| 540 | rmap = syscon_regmap_lookup_by_phandle(dev->of_node, |
| 541 | "syscon-piarbctl"); |
| 542 | if (IS_ERR(rmap)) |
| 543 | rmap = syscon_regmap_lookup_by_phandle(dev->of_node, |
| 544 | "brcm,syscon-piarbctl"); |
| 545 | if (!IS_ERR(rmap)) |
| 546 | priv->ini.syscon_piarbctl = rmap; |
| 547 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 548 | /* start with everything off */ |
| 549 | if (priv->has_xhci) |
| 550 | brcm_usb_uninit_xhci(&priv->ini); |
| 551 | if (priv->has_eohci) |
| 552 | brcm_usb_uninit_eohci(&priv->ini); |
| 553 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 554 | clk_disable_unprepare(priv->usb_20_clk); |
| 555 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 556 | |
| 557 | phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 558 | |
zhong jiang | 5286481 | 2018-08-16 23:58:54 +0800 | [diff] [blame] | 559 | return PTR_ERR_OR_ZERO(phy_provider); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 562 | static int brcm_usb_phy_remove(struct platform_device *pdev) |
| 563 | { |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 564 | struct brcm_usb_phy_data *priv = dev_get_drvdata(&pdev->dev); |
| 565 | |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 566 | sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group); |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 567 | unregister_pm_notifier(&priv->pm_notifier); |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 572 | #ifdef CONFIG_PM_SLEEP |
| 573 | static int brcm_usb_phy_suspend(struct device *dev) |
| 574 | { |
| 575 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 576 | |
| 577 | if (priv->init_count) { |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 578 | dev_dbg(dev, "SUSPEND\n"); |
Al Cooper | b0c0b66 | 2020-01-03 13:18:11 -0500 | [diff] [blame] | 579 | priv->ini.wake_enabled = device_may_wakeup(dev); |
Al Cooper | 6597af4 | 2020-01-03 13:18:01 -0500 | [diff] [blame] | 580 | if (priv->phys[BRCM_USB_PHY_3_0].inited) |
| 581 | brcm_usb_uninit_xhci(&priv->ini); |
| 582 | if (priv->phys[BRCM_USB_PHY_2_0].inited) |
| 583 | brcm_usb_uninit_eohci(&priv->ini); |
| 584 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | b0c0b66 | 2020-01-03 13:18:11 -0500 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * Handle the clocks unless needed for wake. This has |
| 588 | * to work for both older XHCI->3.0-clks, EOHCI->2.0-clks |
| 589 | * and newer XHCI->2.0-clks/3.0-clks. |
| 590 | */ |
| 591 | |
| 592 | if (!priv->ini.suspend_with_clocks) { |
| 593 | if (priv->phys[BRCM_USB_PHY_3_0].inited) |
| 594 | clk_disable_unprepare(priv->usb_30_clk); |
| 595 | if (priv->phys[BRCM_USB_PHY_2_0].inited || |
| 596 | !priv->has_eohci) |
| 597 | clk_disable_unprepare(priv->usb_20_clk); |
| 598 | } |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 599 | if (priv->wake_irq >= 0) |
| 600 | enable_irq_wake(priv->wake_irq); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int brcm_usb_phy_resume(struct device *dev) |
| 606 | { |
| 607 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 608 | |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 609 | clk_prepare_enable(priv->usb_20_clk); |
| 610 | clk_prepare_enable(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 611 | brcm_usb_init_ipp(&priv->ini); |
| 612 | |
| 613 | /* |
| 614 | * Initialize anything that was previously initialized. |
| 615 | * Uninitialize anything that wasn't previously initialized. |
| 616 | */ |
| 617 | if (priv->init_count) { |
Al Cooper | 42fed57 | 2021-12-01 13:06:51 -0500 | [diff] [blame] | 618 | dev_dbg(dev, "RESUME\n"); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 619 | if (priv->wake_irq >= 0) |
| 620 | disable_irq_wake(priv->wake_irq); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 621 | brcm_usb_init_common(&priv->ini); |
| 622 | if (priv->phys[BRCM_USB_PHY_2_0].inited) { |
| 623 | brcm_usb_init_eohci(&priv->ini); |
| 624 | } else if (priv->has_eohci) { |
| 625 | brcm_usb_uninit_eohci(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 626 | clk_disable_unprepare(priv->usb_20_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 627 | } |
| 628 | if (priv->phys[BRCM_USB_PHY_3_0].inited) { |
| 629 | brcm_usb_init_xhci(&priv->ini); |
| 630 | } else if (priv->has_xhci) { |
| 631 | brcm_usb_uninit_xhci(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 632 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | b0c0b66 | 2020-01-03 13:18:11 -0500 | [diff] [blame] | 633 | if (!priv->has_eohci) |
| 634 | clk_disable_unprepare(priv->usb_20_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 635 | } |
| 636 | } else { |
| 637 | if (priv->has_xhci) |
| 638 | brcm_usb_uninit_xhci(&priv->ini); |
| 639 | if (priv->has_eohci) |
| 640 | brcm_usb_uninit_eohci(&priv->ini); |
| 641 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 642 | clk_disable_unprepare(priv->usb_20_clk); |
| 643 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 644 | } |
Al Cooper | b0c0b66 | 2020-01-03 13:18:11 -0500 | [diff] [blame] | 645 | priv->ini.wake_enabled = false; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | #endif /* CONFIG_PM_SLEEP */ |
| 649 | |
| 650 | static const struct dev_pm_ops brcm_usb_phy_pm_ops = { |
| 651 | SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume) |
| 652 | }; |
| 653 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 654 | MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids); |
| 655 | |
| 656 | static struct platform_driver brcm_usb_driver = { |
| 657 | .probe = brcm_usb_phy_probe, |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 658 | .remove = brcm_usb_phy_remove, |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 659 | .driver = { |
| 660 | .name = "brcmstb-usb-phy", |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 661 | .pm = &brcm_usb_phy_pm_ops, |
| 662 | .of_match_table = brcm_usb_dt_ids, |
| 663 | }, |
| 664 | }; |
| 665 | |
| 666 | module_platform_driver(brcm_usb_driver); |
| 667 | |
| 668 | MODULE_ALIAS("platform:brcmstb-usb-phy"); |
| 669 | MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>"); |
| 670 | MODULE_DESCRIPTION("BRCM USB PHY driver"); |
| 671 | MODULE_LICENSE("GPL v2"); |