blob: 88c80fcc39f57bbb69dfacd92dd3b3ae691466e8 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbi72246da2011-08-19 18:10:58 +03002/**
3 * core.c - DesignWare USB3 DRD Controller Core file
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
Masahiro Yamadafe8abf32018-05-16 11:41:07 +090011#include <linux/clk.h>
Felipe Balbifa0ea132014-09-19 15:51:11 -050012#include <linux/version.h>
Felipe Balbia72e6582011-09-05 13:37:28 +030013#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030014#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/platform_device.h>
18#include <linux/pm_runtime.h>
19#include <linux/interrupt.h>
20#include <linux/ioport.h>
21#include <linux/io.h>
22#include <linux/list.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
Felipe Balbi457e84b2012-01-18 18:04:09 +020025#include <linux/of.h>
Heikki Krogerus404905a2014-09-25 10:57:02 +030026#include <linux/acpi.h>
Sekhar Nori63444752015-08-31 21:09:08 +053027#include <linux/pinctrl/consumer.h>
Masahiro Yamadafe8abf32018-05-16 11:41:07 +090028#include <linux/reset.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030029
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
Felipe Balbif7e846f2013-06-30 14:29:51 +030032#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050033#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030034
35#include "core.h"
36#include "gadget.h"
37#include "io.h"
38
39#include "debug.h"
40
Felipe Balbifc8bb912016-05-16 13:14:48 +030041#define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */
Felipe Balbi8300dd22011-10-18 13:54:01 +030042
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070043/**
44 * dwc3_get_dr_mode - Validates and sets dr_mode
45 * @dwc: pointer to our context structure
46 */
47static int dwc3_get_dr_mode(struct dwc3 *dwc)
48{
49 enum usb_dr_mode mode;
50 struct device *dev = dwc->dev;
51 unsigned int hw_mode;
52
53 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
54 dwc->dr_mode = USB_DR_MODE_OTG;
55
56 mode = dwc->dr_mode;
57 hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
58
59 switch (hw_mode) {
60 case DWC3_GHWPARAMS0_MODE_GADGET:
61 if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) {
62 dev_err(dev,
63 "Controller does not support host mode.\n");
64 return -EINVAL;
65 }
66 mode = USB_DR_MODE_PERIPHERAL;
67 break;
68 case DWC3_GHWPARAMS0_MODE_HOST:
69 if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
70 dev_err(dev,
71 "Controller does not support device mode.\n");
72 return -EINVAL;
73 }
74 mode = USB_DR_MODE_HOST;
75 break;
76 default:
77 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
78 mode = USB_DR_MODE_HOST;
79 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
80 mode = USB_DR_MODE_PERIPHERAL;
Thinh Nguyena7700462018-07-26 13:52:11 -070081
82 /*
83 * dwc_usb31 does not support OTG mode. If the controller
84 * supports DRD but the dr_mode is not specified or set to OTG,
85 * then set the mode to peripheral.
86 */
87 if (mode == USB_DR_MODE_OTG && dwc3_is_usb31(dwc))
88 mode = USB_DR_MODE_PERIPHERAL;
Thinh Nguyen9d6173e2016-09-06 19:22:03 -070089 }
90
91 if (mode != dwc->dr_mode) {
92 dev_warn(dev,
93 "Configuration mismatch. dr_mode forced to %s\n",
94 mode == USB_DR_MODE_HOST ? "host" : "gadget");
95
96 dwc->dr_mode = mode;
97 }
98
99 return 0;
100}
101
Roger Quadrosf09cc792018-02-27 13:30:19 +0200102void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100103{
104 u32 reg;
105
106 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
107 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
108 reg |= DWC3_GCTL_PRTCAPDIR(mode);
109 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Manu Gautamc4a51532018-01-18 16:54:30 +0530110
111 dwc->current_dr_role = mode;
Roger Quadros41ce1452017-04-04 12:49:18 +0300112}
Roger Quadros6b3261a2017-04-04 11:25:27 +0300113
Roger Quadros41ce1452017-04-04 12:49:18 +0300114static void __dwc3_set_mode(struct work_struct *work)
115{
116 struct dwc3 *dwc = work_to_dwc(work);
117 unsigned long flags;
118 int ret;
119
Roger Quadrosf09cc792018-02-27 13:30:19 +0200120 if (dwc->dr_mode != USB_DR_MODE_OTG)
121 return;
122
123 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
124 dwc3_otg_update(dwc, 0);
125
Roger Quadros41ce1452017-04-04 12:49:18 +0300126 if (!dwc->desired_dr_role)
127 return;
128
129 if (dwc->desired_dr_role == dwc->current_dr_role)
130 return;
131
Roger Quadrosf09cc792018-02-27 13:30:19 +0200132 if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
Roger Quadros41ce1452017-04-04 12:49:18 +0300133 return;
134
135 switch (dwc->current_dr_role) {
136 case DWC3_GCTL_PRTCAP_HOST:
137 dwc3_host_exit(dwc);
138 break;
139 case DWC3_GCTL_PRTCAP_DEVICE:
140 dwc3_gadget_exit(dwc);
141 dwc3_event_buffers_cleanup(dwc);
142 break;
Roger Quadrosf09cc792018-02-27 13:30:19 +0200143 case DWC3_GCTL_PRTCAP_OTG:
144 dwc3_otg_exit(dwc);
145 spin_lock_irqsave(&dwc->lock, flags);
146 dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE;
147 spin_unlock_irqrestore(&dwc->lock, flags);
148 dwc3_otg_update(dwc, 1);
149 break;
Roger Quadros41ce1452017-04-04 12:49:18 +0300150 default:
151 break;
152 }
153
154 spin_lock_irqsave(&dwc->lock, flags);
155
156 dwc3_set_prtcap(dwc, dwc->desired_dr_role);
157
Roger Quadros41ce1452017-04-04 12:49:18 +0300158 spin_unlock_irqrestore(&dwc->lock, flags);
159
160 switch (dwc->desired_dr_role) {
161 case DWC3_GCTL_PRTCAP_HOST:
162 ret = dwc3_host_init(dwc);
Felipe Balbi958d1a42017-06-05 17:22:10 +0300163 if (ret) {
Roger Quadros41ce1452017-04-04 12:49:18 +0300164 dev_err(dwc->dev, "failed to initialize host\n");
Felipe Balbi958d1a42017-06-05 17:22:10 +0300165 } else {
166 if (dwc->usb2_phy)
167 otg_set_vbus(dwc->usb2_phy->otg, true);
Manu Gautam644cbbc2017-09-27 16:49:22 +0530168 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
169 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
Vivek Gautamd8c80bb2017-10-09 14:00:51 +0200170 phy_calibrate(dwc->usb2_generic_phy);
Felipe Balbi958d1a42017-06-05 17:22:10 +0300171 }
Roger Quadros41ce1452017-04-04 12:49:18 +0300172 break;
173 case DWC3_GCTL_PRTCAP_DEVICE:
174 dwc3_event_buffers_setup(dwc);
Felipe Balbi958d1a42017-06-05 17:22:10 +0300175
176 if (dwc->usb2_phy)
177 otg_set_vbus(dwc->usb2_phy->otg, false);
Manu Gautam644cbbc2017-09-27 16:49:22 +0530178 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
179 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
Felipe Balbi958d1a42017-06-05 17:22:10 +0300180
Roger Quadros41ce1452017-04-04 12:49:18 +0300181 ret = dwc3_gadget_init(dwc);
182 if (ret)
183 dev_err(dwc->dev, "failed to initialize peripheral\n");
184 break;
Roger Quadrosf09cc792018-02-27 13:30:19 +0200185 case DWC3_GCTL_PRTCAP_OTG:
186 dwc3_otg_init(dwc);
187 dwc3_otg_update(dwc, 0);
188 break;
Roger Quadros41ce1452017-04-04 12:49:18 +0300189 default:
190 break;
191 }
Roger Quadrosf09cc792018-02-27 13:30:19 +0200192
Roger Quadros41ce1452017-04-04 12:49:18 +0300193}
194
195void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
196{
197 unsigned long flags;
198
199 spin_lock_irqsave(&dwc->lock, flags);
200 dwc->desired_dr_role = mode;
201 spin_unlock_irqrestore(&dwc->lock, flags);
202
Roger Quadros084a8042018-02-27 12:41:41 +0200203 queue_work(system_freezable_wq, &dwc->drd_work);
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100204}
Felipe Balbi8300dd22011-10-18 13:54:01 +0300205
Felipe Balbicf6d8672016-04-14 15:03:39 +0300206u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
207{
208 struct dwc3 *dwc = dep->dwc;
209 u32 reg;
210
211 dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
212 DWC3_GDBGFIFOSPACE_NUM(dep->number) |
213 DWC3_GDBGFIFOSPACE_TYPE(type));
214
215 reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
216
217 return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
218}
219
Felipe Balbi72246da2011-08-19 18:10:58 +0300220/**
221 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
222 * @dwc: pointer to our context structure
223 */
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530224static int dwc3_core_soft_reset(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300225{
226 u32 reg;
Felipe Balbif59dcab2016-03-11 10:51:52 +0200227 int retries = 1000;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530228 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300229
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300230 usb_phy_init(dwc->usb2_phy);
231 usb_phy_init(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530232 ret = phy_init(dwc->usb2_generic_phy);
233 if (ret < 0)
234 return ret;
235
236 ret = phy_init(dwc->usb3_generic_phy);
237 if (ret < 0) {
238 phy_exit(dwc->usb2_generic_phy);
239 return ret;
240 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300241
Felipe Balbif59dcab2016-03-11 10:51:52 +0200242 /*
243 * We're resetting only the device side because, if we're in host mode,
244 * XHCI driver will reset the host block. If dwc3 was configured for
245 * host-only mode, then we can return early.
246 */
Manu Gautamc4a51532018-01-18 16:54:30 +0530247 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST)
Felipe Balbif59dcab2016-03-11 10:51:52 +0200248 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300249
Felipe Balbif59dcab2016-03-11 10:51:52 +0200250 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
251 reg |= DWC3_DCTL_CSFTRST;
252 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300253
Felipe Balbif59dcab2016-03-11 10:51:52 +0200254 do {
255 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
256 if (!(reg & DWC3_DCTL_CSFTRST))
Thinh Nguyenfab38332018-03-16 15:33:48 -0700257 goto done;
Pratyush Anand45627ac2012-06-21 17:44:28 +0530258
Felipe Balbif59dcab2016-03-11 10:51:52 +0200259 udelay(1);
260 } while (--retries);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530261
Brian Norris00b42172018-01-17 13:22:49 -0800262 phy_exit(dwc->usb3_generic_phy);
263 phy_exit(dwc->usb2_generic_phy);
264
Felipe Balbif59dcab2016-03-11 10:51:52 +0200265 return -ETIMEDOUT;
Thinh Nguyenfab38332018-03-16 15:33:48 -0700266
267done:
268 /*
269 * For DWC_usb31 controller, once DWC3_DCTL_CSFTRST bit is cleared,
270 * we must wait at least 50ms before accessing the PHY domain
271 * (synchronization delay). DWC_usb31 programming guide section 1.3.2.
272 */
273 if (dwc3_is_usb31(dwc))
274 msleep(50);
275
276 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300277}
278
Masahiro Yamadafe8abf32018-05-16 11:41:07 +0900279static const struct clk_bulk_data dwc3_core_clks[] = {
280 { .id = "ref" },
281 { .id = "bus_early" },
282 { .id = "suspend" },
283};
284
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530285/*
286 * dwc3_frame_length_adjustment - Adjusts frame length if required
287 * @dwc3: Pointer to our controller context structure
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530288 */
Felipe Balbibcdb3272016-05-16 10:42:23 +0300289static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530290{
291 u32 reg;
292 u32 dft;
293
294 if (dwc->revision < DWC3_REVISION_250A)
295 return;
296
Felipe Balbibcdb3272016-05-16 10:42:23 +0300297 if (dwc->fladj == 0)
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530298 return;
299
300 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
301 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300302 if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530303 "request value same as default, ignoring\n")) {
304 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
Felipe Balbibcdb3272016-05-16 10:42:23 +0300305 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530306 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
307 }
308}
309
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300310/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300311 * dwc3_free_one_event_buffer - Frees one event buffer
312 * @dwc: Pointer to our controller context structure
313 * @evt: Pointer to event buffer to be freed
314 */
315static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
316 struct dwc3_event_buffer *evt)
317{
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530318 dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300319}
320
321/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800322 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300323 * @dwc: Pointer to our controller context structure
324 * @length: size of the event buffer
325 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800326 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 * otherwise ERR_PTR(errno).
328 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200329static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
330 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300331{
332 struct dwc3_event_buffer *evt;
333
Felipe Balbi380f0d22012-10-11 13:48:36 +0300334 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!evt)
336 return ERR_PTR(-ENOMEM);
337
338 evt->dwc = dwc;
339 evt->length = length;
John Yound9fa4c62016-11-15 12:54:15 +0200340 evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL);
341 if (!evt->cache)
342 return ERR_PTR(-ENOMEM);
343
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530344 evt->buf = dma_alloc_coherent(dwc->sysdev, length,
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200346 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300347 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300348
349 return evt;
350}
351
352/**
353 * dwc3_free_event_buffers - frees all allocated event buffers
354 * @dwc: Pointer to our controller context structure
355 */
356static void dwc3_free_event_buffers(struct dwc3 *dwc)
357{
358 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300359
Felipe Balbi696c8b12016-03-30 09:37:03 +0300360 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300361 if (evt)
362 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300363}
364
365/**
366 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800367 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 * @length: size of event buffer
369 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800370 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300371 * may contain some buffers allocated but not all which were requested.
372 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500373static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300374{
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300375 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300377 evt = dwc3_alloc_one_event_buffer(dwc, length);
378 if (IS_ERR(evt)) {
379 dev_err(dwc->dev, "can't allocate event buffer\n");
380 return PTR_ERR(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300381 }
Felipe Balbi696c8b12016-03-30 09:37:03 +0300382 dwc->ev_buf = evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300383
384 return 0;
385}
386
387/**
388 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800389 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 *
391 * Returns 0 on success otherwise negative errno.
392 */
Roger Quadrosf09cc792018-02-27 13:30:19 +0200393int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300394{
395 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396
Felipe Balbi696c8b12016-03-30 09:37:03 +0300397 evt = dwc->ev_buf;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300398 evt->lpos = 0;
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300399 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
400 lower_32_bits(evt->dma));
401 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
402 upper_32_bits(evt->dma));
403 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
404 DWC3_GEVNTSIZ_SIZE(evt->length));
405 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300406
407 return 0;
408}
409
Roger Quadrosf09cc792018-02-27 13:30:19 +0200410void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300411{
412 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +0300413
Felipe Balbi696c8b12016-03-30 09:37:03 +0300414 evt = dwc->ev_buf;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300415
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300416 evt->lpos = 0;
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300417
Felipe Balbi660e9bd2016-03-30 09:26:24 +0300418 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
419 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
420 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
421 | DWC3_GEVNTSIZ_SIZE(0));
422 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300423}
424
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600425static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
426{
427 if (!dwc->has_hibernation)
428 return 0;
429
430 if (!dwc->nr_scratch)
431 return 0;
432
433 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
434 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
435 if (!dwc->scratchbuf)
436 return -ENOMEM;
437
438 return 0;
439}
440
441static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
442{
443 dma_addr_t scratch_addr;
444 u32 param;
445 int ret;
446
447 if (!dwc->has_hibernation)
448 return 0;
449
450 if (!dwc->nr_scratch)
451 return 0;
452
453 /* should never fall here */
454 if (!WARN_ON(dwc->scratchbuf))
455 return 0;
456
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530457 scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf,
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600458 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
459 DMA_BIDIRECTIONAL);
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530460 if (dma_mapping_error(dwc->sysdev, scratch_addr)) {
461 dev_err(dwc->sysdev, "failed to map scratch buffer\n");
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600462 ret = -EFAULT;
463 goto err0;
464 }
465
466 dwc->scratch_addr = scratch_addr;
467
468 param = lower_32_bits(scratch_addr);
469
470 ret = dwc3_send_gadget_generic_command(dwc,
471 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
472 if (ret < 0)
473 goto err1;
474
475 param = upper_32_bits(scratch_addr);
476
477 ret = dwc3_send_gadget_generic_command(dwc,
478 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
479 if (ret < 0)
480 goto err1;
481
482 return 0;
483
484err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530485 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600486 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
487
488err0:
489 return ret;
490}
491
492static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
493{
494 if (!dwc->has_hibernation)
495 return;
496
497 if (!dwc->nr_scratch)
498 return;
499
500 /* should never fall here */
501 if (!WARN_ON(dwc->scratchbuf))
502 return;
503
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530504 dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch *
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600505 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
506 kfree(dwc->scratchbuf);
507}
508
Felipe Balbi789451f62011-05-05 15:53:10 +0300509static void dwc3_core_num_eps(struct dwc3 *dwc)
510{
511 struct dwc3_hwparams *parms = &dwc->hwparams;
512
Bryan O'Donoghue47d39462017-01-31 20:58:10 +0000513 dwc->num_eps = DWC3_NUM_EPS(parms);
Felipe Balbi789451f62011-05-05 15:53:10 +0300514}
515
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500516static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300517{
518 struct dwc3_hwparams *parms = &dwc->hwparams;
519
520 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
521 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
522 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
523 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
524 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
525 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
526 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
527 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
528 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
529}
530
Roger Quadros98112042018-02-12 15:30:08 +0200531static int dwc3_core_ulpi_init(struct dwc3 *dwc)
532{
533 int intf;
534 int ret = 0;
535
536 intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3);
537
538 if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI ||
539 (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI &&
540 dwc->hsphy_interface &&
541 !strncmp(dwc->hsphy_interface, "ulpi", 4)))
542 ret = dwc3_ulpi_init(dwc);
543
544 return ret;
545}
546
Felipe Balbi72246da2011-08-19 18:10:58 +0300547/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800548 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
549 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300550 *
551 * Returns 0 on success. The USB PHY interfaces are configured but not
552 * initialized. The PHY interfaces and the PHYs get initialized together with
553 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800554 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300555static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800556{
557 u32 reg;
558
559 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
560
Huang Rui2164a472014-10-28 19:54:35 +0800561 /*
Felipe Balbi1966b862016-08-03 14:16:15 +0300562 * Make sure UX_EXIT_PX is cleared as that causes issues with some
563 * PHYs. Also, this bit is not supposed to be used in normal operation.
564 */
565 reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
566
567 /*
Huang Rui2164a472014-10-28 19:54:35 +0800568 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
569 * to '0' during coreConsultant configuration. So default value
570 * will be '0' when the core is reset. Application needs to set it
571 * to '1' after the core initialization is completed.
572 */
573 if (dwc->revision > DWC3_REVISION_194A)
574 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
575
Huang Ruib5a65c42014-10-28 19:54:28 +0800576 if (dwc->u2ss_inp3_quirk)
577 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
578
Rajesh Bhagate58dd352016-03-14 14:40:50 +0530579 if (dwc->dis_rxdet_inp3_quirk)
580 reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
581
Huang Ruidf31f5b2014-10-28 19:54:29 +0800582 if (dwc->req_p1p2p3_quirk)
583 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
584
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800585 if (dwc->del_p1p2p3_quirk)
586 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
587
Huang Rui41c06ff2014-10-28 19:54:31 +0800588 if (dwc->del_phy_power_chg_quirk)
589 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
590
Huang Ruifb67afc2014-10-28 19:54:32 +0800591 if (dwc->lfps_filter_quirk)
592 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
593
Huang Rui14f4ac52014-10-28 19:54:33 +0800594 if (dwc->rx_detect_poll_quirk)
595 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
596
Huang Rui6b6a0c92014-10-31 11:11:12 +0800597 if (dwc->tx_de_emphasis_quirk)
598 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
599
Felipe Balbicd72f892014-11-06 11:31:00 -0600600 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800601 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
602
William Wu00fe0812016-08-16 22:44:39 +0800603 if (dwc->dis_del_phy_power_chg_quirk)
604 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
605
Huang Ruib5a65c42014-10-28 19:54:28 +0800606 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
607
Huang Rui2164a472014-10-28 19:54:35 +0800608 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
609
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300610 /* Select the HS PHY interface */
611 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
612 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500613 if (dwc->hsphy_interface &&
614 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300615 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300616 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500617 } else if (dwc->hsphy_interface &&
618 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300619 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300620 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300621 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300622 /* Relying on default value. */
623 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
624 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300625 }
626 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300627 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300628 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300629 default:
630 break;
631 }
632
William Wu32f2ed82016-08-16 22:44:38 +0800633 switch (dwc->hsphy_mode) {
634 case USBPHY_INTERFACE_MODE_UTMI:
635 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
636 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
637 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
638 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
639 break;
640 case USBPHY_INTERFACE_MODE_UTMIW:
641 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
642 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
643 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
644 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
645 break;
646 default:
647 break;
648 }
649
Huang Rui2164a472014-10-28 19:54:35 +0800650 /*
651 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
652 * '0' during coreConsultant configuration. So default value will
653 * be '0' when the core is reset. Application needs to set it to
654 * '1' after the core initialization is completed.
655 */
656 if (dwc->revision > DWC3_REVISION_194A)
657 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
658
Felipe Balbicd72f892014-11-06 11:31:00 -0600659 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800660 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
661
John Younec791d12015-10-02 20:30:57 -0700662 if (dwc->dis_enblslpm_quirk)
663 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
664
William Wu16199f32016-08-16 22:44:37 +0800665 if (dwc->dis_u2_freeclk_exists_quirk)
666 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
667
Huang Rui2164a472014-10-28 19:54:35 +0800668 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300669
670 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800671}
672
Felipe Balbic499ff72016-05-16 10:49:01 +0300673static void dwc3_core_exit(struct dwc3 *dwc)
674{
675 dwc3_event_buffers_cleanup(dwc);
676
677 usb_phy_shutdown(dwc->usb2_phy);
678 usb_phy_shutdown(dwc->usb3_phy);
679 phy_exit(dwc->usb2_generic_phy);
680 phy_exit(dwc->usb3_generic_phy);
681
682 usb_phy_set_suspend(dwc->usb2_phy, 1);
683 usb_phy_set_suspend(dwc->usb3_phy, 1);
684 phy_power_off(dwc->usb2_generic_phy);
685 phy_power_off(dwc->usb3_generic_phy);
Masahiro Yamadafe8abf32018-05-16 11:41:07 +0900686 clk_bulk_disable(dwc->num_clks, dwc->clks);
687 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
688 reset_control_assert(dwc->reset);
Felipe Balbic499ff72016-05-16 10:49:01 +0300689}
690
Felipe Balbi07599562016-10-14 16:19:01 +0300691static bool dwc3_core_is_valid(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300692{
Felipe Balbi07599562016-10-14 16:19:01 +0300693 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300694
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200695 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
Felipe Balbi07599562016-10-14 16:19:01 +0300696
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200697 /* This should read as U3 followed by revision number */
John Youn690fb372015-09-04 19:15:10 -0700698 if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) {
699 /* Detected DWC_usb3 IP */
700 dwc->revision = reg;
701 } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) {
702 /* Detected DWC_usb31 IP */
703 dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER);
704 dwc->revision |= DWC3_REVISION_IS_DWC31;
705 } else {
Felipe Balbi07599562016-10-14 16:19:01 +0300706 return false;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200707 }
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200708
Felipe Balbi07599562016-10-14 16:19:01 +0300709 return true;
710}
Felipe Balbifa0ea132014-09-19 15:51:11 -0500711
Felipe Balbi941f9182016-10-14 16:23:24 +0300712static void dwc3_core_setup_global_control(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300713{
Felipe Balbi941f9182016-10-14 16:23:24 +0300714 u32 hwparams4 = dwc->hwparams.hwparams4;
715 u32 reg;
Felipe Balbic499ff72016-05-16 10:49:01 +0300716
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100717 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800718 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100719
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100720 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100721 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600722 /**
723 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
724 * issue which would cause xHCI compliance tests to fail.
725 *
726 * Because of that we cannot enable clock gating on such
727 * configurations.
728 *
729 * Refers to:
730 *
731 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
732 * SOF/ITP Mode Used
733 */
734 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
735 dwc->dr_mode == USB_DR_MODE_OTG) &&
736 (dwc->revision >= DWC3_REVISION_210A &&
737 dwc->revision <= DWC3_REVISION_250A))
738 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
739 else
740 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100741 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600742 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
743 /* enable hibernation here */
744 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800745
746 /*
747 * REVISIT Enabling this bit so that host-mode hibernation
748 * will work. Device-mode hibernation is not yet implemented.
749 */
750 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600751 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100752 default:
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200753 /* nothing */
754 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100755 }
756
Huang Rui946bd572014-10-28 19:54:23 +0800757 /* check if current dwc3 is on simulation board */
758 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +0200759 dev_info(dwc->dev, "Running with FPGA optmizations\n");
Huang Rui946bd572014-10-28 19:54:23 +0800760 dwc->is_fpga = true;
761 }
762
Huang Rui3b812212014-10-28 19:54:25 +0800763 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
764 "disable_scramble cannot be used on non-FPGA builds\n");
765
766 if (dwc->disable_scramble_quirk && dwc->is_fpga)
767 reg |= DWC3_GCTL_DISSCRAMBLE;
768 else
769 reg &= ~DWC3_GCTL_DISSCRAMBLE;
770
Huang Rui9a5b2f32014-10-28 19:54:27 +0800771 if (dwc->u2exit_lfps_quirk)
772 reg |= DWC3_GCTL_U2EXIT_LFPS;
773
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100774 /*
775 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800776 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100777 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800778 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100779 */
780 if (dwc->revision < DWC3_REVISION_190A)
781 reg |= DWC3_GCTL_U2RSTECN;
782
783 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Felipe Balbi941f9182016-10-14 16:23:24 +0300784}
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100785
Felipe Balbif54edb52017-06-05 17:03:18 +0300786static int dwc3_core_get_phy(struct dwc3 *dwc);
Roger Quadros98112042018-02-12 15:30:08 +0200787static int dwc3_core_ulpi_init(struct dwc3 *dwc);
Felipe Balbif54edb52017-06-05 17:03:18 +0300788
Pengbo Mud9612c22018-07-23 18:32:37 +0800789/* set global incr burst type configuration registers */
790static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
791{
792 struct device *dev = dwc->dev;
793 /* incrx_mode : for INCR burst type. */
794 bool incrx_mode;
795 /* incrx_size : for size of INCRX burst. */
796 u32 incrx_size;
797 u32 *vals;
798 u32 cfg;
799 int ntype;
800 int ret;
801 int i;
802
803 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
804
805 /*
806 * Handle property "snps,incr-burst-type-adjustment".
807 * Get the number of value from this property:
808 * result <= 0, means this property is not supported.
809 * result = 1, means INCRx burst mode supported.
810 * result > 1, means undefined length burst mode supported.
811 */
812 ntype = device_property_read_u32_array(dev,
813 "snps,incr-burst-type-adjustment", NULL, 0);
814 if (ntype <= 0)
815 return;
816
817 vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL);
818 if (!vals) {
819 dev_err(dev, "Error to get memory\n");
820 return;
821 }
822
823 /* Get INCR burst type, and parse it */
824 ret = device_property_read_u32_array(dev,
825 "snps,incr-burst-type-adjustment", vals, ntype);
826 if (ret) {
827 dev_err(dev, "Error to get property\n");
828 return;
829 }
830
831 incrx_size = *vals;
832
833 if (ntype > 1) {
834 /* INCRX (undefined length) burst mode */
835 incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
836 for (i = 1; i < ntype; i++) {
837 if (vals[i] > incrx_size)
838 incrx_size = vals[i];
839 }
840 } else {
841 /* INCRX burst mode */
842 incrx_mode = INCRX_BURST_MODE;
843 }
844
845 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
846 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
847 if (incrx_mode)
848 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
849 switch (incrx_size) {
850 case 256:
851 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
852 break;
853 case 128:
854 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
855 break;
856 case 64:
857 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
858 break;
859 case 32:
860 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
861 break;
862 case 16:
863 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
864 break;
865 case 8:
866 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
867 break;
868 case 4:
869 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
870 break;
871 case 1:
872 break;
873 default:
874 dev_err(dev, "Invalid property\n");
875 break;
876 }
877
878 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
879}
880
Felipe Balbi941f9182016-10-14 16:23:24 +0300881/**
882 * dwc3_core_init - Low-level initialization of DWC3 Core
883 * @dwc: Pointer to our controller context structure
884 *
885 * Returns 0 on success otherwise negative errno.
886 */
887static int dwc3_core_init(struct dwc3 *dwc)
888{
889 u32 reg;
890 int ret;
891
892 if (!dwc3_core_is_valid(dwc)) {
893 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
894 ret = -ENODEV;
895 goto err0;
896 }
897
898 /*
899 * Write Linux Version Code to our GUID register so it's easy to figure
900 * out which kernel version a bug was found.
901 */
902 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
903
904 /* Handle USB2.0-only core configuration */
905 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
906 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
907 if (dwc->maximum_speed == USB_SPEED_SUPER)
908 dwc->maximum_speed = USB_SPEED_HIGH;
909 }
910
Felipe Balbi941f9182016-10-14 16:23:24 +0300911 ret = dwc3_phy_setup(dwc);
912 if (ret)
913 goto err0;
914
Roger Quadros98112042018-02-12 15:30:08 +0200915 if (!dwc->ulpi_ready) {
916 ret = dwc3_core_ulpi_init(dwc);
917 if (ret)
918 goto err0;
919 dwc->ulpi_ready = true;
920 }
921
922 if (!dwc->phys_ready) {
923 ret = dwc3_core_get_phy(dwc);
924 if (ret)
925 goto err0a;
926 dwc->phys_ready = true;
927 }
928
929 ret = dwc3_core_soft_reset(dwc);
930 if (ret)
931 goto err0a;
932
Felipe Balbi941f9182016-10-14 16:23:24 +0300933 dwc3_core_setup_global_control(dwc);
Felipe Balbic499ff72016-05-16 10:49:01 +0300934 dwc3_core_num_eps(dwc);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600935
936 ret = dwc3_setup_scratch_buffers(dwc);
937 if (ret)
Felipe Balbic499ff72016-05-16 10:49:01 +0300938 goto err1;
939
940 /* Adjust Frame Length */
941 dwc3_frame_length_adjustment(dwc);
942
Pengbo Mud9612c22018-07-23 18:32:37 +0800943 dwc3_set_incr_burst_type(dwc);
944
Felipe Balbic499ff72016-05-16 10:49:01 +0300945 usb_phy_set_suspend(dwc->usb2_phy, 0);
946 usb_phy_set_suspend(dwc->usb3_phy, 0);
947 ret = phy_power_on(dwc->usb2_generic_phy);
948 if (ret < 0)
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600949 goto err2;
950
Felipe Balbic499ff72016-05-16 10:49:01 +0300951 ret = phy_power_on(dwc->usb3_generic_phy);
952 if (ret < 0)
953 goto err3;
954
955 ret = dwc3_event_buffers_setup(dwc);
956 if (ret) {
957 dev_err(dwc->dev, "failed to setup event buffers\n");
958 goto err4;
959 }
960
John Youn06281d42016-08-22 15:39:13 -0700961 /*
962 * ENDXFER polling is available on version 3.10a and later of
963 * the DWC_usb3 controller. It is NOT available in the
964 * DWC_usb31 controller.
965 */
966 if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) {
967 reg = dwc3_readl(dwc->regs, DWC3_GUCTL2);
968 reg |= DWC3_GUCTL2_RST_ACTBITLATER;
969 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg);
970 }
971
William Wu65db7a02017-04-19 20:11:38 +0800972 if (dwc->revision >= DWC3_REVISION_250A) {
John Youn0bb39ca2016-10-12 18:00:55 -0700973 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
William Wu65db7a02017-04-19 20:11:38 +0800974
975 /*
976 * Enable hardware control of sending remote wakeup
977 * in HS when the device is in the L1 state.
978 */
979 if (dwc->revision >= DWC3_REVISION_290A)
980 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
981
982 if (dwc->dis_tx_ipgap_linecheck_quirk)
983 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
984
John Youn0bb39ca2016-10-12 18:00:55 -0700985 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
986 }
987
Anurag Kumar Vulishab138e232018-07-27 13:11:20 +0530988 if (dwc->dr_mode == USB_DR_MODE_HOST ||
989 dwc->dr_mode == USB_DR_MODE_OTG) {
990 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
991
992 /*
993 * Enable Auto retry Feature to make the controller operating in
994 * Host mode on seeing transaction errors(CRC errors or internal
995 * overrun scenerios) on IN transfers to reply to the device
996 * with a non-terminating retry ACK (i.e, an ACK transcation
997 * packet with Retry=1 & Nump != 0)
998 */
999 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1000
1001 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1002 }
1003
Thinh Nguyen938a5ad2018-03-16 15:35:44 -07001004 /*
1005 * Must config both number of packets and max burst settings to enable
1006 * RX and/or TX threshold.
1007 */
1008 if (dwc3_is_usb31(dwc) && dwc->dr_mode == USB_DR_MODE_HOST) {
1009 u8 rx_thr_num = dwc->rx_thr_num_pkt_prd;
1010 u8 rx_maxburst = dwc->rx_max_burst_prd;
1011 u8 tx_thr_num = dwc->tx_thr_num_pkt_prd;
1012 u8 tx_maxburst = dwc->tx_max_burst_prd;
1013
1014 if (rx_thr_num && rx_maxburst) {
1015 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1016 reg |= DWC31_RXTHRNUMPKTSEL_PRD;
1017
1018 reg &= ~DWC31_RXTHRNUMPKT_PRD(~0);
1019 reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num);
1020
1021 reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0);
1022 reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst);
1023
1024 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1025 }
1026
1027 if (tx_thr_num && tx_maxburst) {
1028 reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG);
1029 reg |= DWC31_TXTHRNUMPKTSEL_PRD;
1030
1031 reg &= ~DWC31_TXTHRNUMPKT_PRD(~0);
1032 reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num);
1033
1034 reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0);
1035 reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst);
1036
1037 dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg);
1038 }
1039 }
1040
Felipe Balbi72246da2011-08-19 18:10:58 +03001041 return 0;
1042
Felipe Balbic499ff72016-05-16 10:49:01 +03001043err4:
Vivek Gautam9b9d7cd2016-10-21 16:21:07 +05301044 phy_power_off(dwc->usb3_generic_phy);
Felipe Balbic499ff72016-05-16 10:49:01 +03001045
1046err3:
Vivek Gautam9b9d7cd2016-10-21 16:21:07 +05301047 phy_power_off(dwc->usb2_generic_phy);
Felipe Balbic499ff72016-05-16 10:49:01 +03001048
Felipe Balbi0ffcaf32013-12-19 13:04:28 -06001049err2:
Felipe Balbic499ff72016-05-16 10:49:01 +03001050 usb_phy_set_suspend(dwc->usb2_phy, 1);
1051 usb_phy_set_suspend(dwc->usb3_phy, 1);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -06001052
1053err1:
1054 usb_phy_shutdown(dwc->usb2_phy);
1055 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301056 phy_exit(dwc->usb2_generic_phy);
1057 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -06001058
Roger Quadros98112042018-02-12 15:30:08 +02001059err0a:
1060 dwc3_ulpi_exit(dwc);
1061
Felipe Balbi72246da2011-08-19 18:10:58 +03001062err0:
1063 return ret;
1064}
1065
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001066static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001067{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001068 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +03001069 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001070 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001071
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301072 if (node) {
1073 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1074 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -05001075 } else {
1076 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1077 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301078 }
1079
Felipe Balbid105e7f2013-03-15 10:52:08 +02001080 if (IS_ERR(dwc->usb2_phy)) {
1081 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +05301082 if (ret == -ENXIO || ret == -ENODEV) {
1083 dwc->usb2_phy = NULL;
1084 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +02001085 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +05301086 } else {
1087 dev_err(dev, "no usb2 phy configured\n");
1088 return ret;
1089 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +03001090 }
1091
Felipe Balbid105e7f2013-03-15 10:52:08 +02001092 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -05001093 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +05301094 if (ret == -ENXIO || ret == -ENODEV) {
1095 dwc->usb3_phy = NULL;
1096 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +02001097 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +05301098 } else {
1099 dev_err(dev, "no usb3 phy configured\n");
1100 return ret;
1101 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +03001102 }
1103
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301104 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
1105 if (IS_ERR(dwc->usb2_generic_phy)) {
1106 ret = PTR_ERR(dwc->usb2_generic_phy);
1107 if (ret == -ENOSYS || ret == -ENODEV) {
1108 dwc->usb2_generic_phy = NULL;
1109 } else if (ret == -EPROBE_DEFER) {
1110 return ret;
1111 } else {
1112 dev_err(dev, "no usb2 phy configured\n");
1113 return ret;
1114 }
1115 }
1116
1117 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
1118 if (IS_ERR(dwc->usb3_generic_phy)) {
1119 ret = PTR_ERR(dwc->usb3_generic_phy);
1120 if (ret == -ENOSYS || ret == -ENODEV) {
1121 dwc->usb3_generic_phy = NULL;
1122 } else if (ret == -EPROBE_DEFER) {
1123 return ret;
1124 } else {
1125 dev_err(dev, "no usb3 phy configured\n");
1126 return ret;
1127 }
1128 }
1129
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001130 return 0;
1131}
1132
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001133static int dwc3_core_init_mode(struct dwc3 *dwc)
1134{
1135 struct device *dev = dwc->dev;
1136 int ret;
1137
1138 switch (dwc->dr_mode) {
1139 case USB_DR_MODE_PERIPHERAL:
Roger Quadros41ce1452017-04-04 12:49:18 +03001140 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi958d1a42017-06-05 17:22:10 +03001141
1142 if (dwc->usb2_phy)
1143 otg_set_vbus(dwc->usb2_phy->otg, false);
Manu Gautam644cbbc2017-09-27 16:49:22 +05301144 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
1145 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
Felipe Balbi958d1a42017-06-05 17:22:10 +03001146
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001147 ret = dwc3_gadget_init(dwc);
1148 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +03001149 if (ret != -EPROBE_DEFER)
1150 dev_err(dev, "failed to initialize gadget\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001151 return ret;
1152 }
1153 break;
1154 case USB_DR_MODE_HOST:
Roger Quadros41ce1452017-04-04 12:49:18 +03001155 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
Felipe Balbi958d1a42017-06-05 17:22:10 +03001156
1157 if (dwc->usb2_phy)
1158 otg_set_vbus(dwc->usb2_phy->otg, true);
Manu Gautam644cbbc2017-09-27 16:49:22 +05301159 phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1160 phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
Felipe Balbi958d1a42017-06-05 17:22:10 +03001161
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001162 ret = dwc3_host_init(dwc);
1163 if (ret) {
Roger Quadros9522def2016-06-10 14:48:38 +03001164 if (ret != -EPROBE_DEFER)
1165 dev_err(dev, "failed to initialize host\n");
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001166 return ret;
1167 }
Vivek Gautamd8c80bb2017-10-09 14:00:51 +02001168 phy_calibrate(dwc->usb2_generic_phy);
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001169 break;
1170 case USB_DR_MODE_OTG:
Roger Quadros41ce1452017-04-04 12:49:18 +03001171 INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
Roger Quadros98403542017-04-05 13:39:31 +03001172 ret = dwc3_drd_init(dwc);
1173 if (ret) {
1174 if (ret != -EPROBE_DEFER)
1175 dev_err(dev, "failed to initialize dual-role\n");
1176 return ret;
1177 }
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001178 break;
1179 default:
1180 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
1181 return -EINVAL;
1182 }
1183
1184 return 0;
1185}
1186
1187static void dwc3_core_exit_mode(struct dwc3 *dwc)
1188{
1189 switch (dwc->dr_mode) {
1190 case USB_DR_MODE_PERIPHERAL:
1191 dwc3_gadget_exit(dwc);
1192 break;
1193 case USB_DR_MODE_HOST:
1194 dwc3_host_exit(dwc);
1195 break;
1196 case USB_DR_MODE_OTG:
Roger Quadros98403542017-04-05 13:39:31 +03001197 dwc3_drd_exit(dwc);
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001198 break;
1199 default:
1200 /* do nothing */
1201 break;
1202 }
1203}
1204
Felipe Balbic5ac6112016-10-14 16:30:52 +03001205static void dwc3_get_properties(struct dwc3 *dwc)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001206{
Felipe Balbic5ac6112016-10-14 16:30:52 +03001207 struct device *dev = dwc->dev;
Huang Rui80caf7d2014-10-28 19:54:26 +08001208 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001209 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +08001210 u8 hird_threshold;
Thinh Nguyen938a5ad2018-03-16 15:35:44 -07001211 u8 rx_thr_num_pkt_prd;
1212 u8 rx_max_burst_prd;
1213 u8 tx_thr_num_pkt_prd;
1214 u8 tx_max_burst_prd;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001215
Huang Rui80caf7d2014-10-28 19:54:26 +08001216 /* default to highest possible threshold */
1217 lpm_nyet_threshold = 0xff;
1218
Huang Rui6b6a0c92014-10-31 11:11:12 +08001219 /* default to -3.5dB de-emphasis */
1220 tx_de_emphasis = 1;
1221
Huang Rui460d0982014-10-31 11:11:18 +08001222 /*
1223 * default to assert utmi_sleep_n and use maximum allowed HIRD
1224 * threshold value of 0b1100
1225 */
1226 hird_threshold = 12;
1227
Heikki Krogerus63863b92015-09-21 11:14:32 +03001228 dwc->maximum_speed = usb_get_maximum_speed(dev);
Heikki Krogerus06e71142015-09-21 11:14:34 +03001229 dwc->dr_mode = usb_get_dr_mode(dev);
William Wu32f2ed82016-08-16 22:44:38 +08001230 dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node);
Heikki Krogerus63863b92015-09-21 11:14:32 +03001231
Arnd Bergmannd64ff402016-11-17 17:13:47 +05301232 dwc->sysdev_is_parent = device_property_read_bool(dev,
1233 "linux,sysdev_is_parent");
1234 if (dwc->sysdev_is_parent)
1235 dwc->sysdev = dwc->dev->parent;
1236 else
1237 dwc->sysdev = dwc->dev;
1238
Heikki Krogerus3d128912015-09-21 11:14:35 +03001239 dwc->has_lpm_erratum = device_property_read_bool(dev,
Huang Rui80caf7d2014-10-28 19:54:26 +08001240 "snps,has-lpm-erratum");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001241 device_property_read_u8(dev, "snps,lpm-nyet-threshold",
Huang Rui80caf7d2014-10-28 19:54:26 +08001242 &lpm_nyet_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001243 dwc->is_utmi_l1_suspend = device_property_read_bool(dev,
Huang Rui460d0982014-10-31 11:11:18 +08001244 "snps,is-utmi-l1-suspend");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001245 device_property_read_u8(dev, "snps,hird-threshold",
Huang Rui460d0982014-10-31 11:11:18 +08001246 &hird_threshold);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001247 dwc->usb3_lpm_capable = device_property_read_bool(dev,
Robert Baldygaeac68e82015-03-09 15:06:12 +01001248 "snps,usb3_lpm_capable");
Thinh Nguyen938a5ad2018-03-16 15:35:44 -07001249 device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd",
1250 &rx_thr_num_pkt_prd);
1251 device_property_read_u8(dev, "snps,rx-max-burst-prd",
1252 &rx_max_burst_prd);
1253 device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd",
1254 &tx_thr_num_pkt_prd);
1255 device_property_read_u8(dev, "snps,tx-max-burst-prd",
1256 &tx_max_burst_prd);
Felipe Balbi3c9f94a2014-04-16 15:08:29 -05001257
Heikki Krogerus3d128912015-09-21 11:14:35 +03001258 dwc->disable_scramble_quirk = device_property_read_bool(dev,
Huang Rui3b812212014-10-28 19:54:25 +08001259 "snps,disable_scramble_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001260 dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
Huang Rui9a5b2f32014-10-28 19:54:27 +08001261 "snps,u2exit_lfps_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001262 dwc->u2ss_inp3_quirk = device_property_read_bool(dev,
Huang Ruib5a65c42014-10-28 19:54:28 +08001263 "snps,u2ss_inp3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001264 dwc->req_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruidf31f5b2014-10-28 19:54:29 +08001265 "snps,req_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001266 dwc->del_p1p2p3_quirk = device_property_read_bool(dev,
Huang Ruia2a1d0f2014-10-28 19:54:30 +08001267 "snps,del_p1p2p3_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001268 dwc->del_phy_power_chg_quirk = device_property_read_bool(dev,
Huang Rui41c06ff2014-10-28 19:54:31 +08001269 "snps,del_phy_power_chg_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001270 dwc->lfps_filter_quirk = device_property_read_bool(dev,
Huang Ruifb67afc2014-10-28 19:54:32 +08001271 "snps,lfps_filter_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001272 dwc->rx_detect_poll_quirk = device_property_read_bool(dev,
Huang Rui14f4ac52014-10-28 19:54:33 +08001273 "snps,rx_detect_poll_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001274 dwc->dis_u3_susphy_quirk = device_property_read_bool(dev,
Huang Rui59acfa22014-10-31 11:11:13 +08001275 "snps,dis_u3_susphy_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001276 dwc->dis_u2_susphy_quirk = device_property_read_bool(dev,
Huang Rui0effe0a2014-10-31 11:11:14 +08001277 "snps,dis_u2_susphy_quirk");
John Younec791d12015-10-02 20:30:57 -07001278 dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
1279 "snps,dis_enblslpm_quirk");
Rajesh Bhagate58dd352016-03-14 14:40:50 +05301280 dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
1281 "snps,dis_rxdet_inp3_quirk");
William Wu16199f32016-08-16 22:44:37 +08001282 dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev,
1283 "snps,dis-u2-freeclk-exists-quirk");
William Wu00fe0812016-08-16 22:44:39 +08001284 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev,
1285 "snps,dis-del-phy-power-chg-quirk");
William Wu65db7a02017-04-19 20:11:38 +08001286 dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev,
1287 "snps,dis-tx-ipgap-linecheck-quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +08001288
Heikki Krogerus3d128912015-09-21 11:14:35 +03001289 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
Huang Rui6b6a0c92014-10-31 11:11:12 +08001290 "snps,tx_de_emphasis_quirk");
Heikki Krogerus3d128912015-09-21 11:14:35 +03001291 device_property_read_u8(dev, "snps,tx_de_emphasis",
Huang Rui6b6a0c92014-10-31 11:11:12 +08001292 &tx_de_emphasis);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001293 device_property_read_string(dev, "snps,hsphy_interface",
1294 &dwc->hsphy_interface);
1295 device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
Felipe Balbibcdb3272016-05-16 10:42:23 +03001296 &dwc->fladj);
Heikki Krogerus3d128912015-09-21 11:14:35 +03001297
Roger Quadros42bf02e2017-10-31 15:11:55 +02001298 dwc->dis_metastability_quirk = device_property_read_bool(dev,
1299 "snps,dis_metastability_quirk");
1300
Huang Rui80caf7d2014-10-28 19:54:26 +08001301 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +08001302 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +08001303
Huang Rui460d0982014-10-31 11:11:18 +08001304 dwc->hird_threshold = hird_threshold
1305 | (dwc->is_utmi_l1_suspend << 4);
1306
Thinh Nguyen938a5ad2018-03-16 15:35:44 -07001307 dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd;
1308 dwc->rx_max_burst_prd = rx_max_burst_prd;
1309
1310 dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd;
1311 dwc->tx_max_burst_prd = tx_max_burst_prd;
1312
John Youncf40b862016-11-14 12:32:43 -08001313 dwc->imod_interval = 0;
1314}
1315
1316/* check whether the core supports IMOD */
1317bool dwc3_has_imod(struct dwc3 *dwc)
1318{
1319 return ((dwc3_is_usb3(dwc) &&
1320 dwc->revision >= DWC3_REVISION_300A) ||
1321 (dwc3_is_usb31(dwc) &&
1322 dwc->revision >= DWC3_USB31_REVISION_120A));
Felipe Balbic5ac6112016-10-14 16:30:52 +03001323}
1324
John Youn7ac51a12016-11-10 17:08:51 -08001325static void dwc3_check_params(struct dwc3 *dwc)
1326{
1327 struct device *dev = dwc->dev;
1328
John Youncf40b862016-11-14 12:32:43 -08001329 /* Check for proper value of imod_interval */
1330 if (dwc->imod_interval && !dwc3_has_imod(dwc)) {
1331 dev_warn(dwc->dev, "Interrupt moderation not supported\n");
1332 dwc->imod_interval = 0;
1333 }
1334
John Youn28632b42016-11-14 12:32:45 -08001335 /*
1336 * Workaround for STAR 9000961433 which affects only version
1337 * 3.00a of the DWC_usb3 core. This prevents the controller
1338 * interrupt from being masked while handling events. IMOD
1339 * allows us to work around this issue. Enable it for the
1340 * affected version.
1341 */
1342 if (!dwc->imod_interval &&
1343 (dwc->revision == DWC3_REVISION_300A))
1344 dwc->imod_interval = 1;
1345
John Youn7ac51a12016-11-10 17:08:51 -08001346 /* Check the maximum_speed parameter */
1347 switch (dwc->maximum_speed) {
1348 case USB_SPEED_LOW:
1349 case USB_SPEED_FULL:
1350 case USB_SPEED_HIGH:
1351 case USB_SPEED_SUPER:
1352 case USB_SPEED_SUPER_PLUS:
1353 break;
1354 default:
1355 dev_err(dev, "invalid maximum_speed parameter %d\n",
1356 dwc->maximum_speed);
1357 /* fall through */
1358 case USB_SPEED_UNKNOWN:
1359 /* default to superspeed */
1360 dwc->maximum_speed = USB_SPEED_SUPER;
1361
1362 /*
1363 * default to superspeed plus if we are capable.
1364 */
1365 if (dwc3_is_usb31(dwc) &&
1366 (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
1367 DWC3_GHWPARAMS3_SSPHY_IFC_GEN2))
1368 dwc->maximum_speed = USB_SPEED_SUPER_PLUS;
1369
1370 break;
1371 }
1372}
1373
Felipe Balbic5ac6112016-10-14 16:30:52 +03001374static int dwc3_probe(struct platform_device *pdev)
1375{
1376 struct device *dev = &pdev->dev;
Masahiro Yamada44feb8e2018-04-19 20:03:37 +09001377 struct resource *res, dwc_res;
Felipe Balbic5ac6112016-10-14 16:30:52 +03001378 struct dwc3 *dwc;
1379
1380 int ret;
1381
1382 void __iomem *regs;
1383
1384 dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
1385 if (!dwc)
1386 return -ENOMEM;
1387
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001388 dwc->clks = devm_kmemdup(dev, dwc3_core_clks, sizeof(dwc3_core_clks),
1389 GFP_KERNEL);
1390 if (!dwc->clks)
1391 return -ENOMEM;
1392
Felipe Balbic5ac6112016-10-14 16:30:52 +03001393 dwc->dev = dev;
1394
1395 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1396 if (!res) {
1397 dev_err(dev, "missing memory resource\n");
1398 return -ENODEV;
1399 }
1400
1401 dwc->xhci_resources[0].start = res->start;
1402 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
1403 DWC3_XHCI_REGS_END;
1404 dwc->xhci_resources[0].flags = res->flags;
1405 dwc->xhci_resources[0].name = res->name;
1406
Felipe Balbic5ac6112016-10-14 16:30:52 +03001407 /*
1408 * Request memory region but exclude xHCI regs,
1409 * since it will be requested by the xhci-plat driver.
1410 */
Masahiro Yamada44feb8e2018-04-19 20:03:37 +09001411 dwc_res = *res;
1412 dwc_res.start += DWC3_GLOBALS_REGS_START;
1413
1414 regs = devm_ioremap_resource(dev, &dwc_res);
1415 if (IS_ERR(regs))
1416 return PTR_ERR(regs);
Felipe Balbic5ac6112016-10-14 16:30:52 +03001417
1418 dwc->regs = regs;
Masahiro Yamada44feb8e2018-04-19 20:03:37 +09001419 dwc->regs_size = resource_size(&dwc_res);
Felipe Balbic5ac6112016-10-14 16:30:52 +03001420
1421 dwc3_get_properties(dwc);
1422
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001423 dwc->reset = devm_reset_control_get_optional_shared(dev, NULL);
1424 if (IS_ERR(dwc->reset))
1425 return PTR_ERR(dwc->reset);
1426
Hans de Goede61527772018-06-12 10:24:48 +02001427 if (dev->of_node) {
1428 dwc->num_clks = ARRAY_SIZE(dwc3_core_clks);
1429
1430 ret = clk_bulk_get(dev, dwc->num_clks, dwc->clks);
1431 if (ret == -EPROBE_DEFER)
1432 return ret;
1433 /*
1434 * Clocks are optional, but new DT platforms should support all
1435 * clocks as required by the DT-binding.
1436 */
1437 if (ret)
1438 dwc->num_clks = 0;
1439 }
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001440
1441 ret = reset_control_deassert(dwc->reset);
1442 if (ret)
1443 goto put_clks;
1444
1445 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1446 if (ret)
1447 goto assert_reset;
1448
1449 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1450 if (ret)
1451 goto unprepare_clks;
1452
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001453 platform_set_drvdata(pdev, dwc);
Heikki Krogerus2917e712015-05-13 15:26:46 +03001454 dwc3_cache_hwparams(dwc);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +03001455
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001457
Felipe Balbifc8bb912016-05-16 13:14:48 +03001458 pm_runtime_set_active(dev);
1459 pm_runtime_use_autosuspend(dev);
1460 pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY);
Chanho Park802ca852012-02-15 18:27:55 +09001461 pm_runtime_enable(dev);
Roger Quadros32808232016-06-10 14:38:02 +03001462 ret = pm_runtime_get_sync(dev);
1463 if (ret < 0)
1464 goto err1;
1465
Chanho Park802ca852012-02-15 18:27:55 +09001466 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001467
Felipe Balbi39214262012-10-11 13:54:36 +03001468 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1469 if (ret) {
1470 dev_err(dwc->dev, "failed to allocate event buffers\n");
1471 ret = -ENOMEM;
Roger Quadros32808232016-06-10 14:38:02 +03001472 goto err2;
Felipe Balbi39214262012-10-11 13:54:36 +03001473 }
1474
Thinh Nguyen9d6173e2016-09-06 19:22:03 -07001475 ret = dwc3_get_dr_mode(dwc);
1476 if (ret)
1477 goto err3;
Felipe Balbi32a4a132014-02-25 14:00:13 -06001478
Felipe Balbic499ff72016-05-16 10:49:01 +03001479 ret = dwc3_alloc_scratch_buffers(dwc);
1480 if (ret)
Roger Quadros32808232016-06-10 14:38:02 +03001481 goto err3;
Felipe Balbic499ff72016-05-16 10:49:01 +03001482
Felipe Balbi72246da2011-08-19 18:10:58 +03001483 ret = dwc3_core_init(dwc);
1484 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +09001485 dev_err(dev, "failed to initialize core\n");
Roger Quadros32808232016-06-10 14:38:02 +03001486 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 }
1488
John Youn7ac51a12016-11-10 17:08:51 -08001489 dwc3_check_params(dwc);
John Youn2c7f1bd2016-02-05 17:08:59 -08001490
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001491 ret = dwc3_core_init_mode(dwc);
1492 if (ret)
Roger Quadros32808232016-06-10 14:38:02 +03001493 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03001494
Du, Changbin4e9f3112016-04-12 19:10:18 +08001495 dwc3_debugfs_init(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001496 pm_runtime_put(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001497
1498 return 0;
1499
Roger Quadros32808232016-06-10 14:38:02 +03001500err5:
Felipe Balbif122d332013-02-08 15:15:11 +02001501 dwc3_event_buffers_cleanup(dwc);
1502
Roger Quadros32808232016-06-10 14:38:02 +03001503err4:
Felipe Balbic499ff72016-05-16 10:49:01 +03001504 dwc3_free_scratch_buffers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001505
Roger Quadros32808232016-06-10 14:38:02 +03001506err3:
Felipe Balbi39214262012-10-11 13:54:36 +03001507 dwc3_free_event_buffers(dwc);
1508
Roger Quadros32808232016-06-10 14:38:02 +03001509err2:
1510 pm_runtime_allow(&pdev->dev);
1511
1512err1:
1513 pm_runtime_put_sync(&pdev->dev);
1514 pm_runtime_disable(&pdev->dev);
1515
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001516 clk_bulk_disable(dwc->num_clks, dwc->clks);
1517unprepare_clks:
1518 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1519assert_reset:
1520 reset_control_assert(dwc->reset);
1521put_clks:
1522 clk_bulk_put(dwc->num_clks, dwc->clks);
1523
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 return ret;
1525}
1526
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001527static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001528{
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001530
Felipe Balbifc8bb912016-05-16 13:14:48 +03001531 pm_runtime_get_sync(&pdev->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001532
Felipe Balbidc99f162014-09-03 16:13:37 -05001533 dwc3_debugfs_exit(dwc);
1534 dwc3_core_exit_mode(dwc);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301535
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001537 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001538
Felipe Balbifc8bb912016-05-16 13:14:48 +03001539 pm_runtime_put_sync(&pdev->dev);
1540 pm_runtime_allow(&pdev->dev);
1541 pm_runtime_disable(&pdev->dev);
1542
Felipe Balbic499ff72016-05-16 10:49:01 +03001543 dwc3_free_event_buffers(dwc);
1544 dwc3_free_scratch_buffers(dwc);
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001545 clk_bulk_put(dwc->num_clks, dwc->clks);
Felipe Balbic499ff72016-05-16 10:49:01 +03001546
Felipe Balbi72246da2011-08-19 18:10:58 +03001547 return 0;
1548}
1549
Felipe Balbifc8bb912016-05-16 13:14:48 +03001550#ifdef CONFIG_PM
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001551static int dwc3_core_init_for_resume(struct dwc3 *dwc)
1552{
1553 int ret;
1554
1555 ret = reset_control_deassert(dwc->reset);
1556 if (ret)
1557 return ret;
1558
1559 ret = clk_bulk_prepare(dwc->num_clks, dwc->clks);
1560 if (ret)
1561 goto assert_reset;
1562
1563 ret = clk_bulk_enable(dwc->num_clks, dwc->clks);
1564 if (ret)
1565 goto unprepare_clks;
1566
1567 ret = dwc3_core_init(dwc);
1568 if (ret)
1569 goto disable_clks;
1570
1571 return 0;
1572
1573disable_clks:
1574 clk_bulk_disable(dwc->num_clks, dwc->clks);
1575unprepare_clks:
1576 clk_bulk_unprepare(dwc->num_clks, dwc->clks);
1577assert_reset:
1578 reset_control_assert(dwc->reset);
1579
1580 return ret;
1581}
1582
Manu Gautamc4a51532018-01-18 16:54:30 +05301583static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
Felipe Balbi7415f172012-04-30 14:56:33 +03001584{
Felipe Balbifc8bb912016-05-16 13:14:48 +03001585 unsigned long flags;
Manu Gautambcb12872018-05-09 23:09:21 +05301586 u32 reg;
Felipe Balbi7415f172012-04-30 14:56:33 +03001587
Manu Gautam689bf722017-09-27 16:49:20 +05301588 switch (dwc->current_dr_role) {
1589 case DWC3_GCTL_PRTCAP_DEVICE:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001590 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7415f172012-04-30 14:56:33 +03001591 dwc3_gadget_suspend(dwc);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001592 spin_unlock_irqrestore(&dwc->lock, flags);
Manu Gautam689bf722017-09-27 16:49:20 +05301593 dwc3_core_exit(dwc);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001594 break;
Manu Gautam689bf722017-09-27 16:49:20 +05301595 case DWC3_GCTL_PRTCAP_HOST:
Manu Gautambcb12872018-05-09 23:09:21 +05301596 if (!PMSG_IS_AUTO(msg)) {
Manu Gautamc4a51532018-01-18 16:54:30 +05301597 dwc3_core_exit(dwc);
Manu Gautambcb12872018-05-09 23:09:21 +05301598 break;
1599 }
1600
1601 /* Let controller to suspend HSPHY before PHY driver suspends */
1602 if (dwc->dis_u2_susphy_quirk ||
1603 dwc->dis_enblslpm_quirk) {
1604 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1605 reg |= DWC3_GUSB2PHYCFG_ENBLSLPM |
1606 DWC3_GUSB2PHYCFG_SUSPHY;
1607 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1608
1609 /* Give some time for USB2 PHY to suspend */
1610 usleep_range(5000, 6000);
1611 }
1612
1613 phy_pm_runtime_put_sync(dwc->usb2_generic_phy);
1614 phy_pm_runtime_put_sync(dwc->usb3_generic_phy);
Manu Gautamc4a51532018-01-18 16:54:30 +05301615 break;
Roger Quadrosf09cc792018-02-27 13:30:19 +02001616 case DWC3_GCTL_PRTCAP_OTG:
1617 /* do nothing during runtime_suspend */
1618 if (PMSG_IS_AUTO(msg))
1619 break;
1620
1621 if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1622 spin_lock_irqsave(&dwc->lock, flags);
1623 dwc3_gadget_suspend(dwc);
1624 spin_unlock_irqrestore(&dwc->lock, flags);
1625 }
1626
1627 dwc3_otg_exit(dwc);
1628 dwc3_core_exit(dwc);
1629 break;
Felipe Balbi7415f172012-04-30 14:56:33 +03001630 default:
Felipe Balbi51f5d492016-05-16 10:52:58 +03001631 /* do nothing */
Felipe Balbi7415f172012-04-30 14:56:33 +03001632 break;
1633 }
1634
Felipe Balbifc8bb912016-05-16 13:14:48 +03001635 return 0;
1636}
1637
Manu Gautamc4a51532018-01-18 16:54:30 +05301638static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001639{
1640 unsigned long flags;
1641 int ret;
Manu Gautambcb12872018-05-09 23:09:21 +05301642 u32 reg;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001643
Manu Gautam689bf722017-09-27 16:49:20 +05301644 switch (dwc->current_dr_role) {
1645 case DWC3_GCTL_PRTCAP_DEVICE:
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001646 ret = dwc3_core_init_for_resume(dwc);
Manu Gautam689bf722017-09-27 16:49:20 +05301647 if (ret)
1648 return ret;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001649
Roger Quadros7d11c3a2018-03-16 16:44:27 +02001650 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001651 spin_lock_irqsave(&dwc->lock, flags);
1652 dwc3_gadget_resume(dwc);
1653 spin_unlock_irqrestore(&dwc->lock, flags);
Manu Gautam689bf722017-09-27 16:49:20 +05301654 break;
1655 case DWC3_GCTL_PRTCAP_HOST:
Manu Gautamc4a51532018-01-18 16:54:30 +05301656 if (!PMSG_IS_AUTO(msg)) {
Masahiro Yamadafe8abf32018-05-16 11:41:07 +09001657 ret = dwc3_core_init_for_resume(dwc);
Manu Gautamc4a51532018-01-18 16:54:30 +05301658 if (ret)
1659 return ret;
Roger Quadros7d11c3a2018-03-16 16:44:27 +02001660 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
Manu Gautambcb12872018-05-09 23:09:21 +05301661 break;
Manu Gautamc4a51532018-01-18 16:54:30 +05301662 }
Manu Gautambcb12872018-05-09 23:09:21 +05301663 /* Restore GUSB2PHYCFG bits that were modified in suspend */
1664 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1665 if (dwc->dis_u2_susphy_quirk)
1666 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1667
1668 if (dwc->dis_enblslpm_quirk)
1669 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
1670
1671 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1672
1673 phy_pm_runtime_get_sync(dwc->usb2_generic_phy);
1674 phy_pm_runtime_get_sync(dwc->usb3_generic_phy);
Manu Gautamc4a51532018-01-18 16:54:30 +05301675 break;
Roger Quadrosf09cc792018-02-27 13:30:19 +02001676 case DWC3_GCTL_PRTCAP_OTG:
1677 /* nothing to do on runtime_resume */
1678 if (PMSG_IS_AUTO(msg))
1679 break;
1680
1681 ret = dwc3_core_init(dwc);
1682 if (ret)
1683 return ret;
1684
1685 dwc3_set_prtcap(dwc, dwc->current_dr_role);
1686
1687 dwc3_otg_init(dwc);
1688 if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
1689 dwc3_otg_host_init(dwc);
1690 } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
1691 spin_lock_irqsave(&dwc->lock, flags);
1692 dwc3_gadget_resume(dwc);
1693 spin_unlock_irqrestore(&dwc->lock, flags);
1694 }
1695
1696 break;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001697 default:
1698 /* do nothing */
1699 break;
1700 }
1701
1702 return 0;
1703}
1704
1705static int dwc3_runtime_checks(struct dwc3 *dwc)
1706{
Manu Gautam689bf722017-09-27 16:49:20 +05301707 switch (dwc->current_dr_role) {
Manu Gautamc4a51532018-01-18 16:54:30 +05301708 case DWC3_GCTL_PRTCAP_DEVICE:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001709 if (dwc->connected)
1710 return -EBUSY;
1711 break;
Manu Gautamc4a51532018-01-18 16:54:30 +05301712 case DWC3_GCTL_PRTCAP_HOST:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001713 default:
1714 /* do nothing */
1715 break;
1716 }
1717
1718 return 0;
1719}
1720
1721static int dwc3_runtime_suspend(struct device *dev)
1722{
1723 struct dwc3 *dwc = dev_get_drvdata(dev);
1724 int ret;
1725
1726 if (dwc3_runtime_checks(dwc))
1727 return -EBUSY;
1728
Manu Gautamc4a51532018-01-18 16:54:30 +05301729 ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001730 if (ret)
1731 return ret;
1732
1733 device_init_wakeup(dev, true);
1734
1735 return 0;
1736}
1737
1738static int dwc3_runtime_resume(struct device *dev)
1739{
1740 struct dwc3 *dwc = dev_get_drvdata(dev);
1741 int ret;
1742
1743 device_init_wakeup(dev, false);
1744
Manu Gautamc4a51532018-01-18 16:54:30 +05301745 ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001746 if (ret)
1747 return ret;
1748
Manu Gautam689bf722017-09-27 16:49:20 +05301749 switch (dwc->current_dr_role) {
1750 case DWC3_GCTL_PRTCAP_DEVICE:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001751 dwc3_gadget_process_pending_events(dwc);
1752 break;
Manu Gautam689bf722017-09-27 16:49:20 +05301753 case DWC3_GCTL_PRTCAP_HOST:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001754 default:
1755 /* do nothing */
1756 break;
1757 }
1758
1759 pm_runtime_mark_last_busy(dev);
1760
1761 return 0;
1762}
1763
1764static int dwc3_runtime_idle(struct device *dev)
1765{
1766 struct dwc3 *dwc = dev_get_drvdata(dev);
1767
Manu Gautam689bf722017-09-27 16:49:20 +05301768 switch (dwc->current_dr_role) {
1769 case DWC3_GCTL_PRTCAP_DEVICE:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001770 if (dwc3_runtime_checks(dwc))
1771 return -EBUSY;
1772 break;
Manu Gautam689bf722017-09-27 16:49:20 +05301773 case DWC3_GCTL_PRTCAP_HOST:
Felipe Balbifc8bb912016-05-16 13:14:48 +03001774 default:
1775 /* do nothing */
1776 break;
1777 }
1778
1779 pm_runtime_mark_last_busy(dev);
1780 pm_runtime_autosuspend(dev);
1781
1782 return 0;
1783}
1784#endif /* CONFIG_PM */
1785
1786#ifdef CONFIG_PM_SLEEP
1787static int dwc3_suspend(struct device *dev)
1788{
1789 struct dwc3 *dwc = dev_get_drvdata(dev);
1790 int ret;
1791
Manu Gautamc4a51532018-01-18 16:54:30 +05301792 ret = dwc3_suspend_common(dwc, PMSG_SUSPEND);
Felipe Balbifc8bb912016-05-16 13:14:48 +03001793 if (ret)
1794 return ret;
1795
Sekhar Nori63444752015-08-31 21:09:08 +05301796 pinctrl_pm_select_sleep_state(dev);
1797
Felipe Balbi7415f172012-04-30 14:56:33 +03001798 return 0;
1799}
1800
1801static int dwc3_resume(struct device *dev)
1802{
1803 struct dwc3 *dwc = dev_get_drvdata(dev);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301804 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001805
Sekhar Nori63444752015-08-31 21:09:08 +05301806 pinctrl_pm_select_default_state(dev);
1807
Manu Gautamc4a51532018-01-18 16:54:30 +05301808 ret = dwc3_resume_common(dwc, PMSG_RESUME);
Felipe Balbi51f5d492016-05-16 10:52:58 +03001809 if (ret)
Felipe Balbi5c4ad3182016-04-11 17:12:34 +03001810 return ret;
1811
Felipe Balbi7415f172012-04-30 14:56:33 +03001812 pm_runtime_disable(dev);
1813 pm_runtime_set_active(dev);
1814 pm_runtime_enable(dev);
1815
1816 return 0;
1817}
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001818#endif /* CONFIG_PM_SLEEP */
Felipe Balbi7415f172012-04-30 14:56:33 +03001819
1820static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001821 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
Felipe Balbifc8bb912016-05-16 13:14:48 +03001822 SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume,
1823 dwc3_runtime_idle)
Felipe Balbi7415f172012-04-30 14:56:33 +03001824};
1825
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301826#ifdef CONFIG_OF
1827static const struct of_device_id of_dwc3_match[] = {
1828 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001829 .compatible = "snps,dwc3"
1830 },
1831 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301832 .compatible = "synopsys,dwc3"
1833 },
1834 { },
1835};
1836MODULE_DEVICE_TABLE(of, of_dwc3_match);
1837#endif
1838
Heikki Krogerus404905a2014-09-25 10:57:02 +03001839#ifdef CONFIG_ACPI
1840
1841#define ACPI_ID_INTEL_BSW "808622B7"
1842
1843static const struct acpi_device_id dwc3_acpi_match[] = {
1844 { ACPI_ID_INTEL_BSW, 0 },
1845 { },
1846};
1847MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1848#endif
1849
Felipe Balbi72246da2011-08-19 18:10:58 +03001850static struct platform_driver dwc3_driver = {
1851 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001852 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 .driver = {
1854 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301855 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001856 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7f370ed2016-05-09 15:27:01 +03001857 .pm = &dwc3_dev_pm_ops,
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001859};
1860
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001861module_platform_driver(dwc3_driver);
1862
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001863MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001864MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001865MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001866MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");