blob: 367d02a021456a8a9bb7d4098520989343edd041 [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>
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +030065#include <linux/phy.h>
Peter Chen1542d9c2013-08-14 12:44:03 +030066#include <linux/regulator/consumer.h>
Peter Chen8022d3d2014-10-30 09:15:15 +080067#include <linux/usb/ehci_def.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030068
69#include "ci.h"
70#include "udc.h"
71#include "bits.h"
Alexander Shishkineb70e5a2012-05-11 17:25:54 +030072#include "host.h"
Peter Chenc10b4f02013-08-14 12:44:06 +030073#include "otg.h"
Li Jun4dcf7202014-04-23 15:56:50 +080074#include "otg_fsm.h"
Alexander Shishkine443b332012-05-11 17:25:46 +030075
Alexander Shishkin5f36e232012-05-11 17:25:47 +030076/* Controller register map */
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +080077static const u8 ci_regs_nolpm[] = {
78 [CAP_CAPLENGTH] = 0x00U,
79 [CAP_HCCPARAMS] = 0x08U,
80 [CAP_DCCPARAMS] = 0x24U,
81 [CAP_TESTMODE] = 0x38U,
82 [OP_USBCMD] = 0x00U,
83 [OP_USBSTS] = 0x04U,
84 [OP_USBINTR] = 0x08U,
85 [OP_DEVICEADDR] = 0x14U,
86 [OP_ENDPTLISTADDR] = 0x18U,
Peter Chen28362672015-06-18 11:51:53 +080087 [OP_TTCTRL] = 0x1CU,
Peter Chen96625ea2015-03-17 17:32:45 +080088 [OP_BURSTSIZE] = 0x20U,
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -080089 [OP_ULPI_VIEWPORT] = 0x30U,
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +080090 [OP_PORTSC] = 0x44U,
91 [OP_DEVLC] = 0x84U,
92 [OP_OTGSC] = 0x64U,
93 [OP_USBMODE] = 0x68U,
94 [OP_ENDPTSETUPSTAT] = 0x6CU,
95 [OP_ENDPTPRIME] = 0x70U,
96 [OP_ENDPTFLUSH] = 0x74U,
97 [OP_ENDPTSTAT] = 0x78U,
98 [OP_ENDPTCOMPLETE] = 0x7CU,
99 [OP_ENDPTCTRL] = 0x80U,
Alexander Shishkine443b332012-05-11 17:25:46 +0300100};
101
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +0800102static const u8 ci_regs_lpm[] = {
103 [CAP_CAPLENGTH] = 0x00U,
104 [CAP_HCCPARAMS] = 0x08U,
105 [CAP_DCCPARAMS] = 0x24U,
106 [CAP_TESTMODE] = 0xFCU,
107 [OP_USBCMD] = 0x00U,
108 [OP_USBSTS] = 0x04U,
109 [OP_USBINTR] = 0x08U,
110 [OP_DEVICEADDR] = 0x14U,
111 [OP_ENDPTLISTADDR] = 0x18U,
Peter Chen28362672015-06-18 11:51:53 +0800112 [OP_TTCTRL] = 0x1CU,
Peter Chen96625ea2015-03-17 17:32:45 +0800113 [OP_BURSTSIZE] = 0x20U,
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800114 [OP_ULPI_VIEWPORT] = 0x30U,
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +0800115 [OP_PORTSC] = 0x44U,
116 [OP_DEVLC] = 0x84U,
117 [OP_OTGSC] = 0xC4U,
118 [OP_USBMODE] = 0xC8U,
119 [OP_ENDPTSETUPSTAT] = 0xD8U,
120 [OP_ENDPTPRIME] = 0xDCU,
121 [OP_ENDPTFLUSH] = 0xE0U,
122 [OP_ENDPTSTAT] = 0xE4U,
123 [OP_ENDPTCOMPLETE] = 0xE8U,
124 [OP_ENDPTCTRL] = 0xECU,
Alexander Shishkine443b332012-05-11 17:25:46 +0300125};
126
Nicholas Krause158ec072015-06-27 00:34:48 -0400127static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
Alexander Shishkine443b332012-05-11 17:25:46 +0300128{
129 int i;
130
Alexander Shishkine443b332012-05-11 17:25:46 +0300131 for (i = 0; i < OP_ENDPTCTRL; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300132 ci->hw_bank.regmap[i] =
133 (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
Alexander Shishkine443b332012-05-11 17:25:46 +0300134 (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
135
136 for (; i <= OP_LAST; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300137 ci->hw_bank.regmap[i] = ci->hw_bank.op +
Alexander Shishkine443b332012-05-11 17:25:46 +0300138 4 * (i - OP_ENDPTCTRL) +
139 (is_lpm
140 ? ci_regs_lpm[OP_ENDPTCTRL]
141 : ci_regs_nolpm[OP_ENDPTCTRL]);
142
Alexander Shishkine443b332012-05-11 17:25:46 +0300143}
144
Peter Chencb271f32015-02-11 12:44:55 +0800145static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
146{
147 int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
148 enum ci_revision rev = CI_REVISION_UNKNOWN;
149
150 if (ver == 0x2) {
151 rev = hw_read_id_reg(ci, ID_ID, REVISION)
152 >> __ffs(REVISION);
153 rev += CI_REVISION_20;
154 } else if (ver == 0x0) {
155 rev = CI_REVISION_1X;
156 }
157
158 return rev;
159}
160
Alexander Shishkine443b332012-05-11 17:25:46 +0300161/**
Li Jun36304b02014-04-23 15:56:39 +0800162 * hw_read_intr_enable: returns interrupt enable register
163 *
Peter Chen19353882014-09-22 08:14:17 +0800164 * @ci: the controller
165 *
Li Jun36304b02014-04-23 15:56:39 +0800166 * This function returns register data
167 */
168u32 hw_read_intr_enable(struct ci_hdrc *ci)
169{
170 return hw_read(ci, OP_USBINTR, ~0);
171}
172
173/**
174 * hw_read_intr_status: returns interrupt status register
175 *
Peter Chen19353882014-09-22 08:14:17 +0800176 * @ci: the controller
177 *
Li Jun36304b02014-04-23 15:56:39 +0800178 * This function returns register data
179 */
180u32 hw_read_intr_status(struct ci_hdrc *ci)
181{
182 return hw_read(ci, OP_USBSTS, ~0);
183}
184
185/**
Alexander Shishkine443b332012-05-11 17:25:46 +0300186 * hw_port_test_set: writes port test mode (execute without interruption)
187 * @mode: new value
188 *
189 * This function returns an error code
190 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300191int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
Alexander Shishkine443b332012-05-11 17:25:46 +0300192{
193 const u8 TEST_MODE_MAX = 7;
194
195 if (mode > TEST_MODE_MAX)
196 return -EINVAL;
197
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200198 hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
Alexander Shishkine443b332012-05-11 17:25:46 +0300199 return 0;
200}
201
202/**
203 * hw_port_test_get: reads port test mode value
204 *
Peter Chen19353882014-09-22 08:14:17 +0800205 * @ci: the controller
206 *
Alexander Shishkine443b332012-05-11 17:25:46 +0300207 * This function returns port test mode value
208 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300209u8 hw_port_test_get(struct ci_hdrc *ci)
Alexander Shishkine443b332012-05-11 17:25:46 +0300210{
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200211 return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
Alexander Shishkine443b332012-05-11 17:25:46 +0300212}
213
Peter Chenb82613c2014-11-26 13:44:28 +0800214static void hw_wait_phy_stable(void)
215{
216 /*
217 * The phy needs some delay to output the stable status from low
218 * power mode. And for OTGSC, the status inputs are debounced
219 * using a 1 ms time constant, so, delay 2ms for controller to get
220 * the stable status, like vbus and id when the phy leaves low power.
221 */
222 usleep_range(2000, 2500);
223}
224
Peter Chen864cf942013-09-24 12:47:55 +0800225/* The PHY enters/leaves low power mode */
226static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
227{
228 enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
229 bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
230
Peter Chen6d037db2014-11-26 13:44:27 +0800231 if (enable && !lpm)
Peter Chen864cf942013-09-24 12:47:55 +0800232 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
233 PORTSC_PHCD(ci->hw_bank.lpm));
Peter Chen6d037db2014-11-26 13:44:27 +0800234 else if (!enable && lpm)
Peter Chen864cf942013-09-24 12:47:55 +0800235 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
236 0);
Peter Chen864cf942013-09-24 12:47:55 +0800237}
238
Alexander Shishkin8e229782013-06-24 14:46:36 +0300239static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
Alexander Shishkine443b332012-05-11 17:25:46 +0300240{
241 u32 reg;
242
243 /* bank is a module variable */
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300244 ci->hw_bank.abs = base;
Alexander Shishkine443b332012-05-11 17:25:46 +0300245
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300246 ci->hw_bank.cap = ci->hw_bank.abs;
Richard Zhao77c44002012-06-29 17:48:53 +0800247 ci->hw_bank.cap += ci->platdata->capoffset;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200248 ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
Alexander Shishkine443b332012-05-11 17:25:46 +0300249
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300250 hw_alloc_regmap(ci, false);
251 reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200252 __ffs(HCCPARAMS_LEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300253 ci->hw_bank.lpm = reg;
Chris Ruehlaeb2c122013-12-06 16:35:12 +0800254 if (reg)
255 hw_alloc_regmap(ci, !!reg);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300256 ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
257 ci->hw_bank.size += OP_LAST;
258 ci->hw_bank.size /= sizeof(u32);
Alexander Shishkine443b332012-05-11 17:25:46 +0300259
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300260 reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200261 __ffs(DCCPARAMS_DEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300262 ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
Alexander Shishkine443b332012-05-11 17:25:46 +0300263
Richard Zhao09c94e62012-05-15 21:58:18 +0800264 if (ci->hw_ep_max > ENDPT_MAX)
Alexander Shishkine443b332012-05-11 17:25:46 +0300265 return -ENODEV;
266
Peter Chen864cf942013-09-24 12:47:55 +0800267 ci_hdrc_enter_lpm(ci, false);
268
Peter Chenc344b512013-08-14 12:44:09 +0300269 /* Disable all interrupts bits */
270 hw_write(ci, OP_USBINTR, 0xffffffff, 0);
271
272 /* Clear all interrupts status bits*/
273 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
274
Peter Chencb271f32015-02-11 12:44:55 +0800275 ci->rev = ci_get_revision(ci);
276
277 dev_dbg(ci->dev,
278 "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
279 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
Alexander Shishkine443b332012-05-11 17:25:46 +0300280
281 /* setup lock mode ? */
282
283 /* ENDPTSETUPSTAT is '0' by default */
284
285 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
286
287 return 0;
288}
289
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800290void hw_phymode_configure(struct ci_hdrc *ci)
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300291{
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800292 u32 portsc, lpm, sts = 0;
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300293
294 switch (ci->platdata->phy_mode) {
295 case USBPHY_INTERFACE_MODE_UTMI:
296 portsc = PORTSC_PTS(PTS_UTMI);
297 lpm = DEVLC_PTS(PTS_UTMI);
298 break;
299 case USBPHY_INTERFACE_MODE_UTMIW:
300 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
301 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
302 break;
303 case USBPHY_INTERFACE_MODE_ULPI:
304 portsc = PORTSC_PTS(PTS_ULPI);
305 lpm = DEVLC_PTS(PTS_ULPI);
306 break;
307 case USBPHY_INTERFACE_MODE_SERIAL:
308 portsc = PORTSC_PTS(PTS_SERIAL);
309 lpm = DEVLC_PTS(PTS_SERIAL);
310 sts = 1;
311 break;
312 case USBPHY_INTERFACE_MODE_HSIC:
313 portsc = PORTSC_PTS(PTS_HSIC);
314 lpm = DEVLC_PTS(PTS_HSIC);
315 break;
316 default:
317 return;
318 }
319
320 if (ci->hw_bank.lpm) {
321 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800322 if (sts)
323 hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300324 } else {
325 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800326 if (sts)
327 hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300328 }
329}
330
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
Richard Zhao77c44002012-06-29 17:48:53 +0800506 if (ci->platdata->notify_event)
507 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300508 CI_HDRC_CONTROLLER_RESET_EVENT);
Alexander Shishkine443b332012-05-11 17:25:46 +0300509
Alexander Shishkine443b332012-05-11 17:25:46 +0300510 /* USBMODE should be configured step by step */
511 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
Peter Chen5b157302014-11-26 13:44:33 +0800512 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
Alexander Shishkine443b332012-05-11 17:25:46 +0300513 /* HW >= 2.3 */
514 hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
515
Peter Chen5b157302014-11-26 13:44:33 +0800516 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
517 pr_err("cannot enter in %s device mode", ci_role(ci)->name);
Alexander Shishkine443b332012-05-11 17:25:46 +0300518 pr_err("lpm = %i", ci->hw_bank.lpm);
519 return -ENODEV;
520 }
521
Peter Chenbf9c85e2015-03-17 10:40:50 +0800522 ci_platform_configure(ci);
523
Alexander Shishkine443b332012-05-11 17:25:46 +0300524 return 0;
525}
526
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300527static irqreturn_t ci_irq(int irq, void *data)
528{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300529 struct ci_hdrc *ci = data;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300530 irqreturn_t ret = IRQ_NONE;
Richard Zhaob183c192012-09-12 14:58:11 +0300531 u32 otgsc = 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300532
Peter Chen1f874ed2015-02-11 12:44:45 +0800533 if (ci->in_lpm) {
534 disable_irq_nosync(irq);
535 ci->wakeup_int = true;
536 pm_runtime_get(ci->dev);
537 return IRQ_HANDLED;
538 }
539
Li Jun4dcf7202014-04-23 15:56:50 +0800540 if (ci->is_otg) {
Li Jun0c33bf72014-04-23 15:56:38 +0800541 otgsc = hw_read_otgsc(ci, ~0);
Li Jun4dcf7202014-04-23 15:56:50 +0800542 if (ci_otg_is_fsm_mode(ci)) {
543 ret = ci_otg_fsm_irq(ci);
544 if (ret == IRQ_HANDLED)
545 return ret;
546 }
547 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300548
Peter Chena107f8c2013-08-14 12:44:11 +0300549 /*
550 * Handle id change interrupt, it indicates device/host function
551 * switch.
552 */
553 if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
554 ci->id_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800555 /* Clear ID change irq status */
556 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
Peter Chenbe6b0c12014-05-23 08:12:49 +0800557 ci_otg_queue_work(ci);
Peter Chena107f8c2013-08-14 12:44:11 +0300558 return IRQ_HANDLED;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300559 }
560
Peter Chena107f8c2013-08-14 12:44:11 +0300561 /*
562 * Handle vbus change interrupt, it indicates device connection
563 * and disconnection events.
564 */
565 if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
566 ci->b_sess_valid_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800567 /* Clear BSV irq */
568 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
Peter Chenbe6b0c12014-05-23 08:12:49 +0800569 ci_otg_queue_work(ci);
Peter Chena107f8c2013-08-14 12:44:11 +0300570 return IRQ_HANDLED;
571 }
572
573 /* Handle device/host interrupt */
574 if (ci->role != CI_ROLE_END)
575 ret = ci_role(ci)->irq(ci);
576
Richard Zhaob183c192012-09-12 14:58:11 +0300577 return ret;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300578}
579
Stephen Boyd5cc49262017-01-20 15:11:55 +0800580static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
581 void *ptr)
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300582{
Stephen Boyd5cc49262017-01-20 15:11:55 +0800583 struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb);
584 struct ci_hdrc *ci = cbl->ci;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300585
Stephen Boyd5cc49262017-01-20 15:11:55 +0800586 cbl->connected = event;
587 cbl->changed = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300588
589 ci_irq(ci->irq, ci);
590 return NOTIFY_DONE;
591}
592
Peter Chen1542d9c2013-08-14 12:44:03 +0300593static int ci_get_platdata(struct device *dev,
594 struct ci_hdrc_platform_data *platdata)
595{
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300596 struct extcon_dev *ext_vbus, *ext_id;
597 struct ci_hdrc_cable *cable;
Li Jun79742352015-07-09 15:18:45 +0800598 int ret;
599
Peter Chenc22600c2013-09-17 12:37:22 +0800600 if (!platdata->phy_mode)
601 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
602
603 if (!platdata->dr_mode)
Heikki Krogerus06e71142015-09-21 11:14:34 +0300604 platdata->dr_mode = usb_get_dr_mode(dev);
Peter Chenc22600c2013-09-17 12:37:22 +0800605
606 if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
607 platdata->dr_mode = USB_DR_MODE_OTG;
608
Peter Chenc2ec3a72013-10-30 09:19:29 +0800609 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
610 /* Get the vbus regulator */
611 platdata->reg_vbus = devm_regulator_get(dev, "vbus");
612 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
613 return -EPROBE_DEFER;
614 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
Mickael Maison66294672014-11-26 13:44:38 +0800615 /* no vbus regulator is needed */
Peter Chenc2ec3a72013-10-30 09:19:29 +0800616 platdata->reg_vbus = NULL;
617 } else if (IS_ERR(platdata->reg_vbus)) {
618 dev_err(dev, "Getting regulator error: %ld\n",
619 PTR_ERR(platdata->reg_vbus));
620 return PTR_ERR(platdata->reg_vbus);
621 }
Peter Chenf6a9ff02014-08-19 09:51:56 +0800622 /* Get TPL support */
623 if (!platdata->tpl_support)
624 platdata->tpl_support =
625 of_usb_host_tpl_support(dev->of_node);
Peter Chenc2ec3a72013-10-30 09:19:29 +0800626 }
627
Li Jun79742352015-07-09 15:18:45 +0800628 if (platdata->dr_mode == USB_DR_MODE_OTG) {
629 /* We can support HNP and SRP of OTG 2.0 */
630 platdata->ci_otg_caps.otg_rev = 0x0200;
631 platdata->ci_otg_caps.hnp_support = true;
632 platdata->ci_otg_caps.srp_support = true;
633
634 /* Update otg capabilities by DT properties */
635 ret = of_usb_update_otg_caps(dev->of_node,
636 &platdata->ci_otg_caps);
637 if (ret)
638 return ret;
639 }
640
Heikki Krogerus63863b92015-09-21 11:14:32 +0300641 if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
Michael Grzeschik4f6743d2014-02-19 13:41:43 +0800642 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
643
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530644 of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
Fabio Estevam1fbf4622015-09-08 22:18:14 -0300645 &platdata->phy_clkgate_delay_us);
646
Peter Chendf96ed82014-09-22 16:45:39 +0800647 platdata->itc_setting = 1;
Peter Chendf96ed82014-09-22 16:45:39 +0800648
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530649 of_property_read_u32(dev->of_node, "itc-setting",
650 &platdata->itc_setting);
651
652 ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
653 &platdata->ahb_burst_config);
654 if (!ret) {
Peter Chen65668712015-03-17 14:21:00 +0800655 platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530656 } else if (ret != -EINVAL) {
657 dev_err(dev, "failed to get ahb-burst-config\n");
658 return ret;
Peter Chen65668712015-03-17 14:21:00 +0800659 }
660
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530661 ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
662 &platdata->tx_burst_size);
663 if (!ret) {
Peter Chen96625ea2015-03-17 17:32:45 +0800664 platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530665 } else if (ret != -EINVAL) {
666 dev_err(dev, "failed to get tx-burst-size-dword\n");
667 return ret;
Peter Chen96625ea2015-03-17 17:32:45 +0800668 }
669
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530670 ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
671 &platdata->rx_burst_size);
672 if (!ret) {
Peter Chen96625ea2015-03-17 17:32:45 +0800673 platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
Saurabh Sengar4b19b782015-11-18 09:40:12 +0530674 } else if (ret != -EINVAL) {
675 dev_err(dev, "failed to get rx-burst-size-dword\n");
676 return ret;
Peter Chen96625ea2015-03-17 17:32:45 +0800677 }
678
Peter Chenaa738182016-02-01 14:23:44 +0800679 if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
680 platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
681
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300682 ext_id = ERR_PTR(-ENODEV);
683 ext_vbus = ERR_PTR(-ENODEV);
684 if (of_property_read_bool(dev->of_node, "extcon")) {
685 /* Each one of them is not mandatory */
686 ext_vbus = extcon_get_edev_by_phandle(dev, 0);
687 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
688 return PTR_ERR(ext_vbus);
689
690 ext_id = extcon_get_edev_by_phandle(dev, 1);
691 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
692 return PTR_ERR(ext_id);
693 }
694
695 cable = &platdata->vbus_extcon;
Stephen Boyd5cc49262017-01-20 15:11:55 +0800696 cable->nb.notifier_call = ci_cable_notifier;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300697 cable->edev = ext_vbus;
698
699 if (!IS_ERR(ext_vbus)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900700 ret = extcon_get_state(cable->edev, EXTCON_USB);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300701 if (ret)
Stephen Boyd5cc49262017-01-20 15:11:55 +0800702 cable->connected = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300703 else
Stephen Boyd5cc49262017-01-20 15:11:55 +0800704 cable->connected = false;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300705 }
706
707 cable = &platdata->id_extcon;
Stephen Boyd5cc49262017-01-20 15:11:55 +0800708 cable->nb.notifier_call = ci_cable_notifier;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300709 cable->edev = ext_id;
710
711 if (!IS_ERR(ext_id)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900712 ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300713 if (ret)
Stephen Boyd5cc49262017-01-20 15:11:55 +0800714 cable->connected = true;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300715 else
Stephen Boyd5cc49262017-01-20 15:11:55 +0800716 cable->connected = false;
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300717 }
Peter Chen1542d9c2013-08-14 12:44:03 +0300718 return 0;
719}
720
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300721static int ci_extcon_register(struct ci_hdrc *ci)
722{
723 struct ci_hdrc_cable *id, *vbus;
724 int ret;
725
726 id = &ci->platdata->id_extcon;
727 id->ci = ci;
728 if (!IS_ERR(id->edev)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900729 ret = devm_extcon_register_notifier(ci->dev, id->edev,
730 EXTCON_USB_HOST, &id->nb);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300731 if (ret < 0) {
732 dev_err(ci->dev, "register ID failed\n");
733 return ret;
734 }
735 }
736
737 vbus = &ci->platdata->vbus_extcon;
738 vbus->ci = ci;
739 if (!IS_ERR(vbus->edev)) {
Chanwoo Choi3f991aa2016-11-30 14:57:33 +0900740 ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
741 EXTCON_USB, &vbus->nb);
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300742 if (ret < 0) {
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300743 dev_err(ci->dev, "register VBUS failed\n");
744 return ret;
745 }
746 }
747
748 return 0;
749}
750
Richard Zhaofe6e1252012-07-07 22:56:42 +0800751static DEFINE_IDA(ci_ida);
752
Alexander Shishkin8e229782013-06-24 14:46:36 +0300753struct platform_device *ci_hdrc_add_device(struct device *dev,
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800754 struct resource *res, int nres,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300755 struct ci_hdrc_platform_data *platdata)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800756{
757 struct platform_device *pdev;
Richard Zhaofe6e1252012-07-07 22:56:42 +0800758 int id, ret;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800759
Peter Chen1542d9c2013-08-14 12:44:03 +0300760 ret = ci_get_platdata(dev, platdata);
761 if (ret)
762 return ERR_PTR(ret);
763
Richard Zhaofe6e1252012-07-07 22:56:42 +0800764 id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
765 if (id < 0)
766 return ERR_PTR(id);
767
768 pdev = platform_device_alloc("ci_hdrc", id);
769 if (!pdev) {
770 ret = -ENOMEM;
771 goto put_id;
772 }
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800773
774 pdev->dev.parent = dev;
775 pdev->dev.dma_mask = dev->dma_mask;
776 pdev->dev.dma_parms = dev->dma_parms;
777 dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
778
779 ret = platform_device_add_resources(pdev, res, nres);
780 if (ret)
781 goto err;
782
783 ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
784 if (ret)
785 goto err;
786
787 ret = platform_device_add(pdev);
788 if (ret)
789 goto err;
790
791 return pdev;
792
793err:
794 platform_device_put(pdev);
Richard Zhaofe6e1252012-07-07 22:56:42 +0800795put_id:
796 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800797 return ERR_PTR(ret);
798}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300799EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800800
Alexander Shishkin8e229782013-06-24 14:46:36 +0300801void ci_hdrc_remove_device(struct platform_device *pdev)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800802{
Lothar Waßmann98c35532012-11-22 10:11:25 +0100803 int id = pdev->id;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800804 platform_device_unregister(pdev);
Lothar Waßmann98c35532012-11-22 10:11:25 +0100805 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800806}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300807EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800808
Peter Chen3f124d22013-08-14 12:44:07 +0300809static inline void ci_role_destroy(struct ci_hdrc *ci)
810{
811 ci_hdrc_gadget_destroy(ci);
812 ci_hdrc_host_destroy(ci);
Peter Chencbec6bd2013-08-14 12:44:10 +0300813 if (ci->is_otg)
814 ci_hdrc_otg_destroy(ci);
Peter Chen3f124d22013-08-14 12:44:07 +0300815}
816
Peter Chen577b2322013-08-14 12:44:08 +0300817static void ci_get_otg_capable(struct ci_hdrc *ci)
818{
819 if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
820 ci->is_otg = false;
821 else
822 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
823 DCCPARAMS_DC | DCCPARAMS_HC)
824 == (DCCPARAMS_DC | DCCPARAMS_HC));
Peter Chen2e37cfd2015-02-11 12:44:51 +0800825 if (ci->is_otg) {
Peter Chen577b2322013-08-14 12:44:08 +0300826 dev_dbg(ci->dev, "It is OTG capable controller\n");
Peter Chen2e37cfd2015-02-11 12:44:51 +0800827 /* Disable and clear all OTG irq */
828 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
829 OTGSC_INT_STATUS_BITS);
830 }
Peter Chen577b2322013-08-14 12:44:08 +0300831}
832
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500833static int ci_hdrc_probe(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +0300834{
835 struct device *dev = &pdev->dev;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300836 struct ci_hdrc *ci;
Alexander Shishkine443b332012-05-11 17:25:46 +0300837 struct resource *res;
838 void __iomem *base;
839 int ret;
Sascha Hauer691962d2013-06-13 17:59:57 +0300840 enum usb_dr_mode dr_mode;
Alexander Shishkine443b332012-05-11 17:25:46 +0300841
Jingoo Hanfad56742014-02-19 13:41:42 +0800842 if (!dev_get_platdata(dev)) {
Alexander Shishkine443b332012-05-11 17:25:46 +0300843 dev_err(dev, "platform data missing\n");
844 return -ENODEV;
845 }
846
847 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi19290812013-03-30 02:46:27 +0200848 base = devm_ioremap_resource(dev, res);
849 if (IS_ERR(base))
850 return PTR_ERR(base);
Alexander Shishkine443b332012-05-11 17:25:46 +0300851
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300852 ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
Fabio Estevamd0f99242014-11-26 13:44:23 +0800853 if (!ci)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300854 return -ENOMEM;
Alexander Shishkine443b332012-05-11 17:25:46 +0300855
Peter Chena5d906b2016-11-15 18:05:33 +0800856 spin_lock_init(&ci->lock);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300857 ci->dev = dev;
Jingoo Hanfad56742014-02-19 13:41:42 +0800858 ci->platdata = dev_get_platdata(dev);
Peter Chened8f8312014-01-10 13:51:27 +0800859 ci->imx28_write_fix = !!(ci->platdata->flags &
860 CI_HDRC_IMX28_WRITE_FIX);
Peter Chen1f874ed2015-02-11 12:44:45 +0800861 ci->supports_runtime_pm = !!(ci->platdata->flags &
862 CI_HDRC_SUPPORTS_RUNTIME_PM);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800863 platform_set_drvdata(pdev, ci);
Alexander Shishkine443b332012-05-11 17:25:46 +0300864
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300865 ret = hw_device_init(ci, base);
866 if (ret < 0) {
867 dev_err(dev, "can't initialize hardware\n");
868 return -ENODEV;
869 }
870
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800871 ret = ci_ulpi_init(ci);
872 if (ret)
873 return ret;
874
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100875 if (ci->platdata->phy) {
876 ci->phy = ci->platdata->phy;
877 } else if (ci->platdata->usb_phy) {
Antoine Tenartef44cb42014-10-30 18:41:16 +0100878 ci->usb_phy = ci->platdata->usb_phy;
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100879 } else {
Antoine Tenart21a5b572014-11-26 13:44:35 +0800880 ci->phy = devm_phy_get(dev->parent, "usb-phy");
881 ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2);
Peter Chenc859aa652014-02-19 13:41:40 +0800882
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100883 /* if both generic PHY and USB PHY layers aren't enabled */
884 if (PTR_ERR(ci->phy) == -ENOSYS &&
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800885 PTR_ERR(ci->usb_phy) == -ENXIO) {
886 ret = -ENXIO;
887 goto ulpi_exit;
888 }
Peter Chenc859aa652014-02-19 13:41:40 +0800889
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -0800890 if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) {
891 ret = -EPROBE_DEFER;
892 goto ulpi_exit;
893 }
Antoine Tenart1e5e2d32014-10-30 18:41:19 +0100894
895 if (IS_ERR(ci->phy))
896 ci->phy = NULL;
897 else if (IS_ERR(ci->usb_phy))
898 ci->usb_phy = NULL;
Peter Chenc859aa652014-02-19 13:41:40 +0800899 }
900
Peter Chend03cccf2014-04-23 15:56:37 +0800901 ret = ci_usb_phy_init(ci);
Peter Chen74475ed2013-09-24 12:47:53 +0800902 if (ret) {
903 dev_err(dev, "unable to init phy: %d\n", ret);
904 return ret;
905 }
906
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300907 ci->hw_bank.phys = res->start;
908
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300909 ci->irq = platform_get_irq(pdev, 0);
910 if (ci->irq < 0) {
911 dev_err(dev, "missing IRQ\n");
Fabio Estevam42d18212014-02-19 13:41:44 +0800912 ret = ci->irq;
Peter Chenc859aa652014-02-19 13:41:40 +0800913 goto deinit_phy;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300914 }
915
Peter Chen577b2322013-08-14 12:44:08 +0300916 ci_get_otg_capable(ci);
917
Sascha Hauer691962d2013-06-13 17:59:57 +0300918 dr_mode = ci->platdata->dr_mode;
919 /* initialize role(s) before the interrupt is requested */
920 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
921 ret = ci_hdrc_host_init(ci);
922 if (ret)
923 dev_info(dev, "doesn't support host\n");
924 }
925
926 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
927 ret = ci_hdrc_gadget_init(ci);
928 if (ret)
929 dev_info(dev, "doesn't support gadget\n");
930 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300931
932 if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
933 dev_err(dev, "no supported roles\n");
Peter Chen74475ed2013-09-24 12:47:53 +0800934 ret = -ENODEV;
Peter Chenc859aa652014-02-19 13:41:40 +0800935 goto deinit_phy;
Peter Chencbec6bd2013-08-14 12:44:10 +0300936 }
937
Peter Chen27c62c22014-09-22 08:14:16 +0800938 if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
Peter Chencbec6bd2013-08-14 12:44:10 +0300939 ret = ci_hdrc_otg_init(ci);
940 if (ret) {
941 dev_err(dev, "init otg fails, ret = %d\n", ret);
942 goto stop;
943 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300944 }
945
946 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
Peter Chen577b2322013-08-14 12:44:08 +0300947 if (ci->is_otg) {
Peter Chen577b2322013-08-14 12:44:08 +0300948 ci->role = ci_otg_role(ci);
Li Jun0c33bf72014-04-23 15:56:38 +0800949 /* Enable ID change irq */
950 hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
Peter Chen577b2322013-08-14 12:44:08 +0300951 } else {
952 /*
953 * If the controller is not OTG capable, but support
954 * role switch, the defalt role is gadget, and the
955 * user can switch it through debugfs.
956 */
957 ci->role = CI_ROLE_GADGET;
958 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300959 } else {
960 ci->role = ci->roles[CI_ROLE_HOST]
961 ? CI_ROLE_HOST
962 : CI_ROLE_GADGET;
963 }
964
Li Jun4dcf7202014-04-23 15:56:50 +0800965 if (!ci_otg_is_fsm_mode(ci)) {
Li Jun961ea492015-02-11 12:45:03 +0800966 /* only update vbus status for peripheral */
967 if (ci->role == CI_ROLE_GADGET)
968 ci_handle_vbus_change(ci);
969
Li Jun4dcf7202014-04-23 15:56:50 +0800970 ret = ci_role_start(ci, ci->role);
971 if (ret) {
972 dev_err(dev, "can't start %s role\n",
973 ci_role(ci)->name);
974 goto stop;
975 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300976 }
977
Peter Chen4c503dd2014-11-26 13:44:22 +0800978 ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
979 ci->platdata->name, ci);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300980 if (ret)
981 goto stop;
982
Ivan T. Ivanov3ecb3e02015-09-07 14:45:25 +0300983 ret = ci_extcon_register(ci);
984 if (ret)
985 goto stop;
986
Peter Chen1f874ed2015-02-11 12:44:45 +0800987 if (ci->supports_runtime_pm) {
988 pm_runtime_set_active(&pdev->dev);
989 pm_runtime_enable(&pdev->dev);
990 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
991 pm_runtime_mark_last_busy(ci->dev);
992 pm_runtime_use_autosuspend(&pdev->dev);
993 }
994
Li Jun4dcf7202014-04-23 15:56:50 +0800995 if (ci_otg_is_fsm_mode(ci))
996 ci_hdrc_otg_fsm_start(ci);
997
Peter Chenf8efa762015-02-11 12:44:48 +0800998 device_set_wakeup_capable(&pdev->dev, true);
999
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001000 ret = dbg_create_files(ci);
1001 if (!ret)
1002 return 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001003
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001004stop:
Peter Chen3f124d22013-08-14 12:44:07 +03001005 ci_role_destroy(ci);
Peter Chenc859aa652014-02-19 13:41:40 +08001006deinit_phy:
Antoine Tenart1e5e2d32014-10-30 18:41:19 +01001007 ci_usb_phy_exit(ci);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001008ulpi_exit:
1009 ci_ulpi_exit(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001010
1011 return ret;
1012}
1013
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001014static int ci_hdrc_remove(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +03001015{
Alexander Shishkin8e229782013-06-24 14:46:36 +03001016 struct ci_hdrc *ci = platform_get_drvdata(pdev);
Alexander Shishkine443b332012-05-11 17:25:46 +03001017
Peter Chen1f874ed2015-02-11 12:44:45 +08001018 if (ci->supports_runtime_pm) {
1019 pm_runtime_get_sync(&pdev->dev);
1020 pm_runtime_disable(&pdev->dev);
1021 pm_runtime_put_noidle(&pdev->dev);
1022 }
1023
Alexander Shishkinadf0f732013-03-30 12:53:53 +02001024 dbg_remove_files(ci);
Peter Chen3f124d22013-08-14 12:44:07 +03001025 ci_role_destroy(ci);
Peter Chen864cf942013-09-24 12:47:55 +08001026 ci_hdrc_enter_lpm(ci, true);
Antoine Tenart1e5e2d32014-10-30 18:41:19 +01001027 ci_usb_phy_exit(ci);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001028 ci_ulpi_exit(ci);
Alexander Shishkine443b332012-05-11 17:25:46 +03001029
1030 return 0;
1031}
1032
Peter Chen1f874ed2015-02-11 12:44:45 +08001033#ifdef CONFIG_PM
Li Jun961ea492015-02-11 12:45:03 +08001034/* Prepare wakeup by SRP before suspend */
1035static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
1036{
1037 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1038 !hw_read_otgsc(ci, OTGSC_ID)) {
1039 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
1040 PORTSC_PP);
1041 hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
1042 PORTSC_WKCN);
1043 }
1044}
1045
1046/* Handle SRP when wakeup by data pulse */
1047static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
1048{
1049 if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
1050 (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
1051 if (!hw_read_otgsc(ci, OTGSC_ID)) {
1052 ci->fsm.a_srp_det = 1;
1053 ci->fsm.a_bus_drop = 0;
1054 } else {
1055 ci->fsm.id = 1;
1056 }
1057 ci_otg_queue_work(ci);
1058 }
1059}
1060
Peter Chen80769322014-11-26 13:44:29 +08001061static void ci_controller_suspend(struct ci_hdrc *ci)
1062{
Peter Chen1f874ed2015-02-11 12:44:45 +08001063 disable_irq(ci->irq);
Peter Chen80769322014-11-26 13:44:29 +08001064 ci_hdrc_enter_lpm(ci, true);
Fabio Estevam1fbf4622015-09-08 22:18:14 -03001065 if (ci->platdata->phy_clkgate_delay_us)
1066 usleep_range(ci->platdata->phy_clkgate_delay_us,
1067 ci->platdata->phy_clkgate_delay_us + 50);
Peter Chen1f874ed2015-02-11 12:44:45 +08001068 usb_phy_set_suspend(ci->usb_phy, 1);
1069 ci->in_lpm = true;
1070 enable_irq(ci->irq);
Peter Chen80769322014-11-26 13:44:29 +08001071}
1072
1073static int ci_controller_resume(struct device *dev)
1074{
1075 struct ci_hdrc *ci = dev_get_drvdata(dev);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001076 int ret;
Peter Chen80769322014-11-26 13:44:29 +08001077
1078 dev_dbg(dev, "at %s\n", __func__);
1079
Peter Chen1f874ed2015-02-11 12:44:45 +08001080 if (!ci->in_lpm) {
1081 WARN_ON(1);
1082 return 0;
1083 }
Peter Chen80769322014-11-26 13:44:29 +08001084
Peter Chen1f874ed2015-02-11 12:44:45 +08001085 ci_hdrc_enter_lpm(ci, false);
Stephen Boyd7bb7e9b2016-12-28 14:56:55 -08001086
1087 ret = ci_ulpi_resume(ci);
1088 if (ret)
1089 return ret;
1090
Peter Chen80769322014-11-26 13:44:29 +08001091 if (ci->usb_phy) {
1092 usb_phy_set_suspend(ci->usb_phy, 0);
1093 usb_phy_set_wakeup(ci->usb_phy, false);
1094 hw_wait_phy_stable();
1095 }
1096
Peter Chen1f874ed2015-02-11 12:44:45 +08001097 ci->in_lpm = false;
1098 if (ci->wakeup_int) {
1099 ci->wakeup_int = false;
1100 pm_runtime_mark_last_busy(ci->dev);
1101 pm_runtime_put_autosuspend(ci->dev);
1102 enable_irq(ci->irq);
Li Jun961ea492015-02-11 12:45:03 +08001103 if (ci_otg_is_fsm_mode(ci))
1104 ci_otg_fsm_wakeup_by_srp(ci);
Peter Chen1f874ed2015-02-11 12:44:45 +08001105 }
1106
Peter Chen80769322014-11-26 13:44:29 +08001107 return 0;
1108}
1109
Peter Chen1f874ed2015-02-11 12:44:45 +08001110#ifdef CONFIG_PM_SLEEP
Peter Chen80769322014-11-26 13:44:29 +08001111static int ci_suspend(struct device *dev)
1112{
1113 struct ci_hdrc *ci = dev_get_drvdata(dev);
1114
1115 if (ci->wq)
1116 flush_workqueue(ci->wq);
Peter Chen1f874ed2015-02-11 12:44:45 +08001117 /*
1118 * Controller needs to be active during suspend, otherwise the core
1119 * may run resume when the parent is at suspend if other driver's
1120 * suspend fails, it occurs before parent's suspend has not started,
1121 * but the core suspend has finished.
1122 */
1123 if (ci->in_lpm)
1124 pm_runtime_resume(dev);
1125
1126 if (ci->in_lpm) {
1127 WARN_ON(1);
1128 return 0;
1129 }
Peter Chen80769322014-11-26 13:44:29 +08001130
Peter Chenf8efa762015-02-11 12:44:48 +08001131 if (device_may_wakeup(dev)) {
Li Jun961ea492015-02-11 12:45:03 +08001132 if (ci_otg_is_fsm_mode(ci))
1133 ci_otg_fsm_suspend_for_srp(ci);
1134
Peter Chenf8efa762015-02-11 12:44:48 +08001135 usb_phy_set_wakeup(ci->usb_phy, true);
1136 enable_irq_wake(ci->irq);
1137 }
1138
Peter Chen80769322014-11-26 13:44:29 +08001139 ci_controller_suspend(ci);
1140
1141 return 0;
1142}
1143
1144static int ci_resume(struct device *dev)
1145{
Peter Chen1f874ed2015-02-11 12:44:45 +08001146 struct ci_hdrc *ci = dev_get_drvdata(dev);
1147 int ret;
1148
Peter Chenf8efa762015-02-11 12:44:48 +08001149 if (device_may_wakeup(dev))
1150 disable_irq_wake(ci->irq);
1151
Peter Chen1f874ed2015-02-11 12:44:45 +08001152 ret = ci_controller_resume(dev);
1153 if (ret)
1154 return ret;
1155
1156 if (ci->supports_runtime_pm) {
1157 pm_runtime_disable(dev);
1158 pm_runtime_set_active(dev);
1159 pm_runtime_enable(dev);
1160 }
1161
1162 return ret;
Peter Chen80769322014-11-26 13:44:29 +08001163}
1164#endif /* CONFIG_PM_SLEEP */
1165
Peter Chen1f874ed2015-02-11 12:44:45 +08001166static int ci_runtime_suspend(struct device *dev)
1167{
1168 struct ci_hdrc *ci = dev_get_drvdata(dev);
1169
1170 dev_dbg(dev, "at %s\n", __func__);
1171
1172 if (ci->in_lpm) {
1173 WARN_ON(1);
1174 return 0;
1175 }
1176
Li Jun961ea492015-02-11 12:45:03 +08001177 if (ci_otg_is_fsm_mode(ci))
1178 ci_otg_fsm_suspend_for_srp(ci);
1179
Peter Chen1f874ed2015-02-11 12:44:45 +08001180 usb_phy_set_wakeup(ci->usb_phy, true);
1181 ci_controller_suspend(ci);
1182
1183 return 0;
1184}
1185
1186static int ci_runtime_resume(struct device *dev)
1187{
1188 return ci_controller_resume(dev);
1189}
1190
1191#endif /* CONFIG_PM */
Peter Chen80769322014-11-26 13:44:29 +08001192static const struct dev_pm_ops ci_pm_ops = {
1193 SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
Peter Chen1f874ed2015-02-11 12:44:45 +08001194 SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
Peter Chen80769322014-11-26 13:44:29 +08001195};
Peter Chen1f874ed2015-02-11 12:44:45 +08001196
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001197static struct platform_driver ci_hdrc_driver = {
1198 .probe = ci_hdrc_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001199 .remove = ci_hdrc_remove,
Alexander Shishkine443b332012-05-11 17:25:46 +03001200 .driver = {
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001201 .name = "ci_hdrc",
Peter Chen80769322014-11-26 13:44:29 +08001202 .pm = &ci_pm_ops,
Alexander Shishkine443b332012-05-11 17:25:46 +03001203 },
1204};
1205
Peter Chen2f01a332015-07-21 09:51:29 +08001206static int __init ci_hdrc_platform_register(void)
1207{
1208 ci_hdrc_host_driver_init();
1209 return platform_driver_register(&ci_hdrc_driver);
1210}
1211module_init(ci_hdrc_platform_register);
1212
1213static void __exit ci_hdrc_platform_unregister(void)
1214{
1215 platform_driver_unregister(&ci_hdrc_driver);
1216}
1217module_exit(ci_hdrc_platform_unregister);
Alexander Shishkine443b332012-05-11 17:25:46 +03001218
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001219MODULE_ALIAS("platform:ci_hdrc");
Alexander Shishkine443b332012-05-11 17:25:46 +03001220MODULE_LICENSE("GPL v2");
1221MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
Alexander Shishkin5f36e232012-05-11 17:25:47 +03001222MODULE_DESCRIPTION("ChipIdea HDRC Driver");