blob: 67d478f8701383cce383b55064fc0b39cb1b5ee1 [file] [log] [blame]
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001/*
Zhang Wei8d15efa2007-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 Klotzbuecherd8d023f2006-05-22 16:33:54 +02009 *
10 * (C) Copyright 2003
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020011 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
Markus Klotzbuecherd8d023f2006-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 Baren5afb2fe2009-02-05 22:18:02 -050030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Markus Klotzbuecherd8d023f2006-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 Klotzbuecher18f5ccb2007-06-06 11:49:35 +020041 * 1 - Read doc/README.generic_usb_ohci
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020042 * 2 - this driver is intended for use with USB Mass Storage Devices
Zhang Wei8d15efa2007-06-06 10:08:14 +020043 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020044 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
Markus Klotzbuecherd8d023f2006-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 Klotzbuecher18f5ccb2007-06-06 11:49:35 +020049#include <asm/byteorder.h>
50
51#if defined(CONFIG_PCI_OHCI)
Zhang Wei8d15efa2007-06-06 10:08:14 +020052# include <pci.h>
Sergei Poselenov0b4570f2008-05-22 01:15:53 +020053#if !defined(CONFIG_PCI_OHCI_DEVNO)
54#define CONFIG_PCI_OHCI_DEVNO 0
55#endif
Markus Klotzbuecheraa219212006-05-30 16:56:14 +020056#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020057
58#include <malloc.h>
59#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020060
61#include "ohci.h"
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020062
Wolfgang Denkdcac38b2007-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 Klotzbuecheraa219212006-05-30 16:56:14 +020067#if defined(CONFIG_ARM920T) || \
68 defined(CONFIG_S3C2400) || \
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010069 defined(CONFIG_S3C2410) || \
Guennadi Liakhovetskie726e7a2008-08-31 00:39:46 +020070 defined(CONFIG_S3C6400) || \
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010071 defined(CONFIG_440EP) || \
Zhang Wei8d15efa2007-06-06 10:08:14 +020072 defined(CONFIG_PCI_OHCI) || \
Stefan Roese41e74eb2008-03-05 12:29:32 +010073 defined(CONFIG_MPC5200) || \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020074 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020075# define OHCI_USE_NPS /* force NoPowerSwitching mode */
76#endif
77
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020078#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010079#undef DEBUG
80#undef SHOW_INFO
81#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020082
83/* For initializing controller (mask in an HCFS mode too) */
84#define OHCI_CONTROL_INIT \
85 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
86
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020087/*
88 * e.g. PCI controllers need this
89 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020090#ifdef CONFIG_SYS_OHCI_SWAP_REG_ACCESS
Markus Klotzbüchera84dfb32008-04-23 10:53:23 +020091# define readl(a) __swap_32(*((volatile u32 *)(a)))
92# define writel(a, b) (*((volatile u32 *)(b)) = __swap_32((volatile u32)a))
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020093#else
Markus Klotzbüchera84dfb32008-04-23 10:53:23 +020094# define readl(a) (*((volatile u32 *)(a)))
95# define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096#endif /* CONFIG_SYS_OHCI_SWAP_REG_ACCESS */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020097
Remy Bohmer172a1132008-09-16 14:55:43 +020098#define min_t(type, x, y) \
99 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200100
Zhang Wei8d15efa2007-06-06 10:08:14 +0200101#ifdef CONFIG_PCI_OHCI
102static struct pci_device_id ohci_pci_ids[] = {
103 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saadae3b28422007-09-17 17:04:47 +0200104 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew923f43a2008-01-11 20:42:58 -0600105 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200106 /* Please add supported PCI OHCI controller ids here */
107 {0, 0}
108};
109#endif
110
Yuri Tikhonov11af42c2008-09-04 11:19:05 +0200111#ifdef CONFIG_PCI_EHCI_DEVNO
112static struct pci_device_id ehci_pci_ids[] = {
113 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
114 /* Please add supported PCI EHCI controller ids here */
115 {0, 0}
116};
117#endif
118
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200119#ifdef DEBUG
120#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
121#else
Remy Bohmer172a1132008-09-16 14:55:43 +0200122#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200123#endif /* DEBUG */
124#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200125#ifdef SHOW_INFO
126#define info(format, arg...) printf("INFO: " format "\n", ## arg)
127#else
Remy Bohmer172a1132008-09-16 14:55:43 +0200128#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200129#endif
130
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200131#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200132# define m16_swap(x) cpu_to_be16(x)
133# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +0100134#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200135# define m16_swap(x) cpu_to_le16(x)
136# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200137#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200138
139/* global ohci_t */
140static ohci_t gohci;
141/* this must be aligned to a 256 byte boundary */
142struct ohci_hcca ghcca[1];
143/* a pointer to the aligned storage */
144struct ohci_hcca *phcca;
145/* this allocates EDs for all possible endpoints */
146struct ohci_device ohci_dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200147/* device which was disconnected */
148struct usb_device *devgone;
149
Remy Bohmer172a1132008-09-16 14:55:43 +0200150static inline u32 roothub_a(struct ohci *hc)
151 { return readl(&hc->regs->roothub.a); }
152static inline u32 roothub_b(struct ohci *hc)
153 { return readl(&hc->regs->roothub.b); }
154static inline u32 roothub_status(struct ohci *hc)
155 { return readl(&hc->regs->roothub.status); }
156static inline u32 roothub_portstatus(struct ohci *hc, int i)
157 { return readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200158
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200159/* forward declaration */
Remy Bohmer172a1132008-09-16 14:55:43 +0200160static int hc_interrupt(void);
161static void td_submit_job(struct usb_device *dev, unsigned long pipe,
162 void *buffer, int transfer_len,
163 struct devrequest *setup, urb_priv_t *urb,
164 int interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200165
166/*-------------------------------------------------------------------------*
167 * URB support functions
168 *-------------------------------------------------------------------------*/
169
170/* free HCD-private data associated with this URB */
171
Remy Bohmer172a1132008-09-16 14:55:43 +0200172static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200173{
174 int i;
175 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200176 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200177
178 last = urb->length - 1;
179 if (last >= 0) {
180 for (i = 0; i <= last; i++) {
181 td = urb->td[i];
182 if (td) {
183 td->usb_dev = NULL;
184 urb->td[i] = NULL;
185 }
186 }
187 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200188 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200189}
190
191/*-------------------------------------------------------------------------*/
192
193#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +0200194static int sohci_get_current_frame_number(struct usb_device *dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200195
196/* debug| print the main components of an URB
197 * small: 0) header + data packets 1) just header */
198
Remy Bohmer172a1132008-09-16 14:55:43 +0200199static void pkt_print(urb_priv_t *purb, struct usb_device *dev,
200 unsigned long pipe, void *buffer, int transfer_len,
201 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200202{
Remy Bohmer172a1132008-09-16 14:55:43 +0200203 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200204 str,
Remy Bohmer172a1132008-09-16 14:55:43 +0200205 sohci_get_current_frame_number(dev),
206 usb_pipedevice(pipe),
207 usb_pipeendpoint(pipe),
208 usb_pipeout(pipe)? 'O': 'I',
209 usb_pipetype(pipe) < 2 ? \
210 (usb_pipeint(pipe)? "INTR": "ISOC"): \
211 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200212 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200213 transfer_len, dev->status);
214#ifdef OHCI_VERBOSE_DEBUG
215 if (!small) {
216 int i, len;
217
Remy Bohmer172a1132008-09-16 14:55:43 +0200218 if (usb_pipecontrol(pipe)) {
219 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200220 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200221 printf(" %02x", ((__u8 *) setup) [i]);
222 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200223 }
224 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200225 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200226 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200227 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200228 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200229 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200230 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200231 printf(" %02x", ((__u8 *) buffer) [i]);
232 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200233 }
234 }
235#endif
236}
237
Remy Bohmer172a1132008-09-16 14:55:43 +0200238/* just for debugging; prints non-empty branches of the int ed tree
239 * inclusive iso eds */
240void ep_print_int_eds(ohci_t *ohci, char *str)
241{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200242 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200243 __u32 *ed_p;
244 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200245 j = 5;
246 ed_p = &(ohci->hcca->int_table [i]);
247 if (*ed_p == 0)
248 continue;
Remy Bohmer172a1132008-09-16 14:55:43 +0200249 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200250 while (*ed_p != 0 && j--) {
251 ed_t *ed = (ed_t *)m32_swap(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200252 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200253 ed_p = &ed->hwNextED;
254 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200255 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200256 }
257}
258
Remy Bohmer172a1132008-09-16 14:55:43 +0200259static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200260{
Remy Bohmer172a1132008-09-16 14:55:43 +0200261 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200262 label,
263 mask,
264 (mask & OHCI_INTR_MIE) ? " MIE" : "",
265 (mask & OHCI_INTR_OC) ? " OC" : "",
266 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
267 (mask & OHCI_INTR_FNO) ? " FNO" : "",
268 (mask & OHCI_INTR_UE) ? " UE" : "",
269 (mask & OHCI_INTR_RD) ? " RD" : "",
270 (mask & OHCI_INTR_SF) ? " SF" : "",
271 (mask & OHCI_INTR_WDH) ? " WDH" : "",
272 (mask & OHCI_INTR_SO) ? " SO" : ""
273 );
274}
275
Remy Bohmer172a1132008-09-16 14:55:43 +0200276static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200277{
278 ed_t *edp = (ed_t *)value;
279
280 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200281 dbg("%s %08x", label, value);
282 dbg("%08x", edp->hwINFO);
283 dbg("%08x", edp->hwTailP);
284 dbg("%08x", edp->hwHeadP);
285 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200286 }
287}
288
Remy Bohmer172a1132008-09-16 14:55:43 +0200289static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200290{
291 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200292 case OHCI_USB_RESET: return "reset";
293 case OHCI_USB_RESUME: return "resume";
294 case OHCI_USB_OPER: return "operational";
295 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200296 }
297 return "?";
298}
299
300/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200301static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200302{
303 struct ohci_regs *regs = controller->regs;
304 __u32 temp;
305
Remy Bohmer172a1132008-09-16 14:55:43 +0200306 temp = readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200307 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200308 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200309
Remy Bohmer172a1132008-09-16 14:55:43 +0200310 temp = readl(&regs->control);
311 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200312 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
313 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
314 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200315 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200316 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
317 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
318 (temp & OHCI_CTRL_IE) ? " IE" : "",
319 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
320 temp & OHCI_CTRL_CBSR
321 );
322
Remy Bohmer172a1132008-09-16 14:55:43 +0200323 temp = readl(&regs->cmdstatus);
324 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200325 (temp & OHCI_SOC) >> 16,
326 (temp & OHCI_OCR) ? " OCR" : "",
327 (temp & OHCI_BLF) ? " BLF" : "",
328 (temp & OHCI_CLF) ? " CLF" : "",
329 (temp & OHCI_HCR) ? " HCR" : ""
330 );
331
Remy Bohmer172a1132008-09-16 14:55:43 +0200332 ohci_dump_intr_mask("intrstatus", readl(&regs->intrstatus));
333 ohci_dump_intr_mask("intrenable", readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200334
Remy Bohmer172a1132008-09-16 14:55:43 +0200335 maybe_print_eds("ed_periodcurrent", readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200336
Remy Bohmer172a1132008-09-16 14:55:43 +0200337 maybe_print_eds("ed_controlhead", readl(&regs->ed_controlhead));
338 maybe_print_eds("ed_controlcurrent", readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200339
Remy Bohmer172a1132008-09-16 14:55:43 +0200340 maybe_print_eds("ed_bulkhead", readl(&regs->ed_bulkhead));
341 maybe_print_eds("ed_bulkcurrent", readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200342
Remy Bohmer172a1132008-09-16 14:55:43 +0200343 maybe_print_eds("donehead", readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200344}
345
Remy Bohmer172a1132008-09-16 14:55:43 +0200346static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200347{
348 __u32 temp, ndp, i;
349
Remy Bohmer172a1132008-09-16 14:55:43 +0200350 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200351 ndp = (temp & RH_A_NDP);
352#ifdef CONFIG_AT91C_PQFP_UHPBUG
353 ndp = (ndp == 2) ? 1:0;
354#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200355 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200356 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200357 ((temp & RH_A_POTPGT) >> 24) & 0xff,
358 (temp & RH_A_NOCP) ? " NOCP" : "",
359 (temp & RH_A_OCPM) ? " OCPM" : "",
360 (temp & RH_A_DT) ? " DT" : "",
361 (temp & RH_A_NPS) ? " NPS" : "",
362 (temp & RH_A_PSM) ? " PSM" : "",
363 ndp
364 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200365 temp = roothub_b(controller);
366 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200367 temp,
368 (temp & RH_B_PPCM) >> 16,
369 (temp & RH_B_DR)
370 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200371 temp = roothub_status(controller);
372 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200373 temp,
374 (temp & RH_HS_CRWE) ? " CRWE" : "",
375 (temp & RH_HS_OCIC) ? " OCIC" : "",
376 (temp & RH_HS_LPSC) ? " LPSC" : "",
377 (temp & RH_HS_DRWE) ? " DRWE" : "",
378 (temp & RH_HS_OCI) ? " OCI" : "",
379 (temp & RH_HS_LPS) ? " LPS" : ""
380 );
381 }
382
383 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200384 temp = roothub_portstatus(controller, i);
385 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200386 i,
387 temp,
388 (temp & RH_PS_PRSC) ? " PRSC" : "",
389 (temp & RH_PS_OCIC) ? " OCIC" : "",
390 (temp & RH_PS_PSSC) ? " PSSC" : "",
391 (temp & RH_PS_PESC) ? " PESC" : "",
392 (temp & RH_PS_CSC) ? " CSC" : "",
393
394 (temp & RH_PS_LSDA) ? " LSDA" : "",
395 (temp & RH_PS_PPS) ? " PPS" : "",
396 (temp & RH_PS_PRS) ? " PRS" : "",
397 (temp & RH_PS_POCI) ? " POCI" : "",
398 (temp & RH_PS_PSS) ? " PSS" : "",
399
400 (temp & RH_PS_PES) ? " PES" : "",
401 (temp & RH_PS_CCS) ? " CCS" : ""
402 );
403 }
404}
405
Remy Bohmer172a1132008-09-16 14:55:43 +0200406static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200407{
Remy Bohmer172a1132008-09-16 14:55:43 +0200408 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200409
410 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200411 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200412 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200413 ep_print_int_eds(controller, "hcca");
414 dbg("hcca frame #%04x", controller->hcca->frame_no);
415 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100416}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200417#endif /* DEBUG */
418
419/*-------------------------------------------------------------------------*
420 * Interface functions (URB)
421 *-------------------------------------------------------------------------*/
422
423/* get a transfer request */
424
Zhang Wei8d15efa2007-06-06 10:08:14 +0200425int sohci_submit_job(urb_priv_t *urb, struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200426{
427 ohci_t *ohci;
Remy Bohmer172a1132008-09-16 14:55:43 +0200428 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200429 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200430 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200431 struct usb_device *dev = urb->dev;
432 unsigned long pipe = urb->pipe;
433 void *buffer = urb->transfer_buffer;
434 int transfer_len = urb->transfer_buffer_length;
435 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200436
437 ohci = &gohci;
438
439 /* when controller's hung, permit only roothub cleanup attempts
440 * such as powering down ports */
441 if (ohci->disabled) {
442 err("sohci_submit_job: EPIPE");
443 return -1;
444 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200445
Remy Bohmer172a1132008-09-16 14:55:43 +0200446 /* we're about to begin a new transaction here so mark the
447 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200448 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200449
450 /* every endpoint has a ed, locate and fill it */
Remy Bohmer172a1132008-09-16 14:55:43 +0200451 ed = ep_add_ed(dev, pipe, interval, 1);
452 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200453 err("sohci_submit_job: ENOMEM");
454 return -1;
455 }
456
457 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200458 switch (usb_pipetype(pipe)) {
459 case PIPE_BULK: /* one TD for every 4096 Byte */
460 size = (transfer_len - 1) / 4096 + 1;
461 break;
462 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
463 size = (transfer_len == 0)? 2:
464 (transfer_len - 1) / 4096 + 3;
465 break;
466 case PIPE_INTERRUPT: /* 1 TD */
467 size = 1;
468 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200469 }
470
Zhang Wei8d15efa2007-06-06 10:08:14 +0200471 ed->purb = urb;
472
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200473 if (size >= (N_URB_TD - 1)) {
474 err("need %d TDs, only have %d", size, N_URB_TD);
475 return -1;
476 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200477 purb_priv->pipe = pipe;
478
479 /* fill the private part of the URB */
480 purb_priv->length = size;
481 purb_priv->ed = ed;
482 purb_priv->actual_length = 0;
483
484 /* allocate the TDs */
485 /* note that td[0] was allocated in ep_add_ed */
486 for (i = 0; i < size; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200487 purb_priv->td[i] = td_alloc(dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200488 if (!purb_priv->td[i]) {
489 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200490 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200491 err("sohci_submit_job: ENOMEM");
492 return -1;
493 }
494 }
495
496 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200497 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200498 err("sohci_submit_job: EINVAL");
499 return -1;
500 }
501
502 /* link the ed into a chain if is not already */
503 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200504 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200505
506 /* fill the TDs and link it to the ed */
Remy Bohmer172a1132008-09-16 14:55:43 +0200507 td_submit_job(dev, pipe, buffer, transfer_len,
508 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200509
510 return 0;
511}
512
Zhang Wei8d15efa2007-06-06 10:08:14 +0200513static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
514{
515 struct ohci_regs *regs = hc->regs;
516
Remy Bohmer172a1132008-09-16 14:55:43 +0200517 switch (usb_pipetype(urb->pipe)) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200518 case PIPE_INTERRUPT:
519 /* implicitly requeued */
520 if (urb->dev->irq_handle &&
521 (urb->dev->irq_act_len = urb->actual_length)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200522 writel(OHCI_INTR_WDH, &regs->intrenable);
523 readl(&regs->intrenable); /* PCI posting flush */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200524 urb->dev->irq_handle(urb->dev);
Remy Bohmer172a1132008-09-16 14:55:43 +0200525 writel(OHCI_INTR_WDH, &regs->intrdisable);
526 readl(&regs->intrdisable); /* PCI posting flush */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200527 }
528 urb->actual_length = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200529 td_submit_job(
Zhang Wei8d15efa2007-06-06 10:08:14 +0200530 urb->dev,
531 urb->pipe,
532 urb->transfer_buffer,
533 urb->transfer_buffer_length,
534 NULL,
535 urb,
536 urb->interval);
537 break;
538 case PIPE_CONTROL:
539 case PIPE_BULK:
540 break;
541 default:
542 return 0;
543 }
544 return 1;
545}
546
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200547/*-------------------------------------------------------------------------*/
548
549#ifdef DEBUG
550/* tell us the current USB frame number */
551
Remy Bohmer172a1132008-09-16 14:55:43 +0200552static int sohci_get_current_frame_number(struct usb_device *usb_dev)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200553{
554 ohci_t *ohci = &gohci;
555
Remy Bohmer172a1132008-09-16 14:55:43 +0200556 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200557}
558#endif
559
560/*-------------------------------------------------------------------------*
561 * ED handling functions
562 *-------------------------------------------------------------------------*/
563
Zhang Wei8d15efa2007-06-06 10:08:14 +0200564/* search for the right branch to insert an interrupt ed into the int tree
565 * do some load ballancing;
566 * returns the branch and
567 * sets the interval to interval = 2^integer (ld (interval)) */
568
Remy Bohmer172a1132008-09-16 14:55:43 +0200569static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200570{
571 int i, branch = 0;
572
573 /* search for the least loaded interrupt endpoint
574 * branch of all 32 branches
575 */
576 for (i = 0; i < 32; i++)
577 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
578 branch = i;
579
580 branch = branch % interval;
581 for (i = branch; i < 32; i += interval)
582 ohci->ohci_int_load [i] += load;
583
584 return branch;
585}
586
587/*-------------------------------------------------------------------------*/
588
589/* 2^int( ld (inter)) */
590
Remy Bohmer172a1132008-09-16 14:55:43 +0200591static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200592{
593 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200594 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200595 return 1 << i;
596}
597
598/*-------------------------------------------------------------------------*/
599
600/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200601 * in order to process it sequentially the indexes of the branches have to
602 * be mapped the mapping reverses the bits of a word of num_bits length */
603static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200604{
605 int i, wout = 0;
606
607 for (i = 0; i < num_bits; i++)
608 wout |= (((word >> i) & 1) << (num_bits - i - 1));
609 return wout;
610}
611
612/*-------------------------------------------------------------------------*
613 * ED handling functions
614 *-------------------------------------------------------------------------*/
615
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200616/* link an ed into one of the HC chains */
617
Remy Bohmer172a1132008-09-16 14:55:43 +0200618static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200619{
620 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200621 int int_branch;
622 int i;
623 int inter;
624 int interval;
625 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200626 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200627
628 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200629 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200630
631 switch (ed->type) {
632 case PIPE_CONTROL:
633 ed->hwNextED = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200634 if (ohci->ed_controltail == NULL)
635 writel(ed, &ohci->regs->ed_controlhead);
636 else
637 ohci->ed_controltail->hwNextED =
638 m32_swap((unsigned long)ed);
639
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200640 ed->ed_prev = ohci->ed_controltail;
641 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
642 !ohci->ed_rm_list[1] && !ohci->sleeping) {
643 ohci->hc_control |= OHCI_CTRL_CLE;
Remy Bohmer172a1132008-09-16 14:55:43 +0200644 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200645 }
646 ohci->ed_controltail = edi;
647 break;
648
649 case PIPE_BULK:
650 ed->hwNextED = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200651 if (ohci->ed_bulktail == NULL)
652 writel(ed, &ohci->regs->ed_bulkhead);
653 else
654 ohci->ed_bulktail->hwNextED =
655 m32_swap((unsigned long)ed);
656
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200657 ed->ed_prev = ohci->ed_bulktail;
658 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
659 !ohci->ed_rm_list[1] && !ohci->sleeping) {
660 ohci->hc_control |= OHCI_CTRL_BLE;
Remy Bohmer172a1132008-09-16 14:55:43 +0200661 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200662 }
663 ohci->ed_bulktail = edi;
664 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200665
666 case PIPE_INTERRUPT:
667 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200668 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200669 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200670 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200671 ed->int_branch = int_branch;
672
Remy Bohmer172a1132008-09-16 14:55:43 +0200673 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200674 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200675 for (ed_p = &(ohci->hcca->int_table[\
676 ep_rev(5, i) + int_branch]);
677 (*ed_p != 0) &&
678 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200679 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200680 inter = ep_rev(6,
681 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200682 ed->hwNextED = *ed_p;
Martin Krause8a588192007-08-21 12:40:34 +0200683 *ed_p = m32_swap((unsigned long)ed);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200684 }
685 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200686 }
687 return 0;
688}
689
690/*-------------------------------------------------------------------------*/
691
Zhang Wei8d15efa2007-06-06 10:08:14 +0200692/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200693static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
694 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200695{
696 for (; index < NUM_INTS; index += period) {
697 __u32 *ed_p = &ohci->hcca->int_table [index];
698
699 /* ED might have been unlinked through another path */
700 while (*ed_p != 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200701 if (((struct ed *)
702 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200703 *ed_p = ed->hwNextED;
704 break;
705 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200706 ed_p = &(((struct ed *)
707 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200708 }
709 }
710}
711
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200712/* unlink an ed from one of the HC chains.
713 * just the link to the ed is unlinked.
714 * the link from the ed still points to another operational ed or 0
715 * so the HC can eventually finish the processing of the unlinked ed */
716
Remy Bohmer172a1132008-09-16 14:55:43 +0200717static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200718{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100719 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200720 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100721
Remy Bohmer172a1132008-09-16 14:55:43 +0200722 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200723
724 switch (ed->type) {
725 case PIPE_CONTROL:
726 if (ed->ed_prev == NULL) {
727 if (!ed->hwNextED) {
728 ohci->hc_control &= ~OHCI_CTRL_CLE;
Remy Bohmer172a1132008-09-16 14:55:43 +0200729 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200730 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200731 writel(m32_swap(*((__u32 *)&ed->hwNextED)),
732 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200733 } else {
734 ed->ed_prev->hwNextED = ed->hwNextED;
735 }
736 if (ohci->ed_controltail == ed) {
737 ohci->ed_controltail = ed->ed_prev;
738 } else {
Remy Bohmer172a1132008-09-16 14:55:43 +0200739 ((ed_t *)m32_swap(
740 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200741 }
742 break;
743
744 case PIPE_BULK:
745 if (ed->ed_prev == NULL) {
746 if (!ed->hwNextED) {
747 ohci->hc_control &= ~OHCI_CTRL_BLE;
Remy Bohmer172a1132008-09-16 14:55:43 +0200748 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200749 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200750 writel(m32_swap(*((__u32 *)&ed->hwNextED)),
751 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200752 } else {
753 ed->ed_prev->hwNextED = ed->hwNextED;
754 }
755 if (ohci->ed_bulktail == ed) {
756 ohci->ed_bulktail = ed->ed_prev;
757 } else {
Remy Bohmer172a1132008-09-16 14:55:43 +0200758 ((ed_t *)m32_swap(
759 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200760 }
761 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200762
763 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200764 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200765 for (i = ed->int_branch; i < 32; i += ed->int_interval)
766 ohci->ohci_int_load[i] -= ed->int_load;
767 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200768 }
769 ed->state = ED_UNLINK;
770 return 0;
771}
772
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200773/*-------------------------------------------------------------------------*/
774
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200775/* add/reinit an endpoint; this should be done once at the
776 * usb_set_configuration command, but the USB stack is a little bit
777 * stateless so we do it at every transaction if the state of the ed
778 * is ED_NEW then a dummy td is added and the state is changed to
779 * ED_UNLINK in all other cases the state is left unchanged the ed
780 * info fields are setted anyway even though most of them should not
781 * change
782 */
Remy Bohmer172a1132008-09-16 14:55:43 +0200783static ed_t *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe,
784 int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200785{
786 td_t *td;
787 ed_t *ed_ret;
788 volatile ed_t *ed;
789
Remy Bohmer172a1132008-09-16 14:55:43 +0200790 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
791 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200792
793 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
794 err("ep_add_ed: pending delete");
795 /* pending delete request */
796 return NULL;
797 }
798
799 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200800 /* dummy td; end of td list for ed */
Remy Bohmer172a1132008-09-16 14:55:43 +0200801 td = td_alloc(usb_dev);
802 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200803 ed->hwHeadP = ed->hwTailP;
804 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200805 ed->type = usb_pipetype(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200806 ohci_dev.ed_cnt++;
807 }
808
Remy Bohmer172a1132008-09-16 14:55:43 +0200809 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
810 | usb_pipeendpoint(pipe) << 7
811 | (usb_pipeisoc(pipe)? 0x8000: 0)
812 | (usb_pipecontrol(pipe)? 0: \
813 (usb_pipeout(pipe)? 0x800: 0x1000))
814 | usb_pipeslow(pipe) << 13
815 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200816
Zhang Wei8d15efa2007-06-06 10:08:14 +0200817 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
818 ed->int_period = interval;
819 ed->int_load = load;
820 }
821
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200822 return ed_ret;
823}
824
825/*-------------------------------------------------------------------------*
826 * TD handling functions
827 *-------------------------------------------------------------------------*/
828
829/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
830
Remy Bohmer172a1132008-09-16 14:55:43 +0200831static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200832 void *data, int len,
833 struct usb_device *dev, int index, urb_priv_t *urb_priv)
834{
835 volatile td_t *td, *td_pt;
836#ifdef OHCI_FILL_TRACE
837 int i;
838#endif
839
840 if (index > urb_priv->length) {
841 err("index > length");
842 return;
843 }
844 /* use this td as the next dummy */
845 td_pt = urb_priv->td [index];
846 td_pt->hwNextTD = 0;
847
848 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200849 td = urb_priv->td [index] =
850 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200851
852 td->ed = urb_priv->ed;
853 td->next_dl_td = NULL;
854 td->index = index;
855 td->data = (__u32)data;
856#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200857 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200858 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200859 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200860 printf("\n");
861 }
862#endif
863 if (!len)
864 data = 0;
865
Remy Bohmer172a1132008-09-16 14:55:43 +0200866 td->hwINFO = m32_swap(info);
867 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200868 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200869 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200870 else
871 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200872
873 td->hwNextTD = m32_swap((unsigned long)td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200874
875 /* append to queue */
876 td->ed->hwTailP = td->hwNextTD;
877}
878
879/*-------------------------------------------------------------------------*/
880
881/* prepare all TDs of a transfer */
882
Remy Bohmer172a1132008-09-16 14:55:43 +0200883static void td_submit_job(struct usb_device *dev, unsigned long pipe,
884 void *buffer, int transfer_len,
885 struct devrequest *setup, urb_priv_t *urb,
886 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200887{
888 ohci_t *ohci = &gohci;
889 int data_len = transfer_len;
890 void *data;
891 int cnt = 0;
892 __u32 info = 0;
893 unsigned int toggle = 0;
894
Remy Bohmer172a1132008-09-16 14:55:43 +0200895 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
896 * bits for reseting */
897 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200898 toggle = TD_T_TOGGLE;
899 } else {
900 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200901 usb_settoggle(dev, usb_pipeendpoint(pipe),
902 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200903 }
904 urb->td_cnt = 0;
905 if (data_len)
906 data = buffer;
907 else
908 data = 0;
909
Remy Bohmer172a1132008-09-16 14:55:43 +0200910 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200911 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200912 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200913 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200914 while (data_len > 4096) {
915 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
916 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200917 data += 4096; data_len -= 4096; cnt++;
918 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200919 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200920 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200921 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
922 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200923 cnt++;
924
Remy Bohmer172a1132008-09-16 14:55:43 +0200925 if (!ohci->sleeping) {
926 /* start bulk list */
927 writel(OHCI_BLF, &ohci->regs->cmdstatus);
928 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200929 break;
930
931 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +0200932 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200933 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200934 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
935
936 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200937 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200938 info = usb_pipeout(pipe)?
939 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
940 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200941 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +0200942 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200943 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200944
945 /* Status phase */
946 info = usb_pipeout(pipe)?
947 TD_CC | TD_DP_IN | TD_T_DATA1:
948 TD_CC | TD_DP_OUT | TD_T_DATA1;
949 td_fill(ohci, info, data, 0, dev, cnt++, urb);
950
951 if (!ohci->sleeping) {
952 /* start Control list */
953 writel(OHCI_CLF, &ohci->regs->cmdstatus);
954 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200955 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200956
957 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200958 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +0200959 TD_CC | TD_DP_OUT | toggle:
960 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +0200961 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200962 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200963 }
964 if (urb->length != cnt)
965 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
966}
967
968/*-------------------------------------------------------------------------*
969 * Done List handling functions
970 *-------------------------------------------------------------------------*/
971
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200972/* calculate the transfer length and update the urb */
973
Remy Bohmer172a1132008-09-16 14:55:43 +0200974static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200975{
976 __u32 tdINFO, tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200977 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200978
Remy Bohmer172a1132008-09-16 14:55:43 +0200979 tdINFO = m32_swap(td->hwINFO);
980 tdBE = m32_swap(td->hwBE);
981 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200982
Remy Bohmer47e72082008-10-10 10:23:21 +0200983 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200984 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
985 if (tdBE != 0) {
986 if (td->hwCBP == 0)
987 lurb_priv->actual_length += tdBE - td->data + 1;
988 else
989 lurb_priv->actual_length += tdCBP - td->data;
990 }
991 }
992}
993
994/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +0200995static void check_status(td_t *td_list)
996{
997 urb_priv_t *lurb_priv = td_list->ed->purb;
998 int urb_len = lurb_priv->length;
999 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1000 int cc;
1001
1002 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1003 if (cc) {
1004 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1005
1006 if (*phwHeadP & m32_swap(0x1)) {
1007 if (lurb_priv &&
1008 ((td_list->index + 1) < urb_len)) {
1009 *phwHeadP =
1010 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1011 m32_swap(0xfffffff0)) |
1012 (*phwHeadP & m32_swap(0x2));
1013
1014 lurb_priv->td_cnt += urb_len -
1015 td_list->index - 1;
1016 } else
1017 *phwHeadP &= m32_swap(0xfffffff2);
1018 }
1019#ifdef CONFIG_MPC5200
1020 td_list->hwNextTD = 0;
1021#endif
1022 }
1023}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001024
1025/* replies to the request have to be on a FIFO basis so
1026 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +02001027static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001028{
1029 __u32 td_list_hc;
1030 td_t *td_rev = NULL;
1031 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001032
Remy Bohmer172a1132008-09-16 14:55:43 +02001033 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001034 ohci->hcca->done_head = 0;
1035
1036 while (td_list_hc) {
1037 td_list = (td_t *)td_list_hc;
Remy Bohmer172a1132008-09-16 14:55:43 +02001038 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001039 td_list->next_dl_td = td_rev;
1040 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001041 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001042 }
1043 return td_list;
1044}
1045
1046/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001047/*-------------------------------------------------------------------------*/
1048
1049static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1050{
1051 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1052 urb->finished = sohci_return_job(ohci, urb);
1053 else
1054 dbg("finish_urb: strange.., ED state %x, \n", status);
1055}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001056
Remy Bohmer172a1132008-09-16 14:55:43 +02001057/*
1058 * Used to take back a TD from the host controller. This would normally be
1059 * called from within dl_done_list, however it may be called directly if the
1060 * HC no longer sees the TD and it has not appeared on the donelist (after
1061 * two frames). This bug has been observed on ZF Micro systems.
1062 */
1063static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001064{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001065 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001066 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001067 int stat = 0;
1068 /* urb_t *urb; */
1069 urb_priv_t *lurb_priv;
1070 __u32 tdINFO, edHeadP, edTailP;
1071
Remy Bohmer172a1132008-09-16 14:55:43 +02001072 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001073
Remy Bohmer172a1132008-09-16 14:55:43 +02001074 ed = td_list->ed;
1075 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001076
Remy Bohmer172a1132008-09-16 14:55:43 +02001077 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001078
Remy Bohmer172a1132008-09-16 14:55:43 +02001079 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001080
Remy Bohmer172a1132008-09-16 14:55:43 +02001081 /* error code of transfer */
1082 cc = TD_CC_GET(tdINFO);
1083 if (cc) {
1084 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1085 stat = cc_to_error[cc];
1086 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001087
Remy Bohmer172a1132008-09-16 14:55:43 +02001088 /* see if this done list makes for all TD's of current URB,
1089 * and mark the URB finished if so */
1090 if (lurb_priv->td_cnt == lurb_priv->length)
1091 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001092
Remy Bohmer172a1132008-09-16 14:55:43 +02001093 dbg("dl_done_list: processing TD %x, len %x\n",
1094 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001095
Remy Bohmer47e72082008-10-10 10:23:21 +02001096 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001097 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1098 edTailP = m32_swap(ed->hwTailP);
1099
1100 /* unlink eds if they are not busy */
1101 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1102 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001103 }
1104 return stat;
1105}
1106
Remy Bohmer172a1132008-09-16 14:55:43 +02001107static int dl_done_list(ohci_t *ohci)
1108{
1109 int stat = 0;
1110 td_t *td_list = dl_reverse_done_list(ohci);
1111
1112 while (td_list) {
1113 td_t *td_next = td_list->next_dl_td;
1114 stat = takeback_td(ohci, td_list);
1115 td_list = td_next;
1116 }
1117 return stat;
1118}
1119
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001120/*-------------------------------------------------------------------------*
1121 * Virtual Root Hub
1122 *-------------------------------------------------------------------------*/
1123
1124/* Device descriptor */
1125static __u8 root_hub_dev_des[] =
1126{
1127 0x12, /* __u8 bLength; */
1128 0x01, /* __u8 bDescriptorType; Device */
1129 0x10, /* __u16 bcdUSB; v1.1 */
1130 0x01,
1131 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1132 0x00, /* __u8 bDeviceSubClass; */
1133 0x00, /* __u8 bDeviceProtocol; */
1134 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1135 0x00, /* __u16 idVendor; */
1136 0x00,
1137 0x00, /* __u16 idProduct; */
1138 0x00,
1139 0x00, /* __u16 bcdDevice; */
1140 0x00,
1141 0x00, /* __u8 iManufacturer; */
1142 0x01, /* __u8 iProduct; */
1143 0x00, /* __u8 iSerialNumber; */
1144 0x01 /* __u8 bNumConfigurations; */
1145};
1146
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001147/* Configuration descriptor */
1148static __u8 root_hub_config_des[] =
1149{
1150 0x09, /* __u8 bLength; */
1151 0x02, /* __u8 bDescriptorType; Configuration */
1152 0x19, /* __u16 wTotalLength; */
1153 0x00,
1154 0x01, /* __u8 bNumInterfaces; */
1155 0x01, /* __u8 bConfigurationValue; */
1156 0x00, /* __u8 iConfiguration; */
1157 0x40, /* __u8 bmAttributes;
Remy Bohmer172a1132008-09-16 14:55:43 +02001158 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001159 0x00, /* __u8 MaxPower; */
1160
1161 /* interface */
1162 0x09, /* __u8 if_bLength; */
1163 0x04, /* __u8 if_bDescriptorType; Interface */
1164 0x00, /* __u8 if_bInterfaceNumber; */
1165 0x00, /* __u8 if_bAlternateSetting; */
1166 0x01, /* __u8 if_bNumEndpoints; */
1167 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1168 0x00, /* __u8 if_bInterfaceSubClass; */
1169 0x00, /* __u8 if_bInterfaceProtocol; */
1170 0x00, /* __u8 if_iInterface; */
1171
1172 /* endpoint */
1173 0x07, /* __u8 ep_bLength; */
1174 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1175 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1176 0x03, /* __u8 ep_bmAttributes; Interrupt */
1177 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1178 0x00,
1179 0xff /* __u8 ep_bInterval; 255 ms */
1180};
1181
1182static unsigned char root_hub_str_index0[] =
1183{
1184 0x04, /* __u8 bLength; */
1185 0x03, /* __u8 bDescriptorType; String-descriptor */
1186 0x09, /* __u8 lang ID */
1187 0x04, /* __u8 lang ID */
1188};
1189
1190static unsigned char root_hub_str_index1[] =
1191{
1192 28, /* __u8 bLength; */
1193 0x03, /* __u8 bDescriptorType; String-descriptor */
1194 'O', /* __u8 Unicode */
1195 0, /* __u8 Unicode */
1196 'H', /* __u8 Unicode */
1197 0, /* __u8 Unicode */
1198 'C', /* __u8 Unicode */
1199 0, /* __u8 Unicode */
1200 'I', /* __u8 Unicode */
1201 0, /* __u8 Unicode */
1202 ' ', /* __u8 Unicode */
1203 0, /* __u8 Unicode */
1204 'R', /* __u8 Unicode */
1205 0, /* __u8 Unicode */
1206 'o', /* __u8 Unicode */
1207 0, /* __u8 Unicode */
1208 'o', /* __u8 Unicode */
1209 0, /* __u8 Unicode */
1210 't', /* __u8 Unicode */
1211 0, /* __u8 Unicode */
1212 ' ', /* __u8 Unicode */
1213 0, /* __u8 Unicode */
1214 'H', /* __u8 Unicode */
1215 0, /* __u8 Unicode */
1216 'u', /* __u8 Unicode */
1217 0, /* __u8 Unicode */
1218 'b', /* __u8 Unicode */
1219 0, /* __u8 Unicode */
1220};
1221
1222/* Hub class-specific descriptor is constructed dynamically */
1223
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001224/*-------------------------------------------------------------------------*/
1225
1226#define OK(x) len = (x); break
1227#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001228#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); writel((x), \
1229 &gohci.regs->roothub.status); }
1230#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1231 (x)); writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001232#else
1233#define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
Remy Bohmer172a1132008-09-16 14:55:43 +02001234#define WR_RH_PORTSTAT(x) writel((x), \
1235 &gohci.regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001236#endif
1237#define RD_RH_STAT roothub_status(&gohci)
Remy Bohmer172a1132008-09-16 14:55:43 +02001238#define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001239
1240/* request to virtual root hub */
1241
1242int rh_check_port_status(ohci_t *controller)
1243{
1244 __u32 temp, ndp, i;
1245 int res;
1246
1247 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001248 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001249 ndp = (temp & RH_A_NDP);
1250#ifdef CONFIG_AT91C_PQFP_UHPBUG
1251 ndp = (ndp == 2) ? 1:0;
1252#endif
1253 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001254 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001255 /* check for a device disconnect */
1256 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1257 (RH_PS_PESC | RH_PS_CSC)) &&
1258 ((temp & RH_PS_CCS) == 0)) {
1259 res = i;
1260 break;
1261 }
1262 }
1263 return res;
1264}
1265
1266static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
1267 void *buffer, int transfer_len, struct devrequest *cmd)
1268{
Remy Bohmer172a1132008-09-16 14:55:43 +02001269 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001270 int leni = transfer_len;
1271 int len = 0;
1272 int stat = 0;
1273 __u32 datab[4];
1274 __u8 *data_buf = (__u8 *)datab;
1275 __u16 bmRType_bReq;
1276 __u16 wValue;
1277 __u16 wIndex;
1278 __u16 wLength;
1279
1280#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001281pkt_print(NULL, dev, pipe, buffer, transfer_len,
1282 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001283#else
1284 wait_ms(1);
1285#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001286 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001287 info("Root-Hub submit IRQ: NOT implemented");
1288 return 0;
1289 }
1290
1291 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001292 wValue = le16_to_cpu(cmd->value);
1293 wIndex = le16_to_cpu(cmd->index);
1294 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001295
1296 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1297 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1298
1299 switch (bmRType_bReq) {
1300 /* Request Destination:
1301 without flags: Device,
1302 RH_INTERFACE: interface,
1303 RH_ENDPOINT: endpoint,
1304 RH_CLASS means HUB here,
1305 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1306 */
1307
1308 case RH_GET_STATUS:
Remy Bohmer172a1132008-09-16 14:55:43 +02001309 *(__u16 *) data_buf = cpu_to_le16(1);
1310 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001311 case RH_GET_STATUS | RH_INTERFACE:
Remy Bohmer172a1132008-09-16 14:55:43 +02001312 *(__u16 *) data_buf = cpu_to_le16(0);
1313 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001314 case RH_GET_STATUS | RH_ENDPOINT:
Remy Bohmer172a1132008-09-16 14:55:43 +02001315 *(__u16 *) data_buf = cpu_to_le16(0);
1316 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001317 case RH_GET_STATUS | RH_CLASS:
Remy Bohmer172a1132008-09-16 14:55:43 +02001318 *(__u32 *) data_buf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001319 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001320 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001321 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Remy Bohmer172a1132008-09-16 14:55:43 +02001322 *(__u32 *) data_buf = cpu_to_le32(RD_RH_PORTSTAT);
1323 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001324
1325 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1326 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001327 case (RH_ENDPOINT_STALL):
1328 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001329 }
1330 break;
1331
1332 case RH_CLEAR_FEATURE | RH_CLASS:
1333 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001334 case RH_C_HUB_LOCAL_POWER:
1335 OK(0);
1336 case (RH_C_HUB_OVER_CURRENT):
1337 WR_RH_STAT(RH_HS_OCIC);
1338 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001339 }
1340 break;
1341
1342 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1343 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001344 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1345 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1346 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1347 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1348 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1349 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1350 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1351 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001352 }
1353 break;
1354
1355 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1356 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001357 case (RH_PORT_SUSPEND):
1358 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1359 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1360 if (RD_RH_PORTSTAT & RH_PS_CCS)
1361 WR_RH_PORTSTAT(RH_PS_PRS);
1362 OK(0);
1363 case (RH_PORT_POWER):
1364 WR_RH_PORTSTAT(RH_PS_PPS);
1365 wait_ms(100);
1366 OK(0);
1367 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1368 if (RD_RH_PORTSTAT & RH_PS_CCS)
1369 WR_RH_PORTSTAT(RH_PS_PES);
1370 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001371 }
1372 break;
1373
Remy Bohmer172a1132008-09-16 14:55:43 +02001374 case RH_SET_ADDRESS:
1375 gohci.rh.devnum = wValue;
1376 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001377
1378 case RH_GET_DESCRIPTOR:
1379 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001380 case (0x01): /* device descriptor */
1381 len = min_t(unsigned int,
1382 leni,
1383 min_t(unsigned int,
1384 sizeof(root_hub_dev_des),
1385 wLength));
1386 data_buf = root_hub_dev_des; OK(len);
1387 case (0x02): /* configuration descriptor */
1388 len = min_t(unsigned int,
1389 leni,
1390 min_t(unsigned int,
1391 sizeof(root_hub_config_des),
1392 wLength));
1393 data_buf = root_hub_config_des; OK(len);
1394 case (0x03): /* string descriptors */
1395 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001396 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001397 leni,
1398 min_t(unsigned int,
1399 sizeof(root_hub_str_index0),
1400 wLength));
1401 data_buf = root_hub_str_index0;
1402 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001403 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001404 if (wValue == 0x0301) {
1405 len = min_t(unsigned int,
1406 leni,
1407 min_t(unsigned int,
1408 sizeof(root_hub_str_index1),
1409 wLength));
1410 data_buf = root_hub_str_index1;
1411 OK(len);
1412 }
1413 default:
1414 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001415 }
1416 break;
1417
1418 case RH_GET_DESCRIPTOR | RH_CLASS:
1419 {
Remy Bohmer172a1132008-09-16 14:55:43 +02001420 __u32 temp = roothub_a(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001421
1422 data_buf [0] = 9; /* min length; */
1423 data_buf [1] = 0x29;
1424 data_buf [2] = temp & RH_A_NDP;
1425#ifdef CONFIG_AT91C_PQFP_UHPBUG
1426 data_buf [2] = (data_buf [2] == 2) ? 1:0;
1427#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001428 data_buf [3] = 0;
1429 if (temp & RH_A_PSM) /* per-port power switching? */
1430 data_buf [3] |= 0x1;
1431 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1432 data_buf [3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001433 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001434 data_buf [3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001435
1436 /* corresponds to data_buf[4-7] */
1437 datab [1] = 0;
1438 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
Remy Bohmer172a1132008-09-16 14:55:43 +02001439 temp = roothub_b(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001440 data_buf [7] = temp & RH_B_DR;
1441 if (data_buf [2] < 7) {
1442 data_buf [8] = 0xff;
1443 } else {
1444 data_buf [0] += 2;
1445 data_buf [8] = (temp & RH_B_DR) >> 8;
1446 data_buf [10] = data_buf [9] = 0xff;
1447 }
1448
1449 len = min_t(unsigned int, leni,
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001450 min_t(unsigned int, data_buf [0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001451 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001452 }
1453
Remy Bohmer172a1132008-09-16 14:55:43 +02001454 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001455
Remy Bohmer172a1132008-09-16 14:55:43 +02001456 case RH_SET_CONFIGURATION: WR_RH_STAT(0x10000); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001457
1458 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001459 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001460 stat = USB_ST_STALLED;
1461 }
1462
1463#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001464 ohci_dump_roothub(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001465#else
1466 wait_ms(1);
1467#endif
1468
1469 len = min_t(int, len, leni);
1470 if (data != data_buf)
Remy Bohmer172a1132008-09-16 14:55:43 +02001471 memcpy(data, data_buf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001472 dev->act_len = len;
1473 dev->status = stat;
1474
1475#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001476 pkt_print(NULL, dev, pipe, buffer,
1477 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001478#else
1479 wait_ms(1);
1480#endif
1481
1482 return stat;
1483}
1484
1485/*-------------------------------------------------------------------------*/
1486
1487/* common code for handling submit messages - used for all but root hub */
1488/* accesses. */
1489int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1490 int transfer_len, struct devrequest *setup, int interval)
1491{
1492 int stat = 0;
1493 int maxsize = usb_maxpacket(dev, pipe);
1494 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001495 urb_priv_t *urb;
1496
1497 urb = malloc(sizeof(urb_priv_t));
1498 memset(urb, 0, sizeof(urb_priv_t));
1499
1500 urb->dev = dev;
1501 urb->pipe = pipe;
1502 urb->transfer_buffer = buffer;
1503 urb->transfer_buffer_length = transfer_len;
1504 urb->interval = interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001505
1506 /* device pulled? Shortcut the action. */
1507 if (devgone == dev) {
1508 dev->status = USB_ST_CRC_ERR;
1509 return 0;
1510 }
1511
1512#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001513 urb->actual_length = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001514 pkt_print(urb, dev, pipe, buffer, transfer_len,
1515 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001516#else
1517 wait_ms(1);
1518#endif
1519 if (!maxsize) {
1520 err("submit_common_message: pipesize for pipe %lx is zero",
1521 pipe);
1522 return -1;
1523 }
1524
Zhang Wei8d15efa2007-06-06 10:08:14 +02001525 if (sohci_submit_job(urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001526 err("sohci_submit_job failed");
1527 return -1;
1528 }
1529
Markus Klotzbuecherd209de62006-11-27 11:46:46 +01001530#if 0
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001531 wait_ms(10);
1532 /* ohci_dump_status(&gohci); */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +01001533#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001534
1535 /* allow more time for a BULK device to react - some are slow */
1536#define BULK_TO 5000 /* timeout in milliseconds */
Remy Bohmer47e72082008-10-10 10:23:21 +02001537 if (usb_pipebulk(pipe))
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001538 timeout = BULK_TO;
1539 else
1540 timeout = 100;
1541
1542 /* wait for it to complete */
1543 for (;;) {
1544 /* check whether the controller is done */
1545 stat = hc_interrupt();
1546 if (stat < 0) {
1547 stat = USB_ST_CRC_ERR;
1548 break;
1549 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001550
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001551 /* NOTE: since we are not interrupt driven in U-Boot and always
1552 * handle only one URB at a time, we cannot assume the
1553 * transaction finished on the first successful return from
1554 * hc_interrupt().. unless the flag for current URB is set,
1555 * meaning that all TD's to/from device got actually
1556 * transferred and processed. If the current URB is not
1557 * finished we need to re-iterate this loop so as
1558 * hc_interrupt() gets called again as there needs to be some
1559 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001560 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001561 /* 0xff is returned for an SF-interrupt */
1562 break;
1563 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001564
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001565 if (--timeout) {
1566 wait_ms(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001567 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001568 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001569
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001570 } else {
1571 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001572 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001573 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001574 stat = USB_ST_CRC_ERR;
1575 break;
1576 }
1577 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001578
1579 dev->status = stat;
1580 dev->act_len = transfer_len;
1581
1582#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001583 pkt_print(urb, dev, pipe, buffer, transfer_len,
1584 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001585#else
1586 wait_ms(1);
1587#endif
1588
1589 /* free TDs in urb_priv */
Remy Bohmer47e72082008-10-10 10:23:21 +02001590 if (!usb_pipeint(pipe))
Remy Bohmer172a1132008-09-16 14:55:43 +02001591 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001592 return 0;
1593}
1594
1595/* submit routines called from usb.c */
1596int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1597 int transfer_len)
1598{
1599 info("submit_bulk_msg");
1600 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1601}
1602
1603int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1604 int transfer_len, struct devrequest *setup)
1605{
1606 int maxsize = usb_maxpacket(dev, pipe);
1607
1608 info("submit_control_msg");
1609#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001610 pkt_print(NULL, dev, pipe, buffer, transfer_len,
1611 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001612#else
1613 wait_ms(1);
1614#endif
1615 if (!maxsize) {
1616 err("submit_control_message: pipesize for pipe %lx is zero",
1617 pipe);
1618 return -1;
1619 }
1620 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1621 gohci.rh.dev = dev;
1622 /* root hub - redirect */
1623 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1624 setup);
1625 }
1626
1627 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1628}
1629
1630int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1631 int transfer_len, int interval)
1632{
1633 info("submit_int_msg");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001634 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL,
1635 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001636}
1637
1638/*-------------------------------------------------------------------------*
1639 * HC functions
1640 *-------------------------------------------------------------------------*/
1641
1642/* reset the HC and BUS */
1643
Remy Bohmer172a1132008-09-16 14:55:43 +02001644static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001645{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001646#ifdef CONFIG_PCI_EHCI_DEVNO
1647 pci_dev_t pdev;
1648#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001649 int timeout = 30;
1650 int smm_timeout = 50; /* 0,5 sec */
1651
1652 dbg("%s\n", __FUNCTION__);
1653
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001654#ifdef CONFIG_PCI_EHCI_DEVNO
1655 /*
1656 * Some multi-function controllers (e.g. ISP1562) allow root hub
1657 * resetting via EHCI registers only.
1658 */
1659 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1660 if (pdev != -1) {
1661 u32 base;
1662 int timeout = 1000;
1663
1664 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Remy Bohmer172a1132008-09-16 14:55:43 +02001665 writel(readl(base + EHCI_USBCMD_OFF) | EHCI_USBCMD_HCRESET,
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001666 base + EHCI_USBCMD_OFF);
1667
1668 while (readl(base + EHCI_USBCMD_OFF) & EHCI_USBCMD_HCRESET) {
1669 if (timeout-- <= 0) {
1670 printf("USB RootHub reset timed out!");
1671 break;
1672 }
1673 udelay(1);
1674 }
1675 } else
1676 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1677#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02001678 if (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1679 /* SMM owns the HC */
1680 writel(OHCI_OCR, &ohci->regs->cmdstatus);/* request ownership */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001681 info("USB HC TakeOver from SMM");
Remy Bohmer172a1132008-09-16 14:55:43 +02001682 while (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1683 wait_ms(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001684 if (--smm_timeout == 0) {
1685 err("USB HC TakeOver failed!");
1686 return -1;
1687 }
1688 }
1689 }
1690
1691 /* Disable HC interrupts */
Remy Bohmer172a1132008-09-16 14:55:43 +02001692 writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001693
1694 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1695 ohci->slot_name,
1696 readl(&ohci->regs->control));
1697
1698 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001699 ohci->hc_control = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001700 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001701
1702 /* HC Reset requires max 10 us delay */
Remy Bohmer172a1132008-09-16 14:55:43 +02001703 writel(OHCI_HCR, &ohci->regs->cmdstatus);
1704 while ((readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001705 if (--timeout == 0) {
1706 err("USB HC reset timed out!");
1707 return -1;
1708 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001709 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001710 }
1711 return 0;
1712}
1713
1714/*-------------------------------------------------------------------------*/
1715
1716/* Start an OHCI controller, set the BUS operational
1717 * enable interrupts
1718 * connect the virtual root hub */
1719
Remy Bohmer172a1132008-09-16 14:55:43 +02001720static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001721{
1722 __u32 mask;
1723 unsigned int fminterval;
1724
1725 ohci->disabled = 1;
1726
1727 /* Tell the controller where the control and bulk lists are
1728 * The lists are empty now. */
1729
Remy Bohmer172a1132008-09-16 14:55:43 +02001730 writel(0, &ohci->regs->ed_controlhead);
1731 writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001732
Remy Bohmer172a1132008-09-16 14:55:43 +02001733 writel((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001734
1735 fminterval = 0x2edf;
Remy Bohmer172a1132008-09-16 14:55:43 +02001736 writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001737 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Remy Bohmer172a1132008-09-16 14:55:43 +02001738 writel(fminterval, &ohci->regs->fminterval);
1739 writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001740
1741 /* start controller operations */
1742 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1743 ohci->disabled = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001744 writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001745
1746 /* disable all interrupts */
1747 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1748 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1749 OHCI_INTR_OC | OHCI_INTR_MIE);
Remy Bohmer172a1132008-09-16 14:55:43 +02001750 writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001751 /* clear all interrupts */
1752 mask &= ~OHCI_INTR_MIE;
Remy Bohmer172a1132008-09-16 14:55:43 +02001753 writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001754 /* Choose the interrupts we care about now - but w/o MIE */
1755 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Remy Bohmer172a1132008-09-16 14:55:43 +02001756 writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001757
1758#ifdef OHCI_USE_NPS
1759 /* required for AMD-756 and some Mac platforms */
Remy Bohmer172a1132008-09-16 14:55:43 +02001760 writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001761 &ohci->regs->roothub.a);
Remy Bohmer172a1132008-09-16 14:55:43 +02001762 writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001763#endif /* OHCI_USE_NPS */
1764
Remy Bohmer172a1132008-09-16 14:55:43 +02001765#define mdelay(n) ({unsigned long msec = (n); while (msec--) udelay(1000); })
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001766 /* POTPGT delay is bits 24-31, in 2 ms units. */
Remy Bohmer172a1132008-09-16 14:55:43 +02001767 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001768
1769 /* connect the virtual root hub */
1770 ohci->rh.devnum = 0;
1771
1772 return 0;
1773}
1774
1775/*-------------------------------------------------------------------------*/
1776
Zhang Wei8d15efa2007-06-06 10:08:14 +02001777/* Poll USB interrupt. */
1778void usb_event_poll(void)
1779{
1780 hc_interrupt();
1781}
1782
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001783/* an interrupt happens */
1784
Remy Bohmer172a1132008-09-16 14:55:43 +02001785static int hc_interrupt(void)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001786{
1787 ohci_t *ohci = &gohci;
1788 struct ohci_regs *regs = ohci->regs;
1789 int ints;
1790 int stat = -1;
1791
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001792 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001793 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001794 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001795 } else {
1796 ints = readl(&regs->intrstatus);
1797 if (ints == ~(u32)0) {
1798 ohci->disabled++;
1799 err("%s device removed!", ohci->slot_name);
1800 return -1;
1801 } else {
1802 ints &= readl(&regs->intrenable);
1803 if (ints == 0) {
1804 dbg("hc_interrupt: returning..\n");
1805 return 0xff;
1806 }
1807 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001808 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001809
Remy Bohmer172a1132008-09-16 14:55:43 +02001810 /* dbg("Interrupt: %x frame: %x", ints,
1811 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001812
Remy Bohmer172a1132008-09-16 14:55:43 +02001813 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001814 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001815
1816 if (ints & OHCI_INTR_UE) {
1817 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001818 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001819 ohci->slot_name);
1820 /* e.g. due to PCI Master/Target Abort */
1821
1822#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001823 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001824#else
Remy Bohmer172a1132008-09-16 14:55:43 +02001825 wait_ms(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001826#endif
1827 /* FIXME: be optimistic, hope that bug won't repeat often. */
1828 /* Make some non-interrupt context restart the controller. */
1829 /* Count and limit the retries though; either hardware or */
1830 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001831 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001832 return -1;
1833 }
1834
1835 if (ints & OHCI_INTR_WDH) {
1836 wait_ms(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001837 writel(OHCI_INTR_WDH, &regs->intrdisable);
1838 (void)readl(&regs->intrdisable); /* flush */
1839 stat = dl_done_list(&gohci);
1840 writel(OHCI_INTR_WDH, &regs->intrenable);
1841 (void)readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001842 }
1843
1844 if (ints & OHCI_INTR_SO) {
1845 dbg("USB Schedule overrun\n");
Remy Bohmer172a1132008-09-16 14:55:43 +02001846 writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001847 stat = -1;
1848 }
1849
1850 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1851 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001852 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001853 wait_ms(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001854 writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001855 if (ohci->ed_rm_list[frame] != NULL)
Remy Bohmer172a1132008-09-16 14:55:43 +02001856 writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001857 stat = 0xff;
1858 }
1859
Remy Bohmer172a1132008-09-16 14:55:43 +02001860 writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001861 return stat;
1862}
1863
1864/*-------------------------------------------------------------------------*/
1865
1866/*-------------------------------------------------------------------------*/
1867
1868/* De-allocate all resources.. */
1869
Remy Bohmer172a1132008-09-16 14:55:43 +02001870static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001871{
Remy Bohmer172a1132008-09-16 14:55:43 +02001872 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001873
1874 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001875 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001876}
1877
1878/*-------------------------------------------------------------------------*/
1879
1880/*
1881 * low level initalisation routine, called from usb.c
1882 */
1883static char ohci_inited = 0;
1884
1885int usb_lowlevel_init(void)
1886{
Zhang Wei8d15efa2007-06-06 10:08:14 +02001887#ifdef CONFIG_PCI_OHCI
1888 pci_dev_t pdev;
1889#endif
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001890
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001891#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001892 /* cpu dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02001893 if (usb_cpu_init())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001894 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001895#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001896
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001897#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001898 /* board dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02001899 if (usb_board_init())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001900 return -1;
1901#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02001902 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001903
1904 /* align the storage */
1905 if ((__u32)&ghcca[0] & 0xff) {
1906 err("HCCA not aligned!!");
1907 return -1;
1908 }
1909 phcca = &ghcca[0];
1910 info("aligned ghcca %p", phcca);
1911 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1912 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1913 err("EDs not aligned!!");
1914 return -1;
1915 }
1916 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1917 if ((__u32)gtd & 0x7) {
1918 err("TDs not aligned!!");
1919 return -1;
1920 }
1921 ptd = gtd;
1922 gohci.hcca = phcca;
Remy Bohmer172a1132008-09-16 14:55:43 +02001923 memset(phcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001924
1925 gohci.disabled = 1;
1926 gohci.sleeping = 0;
1927 gohci.irq = -1;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001928#ifdef CONFIG_PCI_OHCI
Sergei Poselenov0b4570f2008-05-22 01:15:53 +02001929 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001930
1931 if (pdev != -1) {
1932 u16 vid, did;
1933 u32 base;
1934 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1935 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1936 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1937 vid, did, (pdev >> 16) & 0xff,
1938 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1939 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1940 printf("OHCI regs address 0x%08x\n", base);
1941 gohci.regs = (struct ohci_regs *)base;
1942 } else
1943 return -1;
1944#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001945 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001946#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001947
1948 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001949 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001950
1951 if (hc_reset (&gohci) < 0) {
1952 hc_release_ohci (&gohci);
1953 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001954#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001955 /* board dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001956 usb_board_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001957#endif
1958
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001959#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001960 /* cpu dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001961 usb_cpu_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001962#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001963 return -1;
1964 }
1965
Remy Bohmer172a1132008-09-16 14:55:43 +02001966 if (hc_start(&gohci) < 0) {
1967 err("can't start usb-%s", gohci.slot_name);
1968 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001969 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001970#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001971 /* board dependant cleanup */
1972 usb_board_stop();
1973#endif
1974
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001975#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001976 /* cpu dependant cleanup */
1977 usb_cpu_stop();
1978#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001979 return -1;
1980 }
1981
1982#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001983 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001984#else
1985 wait_ms(1);
1986#endif
1987 ohci_inited = 1;
1988 return 0;
1989}
1990
1991int usb_lowlevel_stop(void)
1992{
1993 /* this gets called really early - before the controller has */
1994 /* even been initialized! */
1995 if (!ohci_inited)
1996 return 0;
1997 /* TODO release any interrupts, etc. */
1998 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02001999 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002000
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002001#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002002 /* board dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002003 if (usb_board_stop())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002004 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002005#endif
2006
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002007#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002008 /* cpu dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002009 if (usb_cpu_stop())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002010 return -1;
2011#endif
Remy Bohmer743dd492008-08-20 11:22:02 +02002012 /* This driver is no longer initialised. It needs a new low-level
2013 * init (board/cpu) before it can be used again. */
2014 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002015 return 0;
2016}