blob: d1e78f6b0c3c78901d45fe43faef782c604f1d91 [file] [log] [blame]
Stefan Roesec157d8e2005-08-01 16:41:48 +02001/*
Stefan Roese7770ce42005-08-02 10:05:21 +02002 * URB OHCI HCD (Host Controller Driver) for USB on the PPC440EP.
Stefan Roesec157d8e2005-08-01 16:41:48 +02003 *
4 * (C) Copyright 2003-2004
Detlev Zundel792a09e2009-05-13 10:54:10 +02005 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
Stefan Roesec157d8e2005-08-01 16:41:48 +02006 *
7 * (C) Copyright 2004
8 * Pierre Aubert, Staubli Faverges <p.aubert@staubli.com>
9 *
10 * Note: Much of this code has been derived from Linux 2.4
11 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
12 * (C) Copyright 2000-2002 David Brownell
13 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Stefan Roesec157d8e2005-08-01 16:41:48 +020015 */
16/*
17 * IMPORTANT NOTES
18 * 1 - this driver is intended for use with USB Mass Storage Devices
19 * (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
20 */
21
22#include <common.h>
23
24#ifdef CONFIG_USB_OHCI
25
26#include <malloc.h>
27#include <usb.h>
28#include "usb_ohci.h"
Stefan Roese7770ce42005-08-02 10:05:21 +020029
Stefan Roesec157d8e2005-08-01 16:41:48 +020030#define OHCI_USE_NPS /* force NoPowerSwitching mode */
31#undef OHCI_VERBOSE_DEBUG /* not always helpful */
32#undef DEBUG
33#undef SHOW_INFO
34#undef OHCI_FILL_TRACE
35
36/* For initializing controller (mask in an HCFS mode too) */
37#define OHCI_CONTROL_INIT \
38 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
39
Wolfgang Denkd00ce092008-04-25 12:44:08 +020040#define readl(a) (*((volatile u32 *)(a)))
41#define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
Stefan Roesec157d8e2005-08-01 16:41:48 +020042
43#define min_t(type,x,y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
44
45#ifdef DEBUG
46#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
47#else
48#define dbg(format, arg...) do {} while(0)
Stefan Roese7770ce42005-08-02 10:05:21 +020049#endif /* DEBUG */
Stefan Roesec157d8e2005-08-01 16:41:48 +020050#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
51#ifdef SHOW_INFO
52#define info(format, arg...) printf("INFO: " format "\n", ## arg)
53#else
54#define info(format, arg...) do {} while(0)
55#endif
56
57#define m16_swap(x) swap_16(x)
58#define m32_swap(x) swap_32(x)
59
Stefan Roesee01bd212007-03-21 13:38:59 +010060#if defined(CONFIG_405EZ) || defined(CONFIG_440EP) || defined(CONFIG_440EPX)
Stefan Roesec157d8e2005-08-01 16:41:48 +020061#define ohci_cpu_to_le16(x) (x)
62#define ohci_cpu_to_le32(x) (x)
63#else
64#define ohci_cpu_to_le16(x) swap_16(x)
65#define ohci_cpu_to_le32(x) swap_32(x)
66#endif
67
68/* global ohci_t */
69static ohci_t gohci;
70/* this must be aligned to a 256 byte boundary */
71struct ohci_hcca ghcca[1];
72/* a pointer to the aligned storage */
73struct ohci_hcca *phcca;
74/* this allocates EDs for all possible endpoints */
75struct ohci_device ohci_dev;
76/* urb_priv */
77urb_priv_t urb_priv;
78/* RHSC flag */
79int got_rhsc;
80/* device which was disconnected */
81struct usb_device *devgone;
Stefan Roese7770ce42005-08-02 10:05:21 +020082/* flag guarding URB transation */
83int urb_finished = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +020084
85/*-------------------------------------------------------------------------*/
86
87/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
88 * The erratum (#4) description is incorrect. AMD's workaround waits
89 * till some bits (mostly reserved) are clear; ok for all revs.
90 */
91#define OHCI_QUIRK_AMD756 0xabcd
92#define read_roothub(hc, register, mask) ({ \
93 u32 temp = readl (&hc->regs->roothub.register); \
94 if (hc->flags & OHCI_QUIRK_AMD756) \
95 while (temp & mask) \
96 temp = readl (&hc->regs->roothub.register); \
97 temp; })
98
Stefan Roese7770ce42005-08-02 10:05:21 +020099static u32 roothub_a (struct ohci *hc)
100 { return read_roothub (hc, a, 0xfc0fe000); }
101static inline u32 roothub_b (struct ohci *hc)
102 { return readl (&hc->regs->roothub.b); }
103static inline u32 roothub_status (struct ohci *hc)
104 { return readl (&hc->regs->roothub.status); }
105static u32 roothub_portstatus (struct ohci *hc, int i)
106 { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
107
Stefan Roesec157d8e2005-08-01 16:41:48 +0200108
109/* forward declaration */
Stefan Roese7770ce42005-08-02 10:05:21 +0200110static int hc_interrupt (void);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200111static void
Stefan Roese7770ce42005-08-02 10:05:21 +0200112td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
113 int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200114
115/*-------------------------------------------------------------------------*
116 * URB support functions
117 *-------------------------------------------------------------------------*/
118
119/* free HCD-private data associated with this URB */
120
Stefan Roese7770ce42005-08-02 10:05:21 +0200121static void urb_free_priv (urb_priv_t * urb)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200122{
Stefan Roese7770ce42005-08-02 10:05:21 +0200123 int i;
124 int last;
125 struct td * td;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200126
127 last = urb->length - 1;
128 if (last >= 0) {
129 for (i = 0; i <= last; i++) {
130 td = urb->td[i];
131 if (td) {
132 td->usb_dev = NULL;
133 urb->td[i] = NULL;
134 }
135 }
136 }
137}
138
139/*-------------------------------------------------------------------------*/
140
141#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +0200142static int sohci_get_current_frame_number (struct usb_device * dev);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200143
144/* debug| print the main components of an URB
145 * small: 0) header + data packets 1) just header */
146
Stefan Roese7770ce42005-08-02 10:05:21 +0200147static void pkt_print (struct usb_device * dev, unsigned long pipe, void * buffer,
148 int transfer_len, struct devrequest * setup, char * str, int small)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200149{
Stefan Roese7770ce42005-08-02 10:05:21 +0200150 urb_priv_t * purb = &urb_priv;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200151
152 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
Stefan Roese7770ce42005-08-02 10:05:21 +0200153 str,
154 sohci_get_current_frame_number (dev),
155 usb_pipedevice (pipe),
156 usb_pipeendpoint (pipe),
157 usb_pipeout (pipe)? 'O': 'I',
158 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
159 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
160 purb->actual_length,
161 transfer_len, dev->status);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200162#ifdef OHCI_VERBOSE_DEBUG
163 if (!small) {
164 int i, len;
165
Stefan Roese7770ce42005-08-02 10:05:21 +0200166 if (usb_pipecontrol (pipe)) {
167 printf (__FILE__ ": cmd(8):");
168 for (i = 0; i < 8 ; i++)
169 printf (" %02x", ((__u8 *) setup) [i]);
170 printf ("\n");
Stefan Roesec157d8e2005-08-01 16:41:48 +0200171 }
172 if (transfer_len > 0 && buffer) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200173 printf (__FILE__ ": data(%d/%d):",
174 purb->actual_length,
175 transfer_len);
176 len = usb_pipeout (pipe)?
177 transfer_len: purb->actual_length;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200178 for (i = 0; i < 16 && i < len; i++)
Stefan Roese7770ce42005-08-02 10:05:21 +0200179 printf (" %02x", ((__u8 *) buffer) [i]);
180 printf ("%s\n", i < len? "...": "");
Stefan Roesec157d8e2005-08-01 16:41:48 +0200181 }
182 }
183#endif
184}
185
186/* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
Stefan Roese7770ce42005-08-02 10:05:21 +0200187void ep_print_int_eds (ohci_t *ohci, char * str) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200188 int i, j;
Stefan Roese7770ce42005-08-02 10:05:21 +0200189 __u32 * ed_p;
190 for (i= 0; i < 32; i++) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200191 j = 5;
Stefan Roese7770ce42005-08-02 10:05:21 +0200192 ed_p = &(ohci->hcca->int_table [i]);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200193 if (*ed_p == 0)
Stefan Roese7770ce42005-08-02 10:05:21 +0200194 continue;
195 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200196 while (*ed_p != 0 && j--) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200197 ed_t *ed = (ed_t *)ohci_cpu_to_le32(ed_p);
198 printf (" ed: %4x;", ed->hwINFO);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200199 ed_p = &ed->hwNextED;
200 }
Stefan Roese7770ce42005-08-02 10:05:21 +0200201 printf ("\n");
Stefan Roesec157d8e2005-08-01 16:41:48 +0200202 }
203}
204
Stefan Roese7770ce42005-08-02 10:05:21 +0200205static void ohci_dump_intr_mask (char *label, __u32 mask)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200206{
Stefan Roese7770ce42005-08-02 10:05:21 +0200207 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
208 label,
209 mask,
210 (mask & OHCI_INTR_MIE) ? " MIE" : "",
211 (mask & OHCI_INTR_OC) ? " OC" : "",
212 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
213 (mask & OHCI_INTR_FNO) ? " FNO" : "",
214 (mask & OHCI_INTR_UE) ? " UE" : "",
215 (mask & OHCI_INTR_RD) ? " RD" : "",
216 (mask & OHCI_INTR_SF) ? " SF" : "",
217 (mask & OHCI_INTR_WDH) ? " WDH" : "",
218 (mask & OHCI_INTR_SO) ? " SO" : ""
219 );
Stefan Roesec157d8e2005-08-01 16:41:48 +0200220}
221
Stefan Roese7770ce42005-08-02 10:05:21 +0200222static void maybe_print_eds (char *label, __u32 value)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200223{
Stefan Roese7770ce42005-08-02 10:05:21 +0200224 ed_t *edp = (ed_t *)value;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200225
226 if (value) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200227 dbg ("%s %08x", label, value);
228 dbg ("%08x", edp->hwINFO);
229 dbg ("%08x", edp->hwTailP);
230 dbg ("%08x", edp->hwHeadP);
231 dbg ("%08x", edp->hwNextED);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200232 }
233}
234
Stefan Roese7770ce42005-08-02 10:05:21 +0200235static char * hcfs2string (int state)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200236{
237 switch (state) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200238 case OHCI_USB_RESET: return "reset";
239 case OHCI_USB_RESUME: return "resume";
240 case OHCI_USB_OPER: return "operational";
241 case OHCI_USB_SUSPEND: return "suspend";
Stefan Roesec157d8e2005-08-01 16:41:48 +0200242 }
243 return "?";
244}
245
246/* dump control and status registers */
Stefan Roese7770ce42005-08-02 10:05:21 +0200247static void ohci_dump_status (ohci_t *controller)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200248{
Stefan Roese7770ce42005-08-02 10:05:21 +0200249 struct ohci_regs *regs = controller->regs;
250 __u32 temp;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200251
Stefan Roese7770ce42005-08-02 10:05:21 +0200252 temp = readl (&regs->revision) & 0xff;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200253 if (temp != 0x10)
Stefan Roese7770ce42005-08-02 10:05:21 +0200254 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200255
Stefan Roese7770ce42005-08-02 10:05:21 +0200256 temp = readl (&regs->control);
257 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
258 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
259 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
260 (temp & OHCI_CTRL_IR) ? " IR" : "",
261 hcfs2string (temp & OHCI_CTRL_HCFS),
262 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
263 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
264 (temp & OHCI_CTRL_IE) ? " IE" : "",
265 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
266 temp & OHCI_CTRL_CBSR
267 );
Stefan Roesec157d8e2005-08-01 16:41:48 +0200268
Stefan Roese7770ce42005-08-02 10:05:21 +0200269 temp = readl (&regs->cmdstatus);
270 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
271 (temp & OHCI_SOC) >> 16,
272 (temp & OHCI_OCR) ? " OCR" : "",
273 (temp & OHCI_BLF) ? " BLF" : "",
274 (temp & OHCI_CLF) ? " CLF" : "",
275 (temp & OHCI_HCR) ? " HCR" : ""
276 );
Stefan Roesec157d8e2005-08-01 16:41:48 +0200277
Stefan Roese7770ce42005-08-02 10:05:21 +0200278 ohci_dump_intr_mask ("intrstatus", readl (&regs->intrstatus));
279 ohci_dump_intr_mask ("intrenable", readl (&regs->intrenable));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200280
Stefan Roese7770ce42005-08-02 10:05:21 +0200281 maybe_print_eds ("ed_periodcurrent", readl (&regs->ed_periodcurrent));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200282
Stefan Roese7770ce42005-08-02 10:05:21 +0200283 maybe_print_eds ("ed_controlhead", readl (&regs->ed_controlhead));
284 maybe_print_eds ("ed_controlcurrent", readl (&regs->ed_controlcurrent));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200285
Stefan Roese7770ce42005-08-02 10:05:21 +0200286 maybe_print_eds ("ed_bulkhead", readl (&regs->ed_bulkhead));
287 maybe_print_eds ("ed_bulkcurrent", readl (&regs->ed_bulkcurrent));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200288
Stefan Roese7770ce42005-08-02 10:05:21 +0200289 maybe_print_eds ("donehead", readl (&regs->donehead));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200290}
291
Stefan Roese7770ce42005-08-02 10:05:21 +0200292static void ohci_dump_roothub (ohci_t *controller, int verbose)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200293{
Stefan Roese7770ce42005-08-02 10:05:21 +0200294 __u32 temp, ndp, i;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200295
Stefan Roese7770ce42005-08-02 10:05:21 +0200296 temp = roothub_a (controller);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200297 ndp = (temp & RH_A_NDP);
298
299 if (verbose) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200300 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
301 ((temp & RH_A_POTPGT) >> 24) & 0xff,
302 (temp & RH_A_NOCP) ? " NOCP" : "",
303 (temp & RH_A_OCPM) ? " OCPM" : "",
304 (temp & RH_A_DT) ? " DT" : "",
305 (temp & RH_A_NPS) ? " NPS" : "",
306 (temp & RH_A_PSM) ? " PSM" : "",
307 ndp
308 );
309 temp = roothub_b (controller);
310 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
311 temp,
312 (temp & RH_B_PPCM) >> 16,
313 (temp & RH_B_DR)
314 );
315 temp = roothub_status (controller);
316 dbg ("roothub.status: %08x%s%s%s%s%s%s",
317 temp,
318 (temp & RH_HS_CRWE) ? " CRWE" : "",
319 (temp & RH_HS_OCIC) ? " OCIC" : "",
320 (temp & RH_HS_LPSC) ? " LPSC" : "",
321 (temp & RH_HS_DRWE) ? " DRWE" : "",
322 (temp & RH_HS_OCI) ? " OCI" : "",
323 (temp & RH_HS_LPS) ? " LPS" : ""
324 );
Stefan Roesec157d8e2005-08-01 16:41:48 +0200325 }
326
327 for (i = 0; i < ndp; i++) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200328 temp = roothub_portstatus (controller, i);
329 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
330 i,
331 temp,
332 (temp & RH_PS_PRSC) ? " PRSC" : "",
333 (temp & RH_PS_OCIC) ? " OCIC" : "",
334 (temp & RH_PS_PSSC) ? " PSSC" : "",
335 (temp & RH_PS_PESC) ? " PESC" : "",
336 (temp & RH_PS_CSC) ? " CSC" : "",
337
338 (temp & RH_PS_LSDA) ? " LSDA" : "",
339 (temp & RH_PS_PPS) ? " PPS" : "",
340 (temp & RH_PS_PRS) ? " PRS" : "",
341 (temp & RH_PS_POCI) ? " POCI" : "",
342 (temp & RH_PS_PSS) ? " PSS" : "",
343
344 (temp & RH_PS_PES) ? " PES" : "",
345 (temp & RH_PS_CCS) ? " CCS" : ""
346 );
Stefan Roesec157d8e2005-08-01 16:41:48 +0200347 }
348}
349
Stefan Roese7770ce42005-08-02 10:05:21 +0200350static void ohci_dump (ohci_t *controller, int verbose)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200351{
Stefan Roese7770ce42005-08-02 10:05:21 +0200352 dbg ("OHCI controller usb-%s state", controller->slot_name);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200353
354 /* dumps some of the state we know about */
Stefan Roese7770ce42005-08-02 10:05:21 +0200355 ohci_dump_status (controller);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200356 if (verbose)
Stefan Roese7770ce42005-08-02 10:05:21 +0200357 ep_print_int_eds (controller, "hcca");
358 dbg ("hcca frame #%04x", controller->hcca->frame_no);
359 ohci_dump_roothub (controller, 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200360}
361
Stefan Roese7770ce42005-08-02 10:05:21 +0200362
363#endif /* DEBUG */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200364
365/*-------------------------------------------------------------------------*
366 * Interface functions (URB)
367 *-------------------------------------------------------------------------*/
368
369/* get a transfer request */
370
371int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
Stefan Roese7770ce42005-08-02 10:05:21 +0200372 int transfer_len, struct devrequest *setup, int interval)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200373{
374 ohci_t *ohci;
Stefan Roese7770ce42005-08-02 10:05:21 +0200375 ed_t * ed;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200376 urb_priv_t *purb_priv;
377 int i, size = 0;
378
379 ohci = &gohci;
380
381 /* when controller's hung, permit only roothub cleanup attempts
382 * such as powering down ports */
383 if (ohci->disabled) {
384 err("sohci_submit_job: EPIPE");
385 return -1;
386 }
387
Stefan Roese7770ce42005-08-02 10:05:21 +0200388 /* if we have an unfinished URB from previous transaction let's
389 * fail and scream as quickly as possible so as not to corrupt
390 * further communication */
391 if (!urb_finished) {
392 err("sohci_submit_job: URB NOT FINISHED");
393 return -1;
394 }
395 /* we're about to begin a new transaction here so mark the URB unfinished */
396 urb_finished = 0;
397
Stefan Roesec157d8e2005-08-01 16:41:48 +0200398 /* every endpoint has a ed, locate and fill it */
Stefan Roese7770ce42005-08-02 10:05:21 +0200399 if (!(ed = ep_add_ed (dev, pipe))) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200400 err("sohci_submit_job: ENOMEM");
401 return -1;
402 }
403
404 /* for the private part of the URB we need the number of TDs (size) */
Stefan Roese7770ce42005-08-02 10:05:21 +0200405 switch (usb_pipetype (pipe)) {
406 case PIPE_BULK: /* one TD for every 4096 Byte */
407 size = (transfer_len - 1) / 4096 + 1;
408 break;
409 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
410 size = (transfer_len == 0)? 2:
411 (transfer_len - 1) / 4096 + 3;
412 break;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200413 }
414
415 if (size >= (N_URB_TD - 1)) {
416 err("need %d TDs, only have %d", size, N_URB_TD);
417 return -1;
418 }
419 purb_priv = &urb_priv;
420 purb_priv->pipe = pipe;
421
422 /* fill the private part of the URB */
423 purb_priv->length = size;
424 purb_priv->ed = ed;
425 purb_priv->actual_length = 0;
426
427 /* allocate the TDs */
428 /* note that td[0] was allocated in ep_add_ed */
429 for (i = 0; i < size; i++) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200430 purb_priv->td[i] = td_alloc (dev);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200431 if (!purb_priv->td[i]) {
432 purb_priv->length = i;
Stefan Roese7770ce42005-08-02 10:05:21 +0200433 urb_free_priv (purb_priv);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200434 err("sohci_submit_job: ENOMEM");
435 return -1;
436 }
437 }
438
439 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200440 urb_free_priv (purb_priv);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200441 err("sohci_submit_job: EINVAL");
442 return -1;
443 }
444
445 /* link the ed into a chain if is not already */
446 if (ed->state != ED_OPER)
Stefan Roese7770ce42005-08-02 10:05:21 +0200447 ep_link (ohci, ed);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200448
449 /* fill the TDs and link it to the ed */
Stefan Roese7770ce42005-08-02 10:05:21 +0200450 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200451
452 return 0;
453}
454
455/*-------------------------------------------------------------------------*/
456
457#ifdef DEBUG
458/* tell us the current USB frame number */
459
Stefan Roese7770ce42005-08-02 10:05:21 +0200460static int sohci_get_current_frame_number (struct usb_device *usb_dev)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200461{
462 ohci_t *ohci = &gohci;
463
Stefan Roese7770ce42005-08-02 10:05:21 +0200464 return ohci_cpu_to_le16 (ohci->hcca->frame_no);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200465}
466#endif
467
468/*-------------------------------------------------------------------------*
469 * ED handling functions
470 *-------------------------------------------------------------------------*/
471
472/* link an ed into one of the HC chains */
473
Stefan Roese7770ce42005-08-02 10:05:21 +0200474static int ep_link (ohci_t *ohci, ed_t *edi)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200475{
476 volatile ed_t *ed = edi;
477
478 ed->state = ED_OPER;
479
480 switch (ed->type) {
481 case PIPE_CONTROL:
482 ed->hwNextED = 0;
483 if (ohci->ed_controltail == NULL) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200484 writel (ed, &ohci->regs->ed_controlhead);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200485 } else {
Stefan Roese7770ce42005-08-02 10:05:21 +0200486 ohci->ed_controltail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200487 }
488 ed->ed_prev = ohci->ed_controltail;
489 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
Stefan Roese7770ce42005-08-02 10:05:21 +0200490 !ohci->ed_rm_list[1] && !ohci->sleeping) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200491 ohci->hc_control |= OHCI_CTRL_CLE;
Stefan Roese7770ce42005-08-02 10:05:21 +0200492 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200493 }
494 ohci->ed_controltail = edi;
495 break;
496
497 case PIPE_BULK:
498 ed->hwNextED = 0;
499 if (ohci->ed_bulktail == NULL) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200500 writel (ed, &ohci->regs->ed_bulkhead);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200501 } else {
Stefan Roese7770ce42005-08-02 10:05:21 +0200502 ohci->ed_bulktail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200503 }
504 ed->ed_prev = ohci->ed_bulktail;
505 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
Stefan Roese7770ce42005-08-02 10:05:21 +0200506 !ohci->ed_rm_list[1] && !ohci->sleeping) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200507 ohci->hc_control |= OHCI_CTRL_BLE;
Stefan Roese7770ce42005-08-02 10:05:21 +0200508 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200509 }
510 ohci->ed_bulktail = edi;
511 break;
512 }
513 return 0;
514}
515
516/*-------------------------------------------------------------------------*/
517
518/* unlink an ed from one of the HC chains.
519 * just the link to the ed is unlinked.
520 * the link from the ed still points to another operational ed or 0
521 * so the HC can eventually finish the processing of the unlinked ed */
522
Stefan Roese7770ce42005-08-02 10:05:21 +0200523static int ep_unlink (ohci_t *ohci, ed_t *edi)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200524{
525 volatile ed_t *ed = edi;
526
Stefan Roese7770ce42005-08-02 10:05:21 +0200527 ed->hwINFO |= ohci_cpu_to_le32 (OHCI_ED_SKIP);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200528
529 switch (ed->type) {
530 case PIPE_CONTROL:
531 if (ed->ed_prev == NULL) {
532 if (!ed->hwNextED) {
533 ohci->hc_control &= ~OHCI_CTRL_CLE;
Stefan Roese7770ce42005-08-02 10:05:21 +0200534 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200535 }
Stefan Roese7770ce42005-08-02 10:05:21 +0200536 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200537 } else {
538 ed->ed_prev->hwNextED = ed->hwNextED;
539 }
540 if (ohci->ed_controltail == ed) {
541 ohci->ed_controltail = ed->ed_prev;
542 } else {
Stefan Roese7770ce42005-08-02 10:05:21 +0200543 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200544 }
545 break;
546
547 case PIPE_BULK:
548 if (ed->ed_prev == NULL) {
549 if (!ed->hwNextED) {
550 ohci->hc_control &= ~OHCI_CTRL_BLE;
Stefan Roese7770ce42005-08-02 10:05:21 +0200551 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200552 }
Stefan Roese7770ce42005-08-02 10:05:21 +0200553 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200554 } else {
555 ed->ed_prev->hwNextED = ed->hwNextED;
556 }
557 if (ohci->ed_bulktail == ed) {
558 ohci->ed_bulktail = ed->ed_prev;
559 } else {
Stefan Roese7770ce42005-08-02 10:05:21 +0200560 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200561 }
562 break;
563 }
564 ed->state = ED_UNLINK;
565 return 0;
566}
567
Stefan Roese7770ce42005-08-02 10:05:21 +0200568
Stefan Roesec157d8e2005-08-01 16:41:48 +0200569/*-------------------------------------------------------------------------*/
570
571/* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
572 * but the USB stack is a little bit stateless so we do it at every transaction
573 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
574 * in all other cases the state is left unchanged
575 * the ed info fields are setted anyway even though most of them should not change */
576
Stefan Roese7770ce42005-08-02 10:05:21 +0200577static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200578{
579 td_t *td;
580 ed_t *ed_ret;
581 volatile ed_t *ed;
582
Stefan Roese7770ce42005-08-02 10:05:21 +0200583 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
584 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
Stefan Roesec157d8e2005-08-01 16:41:48 +0200585
586 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
587 err("ep_add_ed: pending delete");
588 /* pending delete request */
589 return NULL;
590 }
591
592 if (ed->state == ED_NEW) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200593 ed->hwINFO = ohci_cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200594 /* dummy td; end of td list for ed */
Stefan Roese7770ce42005-08-02 10:05:21 +0200595 td = td_alloc (usb_dev);
596 ed->hwTailP = ohci_cpu_to_le32 ((unsigned long)td);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200597 ed->hwHeadP = ed->hwTailP;
598 ed->state = ED_UNLINK;
Stefan Roese7770ce42005-08-02 10:05:21 +0200599 ed->type = usb_pipetype (pipe);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200600 ohci_dev.ed_cnt++;
601 }
602
Stefan Roese7770ce42005-08-02 10:05:21 +0200603 ed->hwINFO = ohci_cpu_to_le32 (usb_pipedevice (pipe)
604 | usb_pipeendpoint (pipe) << 7
605 | (usb_pipeisoc (pipe)? 0x8000: 0)
606 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
Ilya Yanokc60795f2012-11-06 13:48:20 +0000607 | (usb_dev->speed == USB_SPEED_LOW) << 13
Stefan Roese7770ce42005-08-02 10:05:21 +0200608 | usb_maxpacket (usb_dev, pipe) << 16);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200609
610 return ed_ret;
611}
612
613/*-------------------------------------------------------------------------*
614 * TD handling functions
615 *-------------------------------------------------------------------------*/
616
617/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
618
Stefan Roese7770ce42005-08-02 10:05:21 +0200619static void td_fill (ohci_t *ohci, unsigned int info,
620 void *data, int len,
621 struct usb_device *dev, int index, urb_priv_t *urb_priv)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200622{
Stefan Roese7770ce42005-08-02 10:05:21 +0200623 volatile td_t *td, *td_pt;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200624#ifdef OHCI_FILL_TRACE
625 int i;
626#endif
627
628 if (index > urb_priv->length) {
629 err("index > length");
630 return;
631 }
632 /* use this td as the next dummy */
Stefan Roese7770ce42005-08-02 10:05:21 +0200633 td_pt = urb_priv->td [index];
Stefan Roesec157d8e2005-08-01 16:41:48 +0200634 td_pt->hwNextTD = 0;
635
636 /* fill the old dummy TD */
Stefan Roese7770ce42005-08-02 10:05:21 +0200637 td = urb_priv->td [index] = (td_t *)(ohci_cpu_to_le32 (urb_priv->ed->hwTailP) & ~0xf);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200638
639 td->ed = urb_priv->ed;
640 td->next_dl_td = NULL;
641 td->index = index;
Stefan Roese7770ce42005-08-02 10:05:21 +0200642 td->data = (__u32)data;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200643#ifdef OHCI_FILL_TRACE
Remy Bohmer9dbc3662008-10-10 10:23:22 +0200644 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200645 for (i = 0; i < len; i++)
Stefan Roese7770ce42005-08-02 10:05:21 +0200646 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200647 printf("\n");
648 }
649#endif
650 if (!len)
651 data = 0;
652
Stefan Roese7770ce42005-08-02 10:05:21 +0200653 td->hwINFO = ohci_cpu_to_le32 (info);
654 td->hwCBP = ohci_cpu_to_le32 ((unsigned long)data);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200655 if (data)
Stefan Roese7770ce42005-08-02 10:05:21 +0200656 td->hwBE = ohci_cpu_to_le32 ((unsigned long)(data + len - 1));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200657 else
658 td->hwBE = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +0200659 td->hwNextTD = ohci_cpu_to_le32 ((unsigned long)td_pt);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200660
661 /* append to queue */
662 td->ed->hwTailP = td->hwNextTD;
663}
664
665/*-------------------------------------------------------------------------*/
666
667/* prepare all TDs of a transfer */
Stefan Roese7770ce42005-08-02 10:05:21 +0200668static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
669 int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200670{
671 ohci_t *ohci = &gohci;
672 int data_len = transfer_len;
673 void *data;
674 int cnt = 0;
675 __u32 info = 0;
676 unsigned int toggle = 0;
677
678 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
Stefan Roese7770ce42005-08-02 10:05:21 +0200679 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200680 toggle = TD_T_TOGGLE;
681 } else {
682 toggle = TD_T_DATA0;
Stefan Roese7770ce42005-08-02 10:05:21 +0200683 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200684 }
685 urb->td_cnt = 0;
686 if (data_len)
687 data = buffer;
688 else
689 data = 0;
690
Stefan Roese7770ce42005-08-02 10:05:21 +0200691 switch (usb_pipetype (pipe)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200692 case PIPE_BULK:
Stefan Roese7770ce42005-08-02 10:05:21 +0200693 info = usb_pipeout (pipe)?
694 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
695 while(data_len > 4096) {
696 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
697 data += 4096; data_len -= 4096; cnt++;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200698 }
Stefan Roese7770ce42005-08-02 10:05:21 +0200699 info = usb_pipeout (pipe)?
700 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
701 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200702 cnt++;
703
704 if (!ohci->sleeping)
Stefan Roese7770ce42005-08-02 10:05:21 +0200705 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200706 break;
707
708 case PIPE_CONTROL:
709 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Stefan Roese7770ce42005-08-02 10:05:21 +0200710 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200711 if (data_len > 0) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200712 info = usb_pipeout (pipe)?
713 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200714 /* NOTE: mishandles transfers >8K, some >4K */
Stefan Roese7770ce42005-08-02 10:05:21 +0200715 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200716 }
Stefan Roese7770ce42005-08-02 10:05:21 +0200717 info = usb_pipeout (pipe)?
718 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
719 td_fill (ohci, info, data, 0, dev, cnt++, urb);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200720 if (!ohci->sleeping)
Stefan Roese7770ce42005-08-02 10:05:21 +0200721 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200722 break;
723 }
724 if (urb->length != cnt)
725 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
726}
727
728/*-------------------------------------------------------------------------*
729 * Done List handling functions
730 *-------------------------------------------------------------------------*/
731
Stefan Roese7770ce42005-08-02 10:05:21 +0200732
Stefan Roesec157d8e2005-08-01 16:41:48 +0200733/* calculate the transfer length and update the urb */
734
735static void dl_transfer_length(td_t * td)
736{
Stefan Roeseaf0af522011-11-15 08:01:45 +0000737 __u32 tdBE, tdCBP;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200738 urb_priv_t *lurb_priv = &urb_priv;
739
Stefan Roese7770ce42005-08-02 10:05:21 +0200740 tdBE = ohci_cpu_to_le32 (td->hwBE);
741 tdCBP = ohci_cpu_to_le32 (td->hwCBP);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200742
Stefan Roese7770ce42005-08-02 10:05:21 +0200743
Remy Bohmer9dbc3662008-10-10 10:23:22 +0200744 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Stefan Roese7770ce42005-08-02 10:05:21 +0200745 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200746 if (tdBE != 0) {
747 if (td->hwCBP == 0)
748 lurb_priv->actual_length += tdBE - td->data + 1;
749 else
750 lurb_priv->actual_length += tdCBP - td->data;
751 }
752 }
753}
754
755/*-------------------------------------------------------------------------*/
756
757/* replies to the request have to be on a FIFO basis so
758 * we reverse the reversed done-list */
759
Stefan Roese7770ce42005-08-02 10:05:21 +0200760static td_t * dl_reverse_done_list (ohci_t *ohci)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200761{
762 __u32 td_list_hc;
763 td_t *td_rev = NULL;
764 td_t *td_list = NULL;
765 urb_priv_t *lurb_priv = NULL;
766
Stefan Roese7770ce42005-08-02 10:05:21 +0200767 td_list_hc = ohci_cpu_to_le32 (ohci->hcca->done_head) & 0xfffffff0;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200768 ohci->hcca->done_head = 0;
769
770 while (td_list_hc) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200771 td_list = (td_t *)td_list_hc;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200772
Stefan Roese7770ce42005-08-02 10:05:21 +0200773 if (TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO))) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200774 lurb_priv = &urb_priv;
775 dbg(" USB-error/status: %x : %p",
Stefan Roese7770ce42005-08-02 10:05:21 +0200776 TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO)), td_list);
777 if (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x1)) {
778 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200779 td_list->ed->hwHeadP =
Stefan Roese7770ce42005-08-02 10:05:21 +0200780 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & ohci_cpu_to_le32 (0xfffffff0)) |
781 (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x2));
782 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200783 } else
Stefan Roese7770ce42005-08-02 10:05:21 +0200784 td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200785 }
786#ifdef CONFIG_MPC5200
787 td_list->hwNextTD = 0;
788#endif
789 }
790
791 td_list->next_dl_td = td_rev;
792 td_rev = td_list;
Stefan Roese7770ce42005-08-02 10:05:21 +0200793 td_list_hc = ohci_cpu_to_le32 (td_list->hwNextTD) & 0xfffffff0;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200794 }
795 return td_list;
796}
797
798/*-------------------------------------------------------------------------*/
799
800/* td done list */
Stefan Roese7770ce42005-08-02 10:05:21 +0200801static int dl_done_list (ohci_t *ohci, td_t *td_list)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200802{
803 td_t *td_list_next = NULL;
804 ed_t *ed;
805 int cc = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +0200806 int stat = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200807 /* urb_t *urb; */
808 urb_priv_t *lurb_priv;
809 __u32 tdINFO, edHeadP, edTailP;
810
811 while (td_list) {
812 td_list_next = td_list->next_dl_td;
813
814 lurb_priv = &urb_priv;
Stefan Roese7770ce42005-08-02 10:05:21 +0200815 tdINFO = ohci_cpu_to_le32 (td_list->hwINFO);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200816
817 ed = td_list->ed;
818
819 dl_transfer_length(td_list);
820
821 /* error code of transfer */
Stefan Roese7770ce42005-08-02 10:05:21 +0200822 cc = TD_CC_GET (tdINFO);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200823 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
824 if ((ed->state & (ED_OPER | ED_UNLINK))
Stefan Roese7770ce42005-08-02 10:05:21 +0200825 && (lurb_priv->state != URB_DEL)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200826 dbg("ConditionCode %#x", cc);
827 stat = cc_to_error[cc];
Stefan Roese7770ce42005-08-02 10:05:21 +0200828 urb_finished = 1;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200829 }
830 }
831
832 if (ed->state != ED_NEW) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200833 edHeadP = ohci_cpu_to_le32 (ed->hwHeadP) & 0xfffffff0;
834 edTailP = ohci_cpu_to_le32 (ed->hwTailP);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200835
836 /* unlink eds if they are not busy */
837 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
Stefan Roese7770ce42005-08-02 10:05:21 +0200838 ep_unlink (ohci, ed);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200839 }
840
841 td_list = td_list_next;
842 }
843 return stat;
844}
845
846/*-------------------------------------------------------------------------*
847 * Virtual Root Hub
848 *-------------------------------------------------------------------------*/
849
Stephen Warreneb838e72014-02-13 21:15:18 -0700850#include <usbroothubdes.h>
Stefan Roesec157d8e2005-08-01 16:41:48 +0200851
852/* Hub class-specific descriptor is constructed dynamically */
853
Stefan Roese7770ce42005-08-02 10:05:21 +0200854
Stefan Roesec157d8e2005-08-01 16:41:48 +0200855/*-------------------------------------------------------------------------*/
856
857#define OK(x) len = (x); break
858#ifdef DEBUG
859#define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
860#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
861#else
862#define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
863#define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
864#endif
865#define RD_RH_STAT roothub_status(&gohci)
866#define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
867
868/* request to virtual root hub */
869
Stefan Roese7770ce42005-08-02 10:05:21 +0200870int rh_check_port_status(ohci_t *controller)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200871{
872 __u32 temp, ndp, i;
873 int res;
874
875 res = -1;
Stefan Roese7770ce42005-08-02 10:05:21 +0200876 temp = roothub_a (controller);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200877 ndp = (temp & RH_A_NDP);
878 for (i = 0; i < ndp; i++) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200879 temp = roothub_portstatus (controller, i);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200880 /* check for a device disconnect */
881 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
Stefan Roese7770ce42005-08-02 10:05:21 +0200882 (RH_PS_PESC | RH_PS_CSC)) &&
883 ((temp & RH_PS_CCS) == 0)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200884 res = i;
885 break;
886 }
887 }
888 return res;
889}
890
891static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
Stefan Roese7770ce42005-08-02 10:05:21 +0200892 void *buffer, int transfer_len, struct devrequest *cmd)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200893{
Stefan Roese7770ce42005-08-02 10:05:21 +0200894 void * data = buffer;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200895 int leni = transfer_len;
896 int len = 0;
897 int stat = 0;
898 __u32 datab[4];
Stefan Roese7770ce42005-08-02 10:05:21 +0200899 __u8 *data_buf = (__u8 *)datab;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200900 __u16 bmRType_bReq;
901 __u16 wValue;
902 __u16 wIndex;
903 __u16 wLength;
904
905#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +0200906urb_priv.actual_length = 0;
907pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200908#endif
Remy Bohmer9dbc3662008-10-10 10:23:22 +0200909 if (usb_pipeint(pipe)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +0200910 info("Root-Hub submit IRQ: NOT implemented");
911 return 0;
912 }
913
Stefan Roese7770ce42005-08-02 10:05:21 +0200914 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
915 wValue = m16_swap (cmd->value);
916 wIndex = m16_swap (cmd->index);
917 wLength = m16_swap (cmd->length);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200918
919 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
Stefan Roese7770ce42005-08-02 10:05:21 +0200920 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200921
922 switch (bmRType_bReq) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200923 /* Request Destination:
924 without flags: Device,
925 RH_INTERFACE: interface,
926 RH_ENDPOINT: endpoint,
927 RH_CLASS means HUB here,
928 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
929 */
Stefan Roesec157d8e2005-08-01 16:41:48 +0200930
931 case RH_GET_STATUS:
Stefan Roese7770ce42005-08-02 10:05:21 +0200932 *(__u16 *) data_buf = m16_swap (1); OK (2);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200933 case RH_GET_STATUS | RH_INTERFACE:
Stefan Roese7770ce42005-08-02 10:05:21 +0200934 *(__u16 *) data_buf = m16_swap (0); OK (2);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200935 case RH_GET_STATUS | RH_ENDPOINT:
Stefan Roese7770ce42005-08-02 10:05:21 +0200936 *(__u16 *) data_buf = m16_swap (0); OK (2);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200937 case RH_GET_STATUS | RH_CLASS:
Stefan Roese7770ce42005-08-02 10:05:21 +0200938 *(__u32 *) data_buf = m32_swap (
939 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
940 OK (4);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200941 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Stefan Roese7770ce42005-08-02 10:05:21 +0200942 *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200943
944 case RH_CLEAR_FEATURE | RH_ENDPOINT:
945 switch (wValue) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200946 case (RH_ENDPOINT_STALL): OK (0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200947 }
948 break;
949
950 case RH_CLEAR_FEATURE | RH_CLASS:
951 switch (wValue) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200952 case RH_C_HUB_LOCAL_POWER:
953 OK(0);
954 case (RH_C_HUB_OVER_CURRENT):
955 WR_RH_STAT(RH_HS_OCIC); OK (0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200956 }
957 break;
958
959 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
960 switch (wValue) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200961 case (RH_PORT_ENABLE):
962 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
963 case (RH_PORT_SUSPEND):
964 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
965 case (RH_PORT_POWER):
966 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
967 case (RH_C_PORT_CONNECTION):
968 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
969 case (RH_C_PORT_ENABLE):
970 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
971 case (RH_C_PORT_SUSPEND):
972 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
973 case (RH_C_PORT_OVER_CURRENT):
974 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
975 case (RH_C_PORT_RESET):
976 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200977 }
978 break;
979
980 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
981 switch (wValue) {
Stefan Roese7770ce42005-08-02 10:05:21 +0200982 case (RH_PORT_SUSPEND):
983 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
984 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
985 if (RD_RH_PORTSTAT & RH_PS_CCS)
986 WR_RH_PORTSTAT (RH_PS_PRS);
987 OK (0);
988 case (RH_PORT_POWER):
989 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
990 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
991 if (RD_RH_PORTSTAT & RH_PS_CCS)
992 WR_RH_PORTSTAT (RH_PS_PES );
993 OK (0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200994 }
995 break;
996
Stefan Roese7770ce42005-08-02 10:05:21 +0200997 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200998
999 case RH_GET_DESCRIPTOR:
1000 switch ((wValue & 0xff00) >> 8) {
Stefan Roese7770ce42005-08-02 10:05:21 +02001001 case (0x01): /* device descriptor */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001002 len = min_t(unsigned int,
Stefan Roese7770ce42005-08-02 10:05:21 +02001003 leni,
1004 min_t(unsigned int,
1005 sizeof (root_hub_dev_des),
1006 wLength));
1007 data_buf = root_hub_dev_des; OK(len);
1008 case (0x02): /* configuration descriptor */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001009 len = min_t(unsigned int,
Stefan Roese7770ce42005-08-02 10:05:21 +02001010 leni,
1011 min_t(unsigned int,
1012 sizeof (root_hub_config_des),
1013 wLength));
1014 data_buf = root_hub_config_des; OK(len);
1015 case (0x03): /* string descriptors */
1016 if(wValue==0x0300) {
1017 len = min_t(unsigned int,
1018 leni,
1019 min_t(unsigned int,
1020 sizeof (root_hub_str_index0),
1021 wLength));
1022 data_buf = root_hub_str_index0;
1023 OK(len);
1024 }
1025 if(wValue==0x0301) {
1026 len = min_t(unsigned int,
1027 leni,
1028 min_t(unsigned int,
1029 sizeof (root_hub_str_index1),
1030 wLength));
1031 data_buf = root_hub_str_index1;
1032 OK(len);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001033 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001034 default:
1035 stat = USB_ST_STALLED;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001036 }
1037 break;
1038
1039 case RH_GET_DESCRIPTOR | RH_CLASS:
Stefan Roese7770ce42005-08-02 10:05:21 +02001040 {
1041 __u32 temp = roothub_a (&gohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001042
Stefan Roese7770ce42005-08-02 10:05:21 +02001043 data_buf [0] = 9; /* min length; */
1044 data_buf [1] = 0x29;
1045 data_buf [2] = temp & RH_A_NDP;
1046 data_buf [3] = 0;
1047 if (temp & RH_A_PSM) /* per-port power switching? */
1048 data_buf [3] |= 0x1;
1049 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1050 data_buf [3] |= 0x10;
1051 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1052 data_buf [3] |= 0x8;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001053
Stefan Roese7770ce42005-08-02 10:05:21 +02001054 /* corresponds to data_buf[4-7] */
1055 datab [1] = 0;
1056 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1057 temp = roothub_b (&gohci);
1058 data_buf [7] = temp & RH_B_DR;
1059 if (data_buf [2] < 7) {
1060 data_buf [8] = 0xff;
1061 } else {
1062 data_buf [0] += 2;
1063 data_buf [8] = (temp & RH_B_DR) >> 8;
1064 data_buf [10] = data_buf [9] = 0xff;
1065 }
Stefan Roesec157d8e2005-08-01 16:41:48 +02001066
Stefan Roese7770ce42005-08-02 10:05:21 +02001067 len = min_t(unsigned int, leni,
1068 min_t(unsigned int, data_buf [0], wLength));
1069 OK (len);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001070 }
1071
Stefan Roese7770ce42005-08-02 10:05:21 +02001072 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001073
Stefan Roese7770ce42005-08-02 10:05:21 +02001074 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001075
1076 default:
Stefan Roese7770ce42005-08-02 10:05:21 +02001077 dbg ("unsupported root hub command");
Stefan Roesec157d8e2005-08-01 16:41:48 +02001078 stat = USB_ST_STALLED;
1079 }
1080
1081#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +02001082 ohci_dump_roothub (&gohci, 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001083#endif
1084
1085 len = min_t(int, len, leni);
1086 if (data != data_buf)
Stefan Roese7770ce42005-08-02 10:05:21 +02001087 memcpy (data, data_buf, len);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001088 dev->act_len = len;
1089 dev->status = stat;
1090
1091#ifdef DEBUG
1092 if (transfer_len)
1093 urb_priv.actual_length = transfer_len;
Stefan Roese7770ce42005-08-02 10:05:21 +02001094 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001095#endif
1096
1097 return stat;
1098}
1099
1100/*-------------------------------------------------------------------------*/
1101
1102/* common code for handling submit messages - used for all but root hub */
1103/* accesses. */
1104int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Stefan Roese7770ce42005-08-02 10:05:21 +02001105 int transfer_len, struct devrequest *setup, int interval)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001106{
1107 int stat = 0;
1108 int maxsize = usb_maxpacket(dev, pipe);
1109 int timeout;
1110
1111 /* device pulled? Shortcut the action. */
1112 if (devgone == dev) {
1113 dev->status = USB_ST_CRC_ERR;
1114 return 0;
1115 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001116
Stefan Roesec157d8e2005-08-01 16:41:48 +02001117#ifdef DEBUG
1118 urb_priv.actual_length = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +02001119 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001120#endif
1121 if (!maxsize) {
1122 err("submit_common_message: pipesize for pipe %lx is zero",
Stefan Roese7770ce42005-08-02 10:05:21 +02001123 pipe);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001124 return -1;
1125 }
1126
Stefan Roese7770ce42005-08-02 10:05:21 +02001127 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001128 err("sohci_submit_job failed");
1129 return -1;
1130 }
1131
1132 /* allow more time for a BULK device to react - some are slow */
Stefan Roese7770ce42005-08-02 10:05:21 +02001133#define BULK_TO 5000 /* timeout in milliseconds */
Remy Bohmer9dbc3662008-10-10 10:23:22 +02001134 if (usb_pipebulk(pipe))
Stefan Roesec157d8e2005-08-01 16:41:48 +02001135 timeout = BULK_TO;
1136 else
1137 timeout = 100;
1138
1139 /* wait for it to complete */
1140 for (;;) {
1141 /* check whether the controller is done */
1142 stat = hc_interrupt();
1143 if (stat < 0) {
1144 stat = USB_ST_CRC_ERR;
1145 break;
1146 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001147
1148 /* NOTE: since we are not interrupt driven in U-Boot and always
1149 * handle only one URB at a time, we cannot assume the
1150 * transaction finished on the first successful return from
1151 * hc_interrupt().. unless the flag for current URB is set,
1152 * meaning that all TD's to/from device got actually
1153 * transferred and processed. If the current URB is not
1154 * finished we need to re-iterate this loop so as
1155 * hc_interrupt() gets called again as there needs to be some
1156 * more TD's to process still */
1157 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001158 /* 0xff is returned for an SF-interrupt */
1159 break;
1160 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001161
Stefan Roesec157d8e2005-08-01 16:41:48 +02001162 if (--timeout) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001163 mdelay(1);
Stefan Roese7770ce42005-08-02 10:05:21 +02001164 if (!urb_finished)
1165 dbg("\%");
1166
Stefan Roesec157d8e2005-08-01 16:41:48 +02001167 } else {
1168 err("CTL:TIMEOUT ");
Stefan Roese7770ce42005-08-02 10:05:21 +02001169 dbg("submit_common_msg: TO status %x\n", stat);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001170 stat = USB_ST_CRC_ERR;
Stefan Roese7770ce42005-08-02 10:05:21 +02001171 urb_finished = 1;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001172 break;
1173 }
1174 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001175#if 0
Stefan Roesec157d8e2005-08-01 16:41:48 +02001176 /* we got an Root Hub Status Change interrupt */
1177 if (got_rhsc) {
1178#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +02001179 ohci_dump_roothub (&gohci, 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001180#endif
1181 got_rhsc = 0;
1182 /* abuse timeout */
1183 timeout = rh_check_port_status(&gohci);
1184 if (timeout >= 0) {
Stefan Roese7770ce42005-08-02 10:05:21 +02001185#if 0 /* this does nothing useful, but leave it here in case that changes */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001186 /* the called routine adds 1 to the passed value */
1187 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1188#endif
1189 /*
1190 * XXX
1191 * This is potentially dangerous because it assumes
1192 * that only one device is ever plugged in!
1193 */
1194 devgone = dev;
1195 }
1196 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001197#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +02001198
1199 dev->status = stat;
1200 dev->act_len = transfer_len;
1201
1202#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +02001203 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001204#endif
1205
1206 /* free TDs in urb_priv */
Stefan Roese7770ce42005-08-02 10:05:21 +02001207 urb_free_priv (&urb_priv);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001208 return 0;
1209}
1210
1211/* submit routines called from usb.c */
1212int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Stefan Roese7770ce42005-08-02 10:05:21 +02001213 int transfer_len)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001214{
1215 info("submit_bulk_msg");
1216 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1217}
1218
1219int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Stefan Roese7770ce42005-08-02 10:05:21 +02001220 int transfer_len, struct devrequest *setup)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001221{
1222 int maxsize = usb_maxpacket(dev, pipe);
1223
1224 info("submit_control_msg");
1225#ifdef DEBUG
1226 urb_priv.actual_length = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +02001227 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001228#endif
1229 if (!maxsize) {
1230 err("submit_control_message: pipesize for pipe %lx is zero",
Stefan Roese7770ce42005-08-02 10:05:21 +02001231 pipe);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001232 return -1;
1233 }
1234 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1235 gohci.rh.dev = dev;
1236 /* root hub - redirect */
1237 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
Stefan Roese7770ce42005-08-02 10:05:21 +02001238 setup);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001239 }
1240
1241 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1242}
1243
1244int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Stefan Roese7770ce42005-08-02 10:05:21 +02001245 int transfer_len, int interval)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001246{
1247 info("submit_int_msg");
1248 return -1;
1249}
1250
1251/*-------------------------------------------------------------------------*
1252 * HC functions
1253 *-------------------------------------------------------------------------*/
1254
1255/* reset the HC and BUS */
1256
Stefan Roese7770ce42005-08-02 10:05:21 +02001257static int hc_reset (ohci_t *ohci)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001258{
1259 int timeout = 30;
Stefan Roese7770ce42005-08-02 10:05:21 +02001260 int smm_timeout = 50; /* 0,5 sec */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001261
Stefan Roese7770ce42005-08-02 10:05:21 +02001262 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1263 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001264 info("USB HC TakeOver from SMM");
Stefan Roese7770ce42005-08-02 10:05:21 +02001265 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001266 mdelay (10);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001267 if (--smm_timeout == 0) {
1268 err("USB HC TakeOver failed!");
1269 return -1;
1270 }
1271 }
1272 }
1273
1274 /* Disable HC interrupts */
Stefan Roese7770ce42005-08-02 10:05:21 +02001275 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001276
1277 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
Stefan Roese7770ce42005-08-02 10:05:21 +02001278 ohci->slot_name,
1279 readl (&ohci->regs->control));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001280
1281 /* Reset USB (needed by some controllers) */
1282 ohci->hc_control = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +02001283 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001284
1285 /* HC Reset requires max 10 us delay */
Stefan Roese7770ce42005-08-02 10:05:21 +02001286 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1287 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001288 if (--timeout == 0) {
1289 err("USB HC reset timed out!");
1290 return -1;
1291 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001292 udelay (1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001293 }
1294 return 0;
1295}
1296
1297/*-------------------------------------------------------------------------*/
1298
1299/* Start an OHCI controller, set the BUS operational
1300 * enable interrupts
1301 * connect the virtual root hub */
1302
Stefan Roese7770ce42005-08-02 10:05:21 +02001303static int hc_start (ohci_t * ohci)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001304{
1305 __u32 mask;
1306 unsigned int fminterval;
1307
1308 ohci->disabled = 1;
1309
1310 /* Tell the controller where the control and bulk lists are
1311 * The lists are empty now. */
1312
Stefan Roese7770ce42005-08-02 10:05:21 +02001313 writel (0, &ohci->regs->ed_controlhead);
1314 writel (0, &ohci->regs->ed_bulkhead);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001315
Stefan Roese7770ce42005-08-02 10:05:21 +02001316 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001317
1318 fminterval = 0x2edf;
Stefan Roese7770ce42005-08-02 10:05:21 +02001319 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001320 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Stefan Roese7770ce42005-08-02 10:05:21 +02001321 writel (fminterval, &ohci->regs->fminterval);
1322 writel (0x628, &ohci->regs->lsthresh);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001323
1324 /* start controller operations */
1325 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1326 ohci->disabled = 0;
Stefan Roese7770ce42005-08-02 10:05:21 +02001327 writel (ohci->hc_control, &ohci->regs->control);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001328
1329 /* disable all interrupts */
1330 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
Stefan Roese7770ce42005-08-02 10:05:21 +02001331 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1332 OHCI_INTR_OC | OHCI_INTR_MIE);
1333 writel (mask, &ohci->regs->intrdisable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001334 /* clear all interrupts */
1335 mask &= ~OHCI_INTR_MIE;
Stefan Roese7770ce42005-08-02 10:05:21 +02001336 writel (mask, &ohci->regs->intrstatus);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001337 /* Choose the interrupts we care about now - but w/o MIE */
1338 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Stefan Roese7770ce42005-08-02 10:05:21 +02001339 writel (mask, &ohci->regs->intrenable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001340
1341#ifdef OHCI_USE_NPS
1342 /* required for AMD-756 and some Mac platforms */
Stefan Roese7770ce42005-08-02 10:05:21 +02001343 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1344 &ohci->regs->roothub.a);
1345 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1346#endif /* OHCI_USE_NPS */
Stefan Roesec157d8e2005-08-01 16:41:48 +02001347
Stefan Roesec157d8e2005-08-01 16:41:48 +02001348 /* POTPGT delay is bits 24-31, in 2 ms units. */
Stefan Roese7770ce42005-08-02 10:05:21 +02001349 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001350
1351 /* connect the virtual root hub */
1352 ohci->rh.devnum = 0;
1353
1354 return 0;
1355}
1356
1357/*-------------------------------------------------------------------------*/
1358
1359/* an interrupt happens */
1360
Stefan Roese7770ce42005-08-02 10:05:21 +02001361static int
1362hc_interrupt (void)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001363{
1364 ohci_t *ohci = &gohci;
1365 struct ohci_regs *regs = ohci->regs;
1366 int ints;
1367 int stat = -1;
1368
Stefan Roese7770ce42005-08-02 10:05:21 +02001369 if ((ohci->hcca->done_head != 0) &&
1370 !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
1371
1372 ints = OHCI_INTR_WDH;
1373
1374 } else if ((ints = readl (&regs->intrstatus)) == ~(u32)0) {
1375 ohci->disabled++;
1376 err ("%s device removed!", ohci->slot_name);
1377 return -1;
1378
1379 } else if ((ints &= readl (&regs->intrenable)) == 0) {
1380 dbg("hc_interrupt: returning..\n");
1381 return 0xff;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001382 }
1383
1384 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1385
1386 if (ints & OHCI_INTR_RHSC) {
1387 got_rhsc = 1;
Stefan Roese7770ce42005-08-02 10:05:21 +02001388 stat = 0xff;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001389 }
1390
1391 if (ints & OHCI_INTR_UE) {
1392 ohci->disabled++;
Stefan Roese7770ce42005-08-02 10:05:21 +02001393 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1394 ohci->slot_name);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001395 /* e.g. due to PCI Master/Target Abort */
1396
1397#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +02001398 ohci_dump (ohci, 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001399#endif
1400 /* FIXME: be optimistic, hope that bug won't repeat often. */
1401 /* Make some non-interrupt context restart the controller. */
1402 /* Count and limit the retries though; either hardware or */
1403 /* software errors can go forever... */
Stefan Roese7770ce42005-08-02 10:05:21 +02001404 hc_reset (ohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001405 return -1;
1406 }
1407
1408 if (ints & OHCI_INTR_WDH) {
Stefan Roese7770ce42005-08-02 10:05:21 +02001409 writel (OHCI_INTR_WDH, &regs->intrdisable);
1410 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1411 writel (OHCI_INTR_WDH, &regs->intrenable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001412 }
1413
1414 if (ints & OHCI_INTR_SO) {
1415 dbg("USB Schedule overrun\n");
Stefan Roese7770ce42005-08-02 10:05:21 +02001416 writel (OHCI_INTR_SO, &regs->intrenable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001417 stat = -1;
1418 }
1419
1420 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1421 if (ints & OHCI_INTR_SF) {
Stefan Roese7770ce42005-08-02 10:05:21 +02001422 unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001423 mdelay(1);
Stefan Roese7770ce42005-08-02 10:05:21 +02001424 writel (OHCI_INTR_SF, &regs->intrdisable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001425 if (ohci->ed_rm_list[frame] != NULL)
Stefan Roese7770ce42005-08-02 10:05:21 +02001426 writel (OHCI_INTR_SF, &regs->intrenable);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001427 stat = 0xff;
1428 }
1429
Stefan Roese7770ce42005-08-02 10:05:21 +02001430 writel (ints, &regs->intrstatus);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001431 return stat;
1432}
1433
1434/*-------------------------------------------------------------------------*/
1435
1436/*-------------------------------------------------------------------------*/
1437
1438/* De-allocate all resources.. */
1439
Stefan Roese7770ce42005-08-02 10:05:21 +02001440static void hc_release_ohci (ohci_t *ohci)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001441{
Stefan Roese7770ce42005-08-02 10:05:21 +02001442 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001443
1444 if (!ohci->disabled)
Stefan Roese7770ce42005-08-02 10:05:21 +02001445 hc_reset (ohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001446}
1447
1448/*-------------------------------------------------------------------------*/
1449
1450/*
1451 * low level initalisation routine, called from usb.c
1452 */
1453static char ohci_inited = 0;
1454
Troy Kisky06d513e2013-10-10 15:27:56 -07001455int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001456{
Stefan Roese7770ce42005-08-02 10:05:21 +02001457 memset (&gohci, 0, sizeof (ohci_t));
1458 memset (&urb_priv, 0, sizeof (urb_priv_t));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001459
1460 /* align the storage */
Stefan Roese7770ce42005-08-02 10:05:21 +02001461 if ((__u32)&ghcca[0] & 0xff) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001462 err("HCCA not aligned!!");
1463 return -1;
1464 }
1465 phcca = &ghcca[0];
1466 info("aligned ghcca %p", phcca);
1467 memset(&ohci_dev, 0, sizeof(struct ohci_device));
Stefan Roese7770ce42005-08-02 10:05:21 +02001468 if ((__u32)&ohci_dev.ed[0] & 0x7) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001469 err("EDs not aligned!!");
1470 return -1;
1471 }
1472 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
Stefan Roese7770ce42005-08-02 10:05:21 +02001473 if ((__u32)gtd & 0x7) {
Stefan Roesec157d8e2005-08-01 16:41:48 +02001474 err("TDs not aligned!!");
1475 return -1;
1476 }
1477 ptd = gtd;
1478 gohci.hcca = phcca;
Stefan Roese7770ce42005-08-02 10:05:21 +02001479 memset (phcca, 0, sizeof (struct ohci_hcca));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001480
1481 gohci.disabled = 1;
1482 gohci.sleeping = 0;
1483 gohci.irq = -1;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001484#if defined(CONFIG_440EP)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001485 gohci.regs = (struct ohci_regs *)(CONFIG_SYS_PERIPHERAL_BASE | 0x1000);
1486#elif defined(CONFIG_440EPX) || defined(CONFIG_SYS_USB_HOST)
1487 gohci.regs = (struct ohci_regs *)(CONFIG_SYS_USB_HOST);
Stefan Roese887e2ec2006-09-07 11:51:23 +02001488#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +02001489
1490 gohci.flags = 0;
1491 gohci.slot_name = "ppc440";
1492
Stefan Roese7770ce42005-08-02 10:05:21 +02001493 if (hc_reset (&gohci) < 0) {
1494 hc_release_ohci (&gohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001495 return -1;
1496 }
1497
Stefan Roese7770ce42005-08-02 10:05:21 +02001498 if (hc_start (&gohci) < 0) {
1499 err ("can't start usb-%s", gohci.slot_name);
1500 hc_release_ohci (&gohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001501 return -1;
1502 }
Stefan Roese7770ce42005-08-02 10:05:21 +02001503
Stefan Roesec157d8e2005-08-01 16:41:48 +02001504#ifdef DEBUG
Stefan Roese7770ce42005-08-02 10:05:21 +02001505 ohci_dump (&gohci, 1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001506#endif
1507 ohci_inited = 1;
Stefan Roese7770ce42005-08-02 10:05:21 +02001508 urb_finished = 1;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001509
Stefan Roesec157d8e2005-08-01 16:41:48 +02001510 return 0;
1511}
1512
Lucas Stachc7e3b2b2012-09-26 00:14:34 +02001513int usb_lowlevel_stop(int index)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001514{
1515 /* this gets called really early - before the controller has */
1516 /* even been initialized! */
1517 if (!ohci_inited)
1518 return 0;
1519 /* TODO release any interrupts, etc. */
1520 /* call hc_release_ohci() here ? */
Stefan Roese7770ce42005-08-02 10:05:21 +02001521 hc_reset (&gohci);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001522 return 0;
1523}
1524
Stefan Roese7770ce42005-08-02 10:05:21 +02001525#endif /* CONFIG_USB_OHCI */