blob: c20d7064277bb82f60a649be5f2caee6cbe09eb6 [file] [log] [blame]
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001/*
Zhang Wei4dae14c2007-06-06 10:08:14 +02002 * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
3 *
4 * Interrupt support is added. Now, it has been tested
5 * on ULI1575 chip and works well with USB keyboard.
6 *
7 * (C) Copyright 2007
8 * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02009 *
10 * (C) Copyright 2003
Detlev Zundel792a09e2009-05-13 10:54:10 +020011 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020012 *
13 * Note: Much of this code has been derived from Linux 2.4
14 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
15 * (C) Copyright 2000-2002 David Brownell
16 *
17 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
18 * ebenard@eukrea.com - based on s3c24x0's driver
19 *
20 * See file CREDITS for list of people who contributed to this
21 * project.
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jerry Van Barenae0b05d2009-02-05 22:18:02 -050030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020031 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 * MA 02111-1307 USA
37 *
38 */
39/*
40 * IMPORTANT NOTES
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020041 * 1 - Read doc/README.generic_usb_ohci
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020042 * 2 - this driver is intended for use with USB Mass Storage Devices
Zhang Wei4dae14c2007-06-06 10:08:14 +020043 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020044 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020045 * to activate workaround for bug #41 or this driver will NOT work!
46 */
47
48#include <common.h>
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020049#include <asm/byteorder.h>
50
51#if defined(CONFIG_PCI_OHCI)
Zhang Wei4dae14c2007-06-06 10:08:14 +020052# include <pci.h>
Sergei Poselenov477434c2008-05-22 01:15:53 +020053#if !defined(CONFIG_PCI_OHCI_DEVNO)
54#define CONFIG_PCI_OHCI_DEVNO 0
55#endif
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +020056#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020057
58#include <malloc.h>
59#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020060
61#include "ohci.h"
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020062
Wolfgang Denke8da58f2007-11-19 12:59:14 +010063#ifdef CONFIG_AT91RM9200
64#include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
65#endif
66
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +020067#if defined(CONFIG_ARM920T) || \
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090068 defined(CONFIG_S3C24X0) || \
Guennadi Liakhovetski82628132008-08-31 00:39:46 +020069 defined(CONFIG_S3C6400) || \
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +010070 defined(CONFIG_440EP) || \
Zhang Wei4dae14c2007-06-06 10:08:14 +020071 defined(CONFIG_PCI_OHCI) || \
Stefan Roese2596f5b2008-03-05 12:29:32 +010072 defined(CONFIG_MPC5200) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher24e37642006-05-23 10:33:11 +020074# define OHCI_USE_NPS /* force NoPowerSwitching mode */
75#endif
76
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020077#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +010078#undef DEBUG
79#undef SHOW_INFO
80#undef OHCI_FILL_TRACE
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020081
82/* For initializing controller (mask in an HCFS mode too) */
83#define OHCI_CONTROL_INIT \
84 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
85
Remy Bohmer6f5794a2008-09-16 14:55:43 +020086#define min_t(type, x, y) \
87 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020088
Zhang Wei4dae14c2007-06-06 10:08:14 +020089#ifdef CONFIG_PCI_OHCI
90static struct pci_device_id ohci_pci_ids[] = {
91 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saada97213f32007-09-17 17:04:47 +020092 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew3afac792008-01-11 20:42:58 -060093 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei4dae14c2007-06-06 10:08:14 +020094 /* Please add supported PCI OHCI controller ids here */
95 {0, 0}
96};
97#endif
98
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +020099#ifdef CONFIG_PCI_EHCI_DEVNO
100static struct pci_device_id ehci_pci_ids[] = {
101 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
102 /* Please add supported PCI EHCI controller ids here */
103 {0, 0}
104};
105#endif
106
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200107#ifdef DEBUG
108#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
109#else
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200110#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200111#endif /* DEBUG */
112#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200113#ifdef SHOW_INFO
114#define info(format, arg...) printf("INFO: " format "\n", ## arg)
115#else
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200116#define info(format, arg...) do {} while (0)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200117#endif
118
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +0200120# define m16_swap(x) cpu_to_be16(x)
121# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +0100122#else
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +0200123# define m16_swap(x) cpu_to_le16(x)
124# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200125#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200126
127/* global ohci_t */
128static ohci_t gohci;
129/* this must be aligned to a 256 byte boundary */
130struct ohci_hcca ghcca[1];
131/* a pointer to the aligned storage */
132struct ohci_hcca *phcca;
133/* this allocates EDs for all possible endpoints */
134struct ohci_device ohci_dev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200135/* device which was disconnected */
136struct usb_device *devgone;
137
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200138static inline u32 roothub_a(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500139 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200140static inline u32 roothub_b(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500141 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200142static inline u32 roothub_status(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500143 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200144static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Brucea5496a12010-06-30 13:05:44 -0500145 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200146
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200147/* forward declaration */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200148static int hc_interrupt(void);
149static void td_submit_job(struct usb_device *dev, unsigned long pipe,
150 void *buffer, int transfer_len,
151 struct devrequest *setup, urb_priv_t *urb,
152 int interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200153
154/*-------------------------------------------------------------------------*
155 * URB support functions
156 *-------------------------------------------------------------------------*/
157
158/* free HCD-private data associated with this URB */
159
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200160static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200161{
162 int i;
163 int last;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200164 struct td *td;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200165
166 last = urb->length - 1;
167 if (last >= 0) {
168 for (i = 0; i <= last; i++) {
169 td = urb->td[i];
170 if (td) {
171 td->usb_dev = NULL;
172 urb->td[i] = NULL;
173 }
174 }
175 }
Zhang Wei4dae14c2007-06-06 10:08:14 +0200176 free(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200177}
178
179/*-------------------------------------------------------------------------*/
180
181#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200182static int sohci_get_current_frame_number(struct usb_device *dev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200183
184/* debug| print the main components of an URB
185 * small: 0) header + data packets 1) just header */
186
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200187static void pkt_print(urb_priv_t *purb, struct usb_device *dev,
188 unsigned long pipe, void *buffer, int transfer_len,
189 struct devrequest *setup, char *str, int small)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200190{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200191 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200192 str,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200193 sohci_get_current_frame_number(dev),
194 usb_pipedevice(pipe),
195 usb_pipeendpoint(pipe),
196 usb_pipeout(pipe)? 'O': 'I',
197 usb_pipetype(pipe) < 2 ? \
198 (usb_pipeint(pipe)? "INTR": "ISOC"): \
199 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei4dae14c2007-06-06 10:08:14 +0200200 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200201 transfer_len, dev->status);
202#ifdef OHCI_VERBOSE_DEBUG
203 if (!small) {
204 int i, len;
205
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200206 if (usb_pipecontrol(pipe)) {
207 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200208 for (i = 0; i < 8 ; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200209 printf(" %02x", ((__u8 *) setup) [i]);
210 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200211 }
212 if (transfer_len > 0 && buffer) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200213 printf(__FILE__ ": data(%d/%d):",
Zhang Wei4dae14c2007-06-06 10:08:14 +0200214 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200215 transfer_len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200216 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei4dae14c2007-06-06 10:08:14 +0200217 (purb ? purb->actual_length : 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200218 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200219 printf(" %02x", ((__u8 *) buffer) [i]);
220 printf("%s\n", i < len? "...": "");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200221 }
222 }
223#endif
224}
225
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200226/* just for debugging; prints non-empty branches of the int ed tree
227 * inclusive iso eds */
228void ep_print_int_eds(ohci_t *ohci, char *str)
229{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200230 int i, j;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200231 __u32 *ed_p;
232 for (i = 0; i < 32; i++) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200233 j = 5;
234 ed_p = &(ohci->hcca->int_table [i]);
235 if (*ed_p == 0)
236 continue;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200237 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200238 while (*ed_p != 0 && j--) {
239 ed_t *ed = (ed_t *)m32_swap(ed_p);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200240 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200241 ed_p = &ed->hwNextED;
242 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200243 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200244 }
245}
246
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200247static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200248{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200249 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200250 label,
251 mask,
252 (mask & OHCI_INTR_MIE) ? " MIE" : "",
253 (mask & OHCI_INTR_OC) ? " OC" : "",
254 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
255 (mask & OHCI_INTR_FNO) ? " FNO" : "",
256 (mask & OHCI_INTR_UE) ? " UE" : "",
257 (mask & OHCI_INTR_RD) ? " RD" : "",
258 (mask & OHCI_INTR_SF) ? " SF" : "",
259 (mask & OHCI_INTR_WDH) ? " WDH" : "",
260 (mask & OHCI_INTR_SO) ? " SO" : ""
261 );
262}
263
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200264static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200265{
266 ed_t *edp = (ed_t *)value;
267
268 if (value) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200269 dbg("%s %08x", label, value);
270 dbg("%08x", edp->hwINFO);
271 dbg("%08x", edp->hwTailP);
272 dbg("%08x", edp->hwHeadP);
273 dbg("%08x", edp->hwNextED);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200274 }
275}
276
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200277static char *hcfs2string(int state)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200278{
279 switch (state) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200280 case OHCI_USB_RESET: return "reset";
281 case OHCI_USB_RESUME: return "resume";
282 case OHCI_USB_OPER: return "operational";
283 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200284 }
285 return "?";
286}
287
288/* dump control and status registers */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200289static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200290{
291 struct ohci_regs *regs = controller->regs;
292 __u32 temp;
293
Becky Brucea5496a12010-06-30 13:05:44 -0500294 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200295 if (temp != 0x10)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200296 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200297
Becky Brucea5496a12010-06-30 13:05:44 -0500298 temp = ohci_readl(&regs->control);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200299 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200300 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
301 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
302 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200303 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200304 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
305 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
306 (temp & OHCI_CTRL_IE) ? " IE" : "",
307 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
308 temp & OHCI_CTRL_CBSR
309 );
310
Becky Brucea5496a12010-06-30 13:05:44 -0500311 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200312 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200313 (temp & OHCI_SOC) >> 16,
314 (temp & OHCI_OCR) ? " OCR" : "",
315 (temp & OHCI_BLF) ? " BLF" : "",
316 (temp & OHCI_CLF) ? " CLF" : "",
317 (temp & OHCI_HCR) ? " HCR" : ""
318 );
319
Becky Brucea5496a12010-06-30 13:05:44 -0500320 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
321 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200322
Becky Brucea5496a12010-06-30 13:05:44 -0500323 maybe_print_eds("ed_periodcurrent",
324 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200325
Becky Brucea5496a12010-06-30 13:05:44 -0500326 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
327 maybe_print_eds("ed_controlcurrent",
328 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200329
Becky Brucea5496a12010-06-30 13:05:44 -0500330 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
331 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200332
Becky Brucea5496a12010-06-30 13:05:44 -0500333 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200334}
335
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200336static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200337{
338 __u32 temp, ndp, i;
339
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200340 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200341 ndp = (temp & RH_A_NDP);
342#ifdef CONFIG_AT91C_PQFP_UHPBUG
343 ndp = (ndp == 2) ? 1:0;
344#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200345 if (verbose) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200346 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200347 ((temp & RH_A_POTPGT) >> 24) & 0xff,
348 (temp & RH_A_NOCP) ? " NOCP" : "",
349 (temp & RH_A_OCPM) ? " OCPM" : "",
350 (temp & RH_A_DT) ? " DT" : "",
351 (temp & RH_A_NPS) ? " NPS" : "",
352 (temp & RH_A_PSM) ? " PSM" : "",
353 ndp
354 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200355 temp = roothub_b(controller);
356 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200357 temp,
358 (temp & RH_B_PPCM) >> 16,
359 (temp & RH_B_DR)
360 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200361 temp = roothub_status(controller);
362 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200363 temp,
364 (temp & RH_HS_CRWE) ? " CRWE" : "",
365 (temp & RH_HS_OCIC) ? " OCIC" : "",
366 (temp & RH_HS_LPSC) ? " LPSC" : "",
367 (temp & RH_HS_DRWE) ? " DRWE" : "",
368 (temp & RH_HS_OCI) ? " OCI" : "",
369 (temp & RH_HS_LPS) ? " LPS" : ""
370 );
371 }
372
373 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200374 temp = roothub_portstatus(controller, i);
375 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200376 i,
377 temp,
378 (temp & RH_PS_PRSC) ? " PRSC" : "",
379 (temp & RH_PS_OCIC) ? " OCIC" : "",
380 (temp & RH_PS_PSSC) ? " PSSC" : "",
381 (temp & RH_PS_PESC) ? " PESC" : "",
382 (temp & RH_PS_CSC) ? " CSC" : "",
383
384 (temp & RH_PS_LSDA) ? " LSDA" : "",
385 (temp & RH_PS_PPS) ? " PPS" : "",
386 (temp & RH_PS_PRS) ? " PRS" : "",
387 (temp & RH_PS_POCI) ? " POCI" : "",
388 (temp & RH_PS_PSS) ? " PSS" : "",
389
390 (temp & RH_PS_PES) ? " PES" : "",
391 (temp & RH_PS_CCS) ? " CCS" : ""
392 );
393 }
394}
395
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200396static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200397{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200398 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200399
400 /* dumps some of the state we know about */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200401 ohci_dump_status(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200402 if (verbose)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200403 ep_print_int_eds(controller, "hcca");
404 dbg("hcca frame #%04x", controller->hcca->frame_no);
405 ohci_dump_roothub(controller, 1);
Stefan Roese2596f5b2008-03-05 12:29:32 +0100406}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200407#endif /* DEBUG */
408
409/*-------------------------------------------------------------------------*
410 * Interface functions (URB)
411 *-------------------------------------------------------------------------*/
412
413/* get a transfer request */
414
Zhang Wei4dae14c2007-06-06 10:08:14 +0200415int sohci_submit_job(urb_priv_t *urb, struct devrequest *setup)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200416{
417 ohci_t *ohci;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200418 ed_t *ed;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200419 urb_priv_t *purb_priv = urb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200420 int i, size = 0;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200421 struct usb_device *dev = urb->dev;
422 unsigned long pipe = urb->pipe;
423 void *buffer = urb->transfer_buffer;
424 int transfer_len = urb->transfer_buffer_length;
425 int interval = urb->interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200426
427 ohci = &gohci;
428
429 /* when controller's hung, permit only roothub cleanup attempts
430 * such as powering down ports */
431 if (ohci->disabled) {
432 err("sohci_submit_job: EPIPE");
433 return -1;
434 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +0200435
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200436 /* we're about to begin a new transaction here so mark the
437 * URB unfinished */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200438 urb->finished = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200439
440 /* every endpoint has a ed, locate and fill it */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200441 ed = ep_add_ed(dev, pipe, interval, 1);
442 if (!ed) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200443 err("sohci_submit_job: ENOMEM");
444 return -1;
445 }
446
447 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200448 switch (usb_pipetype(pipe)) {
449 case PIPE_BULK: /* one TD for every 4096 Byte */
450 size = (transfer_len - 1) / 4096 + 1;
451 break;
452 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
453 size = (transfer_len == 0)? 2:
454 (transfer_len - 1) / 4096 + 3;
455 break;
456 case PIPE_INTERRUPT: /* 1 TD */
457 size = 1;
458 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200459 }
460
Zhang Wei4dae14c2007-06-06 10:08:14 +0200461 ed->purb = urb;
462
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200463 if (size >= (N_URB_TD - 1)) {
464 err("need %d TDs, only have %d", size, N_URB_TD);
465 return -1;
466 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200467 purb_priv->pipe = pipe;
468
469 /* fill the private part of the URB */
470 purb_priv->length = size;
471 purb_priv->ed = ed;
472 purb_priv->actual_length = 0;
473
474 /* allocate the TDs */
475 /* note that td[0] was allocated in ep_add_ed */
476 for (i = 0; i < size; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200477 purb_priv->td[i] = td_alloc(dev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200478 if (!purb_priv->td[i]) {
479 purb_priv->length = i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200480 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200481 err("sohci_submit_job: ENOMEM");
482 return -1;
483 }
484 }
485
486 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200487 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200488 err("sohci_submit_job: EINVAL");
489 return -1;
490 }
491
492 /* link the ed into a chain if is not already */
493 if (ed->state != ED_OPER)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200494 ep_link(ohci, ed);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200495
496 /* fill the TDs and link it to the ed */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200497 td_submit_job(dev, pipe, buffer, transfer_len,
498 setup, purb_priv, interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200499
500 return 0;
501}
502
Zhang Wei4dae14c2007-06-06 10:08:14 +0200503static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
504{
505 struct ohci_regs *regs = hc->regs;
506
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200507 switch (usb_pipetype(urb->pipe)) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200508 case PIPE_INTERRUPT:
509 /* implicitly requeued */
510 if (urb->dev->irq_handle &&
511 (urb->dev->irq_act_len = urb->actual_length)) {
Becky Brucea5496a12010-06-30 13:05:44 -0500512 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
513 ohci_readl(&regs->intrenable); /* PCI posting flush */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200514 urb->dev->irq_handle(urb->dev);
Becky Brucea5496a12010-06-30 13:05:44 -0500515 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
516 ohci_readl(&regs->intrdisable); /* PCI posting flush */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200517 }
518 urb->actual_length = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200519 td_submit_job(
Zhang Wei4dae14c2007-06-06 10:08:14 +0200520 urb->dev,
521 urb->pipe,
522 urb->transfer_buffer,
523 urb->transfer_buffer_length,
524 NULL,
525 urb,
526 urb->interval);
527 break;
528 case PIPE_CONTROL:
529 case PIPE_BULK:
530 break;
531 default:
532 return 0;
533 }
534 return 1;
535}
536
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200537/*-------------------------------------------------------------------------*/
538
539#ifdef DEBUG
540/* tell us the current USB frame number */
541
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200542static int sohci_get_current_frame_number(struct usb_device *usb_dev)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200543{
544 ohci_t *ohci = &gohci;
545
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200546 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200547}
548#endif
549
550/*-------------------------------------------------------------------------*
551 * ED handling functions
552 *-------------------------------------------------------------------------*/
553
Zhang Wei4dae14c2007-06-06 10:08:14 +0200554/* search for the right branch to insert an interrupt ed into the int tree
555 * do some load ballancing;
556 * returns the branch and
557 * sets the interval to interval = 2^integer (ld (interval)) */
558
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200559static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200560{
561 int i, branch = 0;
562
563 /* search for the least loaded interrupt endpoint
564 * branch of all 32 branches
565 */
566 for (i = 0; i < 32; i++)
567 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
568 branch = i;
569
570 branch = branch % interval;
571 for (i = branch; i < 32; i += interval)
572 ohci->ohci_int_load [i] += load;
573
574 return branch;
575}
576
577/*-------------------------------------------------------------------------*/
578
579/* 2^int( ld (inter)) */
580
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200581static int ep_2_n_interval(int inter)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200582{
583 int i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200584 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200585 return 1 << i;
586}
587
588/*-------------------------------------------------------------------------*/
589
590/* the int tree is a binary tree
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200591 * in order to process it sequentially the indexes of the branches have to
592 * be mapped the mapping reverses the bits of a word of num_bits length */
593static int ep_rev(int num_bits, int word)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200594{
595 int i, wout = 0;
596
597 for (i = 0; i < num_bits; i++)
598 wout |= (((word >> i) & 1) << (num_bits - i - 1));
599 return wout;
600}
601
602/*-------------------------------------------------------------------------*
603 * ED handling functions
604 *-------------------------------------------------------------------------*/
605
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200606/* link an ed into one of the HC chains */
607
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200608static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200609{
610 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200611 int int_branch;
612 int i;
613 int inter;
614 int interval;
615 int load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200616 __u32 *ed_p;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200617
618 ed->state = ED_OPER;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200619 ed->int_interval = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200620
621 switch (ed->type) {
622 case PIPE_CONTROL:
623 ed->hwNextED = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200624 if (ohci->ed_controltail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500625 ohci_writel(ed, &ohci->regs->ed_controlhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200626 else
627 ohci->ed_controltail->hwNextED =
628 m32_swap((unsigned long)ed);
629
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200630 ed->ed_prev = ohci->ed_controltail;
631 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
632 !ohci->ed_rm_list[1] && !ohci->sleeping) {
633 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500634 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200635 }
636 ohci->ed_controltail = edi;
637 break;
638
639 case PIPE_BULK:
640 ed->hwNextED = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200641 if (ohci->ed_bulktail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500642 ohci_writel(ed, &ohci->regs->ed_bulkhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200643 else
644 ohci->ed_bulktail->hwNextED =
645 m32_swap((unsigned long)ed);
646
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200647 ed->ed_prev = ohci->ed_bulktail;
648 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
649 !ohci->ed_rm_list[1] && !ohci->sleeping) {
650 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500651 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200652 }
653 ohci->ed_bulktail = edi;
654 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200655
656 case PIPE_INTERRUPT:
657 load = ed->int_load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200658 interval = ep_2_n_interval(ed->int_period);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200659 ed->int_interval = interval;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200660 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200661 ed->int_branch = int_branch;
662
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200663 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200664 inter = 1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200665 for (ed_p = &(ohci->hcca->int_table[\
666 ep_rev(5, i) + int_branch]);
667 (*ed_p != 0) &&
668 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200669 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200670 inter = ep_rev(6,
671 ((ed_t *)ed_p)->int_interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200672 ed->hwNextED = *ed_p;
Martin Krause4a8527e2007-08-21 12:40:34 +0200673 *ed_p = m32_swap((unsigned long)ed);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200674 }
675 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200676 }
677 return 0;
678}
679
680/*-------------------------------------------------------------------------*/
681
Zhang Wei4dae14c2007-06-06 10:08:14 +0200682/* scan the periodic table to find and unlink this ED */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200683static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
684 unsigned index, unsigned period)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200685{
686 for (; index < NUM_INTS; index += period) {
687 __u32 *ed_p = &ohci->hcca->int_table [index];
688
689 /* ED might have been unlinked through another path */
690 while (*ed_p != 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200691 if (((struct ed *)
692 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200693 *ed_p = ed->hwNextED;
694 break;
695 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200696 ed_p = &(((struct ed *)
697 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200698 }
699 }
700}
701
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200702/* unlink an ed from one of the HC chains.
703 * just the link to the ed is unlinked.
704 * the link from the ed still points to another operational ed or 0
705 * so the HC can eventually finish the processing of the unlinked ed */
706
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200707static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200708{
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100709 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200710 int i;
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100711
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200712 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200713
714 switch (ed->type) {
715 case PIPE_CONTROL:
716 if (ed->ed_prev == NULL) {
717 if (!ed->hwNextED) {
718 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500719 ohci_writel(ohci->hc_control,
720 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200721 }
Becky Brucea5496a12010-06-30 13:05:44 -0500722 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200723 &ohci->regs->ed_controlhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200724 } else {
725 ed->ed_prev->hwNextED = ed->hwNextED;
726 }
727 if (ohci->ed_controltail == ed) {
728 ohci->ed_controltail = ed->ed_prev;
729 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200730 ((ed_t *)m32_swap(
731 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200732 }
733 break;
734
735 case PIPE_BULK:
736 if (ed->ed_prev == NULL) {
737 if (!ed->hwNextED) {
738 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500739 ohci_writel(ohci->hc_control,
740 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200741 }
Becky Brucea5496a12010-06-30 13:05:44 -0500742 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200743 &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200744 } else {
745 ed->ed_prev->hwNextED = ed->hwNextED;
746 }
747 if (ohci->ed_bulktail == ed) {
748 ohci->ed_bulktail = ed->ed_prev;
749 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200750 ((ed_t *)m32_swap(
751 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200752 }
753 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200754
755 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200756 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200757 for (i = ed->int_branch; i < 32; i += ed->int_interval)
758 ohci->ohci_int_load[i] -= ed->int_load;
759 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200760 }
761 ed->state = ED_UNLINK;
762 return 0;
763}
764
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200765/*-------------------------------------------------------------------------*/
766
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +0200767/* add/reinit an endpoint; this should be done once at the
768 * usb_set_configuration command, but the USB stack is a little bit
769 * stateless so we do it at every transaction if the state of the ed
770 * is ED_NEW then a dummy td is added and the state is changed to
771 * ED_UNLINK in all other cases the state is left unchanged the ed
772 * info fields are setted anyway even though most of them should not
773 * change
774 */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200775static ed_t *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe,
776 int interval, int load)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200777{
778 td_t *td;
779 ed_t *ed_ret;
780 volatile ed_t *ed;
781
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200782 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
783 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200784
785 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
786 err("ep_add_ed: pending delete");
787 /* pending delete request */
788 return NULL;
789 }
790
791 if (ed->state == ED_NEW) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200792 /* dummy td; end of td list for ed */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200793 td = td_alloc(usb_dev);
794 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200795 ed->hwHeadP = ed->hwTailP;
796 ed->state = ED_UNLINK;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200797 ed->type = usb_pipetype(pipe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200798 ohci_dev.ed_cnt++;
799 }
800
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200801 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
802 | usb_pipeendpoint(pipe) << 7
803 | (usb_pipeisoc(pipe)? 0x8000: 0)
804 | (usb_pipecontrol(pipe)? 0: \
805 (usb_pipeout(pipe)? 0x800: 0x1000))
806 | usb_pipeslow(pipe) << 13
807 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200808
Zhang Wei4dae14c2007-06-06 10:08:14 +0200809 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
810 ed->int_period = interval;
811 ed->int_load = load;
812 }
813
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200814 return ed_ret;
815}
816
817/*-------------------------------------------------------------------------*
818 * TD handling functions
819 *-------------------------------------------------------------------------*/
820
821/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
822
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200823static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200824 void *data, int len,
825 struct usb_device *dev, int index, urb_priv_t *urb_priv)
826{
827 volatile td_t *td, *td_pt;
828#ifdef OHCI_FILL_TRACE
829 int i;
830#endif
831
832 if (index > urb_priv->length) {
833 err("index > length");
834 return;
835 }
836 /* use this td as the next dummy */
837 td_pt = urb_priv->td [index];
838 td_pt->hwNextTD = 0;
839
840 /* fill the old dummy TD */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200841 td = urb_priv->td [index] =
842 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200843
844 td->ed = urb_priv->ed;
845 td->next_dl_td = NULL;
846 td->index = index;
847 td->data = (__u32)data;
848#ifdef OHCI_FILL_TRACE
Remy Bohmer48867202008-10-10 10:23:21 +0200849 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200850 for (i = 0; i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200851 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200852 printf("\n");
853 }
854#endif
855 if (!len)
856 data = 0;
857
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200858 td->hwINFO = m32_swap(info);
859 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200860 if (data)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200861 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200862 else
863 td->hwBE = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200864
865 td->hwNextTD = m32_swap((unsigned long)td_pt);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200866
867 /* append to queue */
868 td->ed->hwTailP = td->hwNextTD;
869}
870
871/*-------------------------------------------------------------------------*/
872
873/* prepare all TDs of a transfer */
874
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200875static void td_submit_job(struct usb_device *dev, unsigned long pipe,
876 void *buffer, int transfer_len,
877 struct devrequest *setup, urb_priv_t *urb,
878 int interval)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200879{
880 ohci_t *ohci = &gohci;
881 int data_len = transfer_len;
882 void *data;
883 int cnt = 0;
884 __u32 info = 0;
885 unsigned int toggle = 0;
886
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200887 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
888 * bits for reseting */
889 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200890 toggle = TD_T_TOGGLE;
891 } else {
892 toggle = TD_T_DATA0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200893 usb_settoggle(dev, usb_pipeendpoint(pipe),
894 usb_pipeout(pipe), 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200895 }
896 urb->td_cnt = 0;
897 if (data_len)
898 data = buffer;
899 else
900 data = 0;
901
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200902 switch (usb_pipetype(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200903 case PIPE_BULK:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200904 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200905 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200906 while (data_len > 4096) {
907 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
908 data, 4096, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200909 data += 4096; data_len -= 4096; cnt++;
910 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200911 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200912 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200913 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
914 data_len, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200915 cnt++;
916
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200917 if (!ohci->sleeping) {
918 /* start bulk list */
Becky Brucea5496a12010-06-30 13:05:44 -0500919 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200920 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200921 break;
922
923 case PIPE_CONTROL:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200924 /* Setup phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200925 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200926 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
927
928 /* Optional Data phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200929 if (data_len > 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200930 info = usb_pipeout(pipe)?
931 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
932 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200933 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200934 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200935 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200936
937 /* Status phase */
938 info = usb_pipeout(pipe)?
939 TD_CC | TD_DP_IN | TD_T_DATA1:
940 TD_CC | TD_DP_OUT | TD_T_DATA1;
941 td_fill(ohci, info, data, 0, dev, cnt++, urb);
942
943 if (!ohci->sleeping) {
944 /* start Control list */
Becky Brucea5496a12010-06-30 13:05:44 -0500945 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200946 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200947 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200948
949 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200950 info = usb_pipeout(urb->pipe)?
Zhang Wei4dae14c2007-06-06 10:08:14 +0200951 TD_CC | TD_DP_OUT | toggle:
952 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200953 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200954 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200955 }
956 if (urb->length != cnt)
957 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
958}
959
960/*-------------------------------------------------------------------------*
961 * Done List handling functions
962 *-------------------------------------------------------------------------*/
963
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200964/* calculate the transfer length and update the urb */
965
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200966static void dl_transfer_length(td_t *td)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200967{
Wolfgang Denk6bc52ef2011-10-05 23:03:43 +0200968 __u32 tdBE, tdCBP;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200969 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200970
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200971 tdBE = m32_swap(td->hwBE);
972 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200973
Remy Bohmer48867202008-10-10 10:23:21 +0200974 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200975 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
976 if (tdBE != 0) {
977 if (td->hwCBP == 0)
978 lurb_priv->actual_length += tdBE - td->data + 1;
979 else
980 lurb_priv->actual_length += tdCBP - td->data;
981 }
982 }
983}
984
985/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200986static void check_status(td_t *td_list)
987{
988 urb_priv_t *lurb_priv = td_list->ed->purb;
989 int urb_len = lurb_priv->length;
990 __u32 *phwHeadP = &td_list->ed->hwHeadP;
991 int cc;
992
993 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
994 if (cc) {
995 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
996
997 if (*phwHeadP & m32_swap(0x1)) {
998 if (lurb_priv &&
999 ((td_list->index + 1) < urb_len)) {
1000 *phwHeadP =
1001 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1002 m32_swap(0xfffffff0)) |
1003 (*phwHeadP & m32_swap(0x2));
1004
1005 lurb_priv->td_cnt += urb_len -
1006 td_list->index - 1;
1007 } else
1008 *phwHeadP &= m32_swap(0xfffffff2);
1009 }
1010#ifdef CONFIG_MPC5200
1011 td_list->hwNextTD = 0;
1012#endif
1013 }
1014}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001015
1016/* replies to the request have to be on a FIFO basis so
1017 * we reverse the reversed done-list */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001018static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001019{
1020 __u32 td_list_hc;
1021 td_t *td_rev = NULL;
1022 td_t *td_list = NULL;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001023
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001024 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001025 ohci->hcca->done_head = 0;
1026
1027 while (td_list_hc) {
1028 td_list = (td_t *)td_list_hc;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001029 check_status(td_list);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001030 td_list->next_dl_td = td_rev;
1031 td_rev = td_list;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001032 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001033 }
1034 return td_list;
1035}
1036
1037/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001038/*-------------------------------------------------------------------------*/
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001039
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001040static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001041{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001042 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1043 urb->finished = sohci_return_job(ohci, urb);
1044 else
1045 dbg("finish_urb: strange.., ED state %x, \n", status);
1046}
1047
1048/*
1049 * Used to take back a TD from the host controller. This would normally be
1050 * called from within dl_done_list, however it may be called directly if the
1051 * HC no longer sees the TD and it has not appeared on the donelist (after
1052 * two frames). This bug has been observed on ZF Micro systems.
1053 */
1054static int takeback_td(ohci_t *ohci, td_t *td_list)
1055{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001056 ed_t *ed;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001057 int cc;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001058 int stat = 0;
1059 /* urb_t *urb; */
1060 urb_priv_t *lurb_priv;
1061 __u32 tdINFO, edHeadP, edTailP;
1062
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001063 tdINFO = m32_swap(td_list->hwINFO);
1064
1065 ed = td_list->ed;
1066 lurb_priv = ed->purb;
1067
1068 dl_transfer_length(td_list);
1069
1070 lurb_priv->td_cnt++;
1071
1072 /* error code of transfer */
1073 cc = TD_CC_GET(tdINFO);
1074 if (cc) {
1075 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1076 stat = cc_to_error[cc];
1077 }
1078
1079 /* see if this done list makes for all TD's of current URB,
1080 * and mark the URB finished if so */
1081 if (lurb_priv->td_cnt == lurb_priv->length)
1082 finish_urb(ohci, lurb_priv, ed->state);
1083
1084 dbg("dl_done_list: processing TD %x, len %x\n",
1085 lurb_priv->td_cnt, lurb_priv->length);
1086
Remy Bohmer48867202008-10-10 10:23:21 +02001087 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001088 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1089 edTailP = m32_swap(ed->hwTailP);
1090
1091 /* unlink eds if they are not busy */
1092 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1093 ep_unlink(ohci, ed);
1094 }
1095 return stat;
1096}
1097
1098static int dl_done_list(ohci_t *ohci)
1099{
1100 int stat = 0;
1101 td_t *td_list = dl_reverse_done_list(ohci);
1102
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001103 while (td_list) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001104 td_t *td_next = td_list->next_dl_td;
1105 stat = takeback_td(ohci, td_list);
1106 td_list = td_next;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001107 }
1108 return stat;
1109}
1110
1111/*-------------------------------------------------------------------------*
1112 * Virtual Root Hub
1113 *-------------------------------------------------------------------------*/
1114
1115/* Device descriptor */
1116static __u8 root_hub_dev_des[] =
1117{
1118 0x12, /* __u8 bLength; */
1119 0x01, /* __u8 bDescriptorType; Device */
1120 0x10, /* __u16 bcdUSB; v1.1 */
1121 0x01,
1122 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1123 0x00, /* __u8 bDeviceSubClass; */
1124 0x00, /* __u8 bDeviceProtocol; */
1125 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1126 0x00, /* __u16 idVendor; */
1127 0x00,
1128 0x00, /* __u16 idProduct; */
1129 0x00,
1130 0x00, /* __u16 bcdDevice; */
1131 0x00,
1132 0x00, /* __u8 iManufacturer; */
1133 0x01, /* __u8 iProduct; */
1134 0x00, /* __u8 iSerialNumber; */
1135 0x01 /* __u8 bNumConfigurations; */
1136};
1137
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001138/* Configuration descriptor */
1139static __u8 root_hub_config_des[] =
1140{
1141 0x09, /* __u8 bLength; */
1142 0x02, /* __u8 bDescriptorType; Configuration */
1143 0x19, /* __u16 wTotalLength; */
1144 0x00,
1145 0x01, /* __u8 bNumInterfaces; */
1146 0x01, /* __u8 bConfigurationValue; */
1147 0x00, /* __u8 iConfiguration; */
1148 0x40, /* __u8 bmAttributes;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001149 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001150 0x00, /* __u8 MaxPower; */
1151
1152 /* interface */
1153 0x09, /* __u8 if_bLength; */
1154 0x04, /* __u8 if_bDescriptorType; Interface */
1155 0x00, /* __u8 if_bInterfaceNumber; */
1156 0x00, /* __u8 if_bAlternateSetting; */
1157 0x01, /* __u8 if_bNumEndpoints; */
1158 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1159 0x00, /* __u8 if_bInterfaceSubClass; */
1160 0x00, /* __u8 if_bInterfaceProtocol; */
1161 0x00, /* __u8 if_iInterface; */
1162
1163 /* endpoint */
1164 0x07, /* __u8 ep_bLength; */
1165 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1166 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1167 0x03, /* __u8 ep_bmAttributes; Interrupt */
1168 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1169 0x00,
1170 0xff /* __u8 ep_bInterval; 255 ms */
1171};
1172
1173static unsigned char root_hub_str_index0[] =
1174{
1175 0x04, /* __u8 bLength; */
1176 0x03, /* __u8 bDescriptorType; String-descriptor */
1177 0x09, /* __u8 lang ID */
1178 0x04, /* __u8 lang ID */
1179};
1180
1181static unsigned char root_hub_str_index1[] =
1182{
1183 28, /* __u8 bLength; */
1184 0x03, /* __u8 bDescriptorType; String-descriptor */
1185 'O', /* __u8 Unicode */
1186 0, /* __u8 Unicode */
1187 'H', /* __u8 Unicode */
1188 0, /* __u8 Unicode */
1189 'C', /* __u8 Unicode */
1190 0, /* __u8 Unicode */
1191 'I', /* __u8 Unicode */
1192 0, /* __u8 Unicode */
1193 ' ', /* __u8 Unicode */
1194 0, /* __u8 Unicode */
1195 'R', /* __u8 Unicode */
1196 0, /* __u8 Unicode */
1197 'o', /* __u8 Unicode */
1198 0, /* __u8 Unicode */
1199 'o', /* __u8 Unicode */
1200 0, /* __u8 Unicode */
1201 't', /* __u8 Unicode */
1202 0, /* __u8 Unicode */
1203 ' ', /* __u8 Unicode */
1204 0, /* __u8 Unicode */
1205 'H', /* __u8 Unicode */
1206 0, /* __u8 Unicode */
1207 'u', /* __u8 Unicode */
1208 0, /* __u8 Unicode */
1209 'b', /* __u8 Unicode */
1210 0, /* __u8 Unicode */
1211};
1212
1213/* Hub class-specific descriptor is constructed dynamically */
1214
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001215/*-------------------------------------------------------------------------*/
1216
1217#define OK(x) len = (x); break
1218#ifdef DEBUG
Becky Brucea5496a12010-06-30 13:05:44 -05001219#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001220 &gohci.regs->roothub.status); }
1221#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Becky Brucea5496a12010-06-30 13:05:44 -05001222 (x)); ohci_writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001223#else
Becky Brucea5496a12010-06-30 13:05:44 -05001224#define WR_RH_STAT(x) ohci_writel((x), &gohci.regs->roothub.status)
1225#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001226 &gohci.regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001227#endif
1228#define RD_RH_STAT roothub_status(&gohci)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001229#define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001230
1231/* request to virtual root hub */
1232
1233int rh_check_port_status(ohci_t *controller)
1234{
1235 __u32 temp, ndp, i;
1236 int res;
1237
1238 res = -1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001239 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001240 ndp = (temp & RH_A_NDP);
1241#ifdef CONFIG_AT91C_PQFP_UHPBUG
1242 ndp = (ndp == 2) ? 1:0;
1243#endif
1244 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001245 temp = roothub_portstatus(controller, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001246 /* check for a device disconnect */
1247 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1248 (RH_PS_PESC | RH_PS_CSC)) &&
1249 ((temp & RH_PS_CCS) == 0)) {
1250 res = i;
1251 break;
1252 }
1253 }
1254 return res;
1255}
1256
1257static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
1258 void *buffer, int transfer_len, struct devrequest *cmd)
1259{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001260 void *data = buffer;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001261 int leni = transfer_len;
1262 int len = 0;
1263 int stat = 0;
1264 __u32 datab[4];
1265 __u8 *data_buf = (__u8 *)datab;
1266 __u16 bmRType_bReq;
1267 __u16 wValue;
1268 __u16 wIndex;
1269 __u16 wLength;
1270
1271#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001272pkt_print(NULL, dev, pipe, buffer, transfer_len,
1273 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001274#else
1275 wait_ms(1);
1276#endif
Remy Bohmer48867202008-10-10 10:23:21 +02001277 if (usb_pipeint(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001278 info("Root-Hub submit IRQ: NOT implemented");
1279 return 0;
1280 }
1281
1282 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001283 wValue = le16_to_cpu(cmd->value);
1284 wIndex = le16_to_cpu(cmd->index);
1285 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001286
1287 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1288 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1289
1290 switch (bmRType_bReq) {
1291 /* Request Destination:
1292 without flags: Device,
1293 RH_INTERFACE: interface,
1294 RH_ENDPOINT: endpoint,
1295 RH_CLASS means HUB here,
1296 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1297 */
1298
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001299 case RH_GET_STATUS:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001300 *(__u16 *) data_buf = cpu_to_le16(1);
1301 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001302 case RH_GET_STATUS | RH_INTERFACE:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001303 *(__u16 *) data_buf = cpu_to_le16(0);
1304 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001305 case RH_GET_STATUS | RH_ENDPOINT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001306 *(__u16 *) data_buf = cpu_to_le16(0);
1307 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001308 case RH_GET_STATUS | RH_CLASS:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001309 *(__u32 *) data_buf = cpu_to_le32(
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001310 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001311 OK(4);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001312 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001313 *(__u32 *) data_buf = cpu_to_le32(RD_RH_PORTSTAT);
1314 OK(4);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001315
1316 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1317 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001318 case (RH_ENDPOINT_STALL):
1319 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001320 }
1321 break;
1322
1323 case RH_CLEAR_FEATURE | RH_CLASS:
1324 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001325 case RH_C_HUB_LOCAL_POWER:
1326 OK(0);
1327 case (RH_C_HUB_OVER_CURRENT):
1328 WR_RH_STAT(RH_HS_OCIC);
1329 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001330 }
1331 break;
1332
1333 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1334 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001335 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1336 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1337 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1338 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1339 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1340 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1341 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1342 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001343 }
1344 break;
1345
1346 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1347 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001348 case (RH_PORT_SUSPEND):
1349 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1350 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1351 if (RD_RH_PORTSTAT & RH_PS_CCS)
1352 WR_RH_PORTSTAT(RH_PS_PRS);
1353 OK(0);
1354 case (RH_PORT_POWER):
1355 WR_RH_PORTSTAT(RH_PS_PPS);
1356 wait_ms(100);
1357 OK(0);
1358 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1359 if (RD_RH_PORTSTAT & RH_PS_CCS)
1360 WR_RH_PORTSTAT(RH_PS_PES);
1361 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001362 }
1363 break;
1364
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001365 case RH_SET_ADDRESS:
1366 gohci.rh.devnum = wValue;
1367 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001368
1369 case RH_GET_DESCRIPTOR:
1370 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001371 case (0x01): /* device descriptor */
1372 len = min_t(unsigned int,
1373 leni,
1374 min_t(unsigned int,
1375 sizeof(root_hub_dev_des),
1376 wLength));
1377 data_buf = root_hub_dev_des; OK(len);
1378 case (0x02): /* configuration descriptor */
1379 len = min_t(unsigned int,
1380 leni,
1381 min_t(unsigned int,
1382 sizeof(root_hub_config_des),
1383 wLength));
1384 data_buf = root_hub_config_des; OK(len);
1385 case (0x03): /* string descriptors */
1386 if (wValue == 0x0300) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001387 len = min_t(unsigned int,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001388 leni,
1389 min_t(unsigned int,
1390 sizeof(root_hub_str_index0),
1391 wLength));
1392 data_buf = root_hub_str_index0;
1393 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001394 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001395 if (wValue == 0x0301) {
1396 len = min_t(unsigned int,
1397 leni,
1398 min_t(unsigned int,
1399 sizeof(root_hub_str_index1),
1400 wLength));
1401 data_buf = root_hub_str_index1;
1402 OK(len);
1403 }
1404 default:
1405 stat = USB_ST_STALLED;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001406 }
1407 break;
1408
1409 case RH_GET_DESCRIPTOR | RH_CLASS:
1410 {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001411 __u32 temp = roothub_a(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001412
1413 data_buf [0] = 9; /* min length; */
1414 data_buf [1] = 0x29;
1415 data_buf [2] = temp & RH_A_NDP;
1416#ifdef CONFIG_AT91C_PQFP_UHPBUG
1417 data_buf [2] = (data_buf [2] == 2) ? 1:0;
1418#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001419 data_buf [3] = 0;
1420 if (temp & RH_A_PSM) /* per-port power switching? */
1421 data_buf [3] |= 0x1;
1422 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1423 data_buf [3] |= 0x10;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001424 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001425 data_buf [3] |= 0x8;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001426
1427 /* corresponds to data_buf[4-7] */
1428 datab [1] = 0;
1429 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001430 temp = roothub_b(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001431 data_buf [7] = temp & RH_B_DR;
1432 if (data_buf [2] < 7) {
1433 data_buf [8] = 0xff;
1434 } else {
1435 data_buf [0] += 2;
1436 data_buf [8] = (temp & RH_B_DR) >> 8;
1437 data_buf [10] = data_buf [9] = 0xff;
1438 }
1439
1440 len = min_t(unsigned int, leni,
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001441 min_t(unsigned int, data_buf [0], wLength));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001442 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001443 }
1444
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001445 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001446
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001447 case RH_SET_CONFIGURATION: WR_RH_STAT(0x10000); OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001448
1449 default:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001450 dbg("unsupported root hub command");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001451 stat = USB_ST_STALLED;
1452 }
1453
1454#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001455 ohci_dump_roothub(&gohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001456#else
1457 wait_ms(1);
1458#endif
1459
1460 len = min_t(int, len, leni);
1461 if (data != data_buf)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001462 memcpy(data, data_buf, len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001463 dev->act_len = len;
1464 dev->status = stat;
1465
1466#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001467 pkt_print(NULL, dev, pipe, buffer,
1468 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001469#else
1470 wait_ms(1);
1471#endif
1472
1473 return stat;
1474}
1475
1476/*-------------------------------------------------------------------------*/
1477
1478/* common code for handling submit messages - used for all but root hub */
1479/* accesses. */
1480int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1481 int transfer_len, struct devrequest *setup, int interval)
1482{
1483 int stat = 0;
1484 int maxsize = usb_maxpacket(dev, pipe);
1485 int timeout;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001486 urb_priv_t *urb;
1487
1488 urb = malloc(sizeof(urb_priv_t));
1489 memset(urb, 0, sizeof(urb_priv_t));
1490
1491 urb->dev = dev;
1492 urb->pipe = pipe;
1493 urb->transfer_buffer = buffer;
1494 urb->transfer_buffer_length = transfer_len;
1495 urb->interval = interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001496
1497 /* device pulled? Shortcut the action. */
1498 if (devgone == dev) {
1499 dev->status = USB_ST_CRC_ERR;
1500 return 0;
1501 }
1502
1503#ifdef DEBUG
Zhang Wei4dae14c2007-06-06 10:08:14 +02001504 urb->actual_length = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001505 pkt_print(urb, dev, pipe, buffer, transfer_len,
1506 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001507#else
1508 wait_ms(1);
1509#endif
1510 if (!maxsize) {
1511 err("submit_common_message: pipesize for pipe %lx is zero",
1512 pipe);
1513 return -1;
1514 }
1515
Zhang Wei4dae14c2007-06-06 10:08:14 +02001516 if (sohci_submit_job(urb, setup) < 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001517 err("sohci_submit_job failed");
1518 return -1;
1519 }
1520
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001521#if 0
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001522 wait_ms(10);
1523 /* ohci_dump_status(&gohci); */
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001524#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001525
Simon Glass96820a32011-02-07 14:42:16 -08001526 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001527
1528 /* wait for it to complete */
1529 for (;;) {
1530 /* check whether the controller is done */
1531 stat = hc_interrupt();
1532 if (stat < 0) {
1533 stat = USB_ST_CRC_ERR;
1534 break;
1535 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001536
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001537 /* NOTE: since we are not interrupt driven in U-Boot and always
1538 * handle only one URB at a time, we cannot assume the
1539 * transaction finished on the first successful return from
1540 * hc_interrupt().. unless the flag for current URB is set,
1541 * meaning that all TD's to/from device got actually
1542 * transferred and processed. If the current URB is not
1543 * finished we need to re-iterate this loop so as
1544 * hc_interrupt() gets called again as there needs to be some
1545 * more TD's to process still */
Zhang Wei4dae14c2007-06-06 10:08:14 +02001546 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001547 /* 0xff is returned for an SF-interrupt */
1548 break;
1549 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001550
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001551 if (--timeout) {
1552 wait_ms(1);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001553 if (!urb->finished)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001554 dbg("*");
Zhang Wei4dae14c2007-06-06 10:08:14 +02001555
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001556 } else {
1557 err("CTL:TIMEOUT ");
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001558 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001559 urb->finished = 1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001560 stat = USB_ST_CRC_ERR;
1561 break;
1562 }
1563 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001564
1565 dev->status = stat;
1566 dev->act_len = transfer_len;
1567
1568#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001569 pkt_print(urb, dev, pipe, buffer, transfer_len,
1570 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001571#else
1572 wait_ms(1);
1573#endif
1574
1575 /* free TDs in urb_priv */
Remy Bohmer48867202008-10-10 10:23:21 +02001576 if (!usb_pipeint(pipe))
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001577 urb_free_priv(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001578 return 0;
1579}
1580
1581/* submit routines called from usb.c */
1582int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1583 int transfer_len)
1584{
1585 info("submit_bulk_msg");
1586 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1587}
1588
1589int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1590 int transfer_len, struct devrequest *setup)
1591{
1592 int maxsize = usb_maxpacket(dev, pipe);
1593
1594 info("submit_control_msg");
1595#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001596 pkt_print(NULL, dev, pipe, buffer, transfer_len,
1597 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001598#else
1599 wait_ms(1);
1600#endif
1601 if (!maxsize) {
1602 err("submit_control_message: pipesize for pipe %lx is zero",
1603 pipe);
1604 return -1;
1605 }
1606 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1607 gohci.rh.dev = dev;
1608 /* root hub - redirect */
1609 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1610 setup);
1611 }
1612
1613 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1614}
1615
1616int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1617 int transfer_len, int interval)
1618{
1619 info("submit_int_msg");
Zhang Wei4dae14c2007-06-06 10:08:14 +02001620 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL,
1621 interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001622}
1623
1624/*-------------------------------------------------------------------------*
1625 * HC functions
1626 *-------------------------------------------------------------------------*/
1627
1628/* reset the HC and BUS */
1629
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001630static int hc_reset(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001631{
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001632#ifdef CONFIG_PCI_EHCI_DEVNO
1633 pci_dev_t pdev;
1634#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001635 int timeout = 30;
1636 int smm_timeout = 50; /* 0,5 sec */
1637
1638 dbg("%s\n", __FUNCTION__);
1639
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001640#ifdef CONFIG_PCI_EHCI_DEVNO
1641 /*
1642 * Some multi-function controllers (e.g. ISP1562) allow root hub
1643 * resetting via EHCI registers only.
1644 */
1645 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1646 if (pdev != -1) {
1647 u32 base;
1648 int timeout = 1000;
1649
1650 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Brucea5496a12010-06-30 13:05:44 -05001651 base += EHCI_USBCMD_OFF;
1652 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001653
Becky Brucea5496a12010-06-30 13:05:44 -05001654 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001655 if (timeout-- <= 0) {
1656 printf("USB RootHub reset timed out!");
1657 break;
1658 }
1659 udelay(1);
1660 }
1661 } else
1662 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1663#endif
Becky Brucea5496a12010-06-30 13:05:44 -05001664 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1665 /* SMM owns the HC, request ownership */
1666 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001667 info("USB HC TakeOver from SMM");
Becky Brucea5496a12010-06-30 13:05:44 -05001668 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001669 wait_ms(10);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001670 if (--smm_timeout == 0) {
1671 err("USB HC TakeOver failed!");
1672 return -1;
1673 }
1674 }
1675 }
1676
1677 /* Disable HC interrupts */
Becky Brucea5496a12010-06-30 13:05:44 -05001678 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001679
1680 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1681 ohci->slot_name,
Becky Brucea5496a12010-06-30 13:05:44 -05001682 ohci_readl(&ohci->regs->control));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001683
1684 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +01001685 ohci->hc_control = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001686 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001687
1688 /* HC Reset requires max 10 us delay */
Becky Brucea5496a12010-06-30 13:05:44 -05001689 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1690 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001691 if (--timeout == 0) {
1692 err("USB HC reset timed out!");
1693 return -1;
1694 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001695 udelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001696 }
1697 return 0;
1698}
1699
1700/*-------------------------------------------------------------------------*/
1701
1702/* Start an OHCI controller, set the BUS operational
1703 * enable interrupts
1704 * connect the virtual root hub */
1705
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001706static int hc_start(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001707{
1708 __u32 mask;
1709 unsigned int fminterval;
1710
1711 ohci->disabled = 1;
1712
1713 /* Tell the controller where the control and bulk lists are
1714 * The lists are empty now. */
1715
Becky Brucea5496a12010-06-30 13:05:44 -05001716 ohci_writel(0, &ohci->regs->ed_controlhead);
1717 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001718
Becky Brucea5496a12010-06-30 13:05:44 -05001719 ohci_writel((__u32)ohci->hcca,
1720 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001721
1722 fminterval = 0x2edf;
Becky Brucea5496a12010-06-30 13:05:44 -05001723 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001724 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Brucea5496a12010-06-30 13:05:44 -05001725 ohci_writel(fminterval, &ohci->regs->fminterval);
1726 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001727
1728 /* start controller operations */
1729 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1730 ohci->disabled = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001731 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001732
1733 /* disable all interrupts */
1734 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1735 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1736 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Brucea5496a12010-06-30 13:05:44 -05001737 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001738 /* clear all interrupts */
1739 mask &= ~OHCI_INTR_MIE;
Becky Brucea5496a12010-06-30 13:05:44 -05001740 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001741 /* Choose the interrupts we care about now - but w/o MIE */
1742 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Brucea5496a12010-06-30 13:05:44 -05001743 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001744
1745#ifdef OHCI_USE_NPS
1746 /* required for AMD-756 and some Mac platforms */
Becky Brucea5496a12010-06-30 13:05:44 -05001747 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001748 &ohci->regs->roothub.a);
Becky Brucea5496a12010-06-30 13:05:44 -05001749 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001750#endif /* OHCI_USE_NPS */
1751
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001752#define mdelay(n) ({unsigned long msec = (n); while (msec--) udelay(1000); })
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001753 /* POTPGT delay is bits 24-31, in 2 ms units. */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001754 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001755
1756 /* connect the virtual root hub */
1757 ohci->rh.devnum = 0;
1758
1759 return 0;
1760}
1761
1762/*-------------------------------------------------------------------------*/
1763
Zhang Wei4dae14c2007-06-06 10:08:14 +02001764/* Poll USB interrupt. */
1765void usb_event_poll(void)
1766{
1767 hc_interrupt();
1768}
1769
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001770/* an interrupt happens */
1771
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001772static int hc_interrupt(void)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001773{
1774 ohci_t *ohci = &gohci;
1775 struct ohci_regs *regs = ohci->regs;
1776 int ints;
1777 int stat = -1;
1778
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001779 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001780 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001781 ints = OHCI_INTR_WDH;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001782 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001783 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001784 if (ints == ~(u32)0) {
1785 ohci->disabled++;
1786 err("%s device removed!", ohci->slot_name);
1787 return -1;
1788 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001789 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001790 if (ints == 0) {
1791 dbg("hc_interrupt: returning..\n");
1792 return 0xff;
1793 }
1794 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001795 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +02001796
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001797 /* dbg("Interrupt: %x frame: %x", ints,
1798 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001799
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001800 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001801 stat = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001802
1803 if (ints & OHCI_INTR_UE) {
1804 ohci->disabled++;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001805 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001806 ohci->slot_name);
1807 /* e.g. due to PCI Master/Target Abort */
1808
1809#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001810 ohci_dump(ohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001811#else
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001812 wait_ms(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001813#endif
1814 /* FIXME: be optimistic, hope that bug won't repeat often. */
1815 /* Make some non-interrupt context restart the controller. */
1816 /* Count and limit the retries though; either hardware or */
1817 /* software errors can go forever... */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001818 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001819 return -1;
1820 }
1821
1822 if (ints & OHCI_INTR_WDH) {
1823 wait_ms(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001824 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1825 (void)ohci_readl(&regs->intrdisable); /* flush */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001826 stat = dl_done_list(&gohci);
Becky Brucea5496a12010-06-30 13:05:44 -05001827 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1828 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001829 }
1830
1831 if (ints & OHCI_INTR_SO) {
1832 dbg("USB Schedule overrun\n");
Becky Brucea5496a12010-06-30 13:05:44 -05001833 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001834 stat = -1;
1835 }
1836
1837 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1838 if (ints & OHCI_INTR_SF) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001839 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001840 wait_ms(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001841 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001842 if (ohci->ed_rm_list[frame] != NULL)
Becky Brucea5496a12010-06-30 13:05:44 -05001843 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001844 stat = 0xff;
1845 }
1846
Becky Brucea5496a12010-06-30 13:05:44 -05001847 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001848 return stat;
1849}
1850
1851/*-------------------------------------------------------------------------*/
1852
1853/*-------------------------------------------------------------------------*/
1854
1855/* De-allocate all resources.. */
1856
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001857static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001858{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001859 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001860
1861 if (!ohci->disabled)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001862 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001863}
1864
1865/*-------------------------------------------------------------------------*/
1866
1867/*
1868 * low level initalisation routine, called from usb.c
1869 */
1870static char ohci_inited = 0;
1871
1872int usb_lowlevel_init(void)
1873{
Zhang Wei4dae14c2007-06-06 10:08:14 +02001874#ifdef CONFIG_PCI_OHCI
1875 pci_dev_t pdev;
1876#endif
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001877
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001878#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001879 /* cpu dependant init */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001880 if (usb_cpu_init())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001881 return -1;
1882#endif
1883
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001884#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001885 /* board dependant init */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001886 if (usb_board_init())
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001887 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001888#endif
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001889 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001890
1891 /* align the storage */
1892 if ((__u32)&ghcca[0] & 0xff) {
1893 err("HCCA not aligned!!");
1894 return -1;
1895 }
1896 phcca = &ghcca[0];
1897 info("aligned ghcca %p", phcca);
1898 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1899 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1900 err("EDs not aligned!!");
1901 return -1;
1902 }
1903 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1904 if ((__u32)gtd & 0x7) {
1905 err("TDs not aligned!!");
1906 return -1;
1907 }
1908 ptd = gtd;
1909 gohci.hcca = phcca;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001910 memset(phcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001911
1912 gohci.disabled = 1;
1913 gohci.sleeping = 0;
1914 gohci.irq = -1;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001915#ifdef CONFIG_PCI_OHCI
Sergei Poselenov477434c2008-05-22 01:15:53 +02001916 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001917
1918 if (pdev != -1) {
1919 u16 vid, did;
1920 u32 base;
1921 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1922 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1923 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1924 vid, did, (pdev >> 16) & 0xff,
1925 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1926 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1927 printf("OHCI regs address 0x%08x\n", base);
1928 gohci.regs = (struct ohci_regs *)base;
1929 } else
1930 return -1;
1931#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001932 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001933#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001934
1935 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001936 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001937
1938 if (hc_reset (&gohci) < 0) {
1939 hc_release_ohci (&gohci);
1940 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001941#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001942 /* board dependant cleanup */
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001943 usb_board_init_fail();
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001944#endif
1945
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001946#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001947 /* cpu dependant cleanup */
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001948 usb_cpu_init_fail();
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001949#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001950 return -1;
1951 }
1952
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001953 if (hc_start(&gohci) < 0) {
1954 err("can't start usb-%s", gohci.slot_name);
1955 hc_release_ohci(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001956 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001957#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001958 /* board dependant cleanup */
1959 usb_board_stop();
1960#endif
1961
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001962#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001963 /* cpu dependant cleanup */
1964 usb_cpu_stop();
1965#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001966 return -1;
1967 }
1968
1969#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001970 ohci_dump(&gohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001971#else
1972 wait_ms(1);
1973#endif
1974 ohci_inited = 1;
1975 return 0;
1976}
1977
1978int usb_lowlevel_stop(void)
1979{
1980 /* this gets called really early - before the controller has */
1981 /* even been initialized! */
1982 if (!ohci_inited)
1983 return 0;
1984 /* TODO release any interrupts, etc. */
1985 /* call hc_release_ohci() here ? */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001986 hc_reset(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001987
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001988#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001989 /* board dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001990 if (usb_board_stop())
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001991 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001992#endif
1993
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001994#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001995 /* cpu dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001996 if (usb_cpu_stop())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001997 return -1;
1998#endif
Remy Bohmereba1f2f2008-08-20 11:22:02 +02001999 /* This driver is no longer initialised. It needs a new low-level
2000 * init (board/cpu) before it can be used again. */
2001 ohci_inited = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02002002 return 0;
2003}