blob: bd498da096a7e69f88895f06a38209afa284f9eb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002/*
Zhang Wei8d15efa2007-06-06 10:08:14 +02003 * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
4 *
5 * Interrupt support is added. Now, it has been tested
6 * on ULI1575 chip and works well with USB keyboard.
7 *
8 * (C) Copyright 2007
9 * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020010 *
11 * (C) Copyright 2003
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020012 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020013 *
14 * Note: Much of this code has been derived from Linux 2.4
15 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
16 * (C) Copyright 2000-2002 David Brownell
17 *
18 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
19 * ebenard@eukrea.com - based on s3c24x0's driver
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020020 */
21/*
22 * IMPORTANT NOTES
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020023 * 1 - Read doc/README.generic_usb_ohci
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020024 * 2 - this driver is intended for use with USB Mass Storage Devices
Zhang Wei8d15efa2007-06-06 10:08:14 +020025 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020026 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020027 * to activate workaround for bug #41 or this driver will NOT work!
28 */
29
30#include <common.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020031#include <asm/byteorder.h>
Hans de Goede8a62c502015-05-10 14:10:25 +020032#include <dm.h>
33#include <errno.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020034
35#if defined(CONFIG_PCI_OHCI)
Zhang Wei8d15efa2007-06-06 10:08:14 +020036# include <pci.h>
Sergei Poselenov0b4570f2008-05-22 01:15:53 +020037#if !defined(CONFIG_PCI_OHCI_DEVNO)
38#define CONFIG_PCI_OHCI_DEVNO 0
39#endif
Markus Klotzbuecheraa219212006-05-30 16:56:14 +020040#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020041
42#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060043#include <memalign.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020044#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020045
46#include "ohci.h"
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020047
Wolfgang Denkdcac38b2007-11-19 12:59:14 +010048#ifdef CONFIG_AT91RM9200
49#include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
50#endif
51
Masahiro Yamada7a5b2922014-11-06 14:59:35 +090052#if defined(CONFIG_CPU_ARM920T) || \
Zhang Wei8d15efa2007-06-06 10:08:14 +020053 defined(CONFIG_PCI_OHCI) || \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020055# define OHCI_USE_NPS /* force NoPowerSwitching mode */
56#endif
57
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020058#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010059#undef DEBUG
60#undef SHOW_INFO
61#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020062
63/* For initializing controller (mask in an HCFS mode too) */
64#define OHCI_CONTROL_INIT \
65 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
66
Heiko Schocherfe410a42019-07-16 10:49:03 +020067#if !CONFIG_IS_ENABLED(DM_USB)
Zhang Wei8d15efa2007-06-06 10:08:14 +020068#ifdef CONFIG_PCI_OHCI
69static struct pci_device_id ohci_pci_ids[] = {
70 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saadae3b28422007-09-17 17:04:47 +020071 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew923f43a2008-01-11 20:42:58 -060072 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei8d15efa2007-06-06 10:08:14 +020073 /* Please add supported PCI OHCI controller ids here */
74 {0, 0}
75};
76#endif
Heiko Schocherfe410a42019-07-16 10:49:03 +020077#endif
Zhang Wei8d15efa2007-06-06 10:08:14 +020078
Yuri Tikhonov11af42c2008-09-04 11:19:05 +020079#ifdef CONFIG_PCI_EHCI_DEVNO
80static struct pci_device_id ehci_pci_ids[] = {
81 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
82 /* Please add supported PCI EHCI controller ids here */
83 {0, 0}
84};
85#endif
86
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020087#ifdef DEBUG
88#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
89#else
Remy Bohmer172a1132008-09-16 14:55:43 +020090#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020091#endif /* DEBUG */
92#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020093#ifdef SHOW_INFO
94#define info(format, arg...) printf("INFO: " format "\n", ## arg)
95#else
Remy Bohmer172a1132008-09-16 14:55:43 +020096#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020097#endif
98
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200100# define m16_swap(x) cpu_to_be16(x)
101# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +0100102#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200103# define m16_swap(x) cpu_to_le16(x)
104# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200106
Wu, Joshc3857142015-07-27 11:40:18 +0800107/* We really should do proper cache flushing everywhere */
Hans de Goede8e41f662015-05-05 23:56:13 +0200108#define flush_dcache_buffer(addr, size) \
109 flush_dcache_range((unsigned long)(addr), \
110 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
111#define invalidate_dcache_buffer(addr, size) \
112 invalidate_dcache_range((unsigned long)(addr), \
113 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
Hans de Goede8e41f662015-05-05 23:56:13 +0200114
115/* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
116#define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
117#define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
118#define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
119#define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
120#define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
121#define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
122#define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
123#define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
124
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100125#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goeded6cc1d12015-05-10 14:10:24 +0200126/*
127 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
128 * them around when building for older boards not yet converted to the dm
129 * just in case (to avoid regressions), for dm this turns them into nops.
130 */
131#define ohci_mdelay(x)
132#else
133#define ohci_mdelay(x) mdelay(x)
134#endif
135
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100136#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200137/* global ohci_t */
138static ohci_t gohci;
139/* this must be aligned to a 256 byte boundary */
140struct ohci_hcca ghcca[1];
Hans de Goede8a62c502015-05-10 14:10:25 +0200141#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200142
Hans de Goede516c9872015-05-05 23:56:11 +0200143/* mapping of the OHCI CC status to error codes */
144static int cc_to_error[16] = {
145 /* No Error */ 0,
146 /* CRC Error */ USB_ST_CRC_ERR,
147 /* Bit Stuff */ USB_ST_BIT_ERR,
148 /* Data Togg */ USB_ST_CRC_ERR,
149 /* Stall */ USB_ST_STALLED,
150 /* DevNotResp */ -1,
151 /* PIDCheck */ USB_ST_BIT_ERR,
152 /* UnExpPID */ USB_ST_BIT_ERR,
153 /* DataOver */ USB_ST_BUF_ERR,
154 /* DataUnder */ USB_ST_BUF_ERR,
155 /* reservd */ -1,
156 /* reservd */ -1,
157 /* BufferOver */ USB_ST_BUF_ERR,
158 /* BuffUnder */ USB_ST_BUF_ERR,
159 /* Not Access */ -1,
160 /* Not Access */ -1
161};
162
163static const char *cc_to_string[16] = {
164 "No Error",
165 "CRC: Last data packet from endpoint contained a CRC error.",
166 "BITSTUFFING: Last data packet from endpoint contained a bit " \
167 "stuffing violation",
168 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
169 "that did not match the expected value.",
170 "STALL: TD was moved to the Done Queue because the endpoint returned" \
171 " a STALL PID",
172 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
173 "not provide a handshake (OUT)",
174 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
175 "(IN) or handshake (OUT)",
176 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
177 "value is not defined.",
178 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
179 "either the size of the maximum data packet allowed\n" \
180 "from the endpoint (found in MaximumPacketSize field\n" \
181 "of ED) or the remaining buffer size.",
182 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
183 "and that amount was not sufficient to fill the\n" \
184 "specified buffer",
185 "reserved1",
186 "reserved2",
187 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
188 "than it could be written to system memory",
189 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
190 "system memory fast enough to keep up with data USB " \
191 "data rate.",
192 "NOT ACCESSED: This code is set by software before the TD is placed" \
193 "on a list to be processed by the HC.(1)",
194 "NOT ACCESSED: This code is set by software before the TD is placed" \
195 "on a list to be processed by the HC.(2)",
196};
197
Remy Bohmer172a1132008-09-16 14:55:43 +0200198static inline u32 roothub_a(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500199 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200200static inline u32 roothub_b(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500201 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200202static inline u32 roothub_status(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500203 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200204static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500205 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200206
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200207/* forward declaration */
Hans de Goede2a240e72015-05-05 23:56:07 +0200208static int hc_interrupt(ohci_t *ohci);
209static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
210 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200211 struct devrequest *setup, urb_priv_t *urb,
212 int interval);
Hans de Goede516c9872015-05-05 23:56:11 +0200213static int ep_link(ohci_t * ohci, ed_t * ed);
214static int ep_unlink(ohci_t * ohci, ed_t * ed);
215static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
216 unsigned long pipe, int interval, int load);
217
218/*-------------------------------------------------------------------------*/
219
220/* TDs ... */
221static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
222{
223 int i;
224 struct td *td;
225
226 td = NULL;
227 for (i = 0; i < NUM_TD; i++)
228 {
229 if (ohci_dev->tds[i].usb_dev == NULL)
230 {
231 td = &ohci_dev->tds[i];
232 td->usb_dev = usb_dev;
233 break;
234 }
235 }
236
237 return td;
238}
239
240static inline void ed_free(struct ed *ed)
241{
242 ed->usb_dev = NULL;
243}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200244
245/*-------------------------------------------------------------------------*
246 * URB support functions
247 *-------------------------------------------------------------------------*/
248
249/* free HCD-private data associated with this URB */
250
Remy Bohmer172a1132008-09-16 14:55:43 +0200251static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200252{
253 int i;
254 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200255 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200256
257 last = urb->length - 1;
258 if (last >= 0) {
259 for (i = 0; i <= last; i++) {
260 td = urb->td[i];
261 if (td) {
262 td->usb_dev = NULL;
263 urb->td[i] = NULL;
264 }
265 }
266 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200267 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200268}
269
270/*-------------------------------------------------------------------------*/
271
272#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +0200273static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200274
275/* debug| print the main components of an URB
276 * small: 0) header + data packets 1) just header */
277
Hans de Goede2a240e72015-05-05 23:56:07 +0200278static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer172a1132008-09-16 14:55:43 +0200279 unsigned long pipe, void *buffer, int transfer_len,
280 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200281{
Remy Bohmer172a1132008-09-16 14:55:43 +0200282 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200283 str,
Hans de Goede2a240e72015-05-05 23:56:07 +0200284 sohci_get_current_frame_number(ohci),
Remy Bohmer172a1132008-09-16 14:55:43 +0200285 usb_pipedevice(pipe),
286 usb_pipeendpoint(pipe),
287 usb_pipeout(pipe)? 'O': 'I',
288 usb_pipetype(pipe) < 2 ? \
289 (usb_pipeint(pipe)? "INTR": "ISOC"): \
290 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200291 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200292 transfer_len, dev->status);
293#ifdef OHCI_VERBOSE_DEBUG
294 if (!small) {
295 int i, len;
296
Remy Bohmer172a1132008-09-16 14:55:43 +0200297 if (usb_pipecontrol(pipe)) {
298 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200299 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200300 printf(" %02x", ((__u8 *) setup) [i]);
301 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200302 }
303 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200304 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200305 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200306 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200307 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200308 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200309 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200310 printf(" %02x", ((__u8 *) buffer) [i]);
311 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200312 }
313 }
314#endif
315}
316
Remy Bohmer172a1132008-09-16 14:55:43 +0200317/* just for debugging; prints non-empty branches of the int ed tree
318 * inclusive iso eds */
319void ep_print_int_eds(ohci_t *ohci, char *str)
320{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200321 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200322 __u32 *ed_p;
323 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200324 j = 5;
325 ed_p = &(ohci->hcca->int_table [i]);
326 if (*ed_p == 0)
327 continue;
Hans de Goede8e41f662015-05-05 23:56:13 +0200328 invalidate_dcache_ed(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200329 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200330 while (*ed_p != 0 && j--) {
331 ed_t *ed = (ed_t *)m32_swap(ed_p);
Hans de Goede8e41f662015-05-05 23:56:13 +0200332 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200333 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200334 ed_p = &ed->hwNextED;
335 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200336 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200337 }
338}
339
Remy Bohmer172a1132008-09-16 14:55:43 +0200340static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200341{
Remy Bohmer172a1132008-09-16 14:55:43 +0200342 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200343 label,
344 mask,
345 (mask & OHCI_INTR_MIE) ? " MIE" : "",
346 (mask & OHCI_INTR_OC) ? " OC" : "",
347 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
348 (mask & OHCI_INTR_FNO) ? " FNO" : "",
349 (mask & OHCI_INTR_UE) ? " UE" : "",
350 (mask & OHCI_INTR_RD) ? " RD" : "",
351 (mask & OHCI_INTR_SF) ? " SF" : "",
352 (mask & OHCI_INTR_WDH) ? " WDH" : "",
353 (mask & OHCI_INTR_SO) ? " SO" : ""
354 );
355}
356
Remy Bohmer172a1132008-09-16 14:55:43 +0200357static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200358{
359 ed_t *edp = (ed_t *)value;
360
361 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200362 dbg("%s %08x", label, value);
Hans de Goede8e41f662015-05-05 23:56:13 +0200363 invalidate_dcache_ed(edp);
Remy Bohmer172a1132008-09-16 14:55:43 +0200364 dbg("%08x", edp->hwINFO);
365 dbg("%08x", edp->hwTailP);
366 dbg("%08x", edp->hwHeadP);
367 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200368 }
369}
370
Remy Bohmer172a1132008-09-16 14:55:43 +0200371static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200372{
373 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200374 case OHCI_USB_RESET: return "reset";
375 case OHCI_USB_RESUME: return "resume";
376 case OHCI_USB_OPER: return "operational";
377 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200378 }
379 return "?";
380}
381
382/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200383static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200384{
385 struct ohci_regs *regs = controller->regs;
386 __u32 temp;
387
Becky Bruce4abaacb2010-06-30 13:05:44 -0500388 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200389 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200390 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200391
Becky Bruce4abaacb2010-06-30 13:05:44 -0500392 temp = ohci_readl(&regs->control);
Remy Bohmer172a1132008-09-16 14:55:43 +0200393 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200394 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
395 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
396 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200397 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200398 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
399 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
400 (temp & OHCI_CTRL_IE) ? " IE" : "",
401 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
402 temp & OHCI_CTRL_CBSR
403 );
404
Becky Bruce4abaacb2010-06-30 13:05:44 -0500405 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200406 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200407 (temp & OHCI_SOC) >> 16,
408 (temp & OHCI_OCR) ? " OCR" : "",
409 (temp & OHCI_BLF) ? " BLF" : "",
410 (temp & OHCI_CLF) ? " CLF" : "",
411 (temp & OHCI_HCR) ? " HCR" : ""
412 );
413
Becky Bruce4abaacb2010-06-30 13:05:44 -0500414 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
415 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200416
Becky Bruce4abaacb2010-06-30 13:05:44 -0500417 maybe_print_eds("ed_periodcurrent",
418 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200419
Becky Bruce4abaacb2010-06-30 13:05:44 -0500420 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
421 maybe_print_eds("ed_controlcurrent",
422 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200423
Becky Bruce4abaacb2010-06-30 13:05:44 -0500424 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
425 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200426
Becky Bruce4abaacb2010-06-30 13:05:44 -0500427 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200428}
429
Remy Bohmer172a1132008-09-16 14:55:43 +0200430static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200431{
432 __u32 temp, ndp, i;
433
Remy Bohmer172a1132008-09-16 14:55:43 +0200434 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200435 ndp = (temp & RH_A_NDP);
436#ifdef CONFIG_AT91C_PQFP_UHPBUG
437 ndp = (ndp == 2) ? 1:0;
438#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200439 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200440 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200441 ((temp & RH_A_POTPGT) >> 24) & 0xff,
442 (temp & RH_A_NOCP) ? " NOCP" : "",
443 (temp & RH_A_OCPM) ? " OCPM" : "",
444 (temp & RH_A_DT) ? " DT" : "",
445 (temp & RH_A_NPS) ? " NPS" : "",
446 (temp & RH_A_PSM) ? " PSM" : "",
447 ndp
448 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200449 temp = roothub_b(controller);
450 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200451 temp,
452 (temp & RH_B_PPCM) >> 16,
453 (temp & RH_B_DR)
454 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200455 temp = roothub_status(controller);
456 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200457 temp,
458 (temp & RH_HS_CRWE) ? " CRWE" : "",
459 (temp & RH_HS_OCIC) ? " OCIC" : "",
460 (temp & RH_HS_LPSC) ? " LPSC" : "",
461 (temp & RH_HS_DRWE) ? " DRWE" : "",
462 (temp & RH_HS_OCI) ? " OCI" : "",
463 (temp & RH_HS_LPS) ? " LPS" : ""
464 );
465 }
466
467 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200468 temp = roothub_portstatus(controller, i);
469 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200470 i,
471 temp,
472 (temp & RH_PS_PRSC) ? " PRSC" : "",
473 (temp & RH_PS_OCIC) ? " OCIC" : "",
474 (temp & RH_PS_PSSC) ? " PSSC" : "",
475 (temp & RH_PS_PESC) ? " PESC" : "",
476 (temp & RH_PS_CSC) ? " CSC" : "",
477
478 (temp & RH_PS_LSDA) ? " LSDA" : "",
479 (temp & RH_PS_PPS) ? " PPS" : "",
480 (temp & RH_PS_PRS) ? " PRS" : "",
481 (temp & RH_PS_POCI) ? " POCI" : "",
482 (temp & RH_PS_PSS) ? " PSS" : "",
483
484 (temp & RH_PS_PES) ? " PES" : "",
485 (temp & RH_PS_CCS) ? " CCS" : ""
486 );
487 }
488}
489
Remy Bohmer172a1132008-09-16 14:55:43 +0200490static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200491{
Remy Bohmer172a1132008-09-16 14:55:43 +0200492 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200493
494 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200495 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200496 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200497 ep_print_int_eds(controller, "hcca");
Hans de Goede8e41f662015-05-05 23:56:13 +0200498 invalidate_dcache_hcca(controller->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200499 dbg("hcca frame #%04x", controller->hcca->frame_no);
500 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100501}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200502#endif /* DEBUG */
503
504/*-------------------------------------------------------------------------*
505 * Interface functions (URB)
506 *-------------------------------------------------------------------------*/
507
508/* get a transfer request */
509
Hans de Goede0c0e9602015-05-05 23:56:08 +0200510int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
511 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200512{
Remy Bohmer172a1132008-09-16 14:55:43 +0200513 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200514 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200515 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200516 struct usb_device *dev = urb->dev;
517 unsigned long pipe = urb->pipe;
518 void *buffer = urb->transfer_buffer;
519 int transfer_len = urb->transfer_buffer_length;
520 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200521
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200522 /* when controller's hung, permit only roothub cleanup attempts
523 * such as powering down ports */
524 if (ohci->disabled) {
525 err("sohci_submit_job: EPIPE");
526 return -1;
527 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200528
Remy Bohmer172a1132008-09-16 14:55:43 +0200529 /* we're about to begin a new transaction here so mark the
530 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200531 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200532
533 /* every endpoint has a ed, locate and fill it */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200534 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer172a1132008-09-16 14:55:43 +0200535 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200536 err("sohci_submit_job: ENOMEM");
537 return -1;
538 }
539
540 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200541 switch (usb_pipetype(pipe)) {
542 case PIPE_BULK: /* one TD for every 4096 Byte */
543 size = (transfer_len - 1) / 4096 + 1;
544 break;
545 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
546 size = (transfer_len == 0)? 2:
547 (transfer_len - 1) / 4096 + 3;
548 break;
549 case PIPE_INTERRUPT: /* 1 TD */
550 size = 1;
551 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200552 }
553
Zhang Wei8d15efa2007-06-06 10:08:14 +0200554 ed->purb = urb;
555
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200556 if (size >= (N_URB_TD - 1)) {
557 err("need %d TDs, only have %d", size, N_URB_TD);
558 return -1;
559 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200560 purb_priv->pipe = pipe;
561
562 /* fill the private part of the URB */
563 purb_priv->length = size;
564 purb_priv->ed = ed;
565 purb_priv->actual_length = 0;
566
567 /* allocate the TDs */
568 /* note that td[0] was allocated in ep_add_ed */
569 for (i = 0; i < size; i++) {
Hans de Goedee5ef4212015-05-05 23:56:09 +0200570 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200571 if (!purb_priv->td[i]) {
572 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200573 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200574 err("sohci_submit_job: ENOMEM");
575 return -1;
576 }
577 }
578
579 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200580 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200581 err("sohci_submit_job: EINVAL");
582 return -1;
583 }
584
585 /* link the ed into a chain if is not already */
586 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200587 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200588
589 /* fill the TDs and link it to the ed */
Hans de Goede2a240e72015-05-05 23:56:07 +0200590 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200591 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200592
593 return 0;
594}
595
596/*-------------------------------------------------------------------------*/
597
598#ifdef DEBUG
599/* tell us the current USB frame number */
Hans de Goede2a240e72015-05-05 23:56:07 +0200600static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200601{
Hans de Goede8e41f662015-05-05 23:56:13 +0200602 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200603 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200604}
605#endif
606
607/*-------------------------------------------------------------------------*
608 * ED handling functions
609 *-------------------------------------------------------------------------*/
610
Zhang Wei8d15efa2007-06-06 10:08:14 +0200611/* search for the right branch to insert an interrupt ed into the int tree
612 * do some load ballancing;
613 * returns the branch and
614 * sets the interval to interval = 2^integer (ld (interval)) */
615
Remy Bohmer172a1132008-09-16 14:55:43 +0200616static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200617{
618 int i, branch = 0;
619
620 /* search for the least loaded interrupt endpoint
621 * branch of all 32 branches
622 */
623 for (i = 0; i < 32; i++)
624 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
625 branch = i;
626
627 branch = branch % interval;
628 for (i = branch; i < 32; i += interval)
629 ohci->ohci_int_load [i] += load;
630
631 return branch;
632}
633
634/*-------------------------------------------------------------------------*/
635
636/* 2^int( ld (inter)) */
637
Remy Bohmer172a1132008-09-16 14:55:43 +0200638static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200639{
640 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200641 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200642 return 1 << i;
643}
644
645/*-------------------------------------------------------------------------*/
646
647/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200648 * in order to process it sequentially the indexes of the branches have to
649 * be mapped the mapping reverses the bits of a word of num_bits length */
650static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200651{
652 int i, wout = 0;
653
654 for (i = 0; i < num_bits; i++)
655 wout |= (((word >> i) & 1) << (num_bits - i - 1));
656 return wout;
657}
658
659/*-------------------------------------------------------------------------*
660 * ED handling functions
661 *-------------------------------------------------------------------------*/
662
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200663/* link an ed into one of the HC chains */
664
Remy Bohmer172a1132008-09-16 14:55:43 +0200665static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200666{
667 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200668 int int_branch;
669 int i;
670 int inter;
671 int interval;
672 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200673 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200674
675 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200676 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200677
678 switch (ed->type) {
679 case PIPE_CONTROL:
680 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200681 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200682 if (ohci->ed_controltail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100683 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200684 else
685 ohci->ed_controltail->hwNextED =
686 m32_swap((unsigned long)ed);
687
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200688 ed->ed_prev = ohci->ed_controltail;
689 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
690 !ohci->ed_rm_list[1] && !ohci->sleeping) {
691 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500692 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200693 }
694 ohci->ed_controltail = edi;
695 break;
696
697 case PIPE_BULK:
698 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200699 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200700 if (ohci->ed_bulktail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100701 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200702 else
703 ohci->ed_bulktail->hwNextED =
704 m32_swap((unsigned long)ed);
705
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200706 ed->ed_prev = ohci->ed_bulktail;
707 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
708 !ohci->ed_rm_list[1] && !ohci->sleeping) {
709 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500710 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200711 }
712 ohci->ed_bulktail = edi;
713 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200714
715 case PIPE_INTERRUPT:
716 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200717 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200718 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200719 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200720 ed->int_branch = int_branch;
721
Remy Bohmer172a1132008-09-16 14:55:43 +0200722 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200723 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200724 for (ed_p = &(ohci->hcca->int_table[\
725 ep_rev(5, i) + int_branch]);
726 (*ed_p != 0) &&
727 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200728 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200729 inter = ep_rev(6,
730 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200731 ed->hwNextED = *ed_p;
Hans de Goede8e41f662015-05-05 23:56:13 +0200732 flush_dcache_ed(ed);
Martin Krause8a588192007-08-21 12:40:34 +0200733 *ed_p = m32_swap((unsigned long)ed);
Hans de Goede8e41f662015-05-05 23:56:13 +0200734 flush_dcache_hcca(ohci->hcca);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200735 }
736 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200737 }
738 return 0;
739}
740
741/*-------------------------------------------------------------------------*/
742
Zhang Wei8d15efa2007-06-06 10:08:14 +0200743/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200744static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
745 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200746{
Hans de Goede8e41f662015-05-05 23:56:13 +0200747 __maybe_unused unsigned long aligned_ed_p;
748
Zhang Wei8d15efa2007-06-06 10:08:14 +0200749 for (; index < NUM_INTS; index += period) {
750 __u32 *ed_p = &ohci->hcca->int_table [index];
751
752 /* ED might have been unlinked through another path */
753 while (*ed_p != 0) {
Andre Przywarac114a982016-10-21 02:24:29 +0100754 if (((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200755 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200756 *ed_p = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200757 aligned_ed_p = (unsigned long)ed_p;
758 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
759 flush_dcache_range(aligned_ed_p,
760 aligned_ed_p + ARCH_DMA_MINALIGN);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200761 break;
762 }
Andre Przywarac114a982016-10-21 02:24:29 +0100763 ed_p = &(((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200764 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200765 }
766 }
767}
768
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200769/* unlink an ed from one of the HC chains.
770 * just the link to the ed is unlinked.
771 * the link from the ed still points to another operational ed or 0
772 * so the HC can eventually finish the processing of the unlinked ed */
773
Remy Bohmer172a1132008-09-16 14:55:43 +0200774static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200775{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100776 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200777 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100778
Remy Bohmer172a1132008-09-16 14:55:43 +0200779 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Hans de Goede8e41f662015-05-05 23:56:13 +0200780 flush_dcache_ed(ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200781
782 switch (ed->type) {
783 case PIPE_CONTROL:
784 if (ed->ed_prev == NULL) {
785 if (!ed->hwNextED) {
786 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500787 ohci_writel(ohci->hc_control,
788 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200789 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500790 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200791 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200792 } else {
793 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200794 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200795 }
796 if (ohci->ed_controltail == ed) {
797 ohci->ed_controltail = ed->ed_prev;
798 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100799 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200800 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200801 }
802 break;
803
804 case PIPE_BULK:
805 if (ed->ed_prev == NULL) {
806 if (!ed->hwNextED) {
807 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500808 ohci_writel(ohci->hc_control,
809 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200810 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500811 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200812 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200813 } else {
814 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200815 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200816 }
817 if (ohci->ed_bulktail == ed) {
818 ohci->ed_bulktail = ed->ed_prev;
819 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100820 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200821 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200822 }
823 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200824
825 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200826 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200827 for (i = ed->int_branch; i < 32; i += ed->int_interval)
828 ohci->ohci_int_load[i] -= ed->int_load;
829 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200830 }
831 ed->state = ED_UNLINK;
832 return 0;
833}
834
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200835/*-------------------------------------------------------------------------*/
836
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200837/* add/reinit an endpoint; this should be done once at the
838 * usb_set_configuration command, but the USB stack is a little bit
839 * stateless so we do it at every transaction if the state of the ed
840 * is ED_NEW then a dummy td is added and the state is changed to
841 * ED_UNLINK in all other cases the state is left unchanged the ed
842 * info fields are setted anyway even though most of them should not
843 * change
844 */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200845static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
846 unsigned long pipe, int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200847{
848 td_t *td;
849 ed_t *ed_ret;
850 volatile ed_t *ed;
851
Hans de Goede0c0e9602015-05-05 23:56:08 +0200852 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer172a1132008-09-16 14:55:43 +0200853 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200854
855 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
856 err("ep_add_ed: pending delete");
857 /* pending delete request */
858 return NULL;
859 }
860
861 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200862 /* dummy td; end of td list for ed */
Hans de Goedee5ef4212015-05-05 23:56:09 +0200863 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer172a1132008-09-16 14:55:43 +0200864 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200865 ed->hwHeadP = ed->hwTailP;
866 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200867 ed->type = usb_pipetype(pipe);
Hans de Goede0c0e9602015-05-05 23:56:08 +0200868 ohci_dev->ed_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200869 }
870
Remy Bohmer172a1132008-09-16 14:55:43 +0200871 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
872 | usb_pipeendpoint(pipe) << 7
873 | (usb_pipeisoc(pipe)? 0x8000: 0)
874 | (usb_pipecontrol(pipe)? 0: \
875 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000876 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer172a1132008-09-16 14:55:43 +0200877 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200878
Zhang Wei8d15efa2007-06-06 10:08:14 +0200879 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
880 ed->int_period = interval;
881 ed->int_load = load;
882 }
883
Hans de Goede8e41f662015-05-05 23:56:13 +0200884 flush_dcache_ed(ed);
885
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200886 return ed_ret;
887}
888
889/*-------------------------------------------------------------------------*
890 * TD handling functions
891 *-------------------------------------------------------------------------*/
892
893/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
894
Remy Bohmer172a1132008-09-16 14:55:43 +0200895static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200896 void *data, int len,
897 struct usb_device *dev, int index, urb_priv_t *urb_priv)
898{
899 volatile td_t *td, *td_pt;
900#ifdef OHCI_FILL_TRACE
901 int i;
902#endif
903
904 if (index > urb_priv->length) {
905 err("index > length");
906 return;
907 }
908 /* use this td as the next dummy */
909 td_pt = urb_priv->td [index];
910 td_pt->hwNextTD = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200911 flush_dcache_td(td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200912
913 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200914 td = urb_priv->td [index] =
Andre Przywarac114a982016-10-21 02:24:29 +0100915 (td_t *)(uintptr_t)
916 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200917
918 td->ed = urb_priv->ed;
919 td->next_dl_td = NULL;
920 td->index = index;
Andre Przywarac114a982016-10-21 02:24:29 +0100921 td->data = (uintptr_t)data;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200922#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200923 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200924 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200925 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200926 printf("\n");
927 }
928#endif
929 if (!len)
930 data = 0;
931
Remy Bohmer172a1132008-09-16 14:55:43 +0200932 td->hwINFO = m32_swap(info);
933 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200934 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200935 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200936 else
937 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200938
939 td->hwNextTD = m32_swap((unsigned long)td_pt);
Hans de Goede8e41f662015-05-05 23:56:13 +0200940 flush_dcache_td(td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200941
942 /* append to queue */
943 td->ed->hwTailP = td->hwNextTD;
Hans de Goede8e41f662015-05-05 23:56:13 +0200944 flush_dcache_ed(td->ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200945}
946
947/*-------------------------------------------------------------------------*/
948
949/* prepare all TDs of a transfer */
950
Hans de Goede2a240e72015-05-05 23:56:07 +0200951static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
952 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200953 struct devrequest *setup, urb_priv_t *urb,
954 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200955{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200956 int data_len = transfer_len;
957 void *data;
958 int cnt = 0;
959 __u32 info = 0;
960 unsigned int toggle = 0;
961
Hans de Goede8e41f662015-05-05 23:56:13 +0200962 flush_dcache_buffer(buffer, data_len);
963
Remy Bohmer172a1132008-09-16 14:55:43 +0200964 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700965 * bits for resetting */
Remy Bohmer172a1132008-09-16 14:55:43 +0200966 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200967 toggle = TD_T_TOGGLE;
968 } else {
969 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200970 usb_settoggle(dev, usb_pipeendpoint(pipe),
971 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200972 }
973 urb->td_cnt = 0;
974 if (data_len)
975 data = buffer;
976 else
977 data = 0;
978
Remy Bohmer172a1132008-09-16 14:55:43 +0200979 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200980 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200981 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200982 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200983 while (data_len > 4096) {
984 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
985 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200986 data += 4096; data_len -= 4096; cnt++;
987 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200988 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200989 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200990 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
991 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200992 cnt++;
993
Remy Bohmer172a1132008-09-16 14:55:43 +0200994 if (!ohci->sleeping) {
995 /* start bulk list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500996 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200997 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200998 break;
999
1000 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +02001001 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001002 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001003 flush_dcache_buffer(setup, 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001004 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1005
1006 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001007 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001008 info = usb_pipeout(pipe)?
1009 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1010 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001011 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +02001012 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001013 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001014
1015 /* Status phase */
Hans de Goede60f4bd02015-05-05 23:56:12 +02001016 info = (usb_pipeout(pipe) || data_len == 0) ?
Remy Bohmer172a1132008-09-16 14:55:43 +02001017 TD_CC | TD_DP_IN | TD_T_DATA1:
1018 TD_CC | TD_DP_OUT | TD_T_DATA1;
1019 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1020
1021 if (!ohci->sleeping) {
1022 /* start Control list */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001023 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001024 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001025 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001026
1027 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +02001028 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +02001029 TD_CC | TD_DP_OUT | toggle:
1030 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +02001031 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001032 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001033 }
1034 if (urb->length != cnt)
1035 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1036}
1037
1038/*-------------------------------------------------------------------------*
1039 * Done List handling functions
1040 *-------------------------------------------------------------------------*/
1041
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001042/* calculate the transfer length and update the urb */
1043
Remy Bohmer172a1132008-09-16 14:55:43 +02001044static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001045{
Wolfgang Denk0ef5f622011-10-05 23:03:43 +02001046 __u32 tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001047 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001048
Remy Bohmer172a1132008-09-16 14:55:43 +02001049 tdBE = m32_swap(td->hwBE);
1050 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001051
Remy Bohmer47e72082008-10-10 10:23:21 +02001052 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001053 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1054 if (tdBE != 0) {
1055 if (td->hwCBP == 0)
1056 lurb_priv->actual_length += tdBE - td->data + 1;
1057 else
1058 lurb_priv->actual_length += tdCBP - td->data;
1059 }
1060 }
1061}
1062
1063/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001064static void check_status(td_t *td_list)
1065{
1066 urb_priv_t *lurb_priv = td_list->ed->purb;
1067 int urb_len = lurb_priv->length;
1068 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1069 int cc;
1070
1071 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1072 if (cc) {
1073 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1074
Hans de Goede8e41f662015-05-05 23:56:13 +02001075 invalidate_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001076 if (*phwHeadP & m32_swap(0x1)) {
1077 if (lurb_priv &&
1078 ((td_list->index + 1) < urb_len)) {
1079 *phwHeadP =
1080 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1081 m32_swap(0xfffffff0)) |
1082 (*phwHeadP & m32_swap(0x2));
1083
1084 lurb_priv->td_cnt += urb_len -
1085 td_list->index - 1;
1086 } else
1087 *phwHeadP &= m32_swap(0xfffffff2);
Hans de Goede8e41f662015-05-05 23:56:13 +02001088 flush_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001089 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001090 }
1091}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001092
1093/* replies to the request have to be on a FIFO basis so
1094 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +02001095static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001096{
Andre Przywarac114a982016-10-21 02:24:29 +01001097 uintptr_t td_list_hc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001098 td_t *td_rev = NULL;
1099 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001100
Hans de Goede8e41f662015-05-05 23:56:13 +02001101 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +02001102 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001103 ohci->hcca->done_head = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001104 flush_dcache_hcca(ohci->hcca);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001105
1106 while (td_list_hc) {
1107 td_list = (td_t *)td_list_hc;
Hans de Goede8e41f662015-05-05 23:56:13 +02001108 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001109 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001110 td_list->next_dl_td = td_rev;
1111 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001112 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001113 }
1114 return td_list;
1115}
1116
1117/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001118/*-------------------------------------------------------------------------*/
1119
1120static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1121{
1122 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
Hans de Goede27873372015-05-10 14:10:22 +02001123 urb->finished = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001124 else
1125 dbg("finish_urb: strange.., ED state %x, \n", status);
1126}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001127
Remy Bohmer172a1132008-09-16 14:55:43 +02001128/*
1129 * Used to take back a TD from the host controller. This would normally be
1130 * called from within dl_done_list, however it may be called directly if the
1131 * HC no longer sees the TD and it has not appeared on the donelist (after
1132 * two frames). This bug has been observed on ZF Micro systems.
1133 */
1134static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001135{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001136 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001137 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001138 int stat = 0;
1139 /* urb_t *urb; */
1140 urb_priv_t *lurb_priv;
1141 __u32 tdINFO, edHeadP, edTailP;
1142
Hans de Goede8e41f662015-05-05 23:56:13 +02001143 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001144 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001145
Remy Bohmer172a1132008-09-16 14:55:43 +02001146 ed = td_list->ed;
1147 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001148
Remy Bohmer172a1132008-09-16 14:55:43 +02001149 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001150
Remy Bohmer172a1132008-09-16 14:55:43 +02001151 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001152
Remy Bohmer172a1132008-09-16 14:55:43 +02001153 /* error code of transfer */
1154 cc = TD_CC_GET(tdINFO);
1155 if (cc) {
1156 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1157 stat = cc_to_error[cc];
1158 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001159
Remy Bohmer172a1132008-09-16 14:55:43 +02001160 /* see if this done list makes for all TD's of current URB,
1161 * and mark the URB finished if so */
1162 if (lurb_priv->td_cnt == lurb_priv->length)
1163 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001164
Remy Bohmer172a1132008-09-16 14:55:43 +02001165 dbg("dl_done_list: processing TD %x, len %x\n",
1166 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001167
Remy Bohmer47e72082008-10-10 10:23:21 +02001168 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Hans de Goede8e41f662015-05-05 23:56:13 +02001169 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001170 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1171 edTailP = m32_swap(ed->hwTailP);
1172
1173 /* unlink eds if they are not busy */
1174 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1175 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001176 }
1177 return stat;
1178}
1179
Remy Bohmer172a1132008-09-16 14:55:43 +02001180static int dl_done_list(ohci_t *ohci)
1181{
1182 int stat = 0;
1183 td_t *td_list = dl_reverse_done_list(ohci);
1184
1185 while (td_list) {
1186 td_t *td_next = td_list->next_dl_td;
1187 stat = takeback_td(ohci, td_list);
1188 td_list = td_next;
1189 }
1190 return stat;
1191}
1192
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001193/*-------------------------------------------------------------------------*
1194 * Virtual Root Hub
1195 *-------------------------------------------------------------------------*/
1196
Stephen Warren39c89682014-02-13 21:15:18 -07001197#include <usbroothubdes.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001198
1199/* Hub class-specific descriptor is constructed dynamically */
1200
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001201/*-------------------------------------------------------------------------*/
1202
1203#define OK(x) len = (x); break
1204#ifdef DEBUG
Becky Bruce4abaacb2010-06-30 13:05:44 -05001205#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001206 &ohci->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +02001207#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goede2a240e72015-05-05 23:56:07 +02001208 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001209#else
Hans de Goede2a240e72015-05-05 23:56:07 +02001210#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001211#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001212 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001213#endif
Hans de Goede2a240e72015-05-05 23:56:07 +02001214#define RD_RH_STAT roothub_status(ohci)
1215#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001216
1217/* request to virtual root hub */
1218
1219int rh_check_port_status(ohci_t *controller)
1220{
1221 __u32 temp, ndp, i;
1222 int res;
1223
1224 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001225 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001226 ndp = (temp & RH_A_NDP);
1227#ifdef CONFIG_AT91C_PQFP_UHPBUG
1228 ndp = (ndp == 2) ? 1:0;
1229#endif
1230 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001231 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001232 /* check for a device disconnect */
1233 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1234 (RH_PS_PESC | RH_PS_CSC)) &&
1235 ((temp & RH_PS_CCS) == 0)) {
1236 res = i;
1237 break;
1238 }
1239 }
1240 return res;
1241}
1242
Hans de Goede2a240e72015-05-05 23:56:07 +02001243static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1244 unsigned long pipe, void *buffer, int transfer_len,
1245 struct devrequest *cmd)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001246{
Remy Bohmer172a1132008-09-16 14:55:43 +02001247 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001248 int leni = transfer_len;
1249 int len = 0;
1250 int stat = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001251 __u16 bmRType_bReq;
1252 __u16 wValue;
1253 __u16 wIndex;
1254 __u16 wLength;
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001255 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasutfc5cf882011-10-07 02:00:13 +02001256
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001257#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001258pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001259 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001260#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001261 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001262#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001263 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001264 info("Root-Hub submit IRQ: NOT implemented");
1265 return 0;
1266 }
1267
1268 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001269 wValue = le16_to_cpu(cmd->value);
1270 wIndex = le16_to_cpu(cmd->index);
1271 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001272
1273 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1274 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1275
1276 switch (bmRType_bReq) {
1277 /* Request Destination:
1278 without flags: Device,
1279 RH_INTERFACE: interface,
1280 RH_ENDPOINT: endpoint,
1281 RH_CLASS means HUB here,
1282 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1283 */
1284
1285 case RH_GET_STATUS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001286 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001287 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001288 case RH_GET_STATUS | RH_INTERFACE:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001289 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001290 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001291 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001292 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001293 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001294 case RH_GET_STATUS | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001295 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001296 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001297 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001298 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001299 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer172a1132008-09-16 14:55:43 +02001300 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001301
1302 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1303 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001304 case (RH_ENDPOINT_STALL):
1305 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001306 }
1307 break;
1308
1309 case RH_CLEAR_FEATURE | RH_CLASS:
1310 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001311 case RH_C_HUB_LOCAL_POWER:
1312 OK(0);
1313 case (RH_C_HUB_OVER_CURRENT):
1314 WR_RH_STAT(RH_HS_OCIC);
1315 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001316 }
1317 break;
1318
1319 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1320 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001321 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1322 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1323 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1324 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1325 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1326 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1327 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1328 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001329 }
1330 break;
1331
1332 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1333 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001334 case (RH_PORT_SUSPEND):
1335 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1336 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1337 if (RD_RH_PORTSTAT & RH_PS_CCS)
1338 WR_RH_PORTSTAT(RH_PS_PRS);
1339 OK(0);
1340 case (RH_PORT_POWER):
1341 WR_RH_PORTSTAT(RH_PS_PPS);
Remy Bohmer172a1132008-09-16 14:55:43 +02001342 OK(0);
1343 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1344 if (RD_RH_PORTSTAT & RH_PS_CCS)
1345 WR_RH_PORTSTAT(RH_PS_PES);
1346 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001347 }
1348 break;
1349
Remy Bohmer172a1132008-09-16 14:55:43 +02001350 case RH_SET_ADDRESS:
Hans de Goede2a240e72015-05-05 23:56:07 +02001351 ohci->rh.devnum = wValue;
Remy Bohmer172a1132008-09-16 14:55:43 +02001352 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001353
1354 case RH_GET_DESCRIPTOR:
1355 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001356 case (0x01): /* device descriptor */
1357 len = min_t(unsigned int,
1358 leni,
1359 min_t(unsigned int,
1360 sizeof(root_hub_dev_des),
1361 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001362 databuf = root_hub_dev_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001363 case (0x02): /* configuration descriptor */
1364 len = min_t(unsigned int,
1365 leni,
1366 min_t(unsigned int,
1367 sizeof(root_hub_config_des),
1368 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001369 databuf = root_hub_config_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001370 case (0x03): /* string descriptors */
1371 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001372 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001373 leni,
1374 min_t(unsigned int,
1375 sizeof(root_hub_str_index0),
1376 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001377 databuf = root_hub_str_index0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001378 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001379 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001380 if (wValue == 0x0301) {
1381 len = min_t(unsigned int,
1382 leni,
1383 min_t(unsigned int,
1384 sizeof(root_hub_str_index1),
1385 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001386 databuf = root_hub_str_index1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001387 OK(len);
1388 }
1389 default:
1390 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001391 }
1392 break;
1393
1394 case RH_GET_DESCRIPTOR | RH_CLASS:
1395 {
Hans de Goede2a240e72015-05-05 23:56:07 +02001396 __u32 temp = roothub_a(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001397
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001398 databuf[0] = 9; /* min length; */
1399 databuf[1] = 0x29;
1400 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001401#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001402 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001403#endif
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001404 databuf[3] = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001405 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001406 databuf[3] |= 0x1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001407 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001408 databuf[3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001409 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001410 databuf[3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001411
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001412 databuf[4] = 0;
1413 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1414 databuf[6] = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001415 temp = roothub_b(ohci);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001416 databuf[7] = temp & RH_B_DR;
1417 if (databuf[2] < 7) {
1418 databuf[8] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001419 } else {
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001420 databuf[0] += 2;
1421 databuf[8] = (temp & RH_B_DR) >> 8;
1422 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001423 }
1424
1425 len = min_t(unsigned int, leni,
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001426 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001427 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001428 }
1429
Marek Vasutfc5cf882011-10-07 02:00:13 +02001430 case RH_GET_CONFIGURATION:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001431 databuf[0] = 0x01;
Marek Vasutfc5cf882011-10-07 02:00:13 +02001432 OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001433
Marek Vasutfc5cf882011-10-07 02:00:13 +02001434 case RH_SET_CONFIGURATION:
1435 WR_RH_STAT(0x10000);
1436 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001437
1438 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001439 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001440 stat = USB_ST_STALLED;
1441 }
1442
1443#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001444 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001445#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001446 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001447#endif
1448
1449 len = min_t(int, len, leni);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001450 if (data != databuf)
1451 memcpy(data, databuf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001452 dev->act_len = len;
1453 dev->status = stat;
1454
1455#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001456 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer172a1132008-09-16 14:55:43 +02001457 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001458#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001459 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001460#endif
1461
1462 return stat;
1463}
1464
1465/*-------------------------------------------------------------------------*/
1466
Hans de Goeded7a39422015-05-13 14:42:15 +02001467static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1468{
1469 int i;
1470
1471 if (!intr)
1472 return &ohci->ohci_dev;
1473
1474 /* First see if we already have an ohci_dev for this dev. */
1475 for (i = 0; i < NUM_INT_DEVS; i++) {
1476 if (ohci->int_dev[i].devnum == devnum)
1477 return &ohci->int_dev[i];
1478 }
1479
1480 /* If not then find a free one. */
1481 for (i = 0; i < NUM_INT_DEVS; i++) {
1482 if (ohci->int_dev[i].devnum == -1) {
1483 ohci->int_dev[i].devnum = devnum;
1484 return &ohci->int_dev[i];
1485 }
1486 }
1487
1488 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1489 return NULL;
1490}
1491
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001492/* common code for handling submit messages - used for all but root hub */
1493/* accesses. */
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001494static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1495 void *buffer, int transfer_len, int interval)
1496{
1497 urb_priv_t *urb;
1498
1499 urb = calloc(1, sizeof(urb_priv_t));
1500 if (!urb) {
1501 printf("ohci: Error out of memory allocating urb\n");
1502 return NULL;
1503 }
1504
1505 urb->dev = dev;
1506 urb->pipe = pipe;
1507 urb->transfer_buffer = buffer;
1508 urb->transfer_buffer_length = transfer_len;
1509 urb->interval = interval;
1510
1511 return urb;
1512}
1513
Hans de Goede2a240e72015-05-05 23:56:07 +02001514static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1515 unsigned long pipe, void *buffer, int transfer_len,
1516 struct devrequest *setup, int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001517{
1518 int stat = 0;
1519 int maxsize = usb_maxpacket(dev, pipe);
1520 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001521 urb_priv_t *urb;
Hans de Goeded7a39422015-05-13 14:42:15 +02001522 ohci_dev_t *ohci_dev;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001523
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001524 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1525 if (!urb)
1526 return -ENOMEM;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001527
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001528#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001529 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001530 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001531 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001532#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001533 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001534#endif
1535 if (!maxsize) {
1536 err("submit_common_message: pipesize for pipe %lx is zero",
1537 pipe);
1538 return -1;
1539 }
1540
Hans de Goeded7a39422015-05-13 14:42:15 +02001541 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1542 if (!ohci_dev)
1543 return -ENOMEM;
1544
1545 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001546 err("sohci_submit_job failed");
1547 return -1;
1548 }
1549
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001550 mdelay(10);
Hans de Goede2a240e72015-05-05 23:56:07 +02001551 /* ohci_dump_status(ohci); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001552
Simon Glassfd7f5132011-02-07 14:42:16 -08001553 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001554
1555 /* wait for it to complete */
1556 for (;;) {
1557 /* check whether the controller is done */
Hans de Goede2a240e72015-05-05 23:56:07 +02001558 stat = hc_interrupt(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001559 if (stat < 0) {
1560 stat = USB_ST_CRC_ERR;
1561 break;
1562 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001563
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001564 /* NOTE: since we are not interrupt driven in U-Boot and always
1565 * handle only one URB at a time, we cannot assume the
1566 * transaction finished on the first successful return from
1567 * hc_interrupt().. unless the flag for current URB is set,
1568 * meaning that all TD's to/from device got actually
1569 * transferred and processed. If the current URB is not
1570 * finished we need to re-iterate this loop so as
1571 * hc_interrupt() gets called again as there needs to be some
1572 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001573 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001574 /* 0xff is returned for an SF-interrupt */
1575 break;
1576 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001577
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001578 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001579 mdelay(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001580 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001581 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001582
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001583 } else {
Hans de Goeded98dc992015-05-05 23:56:14 +02001584 if (!usb_pipeint(pipe))
1585 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001586 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001587 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001588 stat = USB_ST_CRC_ERR;
1589 break;
1590 }
1591 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001592
1593 dev->status = stat;
Mateusz Kulikowski10b479d2013-10-23 20:26:27 +02001594 dev->act_len = urb->actual_length;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001595
Hans de Goede8e41f662015-05-05 23:56:13 +02001596 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1597 invalidate_dcache_buffer(buffer, dev->act_len);
1598
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001599#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001600 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001601 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001602#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001603 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001604#endif
Hans de Goede27873372015-05-10 14:10:22 +02001605 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001606 return 0;
1607}
1608
Hans de Goede5477e6f2015-05-13 14:42:17 +02001609#define MAX_INT_QUEUESIZE 8
1610
1611struct int_queue {
1612 int queuesize;
1613 int curr_urb;
1614 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1615};
1616
1617static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1618 struct usb_device *udev, unsigned long pipe, int queuesize,
1619 int elementsize, void *buffer, int interval)
1620{
1621 struct int_queue *queue;
1622 ohci_dev_t *ohci_dev;
1623 int i;
1624
1625 if (queuesize > MAX_INT_QUEUESIZE)
1626 return NULL;
1627
1628 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1629 if (!ohci_dev)
1630 return NULL;
1631
1632 queue = malloc(sizeof(*queue));
1633 if (!queue) {
1634 printf("ohci: Error out of memory allocating int queue\n");
1635 return NULL;
1636 }
1637
1638 for (i = 0; i < queuesize; i++) {
1639 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1640 buffer + i * elementsize,
1641 elementsize, interval);
1642 if (!queue->urb[i])
1643 break;
1644
1645 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1646 printf("ohci: Error submitting int queue job\n");
1647 urb_free_priv(queue->urb[i]);
1648 break;
1649 }
1650 }
1651 if (i == 0) {
1652 /* We did not succeed in submitting even 1 urb */
1653 free(queue);
1654 return NULL;
1655 }
1656
1657 queue->queuesize = i;
1658 queue->curr_urb = 0;
1659
1660 return queue;
1661}
1662
1663static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1664 struct int_queue *queue)
1665{
1666 if (queue->curr_urb == queue->queuesize)
1667 return NULL; /* Queue depleted */
1668
1669 if (hc_interrupt(ohci) < 0)
1670 return NULL;
1671
1672 if (queue->urb[queue->curr_urb]->finished) {
1673 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1674 queue->curr_urb++;
1675 return ret;
1676 }
1677
1678 return NULL;
1679}
1680
1681static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1682 struct int_queue *queue)
1683{
1684 int i;
1685
1686 for (i = 0; i < queue->queuesize; i++)
1687 urb_free_priv(queue->urb[i]);
1688
1689 free(queue);
1690
1691 return 0;
1692}
1693
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001694#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001695/* submit routines called from usb.c */
1696int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1697 int transfer_len)
1698{
1699 info("submit_bulk_msg");
Hans de Goede2a240e72015-05-05 23:56:07 +02001700 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1701 NULL, 0);
1702}
1703
1704int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1705 int transfer_len, int interval)
1706{
1707 info("submit_int_msg");
1708 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1709 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001710}
Hans de Goede5477e6f2015-05-13 14:42:17 +02001711
1712struct int_queue *create_int_queue(struct usb_device *dev,
1713 unsigned long pipe, int queuesize, int elementsize,
1714 void *buffer, int interval)
1715{
1716 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1717 elementsize, buffer, interval);
1718}
1719
1720void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1721{
1722 return _ohci_poll_int_queue(&gohci, dev, queue);
1723}
1724
1725int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1726{
1727 return _ohci_destroy_int_queue(&gohci, dev, queue);
1728}
Hans de Goede8a62c502015-05-10 14:10:25 +02001729#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001730
Hans de Goede2a240e72015-05-05 23:56:07 +02001731static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1732 unsigned long pipe, void *buffer, int transfer_len,
1733 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001734{
1735 int maxsize = usb_maxpacket(dev, pipe);
1736
1737 info("submit_control_msg");
1738#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001739 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001740 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001741#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001742 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001743#endif
1744 if (!maxsize) {
1745 err("submit_control_message: pipesize for pipe %lx is zero",
1746 pipe);
1747 return -1;
1748 }
Hans de Goede2a240e72015-05-05 23:56:07 +02001749 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1750 ohci->rh.dev = dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001751 /* root hub - redirect */
Hans de Goede2a240e72015-05-05 23:56:07 +02001752 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1753 transfer_len, setup);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001754 }
1755
Hans de Goede2a240e72015-05-05 23:56:07 +02001756 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1757 setup, 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001758}
1759
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001760/*-------------------------------------------------------------------------*
1761 * HC functions
1762 *-------------------------------------------------------------------------*/
1763
1764/* reset the HC and BUS */
1765
Remy Bohmer172a1132008-09-16 14:55:43 +02001766static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001767{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001768#ifdef CONFIG_PCI_EHCI_DEVNO
1769 pci_dev_t pdev;
1770#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001771 int timeout = 30;
1772 int smm_timeout = 50; /* 0,5 sec */
1773
1774 dbg("%s\n", __FUNCTION__);
1775
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001776#ifdef CONFIG_PCI_EHCI_DEVNO
1777 /*
1778 * Some multi-function controllers (e.g. ISP1562) allow root hub
1779 * resetting via EHCI registers only.
1780 */
1781 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1782 if (pdev != -1) {
1783 u32 base;
1784 int timeout = 1000;
1785
1786 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001787 base += EHCI_USBCMD_OFF;
1788 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001789
Becky Bruce4abaacb2010-06-30 13:05:44 -05001790 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001791 if (timeout-- <= 0) {
1792 printf("USB RootHub reset timed out!");
1793 break;
1794 }
1795 udelay(1);
1796 }
1797 } else
1798 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1799#endif
Becky Bruce4abaacb2010-06-30 13:05:44 -05001800 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1801 /* SMM owns the HC, request ownership */
1802 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001803 info("USB HC TakeOver from SMM");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001804 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001805 mdelay(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001806 if (--smm_timeout == 0) {
1807 err("USB HC TakeOver failed!");
1808 return -1;
1809 }
1810 }
1811 }
1812
1813 /* Disable HC interrupts */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001814 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001815
1816 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1817 ohci->slot_name,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001818 ohci_readl(&ohci->regs->control));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001819
1820 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001821 ohci->hc_control = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001822 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001823
1824 /* HC Reset requires max 10 us delay */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001825 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1826 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001827 if (--timeout == 0) {
1828 err("USB HC reset timed out!");
1829 return -1;
1830 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001831 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001832 }
1833 return 0;
1834}
1835
1836/*-------------------------------------------------------------------------*/
1837
1838/* Start an OHCI controller, set the BUS operational
1839 * enable interrupts
1840 * connect the virtual root hub */
1841
Remy Bohmer172a1132008-09-16 14:55:43 +02001842static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001843{
1844 __u32 mask;
1845 unsigned int fminterval;
Hans de Goeded7a39422015-05-13 14:42:15 +02001846 int i;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001847
1848 ohci->disabled = 1;
Hans de Goeded7a39422015-05-13 14:42:15 +02001849 for (i = 0; i < NUM_INT_DEVS; i++)
1850 ohci->int_dev[i].devnum = -1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001851
1852 /* Tell the controller where the control and bulk lists are
1853 * The lists are empty now. */
1854
Becky Bruce4abaacb2010-06-30 13:05:44 -05001855 ohci_writel(0, &ohci->regs->ed_controlhead);
1856 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001857
Andre Przywarac114a982016-10-21 02:24:29 +01001858 ohci_writel((uintptr_t)ohci->hcca,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001859 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001860
1861 fminterval = 0x2edf;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001862 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001863 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001864 ohci_writel(fminterval, &ohci->regs->fminterval);
1865 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001866
1867 /* start controller operations */
1868 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1869 ohci->disabled = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001870 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001871
1872 /* disable all interrupts */
1873 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1874 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1875 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001876 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001877 /* clear all interrupts */
1878 mask &= ~OHCI_INTR_MIE;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001879 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001880 /* Choose the interrupts we care about now - but w/o MIE */
1881 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001882 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001883
1884#ifdef OHCI_USE_NPS
1885 /* required for AMD-756 and some Mac platforms */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001886 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001887 &ohci->regs->roothub.a);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001888 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001889#endif /* OHCI_USE_NPS */
1890
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001891 /* connect the virtual root hub */
1892 ohci->rh.devnum = 0;
1893
1894 return 0;
1895}
1896
1897/*-------------------------------------------------------------------------*/
1898
1899/* an interrupt happens */
1900
Hans de Goede2a240e72015-05-05 23:56:07 +02001901static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001902{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001903 struct ohci_regs *regs = ohci->regs;
1904 int ints;
1905 int stat = -1;
1906
Hans de Goede8e41f662015-05-05 23:56:13 +02001907 invalidate_dcache_hcca(ohci->hcca);
1908
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001909 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001910 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001911 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001912 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001913 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001914 if (ints == ~(u32)0) {
1915 ohci->disabled++;
1916 err("%s device removed!", ohci->slot_name);
1917 return -1;
1918 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001919 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer172a1132008-09-16 14:55:43 +02001920 if (ints == 0) {
1921 dbg("hc_interrupt: returning..\n");
1922 return 0xff;
1923 }
1924 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001925 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001926
Remy Bohmer172a1132008-09-16 14:55:43 +02001927 /* dbg("Interrupt: %x frame: %x", ints,
1928 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001929
Remy Bohmer172a1132008-09-16 14:55:43 +02001930 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001931 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001932
1933 if (ints & OHCI_INTR_UE) {
1934 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001935 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001936 ohci->slot_name);
1937 /* e.g. due to PCI Master/Target Abort */
1938
1939#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001940 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001941#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001942 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001943#endif
1944 /* FIXME: be optimistic, hope that bug won't repeat often. */
1945 /* Make some non-interrupt context restart the controller. */
1946 /* Count and limit the retries though; either hardware or */
1947 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001948 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001949 return -1;
1950 }
1951
1952 if (ints & OHCI_INTR_WDH) {
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001953 ohci_mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001954 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1955 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goede2a240e72015-05-05 23:56:07 +02001956 stat = dl_done_list(ohci);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001957 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1958 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001959 }
1960
1961 if (ints & OHCI_INTR_SO) {
1962 dbg("USB Schedule overrun\n");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001963 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001964 stat = -1;
1965 }
1966
1967 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1968 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001969 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001970 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001971 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001972 if (ohci->ed_rm_list[frame] != NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001973 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001974 stat = 0xff;
1975 }
1976
Becky Bruce4abaacb2010-06-30 13:05:44 -05001977 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001978 return stat;
1979}
1980
1981/*-------------------------------------------------------------------------*/
1982
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001983#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02001984
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001985/*-------------------------------------------------------------------------*/
1986
1987/* De-allocate all resources.. */
1988
Remy Bohmer172a1132008-09-16 14:55:43 +02001989static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001990{
Remy Bohmer172a1132008-09-16 14:55:43 +02001991 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001992
1993 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001994 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001995}
1996
1997/*-------------------------------------------------------------------------*/
1998
1999/*
2000 * low level initalisation routine, called from usb.c
2001 */
2002static char ohci_inited = 0;
2003
Troy Kisky8f9c49d2013-10-10 15:27:56 -07002004int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002005{
Zhang Wei8d15efa2007-06-06 10:08:14 +02002006#ifdef CONFIG_PCI_OHCI
2007 pci_dev_t pdev;
2008#endif
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002009
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002010#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002011 /* cpu dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02002012 if (usb_cpu_init())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002013 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002014#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002015
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002016#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002017 /* board dependant init */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002018 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002019 return -1;
2020#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02002021 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002022
2023 /* align the storage */
2024 if ((__u32)&ghcca[0] & 0xff) {
2025 err("HCCA not aligned!!");
2026 return -1;
2027 }
Hans de Goedeeb509d32015-05-05 23:56:10 +02002028 gohci.hcca = &ghcca[0];
2029 info("aligned ghcca %p", gohci.hcca);
2030 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002031
2032 gohci.disabled = 1;
2033 gohci.sleeping = 0;
2034 gohci.irq = -1;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002035#ifdef CONFIG_PCI_OHCI
Sergei Poselenov0b4570f2008-05-22 01:15:53 +02002036 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02002037
2038 if (pdev != -1) {
2039 u16 vid, did;
2040 u32 base;
2041 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2042 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2043 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2044 vid, did, (pdev >> 16) & 0xff,
2045 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2046 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2047 printf("OHCI regs address 0x%08x\n", base);
2048 gohci.regs = (struct ohci_regs *)base;
2049 } else
2050 return -1;
2051#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002052 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002053#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002054
2055 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002056 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002057
2058 if (hc_reset (&gohci) < 0) {
2059 hc_release_ohci (&gohci);
2060 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002061#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002062 /* board dependant cleanup */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002063 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002064#endif
2065
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002066#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002067 /* cpu dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02002068 usb_cpu_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002069#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002070 return -1;
2071 }
2072
Remy Bohmer172a1132008-09-16 14:55:43 +02002073 if (hc_start(&gohci) < 0) {
2074 err("can't start usb-%s", gohci.slot_name);
2075 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002076 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002077#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002078 /* board dependant cleanup */
2079 usb_board_stop();
2080#endif
2081
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002082#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002083 /* cpu dependant cleanup */
2084 usb_cpu_stop();
2085#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002086 return -1;
2087 }
2088
2089#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02002090 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002091#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02002092 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002093#endif
2094 ohci_inited = 1;
2095 return 0;
2096}
2097
Lucas Stacha3231282012-09-26 00:14:34 +02002098int usb_lowlevel_stop(int index)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002099{
2100 /* this gets called really early - before the controller has */
2101 /* even been initialized! */
2102 if (!ohci_inited)
2103 return 0;
2104 /* TODO release any interrupts, etc. */
2105 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02002106 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002108#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002109 /* board dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002110 if (usb_board_stop())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002111 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002112#endif
2113
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002114#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002115 /* cpu dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002116 if (usb_cpu_stop())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002117 return -1;
2118#endif
Remy Bohmer743dd492008-08-20 11:22:02 +02002119 /* This driver is no longer initialised. It needs a new low-level
2120 * init (board/cpu) before it can be used again. */
2121 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002122 return 0;
2123}
Hans de Goede2a240e72015-05-05 23:56:07 +02002124
2125int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2126 void *buffer, int transfer_len, struct devrequest *setup)
2127{
2128 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2129 transfer_len, setup);
2130}
Hans de Goede8a62c502015-05-10 14:10:25 +02002131#endif
2132
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01002133#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02002134static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2135 unsigned long pipe, void *buffer, int length,
2136 struct devrequest *setup)
2137{
2138 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2139
2140 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2141 length, setup);
2142}
2143
2144static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2145 unsigned long pipe, void *buffer, int length)
2146{
2147 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2148
2149 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2150}
2151
2152static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2153 unsigned long pipe, void *buffer, int length,
2154 int interval)
2155{
2156 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2157
2158 return submit_common_msg(ohci, udev, pipe, buffer, length,
2159 NULL, interval);
2160}
2161
Hans de Goede5477e6f2015-05-13 14:42:17 +02002162static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2163 struct usb_device *udev, unsigned long pipe, int queuesize,
2164 int elementsize, void *buffer, int interval)
2165{
2166 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2167
2168 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2169 buffer, interval);
2170}
2171
2172static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2173 struct int_queue *queue)
2174{
2175 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2176
2177 return _ohci_poll_int_queue(ohci, udev, queue);
2178}
2179
2180static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2181 struct int_queue *queue)
2182{
2183 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2184
2185 return _ohci_destroy_int_queue(ohci, udev, queue);
2186}
2187
Hans de Goede8a62c502015-05-10 14:10:25 +02002188int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2189{
2190 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2191 ohci_t *ohci = dev_get_priv(dev);
2192 u32 reg;
2193
2194 priv->desc_before_addr = true;
2195
2196 ohci->regs = regs;
2197 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2198 if (!ohci->hcca)
2199 return -ENOMEM;
2200 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
Hans de Goede66cb3e42015-10-20 18:39:29 +02002201 flush_dcache_hcca(ohci->hcca);
Hans de Goede8a62c502015-05-10 14:10:25 +02002202
2203 if (hc_reset(ohci) < 0)
2204 return -EIO;
2205
2206 if (hc_start(ohci) < 0)
2207 return -EIO;
2208
2209 reg = ohci_readl(&regs->revision);
2210 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2211
2212 return 0;
2213}
2214
2215int ohci_deregister(struct udevice *dev)
2216{
2217 ohci_t *ohci = dev_get_priv(dev);
2218
2219 if (hc_reset(ohci) < 0)
2220 return -EIO;
2221
2222 free(ohci->hcca);
2223
2224 return 0;
2225}
2226
2227struct dm_usb_ops ohci_usb_ops = {
2228 .control = ohci_submit_control_msg,
2229 .bulk = ohci_submit_bulk_msg,
2230 .interrupt = ohci_submit_int_msg,
Hans de Goede5477e6f2015-05-13 14:42:17 +02002231 .create_int_queue = ohci_create_int_queue,
2232 .poll_int_queue = ohci_poll_int_queue,
2233 .destroy_int_queue = ohci_destroy_int_queue,
Hans de Goede8a62c502015-05-10 14:10:25 +02002234};
2235
2236#endif