blob: 128b92ba58a80c2844ae988d1f3a1e990dfe3c9d [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
26 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
27 * - STALL_IN: non-empty bulk-in pipes cannot be halted
28 * if defined mass storage compliance succeeds but with warnings
29 * => case 4: Hi > Dn
30 * => case 5: Hi > Di
31 * => case 8: Hi <> Do
32 * if undefined usbtest 13 fails
33 * - TRACE: enable function tracing (depends on DEBUG)
34 *
35 * Main Features
36 * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
37 * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
38 * - Normal & LPM support
39 *
40 * USBTEST Report
41 * - OK: 0-12, 13 (STALL_IN defined) & 14
42 * - Not Supported: 15 & 16 (ISO)
43 *
44 * TODO List
Michael Grzeschike4ce4ec2013-06-13 17:59:47 +030045 * - Interrupt Traffic
Alexander Shishkine443b332012-05-11 17:25:46 +030046 * - GET_STATUS(device) - always reports 0
47 * - Gadget API (majority of optional features)
48 * - Suspend & Remote Wakeup
49 */
50#include <linux/delay.h>
51#include <linux/device.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030052#include <linux/dma-mapping.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030053#include <linux/platform_device.h>
54#include <linux/module.h>
Richard Zhaofe6e1252012-07-07 22:56:42 +080055#include <linux/idr.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030056#include <linux/interrupt.h>
57#include <linux/io.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030058#include <linux/kernel.h>
59#include <linux/slab.h>
60#include <linux/pm_runtime.h>
61#include <linux/usb/ch9.h>
62#include <linux/usb/gadget.h>
63#include <linux/usb/otg.h>
64#include <linux/usb/chipidea.h>
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +030065#include <linux/usb/of.h>
Michael Grzeschik4f6743d2014-02-19 13:41:43 +080066#include <linux/of.h>
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +030067#include <linux/phy.h>
Peter Chen1542d9c2013-08-14 12:44:03 +030068#include <linux/regulator/consumer.h>
Alexander Shishkine443b332012-05-11 17:25:46 +030069
70#include "ci.h"
71#include "udc.h"
72#include "bits.h"
Alexander Shishkineb70e5a2012-05-11 17:25:54 +030073#include "host.h"
Alexander Shishkine443b332012-05-11 17:25:46 +030074#include "debug.h"
Peter Chenc10b4f02013-08-14 12:44:06 +030075#include "otg.h"
Li Jun4dcf7202014-04-23 15:56:50 +080076#include "otg_fsm.h"
Alexander Shishkine443b332012-05-11 17:25:46 +030077
Alexander Shishkin5f36e232012-05-11 17:25:47 +030078/* Controller register map */
Marc Kleine-Budde987e7bc2014-01-06 10:10:39 +080079static const u8 ci_regs_nolpm[] = {
80 [CAP_CAPLENGTH] = 0x00U,
81 [CAP_HCCPARAMS] = 0x08U,
82 [CAP_DCCPARAMS] = 0x24U,
83 [CAP_TESTMODE] = 0x38U,
84 [OP_USBCMD] = 0x00U,
85 [OP_USBSTS] = 0x04U,
86 [OP_USBINTR] = 0x08U,
87 [OP_DEVICEADDR] = 0x14U,
88 [OP_ENDPTLISTADDR] = 0x18U,
89 [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,
111 [OP_PORTSC] = 0x44U,
112 [OP_DEVLC] = 0x84U,
113 [OP_OTGSC] = 0xC4U,
114 [OP_USBMODE] = 0xC8U,
115 [OP_ENDPTSETUPSTAT] = 0xD8U,
116 [OP_ENDPTPRIME] = 0xDCU,
117 [OP_ENDPTFLUSH] = 0xE0U,
118 [OP_ENDPTSTAT] = 0xE4U,
119 [OP_ENDPTCOMPLETE] = 0xE8U,
120 [OP_ENDPTCTRL] = 0xECU,
Alexander Shishkine443b332012-05-11 17:25:46 +0300121};
122
Alexander Shishkin8e229782013-06-24 14:46:36 +0300123static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
Alexander Shishkine443b332012-05-11 17:25:46 +0300124{
125 int i;
126
Alexander Shishkine443b332012-05-11 17:25:46 +0300127 for (i = 0; i < OP_ENDPTCTRL; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300128 ci->hw_bank.regmap[i] =
129 (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
Alexander Shishkine443b332012-05-11 17:25:46 +0300130 (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
131
132 for (; i <= OP_LAST; i++)
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300133 ci->hw_bank.regmap[i] = ci->hw_bank.op +
Alexander Shishkine443b332012-05-11 17:25:46 +0300134 4 * (i - OP_ENDPTCTRL) +
135 (is_lpm
136 ? ci_regs_lpm[OP_ENDPTCTRL]
137 : ci_regs_nolpm[OP_ENDPTCTRL]);
138
139 return 0;
140}
141
142/**
Li Jun36304b02014-04-23 15:56:39 +0800143 * hw_read_intr_enable: returns interrupt enable register
144 *
145 * This function returns register data
146 */
147u32 hw_read_intr_enable(struct ci_hdrc *ci)
148{
149 return hw_read(ci, OP_USBINTR, ~0);
150}
151
152/**
153 * hw_read_intr_status: returns interrupt status register
154 *
155 * This function returns register data
156 */
157u32 hw_read_intr_status(struct ci_hdrc *ci)
158{
159 return hw_read(ci, OP_USBSTS, ~0);
160}
161
162/**
Alexander Shishkine443b332012-05-11 17:25:46 +0300163 * hw_port_test_set: writes port test mode (execute without interruption)
164 * @mode: new value
165 *
166 * This function returns an error code
167 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300168int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
Alexander Shishkine443b332012-05-11 17:25:46 +0300169{
170 const u8 TEST_MODE_MAX = 7;
171
172 if (mode > TEST_MODE_MAX)
173 return -EINVAL;
174
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200175 hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
Alexander Shishkine443b332012-05-11 17:25:46 +0300176 return 0;
177}
178
179/**
180 * hw_port_test_get: reads port test mode value
181 *
182 * This function returns port test mode value
183 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300184u8 hw_port_test_get(struct ci_hdrc *ci)
Alexander Shishkine443b332012-05-11 17:25:46 +0300185{
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200186 return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
Alexander Shishkine443b332012-05-11 17:25:46 +0300187}
188
Peter Chen864cf942013-09-24 12:47:55 +0800189/* The PHY enters/leaves low power mode */
190static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
191{
192 enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
193 bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
194
195 if (enable && !lpm) {
196 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
197 PORTSC_PHCD(ci->hw_bank.lpm));
198 } else if (!enable && lpm) {
199 hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
200 0);
201 /*
Peter Chen90893b92014-04-23 15:56:41 +0800202 * the PHY needs some time (less
Peter Chen864cf942013-09-24 12:47:55 +0800203 * than 1ms) to leave low power mode.
204 */
Peter Chen90893b92014-04-23 15:56:41 +0800205 usleep_range(1000, 1100);
Peter Chen864cf942013-09-24 12:47:55 +0800206 }
207}
208
Alexander Shishkin8e229782013-06-24 14:46:36 +0300209static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
Alexander Shishkine443b332012-05-11 17:25:46 +0300210{
211 u32 reg;
212
213 /* bank is a module variable */
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300214 ci->hw_bank.abs = base;
Alexander Shishkine443b332012-05-11 17:25:46 +0300215
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300216 ci->hw_bank.cap = ci->hw_bank.abs;
Richard Zhao77c44002012-06-29 17:48:53 +0800217 ci->hw_bank.cap += ci->platdata->capoffset;
Svetoslav Neykov938d3232013-03-30 12:54:03 +0200218 ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
Alexander Shishkine443b332012-05-11 17:25:46 +0300219
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300220 hw_alloc_regmap(ci, false);
221 reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200222 __ffs(HCCPARAMS_LEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300223 ci->hw_bank.lpm = reg;
Chris Ruehlaeb2c122013-12-06 16:35:12 +0800224 if (reg)
225 hw_alloc_regmap(ci, !!reg);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300226 ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
227 ci->hw_bank.size += OP_LAST;
228 ci->hw_bank.size /= sizeof(u32);
Alexander Shishkine443b332012-05-11 17:25:46 +0300229
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300230 reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
Felipe Balbi727b4dd2013-03-30 12:53:55 +0200231 __ffs(DCCPARAMS_DEN);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300232 ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
Alexander Shishkine443b332012-05-11 17:25:46 +0300233
Richard Zhao09c94e62012-05-15 21:58:18 +0800234 if (ci->hw_ep_max > ENDPT_MAX)
Alexander Shishkine443b332012-05-11 17:25:46 +0300235 return -ENODEV;
236
Peter Chen864cf942013-09-24 12:47:55 +0800237 ci_hdrc_enter_lpm(ci, false);
238
Peter Chenc344b512013-08-14 12:44:09 +0300239 /* Disable all interrupts bits */
240 hw_write(ci, OP_USBINTR, 0xffffffff, 0);
241
242 /* Clear all interrupts status bits*/
243 hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
244
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300245 dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
246 ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
Alexander Shishkine443b332012-05-11 17:25:46 +0300247
248 /* setup lock mode ? */
249
250 /* ENDPTSETUPSTAT is '0' by default */
251
252 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
253
254 return 0;
255}
256
Alexander Shishkin8e229782013-06-24 14:46:36 +0300257static void hw_phymode_configure(struct ci_hdrc *ci)
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300258{
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800259 u32 portsc, lpm, sts = 0;
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300260
261 switch (ci->platdata->phy_mode) {
262 case USBPHY_INTERFACE_MODE_UTMI:
263 portsc = PORTSC_PTS(PTS_UTMI);
264 lpm = DEVLC_PTS(PTS_UTMI);
265 break;
266 case USBPHY_INTERFACE_MODE_UTMIW:
267 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
268 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
269 break;
270 case USBPHY_INTERFACE_MODE_ULPI:
271 portsc = PORTSC_PTS(PTS_ULPI);
272 lpm = DEVLC_PTS(PTS_ULPI);
273 break;
274 case USBPHY_INTERFACE_MODE_SERIAL:
275 portsc = PORTSC_PTS(PTS_SERIAL);
276 lpm = DEVLC_PTS(PTS_SERIAL);
277 sts = 1;
278 break;
279 case USBPHY_INTERFACE_MODE_HSIC:
280 portsc = PORTSC_PTS(PTS_HSIC);
281 lpm = DEVLC_PTS(PTS_HSIC);
282 break;
283 default:
284 return;
285 }
286
287 if (ci->hw_bank.lpm) {
288 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800289 if (sts)
290 hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300291 } else {
292 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
Chris Ruehl3b5d3e62014-01-10 13:51:29 +0800293 if (sts)
294 hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
Michael Grzeschik40dcd0e2013-06-13 17:59:56 +0300295 }
296}
297
Alexander Shishkine443b332012-05-11 17:25:46 +0300298/**
Peter Chend03cccf2014-04-23 15:56:37 +0800299 * ci_usb_phy_init: initialize phy according to different phy type
300 * @ci: the controller
301 *
302 * This function returns an error code if usb_phy_init has failed
303 */
304static int ci_usb_phy_init(struct ci_hdrc *ci)
305{
306 int ret;
307
308 switch (ci->platdata->phy_mode) {
309 case USBPHY_INTERFACE_MODE_UTMI:
310 case USBPHY_INTERFACE_MODE_UTMIW:
311 case USBPHY_INTERFACE_MODE_HSIC:
312 ret = usb_phy_init(ci->transceiver);
313 if (ret)
314 return ret;
315 hw_phymode_configure(ci);
316 break;
317 case USBPHY_INTERFACE_MODE_ULPI:
318 case USBPHY_INTERFACE_MODE_SERIAL:
319 hw_phymode_configure(ci);
320 ret = usb_phy_init(ci->transceiver);
321 if (ret)
322 return ret;
323 break;
324 default:
325 ret = usb_phy_init(ci->transceiver);
326 }
327
328 return ret;
329}
330
331/**
Alexander Shishkine443b332012-05-11 17:25:46 +0300332 * hw_device_reset: resets chip (execute without interruption)
333 * @ci: the controller
334 *
335 * This function returns an error code
336 */
Alexander Shishkin8e229782013-06-24 14:46:36 +0300337int hw_device_reset(struct ci_hdrc *ci, u32 mode)
Alexander Shishkine443b332012-05-11 17:25:46 +0300338{
339 /* should flush & stop before reset */
340 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
341 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
342
343 hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
344 while (hw_read(ci, OP_USBCMD, USBCMD_RST))
345 udelay(10); /* not RTOS friendly */
346
Richard Zhao77c44002012-06-29 17:48:53 +0800347 if (ci->platdata->notify_event)
348 ci->platdata->notify_event(ci,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300349 CI_HDRC_CONTROLLER_RESET_EVENT);
Alexander Shishkine443b332012-05-11 17:25:46 +0300350
Alexander Shishkin8e229782013-06-24 14:46:36 +0300351 if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
Alexander Shishkin758fc982012-05-11 17:25:53 +0300352 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
Alexander Shishkine443b332012-05-11 17:25:46 +0300353
Michael Grzeschik4f6743d2014-02-19 13:41:43 +0800354 if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
355 if (ci->hw_bank.lpm)
356 hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
357 else
358 hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
359 }
360
Alexander Shishkine443b332012-05-11 17:25:46 +0300361 /* USBMODE should be configured step by step */
362 hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300363 hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
Alexander Shishkine443b332012-05-11 17:25:46 +0300364 /* HW >= 2.3 */
365 hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
366
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300367 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
368 pr_err("cannot enter in %s mode", ci_role(ci)->name);
Alexander Shishkine443b332012-05-11 17:25:46 +0300369 pr_err("lpm = %i", ci->hw_bank.lpm);
370 return -ENODEV;
371 }
372
373 return 0;
374}
375
Peter Chen22fa8442013-08-14 12:44:12 +0300376/**
377 * hw_wait_reg: wait the register value
378 *
379 * Sometimes, it needs to wait register value before going on.
380 * Eg, when switch to device mode, the vbus value should be lower
381 * than OTGSC_BSV before connects to host.
382 *
383 * @ci: the controller
384 * @reg: register index
385 * @mask: mast bit
386 * @value: the bit value to wait
387 * @timeout_ms: timeout in millisecond
388 *
389 * This function returns an error code if timeout
390 */
391int hw_wait_reg(struct ci_hdrc *ci, enum ci_hw_regs reg, u32 mask,
392 u32 value, unsigned int timeout_ms)
393{
394 unsigned long elapse = jiffies + msecs_to_jiffies(timeout_ms);
395
396 while (hw_read(ci, reg, mask) != value) {
397 if (time_after(jiffies, elapse)) {
398 dev_err(ci->dev, "timeout waiting for %08x in %d\n",
399 mask, reg);
400 return -ETIMEDOUT;
401 }
402 msleep(20);
403 }
404
405 return 0;
406}
407
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300408static irqreturn_t ci_irq(int irq, void *data)
409{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300410 struct ci_hdrc *ci = data;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300411 irqreturn_t ret = IRQ_NONE;
Richard Zhaob183c192012-09-12 14:58:11 +0300412 u32 otgsc = 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300413
Li Jun4dcf7202014-04-23 15:56:50 +0800414 if (ci->is_otg) {
Li Jun0c33bf72014-04-23 15:56:38 +0800415 otgsc = hw_read_otgsc(ci, ~0);
Li Jun4dcf7202014-04-23 15:56:50 +0800416 if (ci_otg_is_fsm_mode(ci)) {
417 ret = ci_otg_fsm_irq(ci);
418 if (ret == IRQ_HANDLED)
419 return ret;
420 }
421 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300422
Peter Chena107f8c2013-08-14 12:44:11 +0300423 /*
424 * Handle id change interrupt, it indicates device/host function
425 * switch.
426 */
427 if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
428 ci->id_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800429 /* Clear ID change irq status */
430 hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
Richard Zhaob183c192012-09-12 14:58:11 +0300431 disable_irq_nosync(ci->irq);
432 queue_work(ci->wq, &ci->work);
Peter Chena107f8c2013-08-14 12:44:11 +0300433 return IRQ_HANDLED;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300434 }
435
Peter Chena107f8c2013-08-14 12:44:11 +0300436 /*
437 * Handle vbus change interrupt, it indicates device connection
438 * and disconnection events.
439 */
440 if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
441 ci->b_sess_valid_event = true;
Li Jun0c33bf72014-04-23 15:56:38 +0800442 /* Clear BSV irq */
443 hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
Peter Chena107f8c2013-08-14 12:44:11 +0300444 disable_irq_nosync(ci->irq);
445 queue_work(ci->wq, &ci->work);
446 return IRQ_HANDLED;
447 }
448
449 /* Handle device/host interrupt */
450 if (ci->role != CI_ROLE_END)
451 ret = ci_role(ci)->irq(ci);
452
Richard Zhaob183c192012-09-12 14:58:11 +0300453 return ret;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300454}
455
Peter Chen1542d9c2013-08-14 12:44:03 +0300456static int ci_get_platdata(struct device *dev,
457 struct ci_hdrc_platform_data *platdata)
458{
Peter Chenc22600c2013-09-17 12:37:22 +0800459 if (!platdata->phy_mode)
460 platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
461
462 if (!platdata->dr_mode)
463 platdata->dr_mode = of_usb_get_dr_mode(dev->of_node);
464
465 if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
466 platdata->dr_mode = USB_DR_MODE_OTG;
467
Peter Chenc2ec3a72013-10-30 09:19:29 +0800468 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
469 /* Get the vbus regulator */
470 platdata->reg_vbus = devm_regulator_get(dev, "vbus");
471 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
472 return -EPROBE_DEFER;
473 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
474 /* no vbus regualator is needed */
475 platdata->reg_vbus = NULL;
476 } else if (IS_ERR(platdata->reg_vbus)) {
477 dev_err(dev, "Getting regulator error: %ld\n",
478 PTR_ERR(platdata->reg_vbus));
479 return PTR_ERR(platdata->reg_vbus);
480 }
481 }
482
Michael Grzeschik4f6743d2014-02-19 13:41:43 +0800483 if (of_usb_get_maximum_speed(dev->of_node) == USB_SPEED_FULL)
484 platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
485
Peter Chen1542d9c2013-08-14 12:44:03 +0300486 return 0;
487}
488
Richard Zhaofe6e1252012-07-07 22:56:42 +0800489static DEFINE_IDA(ci_ida);
490
Alexander Shishkin8e229782013-06-24 14:46:36 +0300491struct platform_device *ci_hdrc_add_device(struct device *dev,
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800492 struct resource *res, int nres,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300493 struct ci_hdrc_platform_data *platdata)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800494{
495 struct platform_device *pdev;
Richard Zhaofe6e1252012-07-07 22:56:42 +0800496 int id, ret;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800497
Peter Chen1542d9c2013-08-14 12:44:03 +0300498 ret = ci_get_platdata(dev, platdata);
499 if (ret)
500 return ERR_PTR(ret);
501
Richard Zhaofe6e1252012-07-07 22:56:42 +0800502 id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
503 if (id < 0)
504 return ERR_PTR(id);
505
506 pdev = platform_device_alloc("ci_hdrc", id);
507 if (!pdev) {
508 ret = -ENOMEM;
509 goto put_id;
510 }
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800511
512 pdev->dev.parent = dev;
513 pdev->dev.dma_mask = dev->dma_mask;
514 pdev->dev.dma_parms = dev->dma_parms;
515 dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
516
517 ret = platform_device_add_resources(pdev, res, nres);
518 if (ret)
519 goto err;
520
521 ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
522 if (ret)
523 goto err;
524
525 ret = platform_device_add(pdev);
526 if (ret)
527 goto err;
528
529 return pdev;
530
531err:
532 platform_device_put(pdev);
Richard Zhaofe6e1252012-07-07 22:56:42 +0800533put_id:
534 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800535 return ERR_PTR(ret);
536}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300537EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800538
Alexander Shishkin8e229782013-06-24 14:46:36 +0300539void ci_hdrc_remove_device(struct platform_device *pdev)
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800540{
Lothar Waßmann98c35532012-11-22 10:11:25 +0100541 int id = pdev->id;
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800542 platform_device_unregister(pdev);
Lothar Waßmann98c35532012-11-22 10:11:25 +0100543 ida_simple_remove(&ci_ida, id);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800544}
Alexander Shishkin8e229782013-06-24 14:46:36 +0300545EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
Richard Zhaocbc6dc22012-07-07 22:56:41 +0800546
Peter Chen3f124d22013-08-14 12:44:07 +0300547static inline void ci_role_destroy(struct ci_hdrc *ci)
548{
549 ci_hdrc_gadget_destroy(ci);
550 ci_hdrc_host_destroy(ci);
Peter Chencbec6bd2013-08-14 12:44:10 +0300551 if (ci->is_otg)
552 ci_hdrc_otg_destroy(ci);
Peter Chen3f124d22013-08-14 12:44:07 +0300553}
554
Peter Chen577b2322013-08-14 12:44:08 +0300555static void ci_get_otg_capable(struct ci_hdrc *ci)
556{
557 if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
558 ci->is_otg = false;
559 else
560 ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
561 DCCPARAMS_DC | DCCPARAMS_HC)
562 == (DCCPARAMS_DC | DCCPARAMS_HC));
Peter Chen90893b92014-04-23 15:56:41 +0800563 if (ci->is_otg)
Peter Chen577b2322013-08-14 12:44:08 +0300564 dev_dbg(ci->dev, "It is OTG capable controller\n");
565}
566
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500567static int ci_hdrc_probe(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +0300568{
569 struct device *dev = &pdev->dev;
Alexander Shishkin8e229782013-06-24 14:46:36 +0300570 struct ci_hdrc *ci;
Alexander Shishkine443b332012-05-11 17:25:46 +0300571 struct resource *res;
572 void __iomem *base;
573 int ret;
Sascha Hauer691962d2013-06-13 17:59:57 +0300574 enum usb_dr_mode dr_mode;
Alexander Shishkine443b332012-05-11 17:25:46 +0300575
Jingoo Hanfad56742014-02-19 13:41:42 +0800576 if (!dev_get_platdata(dev)) {
Alexander Shishkine443b332012-05-11 17:25:46 +0300577 dev_err(dev, "platform data missing\n");
578 return -ENODEV;
579 }
580
581 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbi19290812013-03-30 02:46:27 +0200582 base = devm_ioremap_resource(dev, res);
583 if (IS_ERR(base))
584 return PTR_ERR(base);
Alexander Shishkine443b332012-05-11 17:25:46 +0300585
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300586 ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
587 if (!ci) {
588 dev_err(dev, "can't allocate device\n");
589 return -ENOMEM;
Alexander Shishkine443b332012-05-11 17:25:46 +0300590 }
591
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300592 ci->dev = dev;
Jingoo Hanfad56742014-02-19 13:41:42 +0800593 ci->platdata = dev_get_platdata(dev);
Peter Chened8f8312014-01-10 13:51:27 +0800594 ci->imx28_write_fix = !!(ci->platdata->flags &
595 CI_HDRC_IMX28_WRITE_FIX);
Alexander Shishkine443b332012-05-11 17:25:46 +0300596
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300597 ret = hw_device_init(ci, base);
598 if (ret < 0) {
599 dev_err(dev, "can't initialize hardware\n");
600 return -ENODEV;
601 }
602
Peter Chenc859aa652014-02-19 13:41:40 +0800603 if (ci->platdata->phy)
604 ci->transceiver = ci->platdata->phy;
605 else
606 ci->transceiver = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
607
608 if (IS_ERR(ci->transceiver)) {
609 ret = PTR_ERR(ci->transceiver);
610 /*
611 * if -ENXIO is returned, it means PHY layer wasn't
612 * enabled, so it makes no sense to return -EPROBE_DEFER
613 * in that case, since no PHY driver will ever probe.
614 */
615 if (ret == -ENXIO)
616 return ret;
617
618 dev_err(dev, "no usb2 phy configured\n");
619 return -EPROBE_DEFER;
620 }
621
Peter Chend03cccf2014-04-23 15:56:37 +0800622 ret = ci_usb_phy_init(ci);
Peter Chen74475ed2013-09-24 12:47:53 +0800623 if (ret) {
624 dev_err(dev, "unable to init phy: %d\n", ret);
625 return ret;
Peter Chen90893b92014-04-23 15:56:41 +0800626 } else {
627 /*
628 * The delay to sync PHY's status, the maximum delay is
629 * 2ms since the otgsc uses 1ms timer to debounce the
630 * PHY's input
631 */
632 usleep_range(2000, 2500);
Peter Chen74475ed2013-09-24 12:47:53 +0800633 }
634
Alexander Shishkineb70e5a2012-05-11 17:25:54 +0300635 ci->hw_bank.phys = res->start;
636
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300637 ci->irq = platform_get_irq(pdev, 0);
638 if (ci->irq < 0) {
639 dev_err(dev, "missing IRQ\n");
Fabio Estevam42d18212014-02-19 13:41:44 +0800640 ret = ci->irq;
Peter Chenc859aa652014-02-19 13:41:40 +0800641 goto deinit_phy;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300642 }
643
Peter Chen577b2322013-08-14 12:44:08 +0300644 ci_get_otg_capable(ci);
645
Sascha Hauer691962d2013-06-13 17:59:57 +0300646 dr_mode = ci->platdata->dr_mode;
647 /* initialize role(s) before the interrupt is requested */
648 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
649 ret = ci_hdrc_host_init(ci);
650 if (ret)
651 dev_info(dev, "doesn't support host\n");
652 }
653
654 if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
655 ret = ci_hdrc_gadget_init(ci);
656 if (ret)
657 dev_info(dev, "doesn't support gadget\n");
658 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300659
660 if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
661 dev_err(dev, "no supported roles\n");
Peter Chen74475ed2013-09-24 12:47:53 +0800662 ret = -ENODEV;
Peter Chenc859aa652014-02-19 13:41:40 +0800663 goto deinit_phy;
Peter Chencbec6bd2013-08-14 12:44:10 +0300664 }
665
666 if (ci->is_otg) {
Peter Chen90893b92014-04-23 15:56:41 +0800667 /* Disable and clear all OTG irq */
668 hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
669 OTGSC_INT_STATUS_BITS);
Peter Chencbec6bd2013-08-14 12:44:10 +0300670 ret = ci_hdrc_otg_init(ci);
671 if (ret) {
672 dev_err(dev, "init otg fails, ret = %d\n", ret);
673 goto stop;
674 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300675 }
676
677 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
Peter Chen577b2322013-08-14 12:44:08 +0300678 if (ci->is_otg) {
Peter Chen577b2322013-08-14 12:44:08 +0300679 ci->role = ci_otg_role(ci);
Li Jun0c33bf72014-04-23 15:56:38 +0800680 /* Enable ID change irq */
681 hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
Peter Chen577b2322013-08-14 12:44:08 +0300682 } else {
683 /*
684 * If the controller is not OTG capable, but support
685 * role switch, the defalt role is gadget, and the
686 * user can switch it through debugfs.
687 */
688 ci->role = CI_ROLE_GADGET;
689 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300690 } else {
691 ci->role = ci->roles[CI_ROLE_HOST]
692 ? CI_ROLE_HOST
693 : CI_ROLE_GADGET;
694 }
695
Peter Chen5a1e1452013-12-05 15:20:50 +0800696 /* only update vbus status for peripheral */
697 if (ci->role == CI_ROLE_GADGET)
698 ci_handle_vbus_change(ci);
699
Li Jun4dcf7202014-04-23 15:56:50 +0800700 if (!ci_otg_is_fsm_mode(ci)) {
701 ret = ci_role_start(ci, ci->role);
702 if (ret) {
703 dev_err(dev, "can't start %s role\n",
704 ci_role(ci)->name);
705 goto stop;
706 }
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300707 }
708
709 platform_set_drvdata(pdev, ci);
Richard Zhao77c44002012-06-29 17:48:53 +0800710 ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300711 ci);
712 if (ret)
713 goto stop;
714
Li Jun4dcf7202014-04-23 15:56:50 +0800715 if (ci_otg_is_fsm_mode(ci))
716 ci_hdrc_otg_fsm_start(ci);
717
Alexander Shishkinadf0f732013-03-30 12:53:53 +0200718 ret = dbg_create_files(ci);
719 if (!ret)
720 return 0;
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300721
Alexander Shishkinadf0f732013-03-30 12:53:53 +0200722 free_irq(ci->irq, ci);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300723stop:
Peter Chen3f124d22013-08-14 12:44:07 +0300724 ci_role_destroy(ci);
Peter Chenc859aa652014-02-19 13:41:40 +0800725deinit_phy:
726 usb_phy_shutdown(ci->transceiver);
Alexander Shishkine443b332012-05-11 17:25:46 +0300727
728 return ret;
729}
730
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500731static int ci_hdrc_remove(struct platform_device *pdev)
Alexander Shishkine443b332012-05-11 17:25:46 +0300732{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300733 struct ci_hdrc *ci = platform_get_drvdata(pdev);
Alexander Shishkine443b332012-05-11 17:25:46 +0300734
Alexander Shishkinadf0f732013-03-30 12:53:53 +0200735 dbg_remove_files(ci);
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300736 free_irq(ci->irq, ci);
Peter Chen3f124d22013-08-14 12:44:07 +0300737 ci_role_destroy(ci);
Peter Chen864cf942013-09-24 12:47:55 +0800738 ci_hdrc_enter_lpm(ci, true);
Peter Chenc859aa652014-02-19 13:41:40 +0800739 usb_phy_shutdown(ci->transceiver);
740 kfree(ci->hw_bank.regmap);
Alexander Shishkine443b332012-05-11 17:25:46 +0300741
742 return 0;
743}
744
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300745static struct platform_driver ci_hdrc_driver = {
746 .probe = ci_hdrc_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500747 .remove = ci_hdrc_remove,
Alexander Shishkine443b332012-05-11 17:25:46 +0300748 .driver = {
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300749 .name = "ci_hdrc",
Alexander Shiyan7cf2f862014-04-23 15:56:42 +0800750 .owner = THIS_MODULE,
Alexander Shishkine443b332012-05-11 17:25:46 +0300751 },
752};
753
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300754module_platform_driver(ci_hdrc_driver);
Alexander Shishkine443b332012-05-11 17:25:46 +0300755
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300756MODULE_ALIAS("platform:ci_hdrc");
Alexander Shishkine443b332012-05-11 17:25:46 +0300757MODULE_LICENSE("GPL v2");
758MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
Alexander Shishkin5f36e232012-05-11 17:25:47 +0300759MODULE_DESCRIPTION("ChipIdea HDRC Driver");