blob: 58aa824ec0347685e0b08fac304eb6db6e46cf41 [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) || \
Heiko Schocher2e1134f2019-07-16 10:49:05 +020053 defined(CONFIG_PCI_OHCI) || \
54 defined(CONFIG_DM_PCI) || \
55 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020056# define OHCI_USE_NPS /* force NoPowerSwitching mode */
57#endif
58
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020059#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010060#undef DEBUG
61#undef SHOW_INFO
62#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020063
64/* For initializing controller (mask in an HCFS mode too) */
65#define OHCI_CONTROL_INIT \
66 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
67
Heiko Schocherfe410a42019-07-16 10:49:03 +020068#if !CONFIG_IS_ENABLED(DM_USB)
Zhang Wei8d15efa2007-06-06 10:08:14 +020069#ifdef CONFIG_PCI_OHCI
70static struct pci_device_id ohci_pci_ids[] = {
71 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saadae3b28422007-09-17 17:04:47 +020072 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew923f43a2008-01-11 20:42:58 -060073 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei8d15efa2007-06-06 10:08:14 +020074 /* Please add supported PCI OHCI controller ids here */
75 {0, 0}
76};
77#endif
Heiko Schocherfe410a42019-07-16 10:49:03 +020078#endif
Zhang Wei8d15efa2007-06-06 10:08:14 +020079
Yuri Tikhonov11af42c2008-09-04 11:19:05 +020080#ifdef CONFIG_PCI_EHCI_DEVNO
81static struct pci_device_id ehci_pci_ids[] = {
82 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
83 /* Please add supported PCI EHCI controller ids here */
84 {0, 0}
85};
86#endif
87
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020088#ifdef DEBUG
89#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
90#else
Remy Bohmer172a1132008-09-16 14:55:43 +020091#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020092#endif /* DEBUG */
93#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020094#ifdef SHOW_INFO
95#define info(format, arg...) printf("INFO: " format "\n", ## arg)
96#else
Remy Bohmer172a1132008-09-16 14:55:43 +020097#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020098#endif
99
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200100#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200101# define m16_swap(x) cpu_to_be16(x)
102# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +0100103#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200104# define m16_swap(x) cpu_to_le16(x)
105# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200106#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200107
Wu, Joshc3857142015-07-27 11:40:18 +0800108/* We really should do proper cache flushing everywhere */
Hans de Goede8e41f662015-05-05 23:56:13 +0200109#define flush_dcache_buffer(addr, size) \
110 flush_dcache_range((unsigned long)(addr), \
111 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
112#define invalidate_dcache_buffer(addr, size) \
113 invalidate_dcache_range((unsigned long)(addr), \
114 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
Hans de Goede8e41f662015-05-05 23:56:13 +0200115
116/* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
117#define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
118#define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
119#define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
120#define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
121#define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
122#define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
123#define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
124#define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
125
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100126#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goeded6cc1d12015-05-10 14:10:24 +0200127/*
128 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
129 * them around when building for older boards not yet converted to the dm
130 * just in case (to avoid regressions), for dm this turns them into nops.
131 */
132#define ohci_mdelay(x)
133#else
134#define ohci_mdelay(x) mdelay(x)
135#endif
136
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100137#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200138/* global ohci_t */
139static ohci_t gohci;
140/* this must be aligned to a 256 byte boundary */
141struct ohci_hcca ghcca[1];
Hans de Goede8a62c502015-05-10 14:10:25 +0200142#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200143
Hans de Goede516c9872015-05-05 23:56:11 +0200144/* mapping of the OHCI CC status to error codes */
145static int cc_to_error[16] = {
146 /* No Error */ 0,
147 /* CRC Error */ USB_ST_CRC_ERR,
148 /* Bit Stuff */ USB_ST_BIT_ERR,
149 /* Data Togg */ USB_ST_CRC_ERR,
150 /* Stall */ USB_ST_STALLED,
151 /* DevNotResp */ -1,
152 /* PIDCheck */ USB_ST_BIT_ERR,
153 /* UnExpPID */ USB_ST_BIT_ERR,
154 /* DataOver */ USB_ST_BUF_ERR,
155 /* DataUnder */ USB_ST_BUF_ERR,
156 /* reservd */ -1,
157 /* reservd */ -1,
158 /* BufferOver */ USB_ST_BUF_ERR,
159 /* BuffUnder */ USB_ST_BUF_ERR,
160 /* Not Access */ -1,
161 /* Not Access */ -1
162};
163
164static const char *cc_to_string[16] = {
165 "No Error",
166 "CRC: Last data packet from endpoint contained a CRC error.",
167 "BITSTUFFING: Last data packet from endpoint contained a bit " \
168 "stuffing violation",
169 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
170 "that did not match the expected value.",
171 "STALL: TD was moved to the Done Queue because the endpoint returned" \
172 " a STALL PID",
173 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
174 "not provide a handshake (OUT)",
175 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
176 "(IN) or handshake (OUT)",
177 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
178 "value is not defined.",
179 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
180 "either the size of the maximum data packet allowed\n" \
181 "from the endpoint (found in MaximumPacketSize field\n" \
182 "of ED) or the remaining buffer size.",
183 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
184 "and that amount was not sufficient to fill the\n" \
185 "specified buffer",
186 "reserved1",
187 "reserved2",
188 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
189 "than it could be written to system memory",
190 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
191 "system memory fast enough to keep up with data USB " \
192 "data rate.",
193 "NOT ACCESSED: This code is set by software before the TD is placed" \
194 "on a list to be processed by the HC.(1)",
195 "NOT ACCESSED: This code is set by software before the TD is placed" \
196 "on a list to be processed by the HC.(2)",
197};
198
Remy Bohmer172a1132008-09-16 14:55:43 +0200199static inline u32 roothub_a(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500200 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200201static inline u32 roothub_b(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500202 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200203static inline u32 roothub_status(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500204 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200205static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500206 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200207
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200208/* forward declaration */
Hans de Goede2a240e72015-05-05 23:56:07 +0200209static int hc_interrupt(ohci_t *ohci);
210static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
211 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200212 struct devrequest *setup, urb_priv_t *urb,
213 int interval);
Hans de Goede516c9872015-05-05 23:56:11 +0200214static int ep_link(ohci_t * ohci, ed_t * ed);
215static int ep_unlink(ohci_t * ohci, ed_t * ed);
216static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
217 unsigned long pipe, int interval, int load);
218
219/*-------------------------------------------------------------------------*/
220
221/* TDs ... */
222static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
223{
224 int i;
225 struct td *td;
226
227 td = NULL;
228 for (i = 0; i < NUM_TD; i++)
229 {
230 if (ohci_dev->tds[i].usb_dev == NULL)
231 {
232 td = &ohci_dev->tds[i];
233 td->usb_dev = usb_dev;
234 break;
235 }
236 }
237
238 return td;
239}
240
241static inline void ed_free(struct ed *ed)
242{
243 ed->usb_dev = NULL;
244}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200245
246/*-------------------------------------------------------------------------*
247 * URB support functions
248 *-------------------------------------------------------------------------*/
249
250/* free HCD-private data associated with this URB */
251
Remy Bohmer172a1132008-09-16 14:55:43 +0200252static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200253{
254 int i;
255 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200256 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200257
258 last = urb->length - 1;
259 if (last >= 0) {
260 for (i = 0; i <= last; i++) {
261 td = urb->td[i];
262 if (td) {
263 td->usb_dev = NULL;
264 urb->td[i] = NULL;
265 }
266 }
267 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200268 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200269}
270
271/*-------------------------------------------------------------------------*/
272
273#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +0200274static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200275
276/* debug| print the main components of an URB
277 * small: 0) header + data packets 1) just header */
278
Hans de Goede2a240e72015-05-05 23:56:07 +0200279static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer172a1132008-09-16 14:55:43 +0200280 unsigned long pipe, void *buffer, int transfer_len,
281 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200282{
Remy Bohmer172a1132008-09-16 14:55:43 +0200283 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200284 str,
Hans de Goede2a240e72015-05-05 23:56:07 +0200285 sohci_get_current_frame_number(ohci),
Remy Bohmer172a1132008-09-16 14:55:43 +0200286 usb_pipedevice(pipe),
287 usb_pipeendpoint(pipe),
288 usb_pipeout(pipe)? 'O': 'I',
289 usb_pipetype(pipe) < 2 ? \
290 (usb_pipeint(pipe)? "INTR": "ISOC"): \
291 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200292 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200293 transfer_len, dev->status);
294#ifdef OHCI_VERBOSE_DEBUG
295 if (!small) {
296 int i, len;
297
Remy Bohmer172a1132008-09-16 14:55:43 +0200298 if (usb_pipecontrol(pipe)) {
299 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200300 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200301 printf(" %02x", ((__u8 *) setup) [i]);
302 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200303 }
304 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200305 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200306 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200307 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200308 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200309 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200310 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200311 printf(" %02x", ((__u8 *) buffer) [i]);
312 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200313 }
314 }
315#endif
316}
317
Remy Bohmer172a1132008-09-16 14:55:43 +0200318/* just for debugging; prints non-empty branches of the int ed tree
319 * inclusive iso eds */
320void ep_print_int_eds(ohci_t *ohci, char *str)
321{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200322 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200323 __u32 *ed_p;
324 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200325 j = 5;
326 ed_p = &(ohci->hcca->int_table [i]);
327 if (*ed_p == 0)
328 continue;
Hans de Goede8e41f662015-05-05 23:56:13 +0200329 invalidate_dcache_ed(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200330 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200331 while (*ed_p != 0 && j--) {
332 ed_t *ed = (ed_t *)m32_swap(ed_p);
Hans de Goede8e41f662015-05-05 23:56:13 +0200333 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200334 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200335 ed_p = &ed->hwNextED;
336 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200337 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200338 }
339}
340
Remy Bohmer172a1132008-09-16 14:55:43 +0200341static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200342{
Remy Bohmer172a1132008-09-16 14:55:43 +0200343 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200344 label,
345 mask,
346 (mask & OHCI_INTR_MIE) ? " MIE" : "",
347 (mask & OHCI_INTR_OC) ? " OC" : "",
348 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
349 (mask & OHCI_INTR_FNO) ? " FNO" : "",
350 (mask & OHCI_INTR_UE) ? " UE" : "",
351 (mask & OHCI_INTR_RD) ? " RD" : "",
352 (mask & OHCI_INTR_SF) ? " SF" : "",
353 (mask & OHCI_INTR_WDH) ? " WDH" : "",
354 (mask & OHCI_INTR_SO) ? " SO" : ""
355 );
356}
357
Remy Bohmer172a1132008-09-16 14:55:43 +0200358static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200359{
360 ed_t *edp = (ed_t *)value;
361
362 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200363 dbg("%s %08x", label, value);
Hans de Goede8e41f662015-05-05 23:56:13 +0200364 invalidate_dcache_ed(edp);
Remy Bohmer172a1132008-09-16 14:55:43 +0200365 dbg("%08x", edp->hwINFO);
366 dbg("%08x", edp->hwTailP);
367 dbg("%08x", edp->hwHeadP);
368 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200369 }
370}
371
Remy Bohmer172a1132008-09-16 14:55:43 +0200372static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200373{
374 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200375 case OHCI_USB_RESET: return "reset";
376 case OHCI_USB_RESUME: return "resume";
377 case OHCI_USB_OPER: return "operational";
378 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200379 }
380 return "?";
381}
382
383/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200384static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200385{
386 struct ohci_regs *regs = controller->regs;
387 __u32 temp;
388
Becky Bruce4abaacb2010-06-30 13:05:44 -0500389 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200390 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200391 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200392
Becky Bruce4abaacb2010-06-30 13:05:44 -0500393 temp = ohci_readl(&regs->control);
Remy Bohmer172a1132008-09-16 14:55:43 +0200394 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200395 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
396 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
397 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200398 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200399 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
400 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
401 (temp & OHCI_CTRL_IE) ? " IE" : "",
402 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
403 temp & OHCI_CTRL_CBSR
404 );
405
Becky Bruce4abaacb2010-06-30 13:05:44 -0500406 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200407 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200408 (temp & OHCI_SOC) >> 16,
409 (temp & OHCI_OCR) ? " OCR" : "",
410 (temp & OHCI_BLF) ? " BLF" : "",
411 (temp & OHCI_CLF) ? " CLF" : "",
412 (temp & OHCI_HCR) ? " HCR" : ""
413 );
414
Becky Bruce4abaacb2010-06-30 13:05:44 -0500415 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
416 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200417
Becky Bruce4abaacb2010-06-30 13:05:44 -0500418 maybe_print_eds("ed_periodcurrent",
419 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200420
Becky Bruce4abaacb2010-06-30 13:05:44 -0500421 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
422 maybe_print_eds("ed_controlcurrent",
423 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200424
Becky Bruce4abaacb2010-06-30 13:05:44 -0500425 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
426 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200427
Becky Bruce4abaacb2010-06-30 13:05:44 -0500428 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200429}
430
Remy Bohmer172a1132008-09-16 14:55:43 +0200431static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200432{
433 __u32 temp, ndp, i;
434
Remy Bohmer172a1132008-09-16 14:55:43 +0200435 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200436 ndp = (temp & RH_A_NDP);
437#ifdef CONFIG_AT91C_PQFP_UHPBUG
438 ndp = (ndp == 2) ? 1:0;
439#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200440 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200441 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200442 ((temp & RH_A_POTPGT) >> 24) & 0xff,
443 (temp & RH_A_NOCP) ? " NOCP" : "",
444 (temp & RH_A_OCPM) ? " OCPM" : "",
445 (temp & RH_A_DT) ? " DT" : "",
446 (temp & RH_A_NPS) ? " NPS" : "",
447 (temp & RH_A_PSM) ? " PSM" : "",
448 ndp
449 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200450 temp = roothub_b(controller);
451 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200452 temp,
453 (temp & RH_B_PPCM) >> 16,
454 (temp & RH_B_DR)
455 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200456 temp = roothub_status(controller);
457 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200458 temp,
459 (temp & RH_HS_CRWE) ? " CRWE" : "",
460 (temp & RH_HS_OCIC) ? " OCIC" : "",
461 (temp & RH_HS_LPSC) ? " LPSC" : "",
462 (temp & RH_HS_DRWE) ? " DRWE" : "",
463 (temp & RH_HS_OCI) ? " OCI" : "",
464 (temp & RH_HS_LPS) ? " LPS" : ""
465 );
466 }
467
468 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200469 temp = roothub_portstatus(controller, i);
470 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 +0200471 i,
472 temp,
473 (temp & RH_PS_PRSC) ? " PRSC" : "",
474 (temp & RH_PS_OCIC) ? " OCIC" : "",
475 (temp & RH_PS_PSSC) ? " PSSC" : "",
476 (temp & RH_PS_PESC) ? " PESC" : "",
477 (temp & RH_PS_CSC) ? " CSC" : "",
478
479 (temp & RH_PS_LSDA) ? " LSDA" : "",
480 (temp & RH_PS_PPS) ? " PPS" : "",
481 (temp & RH_PS_PRS) ? " PRS" : "",
482 (temp & RH_PS_POCI) ? " POCI" : "",
483 (temp & RH_PS_PSS) ? " PSS" : "",
484
485 (temp & RH_PS_PES) ? " PES" : "",
486 (temp & RH_PS_CCS) ? " CCS" : ""
487 );
488 }
489}
490
Remy Bohmer172a1132008-09-16 14:55:43 +0200491static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200492{
Remy Bohmer172a1132008-09-16 14:55:43 +0200493 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200494
495 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200496 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200497 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200498 ep_print_int_eds(controller, "hcca");
Hans de Goede8e41f662015-05-05 23:56:13 +0200499 invalidate_dcache_hcca(controller->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200500 dbg("hcca frame #%04x", controller->hcca->frame_no);
501 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100502}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200503#endif /* DEBUG */
504
505/*-------------------------------------------------------------------------*
506 * Interface functions (URB)
507 *-------------------------------------------------------------------------*/
508
509/* get a transfer request */
510
Hans de Goede0c0e9602015-05-05 23:56:08 +0200511int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
512 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200513{
Remy Bohmer172a1132008-09-16 14:55:43 +0200514 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200515 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200516 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200517 struct usb_device *dev = urb->dev;
518 unsigned long pipe = urb->pipe;
519 void *buffer = urb->transfer_buffer;
520 int transfer_len = urb->transfer_buffer_length;
521 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200522
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200523 /* when controller's hung, permit only roothub cleanup attempts
524 * such as powering down ports */
525 if (ohci->disabled) {
526 err("sohci_submit_job: EPIPE");
527 return -1;
528 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200529
Remy Bohmer172a1132008-09-16 14:55:43 +0200530 /* we're about to begin a new transaction here so mark the
531 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200532 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200533
534 /* every endpoint has a ed, locate and fill it */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200535 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer172a1132008-09-16 14:55:43 +0200536 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200537 err("sohci_submit_job: ENOMEM");
538 return -1;
539 }
540
541 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200542 switch (usb_pipetype(pipe)) {
543 case PIPE_BULK: /* one TD for every 4096 Byte */
544 size = (transfer_len - 1) / 4096 + 1;
545 break;
546 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
547 size = (transfer_len == 0)? 2:
548 (transfer_len - 1) / 4096 + 3;
549 break;
550 case PIPE_INTERRUPT: /* 1 TD */
551 size = 1;
552 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200553 }
554
Zhang Wei8d15efa2007-06-06 10:08:14 +0200555 ed->purb = urb;
556
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200557 if (size >= (N_URB_TD - 1)) {
558 err("need %d TDs, only have %d", size, N_URB_TD);
559 return -1;
560 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200561 purb_priv->pipe = pipe;
562
563 /* fill the private part of the URB */
564 purb_priv->length = size;
565 purb_priv->ed = ed;
566 purb_priv->actual_length = 0;
567
568 /* allocate the TDs */
569 /* note that td[0] was allocated in ep_add_ed */
570 for (i = 0; i < size; i++) {
Hans de Goedee5ef4212015-05-05 23:56:09 +0200571 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200572 if (!purb_priv->td[i]) {
573 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200574 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200575 err("sohci_submit_job: ENOMEM");
576 return -1;
577 }
578 }
579
580 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200581 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200582 err("sohci_submit_job: EINVAL");
583 return -1;
584 }
585
586 /* link the ed into a chain if is not already */
587 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200588 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200589
590 /* fill the TDs and link it to the ed */
Hans de Goede2a240e72015-05-05 23:56:07 +0200591 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200592 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200593
594 return 0;
595}
596
597/*-------------------------------------------------------------------------*/
598
599#ifdef DEBUG
600/* tell us the current USB frame number */
Hans de Goede2a240e72015-05-05 23:56:07 +0200601static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200602{
Hans de Goede8e41f662015-05-05 23:56:13 +0200603 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200604 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200605}
606#endif
607
608/*-------------------------------------------------------------------------*
609 * ED handling functions
610 *-------------------------------------------------------------------------*/
611
Zhang Wei8d15efa2007-06-06 10:08:14 +0200612/* search for the right branch to insert an interrupt ed into the int tree
613 * do some load ballancing;
614 * returns the branch and
615 * sets the interval to interval = 2^integer (ld (interval)) */
616
Remy Bohmer172a1132008-09-16 14:55:43 +0200617static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200618{
619 int i, branch = 0;
620
621 /* search for the least loaded interrupt endpoint
622 * branch of all 32 branches
623 */
624 for (i = 0; i < 32; i++)
625 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
626 branch = i;
627
628 branch = branch % interval;
629 for (i = branch; i < 32; i += interval)
630 ohci->ohci_int_load [i] += load;
631
632 return branch;
633}
634
635/*-------------------------------------------------------------------------*/
636
637/* 2^int( ld (inter)) */
638
Remy Bohmer172a1132008-09-16 14:55:43 +0200639static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200640{
641 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200642 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200643 return 1 << i;
644}
645
646/*-------------------------------------------------------------------------*/
647
648/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200649 * in order to process it sequentially the indexes of the branches have to
650 * be mapped the mapping reverses the bits of a word of num_bits length */
651static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200652{
653 int i, wout = 0;
654
655 for (i = 0; i < num_bits; i++)
656 wout |= (((word >> i) & 1) << (num_bits - i - 1));
657 return wout;
658}
659
660/*-------------------------------------------------------------------------*
661 * ED handling functions
662 *-------------------------------------------------------------------------*/
663
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200664/* link an ed into one of the HC chains */
665
Remy Bohmer172a1132008-09-16 14:55:43 +0200666static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200667{
668 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200669 int int_branch;
670 int i;
671 int inter;
672 int interval;
673 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200674 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200675
676 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200677 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200678
679 switch (ed->type) {
680 case PIPE_CONTROL:
681 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200682 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200683 if (ohci->ed_controltail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100684 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200685 else
686 ohci->ed_controltail->hwNextED =
687 m32_swap((unsigned long)ed);
688
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200689 ed->ed_prev = ohci->ed_controltail;
690 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
691 !ohci->ed_rm_list[1] && !ohci->sleeping) {
692 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500693 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200694 }
695 ohci->ed_controltail = edi;
696 break;
697
698 case PIPE_BULK:
699 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200700 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200701 if (ohci->ed_bulktail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100702 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200703 else
704 ohci->ed_bulktail->hwNextED =
705 m32_swap((unsigned long)ed);
706
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200707 ed->ed_prev = ohci->ed_bulktail;
708 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
709 !ohci->ed_rm_list[1] && !ohci->sleeping) {
710 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500711 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200712 }
713 ohci->ed_bulktail = edi;
714 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200715
716 case PIPE_INTERRUPT:
717 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200718 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200719 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200720 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200721 ed->int_branch = int_branch;
722
Remy Bohmer172a1132008-09-16 14:55:43 +0200723 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200724 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200725 for (ed_p = &(ohci->hcca->int_table[\
726 ep_rev(5, i) + int_branch]);
727 (*ed_p != 0) &&
728 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200729 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200730 inter = ep_rev(6,
731 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200732 ed->hwNextED = *ed_p;
Hans de Goede8e41f662015-05-05 23:56:13 +0200733 flush_dcache_ed(ed);
Martin Krause8a588192007-08-21 12:40:34 +0200734 *ed_p = m32_swap((unsigned long)ed);
Hans de Goede8e41f662015-05-05 23:56:13 +0200735 flush_dcache_hcca(ohci->hcca);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200736 }
737 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200738 }
739 return 0;
740}
741
742/*-------------------------------------------------------------------------*/
743
Zhang Wei8d15efa2007-06-06 10:08:14 +0200744/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200745static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
746 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200747{
Hans de Goede8e41f662015-05-05 23:56:13 +0200748 __maybe_unused unsigned long aligned_ed_p;
749
Zhang Wei8d15efa2007-06-06 10:08:14 +0200750 for (; index < NUM_INTS; index += period) {
751 __u32 *ed_p = &ohci->hcca->int_table [index];
752
753 /* ED might have been unlinked through another path */
754 while (*ed_p != 0) {
Andre Przywarac114a982016-10-21 02:24:29 +0100755 if (((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200756 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200757 *ed_p = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200758 aligned_ed_p = (unsigned long)ed_p;
759 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
760 flush_dcache_range(aligned_ed_p,
761 aligned_ed_p + ARCH_DMA_MINALIGN);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200762 break;
763 }
Andre Przywarac114a982016-10-21 02:24:29 +0100764 ed_p = &(((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200765 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200766 }
767 }
768}
769
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200770/* unlink an ed from one of the HC chains.
771 * just the link to the ed is unlinked.
772 * the link from the ed still points to another operational ed or 0
773 * so the HC can eventually finish the processing of the unlinked ed */
774
Remy Bohmer172a1132008-09-16 14:55:43 +0200775static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200776{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100777 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200778 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100779
Remy Bohmer172a1132008-09-16 14:55:43 +0200780 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Hans de Goede8e41f662015-05-05 23:56:13 +0200781 flush_dcache_ed(ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200782
783 switch (ed->type) {
784 case PIPE_CONTROL:
785 if (ed->ed_prev == NULL) {
786 if (!ed->hwNextED) {
787 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500788 ohci_writel(ohci->hc_control,
789 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200790 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500791 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200792 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200793 } else {
794 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200795 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200796 }
797 if (ohci->ed_controltail == ed) {
798 ohci->ed_controltail = ed->ed_prev;
799 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100800 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200801 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200802 }
803 break;
804
805 case PIPE_BULK:
806 if (ed->ed_prev == NULL) {
807 if (!ed->hwNextED) {
808 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500809 ohci_writel(ohci->hc_control,
810 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200811 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500812 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200813 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200814 } else {
815 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200816 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200817 }
818 if (ohci->ed_bulktail == ed) {
819 ohci->ed_bulktail = ed->ed_prev;
820 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100821 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200822 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200823 }
824 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200825
826 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200827 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200828 for (i = ed->int_branch; i < 32; i += ed->int_interval)
829 ohci->ohci_int_load[i] -= ed->int_load;
830 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200831 }
832 ed->state = ED_UNLINK;
833 return 0;
834}
835
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200836/*-------------------------------------------------------------------------*/
837
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200838/* add/reinit an endpoint; this should be done once at the
839 * usb_set_configuration command, but the USB stack is a little bit
840 * stateless so we do it at every transaction if the state of the ed
841 * is ED_NEW then a dummy td is added and the state is changed to
842 * ED_UNLINK in all other cases the state is left unchanged the ed
843 * info fields are setted anyway even though most of them should not
844 * change
845 */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200846static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
847 unsigned long pipe, int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200848{
849 td_t *td;
850 ed_t *ed_ret;
851 volatile ed_t *ed;
852
Hans de Goede0c0e9602015-05-05 23:56:08 +0200853 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer172a1132008-09-16 14:55:43 +0200854 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200855
856 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
857 err("ep_add_ed: pending delete");
858 /* pending delete request */
859 return NULL;
860 }
861
862 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200863 /* dummy td; end of td list for ed */
Hans de Goedee5ef4212015-05-05 23:56:09 +0200864 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer172a1132008-09-16 14:55:43 +0200865 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200866 ed->hwHeadP = ed->hwTailP;
867 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200868 ed->type = usb_pipetype(pipe);
Hans de Goede0c0e9602015-05-05 23:56:08 +0200869 ohci_dev->ed_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200870 }
871
Remy Bohmer172a1132008-09-16 14:55:43 +0200872 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
873 | usb_pipeendpoint(pipe) << 7
874 | (usb_pipeisoc(pipe)? 0x8000: 0)
875 | (usb_pipecontrol(pipe)? 0: \
876 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000877 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer172a1132008-09-16 14:55:43 +0200878 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200879
Zhang Wei8d15efa2007-06-06 10:08:14 +0200880 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
881 ed->int_period = interval;
882 ed->int_load = load;
883 }
884
Hans de Goede8e41f662015-05-05 23:56:13 +0200885 flush_dcache_ed(ed);
886
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200887 return ed_ret;
888}
889
890/*-------------------------------------------------------------------------*
891 * TD handling functions
892 *-------------------------------------------------------------------------*/
893
894/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
895
Remy Bohmer172a1132008-09-16 14:55:43 +0200896static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200897 void *data, int len,
898 struct usb_device *dev, int index, urb_priv_t *urb_priv)
899{
900 volatile td_t *td, *td_pt;
901#ifdef OHCI_FILL_TRACE
902 int i;
903#endif
904
905 if (index > urb_priv->length) {
906 err("index > length");
907 return;
908 }
909 /* use this td as the next dummy */
910 td_pt = urb_priv->td [index];
911 td_pt->hwNextTD = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200912 flush_dcache_td(td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200913
914 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200915 td = urb_priv->td [index] =
Andre Przywarac114a982016-10-21 02:24:29 +0100916 (td_t *)(uintptr_t)
917 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200918
919 td->ed = urb_priv->ed;
920 td->next_dl_td = NULL;
921 td->index = index;
Andre Przywarac114a982016-10-21 02:24:29 +0100922 td->data = (uintptr_t)data;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200923#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200924 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200925 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200926 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200927 printf("\n");
928 }
929#endif
930 if (!len)
931 data = 0;
932
Remy Bohmer172a1132008-09-16 14:55:43 +0200933 td->hwINFO = m32_swap(info);
934 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200935 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200936 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200937 else
938 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200939
940 td->hwNextTD = m32_swap((unsigned long)td_pt);
Hans de Goede8e41f662015-05-05 23:56:13 +0200941 flush_dcache_td(td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200942
943 /* append to queue */
944 td->ed->hwTailP = td->hwNextTD;
Hans de Goede8e41f662015-05-05 23:56:13 +0200945 flush_dcache_ed(td->ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200946}
947
948/*-------------------------------------------------------------------------*/
949
950/* prepare all TDs of a transfer */
951
Hans de Goede2a240e72015-05-05 23:56:07 +0200952static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
953 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200954 struct devrequest *setup, urb_priv_t *urb,
955 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200956{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200957 int data_len = transfer_len;
958 void *data;
959 int cnt = 0;
960 __u32 info = 0;
961 unsigned int toggle = 0;
962
Hans de Goede8e41f662015-05-05 23:56:13 +0200963 flush_dcache_buffer(buffer, data_len);
964
Remy Bohmer172a1132008-09-16 14:55:43 +0200965 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700966 * bits for resetting */
Remy Bohmer172a1132008-09-16 14:55:43 +0200967 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200968 toggle = TD_T_TOGGLE;
969 } else {
970 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200971 usb_settoggle(dev, usb_pipeendpoint(pipe),
972 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200973 }
974 urb->td_cnt = 0;
975 if (data_len)
976 data = buffer;
977 else
978 data = 0;
979
Remy Bohmer172a1132008-09-16 14:55:43 +0200980 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200981 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200982 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200983 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200984 while (data_len > 4096) {
985 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
986 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200987 data += 4096; data_len -= 4096; cnt++;
988 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200989 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200990 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200991 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
992 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200993 cnt++;
994
Remy Bohmer172a1132008-09-16 14:55:43 +0200995 if (!ohci->sleeping) {
996 /* start bulk list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500997 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200998 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200999 break;
1000
1001 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +02001002 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001003 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001004 flush_dcache_buffer(setup, 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001005 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1006
1007 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001008 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001009 info = usb_pipeout(pipe)?
1010 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1011 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001012 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +02001013 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001014 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001015
1016 /* Status phase */
Hans de Goede60f4bd02015-05-05 23:56:12 +02001017 info = (usb_pipeout(pipe) || data_len == 0) ?
Remy Bohmer172a1132008-09-16 14:55:43 +02001018 TD_CC | TD_DP_IN | TD_T_DATA1:
1019 TD_CC | TD_DP_OUT | TD_T_DATA1;
1020 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1021
1022 if (!ohci->sleeping) {
1023 /* start Control list */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001024 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001025 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001026 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001027
1028 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +02001029 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +02001030 TD_CC | TD_DP_OUT | toggle:
1031 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +02001032 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001033 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001034 }
1035 if (urb->length != cnt)
1036 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1037}
1038
1039/*-------------------------------------------------------------------------*
1040 * Done List handling functions
1041 *-------------------------------------------------------------------------*/
1042
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001043/* calculate the transfer length and update the urb */
1044
Remy Bohmer172a1132008-09-16 14:55:43 +02001045static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001046{
Wolfgang Denk0ef5f622011-10-05 23:03:43 +02001047 __u32 tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001048 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001049
Remy Bohmer172a1132008-09-16 14:55:43 +02001050 tdBE = m32_swap(td->hwBE);
1051 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001052
Remy Bohmer47e72082008-10-10 10:23:21 +02001053 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001054 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1055 if (tdBE != 0) {
1056 if (td->hwCBP == 0)
1057 lurb_priv->actual_length += tdBE - td->data + 1;
1058 else
1059 lurb_priv->actual_length += tdCBP - td->data;
1060 }
1061 }
1062}
1063
1064/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001065static void check_status(td_t *td_list)
1066{
1067 urb_priv_t *lurb_priv = td_list->ed->purb;
1068 int urb_len = lurb_priv->length;
1069 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1070 int cc;
1071
1072 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1073 if (cc) {
1074 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1075
Hans de Goede8e41f662015-05-05 23:56:13 +02001076 invalidate_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001077 if (*phwHeadP & m32_swap(0x1)) {
1078 if (lurb_priv &&
1079 ((td_list->index + 1) < urb_len)) {
1080 *phwHeadP =
1081 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1082 m32_swap(0xfffffff0)) |
1083 (*phwHeadP & m32_swap(0x2));
1084
1085 lurb_priv->td_cnt += urb_len -
1086 td_list->index - 1;
1087 } else
1088 *phwHeadP &= m32_swap(0xfffffff2);
Hans de Goede8e41f662015-05-05 23:56:13 +02001089 flush_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001090 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001091 }
1092}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001093
1094/* replies to the request have to be on a FIFO basis so
1095 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +02001096static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001097{
Andre Przywarac114a982016-10-21 02:24:29 +01001098 uintptr_t td_list_hc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001099 td_t *td_rev = NULL;
1100 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001101
Hans de Goede8e41f662015-05-05 23:56:13 +02001102 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +02001103 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001104 ohci->hcca->done_head = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001105 flush_dcache_hcca(ohci->hcca);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001106
1107 while (td_list_hc) {
1108 td_list = (td_t *)td_list_hc;
Hans de Goede8e41f662015-05-05 23:56:13 +02001109 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001110 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001111 td_list->next_dl_td = td_rev;
1112 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001113 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001114 }
1115 return td_list;
1116}
1117
1118/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001119/*-------------------------------------------------------------------------*/
1120
1121static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1122{
1123 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
Hans de Goede27873372015-05-10 14:10:22 +02001124 urb->finished = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001125 else
1126 dbg("finish_urb: strange.., ED state %x, \n", status);
1127}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001128
Remy Bohmer172a1132008-09-16 14:55:43 +02001129/*
1130 * Used to take back a TD from the host controller. This would normally be
1131 * called from within dl_done_list, however it may be called directly if the
1132 * HC no longer sees the TD and it has not appeared on the donelist (after
1133 * two frames). This bug has been observed on ZF Micro systems.
1134 */
1135static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001136{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001137 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001138 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001139 int stat = 0;
1140 /* urb_t *urb; */
1141 urb_priv_t *lurb_priv;
1142 __u32 tdINFO, edHeadP, edTailP;
1143
Hans de Goede8e41f662015-05-05 23:56:13 +02001144 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001145 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001146
Remy Bohmer172a1132008-09-16 14:55:43 +02001147 ed = td_list->ed;
1148 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001149
Remy Bohmer172a1132008-09-16 14:55:43 +02001150 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001151
Remy Bohmer172a1132008-09-16 14:55:43 +02001152 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001153
Remy Bohmer172a1132008-09-16 14:55:43 +02001154 /* error code of transfer */
1155 cc = TD_CC_GET(tdINFO);
1156 if (cc) {
1157 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1158 stat = cc_to_error[cc];
1159 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001160
Remy Bohmer172a1132008-09-16 14:55:43 +02001161 /* see if this done list makes for all TD's of current URB,
1162 * and mark the URB finished if so */
1163 if (lurb_priv->td_cnt == lurb_priv->length)
1164 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001165
Remy Bohmer172a1132008-09-16 14:55:43 +02001166 dbg("dl_done_list: processing TD %x, len %x\n",
1167 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001168
Remy Bohmer47e72082008-10-10 10:23:21 +02001169 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Hans de Goede8e41f662015-05-05 23:56:13 +02001170 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001171 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1172 edTailP = m32_swap(ed->hwTailP);
1173
1174 /* unlink eds if they are not busy */
1175 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1176 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001177 }
1178 return stat;
1179}
1180
Remy Bohmer172a1132008-09-16 14:55:43 +02001181static int dl_done_list(ohci_t *ohci)
1182{
1183 int stat = 0;
1184 td_t *td_list = dl_reverse_done_list(ohci);
1185
1186 while (td_list) {
1187 td_t *td_next = td_list->next_dl_td;
1188 stat = takeback_td(ohci, td_list);
1189 td_list = td_next;
1190 }
1191 return stat;
1192}
1193
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001194/*-------------------------------------------------------------------------*
1195 * Virtual Root Hub
1196 *-------------------------------------------------------------------------*/
1197
Stephen Warren39c89682014-02-13 21:15:18 -07001198#include <usbroothubdes.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001199
1200/* Hub class-specific descriptor is constructed dynamically */
1201
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001202/*-------------------------------------------------------------------------*/
1203
1204#define OK(x) len = (x); break
1205#ifdef DEBUG
Becky Bruce4abaacb2010-06-30 13:05:44 -05001206#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001207 &ohci->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +02001208#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goede2a240e72015-05-05 23:56:07 +02001209 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001210#else
Hans de Goede2a240e72015-05-05 23:56:07 +02001211#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001212#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001213 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001214#endif
Hans de Goede2a240e72015-05-05 23:56:07 +02001215#define RD_RH_STAT roothub_status(ohci)
1216#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001217
1218/* request to virtual root hub */
1219
1220int rh_check_port_status(ohci_t *controller)
1221{
1222 __u32 temp, ndp, i;
1223 int res;
1224
1225 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001226 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001227 ndp = (temp & RH_A_NDP);
1228#ifdef CONFIG_AT91C_PQFP_UHPBUG
1229 ndp = (ndp == 2) ? 1:0;
1230#endif
1231 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001232 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001233 /* check for a device disconnect */
1234 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1235 (RH_PS_PESC | RH_PS_CSC)) &&
1236 ((temp & RH_PS_CCS) == 0)) {
1237 res = i;
1238 break;
1239 }
1240 }
1241 return res;
1242}
1243
Hans de Goede2a240e72015-05-05 23:56:07 +02001244static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1245 unsigned long pipe, void *buffer, int transfer_len,
1246 struct devrequest *cmd)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001247{
Remy Bohmer172a1132008-09-16 14:55:43 +02001248 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001249 int leni = transfer_len;
1250 int len = 0;
1251 int stat = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001252 __u16 bmRType_bReq;
1253 __u16 wValue;
1254 __u16 wIndex;
1255 __u16 wLength;
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001256 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasutfc5cf882011-10-07 02:00:13 +02001257
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001258#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001259pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001260 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001261#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001262 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001263#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001264 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001265 info("Root-Hub submit IRQ: NOT implemented");
1266 return 0;
1267 }
1268
1269 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001270 wValue = le16_to_cpu(cmd->value);
1271 wIndex = le16_to_cpu(cmd->index);
1272 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001273
1274 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1275 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1276
1277 switch (bmRType_bReq) {
1278 /* Request Destination:
1279 without flags: Device,
1280 RH_INTERFACE: interface,
1281 RH_ENDPOINT: endpoint,
1282 RH_CLASS means HUB here,
1283 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1284 */
1285
1286 case RH_GET_STATUS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001287 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001288 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001289 case RH_GET_STATUS | RH_INTERFACE:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001290 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001291 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001292 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001293 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001294 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001295 case RH_GET_STATUS | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001296 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001297 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001298 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001299 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001300 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer172a1132008-09-16 14:55:43 +02001301 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001302
1303 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1304 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001305 case (RH_ENDPOINT_STALL):
1306 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001307 }
1308 break;
1309
1310 case RH_CLEAR_FEATURE | RH_CLASS:
1311 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001312 case RH_C_HUB_LOCAL_POWER:
1313 OK(0);
1314 case (RH_C_HUB_OVER_CURRENT):
1315 WR_RH_STAT(RH_HS_OCIC);
1316 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001317 }
1318 break;
1319
1320 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1321 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001322 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1323 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1324 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1325 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1326 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1327 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1328 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1329 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001330 }
1331 break;
1332
1333 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1334 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001335 case (RH_PORT_SUSPEND):
1336 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1337 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1338 if (RD_RH_PORTSTAT & RH_PS_CCS)
1339 WR_RH_PORTSTAT(RH_PS_PRS);
1340 OK(0);
1341 case (RH_PORT_POWER):
1342 WR_RH_PORTSTAT(RH_PS_PPS);
Remy Bohmer172a1132008-09-16 14:55:43 +02001343 OK(0);
1344 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1345 if (RD_RH_PORTSTAT & RH_PS_CCS)
1346 WR_RH_PORTSTAT(RH_PS_PES);
1347 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001348 }
1349 break;
1350
Remy Bohmer172a1132008-09-16 14:55:43 +02001351 case RH_SET_ADDRESS:
Hans de Goede2a240e72015-05-05 23:56:07 +02001352 ohci->rh.devnum = wValue;
Remy Bohmer172a1132008-09-16 14:55:43 +02001353 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001354
1355 case RH_GET_DESCRIPTOR:
1356 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001357 case (0x01): /* device descriptor */
1358 len = min_t(unsigned int,
1359 leni,
1360 min_t(unsigned int,
1361 sizeof(root_hub_dev_des),
1362 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001363 databuf = root_hub_dev_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001364 case (0x02): /* configuration descriptor */
1365 len = min_t(unsigned int,
1366 leni,
1367 min_t(unsigned int,
1368 sizeof(root_hub_config_des),
1369 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001370 databuf = root_hub_config_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001371 case (0x03): /* string descriptors */
1372 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001373 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001374 leni,
1375 min_t(unsigned int,
1376 sizeof(root_hub_str_index0),
1377 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001378 databuf = root_hub_str_index0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001379 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001380 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001381 if (wValue == 0x0301) {
1382 len = min_t(unsigned int,
1383 leni,
1384 min_t(unsigned int,
1385 sizeof(root_hub_str_index1),
1386 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001387 databuf = root_hub_str_index1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001388 OK(len);
1389 }
1390 default:
1391 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001392 }
1393 break;
1394
1395 case RH_GET_DESCRIPTOR | RH_CLASS:
1396 {
Hans de Goede2a240e72015-05-05 23:56:07 +02001397 __u32 temp = roothub_a(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001398
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001399 databuf[0] = 9; /* min length; */
1400 databuf[1] = 0x29;
1401 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001402#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001403 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001404#endif
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001405 databuf[3] = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001406 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001407 databuf[3] |= 0x1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001408 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001409 databuf[3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001410 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001411 databuf[3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001412
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001413 databuf[4] = 0;
1414 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1415 databuf[6] = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001416 temp = roothub_b(ohci);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001417 databuf[7] = temp & RH_B_DR;
1418 if (databuf[2] < 7) {
1419 databuf[8] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001420 } else {
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001421 databuf[0] += 2;
1422 databuf[8] = (temp & RH_B_DR) >> 8;
1423 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001424 }
1425
1426 len = min_t(unsigned int, leni,
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001427 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001428 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001429 }
1430
Marek Vasutfc5cf882011-10-07 02:00:13 +02001431 case RH_GET_CONFIGURATION:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001432 databuf[0] = 0x01;
Marek Vasutfc5cf882011-10-07 02:00:13 +02001433 OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001434
Marek Vasutfc5cf882011-10-07 02:00:13 +02001435 case RH_SET_CONFIGURATION:
1436 WR_RH_STAT(0x10000);
1437 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001438
1439 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001440 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001441 stat = USB_ST_STALLED;
1442 }
1443
1444#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001445 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001446#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001447 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001448#endif
1449
1450 len = min_t(int, len, leni);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001451 if (data != databuf)
1452 memcpy(data, databuf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001453 dev->act_len = len;
1454 dev->status = stat;
1455
1456#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001457 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer172a1132008-09-16 14:55:43 +02001458 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001459#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001460 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001461#endif
1462
1463 return stat;
1464}
1465
1466/*-------------------------------------------------------------------------*/
1467
Hans de Goeded7a39422015-05-13 14:42:15 +02001468static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1469{
1470 int i;
1471
1472 if (!intr)
1473 return &ohci->ohci_dev;
1474
1475 /* First see if we already have an ohci_dev for this dev. */
1476 for (i = 0; i < NUM_INT_DEVS; i++) {
1477 if (ohci->int_dev[i].devnum == devnum)
1478 return &ohci->int_dev[i];
1479 }
1480
1481 /* If not then find a free one. */
1482 for (i = 0; i < NUM_INT_DEVS; i++) {
1483 if (ohci->int_dev[i].devnum == -1) {
1484 ohci->int_dev[i].devnum = devnum;
1485 return &ohci->int_dev[i];
1486 }
1487 }
1488
1489 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1490 return NULL;
1491}
1492
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001493/* common code for handling submit messages - used for all but root hub */
1494/* accesses. */
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001495static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1496 void *buffer, int transfer_len, int interval)
1497{
1498 urb_priv_t *urb;
1499
1500 urb = calloc(1, sizeof(urb_priv_t));
1501 if (!urb) {
1502 printf("ohci: Error out of memory allocating urb\n");
1503 return NULL;
1504 }
1505
1506 urb->dev = dev;
1507 urb->pipe = pipe;
1508 urb->transfer_buffer = buffer;
1509 urb->transfer_buffer_length = transfer_len;
1510 urb->interval = interval;
1511
1512 return urb;
1513}
1514
Hans de Goede2a240e72015-05-05 23:56:07 +02001515static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1516 unsigned long pipe, void *buffer, int transfer_len,
1517 struct devrequest *setup, int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001518{
1519 int stat = 0;
1520 int maxsize = usb_maxpacket(dev, pipe);
1521 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001522 urb_priv_t *urb;
Hans de Goeded7a39422015-05-13 14:42:15 +02001523 ohci_dev_t *ohci_dev;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001524
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001525 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1526 if (!urb)
1527 return -ENOMEM;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001528
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001529#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001530 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001531 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001532 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001533#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001534 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001535#endif
1536 if (!maxsize) {
1537 err("submit_common_message: pipesize for pipe %lx is zero",
1538 pipe);
1539 return -1;
1540 }
1541
Hans de Goeded7a39422015-05-13 14:42:15 +02001542 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1543 if (!ohci_dev)
1544 return -ENOMEM;
1545
1546 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001547 err("sohci_submit_job failed");
1548 return -1;
1549 }
1550
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001551 mdelay(10);
Hans de Goede2a240e72015-05-05 23:56:07 +02001552 /* ohci_dump_status(ohci); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001553
Simon Glassfd7f5132011-02-07 14:42:16 -08001554 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001555
1556 /* wait for it to complete */
1557 for (;;) {
1558 /* check whether the controller is done */
Hans de Goede2a240e72015-05-05 23:56:07 +02001559 stat = hc_interrupt(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001560 if (stat < 0) {
1561 stat = USB_ST_CRC_ERR;
1562 break;
1563 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001564
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001565 /* NOTE: since we are not interrupt driven in U-Boot and always
1566 * handle only one URB at a time, we cannot assume the
1567 * transaction finished on the first successful return from
1568 * hc_interrupt().. unless the flag for current URB is set,
1569 * meaning that all TD's to/from device got actually
1570 * transferred and processed. If the current URB is not
1571 * finished we need to re-iterate this loop so as
1572 * hc_interrupt() gets called again as there needs to be some
1573 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001574 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001575 /* 0xff is returned for an SF-interrupt */
1576 break;
1577 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001578
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001579 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001580 mdelay(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001581 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001582 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001583
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001584 } else {
Hans de Goeded98dc992015-05-05 23:56:14 +02001585 if (!usb_pipeint(pipe))
1586 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001587 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001588 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001589 stat = USB_ST_CRC_ERR;
1590 break;
1591 }
1592 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001593
1594 dev->status = stat;
Mateusz Kulikowski10b479d2013-10-23 20:26:27 +02001595 dev->act_len = urb->actual_length;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001596
Hans de Goede8e41f662015-05-05 23:56:13 +02001597 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1598 invalidate_dcache_buffer(buffer, dev->act_len);
1599
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001600#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001601 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001602 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001603#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001604 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001605#endif
Hans de Goede27873372015-05-10 14:10:22 +02001606 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001607 return 0;
1608}
1609
Hans de Goede5477e6f2015-05-13 14:42:17 +02001610#define MAX_INT_QUEUESIZE 8
1611
1612struct int_queue {
1613 int queuesize;
1614 int curr_urb;
1615 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1616};
1617
1618static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1619 struct usb_device *udev, unsigned long pipe, int queuesize,
1620 int elementsize, void *buffer, int interval)
1621{
1622 struct int_queue *queue;
1623 ohci_dev_t *ohci_dev;
1624 int i;
1625
1626 if (queuesize > MAX_INT_QUEUESIZE)
1627 return NULL;
1628
1629 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1630 if (!ohci_dev)
1631 return NULL;
1632
1633 queue = malloc(sizeof(*queue));
1634 if (!queue) {
1635 printf("ohci: Error out of memory allocating int queue\n");
1636 return NULL;
1637 }
1638
1639 for (i = 0; i < queuesize; i++) {
1640 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1641 buffer + i * elementsize,
1642 elementsize, interval);
1643 if (!queue->urb[i])
1644 break;
1645
1646 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1647 printf("ohci: Error submitting int queue job\n");
1648 urb_free_priv(queue->urb[i]);
1649 break;
1650 }
1651 }
1652 if (i == 0) {
1653 /* We did not succeed in submitting even 1 urb */
1654 free(queue);
1655 return NULL;
1656 }
1657
1658 queue->queuesize = i;
1659 queue->curr_urb = 0;
1660
1661 return queue;
1662}
1663
1664static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1665 struct int_queue *queue)
1666{
1667 if (queue->curr_urb == queue->queuesize)
1668 return NULL; /* Queue depleted */
1669
1670 if (hc_interrupt(ohci) < 0)
1671 return NULL;
1672
1673 if (queue->urb[queue->curr_urb]->finished) {
1674 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1675 queue->curr_urb++;
1676 return ret;
1677 }
1678
1679 return NULL;
1680}
1681
1682static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1683 struct int_queue *queue)
1684{
1685 int i;
1686
1687 for (i = 0; i < queue->queuesize; i++)
1688 urb_free_priv(queue->urb[i]);
1689
1690 free(queue);
1691
1692 return 0;
1693}
1694
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001695#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001696/* submit routines called from usb.c */
1697int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1698 int transfer_len)
1699{
1700 info("submit_bulk_msg");
Hans de Goede2a240e72015-05-05 23:56:07 +02001701 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1702 NULL, 0);
1703}
1704
1705int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1706 int transfer_len, int interval)
1707{
1708 info("submit_int_msg");
1709 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1710 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001711}
Hans de Goede5477e6f2015-05-13 14:42:17 +02001712
1713struct int_queue *create_int_queue(struct usb_device *dev,
1714 unsigned long pipe, int queuesize, int elementsize,
1715 void *buffer, int interval)
1716{
1717 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1718 elementsize, buffer, interval);
1719}
1720
1721void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1722{
1723 return _ohci_poll_int_queue(&gohci, dev, queue);
1724}
1725
1726int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1727{
1728 return _ohci_destroy_int_queue(&gohci, dev, queue);
1729}
Hans de Goede8a62c502015-05-10 14:10:25 +02001730#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001731
Hans de Goede2a240e72015-05-05 23:56:07 +02001732static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1733 unsigned long pipe, void *buffer, int transfer_len,
1734 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001735{
1736 int maxsize = usb_maxpacket(dev, pipe);
1737
1738 info("submit_control_msg");
1739#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001740 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001741 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001742#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001743 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001744#endif
1745 if (!maxsize) {
1746 err("submit_control_message: pipesize for pipe %lx is zero",
1747 pipe);
1748 return -1;
1749 }
Hans de Goede2a240e72015-05-05 23:56:07 +02001750 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1751 ohci->rh.dev = dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001752 /* root hub - redirect */
Hans de Goede2a240e72015-05-05 23:56:07 +02001753 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1754 transfer_len, setup);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001755 }
1756
Hans de Goede2a240e72015-05-05 23:56:07 +02001757 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1758 setup, 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001759}
1760
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001761/*-------------------------------------------------------------------------*
1762 * HC functions
1763 *-------------------------------------------------------------------------*/
1764
1765/* reset the HC and BUS */
1766
Remy Bohmer172a1132008-09-16 14:55:43 +02001767static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001768{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001769#ifdef CONFIG_PCI_EHCI_DEVNO
1770 pci_dev_t pdev;
1771#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001772 int timeout = 30;
1773 int smm_timeout = 50; /* 0,5 sec */
1774
1775 dbg("%s\n", __FUNCTION__);
1776
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001777#ifdef CONFIG_PCI_EHCI_DEVNO
1778 /*
1779 * Some multi-function controllers (e.g. ISP1562) allow root hub
1780 * resetting via EHCI registers only.
1781 */
1782 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1783 if (pdev != -1) {
1784 u32 base;
1785 int timeout = 1000;
1786
1787 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001788 base += EHCI_USBCMD_OFF;
1789 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001790
Becky Bruce4abaacb2010-06-30 13:05:44 -05001791 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001792 if (timeout-- <= 0) {
1793 printf("USB RootHub reset timed out!");
1794 break;
1795 }
1796 udelay(1);
1797 }
1798 } else
1799 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1800#endif
Becky Bruce4abaacb2010-06-30 13:05:44 -05001801 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1802 /* SMM owns the HC, request ownership */
1803 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001804 info("USB HC TakeOver from SMM");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001805 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001806 mdelay(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001807 if (--smm_timeout == 0) {
1808 err("USB HC TakeOver failed!");
1809 return -1;
1810 }
1811 }
1812 }
1813
1814 /* Disable HC interrupts */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001815 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001816
1817 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1818 ohci->slot_name,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001819 ohci_readl(&ohci->regs->control));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001820
1821 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001822 ohci->hc_control = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001823 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001824
1825 /* HC Reset requires max 10 us delay */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001826 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1827 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001828 if (--timeout == 0) {
1829 err("USB HC reset timed out!");
1830 return -1;
1831 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001832 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001833 }
1834 return 0;
1835}
1836
1837/*-------------------------------------------------------------------------*/
1838
1839/* Start an OHCI controller, set the BUS operational
1840 * enable interrupts
1841 * connect the virtual root hub */
1842
Remy Bohmer172a1132008-09-16 14:55:43 +02001843static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001844{
1845 __u32 mask;
1846 unsigned int fminterval;
Hans de Goeded7a39422015-05-13 14:42:15 +02001847 int i;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001848
1849 ohci->disabled = 1;
Hans de Goeded7a39422015-05-13 14:42:15 +02001850 for (i = 0; i < NUM_INT_DEVS; i++)
1851 ohci->int_dev[i].devnum = -1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001852
1853 /* Tell the controller where the control and bulk lists are
1854 * The lists are empty now. */
1855
Becky Bruce4abaacb2010-06-30 13:05:44 -05001856 ohci_writel(0, &ohci->regs->ed_controlhead);
1857 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001858
Andre Przywarac114a982016-10-21 02:24:29 +01001859 ohci_writel((uintptr_t)ohci->hcca,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001860 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001861
1862 fminterval = 0x2edf;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001863 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001864 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001865 ohci_writel(fminterval, &ohci->regs->fminterval);
1866 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001867
1868 /* start controller operations */
1869 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1870 ohci->disabled = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001871 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001872
1873 /* disable all interrupts */
1874 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1875 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1876 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001877 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001878 /* clear all interrupts */
1879 mask &= ~OHCI_INTR_MIE;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001880 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001881 /* Choose the interrupts we care about now - but w/o MIE */
1882 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001883 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001884
1885#ifdef OHCI_USE_NPS
1886 /* required for AMD-756 and some Mac platforms */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001887 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001888 &ohci->regs->roothub.a);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001889 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001890#endif /* OHCI_USE_NPS */
1891
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001892 /* connect the virtual root hub */
1893 ohci->rh.devnum = 0;
1894
1895 return 0;
1896}
1897
1898/*-------------------------------------------------------------------------*/
1899
1900/* an interrupt happens */
1901
Hans de Goede2a240e72015-05-05 23:56:07 +02001902static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001903{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001904 struct ohci_regs *regs = ohci->regs;
1905 int ints;
1906 int stat = -1;
1907
Hans de Goede8e41f662015-05-05 23:56:13 +02001908 invalidate_dcache_hcca(ohci->hcca);
1909
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001910 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001911 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001912 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001913 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001914 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001915 if (ints == ~(u32)0) {
1916 ohci->disabled++;
1917 err("%s device removed!", ohci->slot_name);
1918 return -1;
1919 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001920 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer172a1132008-09-16 14:55:43 +02001921 if (ints == 0) {
1922 dbg("hc_interrupt: returning..\n");
1923 return 0xff;
1924 }
1925 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001926 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001927
Remy Bohmer172a1132008-09-16 14:55:43 +02001928 /* dbg("Interrupt: %x frame: %x", ints,
1929 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001930
Remy Bohmer172a1132008-09-16 14:55:43 +02001931 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001932 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001933
1934 if (ints & OHCI_INTR_UE) {
1935 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001936 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001937 ohci->slot_name);
1938 /* e.g. due to PCI Master/Target Abort */
1939
1940#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001941 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001942#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001943 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001944#endif
1945 /* FIXME: be optimistic, hope that bug won't repeat often. */
1946 /* Make some non-interrupt context restart the controller. */
1947 /* Count and limit the retries though; either hardware or */
1948 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001949 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001950 return -1;
1951 }
1952
1953 if (ints & OHCI_INTR_WDH) {
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001954 ohci_mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001955 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1956 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goede2a240e72015-05-05 23:56:07 +02001957 stat = dl_done_list(ohci);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001958 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1959 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001960 }
1961
1962 if (ints & OHCI_INTR_SO) {
1963 dbg("USB Schedule overrun\n");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001964 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001965 stat = -1;
1966 }
1967
1968 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1969 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001970 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001971 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001972 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001973 if (ohci->ed_rm_list[frame] != NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001974 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001975 stat = 0xff;
1976 }
1977
Becky Bruce4abaacb2010-06-30 13:05:44 -05001978 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001979 return stat;
1980}
1981
1982/*-------------------------------------------------------------------------*/
1983
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001984#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02001985
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001986/*-------------------------------------------------------------------------*/
1987
1988/* De-allocate all resources.. */
1989
Remy Bohmer172a1132008-09-16 14:55:43 +02001990static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001991{
Remy Bohmer172a1132008-09-16 14:55:43 +02001992 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001993
1994 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001995 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001996}
1997
1998/*-------------------------------------------------------------------------*/
1999
2000/*
2001 * low level initalisation routine, called from usb.c
2002 */
2003static char ohci_inited = 0;
2004
Troy Kisky8f9c49d2013-10-10 15:27:56 -07002005int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002006{
Zhang Wei8d15efa2007-06-06 10:08:14 +02002007#ifdef CONFIG_PCI_OHCI
2008 pci_dev_t pdev;
2009#endif
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002010
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002011#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002012 /* cpu dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02002013 if (usb_cpu_init())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002014 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002015#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002016
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002017#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002018 /* board dependant init */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002019 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002020 return -1;
2021#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02002022 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002023
2024 /* align the storage */
2025 if ((__u32)&ghcca[0] & 0xff) {
2026 err("HCCA not aligned!!");
2027 return -1;
2028 }
Hans de Goedeeb509d32015-05-05 23:56:10 +02002029 gohci.hcca = &ghcca[0];
2030 info("aligned ghcca %p", gohci.hcca);
2031 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002032
2033 gohci.disabled = 1;
2034 gohci.sleeping = 0;
2035 gohci.irq = -1;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002036#ifdef CONFIG_PCI_OHCI
Sergei Poselenov0b4570f2008-05-22 01:15:53 +02002037 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02002038
2039 if (pdev != -1) {
2040 u16 vid, did;
2041 u32 base;
2042 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2043 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2044 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2045 vid, did, (pdev >> 16) & 0xff,
2046 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2047 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2048 printf("OHCI regs address 0x%08x\n", base);
2049 gohci.regs = (struct ohci_regs *)base;
Heiko Schocher882830a2019-07-16 10:49:06 +02002050 } else {
2051 printf("%s: OHCI devnr: %d not found\n", __func__,
2052 CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02002053 return -1;
Heiko Schocher882830a2019-07-16 10:49:06 +02002054 }
Zhang Wei8d15efa2007-06-06 10:08:14 +02002055#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002056 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002057#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002058
2059 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002060 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002061
2062 if (hc_reset (&gohci) < 0) {
2063 hc_release_ohci (&gohci);
2064 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002065#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002066 /* board dependant cleanup */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002067 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002068#endif
2069
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002070#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002071 /* cpu dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02002072 usb_cpu_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002073#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002074 return -1;
2075 }
2076
Remy Bohmer172a1132008-09-16 14:55:43 +02002077 if (hc_start(&gohci) < 0) {
2078 err("can't start usb-%s", gohci.slot_name);
2079 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002080 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002081#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002082 /* board dependant cleanup */
2083 usb_board_stop();
2084#endif
2085
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002086#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002087 /* cpu dependant cleanup */
2088 usb_cpu_stop();
2089#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002090 return -1;
2091 }
2092
2093#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02002094 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002095#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02002096 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002097#endif
2098 ohci_inited = 1;
2099 return 0;
2100}
2101
Lucas Stacha3231282012-09-26 00:14:34 +02002102int usb_lowlevel_stop(int index)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002103{
2104 /* this gets called really early - before the controller has */
2105 /* even been initialized! */
2106 if (!ohci_inited)
2107 return 0;
2108 /* TODO release any interrupts, etc. */
2109 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02002110 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002111
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002112#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002113 /* board dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002114 if (usb_board_stop())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002115 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002116#endif
2117
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002118#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002119 /* cpu dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002120 if (usb_cpu_stop())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002121 return -1;
2122#endif
Remy Bohmer743dd492008-08-20 11:22:02 +02002123 /* This driver is no longer initialised. It needs a new low-level
2124 * init (board/cpu) before it can be used again. */
2125 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002126 return 0;
2127}
Hans de Goede2a240e72015-05-05 23:56:07 +02002128
2129int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2130 void *buffer, int transfer_len, struct devrequest *setup)
2131{
2132 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2133 transfer_len, setup);
2134}
Hans de Goede8a62c502015-05-10 14:10:25 +02002135#endif
2136
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01002137#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02002138static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2139 unsigned long pipe, void *buffer, int length,
2140 struct devrequest *setup)
2141{
2142 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2143
2144 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2145 length, setup);
2146}
2147
2148static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2149 unsigned long pipe, void *buffer, int length)
2150{
2151 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2152
2153 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2154}
2155
2156static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2157 unsigned long pipe, void *buffer, int length,
2158 int interval)
2159{
2160 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2161
2162 return submit_common_msg(ohci, udev, pipe, buffer, length,
2163 NULL, interval);
2164}
2165
Hans de Goede5477e6f2015-05-13 14:42:17 +02002166static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2167 struct usb_device *udev, unsigned long pipe, int queuesize,
2168 int elementsize, void *buffer, int interval)
2169{
2170 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2171
2172 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2173 buffer, interval);
2174}
2175
2176static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2177 struct int_queue *queue)
2178{
2179 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2180
2181 return _ohci_poll_int_queue(ohci, udev, queue);
2182}
2183
2184static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2185 struct int_queue *queue)
2186{
2187 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2188
2189 return _ohci_destroy_int_queue(ohci, udev, queue);
2190}
2191
Hans de Goede8a62c502015-05-10 14:10:25 +02002192int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2193{
2194 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2195 ohci_t *ohci = dev_get_priv(dev);
2196 u32 reg;
2197
2198 priv->desc_before_addr = true;
2199
2200 ohci->regs = regs;
2201 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2202 if (!ohci->hcca)
2203 return -ENOMEM;
2204 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
Hans de Goede66cb3e42015-10-20 18:39:29 +02002205 flush_dcache_hcca(ohci->hcca);
Hans de Goede8a62c502015-05-10 14:10:25 +02002206
2207 if (hc_reset(ohci) < 0)
2208 return -EIO;
2209
2210 if (hc_start(ohci) < 0)
2211 return -EIO;
2212
2213 reg = ohci_readl(&regs->revision);
2214 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2215
2216 return 0;
2217}
2218
2219int ohci_deregister(struct udevice *dev)
2220{
2221 ohci_t *ohci = dev_get_priv(dev);
2222
2223 if (hc_reset(ohci) < 0)
2224 return -EIO;
2225
2226 free(ohci->hcca);
2227
2228 return 0;
2229}
2230
2231struct dm_usb_ops ohci_usb_ops = {
2232 .control = ohci_submit_control_msg,
2233 .bulk = ohci_submit_bulk_msg,
2234 .interrupt = ohci_submit_int_msg,
Hans de Goede5477e6f2015-05-13 14:42:17 +02002235 .create_int_queue = ohci_create_int_queue,
2236 .poll_int_queue = ohci_poll_int_queue,
2237 .destroy_int_queue = ohci_destroy_int_queue,
Hans de Goede8a62c502015-05-10 14:10:25 +02002238};
2239
2240#endif