1 /*****************************************************************
2 * Copyright 2005 Mentor Graphics Corporation
3 * Copyright (C) 2005-2006 by Texas Instruments
4 * Copyright (C) 2006 by Nokia Corporation
6 * This file is part of the Inventra Controller Driver for Linux.
8 * The Inventra Controller Driver for Linux is free software; you
9 * can redistribute it and/or modify it under the terms of the GNU
10 * General Public License version 2 as published by the Free Software
13 * The Inventra Controller Driver for Linux is distributed in
14 * the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 * License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with The Inventra Controller Driver for Linux ; if not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307 USA
24 * ANY DOWNLOAD, USE, REPRODUCTION, MODIFICATION OR DISTRIBUTION
25 * OF THIS DRIVER INDICATES YOUR COMPLETE AND UNCONDITIONAL ACCEPTANCE
26 * OF THOSE TERMS.THIS DRIVER IS PROVIDED "AS IS" AND MENTOR GRAPHICS
27 * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, RELATED TO THIS DRIVER.
28 * MENTOR GRAPHICS SPECIFICALLY DISCLAIMS ALL IMPLIED WARRANTIES
29 * OF MERCHANTABILITY; FITNESS FOR A PARTICULAR PURPOSE AND
30 * NON-INFRINGEMENT. MENTOR GRAPHICS DOES NOT PROVIDE SUPPORT
31 * SERVICES OR UPDATES FOR THIS DRIVER, EVEN IF YOU ARE A MENTOR
32 * GRAPHICS SUPPORT CUSTOMER.
33 ******************************************************************/
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/time.h>
42 #include <linux/timer.h>
44 #include <asm/unaligned.h>
49 static void musb_port_suspend(struct musb *musb, u8 bSuspend)
52 void __iomem *pBase = musb->pRegs;
54 if (!is_host_active(musb))
57 /* NOTE: this doesn't necessarily put PHY into low power mode,
58 * turning off its clock; that's a function of PHY integration and
59 * MGC_M_POWER_ENSUSPEND. PHY may need a clock (sigh) to detect
60 * SE0 changing to connect (J) or wakeup (K) states.
62 power = musb_readb(pBase, MGC_O_HDRC_POWER);
64 power &= ~MGC_M_POWER_RESUME;
65 power |= MGC_M_POWER_SUSPENDM;
66 musb_writeb(pBase, MGC_O_HDRC_POWER, power);
68 DBG(3, "Root port suspended, power %02x\n", power);
70 musb->port1_status |= USB_PORT_STAT_SUSPEND;
71 switch (musb->xceiv.state) {
72 case OTG_STATE_A_HOST:
73 musb->xceiv.state = OTG_STATE_A_SUSPEND;
74 musb->is_active = is_otg_enabled(musb)
75 && musb->xceiv.host->b_hnp_enable;
76 musb_platform_try_idle(musb);
78 case OTG_STATE_B_HOST:
79 musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
81 /* REVISIT restore setting of MGC_M_DEVCTL_HR */
84 DBG(1, "bogus rh suspend? %s\n",
85 otg_state_string(musb));
87 } else if (power & MGC_M_POWER_SUSPENDM) {
88 power &= ~MGC_M_POWER_SUSPENDM;
89 power |= MGC_M_POWER_RESUME;
90 musb_writeb(pBase, MGC_O_HDRC_POWER, power);
92 DBG(3, "Root port resuming, power %02x\n", power);
94 /* later, GetPortStatus will stop RESUME signaling */
95 musb->port1_status |= MUSB_PORT_STAT_RESUME;
96 musb->rh_timer = jiffies + msecs_to_jiffies(20);
100 static void musb_port_reset(struct musb *musb, u8 bReset)
103 void __iomem *pBase = musb->pRegs;
105 #ifdef CONFIG_USB_MUSB_OTG
106 /* REVISIT this looks wrong for HNP */
107 u8 devctl = musb_readb(pBase, MGC_O_HDRC_DEVCTL);
109 if (musb->bDelayPortPowerOff || !(devctl & MGC_M_DEVCTL_HM)) {
114 if (!is_host_active(musb))
117 /* NOTE: caller guarantees it will turn off the reset when
118 * the appropriate amount of time has passed
120 power = musb_readb(pBase, MGC_O_HDRC_POWER);
122 musb->bIgnoreDisconnect = TRUE;
124 musb_writeb(pBase, MGC_O_HDRC_POWER,
125 power | MGC_M_POWER_RESET);
127 musb->port1_status |= USB_PORT_STAT_RESET;
128 musb->port1_status &= ~USB_PORT_STAT_ENABLE;
129 musb->rh_timer = jiffies + msecs_to_jiffies(50);
131 DBG(4, "root port reset stopped\n");
132 musb_writeb(pBase, MGC_O_HDRC_POWER,
133 power & ~MGC_M_POWER_RESET);
135 musb->bIgnoreDisconnect = FALSE;
137 power = musb_readb(pBase, MGC_O_HDRC_POWER);
138 if (power & MGC_M_POWER_HSMODE) {
139 DBG(4, "high-speed device connected\n");
140 musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
143 musb->port1_status &= ~USB_PORT_STAT_RESET;
144 musb->port1_status |= USB_PORT_STAT_ENABLE
145 | (USB_PORT_STAT_C_RESET << 16)
146 | (USB_PORT_STAT_C_ENABLE << 16);
147 usb_hcd_poll_rh_status(musb_to_hcd(musb));
149 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
153 void musb_root_disconnect(struct musb *musb)
155 musb->port1_status = (1 << USB_PORT_FEAT_POWER)
156 | (1 << USB_PORT_FEAT_C_CONNECTION);
158 usb_hcd_poll_rh_status(musb_to_hcd(musb));
161 switch (musb->xceiv.state) {
162 case OTG_STATE_A_HOST:
163 case OTG_STATE_A_SUSPEND:
164 musb->xceiv.state = OTG_STATE_A_WAIT_BCON;
166 case OTG_STATE_A_WAIT_VFALL:
167 musb->xceiv.state = OTG_STATE_B_IDLE;
170 DBG(1, "host disconnect (%s)\n", otg_state_string(musb));
175 /*---------------------------------------------------------------------*/
177 int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
179 struct musb *musb = hcd_to_musb(hcd);
182 /* called in_irq() via usb_hcd_poll_rh_status() */
183 if (musb->port1_status & 0xffff0000) {
190 int musb_hub_control(
198 struct musb *musb = hcd_to_musb(hcd);
203 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
206 /* hub features: always zero, setting is a NOP
207 * port features: reported, sometimes updated when host is active
210 spin_lock_irqsave(&musb->Lock, flags);
212 case ClearHubFeature:
215 case C_HUB_OVER_CURRENT:
216 case C_HUB_LOCAL_POWER:
222 case ClearPortFeature:
227 case USB_PORT_FEAT_ENABLE:
229 case USB_PORT_FEAT_SUSPEND:
230 musb_port_suspend(musb, FALSE);
232 case USB_PORT_FEAT_POWER:
233 if (!(is_otg_enabled(musb) && hcd->self.is_b_host))
234 musb_set_vbus(musb, 0);
236 case USB_PORT_FEAT_C_CONNECTION:
237 case USB_PORT_FEAT_C_ENABLE:
238 case USB_PORT_FEAT_C_OVER_CURRENT:
239 case USB_PORT_FEAT_C_RESET:
240 case USB_PORT_FEAT_C_SUSPEND:
245 DBG(5, "clear feature %d\n", wValue);
246 musb->port1_status &= ~(1 << wValue);
248 case GetHubDescriptor:
250 struct usb_hub_descriptor *desc = (void *)buf;
252 desc->bDescLength = 9;
253 desc->bDescriptorType = 0x29;
255 desc->wHubCharacteristics = __constant_cpu_to_le16(
256 0x0001 /* per-port power switching */
257 | 0x0010 /* no overcurrent reporting */
259 desc->bPwrOn2PwrGood = 5; /* msec/2 */
260 desc->bHubContrCurrent = 0;
262 /* workaround bogus struct definition */
263 desc->DeviceRemovable[0] = 0x02; /* port 1 */
264 desc->DeviceRemovable[1] = 0xff;
269 *(__le32 *) buf = cpu_to_le32 (temp);
275 /* finish RESET signaling? */
276 if ((musb->port1_status & USB_PORT_STAT_RESET)
277 && time_after(jiffies, musb->rh_timer))
278 musb_port_reset(musb, FALSE);
280 /* finish RESUME signaling? */
281 if ((musb->port1_status & MUSB_PORT_STAT_RESUME)
282 && time_after(jiffies, musb->rh_timer)) {
285 power = musb_readb(musb->pRegs, MGC_O_HDRC_POWER);
286 power &= ~MGC_M_POWER_RESUME;
287 DBG(4, "root port resume stopped, power %02x\n",
289 musb_writeb(musb->pRegs, MGC_O_HDRC_POWER, power);
291 /* ISSUE: DaVinci (RTL 1.300) disconnects after
292 * resume of high speed peripherals (but not full
297 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
298 | MUSB_PORT_STAT_RESUME);
299 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
300 usb_hcd_poll_rh_status(musb_to_hcd(musb));
301 /* NOTE: it might really be A_WAIT_BCON ... */
302 musb->xceiv.state = OTG_STATE_A_HOST;
305 put_unaligned(cpu_to_le32(musb->port1_status & ~MUSB_PORT_STAT_RESUME),
308 /* port change status is more interesting */
309 DBG(get_unaligned((u16*)(buf+2)) ? 2 : 5, "port status %08x\n",
313 if ((wIndex & 0xff) != 1)
317 case USB_PORT_FEAT_POWER:
318 /* NOTE: this controller has a strange state machine
319 * that involves "requesting sessions" according to
320 * magic side effects from incompletely-described
321 * rules about startup...
323 * This call is what really starts the host mode; be
324 * very careful about side effects if you reorder any
325 * initialization logic, e.g. for OTG, or change any
326 * logic relating to VBUS power-up.
328 if (!(is_otg_enabled(musb) && hcd->self.is_b_host))
331 case USB_PORT_FEAT_RESET:
332 musb_port_reset(musb, TRUE);
334 case USB_PORT_FEAT_SUSPEND:
335 musb_port_suspend(musb, TRUE);
337 case USB_PORT_FEAT_TEST:
338 if (unlikely(is_host_active(musb)))
344 pr_debug("TEST_J\n");
348 pr_debug("TEST_K\n");
352 pr_debug("TEST_SE0_NAK\n");
353 temp = MGC_M_TEST_SE0_NAK;
356 pr_debug("TEST_PACKET\n");
357 temp = MGC_M_TEST_PACKET;
358 musb_load_testpacket(musb);
361 pr_debug("TEST_FORCE_ENABLE\n");
362 temp = MGC_M_TEST_FORCE_HOST
363 | MGC_M_TEST_FORCE_HS;
365 /* FIXME and enable a session too */
370 musb_writeb(musb->pRegs, MGC_O_HDRC_TESTMODE, temp);
375 DBG(5, "set feature %d\n", wValue);
376 musb->port1_status |= 1 << wValue;
381 /* "protocol stall" on error */
384 spin_unlock_irqrestore(&musb->Lock, flags);