blob: bcb3a9c360d47902aed93ea5ad626cbb4155b76b [file] [log] [blame]
Alexander Shishkine443b332012-05-11 17:25:46 +03001/*
2 * core.c - ChipIdea USB IP core family device controller
3 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
14 * Description: ChipIdea USB IP core family device controller
15 *
16 * This driver is composed of several blocks:
17 * - HW: hardware interface
18 * - DBG: debug facilities (optional)
19 * - UTIL: utilities
20 * - ISR: interrupts handling
21 * - ENDPT: endpoint operations (Gadget API)
22 * - GADGET: gadget operations (Gadget API)
23 * - BUS: bus glue code, bus abstraction layer
24 *
25 * Compile Options
Alexander Shishkine443b332012-05-11 17:25:46 +030026 * - STALL_IN: non-empty bulk-in pipes cannot be halted
27 * if defined mass storage compliance succeeds but with warnings
28 * => case 4: Hi > Dn
29 * => case 5: Hi > Di
30 * => case 8: Hi <> Do
31 * if undefined usbtest 13 fails
32 * - TRACE: enable function tracing (depends on DEBUG)
33 *
34 * Main Features
35 * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
36 * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
37 * - Normal & LPM support
38 *
39 * USBTEST Report
40 * - OK: 0-12, 13 (STALL_IN defined) & 14
41 * - Not Supported: 15 & 16 (ISO)
42 *
43 * TODO List
Alexander Shishkine443b332012-05-11 17:25:46 +030044 * - Suspend & Remote Wakeup
45 */
46#include <linux/delay.h>
47#include <linux/device.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030048#include <linux/dma-mapping.h>
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +030049#include <linux/extcon.h>
Antoine Tenart1e5e2d32014-10-30 18:41:19 +010050#include <linux/phy/phy.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030051#include <linux/platform_device.h>
52#include <linux/module.h>
Richard Zhaofe6e1252012-07-07 22:56:42 +080053#include <linux/idr.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030054#include <linux/interrupt.h>
55#include <linux/io.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030056#include <linux/kernel.h>
57#include <linux/slab.h>
58#include <linux/pm_runtime.h>
59#include <linux/usb/ch9.h>
60#include <linux/usb/gadget.h>
61#include <linux/usb/otg.h>
62#include <linux/usb/chipidea.h>
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +030063#include <linux/usb/of.h>
Michael Grzeschik4f6743d2014-02-19 13:41:43 +080064#include <linux/of.h>
Peter Chen1542d9c2013-08-14 12:44:03 +030065#include <linux/regulator/consumer.h>
Peter Chen8022d3d2014-10-30 09:15:15 +080066#include <linux/usb/ehci_def.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030067
68#include "ci.h"
69#include "udc.h"
70#include "bits.h"
Alexander Shishkineb70e5a2012-05-11 17:25:54 +030071#include "host.h"
Peter Chenc10b4f02013-08-14 12:44:06 +030072#include "otg.h"
Li Jun4dcf7202014-04-23 15:56:50 +080073#include "otg_fsm.h"
Alexander Shishkine443b332012-05-11 17:25:46 +030074
Alexander Shishkin5f36e232012-05-11 17:25:47 +030075/* Controller register map */
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +080076static const u8 ci_regs_nolpm[] = {
77 [CAP_CAPLENGTH] = 0x00U,
78 [CAP_HCCPARAMS] = 0x08U,
79 [CAP_DCCPARAMS] = 0x24U,
80 [CAP_TESTMODE] = 0x38U,
81 [OP_USBCMD] = 0x00U,
82 [OP_USBSTS] = 0x04U,
83 [OP_USBINTR] = 0x08U,
84 [OP_DEVICEADDR] = 0x14U,
85 [OP_ENDPTLISTADDR] = 0x18U,
Peter Chen28362672015-06-18 11:51:53 +080086 [OP_TTCTRL] = 0x1CU,
Peter Chen96625ea2015-03-17 17:32:45 +080087 [OP_BURSTSIZE] = 0x20U,
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -080088 [OP_ULPI_VIEWPORT] = 0x30U,
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +080089 [OP_PORTSC] = 0x44U,
90 [OP_DEVLC] = 0x84U,
91 [OP_OTGSC] = 0x64U,
92 [OP_USBMODE] = 0x68U,
93 [OP_ENDPTSETUPSTAT] = 0x6CU,
94 [OP_ENDPTPRIME] = 0x70U,
95 [OP_ENDPTFLUSH] = 0x74U,
96 [OP_ENDPTSTAT] = 0x78U,
97 [OP_ENDPTCOMPLETE] = 0x7CU,
98 [OP_ENDPTCTRL] = 0x80U,
Alexander Shishkine443b332012-05-11 17:25:46 +030099};
100
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +0800101static const u8 ci_regs_lpm[] = {
102 [CAP_CAPLENGTH] = 0x00U,
103 [CAP_HCCPARAMS] = 0x08U,
104 [CAP_DCCPARAMS] = 0x24U,
105 [CAP_TESTMODE] = 0xFCU,
106 [OP_USBCMD] = 0x00U,
107 [OP_USBSTS] = 0x04U,
108 [OP_USBINTR] = 0x08U,
109 [OP_DEVICEADDR] = 0x14U,
110 [OP_ENDPTLISTADDR] = 0x18U,
Peter Chen28362672015-06-18 11:51:53 +0800111 [OP_TTCTRL] = 0x1CU,
Peter Chen96625ea2015-03-17 17:32:45 +0800112 [OP_BURSTSIZE] = 0x20U,
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800113 [OP_ULPI_VIEWPORT] = 0x30U,
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +0800114 [OP_PORTSC] = 0x44U,
115 [OP_DEVLC] = 0x84U,
116 [OP_OTGSC] = 0xC4U,
117 [OP_USBMODE] = 0xC8U,
118 [OP_ENDPTSETUPSTAT] = 0xD8U,
119 [OP_ENDPTPRIME] = 0xDCU,
120 [OP_ENDPTFLUSH] = 0xE0U,
121 [OP_ENDPTSTAT] = 0xE4U,
122 [OP_ENDPTCOMPLETE] = 0xE8U,
123 [OP_ENDPTCTRL] = 0xECU,
Alexander Shishkine443b332012-05-11 17:25:46 +0300124};
125
Nicholas Krause158ec072015-06-27 00:34:48 -0400126static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
Alexander Shishkine443b332012-05-11 17:25:46 +0300127{
128 int i;
129
Alexander Shishkine443b332012-05-11 17:25:46 +0300130 for (i = 0; i < OP_ENDPTCTRL; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300131 ci->hw_bank.regmap[i] =
132 (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
Alexander Shishkine443b332012-05-11 17:25:46 +0300133 (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
134
135 for (; i <= OP_LAST; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300136 ci->hw_bank.regmap[i] = ci->hw_bank.op +
Alexander Shishkine443b332012-05-11 17:25:46 +0300137 4 * (i - OP_ENDPTCTRL) +
138 (is_lpm
139 ? ci_regs_lpm[OP_ENDPTCTRL]
140 : ci_regs_nolpm[OP_ENDPTCTRL]);
141
Alexander Shishkine443b332012-05-11 17:25:46 +0300142}
143
Peter Chencb271f32015-02-11 12:44:55 +0800144static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
145{
146 int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
147 enum ci_revision rev = CI_REVISION_UNKNOWN;
148
149 if (ver == 0x2) {
150 rev = hw_read_id_reg(ci, ID_ID, REVISION)
151 >> __ffs(REVISION);
152 rev += CI_REVISION_20;
153 } else if (ver == 0x0) {
154 rev = CI_REVISION_1X;
155 }
156
157 return rev;
158}
159
Alexander Shishkine443b332012-05-11 17:25:46 +0300160/**
Li Jun36304b02014-04-23 15:56:39 +0800161 * hw_read_intr_enable: returns interrupt enable register
162 *
Peter Chen19353882014-09-22 08:14:17 +0800163 * @ci: the controller
164 *
Li Jun36304b02014-04-23 15:56:39 +0800165 * This function returns register data
166 */
167u32 hw_read_intr_enable(struct ci_hdrc *ci)
168{
169 return hw_read(ci, OP_USBINTR, ~0);
170}
171
172/**
173 * hw_read_intr_status: returns interrupt status register
174 *
Peter Chen19353882014-09-22 08:14:17 +0800175 * @ci: the controller
176 *
Li Jun36304b02014-04-23 15:56:39 +0800177 * This function returns register data
178 */
179u32 hw_read_intr_status(struct ci_hdrc *ci)
180{
181 return hw_read(ci, OP_USBSTS, ~0);
182}
183
184/**
Alexander Shishkine443b332012-05-11 17:25:46 +0300185 * hw_port_test_set: writes port test mode (execute without interruption)
186 * @mode: new value
187 *
188 * This function returns an error code
189 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300190int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
Alexander Shishkine443b332012-05-11 17:25:46 +0300191{
192 const u8 TEST_MODE_MAX = 7;
193
194 if (mode > TEST_MODE_MAX)
195 return -EINVAL;
196
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200197 hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
Alexander Shishkine443b332012-05-11 17:25:46 +0300198 return 0;
199}
200
201/**
202 * hw_port_test_get: reads port test mode value
203 *
Peter Chen19353882014-09-22 08:14:17 +0800204 * @ci: the controller
205 *
Alexander Shishkine443b332012-05-11 17:25:46 +0300206 * This function returns port test mode value
207 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300208u8 hw_port_test_get(struct ci_hdrc *ci)
Alexander Shishkine443b332012-05-11 17:25:46 +0300209{
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200210 return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
Alexander Shishkine443b332012-05-11 17:25:46 +0300211}
212
Peter Chenb82613c2014-11-26 13:44:28 +0800213static void hw_wait_phy_stable(void)
214{
215 /*
216 * The phy needs some delay to output the stable status from low
217 * power mode. And for OTGSC, the status inputs are debounced
218 * using a 1 ms time constant, so, delay 2ms for controller to get
219 * the stable status, like vbus and id when the phy leaves low power.
220 */
221 usleep_range(2000, 2500);
222}
223
Peter Chen864cf942013-09-24 12:47:55 +0800224/* The PHY enters/leaves low power mode */
225static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
226{
227 enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
228 bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
229
Peter Chen6d037db2014-11-26 13:44:27 +0800230 if (enable && !lpm)
Peter Chen864cf942013-09-24 12:47:55 +0800231 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
232 PORTSC_PHCD(ci->hw_bank.lpm));
Peter Chen6d037db2014-11-26 13:44:27 +0800233 else if (!enable && lpm)
Peter Chen864cf942013-09-24 12:47:55 +0800234 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
235 0);
Peter Chen864cf942013-09-24 12:47:55 +0800236}
237
Alexander Shishkin8e229782013-06-24 14:46:36 +0300238static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
Alexander Shishkine443b332012-05-11 17:25:46 +0300239{
240 u32 reg;
241
242 /* bank is a module variable */
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300243 ci->hw_bank.abs = base;
Alexander Shishkine443b332012-05-11 17:25:46 +0300244
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300245 ci->hw_bank.cap = ci->hw_bank.abs;
Richard Zhao77c44002012-06-29 17:48:53 +0800246 ci->hw_bank.cap += ci->platdata->capoffset;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200247 ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
Alexander Shishkine443b332012-05-11 17:25:46 +0300248
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300249 hw_alloc_regmap(ci, false);
250 reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200251 __ffs(HCCPARAMS_LEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300252 ci->hw_bank.lpm = reg;
Chris Ruehlaeb2c122013-12-06 16:35:12 +0800253 if (reg)
254 hw_alloc_regmap(ci, !!reg);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300255 ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
256 ci->hw_bank.size += OP_LAST;
257 ci->hw_bank.size /= sizeof(u32);
Alexander Shishkine443b332012-05-11 17:25:46 +0300258
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300259 reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200260 __ffs(DCCPARAMS_DEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300261 ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
Alexander Shishkine443b332012-05-11 17:25:46 +0300262
Richard Zhao09c94e62012-05-15 21:58:18 +0800263 if (ci->hw_ep_max > ENDPT_MAX)
Alexander Shishkine443b332012-05-11 17:25:46 +0300264 return -ENODEV;
265
Peter Chen864cf942013-09-24 12:47:55 +0800266 ci_hdrc_enter_lpm(ci, false);
267
Peter Chenc344b512013-08-14 12:44:09 +0300268 /* Disable all interrupts bits */
269 hw_write(ci, OP_USBINTR, 0xffffffff, 0);
270
271 /* Clear all interrupts status bits*/
272 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
273
Peter Chencb271f32015-02-11 12:44:55 +0800274 ci->rev = ci_get_revision(ci);
275
276 dev_dbg(ci->dev,
277 "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
278 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
Alexander Shishkine443b332012-05-11 17:25:46 +0300279
280 /* setup lock mode ? */
281
282 /* ENDPTSETUPSTAT is '0' by default */
283
284 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
285
286 return 0;
287}
288
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800289void hw_phymode_configure(struct ci_hdrc *ci)
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300290{
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800291 u32 portsc, lpm, sts = 0;
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300292
293 switch (ci->platdata->phy_mode) {
294 case USBPHY_INTERFACE_MODE_UTMI:
295 portsc = PORTSC_PTS(PTS_UTMI);
296 lpm = DEVLC_PTS(PTS_UTMI);
297 break;
298 case USBPHY_INTERFACE_MODE_UTMIW:
299 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
300 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
301 break;
302 case USBPHY_INTERFACE_MODE_ULPI:
303 portsc = PORTSC_PTS(PTS_ULPI);
304 lpm = DEVLC_PTS(PTS_ULPI);
305 break;
306 case USBPHY_INTERFACE_MODE_SERIAL:
307 portsc = PORTSC_PTS(PTS_SERIAL);
308 lpm = DEVLC_PTS(PTS_SERIAL);
309 sts = 1;
310 break;
311 case USBPHY_INTERFACE_MODE_HSIC:
312 portsc = PORTSC_PTS(PTS_HSIC);
313 lpm = DEVLC_PTS(PTS_HSIC);
314 break;
315 default:
316 return;
317 }
318
319 if (ci->hw_bank.lpm) {
320 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800321 if (sts)
322 hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300323 } else {
324 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800325 if (sts)
326 hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300327 }
328}
Stephen Boyd11893da2016-12-28 14:57:06 -0800329EXPORT_SYMBOL_GPL(hw_phymode_configure);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300330
Alexander Shishkine443b332012-05-11 17:25:46 +0300331/**
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100332 * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy
333 * interfaces
334 * @ci: the controller
335 *
336 * This function returns an error code if the phy failed to init
337 */
338static int _ci_usb_phy_init(struct ci_hdrc *ci)
339{
340 int ret;
341
342 if (ci->phy) {
343 ret = phy_init(ci->phy);
344 if (ret)
345 return ret;
346
347 ret = phy_power_on(ci->phy);
348 if (ret) {
349 phy_exit(ci->phy);
350 return ret;
351 }
352 } else {
353 ret = usb_phy_init(ci->usb_phy);
354 }
355
356 return ret;
357}
358
359/**
360 * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy
361 * interfaces
362 * @ci: the controller
363 */
364static void ci_usb_phy_exit(struct ci_hdrc *ci)
365{
Stephen Boyd8feb3682016-12-28 14:56:52 -0800366 if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
367 return;
368
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100369 if (ci->phy) {
370 phy_power_off(ci->phy);
371 phy_exit(ci->phy);
372 } else {
373 usb_phy_shutdown(ci->usb_phy);
374 }
375}
376
377/**
Peter Chend03cccf2014-04-23 15:56:37 +0800378 * ci_usb_phy_init: initialize phy according to different phy type
379 * @ci: the controller
Peter Chen19353882014-09-22 08:14:17 +0800380 *
Peter Chend03cccf2014-04-23 15:56:37 +0800381 * This function returns an error code if usb_phy_init has failed
382 */
383static int ci_usb_phy_init(struct ci_hdrc *ci)
384{
385 int ret;
386
Stephen Boyd8feb3682016-12-28 14:56:52 -0800387 if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
388 return 0;
389
Peter Chend03cccf2014-04-23 15:56:37 +0800390 switch (ci->platdata->phy_mode) {
391 case USBPHY_INTERFACE_MODE_UTMI:
392 case USBPHY_INTERFACE_MODE_UTMIW:
393 case USBPHY_INTERFACE_MODE_HSIC:
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100394 ret = _ci_usb_phy_init(ci);
Peter Chenb82613c2014-11-26 13:44:28 +0800395 if (!ret)
396 hw_wait_phy_stable();
397 else
Peter Chend03cccf2014-04-23 15:56:37 +0800398 return ret;
399 hw_phymode_configure(ci);
400 break;
401 case USBPHY_INTERFACE_MODE_ULPI:
402 case USBPHY_INTERFACE_MODE_SERIAL:
403 hw_phymode_configure(ci);
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100404 ret = _ci_usb_phy_init(ci);
Peter Chend03cccf2014-04-23 15:56:37 +0800405 if (ret)
406 return ret;
407 break;
408 default:
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100409 ret = _ci_usb_phy_init(ci);
Peter Chenb82613c2014-11-26 13:44:28 +0800410 if (!ret)
411 hw_wait_phy_stable();
Peter Chend03cccf2014-04-23 15:56:37 +0800412 }
413
414 return ret;
415}
416
Peter Chenbf9c85e2015-03-17 10:40:50 +0800417
418/**
419 * ci_platform_configure: do controller configure
420 * @ci: the controller
421 *
422 */
423void ci_platform_configure(struct ci_hdrc *ci)
424{
Peter Chen8022d3d2014-10-30 09:15:15 +0800425 bool is_device_mode, is_host_mode;
426
427 is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC;
428 is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC;
429
430 if (is_device_mode &&
431 (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING))
432 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
433
434 if (is_host_mode &&
435 (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING))
Peter Chenbf9c85e2015-03-17 10:40:50 +0800436 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
437
438 if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
439 if (ci->hw_bank.lpm)
440 hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
441 else
442 hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
443 }
444
445 if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA)
446 hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA);
Peter Chendf96ed82014-09-22 16:45:39 +0800447
448 hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16);
449
Peter Chen65668712015-03-17 14:21:00 +0800450 if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST)
451 hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK,
452 ci->platdata->ahb_burst_config);
Peter Chen96625ea2015-03-17 17:32:45 +0800453
454 /* override burst size, take effect only when ahb_burst_config is 0 */
455 if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) {
456 if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST)
457 hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK,
458 ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK));
459
460 if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST)
461 hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK,
462 ci->platdata->rx_burst_size);
463 }
Peter Chenbf9c85e2015-03-17 10:40:50 +0800464}
465
Peter Chend03cccf2014-04-23 15:56:37 +0800466/**
Peter Chencdd278f2014-11-26 13:44:32 +0800467 * hw_controller_reset: do controller reset
Alexander Shishkine443b332012-05-11 17:25:46 +0300468 * @ci: the controller
469 *
470 * This function returns an error code
471 */
Peter Chencdd278f2014-11-26 13:44:32 +0800472static int hw_controller_reset(struct ci_hdrc *ci)
473{
474 int count = 0;
475
476 hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
477 while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
478 udelay(10);
479 if (count++ > 1000)
480 return -ETIMEDOUT;
481 }
482
483 return 0;
484}
485
486/**
487 * hw_device_reset: resets chip (execute without interruption)
488 * @ci: the controller
489 *
490 * This function returns an error code
491 */
Peter Chen5b157302014-11-26 13:44:33 +0800492int hw_device_reset(struct ci_hdrc *ci)
Alexander Shishkine443b332012-05-11 17:25:46 +0300493{
Peter Chencdd278f2014-11-26 13:44:32 +0800494 int ret;
495
Alexander Shishkine443b332012-05-11 17:25:46 +0300496 /* should flush & stop before reset */
497 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
498 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
499
Peter Chencdd278f2014-11-26 13:44:32 +0800500 ret = hw_controller_reset(ci);
501 if (ret) {
502 dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
503 return ret;
504 }
Alexander Shishkine443b332012-05-11 17:25:46 +0300505
Stephen Boyd11893da2016-12-28 14:57:06 -0800506 if (ci->platdata->notify_event) {
507 ret = ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300508 CI_HDRC_CONTROLLER_RESET_EVENT);
Stephen Boyd11893da2016-12-28 14:57:06 -0800509 if (ret)
510 return ret;
511 }
Alexander Shishkine443b332012-05-11 17:25:46 +0300512
Alexander Shishkine443b332012-05-11 17:25:46 +0300513 /* USBMODE should be configured step by step */
514 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
Peter Chen5b157302014-11-26 13:44:33 +0800515 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
Alexander Shishkine443b332012-05-11 17:25:46 +0300516 /* HW >= 2.3 */
517 hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
518
Peter Chen5b157302014-11-26 13:44:33 +0800519 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
520 pr_err("cannot enter in %s device mode", ci_role(ci)->name);
Alexander Shishkine443b332012-05-11 17:25:46 +0300521 pr_err("lpm = %i", ci->hw_bank.lpm);
522 return -ENODEV;
523 }
524
Peter Chenbf9c85e2015-03-17 10:40:50 +0800525 ci_platform_configure(ci);
526
Alexander Shishkine443b332012-05-11 17:25:46 +0300527 return 0;
528}
529
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300530static irqreturn_t ci_irq(int irq, void *data)
531{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300532 struct ci_hdrc *ci = data;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300533 irqreturn_t ret = IRQ_NONE;
Richard Zhaob183c192012-09-12 14:58:11 +0300534 u32 otgsc = 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300535
Peter Chen1f874ed2015-02-11 12:44:45 +0800536 if (ci->in_lpm) {
537 disable_irq_nosync(irq);
538 ci->wakeup_int = true;
539 pm_runtime_get(ci->dev);
540 return IRQ_HANDLED;
541 }
542
Li Jun4dcf7202014-04-23 15:56:50 +0800543 if (ci->is_otg) {
Li Jun0c33bf72014-04-23 15:56:38 +0800544 otgsc = hw_read_otgsc(ci, ~0);
Li Jun4dcf7202014-04-23 15:56:50 +0800545 if (ci_otg_is_fsm_mode(ci)) {
546 ret = ci_otg_fsm_irq(ci);
547 if (ret == IRQ_HANDLED)
548 return ret;
549 }
550 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300551
Peter Chena107f8c2013-08-14 12:44:11 +0300552 /*
553 * Handle id change interrupt, it indicates device/host function
554 * switch.
555 */
556 if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
557 ci->id_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800558 /* Clear ID change irq status */
559 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
Peter Chenbe6b0c12014-05-23 08:12:49 +0800560 ci_otg_queue_work(ci);
Peter Chena107f8c2013-08-14 12:44:11 +0300561 return IRQ_HANDLED;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300562 }
563
Peter Chena107f8c2013-08-14 12:44:11 +0300564 /*
565 * Handle vbus change interrupt, it indicates device connection
566 * and disconnection events.
567 */
568 if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
569 ci->b_sess_valid_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800570 /* Clear BSV irq */
571 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
Peter Chenbe6b0c12014-05-23 08:12:49 +0800572 ci_otg_queue_work(ci);
Peter Chena107f8c2013-08-14 12:44:11 +0300573 return IRQ_HANDLED;
574 }
575
576 /* Handle device/host interrupt */
577 if (ci->role != CI_ROLE_END)
578 ret = ci_role(ci)->irq(ci);
579
Richard Zhaob183c192012-09-12 14:58:11 +0300580 return ret;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300581}
582
Stephen Boyd5cc49262017-01-20 15:11:55 +0800583static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
584 void *ptr)
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300585{
Stephen Boyd5cc49262017-01-20 15:11:55 +0800586 struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb);
587 struct ci_hdrc *ci = cbl->ci;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300588
Stephen Boyd5cc49262017-01-20 15:11:55 +0800589 cbl->connected = event;
590 cbl->changed = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300591
592 ci_irq(ci->irq, ci);
593 return NOTIFY_DONE;
594}
595
Peter Chen1542d9c2013-08-14 12:44:03 +0300596static int ci_get_platdata(struct device *dev,
597 struct ci_hdrc_platform_data *platdata)
598{
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300599 struct extcon_dev *ext_vbus, *ext_id;
600 struct ci_hdrc_cable *cable;
Li Jun79742352015-07-09 15:18:45 +0800601 int ret;
602
Peter Chenc22600c2013-09-17 12:37:22 +0800603 if (!platdata->phy_mode)
604 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
605
606 if (!platdata->dr_mode)
Heikki Krogerus06e71142015-09-21 11:14:34 +0300607 platdata->dr_mode = usb_get_dr_mode(dev);
Peter Chenc22600c2013-09-17 12:37:22 +0800608
609 if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
610 platdata->dr_mode = USB_DR_MODE_OTG;
611
Peter Chenc2ec3a72013-10-30 09:19:29 +0800612 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
613 /* Get the vbus regulator */
614 platdata->reg_vbus = devm_regulator_get(dev, "vbus");
615 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
616 return -EPROBE_DEFER;
617 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
Mickael Maison66294672014-11-26 13:44:38 +0800618 /* no vbus regulator is needed */
Peter Chenc2ec3a72013-10-30 09:19:29 +0800619 platdata->reg_vbus = NULL;
620 } else if (IS_ERR(platdata->reg_vbus)) {
621 dev_err(dev, "Getting regulator error: %ld\n",
622 PTR_ERR(platdata->reg_vbus));
623 return PTR_ERR(platdata->reg_vbus);
624 }
Peter Chenf6a9ff02014-08-19 09:51:56 +0800625 /* Get TPL support */
626 if (!platdata->tpl_support)
627 platdata->tpl_support =
628 of_usb_host_tpl_support(dev->of_node);
Peter Chenc2ec3a72013-10-30 09:19:29 +0800629 }
630
Li Jun79742352015-07-09 15:18:45 +0800631 if (platdata->dr_mode == USB_DR_MODE_OTG) {
632 /* We can support HNP and SRP of OTG 2.0 */
633 platdata->ci_otg_caps.otg_rev = 0x0200;
634 platdata->ci_otg_caps.hnp_support = true;
635 platdata->ci_otg_caps.srp_support = true;
636
637 /* Update otg capabilities by DT properties */
638 ret = of_usb_update_otg_caps(dev->of_node,
639 &platdata->ci_otg_caps);
640 if (ret)
641 return ret;
642 }
643
Heikki Krogerus63863b92015-09-21 11:14:32 +0300644 if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
Michael Grzeschik4f6743d2014-02-19 13:41:43 +0800645 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
646
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530647 of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
Fabio Estevam1fbf4622015-09-08 22:18:14 -0300648 &platdata->phy_clkgate_delay_us);
649
Peter Chendf96ed82014-09-22 16:45:39 +0800650 platdata->itc_setting = 1;
Peter Chendf96ed82014-09-22 16:45:39 +0800651
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530652 of_property_read_u32(dev->of_node, "itc-setting",
653 &platdata->itc_setting);
654
655 ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
656 &platdata->ahb_burst_config);
657 if (!ret) {
Peter Chen65668712015-03-17 14:21:00 +0800658 platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530659 } else if (ret != -EINVAL) {
660 dev_err(dev, "failed to get ahb-burst-config\n");
661 return ret;
Peter Chen65668712015-03-17 14:21:00 +0800662 }
663
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530664 ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
665 &platdata->tx_burst_size);
666 if (!ret) {
Peter Chen96625ea2015-03-17 17:32:45 +0800667 platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530668 } else if (ret != -EINVAL) {
669 dev_err(dev, "failed to get tx-burst-size-dword\n");
670 return ret;
Peter Chen96625ea2015-03-17 17:32:45 +0800671 }
672
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530673 ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
674 &platdata->rx_burst_size);
675 if (!ret) {
Peter Chen96625ea2015-03-17 17:32:45 +0800676 platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530677 } else if (ret != -EINVAL) {
678 dev_err(dev, "failed to get rx-burst-size-dword\n");
679 return ret;
Peter Chen96625ea2015-03-17 17:32:45 +0800680 }
681
Peter Chenaa738182016-02-01 14:23:44 +0800682 if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
683 platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
684
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300685 ext_id = ERR_PTR(-ENODEV);
686 ext_vbus = ERR_PTR(-ENODEV);
687 if (of_property_read_bool(dev->of_node, "extcon")) {
688 /* Each one of them is not mandatory */
689 ext_vbus = extcon_get_edev_by_phandle(dev, 0);
690 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
691 return PTR_ERR(ext_vbus);
692
693 ext_id = extcon_get_edev_by_phandle(dev, 1);
694 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
695 return PTR_ERR(ext_id);
696 }
697
698 cable = &platdata->vbus_extcon;
Stephen Boyd5cc49262017-01-20 15:11:55 +0800699 cable->nb.notifier_call = ci_cable_notifier;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300700 cable->edev = ext_vbus;
701
702 if (!IS_ERR(ext_vbus)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900703 ret = extcon_get_state(cable->edev, EXTCON_USB);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300704 if (ret)
Stephen Boyd5cc49262017-01-20 15:11:55 +0800705 cable->connected = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300706 else
Stephen Boyd5cc49262017-01-20 15:11:55 +0800707 cable->connected = false;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300708 }
709
710 cable = &platdata->id_extcon;
Stephen Boyd5cc49262017-01-20 15:11:55 +0800711 cable->nb.notifier_call = ci_cable_notifier;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300712 cable->edev = ext_id;
713
714 if (!IS_ERR(ext_id)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900715 ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300716 if (ret)
Stephen Boyd5cc49262017-01-20 15:11:55 +0800717 cable->connected = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300718 else
Stephen Boyd5cc49262017-01-20 15:11:55 +0800719 cable->connected = false;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300720 }
Peter Chen1542d9c2013-08-14 12:44:03 +0300721 return 0;
722}
723
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300724static int ci_extcon_register(struct ci_hdrc *ci)
725{
726 struct ci_hdrc_cable *id, *vbus;
727 int ret;
728
729 id = &ci->platdata->id_extcon;
730 id->ci = ci;
731 if (!IS_ERR(id->edev)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900732 ret = devm_extcon_register_notifier(ci->dev, id->edev,
733 EXTCON_USB_HOST, &id->nb);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300734 if (ret < 0) {
735 dev_err(ci->dev, "register ID failed\n");
736 return ret;
737 }
738 }
739
740 vbus = &ci->platdata->vbus_extcon;
741 vbus->ci = ci;
742 if (!IS_ERR(vbus->edev)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900743 ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
744 EXTCON_USB, &vbus->nb);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300745 if (ret < 0) {
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300746 dev_err(ci->dev, "register VBUS failed\n");
747 return ret;
748 }
749 }
750
751 return 0;
752}
753
Richard Zhaofe6e1252012-07-07 22:56:42 +0800754static DEFINE_IDA(ci_ida);
755
Alexander Shishkin8e229782013-06-24 14:46:36 +0300756struct platform_device *ci_hdrc_add_device(struct device *dev,
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800757 struct resource *res, int nres,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300758 struct ci_hdrc_platform_data *platdata)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800759{
760 struct platform_device *pdev;
Richard Zhaofe6e1252012-07-07 22:56:42 +0800761 int id, ret;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800762
Peter Chen1542d9c2013-08-14 12:44:03 +0300763 ret = ci_get_platdata(dev, platdata);
764 if (ret)
765 return ERR_PTR(ret);
766
Richard Zhaofe6e1252012-07-07 22:56:42 +0800767 id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
768 if (id < 0)
769 return ERR_PTR(id);
770
771 pdev = platform_device_alloc("ci_hdrc", id);
772 if (!pdev) {
773 ret = -ENOMEM;
774 goto put_id;
775 }
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800776
777 pdev->dev.parent = dev;
778 pdev->dev.dma_mask = dev->dma_mask;
779 pdev->dev.dma_parms = dev->dma_parms;
780 dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
781
782 ret = platform_device_add_resources(pdev, res, nres);
783 if (ret)
784 goto err;
785
786 ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
787 if (ret)
788 goto err;
789
790 ret = platform_device_add(pdev);
791 if (ret)
792 goto err;
793
794 return pdev;
795
796err:
797 platform_device_put(pdev);
Richard Zhaofe6e1252012-07-07 22:56:42 +0800798put_id:
799 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800800 return ERR_PTR(ret);
801}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300802EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800803
Alexander Shishkin8e229782013-06-24 14:46:36 +0300804void ci_hdrc_remove_device(struct platform_device *pdev)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800805{
Lothar Waßmann98c35532012-11-22 10:11:25 +0100806 int id = pdev->id;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800807 platform_device_unregister(pdev);
Lothar Waßmann98c35532012-11-22 10:11:25 +0100808 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800809}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300810EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800811
Peter Chen3f124d22013-08-14 12:44:07 +0300812static inline void ci_role_destroy(struct ci_hdrc *ci)
813{
814 ci_hdrc_gadget_destroy(ci);
815 ci_hdrc_host_destroy(ci);
Peter Chencbec6bd2013-08-14 12:44:10 +0300816 if (ci->is_otg)
817 ci_hdrc_otg_destroy(ci);
Peter Chen3f124d22013-08-14 12:44:07 +0300818}
819
Peter Chen577b2322013-08-14 12:44:08 +0300820static void ci_get_otg_capable(struct ci_hdrc *ci)
821{
822 if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
823 ci->is_otg = false;
824 else
825 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
826 DCCPARAMS_DC | DCCPARAMS_HC)
827 == (DCCPARAMS_DC | DCCPARAMS_HC));
Peter Chen2e37cfd2015-02-11 12:44:51 +0800828 if (ci->is_otg) {
Peter Chen577b2322013-08-14 12:44:08 +0300829 dev_dbg(ci->dev, "It is OTG capable controller\n");
Peter Chen2e37cfd2015-02-11 12:44:51 +0800830 /* Disable and clear all OTG irq */
831 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
832 OTGSC_INT_STATUS_BITS);
833 }
Peter Chen577b2322013-08-14 12:44:08 +0300834}
835
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500836static int ci_hdrc_probe(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +0300837{
838 struct device *dev = &pdev->dev;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300839 struct ci_hdrc *ci;
Alexander Shishkine443b332012-05-11 17:25:46 +0300840 struct resource *res;
841 void __iomem *base;
842 int ret;
Sascha Hauer691962d2013-06-13 17:59:57 +0300843 enum usb_dr_mode dr_mode;
Alexander Shishkine443b332012-05-11 17:25:46 +0300844
Jingoo Hanfad56742014-02-19 13:41:42 +0800845 if (!dev_get_platdata(dev)) {
Alexander Shishkine443b332012-05-11 17:25:46 +0300846 dev_err(dev, "platform data missing\n");
847 return -ENODEV;
848 }
849
850 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi19290812013-03-30 02:46:27 +0200851 base = devm_ioremap_resource(dev, res);
852 if (IS_ERR(base))
853 return PTR_ERR(base);
Alexander Shishkine443b332012-05-11 17:25:46 +0300854
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300855 ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
Fabio Estevamd0f99242014-11-26 13:44:23 +0800856 if (!ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300857 return -ENOMEM;
Alexander Shishkine443b332012-05-11 17:25:46 +0300858
Peter Chena5d906b2016-11-15 18:05:33 +0800859 spin_lock_init(&ci->lock);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300860 ci->dev = dev;
Jingoo Hanfad56742014-02-19 13:41:42 +0800861 ci->platdata = dev_get_platdata(dev);
Peter Chened8f8312014-01-10 13:51:27 +0800862 ci->imx28_write_fix = !!(ci->platdata->flags &
863 CI_HDRC_IMX28_WRITE_FIX);
Peter Chen1f874ed2015-02-11 12:44:45 +0800864 ci->supports_runtime_pm = !!(ci->platdata->flags &
865 CI_HDRC_SUPPORTS_RUNTIME_PM);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800866 platform_set_drvdata(pdev, ci);
Alexander Shishkine443b332012-05-11 17:25:46 +0300867
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300868 ret = hw_device_init(ci, base);
869 if (ret < 0) {
870 dev_err(dev, "can't initialize hardware\n");
871 return -ENODEV;
872 }
873
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800874 ret = ci_ulpi_init(ci);
875 if (ret)
876 return ret;
877
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100878 if (ci->platdata->phy) {
879 ci->phy = ci->platdata->phy;
880 } else if (ci->platdata->usb_phy) {
Antoine Tenartef44cb42014-10-30 18:41:16 +0100881 ci->usb_phy = ci->platdata->usb_phy;
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100882 } else {
Antoine Tenart21a5b572014-11-26 13:44:35 +0800883 ci->phy = devm_phy_get(dev->parent, "usb-phy");
884 ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2);
Peter Chenc859aa652014-02-19 13:41:40 +0800885
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100886 /* if both generic PHY and USB PHY layers aren't enabled */
887 if (PTR_ERR(ci->phy) == -ENOSYS &&
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800888 PTR_ERR(ci->usb_phy) == -ENXIO) {
889 ret = -ENXIO;
890 goto ulpi_exit;
891 }
Peter Chenc859aa652014-02-19 13:41:40 +0800892
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800893 if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) {
894 ret = -EPROBE_DEFER;
895 goto ulpi_exit;
896 }
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100897
898 if (IS_ERR(ci->phy))
899 ci->phy = NULL;
900 else if (IS_ERR(ci->usb_phy))
901 ci->usb_phy = NULL;
Peter Chenc859aa652014-02-19 13:41:40 +0800902 }
903
Peter Chend03cccf2014-04-23 15:56:37 +0800904 ret = ci_usb_phy_init(ci);
Peter Chen74475ed2013-09-24 12:47:53 +0800905 if (ret) {
906 dev_err(dev, "unable to init phy: %d\n", ret);
907 return ret;
908 }
909
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300910 ci->hw_bank.phys = res->start;
911
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300912 ci->irq = platform_get_irq(pdev, 0);
913 if (ci->irq < 0) {
914 dev_err(dev, "missing IRQ\n");
Fabio Estevam42d18212014-02-19 13:41:44 +0800915 ret = ci->irq;
Peter Chenc859aa652014-02-19 13:41:40 +0800916 goto deinit_phy;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300917 }
918
Peter Chen577b2322013-08-14 12:44:08 +0300919 ci_get_otg_capable(ci);
920
Sascha Hauer691962d2013-06-13 17:59:57 +0300921 dr_mode = ci->platdata->dr_mode;
922 /* initialize role(s) before the interrupt is requested */
923 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
924 ret = ci_hdrc_host_init(ci);
925 if (ret)
926 dev_info(dev, "doesn't support host\n");
927 }
928
929 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
930 ret = ci_hdrc_gadget_init(ci);
931 if (ret)
932 dev_info(dev, "doesn't support gadget\n");
933 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300934
935 if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
936 dev_err(dev, "no supported roles\n");
Peter Chen74475ed2013-09-24 12:47:53 +0800937 ret = -ENODEV;
Peter Chenc859aa652014-02-19 13:41:40 +0800938 goto deinit_phy;
Peter Chencbec6bd2013-08-14 12:44:10 +0300939 }
940
Peter Chen27c62c22014-09-22 08:14:16 +0800941 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
Peter Chencbec6bd2013-08-14 12:44:10 +0300942 ret = ci_hdrc_otg_init(ci);
943 if (ret) {
944 dev_err(dev, "init otg fails, ret = %d\n", ret);
945 goto stop;
946 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300947 }
948
949 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
Peter Chen577b2322013-08-14 12:44:08 +0300950 if (ci->is_otg) {
Peter Chen577b2322013-08-14 12:44:08 +0300951 ci->role = ci_otg_role(ci);
Li Jun0c33bf72014-04-23 15:56:38 +0800952 /* Enable ID change irq */
953 hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
Peter Chen577b2322013-08-14 12:44:08 +0300954 } else {
955 /*
956 * If the controller is not OTG capable, but support
957 * role switch, the defalt role is gadget, and the
958 * user can switch it through debugfs.
959 */
960 ci->role = CI_ROLE_GADGET;
961 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300962 } else {
963 ci->role = ci->roles[CI_ROLE_HOST]
964 ? CI_ROLE_HOST
965 : CI_ROLE_GADGET;
966 }
967
Li Jun4dcf7202014-04-23 15:56:50 +0800968 if (!ci_otg_is_fsm_mode(ci)) {
Li Jun961ea492015-02-11 12:45:03 +0800969 /* only update vbus status for peripheral */
970 if (ci->role == CI_ROLE_GADGET)
971 ci_handle_vbus_change(ci);
972
Li Jun4dcf7202014-04-23 15:56:50 +0800973 ret = ci_role_start(ci, ci->role);
974 if (ret) {
975 dev_err(dev, "can't start %s role\n",
976 ci_role(ci)->name);
977 goto stop;
978 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300979 }
980
Peter Chen4c503dd2014-11-26 13:44:22 +0800981 ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
982 ci->platdata->name, ci);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300983 if (ret)
984 goto stop;
985
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300986 ret = ci_extcon_register(ci);
987 if (ret)
988 goto stop;
989
Peter Chen1f874ed2015-02-11 12:44:45 +0800990 if (ci->supports_runtime_pm) {
991 pm_runtime_set_active(&pdev->dev);
992 pm_runtime_enable(&pdev->dev);
993 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
994 pm_runtime_mark_last_busy(ci->dev);
995 pm_runtime_use_autosuspend(&pdev->dev);
996 }
997
Li Jun4dcf7202014-04-23 15:56:50 +0800998 if (ci_otg_is_fsm_mode(ci))
999 ci_hdrc_otg_fsm_start(ci);
1000
Peter Chenf8efa762015-02-11 12:44:48 +08001001 device_set_wakeup_capable(&pdev->dev, true);
1002
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001003 ret = dbg_create_files(ci);
1004 if (!ret)
1005 return 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001006
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001007stop:
Peter Chen3f124d22013-08-14 12:44:07 +03001008 ci_role_destroy(ci);
Peter Chenc859aa652014-02-19 13:41:40 +08001009deinit_phy:
Antoine Tenart1e5e2d32014-10-30 18:41:19 +01001010 ci_usb_phy_exit(ci);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001011ulpi_exit:
1012 ci_ulpi_exit(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001013
1014 return ret;
1015}
1016
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001017static int ci_hdrc_remove(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +03001018{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001019 struct ci_hdrc *ci = platform_get_drvdata(pdev);
Alexander Shishkine443b332012-05-11 17:25:46 +03001020
Peter Chen1f874ed2015-02-11 12:44:45 +08001021 if (ci->supports_runtime_pm) {
1022 pm_runtime_get_sync(&pdev->dev);
1023 pm_runtime_disable(&pdev->dev);
1024 pm_runtime_put_noidle(&pdev->dev);
1025 }
1026
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001027 dbg_remove_files(ci);
Peter Chen3f124d22013-08-14 12:44:07 +03001028 ci_role_destroy(ci);
Peter Chen864cf942013-09-24 12:47:55 +08001029 ci_hdrc_enter_lpm(ci, true);
Antoine Tenart1e5e2d32014-10-30 18:41:19 +01001030 ci_usb_phy_exit(ci);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001031 ci_ulpi_exit(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001032
1033 return 0;
1034}
1035
Peter Chen1f874ed2015-02-11 12:44:45 +08001036#ifdef CONFIG_PM
Li Jun961ea492015-02-11 12:45:03 +08001037/* Prepare wakeup by SRP before suspend */
1038static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
1039{
1040 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1041 !hw_read_otgsc(ci, OTGSC_ID)) {
1042 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
1043 PORTSC_PP);
1044 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
1045 PORTSC_WKCN);
1046 }
1047}
1048
1049/* Handle SRP when wakeup by data pulse */
1050static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
1051{
1052 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1053 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
1054 if (!hw_read_otgsc(ci, OTGSC_ID)) {
1055 ci->fsm.a_srp_det = 1;
1056 ci->fsm.a_bus_drop = 0;
1057 } else {
1058 ci->fsm.id = 1;
1059 }
1060 ci_otg_queue_work(ci);
1061 }
1062}
1063
Peter Chen80769322014-11-26 13:44:29 +08001064static void ci_controller_suspend(struct ci_hdrc *ci)
1065{
Peter Chen1f874ed2015-02-11 12:44:45 +08001066 disable_irq(ci->irq);
Peter Chen80769322014-11-26 13:44:29 +08001067 ci_hdrc_enter_lpm(ci, true);
Fabio Estevam1fbf4622015-09-08 22:18:14 -03001068 if (ci->platdata->phy_clkgate_delay_us)
1069 usleep_range(ci->platdata->phy_clkgate_delay_us,
1070 ci->platdata->phy_clkgate_delay_us + 50);
Peter Chen1f874ed2015-02-11 12:44:45 +08001071 usb_phy_set_suspend(ci->usb_phy, 1);
1072 ci->in_lpm = true;
1073 enable_irq(ci->irq);
Peter Chen80769322014-11-26 13:44:29 +08001074}
1075
1076static int ci_controller_resume(struct device *dev)
1077{
1078 struct ci_hdrc *ci = dev_get_drvdata(dev);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001079 int ret;
Peter Chen80769322014-11-26 13:44:29 +08001080
1081 dev_dbg(dev, "at %s\n", __func__);
1082
Peter Chen1f874ed2015-02-11 12:44:45 +08001083 if (!ci->in_lpm) {
1084 WARN_ON(1);
1085 return 0;
1086 }
Peter Chen80769322014-11-26 13:44:29 +08001087
Peter Chen1f874ed2015-02-11 12:44:45 +08001088 ci_hdrc_enter_lpm(ci, false);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001089
1090 ret = ci_ulpi_resume(ci);
1091 if (ret)
1092 return ret;
1093
Peter Chen80769322014-11-26 13:44:29 +08001094 if (ci->usb_phy) {
1095 usb_phy_set_suspend(ci->usb_phy, 0);
1096 usb_phy_set_wakeup(ci->usb_phy, false);
1097 hw_wait_phy_stable();
1098 }
1099
Peter Chen1f874ed2015-02-11 12:44:45 +08001100 ci->in_lpm = false;
1101 if (ci->wakeup_int) {
1102 ci->wakeup_int = false;
1103 pm_runtime_mark_last_busy(ci->dev);
1104 pm_runtime_put_autosuspend(ci->dev);
1105 enable_irq(ci->irq);
Li Jun961ea492015-02-11 12:45:03 +08001106 if (ci_otg_is_fsm_mode(ci))
1107 ci_otg_fsm_wakeup_by_srp(ci);
Peter Chen1f874ed2015-02-11 12:44:45 +08001108 }
1109
Peter Chen80769322014-11-26 13:44:29 +08001110 return 0;
1111}
1112
Peter Chen1f874ed2015-02-11 12:44:45 +08001113#ifdef CONFIG_PM_SLEEP
Peter Chen80769322014-11-26 13:44:29 +08001114static int ci_suspend(struct device *dev)
1115{
1116 struct ci_hdrc *ci = dev_get_drvdata(dev);
1117
1118 if (ci->wq)
1119 flush_workqueue(ci->wq);
Peter Chen1f874ed2015-02-11 12:44:45 +08001120 /*
1121 * Controller needs to be active during suspend, otherwise the core
1122 * may run resume when the parent is at suspend if other driver's
1123 * suspend fails, it occurs before parent's suspend has not started,
1124 * but the core suspend has finished.
1125 */
1126 if (ci->in_lpm)
1127 pm_runtime_resume(dev);
1128
1129 if (ci->in_lpm) {
1130 WARN_ON(1);
1131 return 0;
1132 }
Peter Chen80769322014-11-26 13:44:29 +08001133
Peter Chenf8efa762015-02-11 12:44:48 +08001134 if (device_may_wakeup(dev)) {
Li Jun961ea492015-02-11 12:45:03 +08001135 if (ci_otg_is_fsm_mode(ci))
1136 ci_otg_fsm_suspend_for_srp(ci);
1137
Peter Chenf8efa762015-02-11 12:44:48 +08001138 usb_phy_set_wakeup(ci->usb_phy, true);
1139 enable_irq_wake(ci->irq);
1140 }
1141
Peter Chen80769322014-11-26 13:44:29 +08001142 ci_controller_suspend(ci);
1143
1144 return 0;
1145}
1146
1147static int ci_resume(struct device *dev)
1148{
Peter Chen1f874ed2015-02-11 12:44:45 +08001149 struct ci_hdrc *ci = dev_get_drvdata(dev);
1150 int ret;
1151
Peter Chenf8efa762015-02-11 12:44:48 +08001152 if (device_may_wakeup(dev))
1153 disable_irq_wake(ci->irq);
1154
Peter Chen1f874ed2015-02-11 12:44:45 +08001155 ret = ci_controller_resume(dev);
1156 if (ret)
1157 return ret;
1158
1159 if (ci->supports_runtime_pm) {
1160 pm_runtime_disable(dev);
1161 pm_runtime_set_active(dev);
1162 pm_runtime_enable(dev);
1163 }
1164
1165 return ret;
Peter Chen80769322014-11-26 13:44:29 +08001166}
1167#endif /* CONFIG_PM_SLEEP */
1168
Peter Chen1f874ed2015-02-11 12:44:45 +08001169static int ci_runtime_suspend(struct device *dev)
1170{
1171 struct ci_hdrc *ci = dev_get_drvdata(dev);
1172
1173 dev_dbg(dev, "at %s\n", __func__);
1174
1175 if (ci->in_lpm) {
1176 WARN_ON(1);
1177 return 0;
1178 }
1179
Li Jun961ea492015-02-11 12:45:03 +08001180 if (ci_otg_is_fsm_mode(ci))
1181 ci_otg_fsm_suspend_for_srp(ci);
1182
Peter Chen1f874ed2015-02-11 12:44:45 +08001183 usb_phy_set_wakeup(ci->usb_phy, true);
1184 ci_controller_suspend(ci);
1185
1186 return 0;
1187}
1188
1189static int ci_runtime_resume(struct device *dev)
1190{
1191 return ci_controller_resume(dev);
1192}
1193
1194#endif /* CONFIG_PM */
Peter Chen80769322014-11-26 13:44:29 +08001195static const struct dev_pm_ops ci_pm_ops = {
1196 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
Peter Chen1f874ed2015-02-11 12:44:45 +08001197 SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
Peter Chen80769322014-11-26 13:44:29 +08001198};
Peter Chen1f874ed2015-02-11 12:44:45 +08001199
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001200static struct platform_driver ci_hdrc_driver = {
1201 .probe = ci_hdrc_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001202 .remove = ci_hdrc_remove,
Alexander Shishkine443b332012-05-11 17:25:46 +03001203 .driver = {
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001204 .name = "ci_hdrc",
Peter Chen80769322014-11-26 13:44:29 +08001205 .pm = &ci_pm_ops,
Alexander Shishkine443b332012-05-11 17:25:46 +03001206 },
1207};
1208
Peter Chen2f01a332015-07-21 09:51:29 +08001209static int __init ci_hdrc_platform_register(void)
1210{
1211 ci_hdrc_host_driver_init();
1212 return platform_driver_register(&ci_hdrc_driver);
1213}
1214module_init(ci_hdrc_platform_register);
1215
1216static void __exit ci_hdrc_platform_unregister(void)
1217{
1218 platform_driver_unregister(&ci_hdrc_driver);
1219}
1220module_exit(ci_hdrc_platform_unregister);
Alexander Shishkine443b332012-05-11 17:25:46 +03001221
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001222MODULE_ALIAS("platform:ci_hdrc");
Alexander Shishkine443b332012-05-11 17:25:46 +03001223MODULE_LICENSE("GPL v2");
1224MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001225MODULE_DESCRIPTION("ChipIdea HDRC Driver");