blob: 55c73b1d870471bff520b619fa27b6a94ee752d8 [file] [log] [blame]
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001// SPDX-License-Identifier: GPL-2.0
2/*
Pawel Laszczak3d829042020-12-07 11:32:24 +01003 * Cadence USBSS and USBSSP DRD Driver.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01004 *
Pawel Laszczakdb8892b2020-12-07 11:32:18 +01005 * Copyright (C) 2018-2020 Cadence.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01006 * Copyright (C) 2019 Texas Instruments
7 *
8 * Author: Pawel Laszczak <pawell@cadence.com>
9 * Roger Quadros <rogerq@ti.com>
10 *
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010011 */
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>
17
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010018#include "drd.h"
19#include "core.h"
20
21/**
Pawel Laszczak0b490042020-12-07 11:32:21 +010022 * cdns_set_mode - change mode of OTG Core
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010023 * @cdns: pointer to context structure
24 * @mode: selected mode from cdns_role
25 *
26 * Returns 0 on success otherwise negative errno
27 */
Pawel Laszczak0b490042020-12-07 11:32:21 +010028static int cdns_set_mode(struct cdns *cdns, enum usb_dr_mode mode)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010029{
Pawel Laszczak6500f302020-12-15 11:27:09 +010030 void __iomem *override_reg;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010031 u32 reg;
32
33 switch (mode) {
34 case USB_DR_MODE_PERIPHERAL:
35 break;
36 case USB_DR_MODE_HOST:
37 break;
38 case USB_DR_MODE_OTG:
39 dev_dbg(cdns->dev, "Set controller to OTG mode\n");
Pawel Laszczak2eae2df2020-09-15 14:45:43 +030040
Pawel Laszczakdb8892b2020-12-07 11:32:18 +010041 if (cdns->version == CDNSP_CONTROLLER_V2)
42 override_reg = &cdns->otg_cdnsp_regs->override;
43 else if (cdns->version == CDNS3_CONTROLLER_V1)
44 override_reg = &cdns->otg_v1_regs->override;
45 else
46 override_reg = &cdns->otg_v0_regs->ctrl1;
47
48 reg = readl(override_reg);
49
50 if (cdns->version != CDNS3_CONTROLLER_V0)
51 reg |= OVERRIDE_IDPULLUP;
52 else
53 reg |= OVERRIDE_IDPULLUP_V0;
54
55 writel(reg, override_reg);
56
57 if (cdns->version == CDNS3_CONTROLLER_V1) {
Pawel Laszczak2eae2df2020-09-15 14:45:43 +030058 /*
59 * Enable work around feature built into the
60 * controller to address issue with RX Sensitivity
61 * est (EL_17) for USB2 PHY. The issue only occures
62 * for 0x0002450D controller version.
63 */
64 if (cdns->phyrst_a_enable) {
65 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
66 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
67 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
68 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010069 }
70
71 /*
72 * Hardware specification says: "ID_VALUE must be valid within
73 * 50ms after idpullup is set to '1" so driver must wait
74 * 50ms before reading this pin.
75 */
76 usleep_range(50000, 60000);
77 break;
78 default:
79 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
80 return -EINVAL;
81 }
82
Pawel Laszczak27afe162020-07-13 12:05:47 +020083 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010084}
85
Pawel Laszczak0b490042020-12-07 11:32:21 +010086int cdns_get_id(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010087{
88 int id;
89
90 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
91 dev_dbg(cdns->dev, "OTG ID: %d", id);
92
93 return id;
94}
95
Pawel Laszczak0b490042020-12-07 11:32:21 +010096int cdns_get_vbus(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010097{
98 int vbus;
99
100 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
101 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
102
103 return vbus;
104}
105
Pawel Laszczak3d829042020-12-07 11:32:24 +0100106void cdns_clear_vbus(struct cdns *cdns)
107{
108 u32 reg;
109
110 if (cdns->version != CDNSP_CONTROLLER_V2)
111 return;
112
113 reg = readl(&cdns->otg_cdnsp_regs->override);
114 reg |= OVERRIDE_SESS_VLD_SEL;
115 writel(reg, &cdns->otg_cdnsp_regs->override);
116}
117EXPORT_SYMBOL_GPL(cdns_clear_vbus);
118
119void cdns_set_vbus(struct cdns *cdns)
120{
121 u32 reg;
122
123 if (cdns->version != CDNSP_CONTROLLER_V2)
124 return;
125
126 reg = readl(&cdns->otg_cdnsp_regs->override);
127 reg &= ~OVERRIDE_SESS_VLD_SEL;
128 writel(reg, &cdns->otg_cdnsp_regs->override);
129}
130EXPORT_SYMBOL_GPL(cdns_set_vbus);
131
Pawel Laszczak0b490042020-12-07 11:32:21 +0100132bool cdns_is_host(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100133{
134 if (cdns->dr_mode == USB_DR_MODE_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200135 return true;
Pawel Laszczak0b490042020-12-07 11:32:21 +0100136 else if (cdns_get_id(cdns) == CDNS3_ID_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200137 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100138
Pawel Laszczak24525842020-07-13 12:05:50 +0200139 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100140}
141
Pawel Laszczak0b490042020-12-07 11:32:21 +0100142bool cdns_is_device(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100143{
144 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200145 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100146 else if (cdns->dr_mode == USB_DR_MODE_OTG)
Pawel Laszczak0b490042020-12-07 11:32:21 +0100147 if (cdns_get_id(cdns) == CDNS3_ID_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200148 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100149
Pawel Laszczak24525842020-07-13 12:05:50 +0200150 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100151}
152
153/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100154 * cdns_otg_disable_irq - Disable all OTG interrupts
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100155 * @cdns: Pointer to controller context structure
156 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100157static void cdns_otg_disable_irq(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100158{
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100159 writel(0, &cdns->otg_irq_regs->ien);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100160}
161
162/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100163 * cdns_otg_enable_irq - enable id and sess_valid interrupts
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100164 * @cdns: Pointer to controller context structure
165 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100166static void cdns_otg_enable_irq(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100167{
168 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100169 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_irq_regs->ien);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100170}
171
172/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100173 * cdns_drd_host_on - start host.
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200174 * @cdns: Pointer to controller context structure.
175 *
176 * Returns 0 on success otherwise negative errno.
177 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100178int cdns_drd_host_on(struct cdns *cdns)
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200179{
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100180 u32 val, ready_bit;
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200181 int ret;
182
183 /* Enable host mode. */
184 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
185 &cdns->otg_regs->cmd);
186
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100187 if (cdns->version == CDNSP_CONTROLLER_V2)
188 ready_bit = OTGSTS_CDNSP_XHCI_READY;
189 else
190 ready_bit = OTGSTS_CDNS3_XHCI_READY;
191
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200192 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
193 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100194 val & ready_bit, 1, 100000);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200195
196 if (ret)
197 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
198
Peter Chen9f650132020-09-01 10:33:51 +0800199 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200200 return ret;
201}
202
203/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100204 * cdns_drd_host_off - stop host.
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200205 * @cdns: Pointer to controller context structure.
206 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100207void cdns_drd_host_off(struct cdns *cdns)
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200208{
209 u32 val;
210
211 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
212 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
213 &cdns->otg_regs->cmd);
214
215 /* Waiting till H_IDLE state.*/
216 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
217 !(val & OTGSTATE_HOST_STATE_MASK),
218 1, 2000000);
Peter Chen9f650132020-09-01 10:33:51 +0800219 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200220}
221
222/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100223 * cdns_drd_gadget_on - start gadget.
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200224 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100225 *
226 * Returns 0 on success otherwise negative errno
227 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100228int cdns_drd_gadget_on(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100229{
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200230 u32 reg = OTGCMD_OTG_DIS;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100231 u32 ready_bit;
232 int ret, val;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100233
234 /* switch OTG core */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200235 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100236
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200237 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
238
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100239 if (cdns->version == CDNSP_CONTROLLER_V2)
240 ready_bit = OTGSTS_CDNSP_DEV_READY;
241 else
242 ready_bit = OTGSTS_CDNS3_DEV_READY;
243
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200244 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100245 val & ready_bit, 1, 100000);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200246 if (ret) {
247 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
248 return ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100249 }
250
Peter Chen9f650132020-09-01 10:33:51 +0800251 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100252 return 0;
253}
Pawel Laszczak0b490042020-12-07 11:32:21 +0100254EXPORT_SYMBOL_GPL(cdns_drd_gadget_on);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100255
256/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100257 * cdns_drd_gadget_off - stop gadget.
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200258 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100259 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100260void cdns_drd_gadget_off(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100261{
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200262 u32 val;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100263
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200264 /*
265 * Driver should wait at least 10us after disabling Device
266 * before turning-off Device (DEV_BUS_DROP).
267 */
268 usleep_range(20, 30);
269 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
270 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
271 &cdns->otg_regs->cmd);
272 /* Waiting till DEV_IDLE state.*/
273 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
274 !(val & OTGSTATE_DEV_STATE_MASK),
275 1, 2000000);
Peter Chen9f650132020-09-01 10:33:51 +0800276 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100277}
Pawel Laszczak0b490042020-12-07 11:32:21 +0100278EXPORT_SYMBOL_GPL(cdns_drd_gadget_off);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100279
280/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100281 * cdns_init_otg_mode - initialize drd controller
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100282 * @cdns: Pointer to controller context structure
283 *
284 * Returns 0 on success otherwise negative errno
285 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100286static int cdns_init_otg_mode(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100287{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200288 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100289
Pawel Laszczak0b490042020-12-07 11:32:21 +0100290 cdns_otg_disable_irq(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100291 /* clear all interrupts */
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100292 writel(~0, &cdns->otg_irq_regs->ivect);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100293
Pawel Laszczak0b490042020-12-07 11:32:21 +0100294 ret = cdns_set_mode(cdns, USB_DR_MODE_OTG);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100295 if (ret)
296 return ret;
297
Pawel Laszczak0b490042020-12-07 11:32:21 +0100298 cdns_otg_enable_irq(cdns);
Pawel Laszczak27afe162020-07-13 12:05:47 +0200299
300 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100301}
302
303/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100304 * cdns_drd_update_mode - initialize mode of operation
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100305 * @cdns: Pointer to controller context structure
306 *
307 * Returns 0 on success otherwise negative errno
308 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100309int cdns_drd_update_mode(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100310{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200311 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100312
313 switch (cdns->dr_mode) {
314 case USB_DR_MODE_PERIPHERAL:
Pawel Laszczak0b490042020-12-07 11:32:21 +0100315 ret = cdns_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100316 break;
317 case USB_DR_MODE_HOST:
Pawel Laszczak0b490042020-12-07 11:32:21 +0100318 ret = cdns_set_mode(cdns, USB_DR_MODE_HOST);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100319 break;
320 case USB_DR_MODE_OTG:
Pawel Laszczak0b490042020-12-07 11:32:21 +0100321 ret = cdns_init_otg_mode(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100322 break;
323 default:
324 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
325 cdns->dr_mode);
326 return -EINVAL;
327 }
328
329 return ret;
330}
331
Pawel Laszczak0b490042020-12-07 11:32:21 +0100332static irqreturn_t cdns_drd_thread_irq(int irq, void *data)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100333{
Pawel Laszczak0b490042020-12-07 11:32:21 +0100334 struct cdns *cdns = data;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100335
Pawel Laszczak0b490042020-12-07 11:32:21 +0100336 cdns_hw_role_switch(cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100337
338 return IRQ_HANDLED;
339}
340
341/**
Pawel Laszczak0b490042020-12-07 11:32:21 +0100342 * cdns_drd_irq - interrupt handler for OTG events
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100343 *
Pawel Laszczak0b490042020-12-07 11:32:21 +0100344 * @irq: irq number for cdns core device
345 * @data: structure of cdns
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100346 *
347 * Returns IRQ_HANDLED or IRQ_NONE
348 */
Pawel Laszczak0b490042020-12-07 11:32:21 +0100349static irqreturn_t cdns_drd_irq(int irq, void *data)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100350{
351 irqreturn_t ret = IRQ_NONE;
Pawel Laszczak0b490042020-12-07 11:32:21 +0100352 struct cdns *cdns = data;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100353 u32 reg;
354
355 if (cdns->dr_mode != USB_DR_MODE_OTG)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200356 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100357
Peter Chenb1234e32020-09-02 17:57:32 +0800358 if (cdns->in_lpm)
359 return ret;
360
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100361 reg = readl(&cdns->otg_irq_regs->ivect);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100362
363 if (!reg)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200364 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100365
366 if (reg & OTGIEN_ID_CHANGE_INT) {
367 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
Pawel Laszczak0b490042020-12-07 11:32:21 +0100368 cdns_get_id(cdns));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100369
370 ret = IRQ_WAKE_THREAD;
371 }
372
373 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
374 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
Pawel Laszczak0b490042020-12-07 11:32:21 +0100375 cdns_get_vbus(cdns));
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100376
377 ret = IRQ_WAKE_THREAD;
378 }
379
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100380 writel(~0, &cdns->otg_irq_regs->ivect);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100381 return ret;
382}
383
Pawel Laszczak0b490042020-12-07 11:32:21 +0100384int cdns_drd_init(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100385{
386 void __iomem *regs;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100387 u32 state;
Pawel Laszczak27afe162020-07-13 12:05:47 +0200388 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100389
390 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
391 if (IS_ERR(regs))
392 return PTR_ERR(regs);
393
394 /* Detection of DRD version. Controller has been released
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100395 * in three versions. All are very similar and are software compatible,
396 * but they have same changes in register maps.
397 * The first register in oldest version is command register and it's
398 * read only. Driver should read 0 from it. On the other hand, in v1
399 * and v2 the first register contains device ID number which is not
400 * set to 0. Driver uses this fact to detect the proper version of
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100401 * controller.
402 */
403 cdns->otg_v0_regs = regs;
404 if (!readl(&cdns->otg_v0_regs->cmd)) {
405 cdns->version = CDNS3_CONTROLLER_V0;
406 cdns->otg_v1_regs = NULL;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100407 cdns->otg_cdnsp_regs = NULL;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100408 cdns->otg_regs = regs;
Pawel Laszczak6500f302020-12-15 11:27:09 +0100409 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100410 &cdns->otg_v0_regs->ien;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100411 writel(1, &cdns->otg_v0_regs->simulate);
Peter Cheneed6ed62020-03-31 16:10:05 +0800412 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100413 readl(&cdns->otg_v0_regs->version));
414 } else {
415 cdns->otg_v0_regs = NULL;
416 cdns->otg_v1_regs = regs;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100417 cdns->otg_cdnsp_regs = regs;
418
Pawel Laszczak6500f302020-12-15 11:27:09 +0100419 cdns->otg_regs = (void __iomem *)&cdns->otg_v1_regs->cmd;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100420
Pawel Laszczak6500f302020-12-15 11:27:09 +0100421 if (readl(&cdns->otg_cdnsp_regs->did) == OTG_CDNSP_DID) {
422 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100423 &cdns->otg_cdnsp_regs->ien;
424 cdns->version = CDNSP_CONTROLLER_V2;
425 } else {
Pawel Laszczak6500f302020-12-15 11:27:09 +0100426 cdns->otg_irq_regs = (struct cdns_otg_irq_regs __iomem *)
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100427 &cdns->otg_v1_regs->ien;
428 writel(1, &cdns->otg_v1_regs->simulate);
429 cdns->version = CDNS3_CONTROLLER_V1;
430 }
431
Peter Cheneed6ed62020-03-31 16:10:05 +0800432 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100433 readl(&cdns->otg_v1_regs->did),
434 readl(&cdns->otg_v1_regs->rid));
435 }
436
437 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
438
439 /* Update dr_mode according to STRAP configuration. */
440 cdns->dr_mode = USB_DR_MODE_OTG;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100441
442 if ((cdns->version == CDNSP_CONTROLLER_V2 &&
443 state == OTGSTS_CDNSP_STRAP_HOST) ||
444 (cdns->version != CDNSP_CONTROLLER_V2 &&
445 state == OTGSTS_STRAP_HOST)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100446 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
447 cdns->dr_mode = USB_DR_MODE_HOST;
Pawel Laszczakdb8892b2020-12-07 11:32:18 +0100448 } else if ((cdns->version == CDNSP_CONTROLLER_V2 &&
449 state == OTGSTS_CDNSP_STRAP_GADGET) ||
450 (cdns->version != CDNSP_CONTROLLER_V2 &&
451 state == OTGSTS_STRAP_GADGET)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100452 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
453 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
454 }
455
456 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
Pawel Laszczak0b490042020-12-07 11:32:21 +0100457 cdns_drd_irq,
458 cdns_drd_thread_irq,
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100459 IRQF_SHARED,
460 dev_name(cdns->dev), cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100461 if (ret) {
462 dev_err(cdns->dev, "couldn't get otg_irq\n");
463 return ret;
464 }
465
466 state = readl(&cdns->otg_regs->sts);
Pawel Laszczakecf4f822020-07-13 12:05:48 +0200467 if (OTGSTS_OTG_NRDY(state)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100468 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
469 return -ENODEV;
470 }
471
Pawel Laszczak27afe162020-07-13 12:05:47 +0200472 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100473}
474
Pawel Laszczak0b490042020-12-07 11:32:21 +0100475int cdns_drd_exit(struct cdns *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100476{
Pawel Laszczak0b490042020-12-07 11:32:21 +0100477 cdns_otg_disable_irq(cdns);
Pawel Laszczak3d829042020-12-07 11:32:24 +0100478
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100479 return 0;
480}
Frank Li2cf25812021-02-18 16:51:08 -0600481
482
483/* Indicate the cdns3 core was power lost before */
484bool cdns_power_is_lost(struct cdns *cdns)
485{
486 if (cdns->version == CDNS3_CONTROLLER_V1) {
487 if (!(readl(&cdns->otg_v1_regs->simulate) & BIT(0)))
488 return true;
489 } else {
490 if (!(readl(&cdns->otg_v0_regs->simulate) & BIT(0)))
491 return true;
492 }
493 return false;
494}
495EXPORT_SYMBOL_GPL(cdns_power_is_lost);