2 * Copyright (C) 2007,2008 Freescale semiconductor, Inc.
4 * Author: Li Yang <LeoLi@freescale.com>
5 * Jerry Huang <Chang-Ming.Huang@freescale.com>
7 * Initialization based on code from Shlomi Gridish.
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.
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.
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.
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/interrupt.h>
32 #include <linux/timer.h>
33 #include <linux/usb.h>
34 #include <linux/device.h>
35 #include <linux/usb/ch9.h>
36 #include <linux/usb/gadget.h>
37 #include <linux/workqueue.h>
38 #include <linux/time.h>
39 #include <linux/fsl_devices.h>
40 #include <linux/platform_device.h>
41 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
45 #include "phy-fsl-usb.h"
48 #define VDBG(fmt, args...) pr_debug("[%s] " fmt, \
51 #define VDBG(stuff...) do {} while (0)
54 #define DRIVER_VERSION "Rev. 1.55"
55 #define DRIVER_AUTHOR "Jerry Huang/Li Yang"
56 #define DRIVER_DESC "Freescale USB OTG Transceiver Driver"
57 #define DRIVER_INFO DRIVER_DESC " " DRIVER_VERSION
59 static const char driver_name[] = "fsl-usb2-otg";
61 const pm_message_t otg_suspend_state = {
67 static struct usb_dr_mmap *usb_dr_regs;
68 static struct fsl_otg *fsl_otg_dev;
69 static int srp_wait_done;
72 struct fsl_otg_timer *a_wait_vrise_tmr, *a_wait_bcon_tmr, *a_aidl_bdis_tmr,
73 *b_ase0_brst_tmr, *b_se0_srp_tmr;
75 /* Driver specific timers */
76 struct fsl_otg_timer *b_data_pulse_tmr, *b_vbus_pulse_tmr, *b_srp_fail_tmr,
77 *b_srp_wait_tmr, *a_wait_enum_tmr;
79 static struct list_head active_timers;
81 static struct fsl_otg_config fsl_otg_initdata = {
86 static u32 _fsl_readl_be(const unsigned __iomem *p)
91 static u32 _fsl_readl_le(const unsigned __iomem *p)
96 static void _fsl_writel_be(u32 v, unsigned __iomem *p)
101 static void _fsl_writel_le(u32 v, unsigned __iomem *p)
106 static u32 (*_fsl_readl)(const unsigned __iomem *p);
107 static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
109 #define fsl_readl(p) (*_fsl_readl)((p))
110 #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
113 #define fsl_readl(addr) readl(addr)
114 #define fsl_writel(val, addr) writel(val, addr)
115 #endif /* CONFIG_PPC32 */
117 int write_ulpi(u8 addr, u8 data)
121 temp = 0x60000000 | (addr << 16) | data;
122 fsl_writel(temp, &usb_dr_regs->ulpiview);
126 /* -------------------------------------------------------------*/
127 /* Operations that will be called from OTG Finite State Machine */
129 /* Charge vbus for vbus pulsing in SRP */
130 void fsl_otg_chrg_vbus(struct otg_fsm *fsm, int on)
134 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
137 /* stop discharging, start charging */
138 tmp = (tmp & ~OTGSC_CTRL_VBUS_DISCHARGE) |
139 OTGSC_CTRL_VBUS_CHARGE;
142 tmp &= ~OTGSC_CTRL_VBUS_CHARGE;
144 fsl_writel(tmp, &usb_dr_regs->otgsc);
147 /* Discharge vbus through a resistor to ground */
148 void fsl_otg_dischrg_vbus(int on)
152 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
155 /* stop charging, start discharging */
156 tmp = (tmp & ~OTGSC_CTRL_VBUS_CHARGE) |
157 OTGSC_CTRL_VBUS_DISCHARGE;
159 /* stop discharging */
160 tmp &= ~OTGSC_CTRL_VBUS_DISCHARGE;
162 fsl_writel(tmp, &usb_dr_regs->otgsc);
165 /* A-device driver vbus, controlled through PP bit in PORTSC */
166 void fsl_otg_drv_vbus(struct otg_fsm *fsm, int on)
171 tmp = fsl_readl(&usb_dr_regs->portsc) & ~PORTSC_W1C_BITS;
172 fsl_writel(tmp | PORTSC_PORT_POWER, &usb_dr_regs->portsc);
174 tmp = fsl_readl(&usb_dr_regs->portsc) &
175 ~PORTSC_W1C_BITS & ~PORTSC_PORT_POWER;
176 fsl_writel(tmp, &usb_dr_regs->portsc);
181 * Pull-up D+, signalling connect by periperal. Also used in
182 * data-line pulsing in SRP
184 void fsl_otg_loc_conn(struct otg_fsm *fsm, int on)
188 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
191 tmp |= OTGSC_CTRL_DATA_PULSING;
193 tmp &= ~OTGSC_CTRL_DATA_PULSING;
195 fsl_writel(tmp, &usb_dr_regs->otgsc);
199 * Generate SOF by host. This is controlled through suspend/resume the
200 * port. In host mode, controller will automatically send SOF.
201 * Suspend will block the data on the port.
203 void fsl_otg_loc_sof(struct otg_fsm *fsm, int on)
207 tmp = fsl_readl(&fsl_otg_dev->dr_mem_map->portsc) & ~PORTSC_W1C_BITS;
209 tmp |= PORTSC_PORT_FORCE_RESUME;
211 tmp |= PORTSC_PORT_SUSPEND;
213 fsl_writel(tmp, &fsl_otg_dev->dr_mem_map->portsc);
217 /* Start SRP pulsing by data-line pulsing, followed with v-bus pulsing. */
218 void fsl_otg_start_pulse(struct otg_fsm *fsm)
224 tmp = fsl_readl(&usb_dr_regs->otgsc) & ~OTGSC_INTSTS_MASK;
225 tmp |= OTGSC_HA_DATA_PULSE;
226 fsl_writel(tmp, &usb_dr_regs->otgsc);
231 fsl_otg_add_timer(fsm, b_data_pulse_tmr);
234 void b_data_pulse_end(unsigned long foo)
241 /* Do VBUS pulse after data pulse */
242 fsl_otg_pulse_vbus();
245 void fsl_otg_pulse_vbus(void)
248 fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 1);
249 /* start the timer to end vbus charge */
250 fsl_otg_add_timer(&fsl_otg_dev->fsm, b_vbus_pulse_tmr);
253 void b_vbus_pulse_end(unsigned long foo)
255 fsl_otg_chrg_vbus(&fsl_otg_dev->fsm, 0);
258 * As USB3300 using the same a_sess_vld and b_sess_vld voltage
259 * we need to discharge the bus for a while to distinguish
260 * residual voltage of vbus pulsing and A device pull up
262 fsl_otg_dischrg_vbus(1);
263 fsl_otg_add_timer(&fsl_otg_dev->fsm, b_srp_wait_tmr);
266 void b_srp_end(unsigned long foo)
268 fsl_otg_dischrg_vbus(0);
271 if ((fsl_otg_dev->phy.otg->state == OTG_STATE_B_SRP_INIT) &&
272 fsl_otg_dev->fsm.b_sess_vld)
273 fsl_otg_dev->fsm.b_srp_done = 1;
277 * Workaround for a_host suspending too fast. When a_bus_req=0,
278 * a_host will start by SRP. It needs to set b_hnp_enable before
279 * actually suspending to start HNP
281 void a_wait_enum(unsigned long foo)
283 VDBG("a_wait_enum timeout\n");
284 if (!fsl_otg_dev->phy.otg->host->b_hnp_enable)
285 fsl_otg_add_timer(&fsl_otg_dev->fsm, a_wait_enum_tmr);
287 otg_statemachine(&fsl_otg_dev->fsm);
290 /* The timeout callback function to set time out bit */
291 void set_tmout(unsigned long indicator)
293 *(int *)indicator = 1;
296 /* Initialize timers */
297 int fsl_otg_init_timers(struct otg_fsm *fsm)
299 /* FSM used timers */
300 a_wait_vrise_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_VRISE,
301 (unsigned long)&fsm->a_wait_vrise_tmout);
302 if (!a_wait_vrise_tmr)
305 a_wait_bcon_tmr = otg_timer_initializer(&set_tmout, TA_WAIT_BCON,
306 (unsigned long)&fsm->a_wait_bcon_tmout);
307 if (!a_wait_bcon_tmr)
310 a_aidl_bdis_tmr = otg_timer_initializer(&set_tmout, TA_AIDL_BDIS,
311 (unsigned long)&fsm->a_aidl_bdis_tmout);
312 if (!a_aidl_bdis_tmr)
315 b_ase0_brst_tmr = otg_timer_initializer(&set_tmout, TB_ASE0_BRST,
316 (unsigned long)&fsm->b_ase0_brst_tmout);
317 if (!b_ase0_brst_tmr)
320 b_se0_srp_tmr = otg_timer_initializer(&set_tmout, TB_SE0_SRP,
321 (unsigned long)&fsm->b_se0_srp);
325 b_srp_fail_tmr = otg_timer_initializer(&set_tmout, TB_SRP_FAIL,
326 (unsigned long)&fsm->b_srp_done);
330 a_wait_enum_tmr = otg_timer_initializer(&a_wait_enum, 10,
331 (unsigned long)&fsm);
332 if (!a_wait_enum_tmr)
335 /* device driver used timers */
336 b_srp_wait_tmr = otg_timer_initializer(&b_srp_end, TB_SRP_WAIT, 0);
340 b_data_pulse_tmr = otg_timer_initializer(&b_data_pulse_end,
342 if (!b_data_pulse_tmr)
345 b_vbus_pulse_tmr = otg_timer_initializer(&b_vbus_pulse_end,
347 if (!b_vbus_pulse_tmr)
353 /* Uninitialize timers */
354 void fsl_otg_uninit_timers(void)
356 /* FSM used timers */
357 kfree(a_wait_vrise_tmr);
358 kfree(a_wait_bcon_tmr);
359 kfree(a_aidl_bdis_tmr);
360 kfree(b_ase0_brst_tmr);
361 kfree(b_se0_srp_tmr);
362 kfree(b_srp_fail_tmr);
363 kfree(a_wait_enum_tmr);
365 /* device driver used timers */
366 kfree(b_srp_wait_tmr);
367 kfree(b_data_pulse_tmr);
368 kfree(b_vbus_pulse_tmr);
371 static struct fsl_otg_timer *fsl_otg_get_timer(enum otg_fsm_timer t)
373 struct fsl_otg_timer *timer;
375 /* REVISIT: use array of pointers to timers instead */
378 timer = a_wait_vrise_tmr;
381 timer = a_wait_vrise_tmr;
384 timer = a_wait_vrise_tmr;
387 timer = a_wait_vrise_tmr;
390 timer = a_wait_vrise_tmr;
393 timer = a_wait_vrise_tmr;
396 timer = a_wait_vrise_tmr;
405 /* Add timer to timer list */
406 void fsl_otg_add_timer(struct otg_fsm *fsm, void *gtimer)
408 struct fsl_otg_timer *timer = gtimer;
409 struct fsl_otg_timer *tmp_timer;
412 * Check if the timer is already in the active list,
413 * if so update timer count
415 list_for_each_entry(tmp_timer, &active_timers, list)
416 if (tmp_timer == timer) {
417 timer->count = timer->expires;
420 timer->count = timer->expires;
421 list_add_tail(&timer->list, &active_timers);
424 static void fsl_otg_fsm_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
426 struct fsl_otg_timer *timer;
428 timer = fsl_otg_get_timer(t);
432 fsl_otg_add_timer(fsm, timer);
435 /* Remove timer from the timer list; clear timeout status */
436 void fsl_otg_del_timer(struct otg_fsm *fsm, void *gtimer)
438 struct fsl_otg_timer *timer = gtimer;
439 struct fsl_otg_timer *tmp_timer, *del_tmp;
441 list_for_each_entry_safe(tmp_timer, del_tmp, &active_timers, list)
442 if (tmp_timer == timer)
443 list_del(&timer->list);
446 static void fsl_otg_fsm_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer t)
448 struct fsl_otg_timer *timer;
450 timer = fsl_otg_get_timer(t);
454 fsl_otg_del_timer(fsm, timer);
457 /* Reset controller, not reset the bus */
458 void otg_reset_controller(void)
462 command = fsl_readl(&usb_dr_regs->usbcmd);
464 fsl_writel(command, &usb_dr_regs->usbcmd);
465 while (fsl_readl(&usb_dr_regs->usbcmd) & (1 << 1))
469 /* Call suspend/resume routines in host driver */
470 int fsl_otg_start_host(struct otg_fsm *fsm, int on)
472 struct usb_otg *otg = fsm->otg;
474 struct fsl_otg *otg_dev =
475 container_of(otg->usb_phy, struct fsl_otg, phy);
480 dev = otg->host->controller;
483 * Update a_vbus_vld state as a_vbus_vld int is disabled
487 !!(fsl_readl(&usb_dr_regs->otgsc) & OTGSC_STS_A_VBUS_VALID);
489 /* start fsl usb host controller */
490 if (otg_dev->host_working)
493 otg_reset_controller();
494 VDBG("host on......\n");
495 if (dev->driver->pm && dev->driver->pm->resume) {
496 retval = dev->driver->pm->resume(dev);
499 fsl_otg_drv_vbus(fsm, 1);
501 * Workaround: b_host can't driver
502 * vbus, but PP in PORTSC needs to
503 * be 1 for host to work.
504 * So we set drv_vbus bit in
505 * transceiver to 0 thru ULPI.
507 write_ulpi(0x0c, 0x20);
511 otg_dev->host_working = 1;
514 /* stop fsl usb host controller */
515 if (!otg_dev->host_working)
518 VDBG("host off......\n");
519 if (dev && dev->driver) {
520 if (dev->driver->pm && dev->driver->pm->suspend)
521 retval = dev->driver->pm->suspend(dev);
524 fsl_otg_drv_vbus(fsm, 0);
526 otg_dev->host_working = 0;
534 * Call suspend and resume function in udc driver
535 * to stop and start udc driver.
537 int fsl_otg_start_gadget(struct otg_fsm *fsm, int on)
539 struct usb_otg *otg = fsm->otg;
542 if (!otg->gadget || !otg->gadget->dev.parent)
545 VDBG("gadget %s\n", on ? "on" : "off");
546 dev = otg->gadget->dev.parent;
549 if (dev->driver->resume)
550 dev->driver->resume(dev);
552 if (dev->driver->suspend)
553 dev->driver->suspend(dev, otg_suspend_state);
560 * Called by initialization code of host driver. Register host controller
561 * to the OTG. Suspend host for OTG role detection.
563 static int fsl_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
565 struct fsl_otg *otg_dev;
570 otg_dev = container_of(otg->usb_phy, struct fsl_otg, phy);
571 if (otg_dev != fsl_otg_dev)
576 otg_dev->fsm.a_bus_drop = 0;
577 otg_dev->fsm.a_bus_req = 1;
580 VDBG("host off......\n");
582 otg->host->otg_port = fsl_otg_initdata.otg_port;
583 otg->host->is_b_host = otg_dev->fsm.id;
585 * must leave time for hub_wq to finish its thing
586 * before yanking the host driver out from under it,
587 * so suspend the host after a short delay.
589 otg_dev->host_working = 1;
590 schedule_delayed_work(&otg_dev->otg_event, 100);
593 /* host driver going away */
594 if (!(fsl_readl(&otg_dev->dr_mem_map->otgsc) &
596 /* Mini-A cable connected */
597 struct otg_fsm *fsm = &otg_dev->fsm;
599 otg->state = OTG_STATE_UNDEFINED;
600 fsm->protocol = PROTO_UNDEF;
604 otg_dev->host_working = 0;
606 otg_statemachine(&otg_dev->fsm);
611 /* Called by initialization code of udc. Register udc to OTG. */
612 static int fsl_otg_set_peripheral(struct usb_otg *otg,
613 struct usb_gadget *gadget)
615 struct fsl_otg *otg_dev;
620 otg_dev = container_of(otg->usb_phy, struct fsl_otg, phy);
621 VDBG("otg_dev 0x%x\n", (int)otg_dev);
622 VDBG("fsl_otg_dev 0x%x\n", (int)fsl_otg_dev);
623 if (otg_dev != fsl_otg_dev)
628 otg->gadget->ops->vbus_draw(otg->gadget, 0);
629 usb_gadget_vbus_disconnect(otg->gadget);
631 otg_dev->fsm.b_bus_req = 0;
632 otg_statemachine(&otg_dev->fsm);
636 otg->gadget = gadget;
637 otg->gadget->is_a_peripheral = !otg_dev->fsm.id;
639 otg_dev->fsm.b_bus_req = 1;
641 /* start the gadget right away if the ID pin says Mini-B */
642 pr_debug("ID pin=%d\n", otg_dev->fsm.id);
643 if (otg_dev->fsm.id == 1) {
644 fsl_otg_start_host(&otg_dev->fsm, 0);
645 otg_drv_vbus(&otg_dev->fsm, 0);
646 fsl_otg_start_gadget(&otg_dev->fsm, 1);
653 * Delayed pin detect interrupt processing.
655 * When the Mini-A cable is disconnected from the board,
656 * the pin-detect interrupt happens before the disconnect
657 * interrupts for the connected device(s). In order to
658 * process the disconnect interrupt(s) prior to switching
659 * roles, the pin-detect interrupts are delayed, and handled
662 static void fsl_otg_event(struct work_struct *work)
664 struct fsl_otg *og = container_of(work, struct fsl_otg, otg_event.work);
665 struct otg_fsm *fsm = &og->fsm;
667 if (fsm->id) { /* switch to gadget */
668 fsl_otg_start_host(fsm, 0);
669 otg_drv_vbus(fsm, 0);
670 fsl_otg_start_gadget(fsm, 1);
674 /* B-device start SRP */
675 static int fsl_otg_start_srp(struct usb_otg *otg)
677 struct fsl_otg *otg_dev;
679 if (!otg || otg->state != OTG_STATE_B_IDLE)
682 otg_dev = container_of(otg->usb_phy, struct fsl_otg, phy);
683 if (otg_dev != fsl_otg_dev)
686 otg_dev->fsm.b_bus_req = 1;
687 otg_statemachine(&otg_dev->fsm);
692 /* A_host suspend will call this function to start hnp */
693 static int fsl_otg_start_hnp(struct usb_otg *otg)
695 struct fsl_otg *otg_dev;
700 otg_dev = container_of(otg->usb_phy, struct fsl_otg, phy);
701 if (otg_dev != fsl_otg_dev)
704 pr_debug("start_hnp...\n");
706 /* clear a_bus_req to enter a_suspend state */
707 otg_dev->fsm.a_bus_req = 0;
708 otg_statemachine(&otg_dev->fsm);
714 * Interrupt handler. OTG/host/peripheral share the same int line.
715 * OTG driver clears OTGSC interrupts and leaves USB interrupts
716 * intact. It needs to have knowledge of some USB interrupts
717 * such as port change.
719 irqreturn_t fsl_otg_isr(int irq, void *dev_id)
721 struct otg_fsm *fsm = &((struct fsl_otg *)dev_id)->fsm;
722 struct usb_otg *otg = ((struct fsl_otg *)dev_id)->phy.otg;
723 u32 otg_int_src, otg_sc;
725 otg_sc = fsl_readl(&usb_dr_regs->otgsc);
726 otg_int_src = otg_sc & OTGSC_INTSTS_MASK & (otg_sc >> 8);
728 /* Only clear otg interrupts */
729 fsl_writel(otg_sc, &usb_dr_regs->otgsc);
731 /*FIXME: ID change not generate when init to 0 */
732 fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
733 otg->default_a = (fsm->id == 0);
735 /* process OTG interrupts */
737 if (otg_int_src & OTGSC_INTSTS_USB_ID) {
738 fsm->id = (otg_sc & OTGSC_STS_USB_ID) ? 1 : 0;
739 otg->default_a = (fsm->id == 0);
740 /* clear conn information */
747 otg->host->is_b_host = fsm->id;
749 otg->gadget->is_a_peripheral = !fsm->id;
750 VDBG("ID int (ID is %d)\n", fsm->id);
752 if (fsm->id) { /* switch to gadget */
753 schedule_delayed_work(
754 &((struct fsl_otg *)dev_id)->otg_event,
756 } else { /* switch to host */
757 cancel_delayed_work(&
758 ((struct fsl_otg *)dev_id)->
760 fsl_otg_start_gadget(fsm, 0);
761 otg_drv_vbus(fsm, 1);
762 fsl_otg_start_host(fsm, 1);
770 static struct otg_fsm_ops fsl_otg_ops = {
771 .chrg_vbus = fsl_otg_chrg_vbus,
772 .drv_vbus = fsl_otg_drv_vbus,
773 .loc_conn = fsl_otg_loc_conn,
774 .loc_sof = fsl_otg_loc_sof,
775 .start_pulse = fsl_otg_start_pulse,
777 .add_timer = fsl_otg_fsm_add_timer,
778 .del_timer = fsl_otg_fsm_del_timer,
780 .start_host = fsl_otg_start_host,
781 .start_gadget = fsl_otg_start_gadget,
784 /* Initialize the global variable fsl_otg_dev and request IRQ for OTG */
785 static int fsl_otg_conf(struct platform_device *pdev)
787 struct fsl_otg *fsl_otg_tc;
793 /* allocate space to fsl otg device */
794 fsl_otg_tc = kzalloc(sizeof(struct fsl_otg), GFP_KERNEL);
798 fsl_otg_tc->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
799 if (!fsl_otg_tc->phy.otg) {
804 INIT_DELAYED_WORK(&fsl_otg_tc->otg_event, fsl_otg_event);
806 INIT_LIST_HEAD(&active_timers);
807 status = fsl_otg_init_timers(&fsl_otg_tc->fsm);
809 pr_info("Couldn't init OTG timers\n");
812 mutex_init(&fsl_otg_tc->fsm.lock);
814 /* Set OTG state machine operations */
815 fsl_otg_tc->fsm.ops = &fsl_otg_ops;
817 /* initialize the otg structure */
818 fsl_otg_tc->phy.label = DRIVER_DESC;
819 fsl_otg_tc->phy.dev = &pdev->dev;
821 fsl_otg_tc->phy.otg->usb_phy = &fsl_otg_tc->phy;
822 fsl_otg_tc->phy.otg->set_host = fsl_otg_set_host;
823 fsl_otg_tc->phy.otg->set_peripheral = fsl_otg_set_peripheral;
824 fsl_otg_tc->phy.otg->start_hnp = fsl_otg_start_hnp;
825 fsl_otg_tc->phy.otg->start_srp = fsl_otg_start_srp;
827 fsl_otg_dev = fsl_otg_tc;
829 /* Store the otg transceiver */
830 status = usb_add_phy(&fsl_otg_tc->phy, USB_PHY_TYPE_USB2);
832 pr_warn(FSL_OTG_NAME ": unable to register OTG transceiver.\n");
838 fsl_otg_uninit_timers();
839 kfree(fsl_otg_tc->phy.otg);
844 /* OTG Initialization */
845 int usb_otg_start(struct platform_device *pdev)
847 struct fsl_otg *p_otg;
848 struct usb_phy *otg_trans = usb_get_phy(USB_PHY_TYPE_USB2);
851 struct resource *res;
853 struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
855 p_otg = container_of(otg_trans, struct fsl_otg, phy);
858 /* Initialize the state machine structure with default values */
859 SET_OTG_STATE(otg_trans, OTG_STATE_UNDEFINED);
860 fsm->otg = p_otg->phy.otg;
862 /* We don't require predefined MEM/IRQ resource index */
863 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
867 /* We don't request_mem_region here to enable resource sharing
868 * with host/device */
870 usb_dr_regs = ioremap(res->start, sizeof(struct usb_dr_mmap));
871 p_otg->dr_mem_map = (struct usb_dr_mmap *)usb_dr_regs;
872 pdata->regs = (void *)usb_dr_regs;
874 if (pdata->init && pdata->init(pdev) != 0)
878 if (pdata->big_endian_mmio) {
879 _fsl_readl = _fsl_readl_be;
880 _fsl_writel = _fsl_writel_be;
882 _fsl_readl = _fsl_readl_le;
883 _fsl_writel = _fsl_writel_le;
888 p_otg->irq = platform_get_irq(pdev, 0);
891 status = request_irq(p_otg->irq, fsl_otg_isr,
892 IRQF_SHARED, driver_name, p_otg);
894 dev_dbg(p_otg->phy.dev, "can't get IRQ %d, error %d\n",
896 iounmap(p_otg->dr_mem_map);
897 kfree(p_otg->phy.otg);
902 /* stop the controller */
903 temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
904 temp &= ~USB_CMD_RUN_STOP;
905 fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
907 /* reset the controller */
908 temp = fsl_readl(&p_otg->dr_mem_map->usbcmd);
909 temp |= USB_CMD_CTRL_RESET;
910 fsl_writel(temp, &p_otg->dr_mem_map->usbcmd);
912 /* wait reset completed */
913 while (fsl_readl(&p_otg->dr_mem_map->usbcmd) & USB_CMD_CTRL_RESET)
916 /* configure the VBUSHS as IDLE(both host and device) */
917 temp = USB_MODE_STREAM_DISABLE | (pdata->es ? USB_MODE_ES : 0);
918 fsl_writel(temp, &p_otg->dr_mem_map->usbmode);
920 /* configure PHY interface */
921 temp = fsl_readl(&p_otg->dr_mem_map->portsc);
922 temp &= ~(PORTSC_PHY_TYPE_SEL | PORTSC_PTW);
923 switch (pdata->phy_mode) {
924 case FSL_USB2_PHY_ULPI:
925 temp |= PORTSC_PTS_ULPI;
927 case FSL_USB2_PHY_UTMI_WIDE:
928 temp |= PORTSC_PTW_16BIT;
930 case FSL_USB2_PHY_UTMI:
931 temp |= PORTSC_PTS_UTMI;
936 fsl_writel(temp, &p_otg->dr_mem_map->portsc);
938 if (pdata->have_sysif_regs) {
939 /* configure control enable IO output, big endian register */
940 temp = __raw_readl(&p_otg->dr_mem_map->control);
941 temp |= USB_CTRL_IOENB;
942 __raw_writel(temp, &p_otg->dr_mem_map->control);
945 /* disable all interrupt and clear all OTGSC status */
946 temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
947 temp &= ~OTGSC_INTERRUPT_ENABLE_BITS_MASK;
948 temp |= OTGSC_INTERRUPT_STATUS_BITS_MASK | OTGSC_CTRL_VBUS_DISCHARGE;
949 fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
952 * The identification (id) input is FALSE when a Mini-A plug is inserted
953 * in the devices Mini-AB receptacle. Otherwise, this input is TRUE.
954 * Also: record initial state of ID pin
956 if (fsl_readl(&p_otg->dr_mem_map->otgsc) & OTGSC_STS_USB_ID) {
957 p_otg->phy.otg->state = OTG_STATE_UNDEFINED;
960 p_otg->phy.otg->state = OTG_STATE_A_IDLE;
964 pr_debug("initial ID pin=%d\n", p_otg->fsm.id);
966 /* enable OTG ID pin interrupt */
967 temp = fsl_readl(&p_otg->dr_mem_map->otgsc);
968 temp |= OTGSC_INTR_USB_ID_EN;
969 temp &= ~(OTGSC_CTRL_VBUS_DISCHARGE | OTGSC_INTR_1MS_TIMER_EN);
970 fsl_writel(temp, &p_otg->dr_mem_map->otgsc);
976 * state file in sysfs
978 static ssize_t show_fsl_usb2_otg_state(struct device *dev,
979 struct device_attribute *attr, char *buf)
981 struct otg_fsm *fsm = &fsl_otg_dev->fsm;
983 unsigned size = PAGE_SIZE;
986 mutex_lock(&fsm->lock);
988 /* basic driver infomation */
989 t = scnprintf(next, size,
990 DRIVER_DESC "\n" "fsl_usb2_otg version: %s\n\n",
996 t = scnprintf(next, size,
1002 "USBINTR: 0x%08x\n",
1003 fsl_readl(&usb_dr_regs->otgsc),
1004 fsl_readl(&usb_dr_regs->portsc),
1005 fsl_readl(&usb_dr_regs->usbmode),
1006 fsl_readl(&usb_dr_regs->usbcmd),
1007 fsl_readl(&usb_dr_regs->usbsts),
1008 fsl_readl(&usb_dr_regs->usbintr));
1013 t = scnprintf(next, size,
1014 "OTG state: %s\n\n",
1015 usb_otg_state_string(fsl_otg_dev->phy.otg->state));
1019 /* State Machine Variables */
1020 t = scnprintf(next, size,
1023 "a_bus_resume: %d\n"
1024 "a_bus_suspend: %d\n"
1029 "b_bus_resume: %d\n"
1030 "b_bus_suspend: %d\n"
1054 mutex_unlock(&fsm->lock);
1056 return PAGE_SIZE - size;
1059 static DEVICE_ATTR(fsl_usb2_otg_state, S_IRUGO, show_fsl_usb2_otg_state, NULL);
1062 /* Char driver interface to control some OTG input */
1065 * Handle some ioctl command, such as get otg
1066 * status and set host suspend
1068 static long fsl_otg_ioctl(struct file *file, unsigned int cmd,
1074 case GET_OTG_STATUS:
1075 retval = fsl_otg_dev->host_working;
1078 case SET_A_SUSPEND_REQ:
1079 fsl_otg_dev->fsm.a_suspend_req_inf = arg;
1082 case SET_A_BUS_DROP:
1083 fsl_otg_dev->fsm.a_bus_drop = arg;
1087 fsl_otg_dev->fsm.a_bus_req = arg;
1091 fsl_otg_dev->fsm.b_bus_req = arg;
1098 otg_statemachine(&fsl_otg_dev->fsm);
1103 static int fsl_otg_open(struct inode *inode, struct file *file)
1108 static int fsl_otg_release(struct inode *inode, struct file *file)
1113 static const struct file_operations otg_fops = {
1114 .owner = THIS_MODULE,
1118 .unlocked_ioctl = fsl_otg_ioctl,
1119 .open = fsl_otg_open,
1120 .release = fsl_otg_release,
1123 static int fsl_otg_probe(struct platform_device *pdev)
1127 if (!dev_get_platdata(&pdev->dev))
1130 /* configure the OTG */
1131 ret = fsl_otg_conf(pdev);
1133 dev_err(&pdev->dev, "Couldn't configure OTG module\n");
1138 ret = usb_otg_start(pdev);
1140 dev_err(&pdev->dev, "Can't init FSL OTG device\n");
1144 ret = register_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME, &otg_fops);
1146 dev_err(&pdev->dev, "unable to register FSL OTG device\n");
1150 ret = device_create_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
1152 dev_warn(&pdev->dev, "Can't register sysfs attribute\n");
1157 static int fsl_otg_remove(struct platform_device *pdev)
1159 struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
1161 usb_remove_phy(&fsl_otg_dev->phy);
1162 free_irq(fsl_otg_dev->irq, fsl_otg_dev);
1164 iounmap((void *)usb_dr_regs);
1166 fsl_otg_uninit_timers();
1167 kfree(fsl_otg_dev->phy.otg);
1170 device_remove_file(&pdev->dev, &dev_attr_fsl_usb2_otg_state);
1172 unregister_chrdev(FSL_OTG_MAJOR, FSL_OTG_NAME);
1180 struct platform_driver fsl_otg_driver = {
1181 .probe = fsl_otg_probe,
1182 .remove = fsl_otg_remove,
1184 .name = driver_name,
1185 .owner = THIS_MODULE,
1189 module_platform_driver(fsl_otg_driver);
1191 MODULE_DESCRIPTION(DRIVER_INFO);
1192 MODULE_AUTHOR(DRIVER_AUTHOR);
1193 MODULE_LICENSE("GPL");