blob: a56aac39cad722abec7648704d53c664843047a6 [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
Hans de Goede8d005ef2015-05-05 23:56:13 +0200106#ifdef CONFIG_DM_USB
107/*
108 * We really should do proper cache flushing everywhere, but for now we only
109 * do it for new (driver-model) usb code to avoid regressions.
110 */
111#define flush_dcache_buffer(addr, size) \
112 flush_dcache_range((unsigned long)(addr), \
113 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
114#define invalidate_dcache_buffer(addr, size) \
115 invalidate_dcache_range((unsigned long)(addr), \
116 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
117#else
118#define flush_dcache_buffer(addr, size)
119#define invalidate_dcache_buffer(addr, size)
120#endif
121
122/* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
123#define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
124#define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
125#define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
126#define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
127#define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
128#define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
129#define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
130#define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
131
Hans de Goede8f761f02015-05-10 14:10:24 +0200132#ifdef CONFIG_DM_USB
133/*
134 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
135 * them around when building for older boards not yet converted to the dm
136 * just in case (to avoid regressions), for dm this turns them into nops.
137 */
138#define ohci_mdelay(x)
139#else
140#define ohci_mdelay(x) mdelay(x)
141#endif
142
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200143/* global ohci_t */
144static ohci_t gohci;
145/* this must be aligned to a 256 byte boundary */
146struct ohci_hcca ghcca[1];
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200147
Hans de Goede6651c142015-05-05 23:56:11 +0200148/* mapping of the OHCI CC status to error codes */
149static int cc_to_error[16] = {
150 /* No Error */ 0,
151 /* CRC Error */ USB_ST_CRC_ERR,
152 /* Bit Stuff */ USB_ST_BIT_ERR,
153 /* Data Togg */ USB_ST_CRC_ERR,
154 /* Stall */ USB_ST_STALLED,
155 /* DevNotResp */ -1,
156 /* PIDCheck */ USB_ST_BIT_ERR,
157 /* UnExpPID */ USB_ST_BIT_ERR,
158 /* DataOver */ USB_ST_BUF_ERR,
159 /* DataUnder */ USB_ST_BUF_ERR,
160 /* reservd */ -1,
161 /* reservd */ -1,
162 /* BufferOver */ USB_ST_BUF_ERR,
163 /* BuffUnder */ USB_ST_BUF_ERR,
164 /* Not Access */ -1,
165 /* Not Access */ -1
166};
167
168static const char *cc_to_string[16] = {
169 "No Error",
170 "CRC: Last data packet from endpoint contained a CRC error.",
171 "BITSTUFFING: Last data packet from endpoint contained a bit " \
172 "stuffing violation",
173 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
174 "that did not match the expected value.",
175 "STALL: TD was moved to the Done Queue because the endpoint returned" \
176 " a STALL PID",
177 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
178 "not provide a handshake (OUT)",
179 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
180 "(IN) or handshake (OUT)",
181 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
182 "value is not defined.",
183 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
184 "either the size of the maximum data packet allowed\n" \
185 "from the endpoint (found in MaximumPacketSize field\n" \
186 "of ED) or the remaining buffer size.",
187 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
188 "and that amount was not sufficient to fill the\n" \
189 "specified buffer",
190 "reserved1",
191 "reserved2",
192 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
193 "than it could be written to system memory",
194 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
195 "system memory fast enough to keep up with data USB " \
196 "data rate.",
197 "NOT ACCESSED: This code is set by software before the TD is placed" \
198 "on a list to be processed by the HC.(1)",
199 "NOT ACCESSED: This code is set by software before the TD is placed" \
200 "on a list to be processed by the HC.(2)",
201};
202
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200203static inline u32 roothub_a(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500204 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200205static inline u32 roothub_b(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500206 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200207static inline u32 roothub_status(struct ohci *hc)
Becky Brucea5496a12010-06-30 13:05:44 -0500208 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200209static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Brucea5496a12010-06-30 13:05:44 -0500210 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200211
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200212/* forward declaration */
Hans de Goedec5613df2015-05-05 23:56:07 +0200213static int hc_interrupt(ohci_t *ohci);
214static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
215 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200216 struct devrequest *setup, urb_priv_t *urb,
217 int interval);
Hans de Goede6651c142015-05-05 23:56:11 +0200218static int ep_link(ohci_t * ohci, ed_t * ed);
219static int ep_unlink(ohci_t * ohci, ed_t * ed);
220static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
221 unsigned long pipe, int interval, int load);
222
223/*-------------------------------------------------------------------------*/
224
225/* TDs ... */
226static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
227{
228 int i;
229 struct td *td;
230
231 td = NULL;
232 for (i = 0; i < NUM_TD; i++)
233 {
234 if (ohci_dev->tds[i].usb_dev == NULL)
235 {
236 td = &ohci_dev->tds[i];
237 td->usb_dev = usb_dev;
238 break;
239 }
240 }
241
242 return td;
243}
244
245static inline void ed_free(struct ed *ed)
246{
247 ed->usb_dev = NULL;
248}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200249
250/*-------------------------------------------------------------------------*
251 * URB support functions
252 *-------------------------------------------------------------------------*/
253
254/* free HCD-private data associated with this URB */
255
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200256static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200257{
258 int i;
259 int last;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200260 struct td *td;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200261
262 last = urb->length - 1;
263 if (last >= 0) {
264 for (i = 0; i <= last; i++) {
265 td = urb->td[i];
266 if (td) {
267 td->usb_dev = NULL;
268 urb->td[i] = NULL;
269 }
270 }
271 }
Zhang Wei4dae14c2007-06-06 10:08:14 +0200272 free(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200273}
274
275/*-------------------------------------------------------------------------*/
276
277#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +0200278static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200279
280/* debug| print the main components of an URB
281 * small: 0) header + data packets 1) just header */
282
Hans de Goedec5613df2015-05-05 23:56:07 +0200283static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200284 unsigned long pipe, void *buffer, int transfer_len,
285 struct devrequest *setup, char *str, int small)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200286{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200287 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200288 str,
Hans de Goedec5613df2015-05-05 23:56:07 +0200289 sohci_get_current_frame_number(ohci),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200290 usb_pipedevice(pipe),
291 usb_pipeendpoint(pipe),
292 usb_pipeout(pipe)? 'O': 'I',
293 usb_pipetype(pipe) < 2 ? \
294 (usb_pipeint(pipe)? "INTR": "ISOC"): \
295 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei4dae14c2007-06-06 10:08:14 +0200296 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200297 transfer_len, dev->status);
298#ifdef OHCI_VERBOSE_DEBUG
299 if (!small) {
300 int i, len;
301
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200302 if (usb_pipecontrol(pipe)) {
303 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200304 for (i = 0; i < 8 ; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200305 printf(" %02x", ((__u8 *) setup) [i]);
306 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200307 }
308 if (transfer_len > 0 && buffer) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200309 printf(__FILE__ ": data(%d/%d):",
Zhang Wei4dae14c2007-06-06 10:08:14 +0200310 (purb ? purb->actual_length : 0),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200311 transfer_len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200312 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei4dae14c2007-06-06 10:08:14 +0200313 (purb ? purb->actual_length : 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200314 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200315 printf(" %02x", ((__u8 *) buffer) [i]);
316 printf("%s\n", i < len? "...": "");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200317 }
318 }
319#endif
320}
321
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200322/* just for debugging; prints non-empty branches of the int ed tree
323 * inclusive iso eds */
324void ep_print_int_eds(ohci_t *ohci, char *str)
325{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200326 int i, j;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200327 __u32 *ed_p;
328 for (i = 0; i < 32; i++) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200329 j = 5;
330 ed_p = &(ohci->hcca->int_table [i]);
331 if (*ed_p == 0)
332 continue;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200333 invalidate_dcache_ed(ed_p);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200334 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200335 while (*ed_p != 0 && j--) {
336 ed_t *ed = (ed_t *)m32_swap(ed_p);
Hans de Goede8d005ef2015-05-05 23:56:13 +0200337 invalidate_dcache_ed(ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200338 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200339 ed_p = &ed->hwNextED;
340 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200341 printf("\n");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200342 }
343}
344
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200345static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200346{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200347 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200348 label,
349 mask,
350 (mask & OHCI_INTR_MIE) ? " MIE" : "",
351 (mask & OHCI_INTR_OC) ? " OC" : "",
352 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
353 (mask & OHCI_INTR_FNO) ? " FNO" : "",
354 (mask & OHCI_INTR_UE) ? " UE" : "",
355 (mask & OHCI_INTR_RD) ? " RD" : "",
356 (mask & OHCI_INTR_SF) ? " SF" : "",
357 (mask & OHCI_INTR_WDH) ? " WDH" : "",
358 (mask & OHCI_INTR_SO) ? " SO" : ""
359 );
360}
361
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200362static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200363{
364 ed_t *edp = (ed_t *)value;
365
366 if (value) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200367 dbg("%s %08x", label, value);
Hans de Goede8d005ef2015-05-05 23:56:13 +0200368 invalidate_dcache_ed(edp);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200369 dbg("%08x", edp->hwINFO);
370 dbg("%08x", edp->hwTailP);
371 dbg("%08x", edp->hwHeadP);
372 dbg("%08x", edp->hwNextED);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200373 }
374}
375
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200376static char *hcfs2string(int state)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200377{
378 switch (state) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200379 case OHCI_USB_RESET: return "reset";
380 case OHCI_USB_RESUME: return "resume";
381 case OHCI_USB_OPER: return "operational";
382 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200383 }
384 return "?";
385}
386
387/* dump control and status registers */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200388static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200389{
390 struct ohci_regs *regs = controller->regs;
391 __u32 temp;
392
Becky Brucea5496a12010-06-30 13:05:44 -0500393 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200394 if (temp != 0x10)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200395 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200396
Becky Brucea5496a12010-06-30 13:05:44 -0500397 temp = ohci_readl(&regs->control);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200398 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200399 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
400 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
401 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200402 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200403 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
404 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
405 (temp & OHCI_CTRL_IE) ? " IE" : "",
406 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
407 temp & OHCI_CTRL_CBSR
408 );
409
Becky Brucea5496a12010-06-30 13:05:44 -0500410 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200411 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200412 (temp & OHCI_SOC) >> 16,
413 (temp & OHCI_OCR) ? " OCR" : "",
414 (temp & OHCI_BLF) ? " BLF" : "",
415 (temp & OHCI_CLF) ? " CLF" : "",
416 (temp & OHCI_HCR) ? " HCR" : ""
417 );
418
Becky Brucea5496a12010-06-30 13:05:44 -0500419 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
420 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200421
Becky Brucea5496a12010-06-30 13:05:44 -0500422 maybe_print_eds("ed_periodcurrent",
423 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200424
Becky Brucea5496a12010-06-30 13:05:44 -0500425 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
426 maybe_print_eds("ed_controlcurrent",
427 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200428
Becky Brucea5496a12010-06-30 13:05:44 -0500429 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
430 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200431
Becky Brucea5496a12010-06-30 13:05:44 -0500432 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200433}
434
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200435static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200436{
437 __u32 temp, ndp, i;
438
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200439 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200440 ndp = (temp & RH_A_NDP);
441#ifdef CONFIG_AT91C_PQFP_UHPBUG
442 ndp = (ndp == 2) ? 1:0;
443#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200444 if (verbose) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200445 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200446 ((temp & RH_A_POTPGT) >> 24) & 0xff,
447 (temp & RH_A_NOCP) ? " NOCP" : "",
448 (temp & RH_A_OCPM) ? " OCPM" : "",
449 (temp & RH_A_DT) ? " DT" : "",
450 (temp & RH_A_NPS) ? " NPS" : "",
451 (temp & RH_A_PSM) ? " PSM" : "",
452 ndp
453 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200454 temp = roothub_b(controller);
455 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200456 temp,
457 (temp & RH_B_PPCM) >> 16,
458 (temp & RH_B_DR)
459 );
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200460 temp = roothub_status(controller);
461 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200462 temp,
463 (temp & RH_HS_CRWE) ? " CRWE" : "",
464 (temp & RH_HS_OCIC) ? " OCIC" : "",
465 (temp & RH_HS_LPSC) ? " LPSC" : "",
466 (temp & RH_HS_DRWE) ? " DRWE" : "",
467 (temp & RH_HS_OCI) ? " OCI" : "",
468 (temp & RH_HS_LPS) ? " LPS" : ""
469 );
470 }
471
472 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200473 temp = roothub_portstatus(controller, i);
474 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 +0200475 i,
476 temp,
477 (temp & RH_PS_PRSC) ? " PRSC" : "",
478 (temp & RH_PS_OCIC) ? " OCIC" : "",
479 (temp & RH_PS_PSSC) ? " PSSC" : "",
480 (temp & RH_PS_PESC) ? " PESC" : "",
481 (temp & RH_PS_CSC) ? " CSC" : "",
482
483 (temp & RH_PS_LSDA) ? " LSDA" : "",
484 (temp & RH_PS_PPS) ? " PPS" : "",
485 (temp & RH_PS_PRS) ? " PRS" : "",
486 (temp & RH_PS_POCI) ? " POCI" : "",
487 (temp & RH_PS_PSS) ? " PSS" : "",
488
489 (temp & RH_PS_PES) ? " PES" : "",
490 (temp & RH_PS_CCS) ? " CCS" : ""
491 );
492 }
493}
494
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200495static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200496{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200497 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200498
499 /* dumps some of the state we know about */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200500 ohci_dump_status(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200501 if (verbose)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200502 ep_print_int_eds(controller, "hcca");
Hans de Goede8d005ef2015-05-05 23:56:13 +0200503 invalidate_dcache_hcca(controller->hcca);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200504 dbg("hcca frame #%04x", controller->hcca->frame_no);
505 ohci_dump_roothub(controller, 1);
Stefan Roese2596f5b2008-03-05 12:29:32 +0100506}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200507#endif /* DEBUG */
508
509/*-------------------------------------------------------------------------*
510 * Interface functions (URB)
511 *-------------------------------------------------------------------------*/
512
513/* get a transfer request */
514
Hans de Goede19d95d52015-05-05 23:56:08 +0200515int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
516 struct devrequest *setup)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200517{
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200518 ed_t *ed;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200519 urb_priv_t *purb_priv = urb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200520 int i, size = 0;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200521 struct usb_device *dev = urb->dev;
522 unsigned long pipe = urb->pipe;
523 void *buffer = urb->transfer_buffer;
524 int transfer_len = urb->transfer_buffer_length;
525 int interval = urb->interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200526
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200527 /* when controller's hung, permit only roothub cleanup attempts
528 * such as powering down ports */
529 if (ohci->disabled) {
530 err("sohci_submit_job: EPIPE");
531 return -1;
532 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +0200533
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200534 /* we're about to begin a new transaction here so mark the
535 * URB unfinished */
Zhang Wei4dae14c2007-06-06 10:08:14 +0200536 urb->finished = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200537
538 /* every endpoint has a ed, locate and fill it */
Hans de Goede19d95d52015-05-05 23:56:08 +0200539 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200540 if (!ed) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200541 err("sohci_submit_job: ENOMEM");
542 return -1;
543 }
544
545 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200546 switch (usb_pipetype(pipe)) {
547 case PIPE_BULK: /* one TD for every 4096 Byte */
548 size = (transfer_len - 1) / 4096 + 1;
549 break;
550 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
551 size = (transfer_len == 0)? 2:
552 (transfer_len - 1) / 4096 + 3;
553 break;
554 case PIPE_INTERRUPT: /* 1 TD */
555 size = 1;
556 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200557 }
558
Zhang Wei4dae14c2007-06-06 10:08:14 +0200559 ed->purb = urb;
560
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200561 if (size >= (N_URB_TD - 1)) {
562 err("need %d TDs, only have %d", size, N_URB_TD);
563 return -1;
564 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200565 purb_priv->pipe = pipe;
566
567 /* fill the private part of the URB */
568 purb_priv->length = size;
569 purb_priv->ed = ed;
570 purb_priv->actual_length = 0;
571
572 /* allocate the TDs */
573 /* note that td[0] was allocated in ep_add_ed */
574 for (i = 0; i < size; i++) {
Hans de Goede3c5497d2015-05-05 23:56:09 +0200575 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200576 if (!purb_priv->td[i]) {
577 purb_priv->length = i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200578 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200579 err("sohci_submit_job: ENOMEM");
580 return -1;
581 }
582 }
583
584 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200585 urb_free_priv(purb_priv);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200586 err("sohci_submit_job: EINVAL");
587 return -1;
588 }
589
590 /* link the ed into a chain if is not already */
591 if (ed->state != ED_OPER)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200592 ep_link(ohci, ed);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200593
594 /* fill the TDs and link it to the ed */
Hans de Goedec5613df2015-05-05 23:56:07 +0200595 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200596 setup, purb_priv, interval);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200597
598 return 0;
599}
600
601/*-------------------------------------------------------------------------*/
602
603#ifdef DEBUG
604/* tell us the current USB frame number */
Hans de Goedec5613df2015-05-05 23:56:07 +0200605static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200606{
Hans de Goede8d005ef2015-05-05 23:56:13 +0200607 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200608 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200609}
610#endif
611
612/*-------------------------------------------------------------------------*
613 * ED handling functions
614 *-------------------------------------------------------------------------*/
615
Zhang Wei4dae14c2007-06-06 10:08:14 +0200616/* search for the right branch to insert an interrupt ed into the int tree
617 * do some load ballancing;
618 * returns the branch and
619 * sets the interval to interval = 2^integer (ld (interval)) */
620
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200621static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200622{
623 int i, branch = 0;
624
625 /* search for the least loaded interrupt endpoint
626 * branch of all 32 branches
627 */
628 for (i = 0; i < 32; i++)
629 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
630 branch = i;
631
632 branch = branch % interval;
633 for (i = branch; i < 32; i += interval)
634 ohci->ohci_int_load [i] += load;
635
636 return branch;
637}
638
639/*-------------------------------------------------------------------------*/
640
641/* 2^int( ld (inter)) */
642
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200643static int ep_2_n_interval(int inter)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200644{
645 int i;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200646 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200647 return 1 << i;
648}
649
650/*-------------------------------------------------------------------------*/
651
652/* the int tree is a binary tree
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200653 * in order to process it sequentially the indexes of the branches have to
654 * be mapped the mapping reverses the bits of a word of num_bits length */
655static int ep_rev(int num_bits, int word)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200656{
657 int i, wout = 0;
658
659 for (i = 0; i < num_bits; i++)
660 wout |= (((word >> i) & 1) << (num_bits - i - 1));
661 return wout;
662}
663
664/*-------------------------------------------------------------------------*
665 * ED handling functions
666 *-------------------------------------------------------------------------*/
667
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200668/* link an ed into one of the HC chains */
669
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200670static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200671{
672 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200673 int int_branch;
674 int i;
675 int inter;
676 int interval;
677 int load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200678 __u32 *ed_p;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200679
680 ed->state = ED_OPER;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200681 ed->int_interval = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200682
683 switch (ed->type) {
684 case PIPE_CONTROL:
685 ed->hwNextED = 0;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200686 flush_dcache_ed(ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200687 if (ohci->ed_controltail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500688 ohci_writel(ed, &ohci->regs->ed_controlhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200689 else
690 ohci->ed_controltail->hwNextED =
691 m32_swap((unsigned long)ed);
692
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200693 ed->ed_prev = ohci->ed_controltail;
694 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
695 !ohci->ed_rm_list[1] && !ohci->sleeping) {
696 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500697 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200698 }
699 ohci->ed_controltail = edi;
700 break;
701
702 case PIPE_BULK:
703 ed->hwNextED = 0;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200704 flush_dcache_ed(ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200705 if (ohci->ed_bulktail == NULL)
Becky Brucea5496a12010-06-30 13:05:44 -0500706 ohci_writel(ed, &ohci->regs->ed_bulkhead);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200707 else
708 ohci->ed_bulktail->hwNextED =
709 m32_swap((unsigned long)ed);
710
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200711 ed->ed_prev = ohci->ed_bulktail;
712 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
713 !ohci->ed_rm_list[1] && !ohci->sleeping) {
714 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500715 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200716 }
717 ohci->ed_bulktail = edi;
718 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200719
720 case PIPE_INTERRUPT:
721 load = ed->int_load;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200722 interval = ep_2_n_interval(ed->int_period);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200723 ed->int_interval = interval;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200724 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200725 ed->int_branch = int_branch;
726
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200727 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200728 inter = 1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200729 for (ed_p = &(ohci->hcca->int_table[\
730 ep_rev(5, i) + int_branch]);
731 (*ed_p != 0) &&
732 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200733 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200734 inter = ep_rev(6,
735 ((ed_t *)ed_p)->int_interval);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200736 ed->hwNextED = *ed_p;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200737 flush_dcache_ed(ed);
Martin Krause4a8527e2007-08-21 12:40:34 +0200738 *ed_p = m32_swap((unsigned long)ed);
Hans de Goede8d005ef2015-05-05 23:56:13 +0200739 flush_dcache_hcca(ohci->hcca);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200740 }
741 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200742 }
743 return 0;
744}
745
746/*-------------------------------------------------------------------------*/
747
Zhang Wei4dae14c2007-06-06 10:08:14 +0200748/* scan the periodic table to find and unlink this ED */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200749static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
750 unsigned index, unsigned period)
Zhang Wei4dae14c2007-06-06 10:08:14 +0200751{
Hans de Goede8d005ef2015-05-05 23:56:13 +0200752 __maybe_unused unsigned long aligned_ed_p;
753
Zhang Wei4dae14c2007-06-06 10:08:14 +0200754 for (; index < NUM_INTS; index += period) {
755 __u32 *ed_p = &ohci->hcca->int_table [index];
756
757 /* ED might have been unlinked through another path */
758 while (*ed_p != 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200759 if (((struct ed *)
760 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei4dae14c2007-06-06 10:08:14 +0200761 *ed_p = ed->hwNextED;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200762#ifdef CONFIG_DM_USB
763 aligned_ed_p = (unsigned long)ed_p;
764 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
765 flush_dcache_range(aligned_ed_p,
766 aligned_ed_p + ARCH_DMA_MINALIGN);
767#endif
Zhang Wei4dae14c2007-06-06 10:08:14 +0200768 break;
769 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200770 ed_p = &(((struct ed *)
771 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200772 }
773 }
774}
775
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200776/* unlink an ed from one of the HC chains.
777 * just the link to the ed is unlinked.
778 * the link from the ed still points to another operational ed or 0
779 * so the HC can eventually finish the processing of the unlinked ed */
780
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200781static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200782{
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100783 volatile ed_t *ed = edi;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200784 int i;
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +0100785
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200786 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Hans de Goede8d005ef2015-05-05 23:56:13 +0200787 flush_dcache_ed(ed);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200788
789 switch (ed->type) {
790 case PIPE_CONTROL:
791 if (ed->ed_prev == NULL) {
792 if (!ed->hwNextED) {
793 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500794 ohci_writel(ohci->hc_control,
795 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200796 }
Becky Brucea5496a12010-06-30 13:05:44 -0500797 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200798 &ohci->regs->ed_controlhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200799 } else {
800 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200801 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200802 }
803 if (ohci->ed_controltail == ed) {
804 ohci->ed_controltail = ed->ed_prev;
805 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200806 ((ed_t *)m32_swap(
807 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200808 }
809 break;
810
811 case PIPE_BULK:
812 if (ed->ed_prev == NULL) {
813 if (!ed->hwNextED) {
814 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Brucea5496a12010-06-30 13:05:44 -0500815 ohci_writel(ohci->hc_control,
816 &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200817 }
Becky Brucea5496a12010-06-30 13:05:44 -0500818 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200819 &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200820 } else {
821 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200822 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200823 }
824 if (ohci->ed_bulktail == ed) {
825 ohci->ed_bulktail = ed->ed_prev;
826 } else {
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200827 ((ed_t *)m32_swap(
828 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200829 }
830 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +0200831
832 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200833 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei4dae14c2007-06-06 10:08:14 +0200834 for (i = ed->int_branch; i < 32; i += ed->int_interval)
835 ohci->ohci_int_load[i] -= ed->int_load;
836 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200837 }
838 ed->state = ED_UNLINK;
839 return 0;
840}
841
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200842/*-------------------------------------------------------------------------*/
843
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +0200844/* add/reinit an endpoint; this should be done once at the
845 * usb_set_configuration command, but the USB stack is a little bit
846 * stateless so we do it at every transaction if the state of the ed
847 * is ED_NEW then a dummy td is added and the state is changed to
848 * ED_UNLINK in all other cases the state is left unchanged the ed
849 * info fields are setted anyway even though most of them should not
850 * change
851 */
Hans de Goede19d95d52015-05-05 23:56:08 +0200852static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
853 unsigned long pipe, int interval, int load)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200854{
855 td_t *td;
856 ed_t *ed_ret;
857 volatile ed_t *ed;
858
Hans de Goede19d95d52015-05-05 23:56:08 +0200859 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200860 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200861
862 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
863 err("ep_add_ed: pending delete");
864 /* pending delete request */
865 return NULL;
866 }
867
868 if (ed->state == ED_NEW) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200869 /* dummy td; end of td list for ed */
Hans de Goede3c5497d2015-05-05 23:56:09 +0200870 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200871 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200872 ed->hwHeadP = ed->hwTailP;
873 ed->state = ED_UNLINK;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200874 ed->type = usb_pipetype(pipe);
Hans de Goede19d95d52015-05-05 23:56:08 +0200875 ohci_dev->ed_cnt++;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200876 }
877
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200878 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
879 | usb_pipeendpoint(pipe) << 7
880 | (usb_pipeisoc(pipe)? 0x8000: 0)
881 | (usb_pipecontrol(pipe)? 0: \
882 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanokc60795f2012-11-06 13:48:20 +0000883 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200884 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200885
Zhang Wei4dae14c2007-06-06 10:08:14 +0200886 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
887 ed->int_period = interval;
888 ed->int_load = load;
889 }
890
Hans de Goede8d005ef2015-05-05 23:56:13 +0200891 flush_dcache_ed(ed);
892
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200893 return ed_ret;
894}
895
896/*-------------------------------------------------------------------------*
897 * TD handling functions
898 *-------------------------------------------------------------------------*/
899
900/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
901
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200902static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200903 void *data, int len,
904 struct usb_device *dev, int index, urb_priv_t *urb_priv)
905{
906 volatile td_t *td, *td_pt;
907#ifdef OHCI_FILL_TRACE
908 int i;
909#endif
910
911 if (index > urb_priv->length) {
912 err("index > length");
913 return;
914 }
915 /* use this td as the next dummy */
916 td_pt = urb_priv->td [index];
917 td_pt->hwNextTD = 0;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200918 flush_dcache_td(td_pt);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200919
920 /* fill the old dummy TD */
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200921 td = urb_priv->td [index] =
922 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200923
924 td->ed = urb_priv->ed;
925 td->next_dl_td = NULL;
926 td->index = index;
927 td->data = (__u32)data;
928#ifdef OHCI_FILL_TRACE
Remy Bohmer48867202008-10-10 10:23:21 +0200929 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200930 for (i = 0; i < len; i++)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200931 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200932 printf("\n");
933 }
934#endif
935 if (!len)
936 data = 0;
937
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200938 td->hwINFO = m32_swap(info);
939 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200940 if (data)
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200941 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200942 else
943 td->hwBE = 0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200944
945 td->hwNextTD = m32_swap((unsigned long)td_pt);
Hans de Goede8d005ef2015-05-05 23:56:13 +0200946 flush_dcache_td(td);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200947
948 /* append to queue */
949 td->ed->hwTailP = td->hwNextTD;
Hans de Goede8d005ef2015-05-05 23:56:13 +0200950 flush_dcache_ed(td->ed);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200951}
952
953/*-------------------------------------------------------------------------*/
954
955/* prepare all TDs of a transfer */
956
Hans de Goedec5613df2015-05-05 23:56:07 +0200957static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
958 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200959 struct devrequest *setup, urb_priv_t *urb,
960 int interval)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200961{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200962 int data_len = transfer_len;
963 void *data;
964 int cnt = 0;
965 __u32 info = 0;
966 unsigned int toggle = 0;
967
Hans de Goede8d005ef2015-05-05 23:56:13 +0200968 flush_dcache_buffer(buffer, data_len);
969
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200970 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
971 * bits for reseting */
972 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200973 toggle = TD_T_TOGGLE;
974 } else {
975 toggle = TD_T_DATA0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200976 usb_settoggle(dev, usb_pipeendpoint(pipe),
977 usb_pipeout(pipe), 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200978 }
979 urb->td_cnt = 0;
980 if (data_len)
981 data = buffer;
982 else
983 data = 0;
984
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200985 switch (usb_pipetype(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200986 case PIPE_BULK:
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200987 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200988 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200989 while (data_len > 4096) {
990 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
991 data, 4096, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200992 data += 4096; data_len -= 4096; cnt++;
993 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200994 info = usb_pipeout(pipe)?
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200995 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer6f5794a2008-09-16 14:55:43 +0200996 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
997 data_len, dev, cnt, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +0200998 cnt++;
999
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001000 if (!ohci->sleeping) {
1001 /* start bulk list */
Becky Brucea5496a12010-06-30 13:05:44 -05001002 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001003 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001004 break;
1005
1006 case PIPE_CONTROL:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001007 /* Setup phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001008 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Hans de Goede8d005ef2015-05-05 23:56:13 +02001009 flush_dcache_buffer(setup, 8);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001010 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1011
1012 /* Optional Data phase */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001013 if (data_len > 0) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001014 info = usb_pipeout(pipe)?
1015 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1016 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001017 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001018 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001019 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001020
1021 /* Status phase */
Hans de Goedecae01cb2015-05-05 23:56:12 +02001022 info = (usb_pipeout(pipe) || data_len == 0) ?
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001023 TD_CC | TD_DP_IN | TD_T_DATA1:
1024 TD_CC | TD_DP_OUT | TD_T_DATA1;
1025 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1026
1027 if (!ohci->sleeping) {
1028 /* start Control list */
Becky Brucea5496a12010-06-30 13:05:44 -05001029 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001030 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001031 break;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001032
1033 case PIPE_INTERRUPT:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001034 info = usb_pipeout(urb->pipe)?
Zhang Wei4dae14c2007-06-06 10:08:14 +02001035 TD_CC | TD_DP_OUT | toggle:
1036 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001037 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001038 break;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001039 }
1040 if (urb->length != cnt)
1041 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1042}
1043
1044/*-------------------------------------------------------------------------*
1045 * Done List handling functions
1046 *-------------------------------------------------------------------------*/
1047
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001048/* calculate the transfer length and update the urb */
1049
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001050static void dl_transfer_length(td_t *td)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001051{
Wolfgang Denk6bc52ef2011-10-05 23:03:43 +02001052 __u32 tdBE, tdCBP;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001053 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001054
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001055 tdBE = m32_swap(td->hwBE);
1056 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001057
Remy Bohmer48867202008-10-10 10:23:21 +02001058 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001059 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1060 if (tdBE != 0) {
1061 if (td->hwCBP == 0)
1062 lurb_priv->actual_length += tdBE - td->data + 1;
1063 else
1064 lurb_priv->actual_length += tdCBP - td->data;
1065 }
1066 }
1067}
1068
1069/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001070static void check_status(td_t *td_list)
1071{
1072 urb_priv_t *lurb_priv = td_list->ed->purb;
1073 int urb_len = lurb_priv->length;
1074 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1075 int cc;
1076
1077 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1078 if (cc) {
1079 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1080
Hans de Goede8d005ef2015-05-05 23:56:13 +02001081 invalidate_dcache_ed(td_list->ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001082 if (*phwHeadP & m32_swap(0x1)) {
1083 if (lurb_priv &&
1084 ((td_list->index + 1) < urb_len)) {
1085 *phwHeadP =
1086 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1087 m32_swap(0xfffffff0)) |
1088 (*phwHeadP & m32_swap(0x2));
1089
1090 lurb_priv->td_cnt += urb_len -
1091 td_list->index - 1;
1092 } else
1093 *phwHeadP &= m32_swap(0xfffffff2);
Hans de Goede8d005ef2015-05-05 23:56:13 +02001094 flush_dcache_ed(td_list->ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001095 }
1096#ifdef CONFIG_MPC5200
1097 td_list->hwNextTD = 0;
Hans de Goede8d005ef2015-05-05 23:56:13 +02001098 flush_dcache_td(td_list);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001099#endif
1100 }
1101}
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001102
1103/* replies to the request have to be on a FIFO basis so
1104 * we reverse the reversed done-list */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001105static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001106{
1107 __u32 td_list_hc;
1108 td_t *td_rev = NULL;
1109 td_t *td_list = NULL;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001110
Hans de Goede8d005ef2015-05-05 23:56:13 +02001111 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001112 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001113 ohci->hcca->done_head = 0;
Hans de Goede8d005ef2015-05-05 23:56:13 +02001114 flush_dcache_hcca(ohci->hcca);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001115
1116 while (td_list_hc) {
1117 td_list = (td_t *)td_list_hc;
Hans de Goede8d005ef2015-05-05 23:56:13 +02001118 invalidate_dcache_td(td_list);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001119 check_status(td_list);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001120 td_list->next_dl_td = td_rev;
1121 td_rev = td_list;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001122 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001123 }
1124 return td_list;
1125}
1126
1127/*-------------------------------------------------------------------------*/
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001128/*-------------------------------------------------------------------------*/
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001129
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001130static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001131{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001132 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
Hans de Goede47976d22015-05-10 14:10:22 +02001133 urb->finished = 1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001134 else
1135 dbg("finish_urb: strange.., ED state %x, \n", status);
1136}
1137
1138/*
1139 * Used to take back a TD from the host controller. This would normally be
1140 * called from within dl_done_list, however it may be called directly if the
1141 * HC no longer sees the TD and it has not appeared on the donelist (after
1142 * two frames). This bug has been observed on ZF Micro systems.
1143 */
1144static int takeback_td(ohci_t *ohci, td_t *td_list)
1145{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001146 ed_t *ed;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001147 int cc;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001148 int stat = 0;
1149 /* urb_t *urb; */
1150 urb_priv_t *lurb_priv;
1151 __u32 tdINFO, edHeadP, edTailP;
1152
Hans de Goede8d005ef2015-05-05 23:56:13 +02001153 invalidate_dcache_td(td_list);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001154 tdINFO = m32_swap(td_list->hwINFO);
1155
1156 ed = td_list->ed;
1157 lurb_priv = ed->purb;
1158
1159 dl_transfer_length(td_list);
1160
1161 lurb_priv->td_cnt++;
1162
1163 /* error code of transfer */
1164 cc = TD_CC_GET(tdINFO);
1165 if (cc) {
1166 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1167 stat = cc_to_error[cc];
1168 }
1169
1170 /* see if this done list makes for all TD's of current URB,
1171 * and mark the URB finished if so */
1172 if (lurb_priv->td_cnt == lurb_priv->length)
1173 finish_urb(ohci, lurb_priv, ed->state);
1174
1175 dbg("dl_done_list: processing TD %x, len %x\n",
1176 lurb_priv->td_cnt, lurb_priv->length);
1177
Remy Bohmer48867202008-10-10 10:23:21 +02001178 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Hans de Goede8d005ef2015-05-05 23:56:13 +02001179 invalidate_dcache_ed(ed);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001180 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1181 edTailP = m32_swap(ed->hwTailP);
1182
1183 /* unlink eds if they are not busy */
1184 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1185 ep_unlink(ohci, ed);
1186 }
1187 return stat;
1188}
1189
1190static int dl_done_list(ohci_t *ohci)
1191{
1192 int stat = 0;
1193 td_t *td_list = dl_reverse_done_list(ohci);
1194
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001195 while (td_list) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001196 td_t *td_next = td_list->next_dl_td;
1197 stat = takeback_td(ohci, td_list);
1198 td_list = td_next;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001199 }
1200 return stat;
1201}
1202
1203/*-------------------------------------------------------------------------*
1204 * Virtual Root Hub
1205 *-------------------------------------------------------------------------*/
1206
Stephen Warreneb838e72014-02-13 21:15:18 -07001207#include <usbroothubdes.h>
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001208
1209/* Hub class-specific descriptor is constructed dynamically */
1210
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001211/*-------------------------------------------------------------------------*/
1212
1213#define OK(x) len = (x); break
1214#ifdef DEBUG
Becky Brucea5496a12010-06-30 13:05:44 -05001215#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goedec5613df2015-05-05 23:56:07 +02001216 &ohci->regs->roothub.status); }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001217#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goedec5613df2015-05-05 23:56:07 +02001218 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001219#else
Hans de Goedec5613df2015-05-05 23:56:07 +02001220#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Brucea5496a12010-06-30 13:05:44 -05001221#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goedec5613df2015-05-05 23:56:07 +02001222 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001223#endif
Hans de Goedec5613df2015-05-05 23:56:07 +02001224#define RD_RH_STAT roothub_status(ohci)
1225#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001226
1227/* request to virtual root hub */
1228
1229int rh_check_port_status(ohci_t *controller)
1230{
1231 __u32 temp, ndp, i;
1232 int res;
1233
1234 res = -1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001235 temp = roothub_a(controller);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001236 ndp = (temp & RH_A_NDP);
1237#ifdef CONFIG_AT91C_PQFP_UHPBUG
1238 ndp = (ndp == 2) ? 1:0;
1239#endif
1240 for (i = 0; i < ndp; i++) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001241 temp = roothub_portstatus(controller, i);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001242 /* check for a device disconnect */
1243 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1244 (RH_PS_PESC | RH_PS_CSC)) &&
1245 ((temp & RH_PS_CCS) == 0)) {
1246 res = i;
1247 break;
1248 }
1249 }
1250 return res;
1251}
1252
Hans de Goedec5613df2015-05-05 23:56:07 +02001253static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1254 unsigned long pipe, void *buffer, int transfer_len,
1255 struct devrequest *cmd)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001256{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001257 void *data = buffer;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001258 int leni = transfer_len;
1259 int len = 0;
1260 int stat = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001261 __u16 bmRType_bReq;
1262 __u16 wValue;
1263 __u16 wIndex;
1264 __u16 wLength;
Troy Kiskyf1273f12012-08-11 14:49:09 -07001265 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasut5f6aa032011-10-07 02:00:13 +02001266
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001267#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001268pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001269 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001270#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001271 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001272#endif
Remy Bohmer48867202008-10-10 10:23:21 +02001273 if (usb_pipeint(pipe)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001274 info("Root-Hub submit IRQ: NOT implemented");
1275 return 0;
1276 }
1277
1278 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001279 wValue = le16_to_cpu(cmd->value);
1280 wIndex = le16_to_cpu(cmd->index);
1281 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001282
1283 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1284 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1285
1286 switch (bmRType_bReq) {
1287 /* Request Destination:
1288 without flags: Device,
1289 RH_INTERFACE: interface,
1290 RH_ENDPOINT: endpoint,
1291 RH_CLASS means HUB here,
1292 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1293 */
1294
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001295 case RH_GET_STATUS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001296 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001297 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001298 case RH_GET_STATUS | RH_INTERFACE:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001299 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001300 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001301 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001302 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001303 OK(2);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001304 case RH_GET_STATUS | RH_CLASS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001305 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001306 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001307 OK(4);
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001308 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001309 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001310 OK(4);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001311
1312 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1313 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001314 case (RH_ENDPOINT_STALL):
1315 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001316 }
1317 break;
1318
1319 case RH_CLEAR_FEATURE | RH_CLASS:
1320 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001321 case RH_C_HUB_LOCAL_POWER:
1322 OK(0);
1323 case (RH_C_HUB_OVER_CURRENT):
1324 WR_RH_STAT(RH_HS_OCIC);
1325 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001326 }
1327 break;
1328
1329 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1330 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001331 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1332 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1333 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1334 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1335 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1336 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1337 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1338 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001339 }
1340 break;
1341
1342 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1343 switch (wValue) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001344 case (RH_PORT_SUSPEND):
1345 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1346 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1347 if (RD_RH_PORTSTAT & RH_PS_CCS)
1348 WR_RH_PORTSTAT(RH_PS_PRS);
1349 OK(0);
1350 case (RH_PORT_POWER):
1351 WR_RH_PORTSTAT(RH_PS_PPS);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001352 OK(0);
1353 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1354 if (RD_RH_PORTSTAT & RH_PS_CCS)
1355 WR_RH_PORTSTAT(RH_PS_PES);
1356 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001357 }
1358 break;
1359
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001360 case RH_SET_ADDRESS:
Hans de Goedec5613df2015-05-05 23:56:07 +02001361 ohci->rh.devnum = wValue;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001362 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001363
1364 case RH_GET_DESCRIPTOR:
1365 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001366 case (0x01): /* device descriptor */
1367 len = min_t(unsigned int,
1368 leni,
1369 min_t(unsigned int,
1370 sizeof(root_hub_dev_des),
1371 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001372 databuf = root_hub_dev_des; OK(len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001373 case (0x02): /* configuration descriptor */
1374 len = min_t(unsigned int,
1375 leni,
1376 min_t(unsigned int,
1377 sizeof(root_hub_config_des),
1378 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001379 databuf = root_hub_config_des; OK(len);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001380 case (0x03): /* string descriptors */
1381 if (wValue == 0x0300) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001382 len = min_t(unsigned int,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001383 leni,
1384 min_t(unsigned int,
1385 sizeof(root_hub_str_index0),
1386 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001387 databuf = root_hub_str_index0;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001388 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001389 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001390 if (wValue == 0x0301) {
1391 len = min_t(unsigned int,
1392 leni,
1393 min_t(unsigned int,
1394 sizeof(root_hub_str_index1),
1395 wLength));
Troy Kiskyf1273f12012-08-11 14:49:09 -07001396 databuf = root_hub_str_index1;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001397 OK(len);
1398 }
1399 default:
1400 stat = USB_ST_STALLED;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001401 }
1402 break;
1403
1404 case RH_GET_DESCRIPTOR | RH_CLASS:
1405 {
Hans de Goedec5613df2015-05-05 23:56:07 +02001406 __u32 temp = roothub_a(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001407
Troy Kiskyf1273f12012-08-11 14:49:09 -07001408 databuf[0] = 9; /* min length; */
1409 databuf[1] = 0x29;
1410 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001411#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kiskyf1273f12012-08-11 14:49:09 -07001412 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001413#endif
Troy Kiskyf1273f12012-08-11 14:49:09 -07001414 databuf[3] = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001415 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001416 databuf[3] |= 0x1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001417 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001418 databuf[3] |= 0x10;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001419 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kiskyf1273f12012-08-11 14:49:09 -07001420 databuf[3] |= 0x8;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001421
Troy Kiskyf1273f12012-08-11 14:49:09 -07001422 databuf[4] = 0;
1423 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1424 databuf[6] = 0;
Hans de Goedec5613df2015-05-05 23:56:07 +02001425 temp = roothub_b(ohci);
Troy Kiskyf1273f12012-08-11 14:49:09 -07001426 databuf[7] = temp & RH_B_DR;
1427 if (databuf[2] < 7) {
1428 databuf[8] = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001429 } else {
Troy Kiskyf1273f12012-08-11 14:49:09 -07001430 databuf[0] += 2;
1431 databuf[8] = (temp & RH_B_DR) >> 8;
1432 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001433 }
1434
1435 len = min_t(unsigned int, leni,
Troy Kiskyf1273f12012-08-11 14:49:09 -07001436 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001437 OK(len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001438 }
1439
Marek Vasut5f6aa032011-10-07 02:00:13 +02001440 case RH_GET_CONFIGURATION:
Troy Kiskyf1273f12012-08-11 14:49:09 -07001441 databuf[0] = 0x01;
Marek Vasut5f6aa032011-10-07 02:00:13 +02001442 OK(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001443
Marek Vasut5f6aa032011-10-07 02:00:13 +02001444 case RH_SET_CONFIGURATION:
1445 WR_RH_STAT(0x10000);
1446 OK(0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001447
1448 default:
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001449 dbg("unsupported root hub command");
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001450 stat = USB_ST_STALLED;
1451 }
1452
1453#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001454 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001455#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001456 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001457#endif
1458
1459 len = min_t(int, len, leni);
Troy Kiskyf1273f12012-08-11 14:49:09 -07001460 if (data != databuf)
1461 memcpy(data, databuf, len);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001462 dev->act_len = len;
1463 dev->status = stat;
1464
1465#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001466 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001467 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001468#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001469 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001470#endif
1471
1472 return stat;
1473}
1474
1475/*-------------------------------------------------------------------------*/
1476
1477/* common code for handling submit messages - used for all but root hub */
1478/* accesses. */
Hans de Goedec5613df2015-05-05 23:56:07 +02001479static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1480 unsigned long pipe, void *buffer, int transfer_len,
1481 struct devrequest *setup, int interval)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001482{
1483 int stat = 0;
1484 int maxsize = usb_maxpacket(dev, pipe);
1485 int timeout;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001486 urb_priv_t *urb;
1487
1488 urb = malloc(sizeof(urb_priv_t));
1489 memset(urb, 0, sizeof(urb_priv_t));
1490
1491 urb->dev = dev;
1492 urb->pipe = pipe;
1493 urb->transfer_buffer = buffer;
1494 urb->transfer_buffer_length = transfer_len;
1495 urb->interval = interval;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001496
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001497#ifdef DEBUG
Zhang Wei4dae14c2007-06-06 10:08:14 +02001498 urb->actual_length = 0;
Hans de Goedec5613df2015-05-05 23:56:07 +02001499 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001500 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001501#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001502 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001503#endif
1504 if (!maxsize) {
1505 err("submit_common_message: pipesize for pipe %lx is zero",
1506 pipe);
1507 return -1;
1508 }
1509
Hans de Goede19d95d52015-05-05 23:56:08 +02001510 if (sohci_submit_job(ohci, &ohci->ohci_dev, urb, setup) < 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001511 err("sohci_submit_job failed");
1512 return -1;
1513 }
1514
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001515#if 0
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001516 mdelay(10);
Hans de Goedec5613df2015-05-05 23:56:07 +02001517 /* ohci_dump_status(ohci); */
Markus Klotzbuecherae3b7702006-11-27 11:46:46 +01001518#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001519
Simon Glass96820a32011-02-07 14:42:16 -08001520 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001521
1522 /* wait for it to complete */
1523 for (;;) {
1524 /* check whether the controller is done */
Hans de Goedec5613df2015-05-05 23:56:07 +02001525 stat = hc_interrupt(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001526 if (stat < 0) {
1527 stat = USB_ST_CRC_ERR;
1528 break;
1529 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001530
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001531 /* NOTE: since we are not interrupt driven in U-Boot and always
1532 * handle only one URB at a time, we cannot assume the
1533 * transaction finished on the first successful return from
1534 * hc_interrupt().. unless the flag for current URB is set,
1535 * meaning that all TD's to/from device got actually
1536 * transferred and processed. If the current URB is not
1537 * finished we need to re-iterate this loop so as
1538 * hc_interrupt() gets called again as there needs to be some
1539 * more TD's to process still */
Zhang Wei4dae14c2007-06-06 10:08:14 +02001540 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001541 /* 0xff is returned for an SF-interrupt */
1542 break;
1543 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001544
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001545 if (--timeout) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001546 mdelay(1);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001547 if (!urb->finished)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001548 dbg("*");
Zhang Wei4dae14c2007-06-06 10:08:14 +02001549
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001550 } else {
Hans de Goedefa5b9ba2015-05-05 23:56:14 +02001551 if (!usb_pipeint(pipe))
1552 err("CTL:TIMEOUT ");
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001553 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001554 urb->finished = 1;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001555 stat = USB_ST_CRC_ERR;
1556 break;
1557 }
1558 }
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001559
1560 dev->status = stat;
Mateusz Kulikowski522c9562013-10-23 20:26:27 +02001561 dev->act_len = urb->actual_length;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001562
Hans de Goede8d005ef2015-05-05 23:56:13 +02001563 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1564 invalidate_dcache_buffer(buffer, dev->act_len);
1565
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001566#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001567 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001568 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001569#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001570 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001571#endif
Hans de Goede47976d22015-05-10 14:10:22 +02001572 urb_free_priv(urb);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001573 return 0;
1574}
1575
1576/* submit routines called from usb.c */
1577int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1578 int transfer_len)
1579{
1580 info("submit_bulk_msg");
Hans de Goedec5613df2015-05-05 23:56:07 +02001581 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1582 NULL, 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001583}
1584
Hans de Goedec5613df2015-05-05 23:56:07 +02001585int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1586 int transfer_len, int interval)
1587{
1588 info("submit_int_msg");
1589 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1590 interval);
1591}
1592
1593static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1594 unsigned long pipe, void *buffer, int transfer_len,
1595 struct devrequest *setup)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001596{
1597 int maxsize = usb_maxpacket(dev, pipe);
1598
1599 info("submit_control_msg");
1600#ifdef DEBUG
Hans de Goedec5613df2015-05-05 23:56:07 +02001601 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001602 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001603#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001604 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001605#endif
1606 if (!maxsize) {
1607 err("submit_control_message: pipesize for pipe %lx is zero",
1608 pipe);
1609 return -1;
1610 }
Hans de Goedec5613df2015-05-05 23:56:07 +02001611 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1612 ohci->rh.dev = dev;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001613 /* root hub - redirect */
Hans de Goedec5613df2015-05-05 23:56:07 +02001614 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1615 transfer_len, setup);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001616 }
1617
Hans de Goedec5613df2015-05-05 23:56:07 +02001618 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1619 setup, 0);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001620}
1621
1622/*-------------------------------------------------------------------------*
1623 * HC functions
1624 *-------------------------------------------------------------------------*/
1625
1626/* reset the HC and BUS */
1627
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001628static int hc_reset(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001629{
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001630#ifdef CONFIG_PCI_EHCI_DEVNO
1631 pci_dev_t pdev;
1632#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001633 int timeout = 30;
1634 int smm_timeout = 50; /* 0,5 sec */
1635
1636 dbg("%s\n", __FUNCTION__);
1637
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001638#ifdef CONFIG_PCI_EHCI_DEVNO
1639 /*
1640 * Some multi-function controllers (e.g. ISP1562) allow root hub
1641 * resetting via EHCI registers only.
1642 */
1643 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1644 if (pdev != -1) {
1645 u32 base;
1646 int timeout = 1000;
1647
1648 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Brucea5496a12010-06-30 13:05:44 -05001649 base += EHCI_USBCMD_OFF;
1650 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001651
Becky Brucea5496a12010-06-30 13:05:44 -05001652 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonove90fb6a2008-09-04 11:19:05 +02001653 if (timeout-- <= 0) {
1654 printf("USB RootHub reset timed out!");
1655 break;
1656 }
1657 udelay(1);
1658 }
1659 } else
1660 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1661#endif
Becky Brucea5496a12010-06-30 13:05:44 -05001662 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1663 /* SMM owns the HC, request ownership */
1664 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001665 info("USB HC TakeOver from SMM");
Becky Brucea5496a12010-06-30 13:05:44 -05001666 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001667 mdelay(10);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001668 if (--smm_timeout == 0) {
1669 err("USB HC TakeOver failed!");
1670 return -1;
1671 }
1672 }
1673 }
1674
1675 /* Disable HC interrupts */
Becky Brucea5496a12010-06-30 13:05:44 -05001676 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001677
1678 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1679 ohci->slot_name,
Becky Brucea5496a12010-06-30 13:05:44 -05001680 ohci_readl(&ohci->regs->control));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001681
1682 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher53e336e2006-11-27 11:43:09 +01001683 ohci->hc_control = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001684 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001685
1686 /* HC Reset requires max 10 us delay */
Becky Brucea5496a12010-06-30 13:05:44 -05001687 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1688 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001689 if (--timeout == 0) {
1690 err("USB HC reset timed out!");
1691 return -1;
1692 }
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001693 udelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001694 }
1695 return 0;
1696}
1697
1698/*-------------------------------------------------------------------------*/
1699
1700/* Start an OHCI controller, set the BUS operational
1701 * enable interrupts
1702 * connect the virtual root hub */
1703
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001704static int hc_start(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001705{
1706 __u32 mask;
1707 unsigned int fminterval;
1708
1709 ohci->disabled = 1;
1710
1711 /* Tell the controller where the control and bulk lists are
1712 * The lists are empty now. */
1713
Becky Brucea5496a12010-06-30 13:05:44 -05001714 ohci_writel(0, &ohci->regs->ed_controlhead);
1715 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001716
Becky Brucea5496a12010-06-30 13:05:44 -05001717 ohci_writel((__u32)ohci->hcca,
1718 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001719
1720 fminterval = 0x2edf;
Becky Brucea5496a12010-06-30 13:05:44 -05001721 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001722 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Brucea5496a12010-06-30 13:05:44 -05001723 ohci_writel(fminterval, &ohci->regs->fminterval);
1724 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001725
1726 /* start controller operations */
1727 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1728 ohci->disabled = 0;
Becky Brucea5496a12010-06-30 13:05:44 -05001729 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001730
1731 /* disable all interrupts */
1732 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1733 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1734 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Brucea5496a12010-06-30 13:05:44 -05001735 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001736 /* clear all interrupts */
1737 mask &= ~OHCI_INTR_MIE;
Becky Brucea5496a12010-06-30 13:05:44 -05001738 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001739 /* Choose the interrupts we care about now - but w/o MIE */
1740 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Brucea5496a12010-06-30 13:05:44 -05001741 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001742
1743#ifdef OHCI_USE_NPS
1744 /* required for AMD-756 and some Mac platforms */
Becky Brucea5496a12010-06-30 13:05:44 -05001745 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001746 &ohci->regs->roothub.a);
Becky Brucea5496a12010-06-30 13:05:44 -05001747 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001748#endif /* OHCI_USE_NPS */
1749
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001750 /* connect the virtual root hub */
1751 ohci->rh.devnum = 0;
1752
1753 return 0;
1754}
1755
1756/*-------------------------------------------------------------------------*/
1757
1758/* an interrupt happens */
1759
Hans de Goedec5613df2015-05-05 23:56:07 +02001760static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001761{
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001762 struct ohci_regs *regs = ohci->regs;
1763 int ints;
1764 int stat = -1;
1765
Hans de Goede8d005ef2015-05-05 23:56:13 +02001766 invalidate_dcache_hcca(ohci->hcca);
1767
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001768 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001769 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001770 ints = OHCI_INTR_WDH;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001771 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001772 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001773 if (ints == ~(u32)0) {
1774 ohci->disabled++;
1775 err("%s device removed!", ohci->slot_name);
1776 return -1;
1777 } else {
Becky Brucea5496a12010-06-30 13:05:44 -05001778 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001779 if (ints == 0) {
1780 dbg("hc_interrupt: returning..\n");
1781 return 0xff;
1782 }
1783 }
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001784 }
Markus Klotzbuecherae79f602007-03-26 11:21:05 +02001785
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001786 /* dbg("Interrupt: %x frame: %x", ints,
1787 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001788
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001789 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001790 stat = 0xff;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001791
1792 if (ints & OHCI_INTR_UE) {
1793 ohci->disabled++;
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001794 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001795 ohci->slot_name);
1796 /* e.g. due to PCI Master/Target Abort */
1797
1798#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001799 ohci_dump(ohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001800#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001801 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001802#endif
1803 /* FIXME: be optimistic, hope that bug won't repeat often. */
1804 /* Make some non-interrupt context restart the controller. */
1805 /* Count and limit the retries though; either hardware or */
1806 /* software errors can go forever... */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001807 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001808 return -1;
1809 }
1810
1811 if (ints & OHCI_INTR_WDH) {
Hans de Goede8f761f02015-05-10 14:10:24 +02001812 ohci_mdelay(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001813 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1814 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goedec5613df2015-05-05 23:56:07 +02001815 stat = dl_done_list(ohci);
Becky Brucea5496a12010-06-30 13:05:44 -05001816 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1817 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001818 }
1819
1820 if (ints & OHCI_INTR_SO) {
1821 dbg("USB Schedule overrun\n");
Becky Brucea5496a12010-06-30 13:05:44 -05001822 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001823 stat = -1;
1824 }
1825
1826 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1827 if (ints & OHCI_INTR_SF) {
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001828 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001829 mdelay(1);
Becky Brucea5496a12010-06-30 13:05:44 -05001830 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001831 if (ohci->ed_rm_list[frame] != NULL)
Becky Brucea5496a12010-06-30 13:05:44 -05001832 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001833 stat = 0xff;
1834 }
1835
Becky Brucea5496a12010-06-30 13:05:44 -05001836 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001837 return stat;
1838}
1839
1840/*-------------------------------------------------------------------------*/
1841
1842/*-------------------------------------------------------------------------*/
1843
1844/* De-allocate all resources.. */
1845
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001846static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001847{
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001848 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001849
1850 if (!ohci->disabled)
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001851 hc_reset(ohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001852}
1853
1854/*-------------------------------------------------------------------------*/
1855
1856/*
1857 * low level initalisation routine, called from usb.c
1858 */
1859static char ohci_inited = 0;
1860
Troy Kisky06d513e2013-10-10 15:27:56 -07001861int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001862{
Zhang Wei4dae14c2007-06-06 10:08:14 +02001863#ifdef CONFIG_PCI_OHCI
1864 pci_dev_t pdev;
1865#endif
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001866
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001867#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001868 /* cpu dependant init */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001869 if (usb_cpu_init())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001870 return -1;
1871#endif
1872
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001873#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001874 /* board dependant init */
Mateusz Zalega16297cf2013-10-04 19:22:26 +02001875 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001876 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001877#endif
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001878 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001879
1880 /* align the storage */
1881 if ((__u32)&ghcca[0] & 0xff) {
1882 err("HCCA not aligned!!");
1883 return -1;
1884 }
Hans de Goede26548bb2015-05-05 23:56:10 +02001885 gohci.hcca = &ghcca[0];
1886 info("aligned ghcca %p", gohci.hcca);
1887 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001888
1889 gohci.disabled = 1;
1890 gohci.sleeping = 0;
1891 gohci.irq = -1;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001892#ifdef CONFIG_PCI_OHCI
Sergei Poselenov477434c2008-05-22 01:15:53 +02001893 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei4dae14c2007-06-06 10:08:14 +02001894
1895 if (pdev != -1) {
1896 u16 vid, did;
1897 u32 base;
1898 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1899 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1900 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1901 vid, did, (pdev >> 16) & 0xff,
1902 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1903 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1904 printf("OHCI regs address 0x%08x\n", base);
1905 gohci.regs = (struct ohci_regs *)base;
1906 } else
1907 return -1;
1908#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001909 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei4dae14c2007-06-06 10:08:14 +02001910#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001911
1912 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001913 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001914
1915 if (hc_reset (&gohci) < 0) {
1916 hc_release_ohci (&gohci);
1917 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001918#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001919 /* board dependant cleanup */
Mateusz Zalega16297cf2013-10-04 19:22:26 +02001920 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001921#endif
1922
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001923#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001924 /* cpu dependant cleanup */
Markus Klotzbuecherddf83a22006-05-30 16:56:14 +02001925 usb_cpu_init_fail();
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001926#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001927 return -1;
1928 }
1929
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001930 if (hc_start(&gohci) < 0) {
1931 err("can't start usb-%s", gohci.slot_name);
1932 hc_release_ohci(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001933 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001934#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001935 /* board dependant cleanup */
1936 usb_board_stop();
1937#endif
1938
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001939#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001940 /* cpu dependant cleanup */
1941 usb_cpu_stop();
1942#endif
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001943 return -1;
1944 }
1945
1946#ifdef DEBUG
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001947 ohci_dump(&gohci, 1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001948#else
Hans de Goede8f761f02015-05-10 14:10:24 +02001949 ohci_mdelay(1);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001950#endif
1951 ohci_inited = 1;
1952 return 0;
1953}
1954
Lucas Stachc7e3b2b2012-09-26 00:14:34 +02001955int usb_lowlevel_stop(int index)
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001956{
1957 /* this gets called really early - before the controller has */
1958 /* even been initialized! */
1959 if (!ohci_inited)
1960 return 0;
1961 /* TODO release any interrupts, etc. */
1962 /* call hc_release_ohci() here ? */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001963 hc_reset(&gohci);
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001964
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001965#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001966 /* board dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001967 if (usb_board_stop())
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001968 return -1;
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001969#endif
1970
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001971#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001972 /* cpu dependant cleanup */
Remy Bohmer6f5794a2008-09-16 14:55:43 +02001973 if (usb_cpu_stop())
Markus Klotzbuecher24e37642006-05-23 10:33:11 +02001974 return -1;
1975#endif
Remy Bohmereba1f2f2008-08-20 11:22:02 +02001976 /* This driver is no longer initialised. It needs a new low-level
1977 * init (board/cpu) before it can be used again. */
1978 ohci_inited = 0;
Markus Klotzbuecher3e326ec2006-05-22 16:33:54 +02001979 return 0;
1980}
Hans de Goedec5613df2015-05-05 23:56:07 +02001981
1982int submit_control_msg(struct usb_device *dev, unsigned long pipe,
1983 void *buffer, int transfer_len, struct devrequest *setup)
1984{
1985 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
1986 transfer_len, setup);
1987}