blob: 38ccd29e4cdef1e0dda34fc13cb4bb774d9642ca [file] [log] [blame]
Pawel Laszczak7733f6c2019-08-26 12:19:30 +01001// 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>
Peter Chen9f650132020-09-01 10:33:51 +080018#include <linux/phy/phy.h>
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010019
20#include "gadget.h"
21#include "drd.h"
22#include "core.h"
23
24/**
25 * cdns3_set_mode - change mode of OTG Core
26 * @cdns: pointer to context structure
27 * @mode: selected mode from cdns_role
28 *
29 * Returns 0 on success otherwise negative errno
30 */
31int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
32{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010033 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");
42 if (cdns->version == CDNS3_CONTROLLER_V1) {
43 reg = readl(&cdns->otg_v1_regs->override);
44 reg |= OVERRIDE_IDPULLUP;
45 writel(reg, &cdns->otg_v1_regs->override);
Pawel Laszczak2eae2df2020-09-15 14:45:43 +030046
47 /*
48 * Enable work around feature built into the
49 * controller to address issue with RX Sensitivity
50 * est (EL_17) for USB2 PHY. The issue only occures
51 * for 0x0002450D controller version.
52 */
53 if (cdns->phyrst_a_enable) {
54 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
55 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
56 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
57 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010058 } else {
59 reg = readl(&cdns->otg_v0_regs->ctrl1);
60 reg |= OVERRIDE_IDPULLUP_V0;
61 writel(reg, &cdns->otg_v0_regs->ctrl1);
62 }
63
64 /*
65 * Hardware specification says: "ID_VALUE must be valid within
66 * 50ms after idpullup is set to '1" so driver must wait
67 * 50ms before reading this pin.
68 */
69 usleep_range(50000, 60000);
70 break;
71 default:
72 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
73 return -EINVAL;
74 }
75
Pawel Laszczak27afe162020-07-13 12:05:47 +020076 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010077}
78
79int cdns3_get_id(struct cdns3 *cdns)
80{
81 int id;
82
83 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
84 dev_dbg(cdns->dev, "OTG ID: %d", id);
85
86 return id;
87}
88
89int cdns3_get_vbus(struct cdns3 *cdns)
90{
91 int vbus;
92
93 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
94 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
95
96 return vbus;
97}
98
Pawel Laszczak24525842020-07-13 12:05:50 +020099bool cdns3_is_host(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100100{
101 if (cdns->dr_mode == USB_DR_MODE_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200102 return true;
Pawel Laszczak08c35dd2020-07-13 12:05:51 +0200103 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200104 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100105
Pawel Laszczak24525842020-07-13 12:05:50 +0200106 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100107}
108
Pawel Laszczak24525842020-07-13 12:05:50 +0200109bool cdns3_is_device(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100110{
111 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200112 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100113 else if (cdns->dr_mode == USB_DR_MODE_OTG)
Pawel Laszczak08c35dd2020-07-13 12:05:51 +0200114 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200115 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100116
Pawel Laszczak24525842020-07-13 12:05:50 +0200117 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100118}
119
120/**
121 * cdns3_otg_disable_irq - Disable all OTG interrupts
122 * @cdns: Pointer to controller context structure
123 */
124static void cdns3_otg_disable_irq(struct cdns3 *cdns)
125{
126 writel(0, &cdns->otg_regs->ien);
127}
128
129/**
130 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
131 * @cdns: Pointer to controller context structure
132 */
133static void cdns3_otg_enable_irq(struct cdns3 *cdns)
134{
135 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
136 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_regs->ien);
137}
138
139/**
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200140 * cdns3_drd_host_on - start host.
141 * @cdns: Pointer to controller context structure.
142 *
143 * Returns 0 on success otherwise negative errno.
144 */
145int cdns3_drd_host_on(struct cdns3 *cdns)
146{
147 u32 val;
148 int ret;
149
150 /* Enable host mode. */
151 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
152 &cdns->otg_regs->cmd);
153
154 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
155 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
156 val & OTGSTS_XHCI_READY, 1, 100000);
157
158 if (ret)
159 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
160
Peter Chen9f650132020-09-01 10:33:51 +0800161 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_HOST);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200162 return ret;
163}
164
165/**
166 * cdns3_drd_host_off - stop host.
167 * @cdns: Pointer to controller context structure.
168 */
169void cdns3_drd_host_off(struct cdns3 *cdns)
170{
171 u32 val;
172
173 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
174 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
175 &cdns->otg_regs->cmd);
176
177 /* Waiting till H_IDLE state.*/
178 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
179 !(val & OTGSTATE_HOST_STATE_MASK),
180 1, 2000000);
Peter Chen9f650132020-09-01 10:33:51 +0800181 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200182}
183
184/**
185 * cdns3_drd_gadget_on - start gadget.
186 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100187 *
188 * Returns 0 on success otherwise negative errno
189 */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200190int cdns3_drd_gadget_on(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100191{
192 int ret, val;
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200193 u32 reg = OTGCMD_OTG_DIS;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100194
195 /* switch OTG core */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200196 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100197
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200198 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
199
200 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
201 val & OTGSTS_DEV_READY,
202 1, 100000);
203 if (ret) {
204 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
205 return ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100206 }
207
Peter Chen9f650132020-09-01 10:33:51 +0800208 phy_set_mode(cdns->usb3_phy, PHY_MODE_USB_DEVICE);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100209 return 0;
210}
211
212/**
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200213 * cdns3_drd_gadget_off - stop gadget.
214 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100215 */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200216void cdns3_drd_gadget_off(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100217{
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200218 u32 val;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100219
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200220 /*
221 * Driver should wait at least 10us after disabling Device
222 * before turning-off Device (DEV_BUS_DROP).
223 */
224 usleep_range(20, 30);
225 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
226 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
227 &cdns->otg_regs->cmd);
228 /* Waiting till DEV_IDLE state.*/
229 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
230 !(val & OTGSTATE_DEV_STATE_MASK),
231 1, 2000000);
Peter Chen9f650132020-09-01 10:33:51 +0800232 phy_set_mode(cdns->usb3_phy, PHY_MODE_INVALID);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100233}
234
235/**
236 * cdns3_init_otg_mode - initialize drd controller
237 * @cdns: Pointer to controller context structure
238 *
239 * Returns 0 on success otherwise negative errno
240 */
241static int cdns3_init_otg_mode(struct cdns3 *cdns)
242{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200243 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100244
245 cdns3_otg_disable_irq(cdns);
246 /* clear all interrupts */
247 writel(~0, &cdns->otg_regs->ivect);
248
249 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
250 if (ret)
251 return ret;
252
253 cdns3_otg_enable_irq(cdns);
Pawel Laszczak27afe162020-07-13 12:05:47 +0200254
255 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100256}
257
258/**
259 * cdns3_drd_update_mode - initialize mode of operation
260 * @cdns: Pointer to controller context structure
261 *
262 * Returns 0 on success otherwise negative errno
263 */
264int cdns3_drd_update_mode(struct cdns3 *cdns)
265{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200266 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100267
268 switch (cdns->dr_mode) {
269 case USB_DR_MODE_PERIPHERAL:
270 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
271 break;
272 case USB_DR_MODE_HOST:
273 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
274 break;
275 case USB_DR_MODE_OTG:
276 ret = cdns3_init_otg_mode(cdns);
277 break;
278 default:
279 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
280 cdns->dr_mode);
281 return -EINVAL;
282 }
283
284 return ret;
285}
286
287static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
288{
289 struct cdns3 *cdns = data;
290
291 cdns3_hw_role_switch(cdns);
292
293 return IRQ_HANDLED;
294}
295
296/**
297 * cdns3_drd_irq - interrupt handler for OTG events
298 *
299 * @irq: irq number for cdns3 core device
300 * @data: structure of cdns3
301 *
302 * Returns IRQ_HANDLED or IRQ_NONE
303 */
304static irqreturn_t cdns3_drd_irq(int irq, void *data)
305{
306 irqreturn_t ret = IRQ_NONE;
307 struct cdns3 *cdns = data;
308 u32 reg;
309
310 if (cdns->dr_mode != USB_DR_MODE_OTG)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200311 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100312
Peter Chenb1234e32020-09-02 17:57:32 +0800313 if (cdns->in_lpm)
314 return ret;
315
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100316 reg = readl(&cdns->otg_regs->ivect);
317
318 if (!reg)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200319 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100320
321 if (reg & OTGIEN_ID_CHANGE_INT) {
322 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
323 cdns3_get_id(cdns));
324
325 ret = IRQ_WAKE_THREAD;
326 }
327
328 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
329 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
330 cdns3_get_vbus(cdns));
331
332 ret = IRQ_WAKE_THREAD;
333 }
334
335 writel(~0, &cdns->otg_regs->ivect);
336 return ret;
337}
338
339int cdns3_drd_init(struct cdns3 *cdns)
340{
341 void __iomem *regs;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100342 u32 state;
Pawel Laszczak27afe162020-07-13 12:05:47 +0200343 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100344
345 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
346 if (IS_ERR(regs))
347 return PTR_ERR(regs);
348
349 /* Detection of DRD version. Controller has been released
350 * in two versions. Both are similar, but they have same changes
351 * in register maps.
352 * The first register in old version is command register and it's read
353 * only, so driver should read 0 from it. On the other hand, in v1
354 * the first register contains device ID number which is not set to 0.
355 * Driver uses this fact to detect the proper version of
356 * controller.
357 */
358 cdns->otg_v0_regs = regs;
359 if (!readl(&cdns->otg_v0_regs->cmd)) {
360 cdns->version = CDNS3_CONTROLLER_V0;
361 cdns->otg_v1_regs = NULL;
362 cdns->otg_regs = regs;
363 writel(1, &cdns->otg_v0_regs->simulate);
Peter Cheneed6ed62020-03-31 16:10:05 +0800364 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100365 readl(&cdns->otg_v0_regs->version));
366 } else {
367 cdns->otg_v0_regs = NULL;
368 cdns->otg_v1_regs = regs;
369 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
370 cdns->version = CDNS3_CONTROLLER_V1;
371 writel(1, &cdns->otg_v1_regs->simulate);
Peter Cheneed6ed62020-03-31 16:10:05 +0800372 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100373 readl(&cdns->otg_v1_regs->did),
374 readl(&cdns->otg_v1_regs->rid));
375 }
376
377 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
378
379 /* Update dr_mode according to STRAP configuration. */
380 cdns->dr_mode = USB_DR_MODE_OTG;
381 if (state == OTGSTS_STRAP_HOST) {
382 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
383 cdns->dr_mode = USB_DR_MODE_HOST;
384 } else if (state == OTGSTS_STRAP_GADGET) {
385 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
386 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
387 }
388
389 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
390 cdns3_drd_irq,
391 cdns3_drd_thread_irq,
392 IRQF_SHARED,
393 dev_name(cdns->dev), cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100394 if (ret) {
395 dev_err(cdns->dev, "couldn't get otg_irq\n");
396 return ret;
397 }
398
399 state = readl(&cdns->otg_regs->sts);
Pawel Laszczakecf4f822020-07-13 12:05:48 +0200400 if (OTGSTS_OTG_NRDY(state)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100401 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
402 return -ENODEV;
403 }
404
Pawel Laszczak27afe162020-07-13 12:05:47 +0200405 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100406}
407
408int cdns3_drd_exit(struct cdns3 *cdns)
409{
410 cdns3_otg_disable_irq(cdns);
411 return 0;
412}