blob: fcd295f566b2dbf2ba7c8e135e0c3a5da7a636bf [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>
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 */
30int cdns3_set_mode(struct cdns3 *cdns, enum usb_dr_mode mode)
31{
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010032 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);
Pawel Laszczak2eae2df2020-09-15 14:45:43 +030045
46 /*
47 * Enable work around feature built into the
48 * controller to address issue with RX Sensitivity
49 * est (EL_17) for USB2 PHY. The issue only occures
50 * for 0x0002450D controller version.
51 */
52 if (cdns->phyrst_a_enable) {
53 reg = readl(&cdns->otg_v1_regs->phyrst_cfg);
54 reg |= PHYRST_CFG_PHYRST_A_ENABLE;
55 writel(reg, &cdns->otg_v1_regs->phyrst_cfg);
56 }
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010057 } else {
58 reg = readl(&cdns->otg_v0_regs->ctrl1);
59 reg |= OVERRIDE_IDPULLUP_V0;
60 writel(reg, &cdns->otg_v0_regs->ctrl1);
61 }
62
63 /*
64 * Hardware specification says: "ID_VALUE must be valid within
65 * 50ms after idpullup is set to '1" so driver must wait
66 * 50ms before reading this pin.
67 */
68 usleep_range(50000, 60000);
69 break;
70 default:
71 dev_err(cdns->dev, "Unsupported mode of operation %d\n", mode);
72 return -EINVAL;
73 }
74
Pawel Laszczak27afe162020-07-13 12:05:47 +020075 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010076}
77
78int cdns3_get_id(struct cdns3 *cdns)
79{
80 int id;
81
82 id = readl(&cdns->otg_regs->sts) & OTGSTS_ID_VALUE;
83 dev_dbg(cdns->dev, "OTG ID: %d", id);
84
85 return id;
86}
87
88int cdns3_get_vbus(struct cdns3 *cdns)
89{
90 int vbus;
91
92 vbus = !!(readl(&cdns->otg_regs->sts) & OTGSTS_VBUS_VALID);
93 dev_dbg(cdns->dev, "OTG VBUS: %d", vbus);
94
95 return vbus;
96}
97
Pawel Laszczak24525842020-07-13 12:05:50 +020098bool cdns3_is_host(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +010099{
100 if (cdns->dr_mode == USB_DR_MODE_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200101 return true;
Pawel Laszczak08c35dd2020-07-13 12:05:51 +0200102 else if (cdns3_get_id(cdns) == CDNS3_ID_HOST)
Pawel Laszczak24525842020-07-13 12:05:50 +0200103 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100104
Pawel Laszczak24525842020-07-13 12:05:50 +0200105 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100106}
107
Pawel Laszczak24525842020-07-13 12:05:50 +0200108bool cdns3_is_device(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100109{
110 if (cdns->dr_mode == USB_DR_MODE_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200111 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100112 else if (cdns->dr_mode == USB_DR_MODE_OTG)
Pawel Laszczak08c35dd2020-07-13 12:05:51 +0200113 if (cdns3_get_id(cdns) == CDNS3_ID_PERIPHERAL)
Pawel Laszczak24525842020-07-13 12:05:50 +0200114 return true;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100115
Pawel Laszczak24525842020-07-13 12:05:50 +0200116 return false;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100117}
118
119/**
120 * cdns3_otg_disable_irq - Disable all OTG interrupts
121 * @cdns: Pointer to controller context structure
122 */
123static void cdns3_otg_disable_irq(struct cdns3 *cdns)
124{
125 writel(0, &cdns->otg_regs->ien);
126}
127
128/**
129 * cdns3_otg_enable_irq - enable id and sess_valid interrupts
130 * @cdns: Pointer to controller context structure
131 */
132static void cdns3_otg_enable_irq(struct cdns3 *cdns)
133{
134 writel(OTGIEN_ID_CHANGE_INT | OTGIEN_VBUSVALID_RISE_INT |
135 OTGIEN_VBUSVALID_FALL_INT, &cdns->otg_regs->ien);
136}
137
138/**
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200139 * cdns3_drd_host_on - start host.
140 * @cdns: Pointer to controller context structure.
141 *
142 * Returns 0 on success otherwise negative errno.
143 */
144int cdns3_drd_host_on(struct cdns3 *cdns)
145{
146 u32 val;
147 int ret;
148
149 /* Enable host mode. */
150 writel(OTGCMD_HOST_BUS_REQ | OTGCMD_OTG_DIS,
151 &cdns->otg_regs->cmd);
152
153 dev_dbg(cdns->dev, "Waiting till Host mode is turned on\n");
154 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
155 val & OTGSTS_XHCI_READY, 1, 100000);
156
157 if (ret)
158 dev_err(cdns->dev, "timeout waiting for xhci_ready\n");
159
160 return ret;
161}
162
163/**
164 * cdns3_drd_host_off - stop host.
165 * @cdns: Pointer to controller context structure.
166 */
167void cdns3_drd_host_off(struct cdns3 *cdns)
168{
169 u32 val;
170
171 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
172 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
173 &cdns->otg_regs->cmd);
174
175 /* Waiting till H_IDLE state.*/
176 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
177 !(val & OTGSTATE_HOST_STATE_MASK),
178 1, 2000000);
179}
180
181/**
182 * cdns3_drd_gadget_on - start gadget.
183 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100184 *
185 * Returns 0 on success otherwise negative errno
186 */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200187int cdns3_drd_gadget_on(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100188{
189 int ret, val;
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200190 u32 reg = OTGCMD_OTG_DIS;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100191
192 /* switch OTG core */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200193 writel(OTGCMD_DEV_BUS_REQ | reg, &cdns->otg_regs->cmd);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100194
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200195 dev_dbg(cdns->dev, "Waiting till Device mode is turned on\n");
196
197 ret = readl_poll_timeout_atomic(&cdns->otg_regs->sts, val,
198 val & OTGSTS_DEV_READY,
199 1, 100000);
200 if (ret) {
201 dev_err(cdns->dev, "timeout waiting for dev_ready\n");
202 return ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100203 }
204
205 return 0;
206}
207
208/**
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200209 * cdns3_drd_gadget_off - stop gadget.
210 * @cdns: Pointer to controller context structure.
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100211 */
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200212void cdns3_drd_gadget_off(struct cdns3 *cdns)
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100213{
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200214 u32 val;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100215
Pawel Laszczakb2aeb6d2020-07-13 12:05:54 +0200216 /*
217 * Driver should wait at least 10us after disabling Device
218 * before turning-off Device (DEV_BUS_DROP).
219 */
220 usleep_range(20, 30);
221 writel(OTGCMD_HOST_BUS_DROP | OTGCMD_DEV_BUS_DROP |
222 OTGCMD_DEV_POWER_OFF | OTGCMD_HOST_POWER_OFF,
223 &cdns->otg_regs->cmd);
224 /* Waiting till DEV_IDLE state.*/
225 readl_poll_timeout_atomic(&cdns->otg_regs->state, val,
226 !(val & OTGSTATE_DEV_STATE_MASK),
227 1, 2000000);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100228}
229
230/**
231 * cdns3_init_otg_mode - initialize drd controller
232 * @cdns: Pointer to controller context structure
233 *
234 * Returns 0 on success otherwise negative errno
235 */
236static int cdns3_init_otg_mode(struct cdns3 *cdns)
237{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200238 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100239
240 cdns3_otg_disable_irq(cdns);
241 /* clear all interrupts */
242 writel(~0, &cdns->otg_regs->ivect);
243
244 ret = cdns3_set_mode(cdns, USB_DR_MODE_OTG);
245 if (ret)
246 return ret;
247
248 cdns3_otg_enable_irq(cdns);
Pawel Laszczak27afe162020-07-13 12:05:47 +0200249
250 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100251}
252
253/**
254 * cdns3_drd_update_mode - initialize mode of operation
255 * @cdns: Pointer to controller context structure
256 *
257 * Returns 0 on success otherwise negative errno
258 */
259int cdns3_drd_update_mode(struct cdns3 *cdns)
260{
Pawel Laszczak27afe162020-07-13 12:05:47 +0200261 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100262
263 switch (cdns->dr_mode) {
264 case USB_DR_MODE_PERIPHERAL:
265 ret = cdns3_set_mode(cdns, USB_DR_MODE_PERIPHERAL);
266 break;
267 case USB_DR_MODE_HOST:
268 ret = cdns3_set_mode(cdns, USB_DR_MODE_HOST);
269 break;
270 case USB_DR_MODE_OTG:
271 ret = cdns3_init_otg_mode(cdns);
272 break;
273 default:
274 dev_err(cdns->dev, "Unsupported mode of operation %d\n",
275 cdns->dr_mode);
276 return -EINVAL;
277 }
278
279 return ret;
280}
281
282static irqreturn_t cdns3_drd_thread_irq(int irq, void *data)
283{
284 struct cdns3 *cdns = data;
285
286 cdns3_hw_role_switch(cdns);
287
288 return IRQ_HANDLED;
289}
290
291/**
292 * cdns3_drd_irq - interrupt handler for OTG events
293 *
294 * @irq: irq number for cdns3 core device
295 * @data: structure of cdns3
296 *
297 * Returns IRQ_HANDLED or IRQ_NONE
298 */
299static irqreturn_t cdns3_drd_irq(int irq, void *data)
300{
301 irqreturn_t ret = IRQ_NONE;
302 struct cdns3 *cdns = data;
303 u32 reg;
304
305 if (cdns->dr_mode != USB_DR_MODE_OTG)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200306 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100307
Peter Chenb1234e32020-09-02 17:57:32 +0800308 if (cdns->in_lpm)
309 return ret;
310
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100311 reg = readl(&cdns->otg_regs->ivect);
312
313 if (!reg)
Pawel Laszczak03cce682020-07-13 12:05:49 +0200314 return IRQ_NONE;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100315
316 if (reg & OTGIEN_ID_CHANGE_INT) {
317 dev_dbg(cdns->dev, "OTG IRQ: new ID: %d\n",
318 cdns3_get_id(cdns));
319
320 ret = IRQ_WAKE_THREAD;
321 }
322
323 if (reg & (OTGIEN_VBUSVALID_RISE_INT | OTGIEN_VBUSVALID_FALL_INT)) {
324 dev_dbg(cdns->dev, "OTG IRQ: new VBUS: %d\n",
325 cdns3_get_vbus(cdns));
326
327 ret = IRQ_WAKE_THREAD;
328 }
329
330 writel(~0, &cdns->otg_regs->ivect);
331 return ret;
332}
333
334int cdns3_drd_init(struct cdns3 *cdns)
335{
336 void __iomem *regs;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100337 u32 state;
Pawel Laszczak27afe162020-07-13 12:05:47 +0200338 int ret;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100339
340 regs = devm_ioremap_resource(cdns->dev, &cdns->otg_res);
341 if (IS_ERR(regs))
342 return PTR_ERR(regs);
343
344 /* Detection of DRD version. Controller has been released
345 * in two versions. Both are similar, but they have same changes
346 * in register maps.
347 * The first register in old version is command register and it's read
348 * only, so driver should read 0 from it. On the other hand, in v1
349 * the first register contains device ID number which is not set to 0.
350 * Driver uses this fact to detect the proper version of
351 * controller.
352 */
353 cdns->otg_v0_regs = regs;
354 if (!readl(&cdns->otg_v0_regs->cmd)) {
355 cdns->version = CDNS3_CONTROLLER_V0;
356 cdns->otg_v1_regs = NULL;
357 cdns->otg_regs = regs;
358 writel(1, &cdns->otg_v0_regs->simulate);
Peter Cheneed6ed62020-03-31 16:10:05 +0800359 dev_dbg(cdns->dev, "DRD version v0 (%08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100360 readl(&cdns->otg_v0_regs->version));
361 } else {
362 cdns->otg_v0_regs = NULL;
363 cdns->otg_v1_regs = regs;
364 cdns->otg_regs = (void *)&cdns->otg_v1_regs->cmd;
365 cdns->version = CDNS3_CONTROLLER_V1;
366 writel(1, &cdns->otg_v1_regs->simulate);
Peter Cheneed6ed62020-03-31 16:10:05 +0800367 dev_dbg(cdns->dev, "DRD version v1 (ID: %08x, rev: %08x)\n",
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100368 readl(&cdns->otg_v1_regs->did),
369 readl(&cdns->otg_v1_regs->rid));
370 }
371
372 state = OTGSTS_STRAP(readl(&cdns->otg_regs->sts));
373
374 /* Update dr_mode according to STRAP configuration. */
375 cdns->dr_mode = USB_DR_MODE_OTG;
376 if (state == OTGSTS_STRAP_HOST) {
377 dev_dbg(cdns->dev, "Controller strapped to HOST\n");
378 cdns->dr_mode = USB_DR_MODE_HOST;
379 } else if (state == OTGSTS_STRAP_GADGET) {
380 dev_dbg(cdns->dev, "Controller strapped to PERIPHERAL\n");
381 cdns->dr_mode = USB_DR_MODE_PERIPHERAL;
382 }
383
384 ret = devm_request_threaded_irq(cdns->dev, cdns->otg_irq,
385 cdns3_drd_irq,
386 cdns3_drd_thread_irq,
387 IRQF_SHARED,
388 dev_name(cdns->dev), cdns);
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100389 if (ret) {
390 dev_err(cdns->dev, "couldn't get otg_irq\n");
391 return ret;
392 }
393
394 state = readl(&cdns->otg_regs->sts);
Pawel Laszczakecf4f822020-07-13 12:05:48 +0200395 if (OTGSTS_OTG_NRDY(state)) {
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100396 dev_err(cdns->dev, "Cadence USB3 OTG device not ready\n");
397 return -ENODEV;
398 }
399
Pawel Laszczak27afe162020-07-13 12:05:47 +0200400 return 0;
Pawel Laszczak7733f6c2019-08-26 12:19:30 +0100401}
402
403int cdns3_drd_exit(struct cdns3 *cdns)
404{
405 cdns3_otg_disable_irq(cdns);
406 return 0;
407}