blob: cf0335c7320feac75f55b5d92364444fb75f4bb3 [file] [log] [blame]
wdenkde887eb2003-09-10 18:20:28 +00001/*
2 * URB OHCI HCD (Host Controller Driver) for USB on the S3C2400.
3 *
4 * (C) Copyright 2003
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +02005 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
wdenkde887eb2003-09-10 18:20:28 +00006 *
wdenkbfad55d2005-03-14 23:56:42 +00007 * Note: Much of this code has been derived from Linux 2.4
8 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
9 * (C) Copyright 2000-2002 David Brownell
10 *
wdenkde887eb2003-09-10 18:20:28 +000011 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 *
wdenkde887eb2003-09-10 18:20:28 +000029 */
30/*
31 * IMPORTANT NOTES
Mike Frysingercc93fc02009-01-01 18:27:27 -050032 * 1 - this driver is intended for use with USB Mass Storage Devices
wdenkde887eb2003-09-10 18:20:28 +000033 * (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
34 */
35
36#include <common.h>
wdenk4ea537d2003-12-07 18:32:37 +000037/* #include <pci.h> no PCI on the S3C24X0 */
wdenkde887eb2003-09-10 18:20:28 +000038
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090039#if defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0)
wdenkde887eb2003-09-10 18:20:28 +000040
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090041#include <asm/arch/s3c24x0_cpu.h>
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090042#include <asm/io.h>
wdenkde887eb2003-09-10 18:20:28 +000043#include <malloc.h>
44#include <usb.h>
45#include "usb_ohci.h"
46
47#define OHCI_USE_NPS /* force NoPowerSwitching mode */
48#undef OHCI_VERBOSE_DEBUG /* not always helpful */
49
50
51/* For initializing controller (mask in an HCFS mode too) */
52#define OHCI_CONTROL_INIT \
53 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
54
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090055#define min_t(type, x, y) \
56 ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
wdenkde887eb2003-09-10 18:20:28 +000057
58#undef DEBUG
59#ifdef DEBUG
60#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
61#else
62#define dbg(format, arg...) do {} while(0)
63#endif /* DEBUG */
64#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
65#undef SHOW_INFO
66#ifdef SHOW_INFO
67#define info(format, arg...) printf("INFO: " format "\n", ## arg)
68#else
69#define info(format, arg...) do {} while(0)
70#endif
71
72#define m16_swap(x) swap_16(x)
73#define m32_swap(x) swap_32(x)
74
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090075/* global struct ohci */
76static struct ohci gohci;
wdenkde887eb2003-09-10 18:20:28 +000077/* this must be aligned to a 256 byte boundary */
78struct ohci_hcca ghcca[1];
79/* a pointer to the aligned storage */
80struct ohci_hcca *phcca;
81/* this allocates EDs for all possible endpoints */
82struct ohci_device ohci_dev;
83/* urb_priv */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090084struct urb_priv urb_priv;
dzu8d7e4d12003-09-29 21:55:54 +000085/* RHSC flag */
86int got_rhsc;
87/* device which was disconnected */
88struct usb_device *devgone;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +020089/* flag guarding URB transation */
90int urb_finished = 0;
wdenkde887eb2003-09-10 18:20:28 +000091
92/*-------------------------------------------------------------------------*/
93
94/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
95 * The erratum (#4) description is incorrect. AMD's workaround waits
96 * till some bits (mostly reserved) are clear; ok for all revs.
97 */
98#define OHCI_QUIRK_AMD756 0xabcd
99#define read_roothub(hc, register, mask) ({ \
100 u32 temp = readl (&hc->regs->roothub.register); \
101 if (hc->flags & OHCI_QUIRK_AMD756) \
102 while (temp & mask) \
103 temp = readl (&hc->regs->roothub.register); \
104 temp; })
105
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900106static u32 roothub_a(struct ohci *hc)
107{
108 return read_roothub(hc, a, 0xfc0fe000);
109}
110static inline u32 roothub_b(struct ohci *hc)
111{
112 return readl(&hc->regs->roothub.b);
113}
114static inline u32 roothub_status(struct ohci *hc)
115{
116 return readl(&hc->regs->roothub.status);
117}
118static u32 roothub_portstatus(struct ohci *hc, int i)
119{
120 return read_roothub(hc, portstatus[i], 0xffe0fce0);
121}
wdenkde887eb2003-09-10 18:20:28 +0000122
123/* forward declaration */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900124static int hc_interrupt(void);
125static void td_submit_job(struct usb_device *dev, unsigned long pipe,
126 void *buffer, int transfer_len,
127 struct devrequest *setup, struct urb_priv *urb,
128 int interval);
wdenkde887eb2003-09-10 18:20:28 +0000129
130/*-------------------------------------------------------------------------*
131 * URB support functions
132 *-------------------------------------------------------------------------*/
133
134/* free HCD-private data associated with this URB */
135
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900136static void urb_free_priv(struct urb_priv *urb)
wdenkde887eb2003-09-10 18:20:28 +0000137{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900138 int i;
139 int last;
140 struct td *td;
wdenkde887eb2003-09-10 18:20:28 +0000141
142 last = urb->length - 1;
143 if (last >= 0) {
144 for (i = 0; i <= last; i++) {
145 td = urb->td[i];
146 if (td) {
147 td->usb_dev = NULL;
148 urb->td[i] = NULL;
149 }
150 }
151 }
152}
153
154/*-------------------------------------------------------------------------*/
155
156#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900157static int sohci_get_current_frame_number(struct usb_device *dev);
wdenkde887eb2003-09-10 18:20:28 +0000158
159/* debug| print the main components of an URB
160 * small: 0) header + data packets 1) just header */
161
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900162static void pkt_print(struct usb_device *dev, unsigned long pipe, void *buffer,
163 int transfer_len, struct devrequest *setup, char *str,
164 int small)
wdenkde887eb2003-09-10 18:20:28 +0000165{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900166 struct urb_priv *purb = &urb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000167
168 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900169 str,
170 sohci_get_current_frame_number(dev),
171 usb_pipedevice(pipe),
172 usb_pipeendpoint(pipe),
173 usb_pipeout(pipe) ? 'O' : 'I',
174 usb_pipetype(pipe) < 2 ?
175 (usb_pipeint(pipe) ? "INTR" : "ISOC") :
176 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"),
177 purb->actual_length, transfer_len, dev->status);
wdenkde887eb2003-09-10 18:20:28 +0000178#ifdef OHCI_VERBOSE_DEBUG
179 if (!small) {
180 int i, len;
181
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900182 if (usb_pipecontrol(pipe)) {
183 printf(__FILE__ ": cmd(8):");
184 for (i = 0; i < 8; i++)
185 printf(" %02x", ((__u8 *) setup)[i]);
186 printf("\n");
wdenkde887eb2003-09-10 18:20:28 +0000187 }
188 if (transfer_len > 0 && buffer) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900189 printf(__FILE__ ": data(%d/%d):",
190 purb->actual_length, transfer_len);
191 len = usb_pipeout(pipe) ?
192 transfer_len : purb->actual_length;
wdenkde887eb2003-09-10 18:20:28 +0000193 for (i = 0; i < 16 && i < len; i++)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900194 printf(" %02x", ((__u8 *) buffer)[i]);
195 printf("%s\n", i < len ? "..." : "");
wdenkde887eb2003-09-10 18:20:28 +0000196 }
197 }
198#endif
199}
200
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900201/* just for debugging; prints non-empty branches of the
202 int ed tree inclusive iso eds*/
203void ep_print_int_eds(struct ohci *ohci, char *str)
204{
wdenkde887eb2003-09-10 18:20:28 +0000205 int i, j;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900206 __u32 *ed_p;
207 for (i = 0; i < 32; i++) {
wdenkde887eb2003-09-10 18:20:28 +0000208 j = 5;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900209 ed_p = &(ohci->hcca->int_table[i]);
wdenkde887eb2003-09-10 18:20:28 +0000210 if (*ed_p == 0)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900211 continue;
212 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
wdenkde887eb2003-09-10 18:20:28 +0000213 while (*ed_p != 0 && j--) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900214 struct ed *ed = (struct ed *) m32_swap(ed_p);
215 printf(" ed: %4x;", ed->hwINFO);
wdenkde887eb2003-09-10 18:20:28 +0000216 ed_p = &ed->hwNextED;
217 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900218 printf("\n");
wdenkde887eb2003-09-10 18:20:28 +0000219 }
220}
221
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900222static void ohci_dump_intr_mask(char *label, __u32 mask)
wdenkde887eb2003-09-10 18:20:28 +0000223{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900224 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
225 label,
226 mask,
227 (mask & OHCI_INTR_MIE) ? " MIE" : "",
228 (mask & OHCI_INTR_OC) ? " OC" : "",
229 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
230 (mask & OHCI_INTR_FNO) ? " FNO" : "",
231 (mask & OHCI_INTR_UE) ? " UE" : "",
232 (mask & OHCI_INTR_RD) ? " RD" : "",
233 (mask & OHCI_INTR_SF) ? " SF" : "",
234 (mask & OHCI_INTR_WDH) ? " WDH" : "",
235 (mask & OHCI_INTR_SO) ? " SO" : "");
wdenkde887eb2003-09-10 18:20:28 +0000236}
237
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900238static void maybe_print_eds(char *label, __u32 value)
wdenkde887eb2003-09-10 18:20:28 +0000239{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900240 struct ed *edp = (struct ed *) value;
wdenkde887eb2003-09-10 18:20:28 +0000241
242 if (value) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900243 dbg("%s %08x", label, value);
244 dbg("%08x", edp->hwINFO);
245 dbg("%08x", edp->hwTailP);
246 dbg("%08x", edp->hwHeadP);
247 dbg("%08x", edp->hwNextED);
wdenkde887eb2003-09-10 18:20:28 +0000248 }
249}
250
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900251static char *hcfs2string(int state)
wdenkde887eb2003-09-10 18:20:28 +0000252{
253 switch (state) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900254 case OHCI_USB_RESET:
255 return "reset";
256 case OHCI_USB_RESUME:
257 return "resume";
258 case OHCI_USB_OPER:
259 return "operational";
260 case OHCI_USB_SUSPEND:
261 return "suspend";
wdenkde887eb2003-09-10 18:20:28 +0000262 }
263 return "?";
264}
265
266/* dump control and status registers */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900267static void ohci_dump_status(struct ohci *controller)
wdenkde887eb2003-09-10 18:20:28 +0000268{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900269 struct ohci_regs *regs = controller->regs;
270 __u32 temp;
wdenkde887eb2003-09-10 18:20:28 +0000271
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900272 temp = readl(&regs->revision) & 0xff;
wdenkde887eb2003-09-10 18:20:28 +0000273 if (temp != 0x10)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900274 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
wdenkde887eb2003-09-10 18:20:28 +0000275
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900276 temp = readl(&regs->control);
277 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
278 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
279 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
280 (temp & OHCI_CTRL_IR) ? " IR" : "",
281 hcfs2string(temp & OHCI_CTRL_HCFS),
282 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
283 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
284 (temp & OHCI_CTRL_IE) ? " IE" : "",
285 (temp & OHCI_CTRL_PLE) ? " PLE" : "", temp & OHCI_CTRL_CBSR);
wdenkde887eb2003-09-10 18:20:28 +0000286
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900287 temp = readl(&regs->cmdstatus);
288 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
289 (temp & OHCI_SOC) >> 16,
290 (temp & OHCI_OCR) ? " OCR" : "",
291 (temp & OHCI_BLF) ? " BLF" : "",
292 (temp & OHCI_CLF) ? " CLF" : "", (temp & OHCI_HCR) ? " HCR" : "");
wdenkde887eb2003-09-10 18:20:28 +0000293
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900294 ohci_dump_intr_mask("intrstatus", readl(&regs->intrstatus));
295 ohci_dump_intr_mask("intrenable", readl(&regs->intrenable));
wdenkde887eb2003-09-10 18:20:28 +0000296
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900297 maybe_print_eds("ed_periodcurrent", readl(&regs->ed_periodcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000298
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900299 maybe_print_eds("ed_controlhead", readl(&regs->ed_controlhead));
300 maybe_print_eds("ed_controlcurrent", readl(&regs->ed_controlcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000301
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900302 maybe_print_eds("ed_bulkhead", readl(&regs->ed_bulkhead));
303 maybe_print_eds("ed_bulkcurrent", readl(&regs->ed_bulkcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000304
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900305 maybe_print_eds("donehead", readl(&regs->donehead));
wdenkde887eb2003-09-10 18:20:28 +0000306}
307
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900308static void ohci_dump_roothub(struct ohci *controller, int verbose)
wdenkde887eb2003-09-10 18:20:28 +0000309{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900310 __u32 temp, ndp, i;
wdenkde887eb2003-09-10 18:20:28 +0000311
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900312 temp = roothub_a(controller);
wdenkde887eb2003-09-10 18:20:28 +0000313 ndp = (temp & RH_A_NDP);
314
315 if (verbose) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900316 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
317 ((temp & RH_A_POTPGT) >> 24) & 0xff,
318 (temp & RH_A_NOCP) ? " NOCP" : "",
319 (temp & RH_A_OCPM) ? " OCPM" : "",
320 (temp & RH_A_DT) ? " DT" : "",
321 (temp & RH_A_NPS) ? " NPS" : "",
322 (temp & RH_A_PSM) ? " PSM" : "", ndp);
323 temp = roothub_b(controller);
324 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
325 temp, (temp & RH_B_PPCM) >> 16, (temp & RH_B_DR)
326 );
327 temp = roothub_status(controller);
328 dbg("roothub.status: %08x%s%s%s%s%s%s",
329 temp,
330 (temp & RH_HS_CRWE) ? " CRWE" : "",
331 (temp & RH_HS_OCIC) ? " OCIC" : "",
332 (temp & RH_HS_LPSC) ? " LPSC" : "",
333 (temp & RH_HS_DRWE) ? " DRWE" : "",
334 (temp & RH_HS_OCI) ? " OCI" : "",
335 (temp & RH_HS_LPS) ? " LPS" : "");
wdenkde887eb2003-09-10 18:20:28 +0000336 }
337
338 for (i = 0; i < ndp; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900339 temp = roothub_portstatus(controller, i);
340 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
341 i,
342 temp,
343 (temp & RH_PS_PRSC) ? " PRSC" : "",
344 (temp & RH_PS_OCIC) ? " OCIC" : "",
345 (temp & RH_PS_PSSC) ? " PSSC" : "",
346 (temp & RH_PS_PESC) ? " PESC" : "",
347 (temp & RH_PS_CSC) ? " CSC" : "",
348 (temp & RH_PS_LSDA) ? " LSDA" : "",
349 (temp & RH_PS_PPS) ? " PPS" : "",
350 (temp & RH_PS_PRS) ? " PRS" : "",
351 (temp & RH_PS_POCI) ? " POCI" : "",
352 (temp & RH_PS_PSS) ? " PSS" : "",
353 (temp & RH_PS_PES) ? " PES" : "",
354 (temp & RH_PS_CCS) ? " CCS" : "");
wdenkde887eb2003-09-10 18:20:28 +0000355 }
356}
357
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900358static void ohci_dump(struct ohci *controller, int verbose)
wdenkde887eb2003-09-10 18:20:28 +0000359{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900360 dbg("OHCI controller usb-%s state", controller->slot_name);
wdenkde887eb2003-09-10 18:20:28 +0000361
362 /* dumps some of the state we know about */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900363 ohci_dump_status(controller);
wdenkde887eb2003-09-10 18:20:28 +0000364 if (verbose)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900365 ep_print_int_eds(controller, "hcca");
366 dbg("hcca frame #%04x", controller->hcca->frame_no);
367 ohci_dump_roothub(controller, 1);
wdenkde887eb2003-09-10 18:20:28 +0000368}
369
wdenkde887eb2003-09-10 18:20:28 +0000370#endif /* DEBUG */
371
372/*-------------------------------------------------------------------------*
373 * Interface functions (URB)
374 *-------------------------------------------------------------------------*/
375
376/* get a transfer request */
377
378int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900379 int transfer_len, struct devrequest *setup, int interval)
wdenkde887eb2003-09-10 18:20:28 +0000380{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900381 struct ohci *ohci;
382 struct ed *ed;
383 struct urb_priv *purb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000384 int i, size = 0;
385
386 ohci = &gohci;
387
388 /* when controller's hung, permit only roothub cleanup attempts
389 * such as powering down ports */
390 if (ohci->disabled) {
391 err("sohci_submit_job: EPIPE");
392 return -1;
393 }
394
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200395 /* if we have an unfinished URB from previous transaction let's
396 * fail and scream as quickly as possible so as not to corrupt
397 * further communication */
398 if (!urb_finished) {
399 err("sohci_submit_job: URB NOT FINISHED");
400 return -1;
401 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900402 /* we're about to begin a new transaction here
403 so mark the URB unfinished */
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200404 urb_finished = 0;
405
wdenkde887eb2003-09-10 18:20:28 +0000406 /* every endpoint has a ed, locate and fill it */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900407 ed = ep_add_ed(dev, pipe);
408 if (!ed) {
wdenkde887eb2003-09-10 18:20:28 +0000409 err("sohci_submit_job: ENOMEM");
410 return -1;
411 }
412
413 /* for the private part of the URB we need the number of TDs (size) */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900414 switch (usb_pipetype(pipe)) {
415 case PIPE_BULK:
416 /* one TD for every 4096 Byte */
417 size = (transfer_len - 1) / 4096 + 1;
418 break;
419 case PIPE_CONTROL:
420 /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
421 size = (transfer_len == 0) ? 2 : (transfer_len - 1) / 4096 + 3;
422 break;
wdenkde887eb2003-09-10 18:20:28 +0000423 }
424
425 if (size >= (N_URB_TD - 1)) {
426 err("need %d TDs, only have %d", size, N_URB_TD);
427 return -1;
428 }
429 purb_priv = &urb_priv;
430 purb_priv->pipe = pipe;
431
432 /* fill the private part of the URB */
433 purb_priv->length = size;
434 purb_priv->ed = ed;
435 purb_priv->actual_length = 0;
436
437 /* allocate the TDs */
438 /* note that td[0] was allocated in ep_add_ed */
439 for (i = 0; i < size; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900440 purb_priv->td[i] = td_alloc(dev);
wdenkde887eb2003-09-10 18:20:28 +0000441 if (!purb_priv->td[i]) {
442 purb_priv->length = i;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900443 urb_free_priv(purb_priv);
wdenkde887eb2003-09-10 18:20:28 +0000444 err("sohci_submit_job: ENOMEM");
445 return -1;
446 }
447 }
448
449 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900450 urb_free_priv(purb_priv);
wdenkde887eb2003-09-10 18:20:28 +0000451 err("sohci_submit_job: EINVAL");
452 return -1;
453 }
454
455 /* link the ed into a chain if is not already */
456 if (ed->state != ED_OPER)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900457 ep_link(ohci, ed);
wdenkde887eb2003-09-10 18:20:28 +0000458
459 /* fill the TDs and link it to the ed */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900460 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv,
461 interval);
wdenkde887eb2003-09-10 18:20:28 +0000462
463 return 0;
464}
465
466/*-------------------------------------------------------------------------*/
467
468#ifdef DEBUG
469/* tell us the current USB frame number */
470
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900471static int sohci_get_current_frame_number(struct usb_device *usb_dev)
wdenkde887eb2003-09-10 18:20:28 +0000472{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900473 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +0000474
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900475 return m16_swap(ohci->hcca->frame_no);
wdenkde887eb2003-09-10 18:20:28 +0000476}
477#endif
478
479/*-------------------------------------------------------------------------*
480 * ED handling functions
481 *-------------------------------------------------------------------------*/
482
483/* link an ed into one of the HC chains */
484
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900485static int ep_link(struct ohci *ohci, struct ed *edi)
wdenkde887eb2003-09-10 18:20:28 +0000486{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900487 struct ed *ed = edi;
wdenkde887eb2003-09-10 18:20:28 +0000488
489 ed->state = ED_OPER;
490
491 switch (ed->type) {
492 case PIPE_CONTROL:
493 ed->hwNextED = 0;
494 if (ohci->ed_controltail == NULL) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900495 writel((u32)ed, &ohci->regs->ed_controlhead);
wdenkde887eb2003-09-10 18:20:28 +0000496 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900497 ohci->ed_controltail->hwNextED = (__u32) m32_swap(ed);
wdenkde887eb2003-09-10 18:20:28 +0000498 }
499 ed->ed_prev = ohci->ed_controltail;
500 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900501 !ohci->ed_rm_list[1] && !ohci->sleeping) {
wdenkde887eb2003-09-10 18:20:28 +0000502 ohci->hc_control |= OHCI_CTRL_CLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900503 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000504 }
505 ohci->ed_controltail = edi;
506 break;
507
508 case PIPE_BULK:
509 ed->hwNextED = 0;
510 if (ohci->ed_bulktail == NULL) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900511 writel((u32)ed, &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +0000512 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900513 ohci->ed_bulktail->hwNextED = (__u32) m32_swap(ed);
wdenkde887eb2003-09-10 18:20:28 +0000514 }
515 ed->ed_prev = ohci->ed_bulktail;
516 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900517 !ohci->ed_rm_list[1] && !ohci->sleeping) {
wdenkde887eb2003-09-10 18:20:28 +0000518 ohci->hc_control |= OHCI_CTRL_BLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900519 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000520 }
521 ohci->ed_bulktail = edi;
522 break;
523 }
524 return 0;
525}
526
527/*-------------------------------------------------------------------------*/
528
529/* unlink an ed from one of the HC chains.
530 * just the link to the ed is unlinked.
531 * the link from the ed still points to another operational ed or 0
532 * so the HC can eventually finish the processing of the unlinked ed */
533
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900534static int ep_unlink(struct ohci *ohci, struct ed *ed)
wdenkde887eb2003-09-10 18:20:28 +0000535{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900536 struct ed *next;
537 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
wdenkde887eb2003-09-10 18:20:28 +0000538
539 switch (ed->type) {
540 case PIPE_CONTROL:
541 if (ed->ed_prev == NULL) {
542 if (!ed->hwNextED) {
543 ohci->hc_control &= ~OHCI_CTRL_CLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900544 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000545 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900546 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
547 &ohci->regs->ed_controlhead);
wdenkde887eb2003-09-10 18:20:28 +0000548 } else {
549 ed->ed_prev->hwNextED = ed->hwNextED;
550 }
551 if (ohci->ed_controltail == ed) {
552 ohci->ed_controltail = ed->ed_prev;
553 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900554 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
555 next->ed_prev = ed->ed_prev;
wdenkde887eb2003-09-10 18:20:28 +0000556 }
557 break;
558
559 case PIPE_BULK:
560 if (ed->ed_prev == NULL) {
561 if (!ed->hwNextED) {
562 ohci->hc_control &= ~OHCI_CTRL_BLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900563 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000564 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900565 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
566 &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +0000567 } else {
568 ed->ed_prev->hwNextED = ed->hwNextED;
569 }
570 if (ohci->ed_bulktail == ed) {
571 ohci->ed_bulktail = ed->ed_prev;
572 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900573 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
574 next->ed_prev = ed->ed_prev;
wdenkde887eb2003-09-10 18:20:28 +0000575 }
576 break;
577 }
578 ed->state = ED_UNLINK;
579 return 0;
580}
581
wdenkde887eb2003-09-10 18:20:28 +0000582/*-------------------------------------------------------------------------*/
583
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900584/* add/reinit an endpoint; this should be done once at the usb_set_configuration
585 * command, but the USB stack is a little bit stateless so we do it at every
586 * transaction. If the state of the ed is ED_NEW then a dummy td is added and
587 * the state is changed to ED_UNLINK. In all other cases the state is left
588 * unchanged. The ed info fields are setted anyway even though most of them
589 * should not change */
wdenkde887eb2003-09-10 18:20:28 +0000590
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900591static struct ed *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe)
wdenkde887eb2003-09-10 18:20:28 +0000592{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900593 struct td *td;
594 struct ed *ed_ret;
595 struct ed *ed;
wdenkde887eb2003-09-10 18:20:28 +0000596
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900597 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
598 (usb_pipecontrol(pipe) ? 0 :
599 usb_pipeout(pipe))];
wdenkde887eb2003-09-10 18:20:28 +0000600
601 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
602 err("ep_add_ed: pending delete");
603 /* pending delete request */
604 return NULL;
605 }
606
607 if (ed->state == ED_NEW) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900608 ed->hwINFO = m32_swap(OHCI_ED_SKIP); /* skip ed */
wdenk9c53f402003-10-15 23:53:47 +0000609 /* dummy td; end of td list for ed */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900610 td = td_alloc(usb_dev);
611 ed->hwTailP = (__u32) m32_swap(td);
wdenkde887eb2003-09-10 18:20:28 +0000612 ed->hwHeadP = ed->hwTailP;
613 ed->state = ED_UNLINK;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900614 ed->type = usb_pipetype(pipe);
wdenkde887eb2003-09-10 18:20:28 +0000615 ohci_dev.ed_cnt++;
616 }
617
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900618 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
619 | usb_pipeendpoint(pipe) << 7
620 | (usb_pipeisoc(pipe) ? 0x8000 : 0)
621 | (usb_pipecontrol(pipe) ? 0 :
622 (usb_pipeout(pipe) ? 0x800 : 0x1000))
623 | usb_pipeslow(pipe) << 13 |
624 usb_maxpacket(usb_dev, pipe) << 16);
wdenkde887eb2003-09-10 18:20:28 +0000625
626 return ed_ret;
627}
628
629/*-------------------------------------------------------------------------*
630 * TD handling functions
631 *-------------------------------------------------------------------------*/
632
633/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
634
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900635static void td_fill(struct ohci *ohci, unsigned int info, void *data, int len,
636 struct usb_device *dev, int index,
637 struct urb_priv *urb_priv)
wdenkde887eb2003-09-10 18:20:28 +0000638{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900639 struct td *td, *td_pt;
wdenkde887eb2003-09-10 18:20:28 +0000640#ifdef OHCI_FILL_TRACE
641 int i;
642#endif
643
644 if (index > urb_priv->length) {
645 err("index > length");
646 return;
647 }
648 /* use this td as the next dummy */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900649 td_pt = urb_priv->td[index];
wdenkde887eb2003-09-10 18:20:28 +0000650 td_pt->hwNextTD = 0;
651
652 /* fill the old dummy TD */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900653 td = urb_priv->td[index] =
654 (struct td *) (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
wdenkde887eb2003-09-10 18:20:28 +0000655
656 td->ed = urb_priv->ed;
657 td->next_dl_td = NULL;
658 td->index = index;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900659 td->data = (__u32) data;
wdenkde887eb2003-09-10 18:20:28 +0000660#ifdef OHCI_FILL_TRACE
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200661 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
wdenkde887eb2003-09-10 18:20:28 +0000662 for (i = 0; i < len; i++)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900663 printf("td->data[%d] %#2x ", i,
664 ((unsigned char *)td->data)[i]);
wdenkde887eb2003-09-10 18:20:28 +0000665 printf("\n");
666 }
667#endif
668 if (!len)
669 data = 0;
670
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900671 td->hwINFO = (__u32) m32_swap(info);
672 td->hwCBP = (__u32) m32_swap(data);
wdenkde887eb2003-09-10 18:20:28 +0000673 if (data)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900674 td->hwBE = (__u32) m32_swap(data + len - 1);
wdenkde887eb2003-09-10 18:20:28 +0000675 else
676 td->hwBE = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900677 td->hwNextTD = (__u32) m32_swap(td_pt);
wdenkde887eb2003-09-10 18:20:28 +0000678
679 /* append to queue */
680 td->ed->hwTailP = td->hwNextTD;
681}
682
683/*-------------------------------------------------------------------------*/
684
685/* prepare all TDs of a transfer */
686
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900687static void td_submit_job(struct usb_device *dev, unsigned long pipe,
688 void *buffer, int transfer_len,
689 struct devrequest *setup, struct urb_priv *urb,
690 int interval)
wdenkde887eb2003-09-10 18:20:28 +0000691{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900692 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +0000693 int data_len = transfer_len;
694 void *data;
695 int cnt = 0;
696 __u32 info = 0;
wdenk9c53f402003-10-15 23:53:47 +0000697 unsigned int toggle = 0;
wdenkde887eb2003-09-10 18:20:28 +0000698
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900699 /* OHCI handles the DATA-toggles itself, we just
700 use the USB-toggle bits for reseting */
701 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
wdenk9c53f402003-10-15 23:53:47 +0000702 toggle = TD_T_TOGGLE;
wdenkde887eb2003-09-10 18:20:28 +0000703 } else {
wdenk9c53f402003-10-15 23:53:47 +0000704 toggle = TD_T_DATA0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900705 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe),
706 1);
wdenkde887eb2003-09-10 18:20:28 +0000707 }
708 urb->td_cnt = 0;
709 if (data_len)
710 data = buffer;
711 else
712 data = 0;
713
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900714 switch (usb_pipetype(pipe)) {
wdenkde887eb2003-09-10 18:20:28 +0000715 case PIPE_BULK:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900716 info = usb_pipeout(pipe) ? TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN;
717 while (data_len > 4096) {
718 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
719 4096, dev, cnt, urb);
720 data += 4096;
721 data_len -= 4096;
722 cnt++;
wdenkde887eb2003-09-10 18:20:28 +0000723 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900724 info = usb_pipeout(pipe) ?
725 TD_CC | TD_DP_OUT :
726 TD_CC | TD_R | TD_DP_IN;
727 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
728 data_len, dev, cnt, urb);
wdenkde887eb2003-09-10 18:20:28 +0000729 cnt++;
730
731 if (!ohci->sleeping)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900732 /* start bulk list */
733 writel(OHCI_BLF, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +0000734 break;
735
736 case PIPE_CONTROL:
737 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900738 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000739 if (data_len > 0) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900740 info = usb_pipeout(pipe) ?
741 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
742 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
wdenkde887eb2003-09-10 18:20:28 +0000743 /* NOTE: mishandles transfers >8K, some >4K */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900744 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000745 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900746 info = usb_pipeout(pipe) ?
747 TD_CC | TD_DP_IN | TD_T_DATA1 :
748 TD_CC | TD_DP_OUT | TD_T_DATA1;
749 td_fill(ohci, info, data, 0, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000750 if (!ohci->sleeping)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900751 /* start Control list */
752 writel(OHCI_CLF, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +0000753 break;
754 }
755 if (urb->length != cnt)
756 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
757}
758
759/*-------------------------------------------------------------------------*
760 * Done List handling functions
761 *-------------------------------------------------------------------------*/
762
763
764/* calculate the transfer length and update the urb */
765
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900766static void dl_transfer_length(struct td *td)
wdenkde887eb2003-09-10 18:20:28 +0000767{
Wolfgang Denka0ebb902011-10-06 09:22:52 +0200768 __u32 tdBE, tdCBP;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900769 struct urb_priv *lurb_priv = &urb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000770
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900771 tdBE = m32_swap(td->hwBE);
772 tdCBP = m32_swap(td->hwCBP);
wdenkde887eb2003-09-10 18:20:28 +0000773
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200774 if (!(usb_pipecontrol(lurb_priv->pipe) &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900775 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
wdenkde887eb2003-09-10 18:20:28 +0000776 if (tdBE != 0) {
777 if (td->hwCBP == 0)
778 lurb_priv->actual_length += tdBE - td->data + 1;
779 else
780 lurb_priv->actual_length += tdCBP - td->data;
781 }
782 }
783}
784
785/*-------------------------------------------------------------------------*/
786
787/* replies to the request have to be on a FIFO basis so
788 * we reverse the reversed done-list */
789
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900790static struct td *dl_reverse_done_list(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +0000791{
792 __u32 td_list_hc;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900793 __u32 tmp;
794 struct td *td_rev = NULL;
795 struct td *td_list = NULL;
796 struct urb_priv *lurb_priv = NULL;
wdenkde887eb2003-09-10 18:20:28 +0000797
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900798 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
wdenkde887eb2003-09-10 18:20:28 +0000799 ohci->hcca->done_head = 0;
800
801 while (td_list_hc) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900802 td_list = (struct td *) td_list_hc;
wdenkde887eb2003-09-10 18:20:28 +0000803
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900804 if (TD_CC_GET(m32_swap(td_list->hwINFO))) {
wdenkde887eb2003-09-10 18:20:28 +0000805 lurb_priv = &urb_priv;
806 dbg(" USB-error/status: %x : %p",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900807 TD_CC_GET(m32_swap(td_list->hwINFO)), td_list);
808 if (td_list->ed->hwHeadP & m32_swap(0x1)) {
809 if (lurb_priv &&
810 ((td_list->index+1) < lurb_priv->length)) {
811 tmp = lurb_priv->length - 1;
wdenkde887eb2003-09-10 18:20:28 +0000812 td_list->ed->hwHeadP =
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900813 (lurb_priv->td[tmp]->hwNextTD &
814 m32_swap(0xfffffff0)) |
815 (td_list->ed->hwHeadP &
816 m32_swap(0x2));
817 lurb_priv->td_cnt += lurb_priv->length -
818 td_list->index - 1;
wdenkde887eb2003-09-10 18:20:28 +0000819 } else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900820 td_list->ed->hwHeadP &=
821 m32_swap(0xfffffff2);
wdenkde887eb2003-09-10 18:20:28 +0000822 }
823 }
824
825 td_list->next_dl_td = td_rev;
826 td_rev = td_list;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900827 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
wdenkde887eb2003-09-10 18:20:28 +0000828 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200829
wdenkde887eb2003-09-10 18:20:28 +0000830 return td_list;
831}
832
833/*-------------------------------------------------------------------------*/
834
835/* td done list */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900836static int dl_done_list(struct ohci *ohci, struct td *td_list)
wdenkde887eb2003-09-10 18:20:28 +0000837{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900838 struct td *td_list_next = NULL;
839 struct ed *ed;
wdenkde887eb2003-09-10 18:20:28 +0000840 int cc = 0;
841 int stat = 0;
842 /* urb_t *urb; */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900843 struct urb_priv *lurb_priv;
wdenk9c53f402003-10-15 23:53:47 +0000844 __u32 tdINFO, edHeadP, edTailP;
wdenkde887eb2003-09-10 18:20:28 +0000845
wdenk9c53f402003-10-15 23:53:47 +0000846 while (td_list) {
847 td_list_next = td_list->next_dl_td;
wdenkde887eb2003-09-10 18:20:28 +0000848
wdenk9c53f402003-10-15 23:53:47 +0000849 lurb_priv = &urb_priv;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900850 tdINFO = m32_swap(td_list->hwINFO);
wdenkde887eb2003-09-10 18:20:28 +0000851
wdenk9c53f402003-10-15 23:53:47 +0000852 ed = td_list->ed;
wdenkde887eb2003-09-10 18:20:28 +0000853
wdenk9c53f402003-10-15 23:53:47 +0000854 dl_transfer_length(td_list);
wdenkde887eb2003-09-10 18:20:28 +0000855
wdenk9c53f402003-10-15 23:53:47 +0000856 /* error code of transfer */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900857 cc = TD_CC_GET(tdINFO);
wdenkde887eb2003-09-10 18:20:28 +0000858 if (cc != 0) {
859 dbg("ConditionCode %#x", cc);
860 stat = cc_to_error[cc];
861 }
862
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200863 /* see if this done list makes for all TD's of current URB,
864 * and mark the URB finished if so */
865 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
866 if ((ed->state & (ED_OPER | ED_UNLINK)))
867 urb_finished = 1;
868 else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900869 dbg("dl_done_list: strange.., ED state %x, "
870 "ed->state\n");
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200871 } else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900872 dbg("dl_done_list: processing TD %x, len %x\n",
873 lurb_priv->td_cnt, lurb_priv->length);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200874
wdenk9c53f402003-10-15 23:53:47 +0000875 if (ed->state != ED_NEW) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900876 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
877 edTailP = m32_swap(ed->hwTailP);
wdenkde887eb2003-09-10 18:20:28 +0000878
879 /* unlink eds if they are not busy */
wdenk9c53f402003-10-15 23:53:47 +0000880 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900881 ep_unlink(ohci, ed);
wdenk9c53f402003-10-15 23:53:47 +0000882 }
wdenkde887eb2003-09-10 18:20:28 +0000883
wdenk9c53f402003-10-15 23:53:47 +0000884 td_list = td_list_next;
885 }
wdenkde887eb2003-09-10 18:20:28 +0000886 return stat;
887}
888
889/*-------------------------------------------------------------------------*
890 * Virtual Root Hub
891 *-------------------------------------------------------------------------*/
892
893/* Device descriptor */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900894static __u8 root_hub_dev_des[] = {
895 0x12, /* __u8 bLength; */
896 0x01, /* __u8 bDescriptorType; Device */
897 0x10, /* __u16 bcdUSB; v1.1 */
wdenkde887eb2003-09-10 18:20:28 +0000898 0x01,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900899 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
900 0x00, /* __u8 bDeviceSubClass; */
901 0x00, /* __u8 bDeviceProtocol; */
902 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
903 0x00, /* __u16 idVendor; */
wdenkde887eb2003-09-10 18:20:28 +0000904 0x00,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900905 0x00, /* __u16 idProduct; */
wdenk9c53f402003-10-15 23:53:47 +0000906 0x00,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900907 0x00, /* __u16 bcdDevice; */
wdenk9c53f402003-10-15 23:53:47 +0000908 0x00,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900909 0x00, /* __u8 iManufacturer; */
910 0x01, /* __u8 iProduct; */
911 0x00, /* __u8 iSerialNumber; */
912 0x01 /* __u8 bNumConfigurations; */
wdenkde887eb2003-09-10 18:20:28 +0000913};
914
wdenkde887eb2003-09-10 18:20:28 +0000915/* Configuration descriptor */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900916static __u8 root_hub_config_des[] = {
917 0x09, /* __u8 bLength; */
918 0x02, /* __u8 bDescriptorType; Configuration */
919 0x19, /* __u16 wTotalLength; */
wdenkde887eb2003-09-10 18:20:28 +0000920 0x00,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900921 0x01, /* __u8 bNumInterfaces; */
922 0x01, /* __u8 bConfigurationValue; */
923 0x00, /* __u8 iConfiguration; */
924 0x40, /* __u8 bmAttributes;
925 Bit 7: Bus-powered, 6: Self-powered,
926 5 Remote-wakwup, 4..0: resvd */
927 0x00, /* __u8 MaxPower; */
wdenkde887eb2003-09-10 18:20:28 +0000928
929 /* interface */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900930 0x09, /* __u8 if_bLength; */
931 0x04, /* __u8 if_bDescriptorType; Interface */
932 0x00, /* __u8 if_bInterfaceNumber; */
933 0x00, /* __u8 if_bAlternateSetting; */
934 0x01, /* __u8 if_bNumEndpoints; */
935 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
936 0x00, /* __u8 if_bInterfaceSubClass; */
937 0x00, /* __u8 if_bInterfaceProtocol; */
938 0x00, /* __u8 if_iInterface; */
wdenkde887eb2003-09-10 18:20:28 +0000939
940 /* endpoint */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900941 0x07, /* __u8 ep_bLength; */
942 0x05, /* __u8 ep_bDescriptorType; Endpoint */
943 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
944 0x03, /* __u8 ep_bmAttributes; Interrupt */
945 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
wdenk9c53f402003-10-15 23:53:47 +0000946 0x00,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900947 0xff /* __u8 ep_bInterval; 255 ms */
wdenkde887eb2003-09-10 18:20:28 +0000948};
949
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900950static unsigned char root_hub_str_index0[] = {
951 0x04, /* __u8 bLength; */
952 0x03, /* __u8 bDescriptorType; String-descriptor */
953 0x09, /* __u8 lang ID */
954 0x04, /* __u8 lang ID */
wdenkde887eb2003-09-10 18:20:28 +0000955};
956
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900957static unsigned char root_hub_str_index1[] = {
958 28, /* __u8 bLength; */
959 0x03, /* __u8 bDescriptorType; String-descriptor */
960 'O', /* __u8 Unicode */
961 0, /* __u8 Unicode */
962 'H', /* __u8 Unicode */
963 0, /* __u8 Unicode */
964 'C', /* __u8 Unicode */
965 0, /* __u8 Unicode */
966 'I', /* __u8 Unicode */
967 0, /* __u8 Unicode */
968 ' ', /* __u8 Unicode */
969 0, /* __u8 Unicode */
970 'R', /* __u8 Unicode */
971 0, /* __u8 Unicode */
972 'o', /* __u8 Unicode */
973 0, /* __u8 Unicode */
974 'o', /* __u8 Unicode */
975 0, /* __u8 Unicode */
976 't', /* __u8 Unicode */
977 0, /* __u8 Unicode */
978 ' ', /* __u8 Unicode */
979 0, /* __u8 Unicode */
980 'H', /* __u8 Unicode */
981 0, /* __u8 Unicode */
982 'u', /* __u8 Unicode */
983 0, /* __u8 Unicode */
984 'b', /* __u8 Unicode */
985 0, /* __u8 Unicode */
wdenkde887eb2003-09-10 18:20:28 +0000986};
987
988/* Hub class-specific descriptor is constructed dynamically */
989
990
991/*-------------------------------------------------------------------------*/
992
Wolfgang Denka1be4762008-05-20 16:00:29 +0200993#define OK(x) len = (x); break
wdenkde887eb2003-09-10 18:20:28 +0000994#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900995#define WR_RH_STAT(x) \
996{ \
997 info("WR:status %#8x", (x)); \
998 writel((x), &gohci.regs->roothub.status); \
999}
1000#define WR_RH_PORTSTAT(x) \
1001{ \
1002 info("WR:portstatus[%d] %#8x", wIndex-1, (x)); \
1003 writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); \
1004}
wdenkde887eb2003-09-10 18:20:28 +00001005#else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001006#define WR_RH_STAT(x) \
1007 writel((x), &gohci.regs->roothub.status)
1008#define WR_RH_PORTSTAT(x)\
1009 writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
wdenkde887eb2003-09-10 18:20:28 +00001010#endif
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001011#define RD_RH_STAT roothub_status(&gohci)
1012#define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
wdenkde887eb2003-09-10 18:20:28 +00001013
1014/* request to virtual root hub */
1015
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001016int rh_check_port_status(struct ohci *controller)
dzu8d7e4d12003-09-29 21:55:54 +00001017{
1018 __u32 temp, ndp, i;
1019 int res;
1020
1021 res = -1;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001022 temp = roothub_a(controller);
dzu8d7e4d12003-09-29 21:55:54 +00001023 ndp = (temp & RH_A_NDP);
1024 for (i = 0; i < ndp; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001025 temp = roothub_portstatus(controller, i);
dzu8d7e4d12003-09-29 21:55:54 +00001026 /* check for a device disconnect */
1027 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001028 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
dzu8d7e4d12003-09-29 21:55:54 +00001029 res = i;
1030 break;
1031 }
1032 }
1033 return res;
1034}
1035
1036static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001037 void *buffer, int transfer_len,
1038 struct devrequest *cmd)
wdenkde887eb2003-09-10 18:20:28 +00001039{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001040 void *data = buffer;
wdenkde887eb2003-09-10 18:20:28 +00001041 int leni = transfer_len;
1042 int len = 0;
1043 int stat = 0;
Simon Glass28cace12011-11-15 18:17:06 +00001044 union {
1045 __u32 word[4];
1046 __u16 hword[8];
1047 __u8 byte[16];
1048 } datab;
1049 __u8 *data_buf = datab.byte;
wdenk9c53f402003-10-15 23:53:47 +00001050 __u16 bmRType_bReq;
wdenkde887eb2003-09-10 18:20:28 +00001051 __u16 wValue;
1052 __u16 wIndex;
1053 __u16 wLength;
1054
1055#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001056 urb_priv.actual_length = 0;
1057 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)",
1058 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001059#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001060 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001061#endif
Remy Bohmerd8c55ab2008-10-10 10:23:22 +02001062 if (usb_pipeint(pipe)) {
wdenkde887eb2003-09-10 18:20:28 +00001063 info("Root-Hub submit IRQ: NOT implemented");
1064 return 0;
1065 }
1066
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001067 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1068 wValue = m16_swap(cmd->value);
1069 wIndex = m16_swap(cmd->index);
1070 wLength = m16_swap(cmd->length);
wdenkde887eb2003-09-10 18:20:28 +00001071
1072 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001073 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
wdenkde887eb2003-09-10 18:20:28 +00001074
1075 switch (bmRType_bReq) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001076 /* Request Destination:
1077 without flags: Device,
1078 RH_INTERFACE: interface,
1079 RH_ENDPOINT: endpoint,
1080 RH_CLASS means HUB here,
1081 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1082 */
wdenkde887eb2003-09-10 18:20:28 +00001083
1084 case RH_GET_STATUS:
Simon Glass28cace12011-11-15 18:17:06 +00001085 datab.hword[0] = m16_swap(1);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001086 OK(2);
wdenkde887eb2003-09-10 18:20:28 +00001087 case RH_GET_STATUS | RH_INTERFACE:
Simon Glass28cace12011-11-15 18:17:06 +00001088 datab.hword[0] = m16_swap(0);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001089 OK(2);
wdenkde887eb2003-09-10 18:20:28 +00001090 case RH_GET_STATUS | RH_ENDPOINT:
Simon Glass28cace12011-11-15 18:17:06 +00001091 datab.hword[0] = m16_swap(0);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001092 OK(2);
wdenkde887eb2003-09-10 18:20:28 +00001093 case RH_GET_STATUS | RH_CLASS:
Simon Glass28cace12011-11-15 18:17:06 +00001094 datab.word[0] =
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001095 m32_swap(RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1096 OK(4);
wdenkde887eb2003-09-10 18:20:28 +00001097 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Simon Glass28cace12011-11-15 18:17:06 +00001098 datab.word[0] = m32_swap(RD_RH_PORTSTAT);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001099 OK(4);
wdenkde887eb2003-09-10 18:20:28 +00001100
1101 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1102 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001103 case (RH_ENDPOINT_STALL):
1104 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001105 }
1106 break;
1107
1108 case RH_CLEAR_FEATURE | RH_CLASS:
1109 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001110 case RH_C_HUB_LOCAL_POWER:
1111 OK(0);
1112 case (RH_C_HUB_OVER_CURRENT):
1113 WR_RH_STAT(RH_HS_OCIC);
1114 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001115 }
1116 break;
1117
1118 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1119 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001120 case (RH_PORT_ENABLE):
1121 WR_RH_PORTSTAT(RH_PS_CCS);
1122 OK(0);
1123 case (RH_PORT_SUSPEND):
1124 WR_RH_PORTSTAT(RH_PS_POCI);
1125 OK(0);
1126 case (RH_PORT_POWER):
1127 WR_RH_PORTSTAT(RH_PS_LSDA);
1128 OK(0);
1129 case (RH_C_PORT_CONNECTION):
1130 WR_RH_PORTSTAT(RH_PS_CSC);
1131 OK(0);
1132 case (RH_C_PORT_ENABLE):
1133 WR_RH_PORTSTAT(RH_PS_PESC);
1134 OK(0);
1135 case (RH_C_PORT_SUSPEND):
1136 WR_RH_PORTSTAT(RH_PS_PSSC);
1137 OK(0);
1138 case (RH_C_PORT_OVER_CURRENT):
1139 WR_RH_PORTSTAT(RH_PS_OCIC);
1140 OK(0);
1141 case (RH_C_PORT_RESET):
1142 WR_RH_PORTSTAT(RH_PS_PRSC);
1143 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001144 }
1145 break;
1146
1147 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1148 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001149 case (RH_PORT_SUSPEND):
1150 WR_RH_PORTSTAT(RH_PS_PSS);
1151 OK(0);
1152 case (RH_PORT_RESET): /* BUG IN HUP CODE ******** */
1153 if (RD_RH_PORTSTAT & RH_PS_CCS)
1154 WR_RH_PORTSTAT(RH_PS_PRS);
1155 OK(0);
1156 case (RH_PORT_POWER):
1157 WR_RH_PORTSTAT(RH_PS_PPS);
1158 OK(0);
1159 case (RH_PORT_ENABLE): /* BUG IN HUP CODE ******** */
1160 if (RD_RH_PORTSTAT & RH_PS_CCS)
1161 WR_RH_PORTSTAT(RH_PS_PES);
1162 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001163 }
1164 break;
1165
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001166 case RH_SET_ADDRESS:
1167 gohci.rh.devnum = wValue;
1168 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001169
1170 case RH_GET_DESCRIPTOR:
1171 switch ((wValue & 0xff00) >> 8) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001172 case (0x01): /* device descriptor */
1173 len = min_t(unsigned int,
1174 leni,
1175 min_t(unsigned int,
1176 sizeof(root_hub_dev_des), wLength));
1177 data_buf = root_hub_dev_des;
1178 OK(len);
1179 case (0x02): /* configuration descriptor */
1180 len = min_t(unsigned int,
1181 leni,
1182 min_t(unsigned int,
1183 sizeof(root_hub_config_des),
1184 wLength));
1185 data_buf = root_hub_config_des;
1186 OK(len);
1187 case (0x03): /* string descriptors */
1188 if (wValue == 0x0300) {
wdenkde887eb2003-09-10 18:20:28 +00001189 len = min_t(unsigned int,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001190 leni,
1191 min_t(unsigned int,
1192 sizeof(root_hub_str_index0),
1193 wLength));
1194 data_buf = root_hub_str_index0;
1195 OK(len);
1196 }
1197 if (wValue == 0x0301) {
wdenkde887eb2003-09-10 18:20:28 +00001198 len = min_t(unsigned int,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001199 leni,
1200 min_t(unsigned int,
1201 sizeof(root_hub_str_index1),
1202 wLength));
1203 data_buf = root_hub_str_index1;
1204 OK(len);
wdenkde887eb2003-09-10 18:20:28 +00001205 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001206 default:
1207 stat = USB_ST_STALLED;
wdenkde887eb2003-09-10 18:20:28 +00001208 }
1209 break;
1210
1211 case RH_GET_DESCRIPTOR | RH_CLASS:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001212 {
1213 __u32 temp = roothub_a(&gohci);
wdenkde887eb2003-09-10 18:20:28 +00001214
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001215 data_buf[0] = 9; /* min length; */
1216 data_buf[1] = 0x29;
1217 data_buf[2] = temp & RH_A_NDP;
1218 data_buf[3] = 0;
1219 if (temp & RH_A_PSM)
1220 /* per-port power switching? */
1221 data_buf[3] |= 0x1;
1222 if (temp & RH_A_NOCP)
1223 /* no overcurrent reporting? */
1224 data_buf[3] |= 0x10;
1225 else if (temp & RH_A_OCPM)
1226 /* per-port overcurrent reporting? */
1227 data_buf[3] |= 0x8;
wdenkde887eb2003-09-10 18:20:28 +00001228
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001229 /* corresponds to data_buf[4-7] */
Simon Glass28cace12011-11-15 18:17:06 +00001230 datab.word[1] = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001231 data_buf[5] = (temp & RH_A_POTPGT) >> 24;
1232 temp = roothub_b(&gohci);
1233 data_buf[7] = temp & RH_B_DR;
1234 if (data_buf[2] < 7) {
1235 data_buf[8] = 0xff;
1236 } else {
1237 data_buf[0] += 2;
1238 data_buf[8] = (temp & RH_B_DR) >> 8;
1239 data_buf[10] = data_buf[9] = 0xff;
1240 }
wdenkde887eb2003-09-10 18:20:28 +00001241
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001242 len = min_t(unsigned int, leni,
1243 min_t(unsigned int, data_buf[0], wLength));
1244 OK(len);
wdenkde887eb2003-09-10 18:20:28 +00001245 }
1246
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001247 case RH_GET_CONFIGURATION:
1248 *(__u8 *) data_buf = 0x01;
1249 OK(1);
wdenkde887eb2003-09-10 18:20:28 +00001250
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001251 case RH_SET_CONFIGURATION:
1252 WR_RH_STAT(0x10000);
1253 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001254
1255 default:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001256 dbg("unsupported root hub command");
wdenkde887eb2003-09-10 18:20:28 +00001257 stat = USB_ST_STALLED;
1258 }
1259
1260#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001261 ohci_dump_roothub(&gohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001262#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001263 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001264#endif
1265
1266 len = min_t(int, len, leni);
1267 if (data != data_buf)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001268 memcpy(data, data_buf, len);
wdenk9c53f402003-10-15 23:53:47 +00001269 dev->act_len = len;
wdenkde887eb2003-09-10 18:20:28 +00001270 dev->status = stat;
1271
1272#ifdef DEBUG
1273 if (transfer_len)
1274 urb_priv.actual_length = transfer_len;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001275 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)",
1276 0 /*usb_pipein(pipe) */);
wdenkde887eb2003-09-10 18:20:28 +00001277#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001278 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001279#endif
1280
1281 return stat;
1282}
1283
1284/*-------------------------------------------------------------------------*/
1285
1286/* common code for handling submit messages - used for all but root hub */
1287/* accesses. */
1288int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001289 int transfer_len, struct devrequest *setup, int interval)
wdenkde887eb2003-09-10 18:20:28 +00001290{
1291 int stat = 0;
1292 int maxsize = usb_maxpacket(dev, pipe);
1293 int timeout;
1294
dzu8d7e4d12003-09-29 21:55:54 +00001295 /* device pulled? Shortcut the action. */
1296 if (devgone == dev) {
1297 dev->status = USB_ST_CRC_ERR;
1298 return 0;
1299 }
wdenkde887eb2003-09-10 18:20:28 +00001300#ifdef DEBUG
1301 urb_priv.actual_length = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001302 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1303 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001304#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001305 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001306#endif
1307 if (!maxsize) {
1308 err("submit_common_message: pipesize for pipe %lx is zero",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001309 pipe);
wdenkde887eb2003-09-10 18:20:28 +00001310 return -1;
1311 }
1312
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001313 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) <
1314 0) {
wdenkde887eb2003-09-10 18:20:28 +00001315 err("sohci_submit_job failed");
1316 return -1;
1317 }
1318
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001319 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001320 /* ohci_dump_status(&gohci); */
wdenk9c53f402003-10-15 23:53:47 +00001321
wdenk934c4f82003-09-11 19:48:06 +00001322 /* allow more time for a BULK device to react - some are slow */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001323#define BULK_TO 5000 /* timeout in milliseconds */
Remy Bohmerd8c55ab2008-10-10 10:23:22 +02001324 if (usb_pipebulk(pipe))
wdenk934c4f82003-09-11 19:48:06 +00001325 timeout = BULK_TO;
1326 else
1327 timeout = 100;
1328
wdenkde887eb2003-09-10 18:20:28 +00001329 /* wait for it to complete */
wdenkde887eb2003-09-10 18:20:28 +00001330 for (;;) {
1331 /* check whether the controller is done */
1332 stat = hc_interrupt();
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001333
wdenkde887eb2003-09-10 18:20:28 +00001334 if (stat < 0) {
dzu8d7e4d12003-09-29 21:55:54 +00001335 stat = USB_ST_CRC_ERR;
wdenkde887eb2003-09-10 18:20:28 +00001336 break;
1337 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001338
1339 /* NOTE: since we are not interrupt driven in U-Boot and always
1340 * handle only one URB at a time, we cannot assume the
1341 * transaction finished on the first successful return from
1342 * hc_interrupt().. unless the flag for current URB is set,
1343 * meaning that all TD's to/from device got actually
1344 * transferred and processed. If the current URB is not
1345 * finished we need to re-iterate this loop so as
1346 * hc_interrupt() gets called again as there needs to be some
1347 * more TD's to process still */
1348 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
wdenkde887eb2003-09-10 18:20:28 +00001349 /* 0xff is returned for an SF-interrupt */
1350 break;
1351 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001352
wdenkde887eb2003-09-10 18:20:28 +00001353 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001354 mdelay(1);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001355 if (!urb_finished)
1356 dbg("\%");
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +02001357
wdenkde887eb2003-09-10 18:20:28 +00001358 } else {
dzu8d7e4d12003-09-29 21:55:54 +00001359 err("CTL:TIMEOUT ");
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001360 dbg("submit_common_msg: TO status %x\n", stat);
dzu8d7e4d12003-09-29 21:55:54 +00001361 stat = USB_ST_CRC_ERR;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001362 urb_finished = 1;
wdenkde887eb2003-09-10 18:20:28 +00001363 break;
1364 }
1365 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001366
1367#if 0
dzu8d7e4d12003-09-29 21:55:54 +00001368 /* we got an Root Hub Status Change interrupt */
1369 if (got_rhsc) {
1370#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001371 ohci_dump_roothub(&gohci, 1);
dzu8d7e4d12003-09-29 21:55:54 +00001372#endif
1373 got_rhsc = 0;
1374 /* abuse timeout */
1375 timeout = rh_check_port_status(&gohci);
1376 if (timeout >= 0) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001377#if 0 /* this does nothing useful, but leave it here
1378 in case that changes */
dzu8d7e4d12003-09-29 21:55:54 +00001379 /* the called routine adds 1 to the passed value */
1380 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1381#endif
1382 /*
1383 * XXX
1384 * This is potentially dangerous because it assumes
1385 * that only one device is ever plugged in!
1386 */
1387 devgone = dev;
1388 }
1389 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001390#endif
dzu8d7e4d12003-09-29 21:55:54 +00001391
wdenkde887eb2003-09-10 18:20:28 +00001392 dev->status = stat;
wdenk9c53f402003-10-15 23:53:47 +00001393 dev->act_len = transfer_len;
wdenkde887eb2003-09-10 18:20:28 +00001394
1395#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001396 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)",
1397 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001398#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001399 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001400#endif
1401
1402 /* free TDs in urb_priv */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001403 urb_free_priv(&urb_priv);
wdenkde887eb2003-09-10 18:20:28 +00001404 return 0;
1405}
1406
1407/* submit routines called from usb.c */
1408int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001409 int transfer_len)
wdenkde887eb2003-09-10 18:20:28 +00001410{
1411 info("submit_bulk_msg");
1412 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1413}
1414
1415int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001416 int transfer_len, struct devrequest *setup)
wdenkde887eb2003-09-10 18:20:28 +00001417{
1418 int maxsize = usb_maxpacket(dev, pipe);
1419
1420 info("submit_control_msg");
1421#ifdef DEBUG
1422 urb_priv.actual_length = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001423 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1424 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001425#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001426 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001427#endif
1428 if (!maxsize) {
1429 err("submit_control_message: pipesize for pipe %lx is zero",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001430 pipe);
wdenkde887eb2003-09-10 18:20:28 +00001431 return -1;
1432 }
dzu8d7e4d12003-09-29 21:55:54 +00001433 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1434 gohci.rh.dev = dev;
wdenkde887eb2003-09-10 18:20:28 +00001435 /* root hub - redirect */
1436 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001437 setup);
dzu8d7e4d12003-09-29 21:55:54 +00001438 }
wdenkde887eb2003-09-10 18:20:28 +00001439
1440 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1441}
1442
1443int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001444 int transfer_len, int interval)
wdenkde887eb2003-09-10 18:20:28 +00001445{
1446 info("submit_int_msg");
1447 return -1;
1448}
1449
1450/*-------------------------------------------------------------------------*
1451 * HC functions
1452 *-------------------------------------------------------------------------*/
1453
1454/* reset the HC and BUS */
1455
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001456static int hc_reset(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001457{
1458 int timeout = 30;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001459 int smm_timeout = 50; /* 0,5 sec */
wdenkde887eb2003-09-10 18:20:28 +00001460
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001461 if (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1462 /* SMM owns the HC - request ownership */
1463 writel(OHCI_OCR, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +00001464 info("USB HC TakeOver from SMM");
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001465 while (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001466 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001467 if (--smm_timeout == 0) {
1468 err("USB HC TakeOver failed!");
1469 return -1;
1470 }
1471 }
1472 }
1473
1474 /* Disable HC interrupts */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001475 writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
wdenkde887eb2003-09-10 18:20:28 +00001476
1477 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001478 ohci->slot_name, readl(&ohci->regs->control));
wdenkde887eb2003-09-10 18:20:28 +00001479
wdenk9c53f402003-10-15 23:53:47 +00001480 /* Reset USB (needed by some controllers) */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001481 writel(0, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +00001482
1483 /* HC Reset requires max 10 us delay */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001484 writel(OHCI_HCR, &ohci->regs->cmdstatus);
1485 while ((readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
wdenkde887eb2003-09-10 18:20:28 +00001486 if (--timeout == 0) {
1487 err("USB HC reset timed out!");
1488 return -1;
1489 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001490 udelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001491 }
1492 return 0;
1493}
1494
1495/*-------------------------------------------------------------------------*/
1496
1497/* Start an OHCI controller, set the BUS operational
1498 * enable interrupts
1499 * connect the virtual root hub */
1500
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001501static int hc_start(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001502{
wdenk9c53f402003-10-15 23:53:47 +00001503 __u32 mask;
1504 unsigned int fminterval;
wdenkde887eb2003-09-10 18:20:28 +00001505
1506 ohci->disabled = 1;
1507
1508 /* Tell the controller where the control and bulk lists are
1509 * The lists are empty now. */
1510
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001511 writel(0, &ohci->regs->ed_controlhead);
1512 writel(0, &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +00001513
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001514 /* a reset clears this */
1515 writel((__u32) ohci->hcca, &ohci->regs->hcca);
wdenkde887eb2003-09-10 18:20:28 +00001516
wdenk9c53f402003-10-15 23:53:47 +00001517 fminterval = 0x2edf;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001518 writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
wdenkde887eb2003-09-10 18:20:28 +00001519 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001520 writel(fminterval, &ohci->regs->fminterval);
1521 writel(0x628, &ohci->regs->lsthresh);
wdenkde887eb2003-09-10 18:20:28 +00001522
wdenk9c53f402003-10-15 23:53:47 +00001523 /* start controller operations */
1524 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
wdenkde887eb2003-09-10 18:20:28 +00001525 ohci->disabled = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001526 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +00001527
dzu8d7e4d12003-09-29 21:55:54 +00001528 /* disable all interrupts */
1529 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001530 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1531 OHCI_INTR_OC | OHCI_INTR_MIE);
1532 writel(mask, &ohci->regs->intrdisable);
dzu8d7e4d12003-09-29 21:55:54 +00001533 /* clear all interrupts */
1534 mask &= ~OHCI_INTR_MIE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001535 writel(mask, &ohci->regs->intrstatus);
dzu8d7e4d12003-09-29 21:55:54 +00001536 /* Choose the interrupts we care about now - but w/o MIE */
1537 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001538 writel(mask, &ohci->regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001539
1540#ifdef OHCI_USE_NPS
1541 /* required for AMD-756 and some Mac platforms */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001542 writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1543 &ohci->regs->roothub.a);
1544 writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1545#endif /* OHCI_USE_NPS */
wdenkde887eb2003-09-10 18:20:28 +00001546
wdenkde887eb2003-09-10 18:20:28 +00001547 /* POTPGT delay is bits 24-31, in 2 ms units. */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001548 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
wdenkde887eb2003-09-10 18:20:28 +00001549
1550 /* connect the virtual root hub */
1551 ohci->rh.devnum = 0;
1552
1553 return 0;
1554}
1555
1556/*-------------------------------------------------------------------------*/
1557
1558/* an interrupt happens */
1559
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001560static int hc_interrupt(void)
wdenkde887eb2003-09-10 18:20:28 +00001561{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001562 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +00001563 struct ohci_regs *regs = ohci->regs;
wdenk9c53f402003-10-15 23:53:47 +00001564 int ints;
wdenkde887eb2003-09-10 18:20:28 +00001565 int stat = -1;
1566
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001567 if ((ohci->hcca->done_head != 0) &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001568 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001569
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001570 ints = OHCI_INTR_WDH;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001571
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001572 } else {
1573 ints = readl(&regs->intrstatus);
1574 if (ints == ~(u32) 0) {
1575 ohci->disabled++;
1576 err("%s device removed!", ohci->slot_name);
1577 return -1;
1578 }
1579 ints &= readl(&regs->intrenable);
1580 if (ints == 0) {
1581 dbg("hc_interrupt: returning..\n");
1582 return 0xff;
1583 }
wdenkde887eb2003-09-10 18:20:28 +00001584 }
1585
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001586 /* dbg("Interrupt: %x frame: %x", ints,
1587 le16_to_cpu(ohci->hcca->frame_no)); */
wdenkde887eb2003-09-10 18:20:28 +00001588
dzu8d7e4d12003-09-29 21:55:54 +00001589 if (ints & OHCI_INTR_RHSC) {
1590 got_rhsc = 1;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001591 stat = 0xff;
dzu8d7e4d12003-09-29 21:55:54 +00001592 }
1593
wdenkde887eb2003-09-10 18:20:28 +00001594 if (ints & OHCI_INTR_UE) {
1595 ohci->disabled++;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001596 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1597 ohci->slot_name);
wdenkde887eb2003-09-10 18:20:28 +00001598 /* e.g. due to PCI Master/Target Abort */
1599
1600#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001601 ohci_dump(ohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001602#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001603 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001604#endif
1605 /* FIXME: be optimistic, hope that bug won't repeat often. */
1606 /* Make some non-interrupt context restart the controller. */
1607 /* Count and limit the retries though; either hardware or */
1608 /* software errors can go forever... */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001609 hc_reset(ohci);
wdenkde887eb2003-09-10 18:20:28 +00001610 return -1;
1611 }
1612
1613 if (ints & OHCI_INTR_WDH) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001614 mdelay(1);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001615
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001616 writel(OHCI_INTR_WDH, &regs->intrdisable);
1617 stat = dl_done_list(&gohci, dl_reverse_done_list(&gohci));
1618 writel(OHCI_INTR_WDH, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001619 }
1620
1621 if (ints & OHCI_INTR_SO) {
1622 dbg("USB Schedule overrun\n");
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001623 writel(OHCI_INTR_SO, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001624 stat = -1;
1625 }
1626
1627 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1628 if (ints & OHCI_INTR_SF) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001629 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001630 mdelay(1);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001631 writel(OHCI_INTR_SF, &regs->intrdisable);
wdenkde887eb2003-09-10 18:20:28 +00001632 if (ohci->ed_rm_list[frame] != NULL)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001633 writel(OHCI_INTR_SF, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001634 stat = 0xff;
1635 }
1636
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001637 writel(ints, &regs->intrstatus);
wdenkde887eb2003-09-10 18:20:28 +00001638 return stat;
1639}
1640
1641/*-------------------------------------------------------------------------*/
1642
1643/*-------------------------------------------------------------------------*/
1644
1645/* De-allocate all resources.. */
1646
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001647static void hc_release_ohci(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001648{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001649 dbg("USB HC release ohci usb-%s", ohci->slot_name);
wdenkde887eb2003-09-10 18:20:28 +00001650
1651 if (!ohci->disabled)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001652 hc_reset(ohci);
wdenkde887eb2003-09-10 18:20:28 +00001653}
1654
1655/*-------------------------------------------------------------------------*/
1656
1657/*
1658 * low level initalisation routine, called from usb.c
1659 */
1660static char ohci_inited = 0;
1661
1662int usb_lowlevel_init(void)
1663{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001664 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1665 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenkde887eb2003-09-10 18:20:28 +00001666
wdenk9c53f402003-10-15 23:53:47 +00001667 /*
1668 * Set the 48 MHz UPLL clocking. Values are taken from
1669 * "PLL value selection guide", 6-23, s3c2400_UM.pdf.
1670 */
C Nauman383c43e2010-10-26 23:04:31 +09001671 clk_power->upllcon = ((40 << 12) + (1 << 4) + 2);
1672 gpio->misccr |= 0x8; /* 1 = use pads related USB for USB host */
wdenkde887eb2003-09-10 18:20:28 +00001673
wdenk9c53f402003-10-15 23:53:47 +00001674 /*
1675 * Enable USB host clock.
1676 */
C Nauman383c43e2010-10-26 23:04:31 +09001677 clk_power->clkcon |= (1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001678
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001679 memset(&gohci, 0, sizeof(struct ohci));
1680 memset(&urb_priv, 0, sizeof(struct urb_priv));
wdenkde887eb2003-09-10 18:20:28 +00001681
1682 /* align the storage */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001683 if ((__u32) &ghcca[0] & 0xff) {
wdenkde887eb2003-09-10 18:20:28 +00001684 err("HCCA not aligned!!");
1685 return -1;
1686 }
1687 phcca = &ghcca[0];
1688 info("aligned ghcca %p", phcca);
1689 memset(&ohci_dev, 0, sizeof(struct ohci_device));
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001690 if ((__u32) &ohci_dev.ed[0] & 0x7) {
wdenkde887eb2003-09-10 18:20:28 +00001691 err("EDs not aligned!!");
1692 return -1;
1693 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001694 memset(gtd, 0, sizeof(struct td) * (NUM_TD + 1));
1695 if ((__u32) gtd & 0x7) {
wdenkde887eb2003-09-10 18:20:28 +00001696 err("TDs not aligned!!");
1697 return -1;
1698 }
1699 ptd = gtd;
1700 gohci.hcca = phcca;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001701 memset(phcca, 0, sizeof(struct ohci_hcca));
wdenkde887eb2003-09-10 18:20:28 +00001702
1703 gohci.disabled = 1;
1704 gohci.sleeping = 0;
1705 gohci.irq = -1;
1706 gohci.regs = (struct ohci_regs *)S3C24X0_USB_HOST_BASE;
1707
1708 gohci.flags = 0;
1709 gohci.slot_name = "s3c2400";
1710
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001711 if (hc_reset(&gohci) < 0) {
1712 hc_release_ohci(&gohci);
wdenk9c53f402003-10-15 23:53:47 +00001713 /* Initialization failed */
C Nauman383c43e2010-10-26 23:04:31 +09001714 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001715 return -1;
1716 }
1717
1718 /* FIXME this is a second HC reset; why?? */
Wolfgang Denk10e4f542006-03-11 23:07:09 +01001719 gohci.hc_control = OHCI_USB_RESET;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001720 writel(gohci.hc_control, &gohci.regs->control);
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001721 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001722
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001723 if (hc_start(&gohci) < 0) {
1724 err("can't start usb-%s", gohci.slot_name);
1725 hc_release_ohci(&gohci);
wdenk9c53f402003-10-15 23:53:47 +00001726 /* Initialization failed */
C Nauman383c43e2010-10-26 23:04:31 +09001727 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001728 return -1;
1729 }
wdenkde887eb2003-09-10 18:20:28 +00001730#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001731 ohci_dump(&gohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001732#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001733 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001734#endif
1735 ohci_inited = 1;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001736 urb_finished = 1;
1737
wdenkde887eb2003-09-10 18:20:28 +00001738 return 0;
1739}
1740
1741int usb_lowlevel_stop(void)
1742{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001743 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
wdenkde887eb2003-09-10 18:20:28 +00001744
1745 /* this gets called really early - before the controller has */
1746 /* even been initialized! */
1747 if (!ohci_inited)
1748 return 0;
1749 /* TODO release any interrupts, etc. */
1750 /* call hc_release_ohci() here ? */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001751 hc_reset(&gohci);
wdenkde887eb2003-09-10 18:20:28 +00001752 /* may not want to do this */
C Nauman383c43e2010-10-26 23:04:31 +09001753 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001754 return 0;
1755}
1756
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +09001757#endif /* defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0) */