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