2 * ADM5120 HCD (Host Controller Driver) for USB
4 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
6 * This file was derived from: drivers/usb/host/ohci-hub.c
7 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
8 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
10 * This file is licenced under the GPL.
13 /*-------------------------------------------------------------------------*/
16 * ADM5120 Root Hub ... the nonsharable stuff
19 #define dbg_port(hc,label,num,value) \
22 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
24 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
25 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
26 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
27 (value & ADMHC_PS_PESC) ? " PESC" : "", \
28 (value & ADMHC_PS_CSC) ? " CSC" : "", \
30 (value & ADMHC_PS_LSDA) ? " LSDA" : "", \
31 (value & ADMHC_PS_PPS) ? " PPS" : "", \
32 (value & ADMHC_PS_PRS) ? " PRS" : "", \
33 (value & ADMHC_PS_POCI) ? " POCI" : "", \
34 (value & ADMHC_PS_PSS) ? " PSS" : "", \
36 (value & ADMHC_PS_PES) ? " PES" : "", \
37 (value & ADMHC_PS_CCS) ? " CCS" : "" \
40 #define dbg_port_write(hc,label,num,value) \
43 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
45 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
46 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
47 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
48 (value & ADMHC_PS_PESC) ? " PESC" : "", \
49 (value & ADMHC_PS_CSC) ? " CSC" : "", \
51 (value & ADMHC_PS_CPP) ? " CPP" : "", \
52 (value & ADMHC_PS_SPP) ? " SPP" : "", \
53 (value & ADMHC_PS_SPR) ? " SPR" : "", \
54 (value & ADMHC_PS_CPS) ? " CPS" : "", \
55 (value & ADMHC_PS_SPS) ? " SPS" : "", \
57 (value & ADMHC_PS_SPE) ? " SPE" : "", \
58 (value & ADMHC_PS_CPE) ? " CPE" : "" \
61 /*-------------------------------------------------------------------------*/
63 /* hcd->hub_irq_enable() */
64 static void admhc_hub_irq_enable(struct usb_hcd
*hcd
)
66 struct admhcd
*ahcd
= hcd_to_admhcd(hcd
);
68 spin_lock_irq(&ahcd
->lock
);
70 del_timer(&hcd
->rh_timer
); /* Prevent next poll */
71 admhc_intr_enable(ahcd
, ADMHC_INTR_INSM
);
72 spin_unlock_irq(&ahcd
->lock
);
75 /*-------------------------------------------------------------------------*/
77 /* build "status change" packet (one or two bytes) from HC registers */
80 admhc_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
82 struct admhcd
*ahcd
= hcd_to_admhcd(hcd
);
83 int i
, changed
= 0, length
= 1;
84 int any_connected
= 0;
88 spin_lock_irqsave(&ahcd
->lock
, flags
);
89 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))
93 status
= admhc_read_rhdesc(ahcd
);
94 if (status
& (ADMHC_RH_LPSC
| ADMHC_RH_OCIC
))
95 buf
[0] = changed
= 1;
98 if (ahcd
->num_ports
> 7) {
103 /* look at each port */
104 for (i
= 0; i
< ahcd
->num_ports
; i
++) {
105 status
= admhc_read_portstatus(ahcd
, i
);
107 /* can't autostop if ports are connected */
108 any_connected
|= (status
& ADMHC_PS_CCS
);
110 if (status
& (ADMHC_PS_CSC
| ADMHC_PS_PESC
| ADMHC_PS_PSSC
111 | ADMHC_PS_OCIC
| ADMHC_PS_PRSC
)) {
114 buf
[0] |= 1 << (i
+ 1);
116 buf
[1] |= 1 << (i
- 7);
120 hcd
->poll_rh
= admhc_root_hub_state_changes(ahcd
, changed
,
124 spin_unlock_irqrestore(&ahcd
->lock
, flags
);
126 return changed
? length
: 0;
129 /*-------------------------------------------------------------------------*/
131 static int admhc_get_hub_descriptor(struct admhcd
*ahcd
, char *buf
)
133 struct usb_hub_descriptor
*desc
= (struct usb_hub_descriptor
*)buf
;
134 u32 rh
= admhc_read_rhdesc(ahcd
);
137 desc
->bDescriptorType
= USB_DT_HUB
; /* Hub-descriptor */
138 desc
->bPwrOn2PwrGood
= ADMHC_POTPGT
/2; /* use default value */
139 desc
->bHubContrCurrent
= 0x00; /* 0mA */
141 desc
->bNbrPorts
= ahcd
->num_ports
;
142 temp
= 1 + (ahcd
->num_ports
/ 8);
143 desc
->bDescLength
= USB_DT_HUB_NONVAR_SIZE
+ 2 * temp
;
147 if (rh
& ADMHC_RH_NPS
) /* no power switching? */
149 if (rh
& ADMHC_RH_PSM
) /* per-port power switching? */
151 if (rh
& ADMHC_RH_NOCP
) /* no overcurrent reporting? */
153 else if (rh
& ADMHC_RH_OCPM
) /* per-port overcurrent reporting? */
155 desc
->wHubCharacteristics
= (__force __u16
)cpu_to_hc16(ahcd
, temp
);
157 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
159 desc
->bitmap
[0] = ~0;
164 static int admhc_get_hub_status(struct admhcd
*ahcd
, char *buf
)
166 struct usb_hub_status
*hs
= (struct usb_hub_status
*)buf
;
167 u32 t
= admhc_read_rhdesc(ahcd
);
171 status
|= (t
& ADMHC_RH_LPS
) ? HUB_STATUS_LOCAL_POWER
: 0;
172 status
|= (t
& ADMHC_RH_OCI
) ? HUB_STATUS_OVERCURRENT
: 0;
175 change
|= (t
& ADMHC_RH_LPSC
) ? HUB_CHANGE_LOCAL_POWER
: 0;
176 change
|= (t
& ADMHC_RH_OCIC
) ? HUB_CHANGE_OVERCURRENT
: 0;
178 hs
->wHubStatus
= (__force __u16
)cpu_to_hc16(ahcd
, status
);
179 hs
->wHubChange
= (__force __u16
)cpu_to_hc16(ahcd
, change
);
184 static int admhc_get_port_status(struct admhcd
*ahcd
, unsigned port
, char *buf
)
186 struct usb_port_status
*ps
= (struct usb_port_status
*)buf
;
187 u32 t
= admhc_read_portstatus(ahcd
, port
);
191 status
|= (t
& ADMHC_PS_CCS
) ? USB_PORT_STAT_CONNECTION
: 0;
192 status
|= (t
& ADMHC_PS_PES
) ? USB_PORT_STAT_ENABLE
: 0;
193 status
|= (t
& ADMHC_PS_PSS
) ? USB_PORT_STAT_SUSPEND
: 0;
194 status
|= (t
& ADMHC_PS_POCI
) ? USB_PORT_STAT_OVERCURRENT
: 0;
195 status
|= (t
& ADMHC_PS_PRS
) ? USB_PORT_STAT_RESET
: 0;
196 status
|= (t
& ADMHC_PS_PPS
) ? USB_PORT_STAT_POWER
: 0;
197 status
|= (t
& ADMHC_PS_LSDA
) ? USB_PORT_STAT_LOW_SPEED
: 0;
200 change
|= (t
& ADMHC_PS_CSC
) ? USB_PORT_STAT_C_CONNECTION
: 0;
201 change
|= (t
& ADMHC_PS_PESC
) ? USB_PORT_STAT_C_ENABLE
: 0;
202 change
|= (t
& ADMHC_PS_PSSC
) ? USB_PORT_STAT_C_SUSPEND
: 0;
203 change
|= (t
& ADMHC_PS_OCIC
) ? USB_PORT_STAT_C_OVERCURRENT
: 0;
204 change
|= (t
& ADMHC_PS_PRSC
) ? USB_PORT_STAT_C_RESET
: 0;
206 ps
->wPortStatus
= (__force __u16
)cpu_to_hc16(ahcd
, status
);
207 ps
->wPortChange
= (__force __u16
)cpu_to_hc16(ahcd
, change
);
212 /*-------------------------------------------------------------------------*/
214 #ifdef CONFIG_USB_OTG
216 static int admhc_start_port_reset(struct usb_hcd
*hcd
, unsigned port
)
218 struct admhcd
*ahcd
= hcd_to_admhcd(hcd
);
225 /* start port reset before HNP protocol times out */
226 status
= admhc_read_portstatus(ahcd
, port
);
227 if (!(status
& ADMHC_PS_CCS
))
230 /* khubd will finish the reset later */
231 admhc_write_portstatus(ahcd
, port
, ADMHC_PS_PRS
);
235 static void start_hnp(struct admhcd
*ahcd
);
239 #define admhc_start_port_reset NULL
243 /*-------------------------------------------------------------------------*/
246 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
247 * not necessarily continuous ... to guard against resume signaling.
248 * The short timeout is safe for non-root hubs, and is backward-compatible
249 * with earlier Linux hosts.
251 #ifdef CONFIG_USB_SUSPEND
252 #define PORT_RESET_MSEC 50
254 #define PORT_RESET_MSEC 10
257 /* this timer value might be vendor-specific ... */
258 #define PORT_RESET_HW_MSEC 10
260 /* wrap-aware logic morphed from <linux/jiffies.h> */
261 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
263 /* called from some task, normally khubd */
264 static inline int admhc_port_reset(struct admhcd
*ahcd
, unsigned port
)
268 admhc_vdbg(ahcd
, "reset port%d\n", port
);
269 t
= admhc_read_portstatus(ahcd
, port
);
270 if (!(t
& ADMHC_PS_CCS
))
273 admhc_write_portstatus(ahcd
, port
, ADMHC_PS_SPR
);
275 admhc_write_portstatus(ahcd
, port
, (ADMHC_PS_SPE
| ADMHC_PS_CSC
));
281 static inline int admhc_port_enable(struct admhcd
*ahcd
, unsigned port
)
285 admhc_vdbg(ahcd
, "enable port%d\n", port
);
286 t
= admhc_read_portstatus(ahcd
, port
);
287 if (!(t
& ADMHC_PS_CCS
))
290 admhc_write_portstatus(ahcd
, port
, ADMHC_PS_SPE
);
295 static inline int admhc_port_disable(struct admhcd
*ahcd
, unsigned port
)
299 admhc_vdbg(ahcd
, "disable port%d\n", port
);
300 t
= admhc_read_portstatus(ahcd
, port
);
301 if (!(t
& ADMHC_PS_CCS
))
304 admhc_write_portstatus(ahcd
, port
, ADMHC_PS_CPE
);
309 static inline int admhc_port_write(struct admhcd
*ahcd
, unsigned port
,
312 #ifdef ADMHC_VERBOSE_DEBUG
313 dbg_port_write(ahcd
, "write", port
, val
);
315 admhc_write_portstatus(ahcd
, port
, val
);
320 static int admhc_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
321 u16 wIndex
, char *buf
, u16 wLength
)
323 struct admhcd
*ahcd
= hcd_to_admhcd(hcd
);
324 int ports
= hcd_to_bus(hcd
)->root_hub
->maxchild
;
327 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
)))
331 case ClearHubFeature
:
333 case C_HUB_OVER_CURRENT
:
335 admhc_writel(ahcd
, ADMHC_RH_OCIC
,
336 &ahcd
->regs
->roothub
.status
);
338 case C_HUB_LOCAL_POWER
:
344 case ClearPortFeature
:
345 if (!wIndex
|| wIndex
> ports
)
350 case USB_PORT_FEAT_ENABLE
:
351 ret
= admhc_port_disable(ahcd
, wIndex
);
353 case USB_PORT_FEAT_SUSPEND
:
354 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_CPS
);
356 case USB_PORT_FEAT_POWER
:
357 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_CPP
);
359 case USB_PORT_FEAT_C_CONNECTION
:
360 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_CSC
);
362 case USB_PORT_FEAT_C_ENABLE
:
363 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_PESC
);
365 case USB_PORT_FEAT_C_SUSPEND
:
366 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_PSSC
);
368 case USB_PORT_FEAT_C_OVER_CURRENT
:
369 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_OCIC
);
371 case USB_PORT_FEAT_C_RESET
:
372 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_PRSC
);
378 case GetHubDescriptor
:
379 ret
= admhc_get_hub_descriptor(ahcd
, buf
);
382 ret
= admhc_get_hub_status(ahcd
, buf
);
385 if (!wIndex
|| wIndex
> ports
)
389 ret
= admhc_get_port_status(ahcd
, wIndex
, buf
);
393 case C_HUB_OVER_CURRENT
:
394 /* FIXME: this can be cleared, yes? */
395 case C_HUB_LOCAL_POWER
:
402 if (!wIndex
|| wIndex
> ports
)
407 case USB_PORT_FEAT_ENABLE
:
408 ret
= admhc_port_enable(ahcd
, wIndex
);
410 case USB_PORT_FEAT_RESET
:
411 ret
= admhc_port_reset(ahcd
, wIndex
);
413 case USB_PORT_FEAT_SUSPEND
:
414 #ifdef CONFIG_USB_OTG
415 if (hcd
->self
.otg_port
== (wIndex
+ 1)
416 && hcd
->self
.b_hnp_enable
)
420 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_SPS
);
422 case USB_PORT_FEAT_POWER
:
423 ret
= admhc_port_write(ahcd
, wIndex
, ADMHC_PS_SPP
);
432 /* "protocol stall" on error */