blob: 22c7b1810aea0aa49aaa9d69a1ac557bc34c24cf [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020020 * SPDX-License-Identifier: GPL-2.0+
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020021 */
22/*
23 * IMPORTANT NOTES
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020024 * 1 - Read doc/README.generic_usb_ohci
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020025 * 2 - this driver is intended for use with USB Mass Storage Devices
Zhang Wei8d15efa2007-06-06 10:08:14 +020026 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020027 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020028 * to activate workaround for bug #41 or this driver will NOT work!
29 */
30
31#include <common.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020032#include <asm/byteorder.h>
33
34#if defined(CONFIG_PCI_OHCI)
Zhang Wei8d15efa2007-06-06 10:08:14 +020035# include <pci.h>
Sergei Poselenov0b4570f2008-05-22 01:15:53 +020036#if !defined(CONFIG_PCI_OHCI_DEVNO)
37#define CONFIG_PCI_OHCI_DEVNO 0
38#endif
Markus Klotzbuecheraa219212006-05-30 16:56:14 +020039#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020040
41#include <malloc.h>
42#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020043
44#include "ohci.h"
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020045
Wolfgang Denkdcac38b2007-11-19 12:59:14 +010046#ifdef CONFIG_AT91RM9200
47#include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
48#endif
49
Masahiro Yamada7a5b2922014-11-06 14:59:35 +090050#if defined(CONFIG_CPU_ARM920T) || \
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090051 defined(CONFIG_S3C24X0) || \
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010052 defined(CONFIG_440EP) || \
Zhang Wei8d15efa2007-06-06 10:08:14 +020053 defined(CONFIG_PCI_OHCI) || \
Stefan Roese41e74eb2008-03-05 12:29:32 +010054 defined(CONFIG_MPC5200) || \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020055 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020056# define OHCI_USE_NPS /* force NoPowerSwitching mode */
57#endif
58
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020059#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010060#undef DEBUG
61#undef SHOW_INFO
62#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020063
64/* For initializing controller (mask in an HCFS mode too) */
65#define OHCI_CONTROL_INIT \
66 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
67
Zhang Wei8d15efa2007-06-06 10:08:14 +020068#ifdef CONFIG_PCI_OHCI
69static struct pci_device_id ohci_pci_ids[] = {
70 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saadae3b28422007-09-17 17:04:47 +020071 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew923f43a2008-01-11 20:42:58 -060072 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei8d15efa2007-06-06 10:08:14 +020073 /* Please add supported PCI OHCI controller ids here */
74 {0, 0}
75};
76#endif
77
Yuri Tikhonov11af42c2008-09-04 11:19:05 +020078#ifdef CONFIG_PCI_EHCI_DEVNO
79static struct pci_device_id ehci_pci_ids[] = {
80 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
81 /* Please add supported PCI EHCI controller ids here */
82 {0, 0}
83};
84#endif
85
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020086#ifdef DEBUG
87#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
88#else
Remy Bohmer172a1132008-09-16 14:55:43 +020089#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020090#endif /* DEBUG */
91#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020092#ifdef SHOW_INFO
93#define info(format, arg...) printf("INFO: " format "\n", ## arg)
94#else
Remy Bohmer172a1132008-09-16 14:55:43 +020095#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020096#endif
97
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020098#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020099# define m16_swap(x) cpu_to_be16(x)
100# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +0100101#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200102# define m16_swap(x) cpu_to_le16(x)
103# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200104#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200105
106/* global ohci_t */
107static ohci_t gohci;
108/* this must be aligned to a 256 byte boundary */
109struct ohci_hcca ghcca[1];
110/* a pointer to the aligned storage */
111struct ohci_hcca *phcca;
112/* this allocates EDs for all possible endpoints */
113struct ohci_device ohci_dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200114
Remy Bohmer172a1132008-09-16 14:55:43 +0200115static inline u32 roothub_a(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500116 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200117static inline u32 roothub_b(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500118 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200119static inline u32 roothub_status(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500120 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200121static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500122 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200123
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200124/* forward declaration */
Hans de Goede2a240e72015-05-05 23:56:07 +0200125static int hc_interrupt(ohci_t *ohci);
126static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
127 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200128 struct devrequest *setup, urb_priv_t *urb,
129 int interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200130
131/*-------------------------------------------------------------------------*
132 * URB support functions
133 *-------------------------------------------------------------------------*/
134
135/* free HCD-private data associated with this URB */
136
Remy Bohmer172a1132008-09-16 14:55:43 +0200137static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200138{
139 int i;
140 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200141 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200142
143 last = urb->length - 1;
144 if (last >= 0) {
145 for (i = 0; i <= last; i++) {
146 td = urb->td[i];
147 if (td) {
148 td->usb_dev = NULL;
149 urb->td[i] = NULL;
150 }
151 }
152 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200153 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200154}
155
156/*-------------------------------------------------------------------------*/
157
158#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +0200159static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200160
161/* debug| print the main components of an URB
162 * small: 0) header + data packets 1) just header */
163
Hans de Goede2a240e72015-05-05 23:56:07 +0200164static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer172a1132008-09-16 14:55:43 +0200165 unsigned long pipe, void *buffer, int transfer_len,
166 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200167{
Remy Bohmer172a1132008-09-16 14:55:43 +0200168 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200169 str,
Hans de Goede2a240e72015-05-05 23:56:07 +0200170 sohci_get_current_frame_number(ohci),
Remy Bohmer172a1132008-09-16 14:55:43 +0200171 usb_pipedevice(pipe),
172 usb_pipeendpoint(pipe),
173 usb_pipeout(pipe)? 'O': 'I',
174 usb_pipetype(pipe) < 2 ? \
175 (usb_pipeint(pipe)? "INTR": "ISOC"): \
176 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200177 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200178 transfer_len, dev->status);
179#ifdef OHCI_VERBOSE_DEBUG
180 if (!small) {
181 int i, len;
182
Remy Bohmer172a1132008-09-16 14:55:43 +0200183 if (usb_pipecontrol(pipe)) {
184 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200185 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200186 printf(" %02x", ((__u8 *) setup) [i]);
187 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200188 }
189 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200190 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200191 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200192 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200193 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200194 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200195 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200196 printf(" %02x", ((__u8 *) buffer) [i]);
197 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200198 }
199 }
200#endif
201}
202
Remy Bohmer172a1132008-09-16 14:55:43 +0200203/* just for debugging; prints non-empty branches of the int ed tree
204 * inclusive iso eds */
205void ep_print_int_eds(ohci_t *ohci, char *str)
206{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200207 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200208 __u32 *ed_p;
209 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200210 j = 5;
211 ed_p = &(ohci->hcca->int_table [i]);
212 if (*ed_p == 0)
213 continue;
Remy Bohmer172a1132008-09-16 14:55:43 +0200214 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200215 while (*ed_p != 0 && j--) {
216 ed_t *ed = (ed_t *)m32_swap(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200217 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200218 ed_p = &ed->hwNextED;
219 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200220 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200221 }
222}
223
Remy Bohmer172a1132008-09-16 14:55:43 +0200224static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200225{
Remy Bohmer172a1132008-09-16 14:55:43 +0200226 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200227 label,
228 mask,
229 (mask & OHCI_INTR_MIE) ? " MIE" : "",
230 (mask & OHCI_INTR_OC) ? " OC" : "",
231 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
232 (mask & OHCI_INTR_FNO) ? " FNO" : "",
233 (mask & OHCI_INTR_UE) ? " UE" : "",
234 (mask & OHCI_INTR_RD) ? " RD" : "",
235 (mask & OHCI_INTR_SF) ? " SF" : "",
236 (mask & OHCI_INTR_WDH) ? " WDH" : "",
237 (mask & OHCI_INTR_SO) ? " SO" : ""
238 );
239}
240
Remy Bohmer172a1132008-09-16 14:55:43 +0200241static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200242{
243 ed_t *edp = (ed_t *)value;
244
245 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200246 dbg("%s %08x", label, value);
247 dbg("%08x", edp->hwINFO);
248 dbg("%08x", edp->hwTailP);
249 dbg("%08x", edp->hwHeadP);
250 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200251 }
252}
253
Remy Bohmer172a1132008-09-16 14:55:43 +0200254static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200255{
256 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200257 case OHCI_USB_RESET: return "reset";
258 case OHCI_USB_RESUME: return "resume";
259 case OHCI_USB_OPER: return "operational";
260 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200261 }
262 return "?";
263}
264
265/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200266static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200267{
268 struct ohci_regs *regs = controller->regs;
269 __u32 temp;
270
Becky Bruce4abaacb2010-06-30 13:05:44 -0500271 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200272 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200273 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200274
Becky Bruce4abaacb2010-06-30 13:05:44 -0500275 temp = ohci_readl(&regs->control);
Remy Bohmer172a1132008-09-16 14:55:43 +0200276 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200277 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
278 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
279 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200280 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200281 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
282 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
283 (temp & OHCI_CTRL_IE) ? " IE" : "",
284 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
285 temp & OHCI_CTRL_CBSR
286 );
287
Becky Bruce4abaacb2010-06-30 13:05:44 -0500288 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200289 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200290 (temp & OHCI_SOC) >> 16,
291 (temp & OHCI_OCR) ? " OCR" : "",
292 (temp & OHCI_BLF) ? " BLF" : "",
293 (temp & OHCI_CLF) ? " CLF" : "",
294 (temp & OHCI_HCR) ? " HCR" : ""
295 );
296
Becky Bruce4abaacb2010-06-30 13:05:44 -0500297 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
298 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200299
Becky Bruce4abaacb2010-06-30 13:05:44 -0500300 maybe_print_eds("ed_periodcurrent",
301 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200302
Becky Bruce4abaacb2010-06-30 13:05:44 -0500303 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
304 maybe_print_eds("ed_controlcurrent",
305 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200306
Becky Bruce4abaacb2010-06-30 13:05:44 -0500307 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
308 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200309
Becky Bruce4abaacb2010-06-30 13:05:44 -0500310 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200311}
312
Remy Bohmer172a1132008-09-16 14:55:43 +0200313static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200314{
315 __u32 temp, ndp, i;
316
Remy Bohmer172a1132008-09-16 14:55:43 +0200317 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200318 ndp = (temp & RH_A_NDP);
319#ifdef CONFIG_AT91C_PQFP_UHPBUG
320 ndp = (ndp == 2) ? 1:0;
321#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200322 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200323 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200324 ((temp & RH_A_POTPGT) >> 24) & 0xff,
325 (temp & RH_A_NOCP) ? " NOCP" : "",
326 (temp & RH_A_OCPM) ? " OCPM" : "",
327 (temp & RH_A_DT) ? " DT" : "",
328 (temp & RH_A_NPS) ? " NPS" : "",
329 (temp & RH_A_PSM) ? " PSM" : "",
330 ndp
331 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200332 temp = roothub_b(controller);
333 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200334 temp,
335 (temp & RH_B_PPCM) >> 16,
336 (temp & RH_B_DR)
337 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200338 temp = roothub_status(controller);
339 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200340 temp,
341 (temp & RH_HS_CRWE) ? " CRWE" : "",
342 (temp & RH_HS_OCIC) ? " OCIC" : "",
343 (temp & RH_HS_LPSC) ? " LPSC" : "",
344 (temp & RH_HS_DRWE) ? " DRWE" : "",
345 (temp & RH_HS_OCI) ? " OCI" : "",
346 (temp & RH_HS_LPS) ? " LPS" : ""
347 );
348 }
349
350 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200351 temp = roothub_portstatus(controller, i);
352 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 +0200353 i,
354 temp,
355 (temp & RH_PS_PRSC) ? " PRSC" : "",
356 (temp & RH_PS_OCIC) ? " OCIC" : "",
357 (temp & RH_PS_PSSC) ? " PSSC" : "",
358 (temp & RH_PS_PESC) ? " PESC" : "",
359 (temp & RH_PS_CSC) ? " CSC" : "",
360
361 (temp & RH_PS_LSDA) ? " LSDA" : "",
362 (temp & RH_PS_PPS) ? " PPS" : "",
363 (temp & RH_PS_PRS) ? " PRS" : "",
364 (temp & RH_PS_POCI) ? " POCI" : "",
365 (temp & RH_PS_PSS) ? " PSS" : "",
366
367 (temp & RH_PS_PES) ? " PES" : "",
368 (temp & RH_PS_CCS) ? " CCS" : ""
369 );
370 }
371}
372
Remy Bohmer172a1132008-09-16 14:55:43 +0200373static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200374{
Remy Bohmer172a1132008-09-16 14:55:43 +0200375 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200376
377 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200378 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200379 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200380 ep_print_int_eds(controller, "hcca");
381 dbg("hcca frame #%04x", controller->hcca->frame_no);
382 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100383}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200384#endif /* DEBUG */
385
386/*-------------------------------------------------------------------------*
387 * Interface functions (URB)
388 *-------------------------------------------------------------------------*/
389
390/* get a transfer request */
391
Hans de Goede2a240e72015-05-05 23:56:07 +0200392int sohci_submit_job(ohci_t *ohci, urb_priv_t *urb, struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200393{
Remy Bohmer172a1132008-09-16 14:55:43 +0200394 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200395 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200396 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200397 struct usb_device *dev = urb->dev;
398 unsigned long pipe = urb->pipe;
399 void *buffer = urb->transfer_buffer;
400 int transfer_len = urb->transfer_buffer_length;
401 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200402
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200403 /* when controller's hung, permit only roothub cleanup attempts
404 * such as powering down ports */
405 if (ohci->disabled) {
406 err("sohci_submit_job: EPIPE");
407 return -1;
408 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200409
Remy Bohmer172a1132008-09-16 14:55:43 +0200410 /* we're about to begin a new transaction here so mark the
411 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200412 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200413
414 /* every endpoint has a ed, locate and fill it */
Remy Bohmer172a1132008-09-16 14:55:43 +0200415 ed = ep_add_ed(dev, pipe, interval, 1);
416 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200417 err("sohci_submit_job: ENOMEM");
418 return -1;
419 }
420
421 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200422 switch (usb_pipetype(pipe)) {
423 case PIPE_BULK: /* one TD for every 4096 Byte */
424 size = (transfer_len - 1) / 4096 + 1;
425 break;
426 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
427 size = (transfer_len == 0)? 2:
428 (transfer_len - 1) / 4096 + 3;
429 break;
430 case PIPE_INTERRUPT: /* 1 TD */
431 size = 1;
432 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200433 }
434
Zhang Wei8d15efa2007-06-06 10:08:14 +0200435 ed->purb = urb;
436
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200437 if (size >= (N_URB_TD - 1)) {
438 err("need %d TDs, only have %d", size, N_URB_TD);
439 return -1;
440 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200441 purb_priv->pipe = pipe;
442
443 /* fill the private part of the URB */
444 purb_priv->length = size;
445 purb_priv->ed = ed;
446 purb_priv->actual_length = 0;
447
448 /* allocate the TDs */
449 /* note that td[0] was allocated in ep_add_ed */
450 for (i = 0; i < size; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200451 purb_priv->td[i] = td_alloc(dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200452 if (!purb_priv->td[i]) {
453 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200454 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200455 err("sohci_submit_job: ENOMEM");
456 return -1;
457 }
458 }
459
460 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200461 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200462 err("sohci_submit_job: EINVAL");
463 return -1;
464 }
465
466 /* link the ed into a chain if is not already */
467 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200468 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200469
470 /* fill the TDs and link it to the ed */
Hans de Goede2a240e72015-05-05 23:56:07 +0200471 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200472 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200473
474 return 0;
475}
476
Zhang Wei8d15efa2007-06-06 10:08:14 +0200477static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
478{
479 struct ohci_regs *regs = hc->regs;
480
Remy Bohmer172a1132008-09-16 14:55:43 +0200481 switch (usb_pipetype(urb->pipe)) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200482 case PIPE_INTERRUPT:
483 /* implicitly requeued */
484 if (urb->dev->irq_handle &&
485 (urb->dev->irq_act_len = urb->actual_length)) {
Becky Bruce4abaacb2010-06-30 13:05:44 -0500486 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
487 ohci_readl(&regs->intrenable); /* PCI posting flush */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200488 urb->dev->irq_handle(urb->dev);
Becky Bruce4abaacb2010-06-30 13:05:44 -0500489 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
490 ohci_readl(&regs->intrdisable); /* PCI posting flush */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200491 }
492 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +0200493 td_submit_job( hc,
Zhang Wei8d15efa2007-06-06 10:08:14 +0200494 urb->dev,
495 urb->pipe,
496 urb->transfer_buffer,
497 urb->transfer_buffer_length,
498 NULL,
499 urb,
500 urb->interval);
501 break;
502 case PIPE_CONTROL:
503 case PIPE_BULK:
504 break;
505 default:
506 return 0;
507 }
508 return 1;
509}
510
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200511/*-------------------------------------------------------------------------*/
512
513#ifdef DEBUG
514/* tell us the current USB frame number */
Hans de Goede2a240e72015-05-05 23:56:07 +0200515static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200516{
Remy Bohmer172a1132008-09-16 14:55:43 +0200517 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200518}
519#endif
520
521/*-------------------------------------------------------------------------*
522 * ED handling functions
523 *-------------------------------------------------------------------------*/
524
Zhang Wei8d15efa2007-06-06 10:08:14 +0200525/* search for the right branch to insert an interrupt ed into the int tree
526 * do some load ballancing;
527 * returns the branch and
528 * sets the interval to interval = 2^integer (ld (interval)) */
529
Remy Bohmer172a1132008-09-16 14:55:43 +0200530static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200531{
532 int i, branch = 0;
533
534 /* search for the least loaded interrupt endpoint
535 * branch of all 32 branches
536 */
537 for (i = 0; i < 32; i++)
538 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
539 branch = i;
540
541 branch = branch % interval;
542 for (i = branch; i < 32; i += interval)
543 ohci->ohci_int_load [i] += load;
544
545 return branch;
546}
547
548/*-------------------------------------------------------------------------*/
549
550/* 2^int( ld (inter)) */
551
Remy Bohmer172a1132008-09-16 14:55:43 +0200552static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200553{
554 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200555 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200556 return 1 << i;
557}
558
559/*-------------------------------------------------------------------------*/
560
561/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200562 * in order to process it sequentially the indexes of the branches have to
563 * be mapped the mapping reverses the bits of a word of num_bits length */
564static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200565{
566 int i, wout = 0;
567
568 for (i = 0; i < num_bits; i++)
569 wout |= (((word >> i) & 1) << (num_bits - i - 1));
570 return wout;
571}
572
573/*-------------------------------------------------------------------------*
574 * ED handling functions
575 *-------------------------------------------------------------------------*/
576
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200577/* link an ed into one of the HC chains */
578
Remy Bohmer172a1132008-09-16 14:55:43 +0200579static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200580{
581 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200582 int int_branch;
583 int i;
584 int inter;
585 int interval;
586 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200587 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200588
589 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200590 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200591
592 switch (ed->type) {
593 case PIPE_CONTROL:
594 ed->hwNextED = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200595 if (ohci->ed_controltail == NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500596 ohci_writel(ed, &ohci->regs->ed_controlhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200597 else
598 ohci->ed_controltail->hwNextED =
599 m32_swap((unsigned long)ed);
600
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200601 ed->ed_prev = ohci->ed_controltail;
602 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
603 !ohci->ed_rm_list[1] && !ohci->sleeping) {
604 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500605 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200606 }
607 ohci->ed_controltail = edi;
608 break;
609
610 case PIPE_BULK:
611 ed->hwNextED = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200612 if (ohci->ed_bulktail == NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500613 ohci_writel(ed, &ohci->regs->ed_bulkhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200614 else
615 ohci->ed_bulktail->hwNextED =
616 m32_swap((unsigned long)ed);
617
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200618 ed->ed_prev = ohci->ed_bulktail;
619 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
620 !ohci->ed_rm_list[1] && !ohci->sleeping) {
621 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500622 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200623 }
624 ohci->ed_bulktail = edi;
625 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200626
627 case PIPE_INTERRUPT:
628 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200629 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200630 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200631 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200632 ed->int_branch = int_branch;
633
Remy Bohmer172a1132008-09-16 14:55:43 +0200634 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200635 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200636 for (ed_p = &(ohci->hcca->int_table[\
637 ep_rev(5, i) + int_branch]);
638 (*ed_p != 0) &&
639 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200640 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200641 inter = ep_rev(6,
642 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200643 ed->hwNextED = *ed_p;
Martin Krause8a588192007-08-21 12:40:34 +0200644 *ed_p = m32_swap((unsigned long)ed);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200645 }
646 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200647 }
648 return 0;
649}
650
651/*-------------------------------------------------------------------------*/
652
Zhang Wei8d15efa2007-06-06 10:08:14 +0200653/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200654static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
655 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200656{
657 for (; index < NUM_INTS; index += period) {
658 __u32 *ed_p = &ohci->hcca->int_table [index];
659
660 /* ED might have been unlinked through another path */
661 while (*ed_p != 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200662 if (((struct ed *)
663 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200664 *ed_p = ed->hwNextED;
665 break;
666 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200667 ed_p = &(((struct ed *)
668 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200669 }
670 }
671}
672
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200673/* unlink an ed from one of the HC chains.
674 * just the link to the ed is unlinked.
675 * the link from the ed still points to another operational ed or 0
676 * so the HC can eventually finish the processing of the unlinked ed */
677
Remy Bohmer172a1132008-09-16 14:55:43 +0200678static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200679{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100680 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200681 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100682
Remy Bohmer172a1132008-09-16 14:55:43 +0200683 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200684
685 switch (ed->type) {
686 case PIPE_CONTROL:
687 if (ed->ed_prev == NULL) {
688 if (!ed->hwNextED) {
689 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500690 ohci_writel(ohci->hc_control,
691 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200692 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500693 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200694 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200695 } else {
696 ed->ed_prev->hwNextED = ed->hwNextED;
697 }
698 if (ohci->ed_controltail == ed) {
699 ohci->ed_controltail = ed->ed_prev;
700 } else {
Remy Bohmer172a1132008-09-16 14:55:43 +0200701 ((ed_t *)m32_swap(
702 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200703 }
704 break;
705
706 case PIPE_BULK:
707 if (ed->ed_prev == NULL) {
708 if (!ed->hwNextED) {
709 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500710 ohci_writel(ohci->hc_control,
711 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200712 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500713 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200714 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200715 } else {
716 ed->ed_prev->hwNextED = ed->hwNextED;
717 }
718 if (ohci->ed_bulktail == ed) {
719 ohci->ed_bulktail = ed->ed_prev;
720 } else {
Remy Bohmer172a1132008-09-16 14:55:43 +0200721 ((ed_t *)m32_swap(
722 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200723 }
724 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200725
726 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200727 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200728 for (i = ed->int_branch; i < 32; i += ed->int_interval)
729 ohci->ohci_int_load[i] -= ed->int_load;
730 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200731 }
732 ed->state = ED_UNLINK;
733 return 0;
734}
735
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200736/*-------------------------------------------------------------------------*/
737
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200738/* add/reinit an endpoint; this should be done once at the
739 * usb_set_configuration command, but the USB stack is a little bit
740 * stateless so we do it at every transaction if the state of the ed
741 * is ED_NEW then a dummy td is added and the state is changed to
742 * ED_UNLINK in all other cases the state is left unchanged the ed
743 * info fields are setted anyway even though most of them should not
744 * change
745 */
Remy Bohmer172a1132008-09-16 14:55:43 +0200746static ed_t *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe,
747 int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200748{
749 td_t *td;
750 ed_t *ed_ret;
751 volatile ed_t *ed;
752
Remy Bohmer172a1132008-09-16 14:55:43 +0200753 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
754 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200755
756 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
757 err("ep_add_ed: pending delete");
758 /* pending delete request */
759 return NULL;
760 }
761
762 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200763 /* dummy td; end of td list for ed */
Remy Bohmer172a1132008-09-16 14:55:43 +0200764 td = td_alloc(usb_dev);
765 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200766 ed->hwHeadP = ed->hwTailP;
767 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200768 ed->type = usb_pipetype(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200769 ohci_dev.ed_cnt++;
770 }
771
Remy Bohmer172a1132008-09-16 14:55:43 +0200772 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
773 | usb_pipeendpoint(pipe) << 7
774 | (usb_pipeisoc(pipe)? 0x8000: 0)
775 | (usb_pipecontrol(pipe)? 0: \
776 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000777 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer172a1132008-09-16 14:55:43 +0200778 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200779
Zhang Wei8d15efa2007-06-06 10:08:14 +0200780 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
781 ed->int_period = interval;
782 ed->int_load = load;
783 }
784
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200785 return ed_ret;
786}
787
788/*-------------------------------------------------------------------------*
789 * TD handling functions
790 *-------------------------------------------------------------------------*/
791
792/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
793
Remy Bohmer172a1132008-09-16 14:55:43 +0200794static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200795 void *data, int len,
796 struct usb_device *dev, int index, urb_priv_t *urb_priv)
797{
798 volatile td_t *td, *td_pt;
799#ifdef OHCI_FILL_TRACE
800 int i;
801#endif
802
803 if (index > urb_priv->length) {
804 err("index > length");
805 return;
806 }
807 /* use this td as the next dummy */
808 td_pt = urb_priv->td [index];
809 td_pt->hwNextTD = 0;
810
811 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200812 td = urb_priv->td [index] =
813 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200814
815 td->ed = urb_priv->ed;
816 td->next_dl_td = NULL;
817 td->index = index;
818 td->data = (__u32)data;
819#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200820 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200821 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200822 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200823 printf("\n");
824 }
825#endif
826 if (!len)
827 data = 0;
828
Remy Bohmer172a1132008-09-16 14:55:43 +0200829 td->hwINFO = m32_swap(info);
830 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200831 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200832 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200833 else
834 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200835
836 td->hwNextTD = m32_swap((unsigned long)td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200837
838 /* append to queue */
839 td->ed->hwTailP = td->hwNextTD;
840}
841
842/*-------------------------------------------------------------------------*/
843
844/* prepare all TDs of a transfer */
845
Hans de Goede2a240e72015-05-05 23:56:07 +0200846static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
847 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200848 struct devrequest *setup, urb_priv_t *urb,
849 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200850{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200851 int data_len = transfer_len;
852 void *data;
853 int cnt = 0;
854 __u32 info = 0;
855 unsigned int toggle = 0;
856
Remy Bohmer172a1132008-09-16 14:55:43 +0200857 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
858 * bits for reseting */
859 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200860 toggle = TD_T_TOGGLE;
861 } else {
862 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200863 usb_settoggle(dev, usb_pipeendpoint(pipe),
864 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200865 }
866 urb->td_cnt = 0;
867 if (data_len)
868 data = buffer;
869 else
870 data = 0;
871
Remy Bohmer172a1132008-09-16 14:55:43 +0200872 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200873 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200874 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200875 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200876 while (data_len > 4096) {
877 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
878 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200879 data += 4096; data_len -= 4096; cnt++;
880 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200881 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200882 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200883 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
884 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200885 cnt++;
886
Remy Bohmer172a1132008-09-16 14:55:43 +0200887 if (!ohci->sleeping) {
888 /* start bulk list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500889 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200890 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200891 break;
892
893 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +0200894 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200895 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200896 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
897
898 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200899 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200900 info = usb_pipeout(pipe)?
901 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
902 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200903 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +0200904 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200905 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200906
907 /* Status phase */
908 info = usb_pipeout(pipe)?
909 TD_CC | TD_DP_IN | TD_T_DATA1:
910 TD_CC | TD_DP_OUT | TD_T_DATA1;
911 td_fill(ohci, info, data, 0, dev, cnt++, urb);
912
913 if (!ohci->sleeping) {
914 /* start Control list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500915 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200916 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200917 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200918
919 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200920 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +0200921 TD_CC | TD_DP_OUT | toggle:
922 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +0200923 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200924 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200925 }
926 if (urb->length != cnt)
927 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
928}
929
930/*-------------------------------------------------------------------------*
931 * Done List handling functions
932 *-------------------------------------------------------------------------*/
933
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200934/* calculate the transfer length and update the urb */
935
Remy Bohmer172a1132008-09-16 14:55:43 +0200936static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200937{
Wolfgang Denk0ef5f622011-10-05 23:03:43 +0200938 __u32 tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200939 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200940
Remy Bohmer172a1132008-09-16 14:55:43 +0200941 tdBE = m32_swap(td->hwBE);
942 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200943
Remy Bohmer47e72082008-10-10 10:23:21 +0200944 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200945 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
946 if (tdBE != 0) {
947 if (td->hwCBP == 0)
948 lurb_priv->actual_length += tdBE - td->data + 1;
949 else
950 lurb_priv->actual_length += tdCBP - td->data;
951 }
952 }
953}
954
955/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +0200956static void check_status(td_t *td_list)
957{
958 urb_priv_t *lurb_priv = td_list->ed->purb;
959 int urb_len = lurb_priv->length;
960 __u32 *phwHeadP = &td_list->ed->hwHeadP;
961 int cc;
962
963 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
964 if (cc) {
965 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
966
967 if (*phwHeadP & m32_swap(0x1)) {
968 if (lurb_priv &&
969 ((td_list->index + 1) < urb_len)) {
970 *phwHeadP =
971 (lurb_priv->td[urb_len - 1]->hwNextTD &\
972 m32_swap(0xfffffff0)) |
973 (*phwHeadP & m32_swap(0x2));
974
975 lurb_priv->td_cnt += urb_len -
976 td_list->index - 1;
977 } else
978 *phwHeadP &= m32_swap(0xfffffff2);
979 }
980#ifdef CONFIG_MPC5200
981 td_list->hwNextTD = 0;
982#endif
983 }
984}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200985
986/* replies to the request have to be on a FIFO basis so
987 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +0200988static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200989{
990 __u32 td_list_hc;
991 td_t *td_rev = NULL;
992 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200993
Remy Bohmer172a1132008-09-16 14:55:43 +0200994 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200995 ohci->hcca->done_head = 0;
996
997 while (td_list_hc) {
998 td_list = (td_t *)td_list_hc;
Remy Bohmer172a1132008-09-16 14:55:43 +0200999 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001000 td_list->next_dl_td = td_rev;
1001 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001002 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001003 }
1004 return td_list;
1005}
1006
1007/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001008/*-------------------------------------------------------------------------*/
1009
1010static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1011{
1012 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1013 urb->finished = sohci_return_job(ohci, urb);
1014 else
1015 dbg("finish_urb: strange.., ED state %x, \n", status);
1016}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001017
Remy Bohmer172a1132008-09-16 14:55:43 +02001018/*
1019 * Used to take back a TD from the host controller. This would normally be
1020 * called from within dl_done_list, however it may be called directly if the
1021 * HC no longer sees the TD and it has not appeared on the donelist (after
1022 * two frames). This bug has been observed on ZF Micro systems.
1023 */
1024static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001025{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001026 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001027 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001028 int stat = 0;
1029 /* urb_t *urb; */
1030 urb_priv_t *lurb_priv;
1031 __u32 tdINFO, edHeadP, edTailP;
1032
Remy Bohmer172a1132008-09-16 14:55:43 +02001033 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001034
Remy Bohmer172a1132008-09-16 14:55:43 +02001035 ed = td_list->ed;
1036 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001037
Remy Bohmer172a1132008-09-16 14:55:43 +02001038 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001039
Remy Bohmer172a1132008-09-16 14:55:43 +02001040 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001041
Remy Bohmer172a1132008-09-16 14:55:43 +02001042 /* error code of transfer */
1043 cc = TD_CC_GET(tdINFO);
1044 if (cc) {
1045 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1046 stat = cc_to_error[cc];
1047 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001048
Remy Bohmer172a1132008-09-16 14:55:43 +02001049 /* see if this done list makes for all TD's of current URB,
1050 * and mark the URB finished if so */
1051 if (lurb_priv->td_cnt == lurb_priv->length)
1052 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001053
Remy Bohmer172a1132008-09-16 14:55:43 +02001054 dbg("dl_done_list: processing TD %x, len %x\n",
1055 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001056
Remy Bohmer47e72082008-10-10 10:23:21 +02001057 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001058 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1059 edTailP = m32_swap(ed->hwTailP);
1060
1061 /* unlink eds if they are not busy */
1062 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1063 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001064 }
1065 return stat;
1066}
1067
Remy Bohmer172a1132008-09-16 14:55:43 +02001068static int dl_done_list(ohci_t *ohci)
1069{
1070 int stat = 0;
1071 td_t *td_list = dl_reverse_done_list(ohci);
1072
1073 while (td_list) {
1074 td_t *td_next = td_list->next_dl_td;
1075 stat = takeback_td(ohci, td_list);
1076 td_list = td_next;
1077 }
1078 return stat;
1079}
1080
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001081/*-------------------------------------------------------------------------*
1082 * Virtual Root Hub
1083 *-------------------------------------------------------------------------*/
1084
Stephen Warren39c89682014-02-13 21:15:18 -07001085#include <usbroothubdes.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001086
1087/* Hub class-specific descriptor is constructed dynamically */
1088
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001089/*-------------------------------------------------------------------------*/
1090
1091#define OK(x) len = (x); break
1092#ifdef DEBUG
Becky Bruce4abaacb2010-06-30 13:05:44 -05001093#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001094 &ohci->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +02001095#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goede2a240e72015-05-05 23:56:07 +02001096 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001097#else
Hans de Goede2a240e72015-05-05 23:56:07 +02001098#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001099#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001100 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001101#endif
Hans de Goede2a240e72015-05-05 23:56:07 +02001102#define RD_RH_STAT roothub_status(ohci)
1103#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001104
1105/* request to virtual root hub */
1106
1107int rh_check_port_status(ohci_t *controller)
1108{
1109 __u32 temp, ndp, i;
1110 int res;
1111
1112 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001113 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001114 ndp = (temp & RH_A_NDP);
1115#ifdef CONFIG_AT91C_PQFP_UHPBUG
1116 ndp = (ndp == 2) ? 1:0;
1117#endif
1118 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001119 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001120 /* check for a device disconnect */
1121 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1122 (RH_PS_PESC | RH_PS_CSC)) &&
1123 ((temp & RH_PS_CCS) == 0)) {
1124 res = i;
1125 break;
1126 }
1127 }
1128 return res;
1129}
1130
Hans de Goede2a240e72015-05-05 23:56:07 +02001131static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1132 unsigned long pipe, void *buffer, int transfer_len,
1133 struct devrequest *cmd)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001134{
Remy Bohmer172a1132008-09-16 14:55:43 +02001135 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001136 int leni = transfer_len;
1137 int len = 0;
1138 int stat = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001139 __u16 bmRType_bReq;
1140 __u16 wValue;
1141 __u16 wIndex;
1142 __u16 wLength;
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001143 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasutfc5cf882011-10-07 02:00:13 +02001144
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001145#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001146pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001147 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001148#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001149 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001150#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001151 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001152 info("Root-Hub submit IRQ: NOT implemented");
1153 return 0;
1154 }
1155
1156 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001157 wValue = le16_to_cpu(cmd->value);
1158 wIndex = le16_to_cpu(cmd->index);
1159 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001160
1161 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1162 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1163
1164 switch (bmRType_bReq) {
1165 /* Request Destination:
1166 without flags: Device,
1167 RH_INTERFACE: interface,
1168 RH_ENDPOINT: endpoint,
1169 RH_CLASS means HUB here,
1170 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1171 */
1172
1173 case RH_GET_STATUS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001174 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001175 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001176 case RH_GET_STATUS | RH_INTERFACE:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001177 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001178 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001179 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001180 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001181 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001182 case RH_GET_STATUS | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001183 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001184 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001185 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001186 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001187 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer172a1132008-09-16 14:55:43 +02001188 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001189
1190 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1191 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001192 case (RH_ENDPOINT_STALL):
1193 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001194 }
1195 break;
1196
1197 case RH_CLEAR_FEATURE | RH_CLASS:
1198 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001199 case RH_C_HUB_LOCAL_POWER:
1200 OK(0);
1201 case (RH_C_HUB_OVER_CURRENT):
1202 WR_RH_STAT(RH_HS_OCIC);
1203 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001204 }
1205 break;
1206
1207 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1208 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001209 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1210 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1211 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1212 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1213 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1214 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1215 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1216 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001217 }
1218 break;
1219
1220 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1221 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001222 case (RH_PORT_SUSPEND):
1223 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1224 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1225 if (RD_RH_PORTSTAT & RH_PS_CCS)
1226 WR_RH_PORTSTAT(RH_PS_PRS);
1227 OK(0);
1228 case (RH_PORT_POWER):
1229 WR_RH_PORTSTAT(RH_PS_PPS);
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001230 mdelay(100);
Remy Bohmer172a1132008-09-16 14:55:43 +02001231 OK(0);
1232 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1233 if (RD_RH_PORTSTAT & RH_PS_CCS)
1234 WR_RH_PORTSTAT(RH_PS_PES);
1235 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001236 }
1237 break;
1238
Remy Bohmer172a1132008-09-16 14:55:43 +02001239 case RH_SET_ADDRESS:
Hans de Goede2a240e72015-05-05 23:56:07 +02001240 ohci->rh.devnum = wValue;
Remy Bohmer172a1132008-09-16 14:55:43 +02001241 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001242
1243 case RH_GET_DESCRIPTOR:
1244 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001245 case (0x01): /* device descriptor */
1246 len = min_t(unsigned int,
1247 leni,
1248 min_t(unsigned int,
1249 sizeof(root_hub_dev_des),
1250 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001251 databuf = root_hub_dev_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001252 case (0x02): /* configuration descriptor */
1253 len = min_t(unsigned int,
1254 leni,
1255 min_t(unsigned int,
1256 sizeof(root_hub_config_des),
1257 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001258 databuf = root_hub_config_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001259 case (0x03): /* string descriptors */
1260 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001261 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001262 leni,
1263 min_t(unsigned int,
1264 sizeof(root_hub_str_index0),
1265 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001266 databuf = root_hub_str_index0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001267 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001268 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001269 if (wValue == 0x0301) {
1270 len = min_t(unsigned int,
1271 leni,
1272 min_t(unsigned int,
1273 sizeof(root_hub_str_index1),
1274 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001275 databuf = root_hub_str_index1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001276 OK(len);
1277 }
1278 default:
1279 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001280 }
1281 break;
1282
1283 case RH_GET_DESCRIPTOR | RH_CLASS:
1284 {
Hans de Goede2a240e72015-05-05 23:56:07 +02001285 __u32 temp = roothub_a(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001286
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001287 databuf[0] = 9; /* min length; */
1288 databuf[1] = 0x29;
1289 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001290#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001291 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001292#endif
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001293 databuf[3] = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001294 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001295 databuf[3] |= 0x1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001296 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001297 databuf[3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001298 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001299 databuf[3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001300
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001301 databuf[4] = 0;
1302 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1303 databuf[6] = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001304 temp = roothub_b(ohci);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001305 databuf[7] = temp & RH_B_DR;
1306 if (databuf[2] < 7) {
1307 databuf[8] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001308 } else {
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001309 databuf[0] += 2;
1310 databuf[8] = (temp & RH_B_DR) >> 8;
1311 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001312 }
1313
1314 len = min_t(unsigned int, leni,
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001315 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001316 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001317 }
1318
Marek Vasutfc5cf882011-10-07 02:00:13 +02001319 case RH_GET_CONFIGURATION:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001320 databuf[0] = 0x01;
Marek Vasutfc5cf882011-10-07 02:00:13 +02001321 OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001322
Marek Vasutfc5cf882011-10-07 02:00:13 +02001323 case RH_SET_CONFIGURATION:
1324 WR_RH_STAT(0x10000);
1325 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001326
1327 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001328 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001329 stat = USB_ST_STALLED;
1330 }
1331
1332#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001333 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001334#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001335 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001336#endif
1337
1338 len = min_t(int, len, leni);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001339 if (data != databuf)
1340 memcpy(data, databuf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001341 dev->act_len = len;
1342 dev->status = stat;
1343
1344#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001345 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer172a1132008-09-16 14:55:43 +02001346 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001347#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001348 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001349#endif
1350
1351 return stat;
1352}
1353
1354/*-------------------------------------------------------------------------*/
1355
1356/* common code for handling submit messages - used for all but root hub */
1357/* accesses. */
Hans de Goede2a240e72015-05-05 23:56:07 +02001358static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1359 unsigned long pipe, void *buffer, int transfer_len,
1360 struct devrequest *setup, int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001361{
1362 int stat = 0;
1363 int maxsize = usb_maxpacket(dev, pipe);
1364 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001365 urb_priv_t *urb;
1366
1367 urb = malloc(sizeof(urb_priv_t));
1368 memset(urb, 0, sizeof(urb_priv_t));
1369
1370 urb->dev = dev;
1371 urb->pipe = pipe;
1372 urb->transfer_buffer = buffer;
1373 urb->transfer_buffer_length = transfer_len;
1374 urb->interval = interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001375
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001376#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001377 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001378 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001379 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001380#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001381 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001382#endif
1383 if (!maxsize) {
1384 err("submit_common_message: pipesize for pipe %lx is zero",
1385 pipe);
1386 return -1;
1387 }
1388
Hans de Goede2a240e72015-05-05 23:56:07 +02001389 if (sohci_submit_job(ohci, urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001390 err("sohci_submit_job failed");
1391 return -1;
1392 }
1393
Markus Klotzbuecherd209de62006-11-27 11:46:46 +01001394#if 0
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001395 mdelay(10);
Hans de Goede2a240e72015-05-05 23:56:07 +02001396 /* ohci_dump_status(ohci); */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +01001397#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001398
Simon Glassfd7f5132011-02-07 14:42:16 -08001399 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001400
1401 /* wait for it to complete */
1402 for (;;) {
1403 /* check whether the controller is done */
Hans de Goede2a240e72015-05-05 23:56:07 +02001404 stat = hc_interrupt(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001405 if (stat < 0) {
1406 stat = USB_ST_CRC_ERR;
1407 break;
1408 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001409
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001410 /* NOTE: since we are not interrupt driven in U-Boot and always
1411 * handle only one URB at a time, we cannot assume the
1412 * transaction finished on the first successful return from
1413 * hc_interrupt().. unless the flag for current URB is set,
1414 * meaning that all TD's to/from device got actually
1415 * transferred and processed. If the current URB is not
1416 * finished we need to re-iterate this loop so as
1417 * hc_interrupt() gets called again as there needs to be some
1418 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001419 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001420 /* 0xff is returned for an SF-interrupt */
1421 break;
1422 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001423
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001424 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001425 mdelay(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001426 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001427 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001428
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001429 } else {
1430 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001431 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001432 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001433 stat = USB_ST_CRC_ERR;
1434 break;
1435 }
1436 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001437
1438 dev->status = stat;
Mateusz Kulikowski10b479d2013-10-23 20:26:27 +02001439 dev->act_len = urb->actual_length;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001440
1441#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001442 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001443 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001444#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001445 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001446#endif
1447
1448 /* free TDs in urb_priv */
Remy Bohmer47e72082008-10-10 10:23:21 +02001449 if (!usb_pipeint(pipe))
Remy Bohmer172a1132008-09-16 14:55:43 +02001450 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001451 return 0;
1452}
1453
1454/* submit routines called from usb.c */
1455int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1456 int transfer_len)
1457{
1458 info("submit_bulk_msg");
Hans de Goede2a240e72015-05-05 23:56:07 +02001459 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1460 NULL, 0);
1461}
1462
1463int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1464 int transfer_len, int interval)
1465{
1466 info("submit_int_msg");
1467 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1468 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001469}
1470
Hans de Goede2a240e72015-05-05 23:56:07 +02001471static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1472 unsigned long pipe, void *buffer, int transfer_len,
1473 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001474{
1475 int maxsize = usb_maxpacket(dev, pipe);
1476
1477 info("submit_control_msg");
1478#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001479 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001480 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001481#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001482 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001483#endif
1484 if (!maxsize) {
1485 err("submit_control_message: pipesize for pipe %lx is zero",
1486 pipe);
1487 return -1;
1488 }
Hans de Goede2a240e72015-05-05 23:56:07 +02001489 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1490 ohci->rh.dev = dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001491 /* root hub - redirect */
Hans de Goede2a240e72015-05-05 23:56:07 +02001492 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1493 transfer_len, setup);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001494 }
1495
Hans de Goede2a240e72015-05-05 23:56:07 +02001496 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1497 setup, 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001498}
1499
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001500/*-------------------------------------------------------------------------*
1501 * HC functions
1502 *-------------------------------------------------------------------------*/
1503
1504/* reset the HC and BUS */
1505
Remy Bohmer172a1132008-09-16 14:55:43 +02001506static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001507{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001508#ifdef CONFIG_PCI_EHCI_DEVNO
1509 pci_dev_t pdev;
1510#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001511 int timeout = 30;
1512 int smm_timeout = 50; /* 0,5 sec */
1513
1514 dbg("%s\n", __FUNCTION__);
1515
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001516#ifdef CONFIG_PCI_EHCI_DEVNO
1517 /*
1518 * Some multi-function controllers (e.g. ISP1562) allow root hub
1519 * resetting via EHCI registers only.
1520 */
1521 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1522 if (pdev != -1) {
1523 u32 base;
1524 int timeout = 1000;
1525
1526 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001527 base += EHCI_USBCMD_OFF;
1528 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001529
Becky Bruce4abaacb2010-06-30 13:05:44 -05001530 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001531 if (timeout-- <= 0) {
1532 printf("USB RootHub reset timed out!");
1533 break;
1534 }
1535 udelay(1);
1536 }
1537 } else
1538 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1539#endif
Becky Bruce4abaacb2010-06-30 13:05:44 -05001540 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1541 /* SMM owns the HC, request ownership */
1542 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001543 info("USB HC TakeOver from SMM");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001544 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001545 mdelay(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001546 if (--smm_timeout == 0) {
1547 err("USB HC TakeOver failed!");
1548 return -1;
1549 }
1550 }
1551 }
1552
1553 /* Disable HC interrupts */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001554 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001555
1556 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1557 ohci->slot_name,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001558 ohci_readl(&ohci->regs->control));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001559
1560 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001561 ohci->hc_control = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001562 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001563
1564 /* HC Reset requires max 10 us delay */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001565 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1566 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001567 if (--timeout == 0) {
1568 err("USB HC reset timed out!");
1569 return -1;
1570 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001571 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001572 }
1573 return 0;
1574}
1575
1576/*-------------------------------------------------------------------------*/
1577
1578/* Start an OHCI controller, set the BUS operational
1579 * enable interrupts
1580 * connect the virtual root hub */
1581
Remy Bohmer172a1132008-09-16 14:55:43 +02001582static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001583{
1584 __u32 mask;
1585 unsigned int fminterval;
1586
1587 ohci->disabled = 1;
1588
1589 /* Tell the controller where the control and bulk lists are
1590 * The lists are empty now. */
1591
Becky Bruce4abaacb2010-06-30 13:05:44 -05001592 ohci_writel(0, &ohci->regs->ed_controlhead);
1593 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001594
Becky Bruce4abaacb2010-06-30 13:05:44 -05001595 ohci_writel((__u32)ohci->hcca,
1596 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001597
1598 fminterval = 0x2edf;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001599 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001600 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001601 ohci_writel(fminterval, &ohci->regs->fminterval);
1602 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001603
1604 /* start controller operations */
1605 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1606 ohci->disabled = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001607 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001608
1609 /* disable all interrupts */
1610 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1611 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1612 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001613 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001614 /* clear all interrupts */
1615 mask &= ~OHCI_INTR_MIE;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001616 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001617 /* Choose the interrupts we care about now - but w/o MIE */
1618 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001619 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001620
1621#ifdef OHCI_USE_NPS
1622 /* required for AMD-756 and some Mac platforms */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001623 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001624 &ohci->regs->roothub.a);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001625 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001626#endif /* OHCI_USE_NPS */
1627
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001628 /* POTPGT delay is bits 24-31, in 2 ms units. */
Remy Bohmer172a1132008-09-16 14:55:43 +02001629 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001630
1631 /* connect the virtual root hub */
1632 ohci->rh.devnum = 0;
1633
1634 return 0;
1635}
1636
1637/*-------------------------------------------------------------------------*/
1638
1639/* an interrupt happens */
1640
Hans de Goede2a240e72015-05-05 23:56:07 +02001641static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001642{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001643 struct ohci_regs *regs = ohci->regs;
1644 int ints;
1645 int stat = -1;
1646
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001647 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001648 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001649 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001650 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001651 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001652 if (ints == ~(u32)0) {
1653 ohci->disabled++;
1654 err("%s device removed!", ohci->slot_name);
1655 return -1;
1656 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001657 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer172a1132008-09-16 14:55:43 +02001658 if (ints == 0) {
1659 dbg("hc_interrupt: returning..\n");
1660 return 0xff;
1661 }
1662 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001663 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001664
Remy Bohmer172a1132008-09-16 14:55:43 +02001665 /* dbg("Interrupt: %x frame: %x", ints,
1666 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001667
Remy Bohmer172a1132008-09-16 14:55:43 +02001668 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001669 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001670
1671 if (ints & OHCI_INTR_UE) {
1672 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001673 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001674 ohci->slot_name);
1675 /* e.g. due to PCI Master/Target Abort */
1676
1677#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001678 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001679#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001680 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001681#endif
1682 /* FIXME: be optimistic, hope that bug won't repeat often. */
1683 /* Make some non-interrupt context restart the controller. */
1684 /* Count and limit the retries though; either hardware or */
1685 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001686 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001687 return -1;
1688 }
1689
1690 if (ints & OHCI_INTR_WDH) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001691 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001692 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1693 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goede2a240e72015-05-05 23:56:07 +02001694 stat = dl_done_list(ohci);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001695 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1696 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001697 }
1698
1699 if (ints & OHCI_INTR_SO) {
1700 dbg("USB Schedule overrun\n");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001701 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001702 stat = -1;
1703 }
1704
1705 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1706 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001707 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001708 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001709 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001710 if (ohci->ed_rm_list[frame] != NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001711 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001712 stat = 0xff;
1713 }
1714
Becky Bruce4abaacb2010-06-30 13:05:44 -05001715 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001716 return stat;
1717}
1718
1719/*-------------------------------------------------------------------------*/
1720
1721/*-------------------------------------------------------------------------*/
1722
1723/* De-allocate all resources.. */
1724
Remy Bohmer172a1132008-09-16 14:55:43 +02001725static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001726{
Remy Bohmer172a1132008-09-16 14:55:43 +02001727 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001728
1729 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001730 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001731}
1732
1733/*-------------------------------------------------------------------------*/
1734
1735/*
1736 * low level initalisation routine, called from usb.c
1737 */
1738static char ohci_inited = 0;
1739
Troy Kisky8f9c49d2013-10-10 15:27:56 -07001740int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001741{
Zhang Wei8d15efa2007-06-06 10:08:14 +02001742#ifdef CONFIG_PCI_OHCI
1743 pci_dev_t pdev;
1744#endif
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001745
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001746#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001747 /* cpu dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02001748 if (usb_cpu_init())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001749 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001750#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001751
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001752#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001753 /* board dependant init */
Mateusz Zalegad862f892013-10-04 19:22:26 +02001754 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001755 return -1;
1756#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02001757 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001758
1759 /* align the storage */
1760 if ((__u32)&ghcca[0] & 0xff) {
1761 err("HCCA not aligned!!");
1762 return -1;
1763 }
1764 phcca = &ghcca[0];
1765 info("aligned ghcca %p", phcca);
1766 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1767 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1768 err("EDs not aligned!!");
1769 return -1;
1770 }
1771 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1772 if ((__u32)gtd & 0x7) {
1773 err("TDs not aligned!!");
1774 return -1;
1775 }
1776 ptd = gtd;
1777 gohci.hcca = phcca;
Remy Bohmer172a1132008-09-16 14:55:43 +02001778 memset(phcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001779
1780 gohci.disabled = 1;
1781 gohci.sleeping = 0;
1782 gohci.irq = -1;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001783#ifdef CONFIG_PCI_OHCI
Sergei Poselenov0b4570f2008-05-22 01:15:53 +02001784 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001785
1786 if (pdev != -1) {
1787 u16 vid, did;
1788 u32 base;
1789 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1790 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1791 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1792 vid, did, (pdev >> 16) & 0xff,
1793 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1794 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1795 printf("OHCI regs address 0x%08x\n", base);
1796 gohci.regs = (struct ohci_regs *)base;
1797 } else
1798 return -1;
1799#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001800 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001801#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001802
1803 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001804 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001805
1806 if (hc_reset (&gohci) < 0) {
1807 hc_release_ohci (&gohci);
1808 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001809#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001810 /* board dependant cleanup */
Mateusz Zalegad862f892013-10-04 19:22:26 +02001811 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001812#endif
1813
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001814#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001815 /* cpu dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001816 usb_cpu_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001817#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001818 return -1;
1819 }
1820
Remy Bohmer172a1132008-09-16 14:55:43 +02001821 if (hc_start(&gohci) < 0) {
1822 err("can't start usb-%s", gohci.slot_name);
1823 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001824 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001825#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001826 /* board dependant cleanup */
1827 usb_board_stop();
1828#endif
1829
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001830#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001831 /* cpu dependant cleanup */
1832 usb_cpu_stop();
1833#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001834 return -1;
1835 }
1836
1837#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001838 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001839#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001840 mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001841#endif
1842 ohci_inited = 1;
1843 return 0;
1844}
1845
Lucas Stacha3231282012-09-26 00:14:34 +02001846int usb_lowlevel_stop(int index)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001847{
1848 /* this gets called really early - before the controller has */
1849 /* even been initialized! */
1850 if (!ohci_inited)
1851 return 0;
1852 /* TODO release any interrupts, etc. */
1853 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02001854 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001855
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001856#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001857 /* board dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02001858 if (usb_board_stop())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001859 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001860#endif
1861
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001862#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001863 /* cpu dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02001864 if (usb_cpu_stop())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02001865 return -1;
1866#endif
Remy Bohmer743dd492008-08-20 11:22:02 +02001867 /* This driver is no longer initialised. It needs a new low-level
1868 * init (board/cpu) before it can be used again. */
1869 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001870 return 0;
1871}
Hans de Goede2a240e72015-05-05 23:56:07 +02001872
1873int submit_control_msg(struct usb_device *dev, unsigned long pipe,
1874 void *buffer, int transfer_len, struct devrequest *setup)
1875{
1876 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
1877 transfer_len, setup);
1878}