Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Cadence USBSS DRD Driver. |
| 4 | * |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 5 | * Copyright (C) 2018-2020 Cadence. |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 6 | * Copyright (C) 2019 Texas Instruments |
| 7 | * |
| 8 | * Author: Pawel Laszczak <pawell@cadence.com> |
| 9 | * Roger Quadros <rogerq@ti.com> |
| 10 | * |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/iopoll.h> |
| 16 | #include <linux/usb/otg.h> |
Peter Chen | 9f65013 | 2020-09-01 10:33:51 +0800 | [diff] [blame] | 17 | #include <linux/phy/phy.h> |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 18 | |
| 19 | #include "gadget.h" |
| 20 | #include "drd.h" |
| 21 | #include "core.h" |
| 22 | |
| 23 | /** |
| 24 | * cdns3_set_mode - change mode of OTG Core |
| 25 | * @cdns: pointer to context structure |
| 26 | * @mode: selected mode from cdns_role |
| 27 | * |
| 28 | * Returns 0 on success otherwise negative errno |
| 29 | */ |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 30 | static int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode) |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 31 | { |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 32 | u32 __iomem *override_reg; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 33 | u32 reg; |
| 34 | |
| 35 | switch (mode) { |
| 36 | case USB_DR_MODE_PERIPHERAL: |
| 37 | break; |
| 38 | case USB_DR_MODE_HOST: |
| 39 | break; |
| 40 | case USB_DR_MODE_OTG: |
| 41 | dev_dbg(cdns->dev, "Set controller to OTG mode\n"); |
Pawel Laszczak | 2eae2df | 2020-09-15 14:45:43 +0300 | [diff] [blame] | 42 | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 43 | if (cdns->version == CDNSP_CONTROLLER_V2) |
| 44 | override_reg = &cdns->otg_cdnsp_regs->override; |
| 45 | else if (cdns->version == CDNS3_CONTROLLER_V1) |
| 46 | override_reg = &cdns->otg_v1_regs->override; |
| 47 | else |
| 48 | override_reg = &cdns->otg_v0_regs->ctrl1; |
| 49 | |
| 50 | reg = readl(override_reg); |
| 51 | |
| 52 | if (cdns->version != CDNS3_CONTROLLER_V0) |
| 53 | reg |= OVERRIDE_IDPULLUP; |
| 54 | else |
| 55 | reg |= OVERRIDE_IDPULLUP_V0; |
| 56 | |
| 57 | writel(reg, override_reg); |
| 58 | |
| 59 | if (cdns->version == CDNS3_CONTROLLER_V1) { |
Pawel Laszczak | 2eae2df | 2020-09-15 14:45:43 +0300 | [diff] [blame] | 60 | /* |
| 61 | * Enable work around feature built into the |
| 62 | * controller to address issue with RX Sensitivity |
| 63 | * est (EL_17) for USB2 PHY. The issue only occures |
| 64 | * for 0x0002450D controller version. |
| 65 | */ |
| 66 | if (cdns->phyrst_a_enable) { |
| 67 | reg = readl(&cdns->otg_v1_regs->phyrst_cfg); |
| 68 | reg |= PHYRST_CFG_PHYRST_A_ENABLE; |
| 69 | writel(reg, &cdns->otg_v1_regs->phyrst_cfg); |
| 70 | } |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Hardware specification says: "ID_VALUE must be valid within |
| 75 | * 50ms after idpullup is set to '1" so driver must wait |
| 76 | * 50ms before reading this pin. |
| 77 | */ |
| 78 | usleep_range(50000, 60000); |
| 79 | break; |
| 80 | default: |
| 81 | dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode); |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 85 | return 0; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int cdns3_get_id(struct cdns3 *cdns) |
| 89 | { |
| 90 | int id; |
| 91 | |
| 92 | id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE; |
| 93 | dev_dbg(cdns->dev, "OTG ID: %d", id); |
| 94 | |
| 95 | return id; |
| 96 | } |
| 97 | |
| 98 | int cdns3_get_vbus(struct cdns3 *cdns) |
| 99 | { |
| 100 | int vbus; |
| 101 | |
| 102 | vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID); |
| 103 | dev_dbg(cdns->dev, "OTG VBUS: %d", vbus); |
| 104 | |
| 105 | return vbus; |
| 106 | } |
| 107 | |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 108 | bool cdns3_is_host(struct cdns3 *cdns) |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 109 | { |
| 110 | if (cdns->dr_mode == USB_DR_MODE_HOST) |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 111 | return true; |
Pawel Laszczak | 08c35dd | 2020-07-13 12:05:51 +0200 | [diff] [blame] | 112 | else if (cdns3_get_id(cdns) == CDNS3_ID_HOST) |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 113 | return true; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 114 | |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 115 | return false; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 118 | bool cdns3_is_device(struct cdns3 *cdns) |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 119 | { |
| 120 | if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL) |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 121 | return true; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 122 | else if (cdns->dr_mode == USB_DR_MODE_OTG) |
Pawel Laszczak | 08c35dd | 2020-07-13 12:05:51 +0200 | [diff] [blame] | 123 | if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL) |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 124 | return true; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 125 | |
Pawel Laszczak | 2452584 | 2020-07-13 12:05:50 +0200 | [diff] [blame] | 126 | return false; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * cdns3_otg_disable_irq - Disable all OTG interrupts |
| 131 | * @cdns: Pointer to controller context structure |
| 132 | */ |
| 133 | static void cdns3_otg_disable_irq(struct cdns3 *cdns) |
| 134 | { |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 135 | writel(0, &cdns->otg_irq_regs->ien); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * cdns3_otg_enable_irq - enable id and sess_valid interrupts |
| 140 | * @cdns: Pointer to controller context structure |
| 141 | */ |
| 142 | static void cdns3_otg_enable_irq(struct cdns3 *cdns) |
| 143 | { |
| 144 | writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 145 | OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 149 | * cdns3_drd_host_on - start host. |
| 150 | * @cdns: Pointer to controller context structure. |
| 151 | * |
| 152 | * Returns 0 on success otherwise negative errno. |
| 153 | */ |
| 154 | int cdns3_drd_host_on(struct cdns3 *cdns) |
| 155 | { |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 156 | u32 val, ready_bit; |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 157 | int ret; |
| 158 | |
| 159 | /* Enable host mode. */ |
| 160 | writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS, |
| 161 | &cdns->otg_regs->cmd); |
| 162 | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 163 | if (cdns->version == CDNSP_CONTROLLER_V2) |
| 164 | ready_bit = OTGSTS_CDNSP_XHCI_READY; |
| 165 | else |
| 166 | ready_bit = OTGSTS_CDNS3_XHCI_READY; |
| 167 | |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 168 | dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n"); |
| 169 | ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 170 | val & ready_bit, 1, 100000); |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 171 | |
| 172 | if (ret) |
| 173 | dev_err(cdns->dev, "timeout waiting for xhci_ready\n"); |
| 174 | |
Peter Chen | 9f65013 | 2020-09-01 10:33:51 +0800 | [diff] [blame] | 175 | phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST); |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * cdns3_drd_host_off - stop host. |
| 181 | * @cdns: Pointer to controller context structure. |
| 182 | */ |
| 183 | void cdns3_drd_host_off(struct cdns3 *cdns) |
| 184 | { |
| 185 | u32 val; |
| 186 | |
| 187 | writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP | |
| 188 | OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF, |
| 189 | &cdns->otg_regs->cmd); |
| 190 | |
| 191 | /* Waiting till H_IDLE state.*/ |
| 192 | readl_poll_timeout_atomic(&cdns->otg_regs->state, val, |
| 193 | !(val & OTGSTATE_HOST_STATE_MASK), |
| 194 | 1, 2000000); |
Peter Chen | 9f65013 | 2020-09-01 10:33:51 +0800 | [diff] [blame] | 195 | phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * cdns3_drd_gadget_on - start gadget. |
| 200 | * @cdns: Pointer to controller context structure. |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 201 | * |
| 202 | * Returns 0 on success otherwise negative errno |
| 203 | */ |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 204 | int cdns3_drd_gadget_on(struct cdns3 *cdns) |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 205 | { |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 206 | u32 reg = OTGCMD_OTG_DIS; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 207 | u32 ready_bit; |
| 208 | int ret, val; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 209 | |
| 210 | /* switch OTG core */ |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 211 | writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 212 | |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 213 | dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n"); |
| 214 | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 215 | if (cdns->version == CDNSP_CONTROLLER_V2) |
| 216 | ready_bit = OTGSTS_CDNSP_DEV_READY; |
| 217 | else |
| 218 | ready_bit = OTGSTS_CDNS3_DEV_READY; |
| 219 | |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 220 | ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val, |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 221 | val & ready_bit, 1, 100000); |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 222 | if (ret) { |
| 223 | dev_err(cdns->dev, "timeout waiting for dev_ready\n"); |
| 224 | return ret; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Peter Chen | 9f65013 | 2020-09-01 10:33:51 +0800 | [diff] [blame] | 227 | phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /** |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 232 | * cdns3_drd_gadget_off - stop gadget. |
| 233 | * @cdns: Pointer to controller context structure. |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 234 | */ |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 235 | void cdns3_drd_gadget_off(struct cdns3 *cdns) |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 236 | { |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 237 | u32 val; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 238 | |
Pawel Laszczak | b2aeb6d | 2020-07-13 12:05:54 +0200 | [diff] [blame] | 239 | /* |
| 240 | * Driver should wait at least 10us after disabling Device |
| 241 | * before turning-off Device (DEV_BUS_DROP). |
| 242 | */ |
| 243 | usleep_range(20, 30); |
| 244 | writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP | |
| 245 | OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF, |
| 246 | &cdns->otg_regs->cmd); |
| 247 | /* Waiting till DEV_IDLE state.*/ |
| 248 | readl_poll_timeout_atomic(&cdns->otg_regs->state, val, |
| 249 | !(val & OTGSTATE_DEV_STATE_MASK), |
| 250 | 1, 2000000); |
Peter Chen | 9f65013 | 2020-09-01 10:33:51 +0800 | [diff] [blame] | 251 | phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * cdns3_init_otg_mode - initialize drd controller |
| 256 | * @cdns: Pointer to controller context structure |
| 257 | * |
| 258 | * Returns 0 on success otherwise negative errno |
| 259 | */ |
| 260 | static int cdns3_init_otg_mode(struct cdns3 *cdns) |
| 261 | { |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 262 | int ret; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 263 | |
| 264 | cdns3_otg_disable_irq(cdns); |
| 265 | /* clear all interrupts */ |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 266 | writel(~0, &cdns->otg_irq_regs->ivect); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 267 | |
| 268 | ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | cdns3_otg_enable_irq(cdns); |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 273 | |
| 274 | return 0; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * cdns3_drd_update_mode - initialize mode of operation |
| 279 | * @cdns: Pointer to controller context structure |
| 280 | * |
| 281 | * Returns 0 on success otherwise negative errno |
| 282 | */ |
| 283 | int cdns3_drd_update_mode(struct cdns3 *cdns) |
| 284 | { |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 285 | int ret; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 286 | |
| 287 | switch (cdns->dr_mode) { |
| 288 | case USB_DR_MODE_PERIPHERAL: |
| 289 | ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL); |
| 290 | break; |
| 291 | case USB_DR_MODE_HOST: |
| 292 | ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST); |
| 293 | break; |
| 294 | case USB_DR_MODE_OTG: |
| 295 | ret = cdns3_init_otg_mode(cdns); |
| 296 | break; |
| 297 | default: |
| 298 | dev_err(cdns->dev, "Unsupported mode of operation %d\n", |
| 299 | cdns->dr_mode); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static irqreturn_t cdns3_drd_thread_irq(int irq, void *data) |
| 307 | { |
| 308 | struct cdns3 *cdns = data; |
| 309 | |
| 310 | cdns3_hw_role_switch(cdns); |
| 311 | |
| 312 | return IRQ_HANDLED; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * cdns3_drd_irq - interrupt handler for OTG events |
| 317 | * |
| 318 | * @irq: irq number for cdns3 core device |
| 319 | * @data: structure of cdns3 |
| 320 | * |
| 321 | * Returns IRQ_HANDLED or IRQ_NONE |
| 322 | */ |
| 323 | static irqreturn_t cdns3_drd_irq(int irq, void *data) |
| 324 | { |
| 325 | irqreturn_t ret = IRQ_NONE; |
| 326 | struct cdns3 *cdns = data; |
| 327 | u32 reg; |
| 328 | |
| 329 | if (cdns->dr_mode != USB_DR_MODE_OTG) |
Pawel Laszczak | 03cce68 | 2020-07-13 12:05:49 +0200 | [diff] [blame] | 330 | return IRQ_NONE; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 331 | |
Peter Chen | b1234e3 | 2020-09-02 17:57:32 +0800 | [diff] [blame] | 332 | if (cdns->in_lpm) |
| 333 | return ret; |
| 334 | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 335 | reg = readl(&cdns->otg_irq_regs->ivect); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 336 | |
| 337 | if (!reg) |
Pawel Laszczak | 03cce68 | 2020-07-13 12:05:49 +0200 | [diff] [blame] | 338 | return IRQ_NONE; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 339 | |
| 340 | if (reg & OTGIEN_ID_CHANGE_INT) { |
| 341 | dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n", |
| 342 | cdns3_get_id(cdns)); |
| 343 | |
| 344 | ret = IRQ_WAKE_THREAD; |
| 345 | } |
| 346 | |
| 347 | if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) { |
| 348 | dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n", |
| 349 | cdns3_get_vbus(cdns)); |
| 350 | |
| 351 | ret = IRQ_WAKE_THREAD; |
| 352 | } |
| 353 | |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 354 | writel(~0, &cdns->otg_irq_regs->ivect); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | int cdns3_drd_init(struct cdns3 *cdns) |
| 359 | { |
| 360 | void __iomem *regs; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 361 | u32 state; |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 362 | int ret; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 363 | |
| 364 | regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res); |
| 365 | if (IS_ERR(regs)) |
| 366 | return PTR_ERR(regs); |
| 367 | |
| 368 | /* Detection of DRD version. Controller has been released |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 369 | * in three versions. All are very similar and are software compatible, |
| 370 | * but they have same changes in register maps. |
| 371 | * The first register in oldest version is command register and it's |
| 372 | * read only. Driver should read 0 from it. On the other hand, in v1 |
| 373 | * and v2 the first register contains device ID number which is not |
| 374 | * set to 0. Driver uses this fact to detect the proper version of |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 375 | * controller. |
| 376 | */ |
| 377 | cdns->otg_v0_regs = regs; |
| 378 | if (!readl(&cdns->otg_v0_regs->cmd)) { |
| 379 | cdns->version = CDNS3_CONTROLLER_V0; |
| 380 | cdns->otg_v1_regs = NULL; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 381 | cdns->otg_cdnsp_regs = NULL; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 382 | cdns->otg_regs = regs; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 383 | cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) |
| 384 | &cdns->otg_v0_regs->ien; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 385 | writel(1, &cdns->otg_v0_regs->simulate); |
Peter Chen | eed6ed6 | 2020-03-31 16:10:05 +0800 | [diff] [blame] | 386 | dev_dbg(cdns->dev, "DRD version v0 (%08x)\n", |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 387 | readl(&cdns->otg_v0_regs->version)); |
| 388 | } else { |
| 389 | cdns->otg_v0_regs = NULL; |
| 390 | cdns->otg_v1_regs = regs; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 391 | cdns->otg_cdnsp_regs = regs; |
| 392 | |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 393 | cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 394 | |
| 395 | if (cdns->otg_cdnsp_regs->did == OTG_CDNSP_DID) { |
| 396 | cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) |
| 397 | &cdns->otg_cdnsp_regs->ien; |
| 398 | cdns->version = CDNSP_CONTROLLER_V2; |
| 399 | } else { |
| 400 | cdns->otg_irq_regs = (struct cdns3_otg_irq_regs *) |
| 401 | &cdns->otg_v1_regs->ien; |
| 402 | writel(1, &cdns->otg_v1_regs->simulate); |
| 403 | cdns->version = CDNS3_CONTROLLER_V1; |
| 404 | } |
| 405 | |
Peter Chen | eed6ed6 | 2020-03-31 16:10:05 +0800 | [diff] [blame] | 406 | dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n", |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 407 | readl(&cdns->otg_v1_regs->did), |
| 408 | readl(&cdns->otg_v1_regs->rid)); |
| 409 | } |
| 410 | |
| 411 | state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts)); |
| 412 | |
| 413 | /* Update dr_mode according to STRAP configuration. */ |
| 414 | cdns->dr_mode = USB_DR_MODE_OTG; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 415 | |
| 416 | if ((cdns->version == CDNSP_CONTROLLER_V2 && |
| 417 | state == OTGSTS_CDNSP_STRAP_HOST) || |
| 418 | (cdns->version != CDNSP_CONTROLLER_V2 && |
| 419 | state == OTGSTS_STRAP_HOST)) { |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 420 | dev_dbg(cdns->dev, "Controller strapped to HOST\n"); |
| 421 | cdns->dr_mode = USB_DR_MODE_HOST; |
Pawel Laszczak | db8892b | 2020-12-07 11:32:18 +0100 | [diff] [blame^] | 422 | } else if ((cdns->version == CDNSP_CONTROLLER_V2 && |
| 423 | state == OTGSTS_CDNSP_STRAP_GADGET) || |
| 424 | (cdns->version != CDNSP_CONTROLLER_V2 && |
| 425 | state == OTGSTS_STRAP_GADGET)) { |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 426 | dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n"); |
| 427 | cdns->dr_mode = USB_DR_MODE_PERIPHERAL; |
| 428 | } |
| 429 | |
| 430 | ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq, |
| 431 | cdns3_drd_irq, |
| 432 | cdns3_drd_thread_irq, |
| 433 | IRQF_SHARED, |
| 434 | dev_name(cdns->dev), cdns); |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 435 | if (ret) { |
| 436 | dev_err(cdns->dev, "couldn't get otg_irq\n"); |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | state = readl(&cdns->otg_regs->sts); |
Pawel Laszczak | ecf4f82 | 2020-07-13 12:05:48 +0200 | [diff] [blame] | 441 | if (OTGSTS_OTG_NRDY(state)) { |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 442 | dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n"); |
| 443 | return -ENODEV; |
| 444 | } |
| 445 | |
Pawel Laszczak | 27afe16 | 2020-07-13 12:05:47 +0200 | [diff] [blame] | 446 | return 0; |
Pawel Laszczak | 7733f6c | 2019-08-26 12:19:30 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | int cdns3_drd_exit(struct cdns3 *cdns) |
| 450 | { |
| 451 | cdns3_otg_disable_irq(cdns); |
| 452 | return 0; |
| 453 | } |