blob: 07e0848c6ece36ad8e1bad1ed90c5cd969cc5b7f [file] [log] [blame]
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001/*
Zhang Wei4dae14c2007-06-06 10:08:14 +02002 * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
3 *
4 * Interrupt support is added. Now, it has been tested
5 * on ULI1575 chip and works well with USB keyboard.
6 *
7 * (C) Copyright 2007
8 * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02009 *
10 * (C) Copyright 2003
Detlev Zundel792a09e2009-05-13 10:54:10 +020011 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020012 *
13 * Note: Much of this code has been derived from Linux 2.4
14 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
15 * (C) Copyright 2000-2002 David Brownell
16 *
17 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
18 * ebenard@eukrea.com - based on s3c24x0's driver
19 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020020 * SPDX-License-Identifier: GPL-2.0+
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020021 */
22/*
23 * IMPORTANT NOTES
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020024 * 1 - Read doc/README.generic_usb_ohci
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020025 * 2 - this driver is intended for use with USB Mass Storage Devices
Zhang Wei4dae14c2007-06-06 10:08:14 +020026 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020027 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
Markus Klotzbuecher3e326ec2006-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 Klotzbuecherfc43be42007-06-06 11:49:35 +020032#include <asm/byteorder.h>
33
34#if defined(CONFIG_PCI_OHCI)
Zhang Wei4dae14c2007-06-06 10:08:14 +020035# include <pci.h>
Sergei Poselenov477434c2008-05-22 01:15:53 +020036#if !defined(CONFIG_PCI_OHCI_DEVNO)
37#define CONFIG_PCI_OHCI_DEVNO 0
38#endif
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +020039#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020040
41#include <malloc.h>
42#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020043
44#include "ohci.h"
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020045
Wolfgang Denke8da58f2007-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 Yamadaf2168442014-11-06 14:59:35 +090050#if defined(CONFIG_CPU_ARM920T) || \
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090051 defined(CONFIG_S3C24X0) || \
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +010052 defined(CONFIG_440EP) || \
Zhang Wei4dae14c2007-06-06 10:08:14 +020053 defined(CONFIG_PCI_OHCI) || \
Stefan Roese2596f5b2008-03-05 12:29:32 +010054 defined(CONFIG_MPC5200) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020055 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher24e37642006-05-23 10:33:11 +020056# define OHCI_USE_NPS /* force NoPowerSwitching mode */
57#endif
58
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020059#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +010060#undef DEBUG
61#undef SHOW_INFO
62#undef OHCI_FILL_TRACE
Markus Klotzbuecher3e326ec2006-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 Wei4dae14c2007-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 Saada97213f32007-09-17 17:04:47 +020071 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew3afac792008-01-11 20:42:58 -060072 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei4dae14c2007-06-06 10:08:14 +020073 /* Please add supported PCI OHCI controller ids here */
74 {0, 0}
75};
76#endif
77
Yuri Tikhonove90fb6a2008-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 Klotzbuecher3e326ec2006-05-22 16:33:54 +020086#ifdef DEBUG
87#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
88#else
Remy Bohmer6f5794a2008-09-16 14:55:43 +020089#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020090#endif /* DEBUG */
91#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020092#ifdef SHOW_INFO
93#define info(format, arg...) printf("INFO: " format "\n", ## arg)
94#else
Remy Bohmer6f5794a2008-09-16 14:55:43 +020095#define info(format, arg...) do {} while (0)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +020096#endif
97
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020098#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecherfc43be42007-06-06 11:49:35 +020099# define m16_swap(x) cpu_to_be16(x)
100# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +0100101#else
Markus Klotzbuecherfc43be42007-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200104#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecher3e326ec2006-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];
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200110
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200111static inline u32 roothub_a(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500112 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200113static inline u32 roothub_b(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500114 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200115static inline u32 roothub_status(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500116 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200117static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Brucea5496a12010-06-30 13:05:44 -0500118 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200119
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200120/* forward declaration */
Hans de Goedec5613df2015-05-05 23:56:07 +0200121static int hc_interrupt(ohci_t *ohci);
122static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
123 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200124 struct devrequest *setup, urb_priv_t *urb,
125 int interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200126
127/*-------------------------------------------------------------------------*
128 * URB support functions
129 *-------------------------------------------------------------------------*/
130
131/* free HCD-private data associated with this URB */
132
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200133static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200134{
135 int i;
136 int last;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200137 struct td *td;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200138
139 last = urb->length - 1;
140 if (last >= 0) {
141 for (i = 0; i <= last; i++) {
142 td = urb->td[i];
143 if (td) {
144 td->usb_dev = NULL;
145 urb->td[i] = NULL;
146 }
147 }
148 }
Zhang Wei4dae14c2007-06-06 10:08:14 +0200149 free(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200150}
151
152/*-------------------------------------------------------------------------*/
153
154#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +0200155static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200156
157/* debug| print the main components of an URB
158 * small: 0) header + data packets 1) just header */
159
Hans de Goedec5613df2015-05-05 23:56:07 +0200160static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200161 unsigned long pipe, void *buffer, int transfer_len,
162 struct devrequest *setup, char *str, int small)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200163{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200164 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200165 str,
Hans de Goedec5613df2015-05-05 23:56:07 +0200166 sohci_get_current_frame_number(ohci),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200167 usb_pipedevice(pipe),
168 usb_pipeendpoint(pipe),
169 usb_pipeout(pipe)? 'O': 'I',
170 usb_pipetype(pipe) < 2 ? \
171 (usb_pipeint(pipe)? "INTR": "ISOC"): \
172 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei4dae14c2007-06-06 10:08:14 +0200173 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200174 transfer_len, dev->status);
175#ifdef OHCI_VERBOSE_DEBUG
176 if (!small) {
177 int i, len;
178
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200179 if (usb_pipecontrol(pipe)) {
180 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200181 for (i = 0; i < 8 ; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200182 printf(" %02x", ((__u8 *) setup) [i]);
183 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200184 }
185 if (transfer_len > 0 && buffer) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200186 printf(__FILE__ ": data(%d/%d):",
Zhang Wei4dae14c2007-06-06 10:08:14 +0200187 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200188 transfer_len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200189 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei4dae14c2007-06-06 10:08:14 +0200190 (purb ? purb->actual_length : 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200191 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200192 printf(" %02x", ((__u8 *) buffer) [i]);
193 printf("%s\n", i < len? "...": "");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200194 }
195 }
196#endif
197}
198
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200199/* just for debugging; prints non-empty branches of the int ed tree
200 * inclusive iso eds */
201void ep_print_int_eds(ohci_t *ohci, char *str)
202{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200203 int i, j;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200204 __u32 *ed_p;
205 for (i = 0; i < 32; i++) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200206 j = 5;
207 ed_p = &(ohci->hcca->int_table [i]);
208 if (*ed_p == 0)
209 continue;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200210 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200211 while (*ed_p != 0 && j--) {
212 ed_t *ed = (ed_t *)m32_swap(ed_p);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200213 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200214 ed_p = &ed->hwNextED;
215 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200216 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200217 }
218}
219
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200220static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200221{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200222 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200223 label,
224 mask,
225 (mask & OHCI_INTR_MIE) ? " MIE" : "",
226 (mask & OHCI_INTR_OC) ? " OC" : "",
227 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
228 (mask & OHCI_INTR_FNO) ? " FNO" : "",
229 (mask & OHCI_INTR_UE) ? " UE" : "",
230 (mask & OHCI_INTR_RD) ? " RD" : "",
231 (mask & OHCI_INTR_SF) ? " SF" : "",
232 (mask & OHCI_INTR_WDH) ? " WDH" : "",
233 (mask & OHCI_INTR_SO) ? " SO" : ""
234 );
235}
236
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200237static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200238{
239 ed_t *edp = (ed_t *)value;
240
241 if (value) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200242 dbg("%s %08x", label, value);
243 dbg("%08x", edp->hwINFO);
244 dbg("%08x", edp->hwTailP);
245 dbg("%08x", edp->hwHeadP);
246 dbg("%08x", edp->hwNextED);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200247 }
248}
249
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200250static char *hcfs2string(int state)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200251{
252 switch (state) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200253 case OHCI_USB_RESET: return "reset";
254 case OHCI_USB_RESUME: return "resume";
255 case OHCI_USB_OPER: return "operational";
256 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200257 }
258 return "?";
259}
260
261/* dump control and status registers */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200262static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200263{
264 struct ohci_regs *regs = controller->regs;
265 __u32 temp;
266
Becky Brucea5496a12010-06-30 13:05:44 -0500267 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200268 if (temp != 0x10)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200269 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200270
Becky Brucea5496a12010-06-30 13:05:44 -0500271 temp = ohci_readl(&regs->control);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200272 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200273 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
274 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
275 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200276 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200277 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
278 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
279 (temp & OHCI_CTRL_IE) ? " IE" : "",
280 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
281 temp & OHCI_CTRL_CBSR
282 );
283
Becky Brucea5496a12010-06-30 13:05:44 -0500284 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200285 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200286 (temp & OHCI_SOC) >> 16,
287 (temp & OHCI_OCR) ? " OCR" : "",
288 (temp & OHCI_BLF) ? " BLF" : "",
289 (temp & OHCI_CLF) ? " CLF" : "",
290 (temp & OHCI_HCR) ? " HCR" : ""
291 );
292
Becky Brucea5496a12010-06-30 13:05:44 -0500293 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
294 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200295
Becky Brucea5496a12010-06-30 13:05:44 -0500296 maybe_print_eds("ed_periodcurrent",
297 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200298
Becky Brucea5496a12010-06-30 13:05:44 -0500299 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
300 maybe_print_eds("ed_controlcurrent",
301 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200302
Becky Brucea5496a12010-06-30 13:05:44 -0500303 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
304 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200305
Becky Brucea5496a12010-06-30 13:05:44 -0500306 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200307}
308
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200309static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200310{
311 __u32 temp, ndp, i;
312
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200313 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200314 ndp = (temp & RH_A_NDP);
315#ifdef CONFIG_AT91C_PQFP_UHPBUG
316 ndp = (ndp == 2) ? 1:0;
317#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200318 if (verbose) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200319 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200320 ((temp & RH_A_POTPGT) >> 24) & 0xff,
321 (temp & RH_A_NOCP) ? " NOCP" : "",
322 (temp & RH_A_OCPM) ? " OCPM" : "",
323 (temp & RH_A_DT) ? " DT" : "",
324 (temp & RH_A_NPS) ? " NPS" : "",
325 (temp & RH_A_PSM) ? " PSM" : "",
326 ndp
327 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200328 temp = roothub_b(controller);
329 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200330 temp,
331 (temp & RH_B_PPCM) >> 16,
332 (temp & RH_B_DR)
333 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200334 temp = roothub_status(controller);
335 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200336 temp,
337 (temp & RH_HS_CRWE) ? " CRWE" : "",
338 (temp & RH_HS_OCIC) ? " OCIC" : "",
339 (temp & RH_HS_LPSC) ? " LPSC" : "",
340 (temp & RH_HS_DRWE) ? " DRWE" : "",
341 (temp & RH_HS_OCI) ? " OCI" : "",
342 (temp & RH_HS_LPS) ? " LPS" : ""
343 );
344 }
345
346 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200347 temp = roothub_portstatus(controller, i);
348 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200349 i,
350 temp,
351 (temp & RH_PS_PRSC) ? " PRSC" : "",
352 (temp & RH_PS_OCIC) ? " OCIC" : "",
353 (temp & RH_PS_PSSC) ? " PSSC" : "",
354 (temp & RH_PS_PESC) ? " PESC" : "",
355 (temp & RH_PS_CSC) ? " CSC" : "",
356
357 (temp & RH_PS_LSDA) ? " LSDA" : "",
358 (temp & RH_PS_PPS) ? " PPS" : "",
359 (temp & RH_PS_PRS) ? " PRS" : "",
360 (temp & RH_PS_POCI) ? " POCI" : "",
361 (temp & RH_PS_PSS) ? " PSS" : "",
362
363 (temp & RH_PS_PES) ? " PES" : "",
364 (temp & RH_PS_CCS) ? " CCS" : ""
365 );
366 }
367}
368
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200369static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200370{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200371 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200372
373 /* dumps some of the state we know about */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200374 ohci_dump_status(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200375 if (verbose)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200376 ep_print_int_eds(controller, "hcca");
377 dbg("hcca frame #%04x", controller->hcca->frame_no);
378 ohci_dump_roothub(controller, 1);
Stefan Roese2596f5b2008-03-05 12:29:32 +0100379}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200380#endif /* DEBUG */
381
382/*-------------------------------------------------------------------------*
383 * Interface functions (URB)
384 *-------------------------------------------------------------------------*/
385
386/* get a transfer request */
387
Hans de Goede19d95d52015-05-05 23:56:08 +0200388int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
389 struct devrequest *setup)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200390{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200391 ed_t *ed;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200392 urb_priv_t *purb_priv = urb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200393 int i, size = 0;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200394 struct usb_device *dev = urb->dev;
395 unsigned long pipe = urb->pipe;
396 void *buffer = urb->transfer_buffer;
397 int transfer_len = urb->transfer_buffer_length;
398 int interval = urb->interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200399
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200400 /* when controller's hung, permit only roothub cleanup attempts
401 * such as powering down ports */
402 if (ohci->disabled) {
403 err("sohci_submit_job: EPIPE");
404 return -1;
405 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +0200406
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200407 /* we're about to begin a new transaction here so mark the
408 * URB unfinished */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200409 urb->finished = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200410
411 /* every endpoint has a ed, locate and fill it */
Hans de Goede19d95d52015-05-05 23:56:08 +0200412 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200413 if (!ed) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200414 err("sohci_submit_job: ENOMEM");
415 return -1;
416 }
417
418 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200419 switch (usb_pipetype(pipe)) {
420 case PIPE_BULK: /* one TD for every 4096 Byte */
421 size = (transfer_len - 1) / 4096 + 1;
422 break;
423 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
424 size = (transfer_len == 0)? 2:
425 (transfer_len - 1) / 4096 + 3;
426 break;
427 case PIPE_INTERRUPT: /* 1 TD */
428 size = 1;
429 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200430 }
431
Zhang Wei4dae14c2007-06-06 10:08:14 +0200432 ed->purb = urb;
433
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200434 if (size >= (N_URB_TD - 1)) {
435 err("need %d TDs, only have %d", size, N_URB_TD);
436 return -1;
437 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200438 purb_priv->pipe = pipe;
439
440 /* fill the private part of the URB */
441 purb_priv->length = size;
442 purb_priv->ed = ed;
443 purb_priv->actual_length = 0;
444
445 /* allocate the TDs */
446 /* note that td[0] was allocated in ep_add_ed */
447 for (i = 0; i < size; i++) {
Hans de Goede3c5497d2015-05-05 23:56:09 +0200448 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200449 if (!purb_priv->td[i]) {
450 purb_priv->length = i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200451 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200452 err("sohci_submit_job: ENOMEM");
453 return -1;
454 }
455 }
456
457 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200458 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200459 err("sohci_submit_job: EINVAL");
460 return -1;
461 }
462
463 /* link the ed into a chain if is not already */
464 if (ed->state != ED_OPER)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200465 ep_link(ohci, ed);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200466
467 /* fill the TDs and link it to the ed */
Hans de Goedec5613df2015-05-05 23:56:07 +0200468 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200469 setup, purb_priv, interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200470
471 return 0;
472}
473
Zhang Wei4dae14c2007-06-06 10:08:14 +0200474static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
475{
476 struct ohci_regs *regs = hc->regs;
477
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200478 switch (usb_pipetype(urb->pipe)) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200479 case PIPE_INTERRUPT:
480 /* implicitly requeued */
481 if (urb->dev->irq_handle &&
482 (urb->dev->irq_act_len = urb->actual_length)) {
Becky Brucea5496a12010-06-30 13:05:44 -0500483 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
484 ohci_readl(&regs->intrenable); /* PCI posting flush */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200485 urb->dev->irq_handle(urb->dev);
Becky Brucea5496a12010-06-30 13:05:44 -0500486 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
487 ohci_readl(&regs->intrdisable); /* PCI posting flush */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200488 }
489 urb->actual_length = 0;
Hans de Goedec5613df2015-05-05 23:56:07 +0200490 td_submit_job( hc,
Zhang Wei4dae14c2007-06-06 10:08:14 +0200491 urb->dev,
492 urb->pipe,
493 urb->transfer_buffer,
494 urb->transfer_buffer_length,
495 NULL,
496 urb,
497 urb->interval);
498 break;
499 case PIPE_CONTROL:
500 case PIPE_BULK:
501 break;
502 default:
503 return 0;
504 }
505 return 1;
506}
507
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200508/*-------------------------------------------------------------------------*/
509
510#ifdef DEBUG
511/* tell us the current USB frame number */
Hans de Goedec5613df2015-05-05 23:56:07 +0200512static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200513{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200514 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200515}
516#endif
517
518/*-------------------------------------------------------------------------*
519 * ED handling functions
520 *-------------------------------------------------------------------------*/
521
Zhang Wei4dae14c2007-06-06 10:08:14 +0200522/* search for the right branch to insert an interrupt ed into the int tree
523 * do some load ballancing;
524 * returns the branch and
525 * sets the interval to interval = 2^integer (ld (interval)) */
526
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200527static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200528{
529 int i, branch = 0;
530
531 /* search for the least loaded interrupt endpoint
532 * branch of all 32 branches
533 */
534 for (i = 0; i < 32; i++)
535 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
536 branch = i;
537
538 branch = branch % interval;
539 for (i = branch; i < 32; i += interval)
540 ohci->ohci_int_load [i] += load;
541
542 return branch;
543}
544
545/*-------------------------------------------------------------------------*/
546
547/* 2^int( ld (inter)) */
548
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200549static int ep_2_n_interval(int inter)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200550{
551 int i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200552 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200553 return 1 << i;
554}
555
556/*-------------------------------------------------------------------------*/
557
558/* the int tree is a binary tree
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200559 * in order to process it sequentially the indexes of the branches have to
560 * be mapped the mapping reverses the bits of a word of num_bits length */
561static int ep_rev(int num_bits, int word)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200562{
563 int i, wout = 0;
564
565 for (i = 0; i < num_bits; i++)
566 wout |= (((word >> i) & 1) << (num_bits - i - 1));
567 return wout;
568}
569
570/*-------------------------------------------------------------------------*
571 * ED handling functions
572 *-------------------------------------------------------------------------*/
573
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200574/* link an ed into one of the HC chains */
575
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200576static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200577{
578 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200579 int int_branch;
580 int i;
581 int inter;
582 int interval;
583 int load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200584 __u32 *ed_p;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200585
586 ed->state = ED_OPER;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200587 ed->int_interval = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200588
589 switch (ed->type) {
590 case PIPE_CONTROL:
591 ed->hwNextED = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200592 if (ohci->ed_controltail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500593 ohci_writel(ed, &ohci->regs->ed_controlhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200594 else
595 ohci->ed_controltail->hwNextED =
596 m32_swap((unsigned long)ed);
597
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200598 ed->ed_prev = ohci->ed_controltail;
599 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
600 !ohci->ed_rm_list[1] && !ohci->sleeping) {
601 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500602 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200603 }
604 ohci->ed_controltail = edi;
605 break;
606
607 case PIPE_BULK:
608 ed->hwNextED = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200609 if (ohci->ed_bulktail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500610 ohci_writel(ed, &ohci->regs->ed_bulkhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200611 else
612 ohci->ed_bulktail->hwNextED =
613 m32_swap((unsigned long)ed);
614
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200615 ed->ed_prev = ohci->ed_bulktail;
616 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
617 !ohci->ed_rm_list[1] && !ohci->sleeping) {
618 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500619 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200620 }
621 ohci->ed_bulktail = edi;
622 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200623
624 case PIPE_INTERRUPT:
625 load = ed->int_load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200626 interval = ep_2_n_interval(ed->int_period);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200627 ed->int_interval = interval;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200628 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200629 ed->int_branch = int_branch;
630
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200631 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200632 inter = 1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200633 for (ed_p = &(ohci->hcca->int_table[\
634 ep_rev(5, i) + int_branch]);
635 (*ed_p != 0) &&
636 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200637 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200638 inter = ep_rev(6,
639 ((ed_t *)ed_p)->int_interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200640 ed->hwNextED = *ed_p;
Martin Krause4a8527e2007-08-21 12:40:34 +0200641 *ed_p = m32_swap((unsigned long)ed);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200642 }
643 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200644 }
645 return 0;
646}
647
648/*-------------------------------------------------------------------------*/
649
Zhang Wei4dae14c2007-06-06 10:08:14 +0200650/* scan the periodic table to find and unlink this ED */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200651static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
652 unsigned index, unsigned period)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200653{
654 for (; index < NUM_INTS; index += period) {
655 __u32 *ed_p = &ohci->hcca->int_table [index];
656
657 /* ED might have been unlinked through another path */
658 while (*ed_p != 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200659 if (((struct ed *)
660 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200661 *ed_p = ed->hwNextED;
662 break;
663 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200664 ed_p = &(((struct ed *)
665 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200666 }
667 }
668}
669
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200670/* unlink an ed from one of the HC chains.
671 * just the link to the ed is unlinked.
672 * the link from the ed still points to another operational ed or 0
673 * so the HC can eventually finish the processing of the unlinked ed */
674
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200675static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200676{
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100677 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200678 int i;
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100679
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200680 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200681
682 switch (ed->type) {
683 case PIPE_CONTROL:
684 if (ed->ed_prev == NULL) {
685 if (!ed->hwNextED) {
686 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500687 ohci_writel(ohci->hc_control,
688 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200689 }
Becky Brucea5496a12010-06-30 13:05:44 -0500690 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200691 &ohci->regs->ed_controlhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200692 } else {
693 ed->ed_prev->hwNextED = ed->hwNextED;
694 }
695 if (ohci->ed_controltail == ed) {
696 ohci->ed_controltail = ed->ed_prev;
697 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200698 ((ed_t *)m32_swap(
699 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200700 }
701 break;
702
703 case PIPE_BULK:
704 if (ed->ed_prev == NULL) {
705 if (!ed->hwNextED) {
706 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500707 ohci_writel(ohci->hc_control,
708 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200709 }
Becky Brucea5496a12010-06-30 13:05:44 -0500710 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200711 &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200712 } else {
713 ed->ed_prev->hwNextED = ed->hwNextED;
714 }
715 if (ohci->ed_bulktail == ed) {
716 ohci->ed_bulktail = ed->ed_prev;
717 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200718 ((ed_t *)m32_swap(
719 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200720 }
721 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200722
723 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200724 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200725 for (i = ed->int_branch; i < 32; i += ed->int_interval)
726 ohci->ohci_int_load[i] -= ed->int_load;
727 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200728 }
729 ed->state = ED_UNLINK;
730 return 0;
731}
732
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200733/*-------------------------------------------------------------------------*/
734
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +0200735/* add/reinit an endpoint; this should be done once at the
736 * usb_set_configuration command, but the USB stack is a little bit
737 * stateless so we do it at every transaction if the state of the ed
738 * is ED_NEW then a dummy td is added and the state is changed to
739 * ED_UNLINK in all other cases the state is left unchanged the ed
740 * info fields are setted anyway even though most of them should not
741 * change
742 */
Hans de Goede19d95d52015-05-05 23:56:08 +0200743static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
744 unsigned long pipe, int interval, int load)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200745{
746 td_t *td;
747 ed_t *ed_ret;
748 volatile ed_t *ed;
749
Hans de Goede19d95d52015-05-05 23:56:08 +0200750 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200751 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200752
753 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
754 err("ep_add_ed: pending delete");
755 /* pending delete request */
756 return NULL;
757 }
758
759 if (ed->state == ED_NEW) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200760 /* dummy td; end of td list for ed */
Hans de Goede3c5497d2015-05-05 23:56:09 +0200761 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200762 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200763 ed->hwHeadP = ed->hwTailP;
764 ed->state = ED_UNLINK;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200765 ed->type = usb_pipetype(pipe);
Hans de Goede19d95d52015-05-05 23:56:08 +0200766 ohci_dev->ed_cnt++;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200767 }
768
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200769 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
770 | usb_pipeendpoint(pipe) << 7
771 | (usb_pipeisoc(pipe)? 0x8000: 0)
772 | (usb_pipecontrol(pipe)? 0: \
773 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanokc60795f2012-11-06 13:48:20 +0000774 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200775 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200776
Zhang Wei4dae14c2007-06-06 10:08:14 +0200777 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
778 ed->int_period = interval;
779 ed->int_load = load;
780 }
781
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200782 return ed_ret;
783}
784
785/*-------------------------------------------------------------------------*
786 * TD handling functions
787 *-------------------------------------------------------------------------*/
788
789/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
790
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200791static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200792 void *data, int len,
793 struct usb_device *dev, int index, urb_priv_t *urb_priv)
794{
795 volatile td_t *td, *td_pt;
796#ifdef OHCI_FILL_TRACE
797 int i;
798#endif
799
800 if (index > urb_priv->length) {
801 err("index > length");
802 return;
803 }
804 /* use this td as the next dummy */
805 td_pt = urb_priv->td [index];
806 td_pt->hwNextTD = 0;
807
808 /* fill the old dummy TD */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200809 td = urb_priv->td [index] =
810 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200811
812 td->ed = urb_priv->ed;
813 td->next_dl_td = NULL;
814 td->index = index;
815 td->data = (__u32)data;
816#ifdef OHCI_FILL_TRACE
Remy Bohmer48867202008-10-10 10:23:21 +0200817 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200818 for (i = 0; i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200819 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200820 printf("\n");
821 }
822#endif
823 if (!len)
824 data = 0;
825
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200826 td->hwINFO = m32_swap(info);
827 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200828 if (data)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200829 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200830 else
831 td->hwBE = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200832
833 td->hwNextTD = m32_swap((unsigned long)td_pt);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200834
835 /* append to queue */
836 td->ed->hwTailP = td->hwNextTD;
837}
838
839/*-------------------------------------------------------------------------*/
840
841/* prepare all TDs of a transfer */
842
Hans de Goedec5613df2015-05-05 23:56:07 +0200843static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
844 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200845 struct devrequest *setup, urb_priv_t *urb,
846 int interval)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200847{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200848 int data_len = transfer_len;
849 void *data;
850 int cnt = 0;
851 __u32 info = 0;
852 unsigned int toggle = 0;
853
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200854 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
855 * bits for reseting */
856 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200857 toggle = TD_T_TOGGLE;
858 } else {
859 toggle = TD_T_DATA0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200860 usb_settoggle(dev, usb_pipeendpoint(pipe),
861 usb_pipeout(pipe), 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200862 }
863 urb->td_cnt = 0;
864 if (data_len)
865 data = buffer;
866 else
867 data = 0;
868
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200869 switch (usb_pipetype(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200870 case PIPE_BULK:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200871 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200872 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200873 while (data_len > 4096) {
874 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
875 data, 4096, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200876 data += 4096; data_len -= 4096; cnt++;
877 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200878 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200879 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200880 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
881 data_len, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200882 cnt++;
883
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200884 if (!ohci->sleeping) {
885 /* start bulk list */
Becky Brucea5496a12010-06-30 13:05:44 -0500886 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200887 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200888 break;
889
890 case PIPE_CONTROL:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200891 /* Setup phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200892 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200893 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
894
895 /* Optional Data phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200896 if (data_len > 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200897 info = usb_pipeout(pipe)?
898 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
899 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200900 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200901 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200902 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200903
904 /* Status phase */
905 info = usb_pipeout(pipe)?
906 TD_CC | TD_DP_IN | TD_T_DATA1:
907 TD_CC | TD_DP_OUT | TD_T_DATA1;
908 td_fill(ohci, info, data, 0, dev, cnt++, urb);
909
910 if (!ohci->sleeping) {
911 /* start Control list */
Becky Brucea5496a12010-06-30 13:05:44 -0500912 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200913 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200914 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200915
916 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200917 info = usb_pipeout(urb->pipe)?
Zhang Wei4dae14c2007-06-06 10:08:14 +0200918 TD_CC | TD_DP_OUT | toggle:
919 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200920 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200921 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200922 }
923 if (urb->length != cnt)
924 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
925}
926
927/*-------------------------------------------------------------------------*
928 * Done List handling functions
929 *-------------------------------------------------------------------------*/
930
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200931/* calculate the transfer length and update the urb */
932
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200933static void dl_transfer_length(td_t *td)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200934{
Wolfgang Denk6bc52ef2011-10-05 23:03:43 +0200935 __u32 tdBE, tdCBP;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200936 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200937
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200938 tdBE = m32_swap(td->hwBE);
939 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200940
Remy Bohmer48867202008-10-10 10:23:21 +0200941 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200942 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
943 if (tdBE != 0) {
944 if (td->hwCBP == 0)
945 lurb_priv->actual_length += tdBE - td->data + 1;
946 else
947 lurb_priv->actual_length += tdCBP - td->data;
948 }
949 }
950}
951
952/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200953static void check_status(td_t *td_list)
954{
955 urb_priv_t *lurb_priv = td_list->ed->purb;
956 int urb_len = lurb_priv->length;
957 __u32 *phwHeadP = &td_list->ed->hwHeadP;
958 int cc;
959
960 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
961 if (cc) {
962 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
963
964 if (*phwHeadP & m32_swap(0x1)) {
965 if (lurb_priv &&
966 ((td_list->index + 1) < urb_len)) {
967 *phwHeadP =
968 (lurb_priv->td[urb_len - 1]->hwNextTD &\
969 m32_swap(0xfffffff0)) |
970 (*phwHeadP & m32_swap(0x2));
971
972 lurb_priv->td_cnt += urb_len -
973 td_list->index - 1;
974 } else
975 *phwHeadP &= m32_swap(0xfffffff2);
976 }
977#ifdef CONFIG_MPC5200
978 td_list->hwNextTD = 0;
979#endif
980 }
981}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200982
983/* replies to the request have to be on a FIFO basis so
984 * we reverse the reversed done-list */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200985static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200986{
987 __u32 td_list_hc;
988 td_t *td_rev = NULL;
989 td_t *td_list = NULL;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200990
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200991 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200992 ohci->hcca->done_head = 0;
993
994 while (td_list_hc) {
995 td_list = (td_t *)td_list_hc;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200996 check_status(td_list);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200997 td_list->next_dl_td = td_rev;
998 td_rev = td_list;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200999 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001000 }
1001 return td_list;
1002}
1003
1004/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001005/*-------------------------------------------------------------------------*/
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001006
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001007static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001008{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001009 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1010 urb->finished = sohci_return_job(ohci, urb);
1011 else
1012 dbg("finish_urb: strange.., ED state %x, \n", status);
1013}
1014
1015/*
1016 * Used to take back a TD from the host controller. This would normally be
1017 * called from within dl_done_list, however it may be called directly if the
1018 * HC no longer sees the TD and it has not appeared on the donelist (after
1019 * two frames). This bug has been observed on ZF Micro systems.
1020 */
1021static int takeback_td(ohci_t *ohci, td_t *td_list)
1022{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001023 ed_t *ed;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001024 int cc;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001025 int stat = 0;
1026 /* urb_t *urb; */
1027 urb_priv_t *lurb_priv;
1028 __u32 tdINFO, edHeadP, edTailP;
1029
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001030 tdINFO = m32_swap(td_list->hwINFO);
1031
1032 ed = td_list->ed;
1033 lurb_priv = ed->purb;
1034
1035 dl_transfer_length(td_list);
1036
1037 lurb_priv->td_cnt++;
1038
1039 /* error code of transfer */
1040 cc = TD_CC_GET(tdINFO);
1041 if (cc) {
1042 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1043 stat = cc_to_error[cc];
1044 }
1045
1046 /* see if this done list makes for all TD's of current URB,
1047 * and mark the URB finished if so */
1048 if (lurb_priv->td_cnt == lurb_priv->length)
1049 finish_urb(ohci, lurb_priv, ed->state);
1050
1051 dbg("dl_done_list: processing TD %x, len %x\n",
1052 lurb_priv->td_cnt, lurb_priv->length);
1053
Remy Bohmer48867202008-10-10 10:23:21 +02001054 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001055 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1056 edTailP = m32_swap(ed->hwTailP);
1057
1058 /* unlink eds if they are not busy */
1059 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1060 ep_unlink(ohci, ed);
1061 }
1062 return stat;
1063}
1064
1065static int dl_done_list(ohci_t *ohci)
1066{
1067 int stat = 0;
1068 td_t *td_list = dl_reverse_done_list(ohci);
1069
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001070 while (td_list) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001071 td_t *td_next = td_list->next_dl_td;
1072 stat = takeback_td(ohci, td_list);
1073 td_list = td_next;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001074 }
1075 return stat;
1076}
1077
1078/*-------------------------------------------------------------------------*
1079 * Virtual Root Hub
1080 *-------------------------------------------------------------------------*/
1081
Stephen Warreneb838e72014-02-13 21:15:18 -07001082#include <usbroothubdes.h>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001083
1084/* Hub class-specific descriptor is constructed dynamically */
1085
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001086/*-------------------------------------------------------------------------*/
1087
1088#define OK(x) len = (x); break
1089#ifdef DEBUG
Becky Brucea5496a12010-06-30 13:05:44 -05001090#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goedec5613df2015-05-05 23:56:07 +02001091 &ohci->regs->roothub.status); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001092#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goedec5613df2015-05-05 23:56:07 +02001093 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001094#else
Hans de Goedec5613df2015-05-05 23:56:07 +02001095#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Brucea5496a12010-06-30 13:05:44 -05001096#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goedec5613df2015-05-05 23:56:07 +02001097 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001098#endif
Hans de Goedec5613df2015-05-05 23:56:07 +02001099#define RD_RH_STAT roothub_status(ohci)
1100#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001101
1102/* request to virtual root hub */
1103
1104int rh_check_port_status(ohci_t *controller)
1105{
1106 __u32 temp, ndp, i;
1107 int res;
1108
1109 res = -1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001110 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001111 ndp = (temp & RH_A_NDP);
1112#ifdef CONFIG_AT91C_PQFP_UHPBUG
1113 ndp = (ndp == 2) ? 1:0;
1114#endif
1115 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001116 temp = roothub_portstatus(controller, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001117 /* check for a device disconnect */
1118 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1119 (RH_PS_PESC | RH_PS_CSC)) &&
1120 ((temp & RH_PS_CCS) == 0)) {
1121 res = i;
1122 break;
1123 }
1124 }
1125 return res;
1126}
1127
Hans de Goedec5613df2015-05-05 23:56:07 +02001128static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1129 unsigned long pipe, void *buffer, int transfer_len,
1130 struct devrequest *cmd)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001131{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001132 void *data = buffer;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001133 int leni = transfer_len;
1134 int len = 0;
1135 int stat = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001136 __u16 bmRType_bReq;
1137 __u16 wValue;
1138 __u16 wIndex;
1139 __u16 wLength;
Troy Kiskyf1273f12012-08-11 14:49:09 -07001140 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasut5f6aa032011-10-07 02:00:13 +02001141
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001142#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001143pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001144 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001145#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001146 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001147#endif
Remy Bohmer48867202008-10-10 10:23:21 +02001148 if (usb_pipeint(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001149 info("Root-Hub submit IRQ: NOT implemented");
1150 return 0;
1151 }
1152
1153 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001154 wValue = le16_to_cpu(cmd->value);
1155 wIndex = le16_to_cpu(cmd->index);
1156 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001157
1158 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1159 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1160
1161 switch (bmRType_bReq) {
1162 /* Request Destination:
1163 without flags: Device,
1164 RH_INTERFACE: interface,
1165 RH_ENDPOINT: endpoint,
1166 RH_CLASS means HUB here,
1167 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1168 */
1169
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001170 case RH_GET_STATUS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001171 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001172 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001173 case RH_GET_STATUS | RH_INTERFACE:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001174 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001175 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001176 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001177 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001178 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001179 case RH_GET_STATUS | RH_CLASS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001180 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001181 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001182 OK(4);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001183 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001184 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001185 OK(4);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001186
1187 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1188 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001189 case (RH_ENDPOINT_STALL):
1190 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001191 }
1192 break;
1193
1194 case RH_CLEAR_FEATURE | RH_CLASS:
1195 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001196 case RH_C_HUB_LOCAL_POWER:
1197 OK(0);
1198 case (RH_C_HUB_OVER_CURRENT):
1199 WR_RH_STAT(RH_HS_OCIC);
1200 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001201 }
1202 break;
1203
1204 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1205 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001206 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1207 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1208 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1209 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1210 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1211 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1212 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1213 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001214 }
1215 break;
1216
1217 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1218 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001219 case (RH_PORT_SUSPEND):
1220 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1221 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1222 if (RD_RH_PORTSTAT & RH_PS_CCS)
1223 WR_RH_PORTSTAT(RH_PS_PRS);
1224 OK(0);
1225 case (RH_PORT_POWER):
1226 WR_RH_PORTSTAT(RH_PS_PPS);
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001227 mdelay(100);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001228 OK(0);
1229 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1230 if (RD_RH_PORTSTAT & RH_PS_CCS)
1231 WR_RH_PORTSTAT(RH_PS_PES);
1232 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001233 }
1234 break;
1235
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001236 case RH_SET_ADDRESS:
Hans de Goedec5613df2015-05-05 23:56:07 +02001237 ohci->rh.devnum = wValue;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001238 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001239
1240 case RH_GET_DESCRIPTOR:
1241 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001242 case (0x01): /* device descriptor */
1243 len = min_t(unsigned int,
1244 leni,
1245 min_t(unsigned int,
1246 sizeof(root_hub_dev_des),
1247 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001248 databuf = root_hub_dev_des; OK(len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001249 case (0x02): /* configuration descriptor */
1250 len = min_t(unsigned int,
1251 leni,
1252 min_t(unsigned int,
1253 sizeof(root_hub_config_des),
1254 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001255 databuf = root_hub_config_des; OK(len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001256 case (0x03): /* string descriptors */
1257 if (wValue == 0x0300) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001258 len = min_t(unsigned int,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001259 leni,
1260 min_t(unsigned int,
1261 sizeof(root_hub_str_index0),
1262 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001263 databuf = root_hub_str_index0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001264 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001265 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001266 if (wValue == 0x0301) {
1267 len = min_t(unsigned int,
1268 leni,
1269 min_t(unsigned int,
1270 sizeof(root_hub_str_index1),
1271 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001272 databuf = root_hub_str_index1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001273 OK(len);
1274 }
1275 default:
1276 stat = USB_ST_STALLED;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001277 }
1278 break;
1279
1280 case RH_GET_DESCRIPTOR | RH_CLASS:
1281 {
Hans de Goedec5613df2015-05-05 23:56:07 +02001282 __u32 temp = roothub_a(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001283
Troy Kiskyf1273f12012-08-11 14:49:09 -07001284 databuf[0] = 9; /* min length; */
1285 databuf[1] = 0x29;
1286 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001287#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kiskyf1273f12012-08-11 14:49:09 -07001288 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001289#endif
Troy Kiskyf1273f12012-08-11 14:49:09 -07001290 databuf[3] = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001291 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001292 databuf[3] |= 0x1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001293 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001294 databuf[3] |= 0x10;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001295 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001296 databuf[3] |= 0x8;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001297
Troy Kiskyf1273f12012-08-11 14:49:09 -07001298 databuf[4] = 0;
1299 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1300 databuf[6] = 0;
Hans de Goedec5613df2015-05-05 23:56:07 +02001301 temp = roothub_b(ohci);
Troy Kiskyf1273f12012-08-11 14:49:09 -07001302 databuf[7] = temp & RH_B_DR;
1303 if (databuf[2] < 7) {
1304 databuf[8] = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001305 } else {
Troy Kiskyf1273f12012-08-11 14:49:09 -07001306 databuf[0] += 2;
1307 databuf[8] = (temp & RH_B_DR) >> 8;
1308 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001309 }
1310
1311 len = min_t(unsigned int, leni,
Troy Kiskyf1273f12012-08-11 14:49:09 -07001312 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001313 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001314 }
1315
Marek Vasut5f6aa032011-10-07 02:00:13 +02001316 case RH_GET_CONFIGURATION:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001317 databuf[0] = 0x01;
Marek Vasut5f6aa032011-10-07 02:00:13 +02001318 OK(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001319
Marek Vasut5f6aa032011-10-07 02:00:13 +02001320 case RH_SET_CONFIGURATION:
1321 WR_RH_STAT(0x10000);
1322 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001323
1324 default:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001325 dbg("unsupported root hub command");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001326 stat = USB_ST_STALLED;
1327 }
1328
1329#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001330 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001331#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001332 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001333#endif
1334
1335 len = min_t(int, len, leni);
Troy Kiskyf1273f12012-08-11 14:49:09 -07001336 if (data != databuf)
1337 memcpy(data, databuf, len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001338 dev->act_len = len;
1339 dev->status = stat;
1340
1341#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001342 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001343 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001344#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001345 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001346#endif
1347
1348 return stat;
1349}
1350
1351/*-------------------------------------------------------------------------*/
1352
1353/* common code for handling submit messages - used for all but root hub */
1354/* accesses. */
Hans de Goedec5613df2015-05-05 23:56:07 +02001355static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1356 unsigned long pipe, void *buffer, int transfer_len,
1357 struct devrequest *setup, int interval)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001358{
1359 int stat = 0;
1360 int maxsize = usb_maxpacket(dev, pipe);
1361 int timeout;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001362 urb_priv_t *urb;
1363
1364 urb = malloc(sizeof(urb_priv_t));
1365 memset(urb, 0, sizeof(urb_priv_t));
1366
1367 urb->dev = dev;
1368 urb->pipe = pipe;
1369 urb->transfer_buffer = buffer;
1370 urb->transfer_buffer_length = transfer_len;
1371 urb->interval = interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001372
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001373#ifdef DEBUG
Zhang Wei4dae14c2007-06-06 10:08:14 +02001374 urb->actual_length = 0;
Hans de Goedec5613df2015-05-05 23:56:07 +02001375 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001376 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001377#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001378 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001379#endif
1380 if (!maxsize) {
1381 err("submit_common_message: pipesize for pipe %lx is zero",
1382 pipe);
1383 return -1;
1384 }
1385
Hans de Goede19d95d52015-05-05 23:56:08 +02001386 if (sohci_submit_job(ohci, &ohci->ohci_dev, urb, setup) < 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001387 err("sohci_submit_job failed");
1388 return -1;
1389 }
1390
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001391#if 0
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001392 mdelay(10);
Hans de Goedec5613df2015-05-05 23:56:07 +02001393 /* ohci_dump_status(ohci); */
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001394#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001395
Simon Glass96820a32011-02-07 14:42:16 -08001396 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001397
1398 /* wait for it to complete */
1399 for (;;) {
1400 /* check whether the controller is done */
Hans de Goedec5613df2015-05-05 23:56:07 +02001401 stat = hc_interrupt(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001402 if (stat < 0) {
1403 stat = USB_ST_CRC_ERR;
1404 break;
1405 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001406
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001407 /* NOTE: since we are not interrupt driven in U-Boot and always
1408 * handle only one URB at a time, we cannot assume the
1409 * transaction finished on the first successful return from
1410 * hc_interrupt().. unless the flag for current URB is set,
1411 * meaning that all TD's to/from device got actually
1412 * transferred and processed. If the current URB is not
1413 * finished we need to re-iterate this loop so as
1414 * hc_interrupt() gets called again as there needs to be some
1415 * more TD's to process still */
Zhang Wei4dae14c2007-06-06 10:08:14 +02001416 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001417 /* 0xff is returned for an SF-interrupt */
1418 break;
1419 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001420
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001421 if (--timeout) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001422 mdelay(1);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001423 if (!urb->finished)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001424 dbg("*");
Zhang Wei4dae14c2007-06-06 10:08:14 +02001425
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001426 } else {
1427 err("CTL:TIMEOUT ");
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001428 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001429 urb->finished = 1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001430 stat = USB_ST_CRC_ERR;
1431 break;
1432 }
1433 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001434
1435 dev->status = stat;
Mateusz Kulikowski522c9562013-10-23 20:26:27 +02001436 dev->act_len = urb->actual_length;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001437
1438#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001439 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001440 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001441#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001442 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001443#endif
1444
1445 /* free TDs in urb_priv */
Remy Bohmer48867202008-10-10 10:23:21 +02001446 if (!usb_pipeint(pipe))
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001447 urb_free_priv(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001448 return 0;
1449}
1450
1451/* submit routines called from usb.c */
1452int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1453 int transfer_len)
1454{
1455 info("submit_bulk_msg");
Hans de Goedec5613df2015-05-05 23:56:07 +02001456 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1457 NULL, 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001458}
1459
Hans de Goedec5613df2015-05-05 23:56:07 +02001460int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1461 int transfer_len, int interval)
1462{
1463 info("submit_int_msg");
1464 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1465 interval);
1466}
1467
1468static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1469 unsigned long pipe, void *buffer, int transfer_len,
1470 struct devrequest *setup)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001471{
1472 int maxsize = usb_maxpacket(dev, pipe);
1473
1474 info("submit_control_msg");
1475#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001476 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001477 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001478#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001479 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001480#endif
1481 if (!maxsize) {
1482 err("submit_control_message: pipesize for pipe %lx is zero",
1483 pipe);
1484 return -1;
1485 }
Hans de Goedec5613df2015-05-05 23:56:07 +02001486 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1487 ohci->rh.dev = dev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001488 /* root hub - redirect */
Hans de Goedec5613df2015-05-05 23:56:07 +02001489 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1490 transfer_len, setup);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001491 }
1492
Hans de Goedec5613df2015-05-05 23:56:07 +02001493 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1494 setup, 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001495}
1496
1497/*-------------------------------------------------------------------------*
1498 * HC functions
1499 *-------------------------------------------------------------------------*/
1500
1501/* reset the HC and BUS */
1502
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001503static int hc_reset(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001504{
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001505#ifdef CONFIG_PCI_EHCI_DEVNO
1506 pci_dev_t pdev;
1507#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001508 int timeout = 30;
1509 int smm_timeout = 50; /* 0,5 sec */
1510
1511 dbg("%s\n", __FUNCTION__);
1512
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001513#ifdef CONFIG_PCI_EHCI_DEVNO
1514 /*
1515 * Some multi-function controllers (e.g. ISP1562) allow root hub
1516 * resetting via EHCI registers only.
1517 */
1518 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1519 if (pdev != -1) {
1520 u32 base;
1521 int timeout = 1000;
1522
1523 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Brucea5496a12010-06-30 13:05:44 -05001524 base += EHCI_USBCMD_OFF;
1525 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001526
Becky Brucea5496a12010-06-30 13:05:44 -05001527 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001528 if (timeout-- <= 0) {
1529 printf("USB RootHub reset timed out!");
1530 break;
1531 }
1532 udelay(1);
1533 }
1534 } else
1535 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1536#endif
Becky Brucea5496a12010-06-30 13:05:44 -05001537 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1538 /* SMM owns the HC, request ownership */
1539 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001540 info("USB HC TakeOver from SMM");
Becky Brucea5496a12010-06-30 13:05:44 -05001541 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001542 mdelay(10);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001543 if (--smm_timeout == 0) {
1544 err("USB HC TakeOver failed!");
1545 return -1;
1546 }
1547 }
1548 }
1549
1550 /* Disable HC interrupts */
Becky Brucea5496a12010-06-30 13:05:44 -05001551 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001552
1553 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1554 ohci->slot_name,
Becky Brucea5496a12010-06-30 13:05:44 -05001555 ohci_readl(&ohci->regs->control));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001556
1557 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +01001558 ohci->hc_control = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001559 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001560
1561 /* HC Reset requires max 10 us delay */
Becky Brucea5496a12010-06-30 13:05:44 -05001562 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1563 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001564 if (--timeout == 0) {
1565 err("USB HC reset timed out!");
1566 return -1;
1567 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001568 udelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001569 }
1570 return 0;
1571}
1572
1573/*-------------------------------------------------------------------------*/
1574
1575/* Start an OHCI controller, set the BUS operational
1576 * enable interrupts
1577 * connect the virtual root hub */
1578
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001579static int hc_start(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001580{
1581 __u32 mask;
1582 unsigned int fminterval;
1583
1584 ohci->disabled = 1;
1585
1586 /* Tell the controller where the control and bulk lists are
1587 * The lists are empty now. */
1588
Becky Brucea5496a12010-06-30 13:05:44 -05001589 ohci_writel(0, &ohci->regs->ed_controlhead);
1590 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001591
Becky Brucea5496a12010-06-30 13:05:44 -05001592 ohci_writel((__u32)ohci->hcca,
1593 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001594
1595 fminterval = 0x2edf;
Becky Brucea5496a12010-06-30 13:05:44 -05001596 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001597 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Brucea5496a12010-06-30 13:05:44 -05001598 ohci_writel(fminterval, &ohci->regs->fminterval);
1599 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001600
1601 /* start controller operations */
1602 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1603 ohci->disabled = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001604 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001605
1606 /* disable all interrupts */
1607 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1608 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1609 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Brucea5496a12010-06-30 13:05:44 -05001610 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001611 /* clear all interrupts */
1612 mask &= ~OHCI_INTR_MIE;
Becky Brucea5496a12010-06-30 13:05:44 -05001613 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001614 /* Choose the interrupts we care about now - but w/o MIE */
1615 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Brucea5496a12010-06-30 13:05:44 -05001616 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001617
1618#ifdef OHCI_USE_NPS
1619 /* required for AMD-756 and some Mac platforms */
Becky Brucea5496a12010-06-30 13:05:44 -05001620 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001621 &ohci->regs->roothub.a);
Becky Brucea5496a12010-06-30 13:05:44 -05001622 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001623#endif /* OHCI_USE_NPS */
1624
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001625 /* POTPGT delay is bits 24-31, in 2 ms units. */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001626 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001627
1628 /* connect the virtual root hub */
1629 ohci->rh.devnum = 0;
1630
1631 return 0;
1632}
1633
1634/*-------------------------------------------------------------------------*/
1635
1636/* an interrupt happens */
1637
Hans de Goedec5613df2015-05-05 23:56:07 +02001638static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001639{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001640 struct ohci_regs *regs = ohci->regs;
1641 int ints;
1642 int stat = -1;
1643
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001644 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001645 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001646 ints = OHCI_INTR_WDH;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001647 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001648 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001649 if (ints == ~(u32)0) {
1650 ohci->disabled++;
1651 err("%s device removed!", ohci->slot_name);
1652 return -1;
1653 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001654 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001655 if (ints == 0) {
1656 dbg("hc_interrupt: returning..\n");
1657 return 0xff;
1658 }
1659 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001660 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +02001661
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001662 /* dbg("Interrupt: %x frame: %x", ints,
1663 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001664
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001665 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001666 stat = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001667
1668 if (ints & OHCI_INTR_UE) {
1669 ohci->disabled++;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001670 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001671 ohci->slot_name);
1672 /* e.g. due to PCI Master/Target Abort */
1673
1674#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001675 ohci_dump(ohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001676#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001677 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001678#endif
1679 /* FIXME: be optimistic, hope that bug won't repeat often. */
1680 /* Make some non-interrupt context restart the controller. */
1681 /* Count and limit the retries though; either hardware or */
1682 /* software errors can go forever... */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001683 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001684 return -1;
1685 }
1686
1687 if (ints & OHCI_INTR_WDH) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001688 mdelay(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001689 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1690 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goedec5613df2015-05-05 23:56:07 +02001691 stat = dl_done_list(ohci);
Becky Brucea5496a12010-06-30 13:05:44 -05001692 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1693 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001694 }
1695
1696 if (ints & OHCI_INTR_SO) {
1697 dbg("USB Schedule overrun\n");
Becky Brucea5496a12010-06-30 13:05:44 -05001698 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001699 stat = -1;
1700 }
1701
1702 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1703 if (ints & OHCI_INTR_SF) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001704 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001705 mdelay(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001706 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001707 if (ohci->ed_rm_list[frame] != NULL)
Becky Brucea5496a12010-06-30 13:05:44 -05001708 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001709 stat = 0xff;
1710 }
1711
Becky Brucea5496a12010-06-30 13:05:44 -05001712 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001713 return stat;
1714}
1715
1716/*-------------------------------------------------------------------------*/
1717
1718/*-------------------------------------------------------------------------*/
1719
1720/* De-allocate all resources.. */
1721
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001722static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001723{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001724 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001725
1726 if (!ohci->disabled)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001727 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001728}
1729
1730/*-------------------------------------------------------------------------*/
1731
1732/*
1733 * low level initalisation routine, called from usb.c
1734 */
1735static char ohci_inited = 0;
1736
Troy Kisky06d513e2013-10-10 15:27:56 -07001737int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001738{
Zhang Wei4dae14c2007-06-06 10:08:14 +02001739#ifdef CONFIG_PCI_OHCI
1740 pci_dev_t pdev;
1741#endif
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001742
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001743#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001744 /* cpu dependant init */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001745 if (usb_cpu_init())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001746 return -1;
1747#endif
1748
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001749#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001750 /* board dependant init */
Mateusz Zalega16297cf2013-10-04 19:22:26 +02001751 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001752 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001753#endif
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001754 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001755
1756 /* align the storage */
1757 if ((__u32)&ghcca[0] & 0xff) {
1758 err("HCCA not aligned!!");
1759 return -1;
1760 }
Hans de Goede26548bb2015-05-05 23:56:10 +02001761 gohci.hcca = &ghcca[0];
1762 info("aligned ghcca %p", gohci.hcca);
1763 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001764
1765 gohci.disabled = 1;
1766 gohci.sleeping = 0;
1767 gohci.irq = -1;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001768#ifdef CONFIG_PCI_OHCI
Sergei Poselenov477434c2008-05-22 01:15:53 +02001769 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001770
1771 if (pdev != -1) {
1772 u16 vid, did;
1773 u32 base;
1774 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1775 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1776 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1777 vid, did, (pdev >> 16) & 0xff,
1778 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1779 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1780 printf("OHCI regs address 0x%08x\n", base);
1781 gohci.regs = (struct ohci_regs *)base;
1782 } else
1783 return -1;
1784#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001785 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001786#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001787
1788 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001789 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001790
1791 if (hc_reset (&gohci) < 0) {
1792 hc_release_ohci (&gohci);
1793 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001794#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001795 /* board dependant cleanup */
Mateusz Zalega16297cf2013-10-04 19:22:26 +02001796 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001797#endif
1798
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001799#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001800 /* cpu dependant cleanup */
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001801 usb_cpu_init_fail();
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001802#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001803 return -1;
1804 }
1805
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001806 if (hc_start(&gohci) < 0) {
1807 err("can't start usb-%s", gohci.slot_name);
1808 hc_release_ohci(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001809 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001810#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001811 /* board dependant cleanup */
1812 usb_board_stop();
1813#endif
1814
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001815#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001816 /* cpu dependant cleanup */
1817 usb_cpu_stop();
1818#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001819 return -1;
1820 }
1821
1822#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001823 ohci_dump(&gohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001824#else
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001825 mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001826#endif
1827 ohci_inited = 1;
1828 return 0;
1829}
1830
Lucas Stachc7e3b2b2012-09-26 00:14:34 +02001831int usb_lowlevel_stop(int index)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001832{
1833 /* this gets called really early - before the controller has */
1834 /* even been initialized! */
1835 if (!ohci_inited)
1836 return 0;
1837 /* TODO release any interrupts, etc. */
1838 /* call hc_release_ohci() here ? */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001839 hc_reset(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001840
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001841#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001842 /* board dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001843 if (usb_board_stop())
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001844 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001845#endif
1846
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001847#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001848 /* cpu dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001849 if (usb_cpu_stop())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001850 return -1;
1851#endif
Remy Bohmereba1f2f2008-08-20 11:22:02 +02001852 /* This driver is no longer initialised. It needs a new low-level
1853 * init (board/cpu) before it can be used again. */
1854 ohci_inited = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001855 return 0;
1856}
Hans de Goedec5613df2015-05-05 23:56:07 +02001857
1858int submit_control_msg(struct usb_device *dev, unsigned long pipe,
1859 void *buffer, int transfer_len, struct devrequest *setup)
1860{
1861 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
1862 transfer_len, setup);
1863}