blob: 7f3c73b967ce6faf804ee1f29b82acbdde1f59f1 [file] [log] [blame]
Li Yang0807c502011-04-18 22:01:59 +02001/*
2 * Copyright (C) 2007,2008 Freescale semiconductor, Inc.
3 *
4 * Author: Li Yang <LeoLi@freescale.com>
5 * Jerry Huang <Chang-Ming.Huang@freescale.com>
6 *
7 * Initialization based on code from Shlomi Gridish.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/proc_fs.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/io.h>
33#include <linux/timer.h>
34#include <linux/usb.h>
35#include <linux/device.h>
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
38#include <linux/workqueue.h>
39#include <linux/time.h>
40#include <linux/fsl_devices.h>
41#include <linux/platform_device.h>
42#include <linux/uaccess.h>
43
44#include <asm/unaligned.h>
45
Felipe Balbi94ae9842013-03-07 17:37:59 +020046#include "phy-fsl-usb.h"
Li Yang0807c502011-04-18 22:01:59 +020047
48#define DRIVER_VERSION "Rev. 1.55"
49#define DRIVER_AUTHOR "Jerry Huang/Li Yang"
50#define DRIVER_DESC "Freescale USB OTG Transceiver Driver"
51#define DRIVER_INFO DRIVER_DESC " " DRIVER_VERSION
52
53static const char driver_name[] = "fsl-usb2-otg";
54
55const pm_message_t otg_suspend_state = {
56 .event = 1,
57};
58
59#define HA_DATA_PULSE
60
61static struct usb_dr_mmap *usb_dr_regs;
62static struct fsl_otg *fsl_otg_dev;
63static int srp_wait_done;
64
65/* FSM timers */
66struct fsl_otg_timer *a_wait_vrise_tmr, *a_wait_bcon_tmr, *a_aidl_bdis_tmr,
67 *b_ase0_brst_tmr, *b_se0_srp_tmr;
68
69/* Driver specific timers */
70struct fsl_otg_timer *b_data_pulse_tmr, *b_vbus_pulse_tmr, *b_srp_fail_tmr,
71 *b_srp_wait_tmr, *a_wait_enum_tmr;
72
73static struct list_head active_timers;
74
75static struct fsl_otg_config fsl_otg_initdata = {
76 .otg_port = 1,
77};
78
79#ifdef CONFIG_PPC32
80static u32 _fsl_readl_be(const unsigned __iomem *p)
81{
82 return in_be32(p);
83}
84
85static u32 _fsl_readl_le(const unsigned __iomem *p)
86{
87 return in_le32(p);
88}
89
90static void _fsl_writel_be(u32 v, unsigned __iomem *p)
91{
92 out_be32(p, v);
93}
94
95static void _fsl_writel_le(u32 v, unsigned __iomem *p)
96{
97 out_le32(p, v);
98}
99
100static u32 (*_fsl_readl)(const unsigned __iomem *p);
101static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
102
103#define fsl_readl(p) (*_fsl_readl)((p))
104#define fsl_writel(v, p) (*_fsl_writel)((v), (p))
105
106#else
107#define fsl_readl(addr) readl(addr)
108#define fsl_writel(val, addr) writel(val, addr)
109#endif /* CONFIG_PPC32 */
110
111/* Routines to access transceiver ULPI registers */
112u8 view_ulpi(u8 addr)
113{
114 u32 temp;
115
116 temp = 0x40000000 | (addr << 16);
117 fsl_writel(temp, &usb_dr_regs->ulpiview);
118 udelay(1000);
119 while (temp & 0x40)
120 temp = fsl_readl(&usb_dr_regs->ulpiview);
121 return (le32_to_cpu(temp) & 0x0000ff00) >> 8;
122}
123
124int write_ulpi(u8 addr, u8 data)
125{
126 u32 temp;
127
128 temp = 0x60000000 | (addr << 16) | data;
129 fsl_writel(temp, &usb_dr_regs->ulpiview);
130 return 0;
131}
132
133/* -------------------------------------------------------------*/
134/* Operations that will be called from OTG Finite State Machine */
135
136/* Charge vbus for vbus pulsing in SRP */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900137void fsl_otg_chrg_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200138{
139 u32 tmp;
140
141 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
142
143 if (on)
144 /* stop discharging, start charging */
145 tmp = (tmp & ~OTGSC_CTRL_VBUS_DISCHARGE) |
146 OTGSC_CTRL_VBUS_CHARGE;
147 else
148 /* stop charging */
149 tmp &= ~OTGSC_CTRL_VBUS_CHARGE;
150
151 fsl_writel(tmp, &usb_dr_regs->otgsc);
152}
153
154/* Discharge vbus through a resistor to ground */
155void fsl_otg_dischrg_vbus(int on)
156{
157 u32 tmp;
158
159 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
160
161 if (on)
162 /* stop charging, start discharging */
163 tmp = (tmp & ~OTGSC_CTRL_VBUS_CHARGE) |
164 OTGSC_CTRL_VBUS_DISCHARGE;
165 else
166 /* stop discharging */
167 tmp &= ~OTGSC_CTRL_VBUS_DISCHARGE;
168
169 fsl_writel(tmp, &usb_dr_regs->otgsc);
170}
171
172/* A-device driver vbus, controlled through PP bit in PORTSC */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900173void fsl_otg_drv_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200174{
175 u32 tmp;
176
177 if (on) {
178 tmp = fsl_readl(&usb_dr_regs->portsc) & ~PORTSC_W1C_BITS;
179 fsl_writel(tmp | PORTSC_PORT_POWER, &usb_dr_regs->portsc);
180 } else {
181 tmp = fsl_readl(&usb_dr_regs->portsc) &
182 ~PORTSC_W1C_BITS & ~PORTSC_PORT_POWER;
183 fsl_writel(tmp, &usb_dr_regs->portsc);
184 }
185}
186
187/*
188 * Pull-up D+, signalling connect by periperal. Also used in
189 * data-line pulsing in SRP
190 */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900191void fsl_otg_loc_conn(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200192{
193 u32 tmp;
194
195 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
196
197 if (on)
198 tmp |= OTGSC_CTRL_DATA_PULSING;
199 else
200 tmp &= ~OTGSC_CTRL_DATA_PULSING;
201
202 fsl_writel(tmp, &usb_dr_regs->otgsc);
203}
204
205/*
206 * Generate SOF by host. This is controlled through suspend/resume the
207 * port. In host mode, controller will automatically send SOF.
208 * Suspend will block the data on the port.
209 */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900210void fsl_otg_loc_sof(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200211{
212 u32 tmp;
213
214 tmp = fsl_readl(&fsl_otg_dev->dr_mem_map->portsc) & ~PORTSC_W1C_BITS;
215 if (on)
216 tmp |= PORTSC_PORT_FORCE_RESUME;
217 else
218 tmp |= PORTSC_PORT_SUSPEND;
219
220 fsl_writel(tmp, &fsl_otg_dev->dr_mem_map->portsc);
221
222}
223
224/* Start SRP pulsing by data-line pulsing, followed with v-bus pulsing. */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900225void fsl_otg_start_pulse(struct otg_fsm *fsm)
Li Yang0807c502011-04-18 22:01:59 +0200226{
227 u32 tmp;
228
229 srp_wait_done = 0;
230#ifdef HA_DATA_PULSE
231 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
232 tmp |= OTGSC_HA_DATA_PULSE;
233 fsl_writel(tmp, &usb_dr_regs->otgsc);
234#else
235 fsl_otg_loc_conn(1);
236#endif
237
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900238 fsl_otg_add_timer(fsm, b_data_pulse_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200239}
240
241void b_data_pulse_end(unsigned long foo)
242{
243#ifdef HA_DATA_PULSE
244#else
245 fsl_otg_loc_conn(0);
246#endif
247
248 /* Do VBUS pulse after data pulse */
249 fsl_otg_pulse_vbus();
250}
251
252void fsl_otg_pulse_vbus(void)
253{
254 srp_wait_done = 0;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900255 fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 1);
Li Yang0807c502011-04-18 22:01:59 +0200256 /* start the timer to end vbus charge */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900257 fsl_otg_add_timer(&fsl_otg_dev->fsm, b_vbus_pulse_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200258}
259
260void b_vbus_pulse_end(unsigned long foo)
261{
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900262 fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 0);
Li Yang0807c502011-04-18 22:01:59 +0200263
264 /*
265 * As USB3300 using the same a_sess_vld and b_sess_vld voltage
266 * we need to discharge the bus for a while to distinguish
267 * residual voltage of vbus pulsing and A device pull up
268 */
269 fsl_otg_dischrg_vbus(1);
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900270 fsl_otg_add_timer(&fsl_otg_dev->fsm, b_srp_wait_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200271}
272
273void b_srp_end(unsigned long foo)
274{
275 fsl_otg_dischrg_vbus(0);
276 srp_wait_done = 1;
277
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200278 if ((fsl_otg_dev->phy.state == OTG_STATE_B_SRP_INIT) &&
Li Yang0807c502011-04-18 22:01:59 +0200279 fsl_otg_dev->fsm.b_sess_vld)
280 fsl_otg_dev->fsm.b_srp_done = 1;
281}
282
283/*
284 * Workaround for a_host suspending too fast. When a_bus_req=0,
285 * a_host will start by SRP. It needs to set b_hnp_enable before
286 * actually suspending to start HNP
287 */
288void a_wait_enum(unsigned long foo)
289{
290 VDBG("a_wait_enum timeout\n");
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200291 if (!fsl_otg_dev->phy.otg->host->b_hnp_enable)
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900292 fsl_otg_add_timer(&fsl_otg_dev->fsm, a_wait_enum_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200293 else
294 otg_statemachine(&fsl_otg_dev->fsm);
295}
296
297/* The timeout callback function to set time out bit */
298void set_tmout(unsigned long indicator)
299{
300 *(int *)indicator = 1;
301}
302
303/* Initialize timers */
304int fsl_otg_init_timers(struct otg_fsm *fsm)
305{
306 /* FSM used timers */
307 a_wait_vrise_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_VRISE,
308 (unsigned long)&fsm->a_wait_vrise_tmout);
309 if (!a_wait_vrise_tmr)
310 return -ENOMEM;
311
312 a_wait_bcon_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_BCON,
313 (unsigned long)&fsm->a_wait_bcon_tmout);
314 if (!a_wait_bcon_tmr)
315 return -ENOMEM;
316
317 a_aidl_bdis_tmr = otg_timer_initializer(&set_tmout, TA_AIDL_BDIS,
318 (unsigned long)&fsm->a_aidl_bdis_tmout);
319 if (!a_aidl_bdis_tmr)
320 return -ENOMEM;
321
322 b_ase0_brst_tmr = otg_timer_initializer(&set_tmout, TB_ASE0_BRST,
323 (unsigned long)&fsm->b_ase0_brst_tmout);
324 if (!b_ase0_brst_tmr)
325 return -ENOMEM;
326
327 b_se0_srp_tmr = otg_timer_initializer(&set_tmout, TB_SE0_SRP,
328 (unsigned long)&fsm->b_se0_srp);
329 if (!b_se0_srp_tmr)
330 return -ENOMEM;
331
332 b_srp_fail_tmr = otg_timer_initializer(&set_tmout, TB_SRP_FAIL,
333 (unsigned long)&fsm->b_srp_done);
334 if (!b_srp_fail_tmr)
335 return -ENOMEM;
336
337 a_wait_enum_tmr = otg_timer_initializer(&a_wait_enum, 10,
338 (unsigned long)&fsm);
339 if (!a_wait_enum_tmr)
340 return -ENOMEM;
341
342 /* device driver used timers */
343 b_srp_wait_tmr = otg_timer_initializer(&b_srp_end, TB_SRP_WAIT, 0);
344 if (!b_srp_wait_tmr)
345 return -ENOMEM;
346
347 b_data_pulse_tmr = otg_timer_initializer(&b_data_pulse_end,
348 TB_DATA_PLS, 0);
349 if (!b_data_pulse_tmr)
350 return -ENOMEM;
351
352 b_vbus_pulse_tmr = otg_timer_initializer(&b_vbus_pulse_end,
353 TB_VBUS_PLS, 0);
354 if (!b_vbus_pulse_tmr)
355 return -ENOMEM;
356
357 return 0;
358}
359
360/* Uninitialize timers */
361void fsl_otg_uninit_timers(void)
362{
363 /* FSM used timers */
Syam Sidhardhan5f717912013-03-06 01:04:50 +0530364 kfree(a_wait_vrise_tmr);
365 kfree(a_wait_bcon_tmr);
366 kfree(a_aidl_bdis_tmr);
367 kfree(b_ase0_brst_tmr);
368 kfree(b_se0_srp_tmr);
369 kfree(b_srp_fail_tmr);
370 kfree(a_wait_enum_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200371
372 /* device driver used timers */
Syam Sidhardhan5f717912013-03-06 01:04:50 +0530373 kfree(b_srp_wait_tmr);
374 kfree(b_data_pulse_tmr);
375 kfree(b_vbus_pulse_tmr);
Li Yang0807c502011-04-18 22:01:59 +0200376}
377
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900378static struct fsl_otg_timer *fsl_otg_get_timer(enum otg_fsm_timer t)
379{
380 struct fsl_otg_timer *timer;
381
382 /* REVISIT: use array of pointers to timers instead */
383 switch (t) {
384 case A_WAIT_VRISE:
385 timer = a_wait_vrise_tmr;
386 break;
387 case A_WAIT_BCON:
388 timer = a_wait_vrise_tmr;
389 break;
390 case A_AIDL_BDIS:
391 timer = a_wait_vrise_tmr;
392 break;
393 case B_ASE0_BRST:
394 timer = a_wait_vrise_tmr;
395 break;
396 case B_SE0_SRP:
397 timer = a_wait_vrise_tmr;
398 break;
399 case B_SRP_FAIL:
400 timer = a_wait_vrise_tmr;
401 break;
402 case A_WAIT_ENUM:
403 timer = a_wait_vrise_tmr;
404 break;
405 default:
406 timer = NULL;
407 }
408
409 return timer;
410}
411
Li Yang0807c502011-04-18 22:01:59 +0200412/* Add timer to timer list */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900413void fsl_otg_add_timer(struct otg_fsm *fsm, void *gtimer)
Li Yang0807c502011-04-18 22:01:59 +0200414{
415 struct fsl_otg_timer *timer = gtimer;
416 struct fsl_otg_timer *tmp_timer;
417
418 /*
419 * Check if the timer is already in the active list,
420 * if so update timer count
421 */
422 list_for_each_entry(tmp_timer, &active_timers, list)
423 if (tmp_timer == timer) {
424 timer->count = timer->expires;
425 return;
426 }
427 timer->count = timer->expires;
428 list_add_tail(&timer->list, &active_timers);
429}
430
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900431static void fsl_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
432{
433 struct fsl_otg_timer *timer;
434
435 timer = fsl_otg_get_timer(t);
436 if (!timer)
437 return;
438
439 fsl_otg_add_timer(fsm, timer);
440}
441
Li Yang0807c502011-04-18 22:01:59 +0200442/* Remove timer from the timer list; clear timeout status */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900443void fsl_otg_del_timer(struct otg_fsm *fsm, void *gtimer)
Li Yang0807c502011-04-18 22:01:59 +0200444{
445 struct fsl_otg_timer *timer = gtimer;
446 struct fsl_otg_timer *tmp_timer, *del_tmp;
447
448 list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list)
449 if (tmp_timer == timer)
450 list_del(&timer->list);
451}
452
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900453static void fsl_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
454{
455 struct fsl_otg_timer *timer;
456
457 timer = fsl_otg_get_timer(t);
458 if (!timer)
459 return;
460
461 fsl_otg_del_timer(fsm, timer);
462}
463
Li Yang0807c502011-04-18 22:01:59 +0200464/*
465 * Reduce timer count by 1, and find timeout conditions.
466 * Called by fsl_otg 1ms timer interrupt
467 */
468int fsl_otg_tick_timer(void)
469{
470 struct fsl_otg_timer *tmp_timer, *del_tmp;
471 int expired = 0;
472
473 list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list) {
474 tmp_timer->count--;
475 /* check if timer expires */
476 if (!tmp_timer->count) {
477 list_del(&tmp_timer->list);
478 tmp_timer->function(tmp_timer->data);
479 expired = 1;
480 }
481 }
482
483 return expired;
484}
485
486/* Reset controller, not reset the bus */
487void otg_reset_controller(void)
488{
489 u32 command;
490
491 command = fsl_readl(&usb_dr_regs->usbcmd);
492 command |= (1 << 1);
493 fsl_writel(command, &usb_dr_regs->usbcmd);
494 while (fsl_readl(&usb_dr_regs->usbcmd) & (1 << 1))
495 ;
496}
497
498/* Call suspend/resume routines in host driver */
499int fsl_otg_start_host(struct otg_fsm *fsm, int on)
500{
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200501 struct usb_otg *otg = fsm->otg;
Li Yang0807c502011-04-18 22:01:59 +0200502 struct device *dev;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200503 struct fsl_otg *otg_dev = container_of(otg->phy, struct fsl_otg, phy);
Li Yang0807c502011-04-18 22:01:59 +0200504 u32 retval = 0;
505
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200506 if (!otg->host)
Li Yang0807c502011-04-18 22:01:59 +0200507 return -ENODEV;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200508 dev = otg->host->controller;
Li Yang0807c502011-04-18 22:01:59 +0200509
510 /*
511 * Update a_vbus_vld state as a_vbus_vld int is disabled
512 * in device mode
513 */
514 fsm->a_vbus_vld =
515 !!(fsl_readl(&usb_dr_regs->otgsc) & OTGSC_STS_A_VBUS_VALID);
516 if (on) {
517 /* start fsl usb host controller */
518 if (otg_dev->host_working)
519 goto end;
520 else {
521 otg_reset_controller();
522 VDBG("host on......\n");
523 if (dev->driver->pm && dev->driver->pm->resume) {
524 retval = dev->driver->pm->resume(dev);
525 if (fsm->id) {
526 /* default-b */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900527 fsl_otg_drv_vbus(fsm, 1);
Li Yang0807c502011-04-18 22:01:59 +0200528 /*
529 * Workaround: b_host can't driver
530 * vbus, but PP in PORTSC needs to
531 * be 1 for host to work.
532 * So we set drv_vbus bit in
533 * transceiver to 0 thru ULPI.
534 */
535 write_ulpi(0x0c, 0x20);
536 }
537 }
538
539 otg_dev->host_working = 1;
540 }
541 } else {
542 /* stop fsl usb host controller */
543 if (!otg_dev->host_working)
544 goto end;
545 else {
546 VDBG("host off......\n");
547 if (dev && dev->driver) {
548 if (dev->driver->pm && dev->driver->pm->suspend)
549 retval = dev->driver->pm->suspend(dev);
550 if (fsm->id)
551 /* default-b */
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900552 fsl_otg_drv_vbus(fsm, 0);
Li Yang0807c502011-04-18 22:01:59 +0200553 }
554 otg_dev->host_working = 0;
555 }
556 }
557end:
558 return retval;
559}
560
561/*
562 * Call suspend and resume function in udc driver
563 * to stop and start udc driver.
564 */
565int fsl_otg_start_gadget(struct otg_fsm *fsm, int on)
566{
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200567 struct usb_otg *otg = fsm->otg;
Li Yang0807c502011-04-18 22:01:59 +0200568 struct device *dev;
569
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200570 if (!otg->gadget || !otg->gadget->dev.parent)
Li Yang0807c502011-04-18 22:01:59 +0200571 return -ENODEV;
572
573 VDBG("gadget %s\n", on ? "on" : "off");
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200574 dev = otg->gadget->dev.parent;
Li Yang0807c502011-04-18 22:01:59 +0200575
576 if (on) {
577 if (dev->driver->resume)
578 dev->driver->resume(dev);
579 } else {
580 if (dev->driver->suspend)
581 dev->driver->suspend(dev, otg_suspend_state);
582 }
583
584 return 0;
585}
586
587/*
588 * Called by initialization code of host driver. Register host controller
589 * to the OTG. Suspend host for OTG role detection.
590 */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200591static int fsl_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Li Yang0807c502011-04-18 22:01:59 +0200592{
Wei Yongjun58add6c2012-09-10 12:42:31 +0800593 struct fsl_otg *otg_dev;
Li Yang0807c502011-04-18 22:01:59 +0200594
Wei Yongjun58add6c2012-09-10 12:42:31 +0800595 if (!otg)
596 return -ENODEV;
597
598 otg_dev = container_of(otg->phy, struct fsl_otg, phy);
599 if (otg_dev != fsl_otg_dev)
Li Yang0807c502011-04-18 22:01:59 +0200600 return -ENODEV;
601
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200602 otg->host = host;
Li Yang0807c502011-04-18 22:01:59 +0200603
604 otg_dev->fsm.a_bus_drop = 0;
605 otg_dev->fsm.a_bus_req = 1;
606
607 if (host) {
608 VDBG("host off......\n");
609
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200610 otg->host->otg_port = fsl_otg_initdata.otg_port;
611 otg->host->is_b_host = otg_dev->fsm.id;
Li Yang0807c502011-04-18 22:01:59 +0200612 /*
613 * must leave time for khubd to finish its thing
614 * before yanking the host driver out from under it,
615 * so suspend the host after a short delay.
616 */
617 otg_dev->host_working = 1;
618 schedule_delayed_work(&otg_dev->otg_event, 100);
619 return 0;
620 } else {
621 /* host driver going away */
622 if (!(fsl_readl(&otg_dev->dr_mem_map->otgsc) &
623 OTGSC_STS_USB_ID)) {
624 /* Mini-A cable connected */
625 struct otg_fsm *fsm = &otg_dev->fsm;
626
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200627 otg->phy->state = OTG_STATE_UNDEFINED;
Li Yang0807c502011-04-18 22:01:59 +0200628 fsm->protocol = PROTO_UNDEF;
629 }
630 }
631
632 otg_dev->host_working = 0;
633
634 otg_statemachine(&otg_dev->fsm);
635
636 return 0;
637}
638
639/* Called by initialization code of udc. Register udc to OTG. */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200640static int fsl_otg_set_peripheral(struct usb_otg *otg,
641 struct usb_gadget *gadget)
Li Yang0807c502011-04-18 22:01:59 +0200642{
Wei Yongjun58add6c2012-09-10 12:42:31 +0800643 struct fsl_otg *otg_dev;
Li Yang0807c502011-04-18 22:01:59 +0200644
Wei Yongjun58add6c2012-09-10 12:42:31 +0800645 if (!otg)
646 return -ENODEV;
647
648 otg_dev = container_of(otg->phy, struct fsl_otg, phy);
Li Yang0807c502011-04-18 22:01:59 +0200649 VDBG("otg_dev 0x%x\n", (int)otg_dev);
650 VDBG("fsl_otg_dev 0x%x\n", (int)fsl_otg_dev);
Wei Yongjun58add6c2012-09-10 12:42:31 +0800651 if (otg_dev != fsl_otg_dev)
Li Yang0807c502011-04-18 22:01:59 +0200652 return -ENODEV;
653
654 if (!gadget) {
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200655 if (!otg->default_a)
656 otg->gadget->ops->vbus_draw(otg->gadget, 0);
657 usb_gadget_vbus_disconnect(otg->gadget);
658 otg->gadget = 0;
Li Yang0807c502011-04-18 22:01:59 +0200659 otg_dev->fsm.b_bus_req = 0;
660 otg_statemachine(&otg_dev->fsm);
661 return 0;
662 }
663
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200664 otg->gadget = gadget;
665 otg->gadget->is_a_peripheral = !otg_dev->fsm.id;
Li Yang0807c502011-04-18 22:01:59 +0200666
667 otg_dev->fsm.b_bus_req = 1;
668
669 /* start the gadget right away if the ID pin says Mini-B */
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -0700670 pr_debug("ID pin=%d\n", otg_dev->fsm.id);
Li Yang0807c502011-04-18 22:01:59 +0200671 if (otg_dev->fsm.id == 1) {
672 fsl_otg_start_host(&otg_dev->fsm, 0);
673 otg_drv_vbus(&otg_dev->fsm, 0);
674 fsl_otg_start_gadget(&otg_dev->fsm, 1);
675 }
676
677 return 0;
678}
679
680/* Set OTG port power, only for B-device */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200681static int fsl_otg_set_power(struct usb_phy *phy, unsigned mA)
Li Yang0807c502011-04-18 22:01:59 +0200682{
683 if (!fsl_otg_dev)
684 return -ENODEV;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200685 if (phy->state == OTG_STATE_B_PERIPHERAL)
Li Yang0807c502011-04-18 22:01:59 +0200686 pr_info("FSL OTG: Draw %d mA\n", mA);
687
688 return 0;
689}
690
691/*
692 * Delayed pin detect interrupt processing.
693 *
694 * When the Mini-A cable is disconnected from the board,
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800695 * the pin-detect interrupt happens before the disconnect
Li Yang0807c502011-04-18 22:01:59 +0200696 * interrupts for the connected device(s). In order to
697 * process the disconnect interrupt(s) prior to switching
698 * roles, the pin-detect interrupts are delayed, and handled
699 * by this routine.
700 */
701static void fsl_otg_event(struct work_struct *work)
702{
703 struct fsl_otg *og = container_of(work, struct fsl_otg, otg_event.work);
704 struct otg_fsm *fsm = &og->fsm;
705
706 if (fsm->id) { /* switch to gadget */
707 fsl_otg_start_host(fsm, 0);
708 otg_drv_vbus(fsm, 0);
709 fsl_otg_start_gadget(fsm, 1);
710 }
711}
712
713/* B-device start SRP */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200714static int fsl_otg_start_srp(struct usb_otg *otg)
Li Yang0807c502011-04-18 22:01:59 +0200715{
Wei Yongjun58add6c2012-09-10 12:42:31 +0800716 struct fsl_otg *otg_dev;
Li Yang0807c502011-04-18 22:01:59 +0200717
Wei Yongjun58add6c2012-09-10 12:42:31 +0800718 if (!otg || otg->phy->state != OTG_STATE_B_IDLE)
719 return -ENODEV;
720
721 otg_dev = container_of(otg->phy, struct fsl_otg, phy);
722 if (otg_dev != fsl_otg_dev)
Li Yang0807c502011-04-18 22:01:59 +0200723 return -ENODEV;
724
725 otg_dev->fsm.b_bus_req = 1;
726 otg_statemachine(&otg_dev->fsm);
727
728 return 0;
729}
730
731/* A_host suspend will call this function to start hnp */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200732static int fsl_otg_start_hnp(struct usb_otg *otg)
Li Yang0807c502011-04-18 22:01:59 +0200733{
Wei Yongjun58add6c2012-09-10 12:42:31 +0800734 struct fsl_otg *otg_dev;
Li Yang0807c502011-04-18 22:01:59 +0200735
Wei Yongjun58add6c2012-09-10 12:42:31 +0800736 if (!otg)
737 return -ENODEV;
738
739 otg_dev = container_of(otg->phy, struct fsl_otg, phy);
740 if (otg_dev != fsl_otg_dev)
Li Yang0807c502011-04-18 22:01:59 +0200741 return -ENODEV;
742
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -0700743 pr_debug("start_hnp...\n");
Li Yang0807c502011-04-18 22:01:59 +0200744
745 /* clear a_bus_req to enter a_suspend state */
746 otg_dev->fsm.a_bus_req = 0;
747 otg_statemachine(&otg_dev->fsm);
748
749 return 0;
750}
751
752/*
753 * Interrupt handler. OTG/host/peripheral share the same int line.
754 * OTG driver clears OTGSC interrupts and leaves USB interrupts
755 * intact. It needs to have knowledge of some USB interrupts
756 * such as port change.
757 */
758irqreturn_t fsl_otg_isr(int irq, void *dev_id)
759{
760 struct otg_fsm *fsm = &((struct fsl_otg *)dev_id)->fsm;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200761 struct usb_otg *otg = ((struct fsl_otg *)dev_id)->phy.otg;
Li Yang0807c502011-04-18 22:01:59 +0200762 u32 otg_int_src, otg_sc;
763
764 otg_sc = fsl_readl(&usb_dr_regs->otgsc);
765 otg_int_src = otg_sc & OTGSC_INTSTS_MASK & (otg_sc >> 8);
766
767 /* Only clear otg interrupts */
768 fsl_writel(otg_sc, &usb_dr_regs->otgsc);
769
770 /*FIXME: ID change not generate when init to 0 */
771 fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
772 otg->default_a = (fsm->id == 0);
773
774 /* process OTG interrupts */
775 if (otg_int_src) {
776 if (otg_int_src & OTGSC_INTSTS_USB_ID) {
777 fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
778 otg->default_a = (fsm->id == 0);
779 /* clear conn information */
780 if (fsm->id)
781 fsm->b_conn = 0;
782 else
783 fsm->a_conn = 0;
784
785 if (otg->host)
786 otg->host->is_b_host = fsm->id;
787 if (otg->gadget)
788 otg->gadget->is_a_peripheral = !fsm->id;
789 VDBG("ID int (ID is %d)\n", fsm->id);
790
791 if (fsm->id) { /* switch to gadget */
792 schedule_delayed_work(
793 &((struct fsl_otg *)dev_id)->otg_event,
794 100);
795 } else { /* switch to host */
796 cancel_delayed_work(&
797 ((struct fsl_otg *)dev_id)->
798 otg_event);
799 fsl_otg_start_gadget(fsm, 0);
800 otg_drv_vbus(fsm, 1);
801 fsl_otg_start_host(fsm, 1);
802 }
803 return IRQ_HANDLED;
804 }
805 }
806 return IRQ_NONE;
807}
808
809static struct otg_fsm_ops fsl_otg_ops = {
810 .chrg_vbus = fsl_otg_chrg_vbus,
811 .drv_vbus = fsl_otg_drv_vbus,
812 .loc_conn = fsl_otg_loc_conn,
813 .loc_sof = fsl_otg_loc_sof,
814 .start_pulse = fsl_otg_start_pulse,
815
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900816 .add_timer = fsl_otg_fsm_add_timer,
817 .del_timer = fsl_otg_fsm_del_timer,
Li Yang0807c502011-04-18 22:01:59 +0200818
819 .start_host = fsl_otg_start_host,
820 .start_gadget = fsl_otg_start_gadget,
821};
822
823/* Initialize the global variable fsl_otg_dev and request IRQ for OTG */
824static int fsl_otg_conf(struct platform_device *pdev)
825{
826 struct fsl_otg *fsl_otg_tc;
827 int status;
828
829 if (fsl_otg_dev)
830 return 0;
831
832 /* allocate space to fsl otg device */
833 fsl_otg_tc = kzalloc(sizeof(struct fsl_otg), GFP_KERNEL);
834 if (!fsl_otg_tc)
835 return -ENOMEM;
836
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200837 fsl_otg_tc->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
838 if (!fsl_otg_tc->phy.otg) {
839 kfree(fsl_otg_tc);
840 return -ENOMEM;
841 }
842
Li Yang0807c502011-04-18 22:01:59 +0200843 INIT_DELAYED_WORK(&fsl_otg_tc->otg_event, fsl_otg_event);
844
845 INIT_LIST_HEAD(&active_timers);
846 status = fsl_otg_init_timers(&fsl_otg_tc->fsm);
847 if (status) {
848 pr_info("Couldn't init OTG timers\n");
849 goto err;
850 }
851 spin_lock_init(&fsl_otg_tc->fsm.lock);
852
853 /* Set OTG state machine operations */
854 fsl_otg_tc->fsm.ops = &fsl_otg_ops;
855
856 /* initialize the otg structure */
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200857 fsl_otg_tc->phy.label = DRIVER_DESC;
Robert Jarzmik1abd8b32013-05-10 15:15:08 +0530858 fsl_otg_tc->phy.dev = &pdev->dev;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200859 fsl_otg_tc->phy.set_power = fsl_otg_set_power;
860
861 fsl_otg_tc->phy.otg->phy = &fsl_otg_tc->phy;
862 fsl_otg_tc->phy.otg->set_host = fsl_otg_set_host;
863 fsl_otg_tc->phy.otg->set_peripheral = fsl_otg_set_peripheral;
864 fsl_otg_tc->phy.otg->start_hnp = fsl_otg_start_hnp;
865 fsl_otg_tc->phy.otg->start_srp = fsl_otg_start_srp;
Li Yang0807c502011-04-18 22:01:59 +0200866
867 fsl_otg_dev = fsl_otg_tc;
868
869 /* Store the otg transceiver */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530870 status = usb_add_phy(&fsl_otg_tc->phy, USB_PHY_TYPE_USB2);
Li Yang0807c502011-04-18 22:01:59 +0200871 if (status) {
872 pr_warn(FSL_OTG_NAME ": unable to register OTG transceiver.\n");
873 goto err;
874 }
875
876 return 0;
877err:
878 fsl_otg_uninit_timers();
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200879 kfree(fsl_otg_tc->phy.otg);
Li Yang0807c502011-04-18 22:01:59 +0200880 kfree(fsl_otg_tc);
881 return status;
882}
883
884/* OTG Initialization */
885int usb_otg_start(struct platform_device *pdev)
886{
887 struct fsl_otg *p_otg;
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530888 struct usb_phy *otg_trans = usb_get_phy(USB_PHY_TYPE_USB2);
Li Yang0807c502011-04-18 22:01:59 +0200889 struct otg_fsm *fsm;
890 int status;
891 struct resource *res;
892 u32 temp;
Jingoo Han19f9e182013-07-30 17:02:13 +0900893 struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
Li Yang0807c502011-04-18 22:01:59 +0200894
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200895 p_otg = container_of(otg_trans, struct fsl_otg, phy);
Li Yang0807c502011-04-18 22:01:59 +0200896 fsm = &p_otg->fsm;
897
898 /* Initialize the state machine structure with default values */
899 SET_OTG_STATE(otg_trans, OTG_STATE_UNDEFINED);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200900 fsm->otg = p_otg->phy.otg;
Li Yang0807c502011-04-18 22:01:59 +0200901
902 /* We don't require predefined MEM/IRQ resource index */
903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
904 if (!res)
905 return -ENXIO;
906
907 /* We don't request_mem_region here to enable resource sharing
908 * with host/device */
909
910 usb_dr_regs = ioremap(res->start, sizeof(struct usb_dr_mmap));
911 p_otg->dr_mem_map = (struct usb_dr_mmap *)usb_dr_regs;
912 pdata->regs = (void *)usb_dr_regs;
913
914 if (pdata->init && pdata->init(pdev) != 0)
915 return -EINVAL;
916
917 if (pdata->big_endian_mmio) {
918 _fsl_readl = _fsl_readl_be;
919 _fsl_writel = _fsl_writel_be;
920 } else {
921 _fsl_readl = _fsl_readl_le;
922 _fsl_writel = _fsl_writel_le;
923 }
924
925 /* request irq */
926 p_otg->irq = platform_get_irq(pdev, 0);
927 status = request_irq(p_otg->irq, fsl_otg_isr,
928 IRQF_SHARED, driver_name, p_otg);
929 if (status) {
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200930 dev_dbg(p_otg->phy.dev, "can't get IRQ %d, error %d\n",
Li Yang0807c502011-04-18 22:01:59 +0200931 p_otg->irq, status);
932 iounmap(p_otg->dr_mem_map);
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200933 kfree(p_otg->phy.otg);
Li Yang0807c502011-04-18 22:01:59 +0200934 kfree(p_otg);
935 return status;
936 }
937
938 /* stop the controller */
939 temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
940 temp &= ~USB_CMD_RUN_STOP;
941 fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
942
943 /* reset the controller */
944 temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
945 temp |= USB_CMD_CTRL_RESET;
946 fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
947
948 /* wait reset completed */
949 while (fsl_readl(&p_otg->dr_mem_map->usbcmd) & USB_CMD_CTRL_RESET)
950 ;
951
952 /* configure the VBUSHS as IDLE(both host and device) */
953 temp = USB_MODE_STREAM_DISABLE | (pdata->es ? USB_MODE_ES : 0);
954 fsl_writel(temp, &p_otg->dr_mem_map->usbmode);
955
956 /* configure PHY interface */
957 temp = fsl_readl(&p_otg->dr_mem_map->portsc);
958 temp &= ~(PORTSC_PHY_TYPE_SEL | PORTSC_PTW);
959 switch (pdata->phy_mode) {
960 case FSL_USB2_PHY_ULPI:
961 temp |= PORTSC_PTS_ULPI;
962 break;
963 case FSL_USB2_PHY_UTMI_WIDE:
964 temp |= PORTSC_PTW_16BIT;
965 /* fall through */
966 case FSL_USB2_PHY_UTMI:
967 temp |= PORTSC_PTS_UTMI;
968 /* fall through */
969 default:
970 break;
971 }
972 fsl_writel(temp, &p_otg->dr_mem_map->portsc);
973
974 if (pdata->have_sysif_regs) {
975 /* configure control enable IO output, big endian register */
976 temp = __raw_readl(&p_otg->dr_mem_map->control);
977 temp |= USB_CTRL_IOENB;
978 __raw_writel(temp, &p_otg->dr_mem_map->control);
979 }
980
981 /* disable all interrupt and clear all OTGSC status */
982 temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
983 temp &= ~OTGSC_INTERRUPT_ENABLE_BITS_MASK;
984 temp |= OTGSC_INTERRUPT_STATUS_BITS_MASK | OTGSC_CTRL_VBUS_DISCHARGE;
985 fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
986
987 /*
988 * The identification (id) input is FALSE when a Mini-A plug is inserted
989 * in the devices Mini-AB receptacle. Otherwise, this input is TRUE.
990 * Also: record initial state of ID pin
991 */
992 if (fsl_readl(&p_otg->dr_mem_map->otgsc) & OTGSC_STS_USB_ID) {
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200993 p_otg->phy.state = OTG_STATE_UNDEFINED;
Li Yang0807c502011-04-18 22:01:59 +0200994 p_otg->fsm.id = 1;
995 } else {
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200996 p_otg->phy.state = OTG_STATE_A_IDLE;
Li Yang0807c502011-04-18 22:01:59 +0200997 p_otg->fsm.id = 0;
998 }
999
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -07001000 pr_debug("initial ID pin=%d\n", p_otg->fsm.id);
Li Yang0807c502011-04-18 22:01:59 +02001001
1002 /* enable OTG ID pin interrupt */
1003 temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
1004 temp |= OTGSC_INTR_USB_ID_EN;
1005 temp &= ~(OTGSC_CTRL_VBUS_DISCHARGE | OTGSC_INTR_1MS_TIMER_EN);
1006 fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
1007
1008 return 0;
1009}
1010
1011/*
1012 * state file in sysfs
1013 */
1014static int show_fsl_usb2_otg_state(struct device *dev,
1015 struct device_attribute *attr, char *buf)
1016{
1017 struct otg_fsm *fsm = &fsl_otg_dev->fsm;
1018 char *next = buf;
1019 unsigned size = PAGE_SIZE;
1020 unsigned long flags;
1021 int t;
1022
1023 spin_lock_irqsave(&fsm->lock, flags);
1024
1025 /* basic driver infomation */
1026 t = scnprintf(next, size,
1027 DRIVER_DESC "\n" "fsl_usb2_otg version: %s\n\n",
1028 DRIVER_VERSION);
1029 size -= t;
1030 next += t;
1031
1032 /* Registers */
1033 t = scnprintf(next, size,
1034 "OTGSC: 0x%08x\n"
1035 "PORTSC: 0x%08x\n"
1036 "USBMODE: 0x%08x\n"
1037 "USBCMD: 0x%08x\n"
1038 "USBSTS: 0x%08x\n"
1039 "USBINTR: 0x%08x\n",
1040 fsl_readl(&usb_dr_regs->otgsc),
1041 fsl_readl(&usb_dr_regs->portsc),
1042 fsl_readl(&usb_dr_regs->usbmode),
1043 fsl_readl(&usb_dr_regs->usbcmd),
1044 fsl_readl(&usb_dr_regs->usbsts),
1045 fsl_readl(&usb_dr_regs->usbintr));
1046 size -= t;
1047 next += t;
1048
1049 /* State */
1050 t = scnprintf(next, size,
1051 "OTG state: %s\n\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02001052 usb_otg_state_string(fsl_otg_dev->phy.state));
Li Yang0807c502011-04-18 22:01:59 +02001053 size -= t;
1054 next += t;
1055
1056 /* State Machine Variables */
1057 t = scnprintf(next, size,
1058 "a_bus_req: %d\n"
1059 "b_bus_req: %d\n"
1060 "a_bus_resume: %d\n"
1061 "a_bus_suspend: %d\n"
1062 "a_conn: %d\n"
1063 "a_sess_vld: %d\n"
1064 "a_srp_det: %d\n"
1065 "a_vbus_vld: %d\n"
1066 "b_bus_resume: %d\n"
1067 "b_bus_suspend: %d\n"
1068 "b_conn: %d\n"
1069 "b_se0_srp: %d\n"
Anton Tikhomirov68041782013-10-03 12:42:04 +09001070 "b_ssend_srp: %d\n"
Li Yang0807c502011-04-18 22:01:59 +02001071 "b_sess_vld: %d\n"
1072 "id: %d\n",
1073 fsm->a_bus_req,
1074 fsm->b_bus_req,
1075 fsm->a_bus_resume,
1076 fsm->a_bus_suspend,
1077 fsm->a_conn,
1078 fsm->a_sess_vld,
1079 fsm->a_srp_det,
1080 fsm->a_vbus_vld,
1081 fsm->b_bus_resume,
1082 fsm->b_bus_suspend,
1083 fsm->b_conn,
1084 fsm->b_se0_srp,
Anton Tikhomirov68041782013-10-03 12:42:04 +09001085 fsm->b_ssend_srp,
Li Yang0807c502011-04-18 22:01:59 +02001086 fsm->b_sess_vld,
1087 fsm->id);
1088 size -= t;
1089 next += t;
1090
1091 spin_unlock_irqrestore(&fsm->lock, flags);
1092
1093 return PAGE_SIZE - size;
1094}
1095
1096static DEVICE_ATTR(fsl_usb2_otg_state, S_IRUGO, show_fsl_usb2_otg_state, NULL);
1097
1098
1099/* Char driver interface to control some OTG input */
1100
1101/*
1102 * Handle some ioctl command, such as get otg
1103 * status and set host suspend
1104 */
1105static long fsl_otg_ioctl(struct file *file, unsigned int cmd,
1106 unsigned long arg)
1107{
1108 u32 retval = 0;
1109
1110 switch (cmd) {
1111 case GET_OTG_STATUS:
1112 retval = fsl_otg_dev->host_working;
1113 break;
1114
1115 case SET_A_SUSPEND_REQ:
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +09001116 fsl_otg_dev->fsm.a_suspend_req_inf = arg;
Li Yang0807c502011-04-18 22:01:59 +02001117 break;
1118
1119 case SET_A_BUS_DROP:
1120 fsl_otg_dev->fsm.a_bus_drop = arg;
1121 break;
1122
1123 case SET_A_BUS_REQ:
1124 fsl_otg_dev->fsm.a_bus_req = arg;
1125 break;
1126
1127 case SET_B_BUS_REQ:
1128 fsl_otg_dev->fsm.b_bus_req = arg;
1129 break;
1130
1131 default:
1132 break;
1133 }
1134
1135 otg_statemachine(&fsl_otg_dev->fsm);
1136
1137 return retval;
1138}
1139
1140static int fsl_otg_open(struct inode *inode, struct file *file)
1141{
1142 return 0;
1143}
1144
1145static int fsl_otg_release(struct inode *inode, struct file *file)
1146{
1147 return 0;
1148}
1149
1150static const struct file_operations otg_fops = {
1151 .owner = THIS_MODULE,
1152 .llseek = NULL,
1153 .read = NULL,
1154 .write = NULL,
1155 .unlocked_ioctl = fsl_otg_ioctl,
1156 .open = fsl_otg_open,
1157 .release = fsl_otg_release,
1158};
1159
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001160static int fsl_otg_probe(struct platform_device *pdev)
Li Yang0807c502011-04-18 22:01:59 +02001161{
1162 int ret;
1163
Jingoo Han19f9e182013-07-30 17:02:13 +09001164 if (!dev_get_platdata(&pdev->dev))
Li Yang0807c502011-04-18 22:01:59 +02001165 return -ENODEV;
1166
1167 /* configure the OTG */
1168 ret = fsl_otg_conf(pdev);
1169 if (ret) {
1170 dev_err(&pdev->dev, "Couldn't configure OTG module\n");
1171 return ret;
1172 }
1173
1174 /* start OTG */
1175 ret = usb_otg_start(pdev);
1176 if (ret) {
1177 dev_err(&pdev->dev, "Can't init FSL OTG device\n");
1178 return ret;
1179 }
1180
1181 ret = register_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME, &otg_fops);
1182 if (ret) {
1183 dev_err(&pdev->dev, "unable to register FSL OTG device\n");
1184 return ret;
1185 }
1186
1187 ret = device_create_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
1188 if (ret)
1189 dev_warn(&pdev->dev, "Can't register sysfs attribute\n");
1190
1191 return ret;
1192}
1193
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001194static int fsl_otg_remove(struct platform_device *pdev)
Li Yang0807c502011-04-18 22:01:59 +02001195{
Jingoo Han19f9e182013-07-30 17:02:13 +09001196 struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
Li Yang0807c502011-04-18 22:01:59 +02001197
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301198 usb_remove_phy(&fsl_otg_dev->phy);
Li Yang0807c502011-04-18 22:01:59 +02001199 free_irq(fsl_otg_dev->irq, fsl_otg_dev);
1200
1201 iounmap((void *)usb_dr_regs);
1202
1203 fsl_otg_uninit_timers();
Heikki Krogerus7e062c02012-02-13 13:24:06 +02001204 kfree(fsl_otg_dev->phy.otg);
Li Yang0807c502011-04-18 22:01:59 +02001205 kfree(fsl_otg_dev);
1206
1207 device_remove_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
1208
1209 unregister_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME);
1210
1211 if (pdata->exit)
1212 pdata->exit(pdev);
1213
1214 return 0;
1215}
1216
1217struct platform_driver fsl_otg_driver = {
1218 .probe = fsl_otg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001219 .remove = fsl_otg_remove,
Li Yang0807c502011-04-18 22:01:59 +02001220 .driver = {
1221 .name = driver_name,
1222 .owner = THIS_MODULE,
1223 },
1224};
1225
Axel Lincc27c962011-11-27 20:16:27 +08001226module_platform_driver(fsl_otg_driver);
Li Yang0807c502011-04-18 22:01:59 +02001227
1228MODULE_DESCRIPTION(DRIVER_INFO);
1229MODULE_AUTHOR(DRIVER_AUTHOR);
1230MODULE_LICENSE("GPL");