blob: ac1ad29b02b8956afcf84b4db0a1577ea91772fe [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>
Simon Glass63334482019-11-14 12:57:39 -070031#include <cpu_func.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020032#include <asm/byteorder.h>
Hans de Goede8a62c502015-05-10 14:10:25 +020033#include <dm.h>
34#include <errno.h>
Simon Glass274e0b02020-05-10 11:39:56 -060035#include <asm/cache.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020036
37#if defined(CONFIG_PCI_OHCI)
Zhang Wei8d15efa2007-06-06 10:08:14 +020038# include <pci.h>
Sergei Poselenov0b4570f2008-05-22 01:15:53 +020039#if !defined(CONFIG_PCI_OHCI_DEVNO)
40#define CONFIG_PCI_OHCI_DEVNO 0
41#endif
Markus Klotzbuecheraa219212006-05-30 16:56:14 +020042#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020043
44#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060045#include <memalign.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020046#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020047
48#include "ohci.h"
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020049
Wolfgang Denkdcac38b2007-11-19 12:59:14 +010050#ifdef CONFIG_AT91RM9200
51#include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
52#endif
53
Masahiro Yamada7a5b2922014-11-06 14:59:35 +090054#if defined(CONFIG_CPU_ARM920T) || \
Heiko Schocher2e1134f2019-07-16 10:49:05 +020055 defined(CONFIG_PCI_OHCI) || \
56 defined(CONFIG_DM_PCI) || \
57 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020058# define OHCI_USE_NPS /* force NoPowerSwitching mode */
59#endif
60
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020061#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010062#undef DEBUG
63#undef SHOW_INFO
64#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020065
66/* For initializing controller (mask in an HCFS mode too) */
67#define OHCI_CONTROL_INIT \
68 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
69
Heiko Schocherfe410a42019-07-16 10:49:03 +020070#if !CONFIG_IS_ENABLED(DM_USB)
Zhang Wei8d15efa2007-06-06 10:08:14 +020071#ifdef CONFIG_PCI_OHCI
72static struct pci_device_id ohci_pci_ids[] = {
73 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
David Saadae3b28422007-09-17 17:04:47 +020074 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
TsiChung Liew923f43a2008-01-11 20:42:58 -060075 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
Zhang Wei8d15efa2007-06-06 10:08:14 +020076 /* Please add supported PCI OHCI controller ids here */
77 {0, 0}
78};
79#endif
Heiko Schocherfe410a42019-07-16 10:49:03 +020080#endif
Zhang Wei8d15efa2007-06-06 10:08:14 +020081
Yuri Tikhonov11af42c2008-09-04 11:19:05 +020082#ifdef CONFIG_PCI_EHCI_DEVNO
83static struct pci_device_id ehci_pci_ids[] = {
84 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
85 /* Please add supported PCI EHCI controller ids here */
86 {0, 0}
87};
88#endif
89
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020090#ifdef DEBUG
91#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
92#else
Remy Bohmer172a1132008-09-16 14:55:43 +020093#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020094#endif /* DEBUG */
95#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020096#ifdef SHOW_INFO
97#define info(format, arg...) printf("INFO: " format "\n", ## arg)
98#else
Remy Bohmer172a1132008-09-16 14:55:43 +020099#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200100#endif
101
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200102#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200103# define m16_swap(x) cpu_to_be16(x)
104# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +0100105#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +0200106# define m16_swap(x) cpu_to_le16(x)
107# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200109
Wu, Joshc3857142015-07-27 11:40:18 +0800110/* We really should do proper cache flushing everywhere */
Hans de Goede8e41f662015-05-05 23:56:13 +0200111#define flush_dcache_buffer(addr, size) \
112 flush_dcache_range((unsigned long)(addr), \
113 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
114#define invalidate_dcache_buffer(addr, size) \
115 invalidate_dcache_range((unsigned long)(addr), \
116 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
Hans de Goede8e41f662015-05-05 23:56:13 +0200117
118/* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
119#define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
120#define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
121#define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
122#define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
123#define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
124#define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
125#define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
126#define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
127
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100128#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goeded6cc1d12015-05-10 14:10:24 +0200129/*
130 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
131 * them around when building for older boards not yet converted to the dm
132 * just in case (to avoid regressions), for dm this turns them into nops.
133 */
134#define ohci_mdelay(x)
135#else
136#define ohci_mdelay(x) mdelay(x)
137#endif
138
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100139#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200140/* global ohci_t */
141static ohci_t gohci;
142/* this must be aligned to a 256 byte boundary */
143struct ohci_hcca ghcca[1];
Hans de Goede8a62c502015-05-10 14:10:25 +0200144#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200145
Hans de Goede516c9872015-05-05 23:56:11 +0200146/* mapping of the OHCI CC status to error codes */
147static int cc_to_error[16] = {
148 /* No Error */ 0,
149 /* CRC Error */ USB_ST_CRC_ERR,
150 /* Bit Stuff */ USB_ST_BIT_ERR,
151 /* Data Togg */ USB_ST_CRC_ERR,
152 /* Stall */ USB_ST_STALLED,
153 /* DevNotResp */ -1,
154 /* PIDCheck */ USB_ST_BIT_ERR,
155 /* UnExpPID */ USB_ST_BIT_ERR,
156 /* DataOver */ USB_ST_BUF_ERR,
157 /* DataUnder */ USB_ST_BUF_ERR,
158 /* reservd */ -1,
159 /* reservd */ -1,
160 /* BufferOver */ USB_ST_BUF_ERR,
161 /* BuffUnder */ USB_ST_BUF_ERR,
162 /* Not Access */ -1,
163 /* Not Access */ -1
164};
165
166static const char *cc_to_string[16] = {
167 "No Error",
168 "CRC: Last data packet from endpoint contained a CRC error.",
169 "BITSTUFFING: Last data packet from endpoint contained a bit " \
170 "stuffing violation",
171 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
172 "that did not match the expected value.",
173 "STALL: TD was moved to the Done Queue because the endpoint returned" \
174 " a STALL PID",
175 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
176 "not provide a handshake (OUT)",
177 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
178 "(IN) or handshake (OUT)",
179 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
180 "value is not defined.",
181 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
182 "either the size of the maximum data packet allowed\n" \
183 "from the endpoint (found in MaximumPacketSize field\n" \
184 "of ED) or the remaining buffer size.",
185 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
186 "and that amount was not sufficient to fill the\n" \
187 "specified buffer",
188 "reserved1",
189 "reserved2",
190 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
191 "than it could be written to system memory",
192 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
193 "system memory fast enough to keep up with data USB " \
194 "data rate.",
195 "NOT ACCESSED: This code is set by software before the TD is placed" \
196 "on a list to be processed by the HC.(1)",
197 "NOT ACCESSED: This code is set by software before the TD is placed" \
198 "on a list to be processed by the HC.(2)",
199};
200
Remy Bohmer172a1132008-09-16 14:55:43 +0200201static inline u32 roothub_a(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500202 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200203static inline u32 roothub_b(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500204 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200205static inline u32 roothub_status(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500206 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200207static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500208 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200209
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200210/* forward declaration */
Hans de Goede2a240e72015-05-05 23:56:07 +0200211static int hc_interrupt(ohci_t *ohci);
212static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
213 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200214 struct devrequest *setup, urb_priv_t *urb,
215 int interval);
Hans de Goede516c9872015-05-05 23:56:11 +0200216static int ep_link(ohci_t * ohci, ed_t * ed);
217static int ep_unlink(ohci_t * ohci, ed_t * ed);
218static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
219 unsigned long pipe, int interval, int load);
220
221/*-------------------------------------------------------------------------*/
222
223/* TDs ... */
224static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
225{
226 int i;
227 struct td *td;
228
229 td = NULL;
230 for (i = 0; i < NUM_TD; i++)
231 {
232 if (ohci_dev->tds[i].usb_dev == NULL)
233 {
234 td = &ohci_dev->tds[i];
235 td->usb_dev = usb_dev;
236 break;
237 }
238 }
239
240 return td;
241}
242
243static inline void ed_free(struct ed *ed)
244{
245 ed->usb_dev = NULL;
246}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200247
248/*-------------------------------------------------------------------------*
249 * URB support functions
250 *-------------------------------------------------------------------------*/
251
252/* free HCD-private data associated with this URB */
253
Remy Bohmer172a1132008-09-16 14:55:43 +0200254static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200255{
256 int i;
257 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200258 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200259
260 last = urb->length - 1;
261 if (last >= 0) {
262 for (i = 0; i <= last; i++) {
263 td = urb->td[i];
264 if (td) {
265 td->usb_dev = NULL;
266 urb->td[i] = NULL;
267 }
268 }
269 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200270 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200271}
272
273/*-------------------------------------------------------------------------*/
274
275#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +0200276static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200277
278/* debug| print the main components of an URB
279 * small: 0) header + data packets 1) just header */
280
Hans de Goede2a240e72015-05-05 23:56:07 +0200281static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer172a1132008-09-16 14:55:43 +0200282 unsigned long pipe, void *buffer, int transfer_len,
283 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200284{
Remy Bohmer172a1132008-09-16 14:55:43 +0200285 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200286 str,
Hans de Goede2a240e72015-05-05 23:56:07 +0200287 sohci_get_current_frame_number(ohci),
Remy Bohmer172a1132008-09-16 14:55:43 +0200288 usb_pipedevice(pipe),
289 usb_pipeendpoint(pipe),
290 usb_pipeout(pipe)? 'O': 'I',
291 usb_pipetype(pipe) < 2 ? \
292 (usb_pipeint(pipe)? "INTR": "ISOC"): \
293 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200294 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200295 transfer_len, dev->status);
296#ifdef OHCI_VERBOSE_DEBUG
297 if (!small) {
298 int i, len;
299
Remy Bohmer172a1132008-09-16 14:55:43 +0200300 if (usb_pipecontrol(pipe)) {
301 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200302 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200303 printf(" %02x", ((__u8 *) setup) [i]);
304 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200305 }
306 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200307 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200308 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200309 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200310 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200311 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200312 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200313 printf(" %02x", ((__u8 *) buffer) [i]);
314 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200315 }
316 }
317#endif
318}
319
Remy Bohmer172a1132008-09-16 14:55:43 +0200320/* just for debugging; prints non-empty branches of the int ed tree
321 * inclusive iso eds */
322void ep_print_int_eds(ohci_t *ohci, char *str)
323{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200324 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200325 __u32 *ed_p;
326 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200327 j = 5;
328 ed_p = &(ohci->hcca->int_table [i]);
329 if (*ed_p == 0)
330 continue;
Hans de Goede8e41f662015-05-05 23:56:13 +0200331 invalidate_dcache_ed(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200332 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200333 while (*ed_p != 0 && j--) {
334 ed_t *ed = (ed_t *)m32_swap(ed_p);
Hans de Goede8e41f662015-05-05 23:56:13 +0200335 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200336 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200337 ed_p = &ed->hwNextED;
338 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200339 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200340 }
341}
342
Remy Bohmer172a1132008-09-16 14:55:43 +0200343static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200344{
Remy Bohmer172a1132008-09-16 14:55:43 +0200345 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200346 label,
347 mask,
348 (mask & OHCI_INTR_MIE) ? " MIE" : "",
349 (mask & OHCI_INTR_OC) ? " OC" : "",
350 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
351 (mask & OHCI_INTR_FNO) ? " FNO" : "",
352 (mask & OHCI_INTR_UE) ? " UE" : "",
353 (mask & OHCI_INTR_RD) ? " RD" : "",
354 (mask & OHCI_INTR_SF) ? " SF" : "",
355 (mask & OHCI_INTR_WDH) ? " WDH" : "",
356 (mask & OHCI_INTR_SO) ? " SO" : ""
357 );
358}
359
Remy Bohmer172a1132008-09-16 14:55:43 +0200360static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200361{
362 ed_t *edp = (ed_t *)value;
363
364 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200365 dbg("%s %08x", label, value);
Hans de Goede8e41f662015-05-05 23:56:13 +0200366 invalidate_dcache_ed(edp);
Remy Bohmer172a1132008-09-16 14:55:43 +0200367 dbg("%08x", edp->hwINFO);
368 dbg("%08x", edp->hwTailP);
369 dbg("%08x", edp->hwHeadP);
370 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200371 }
372}
373
Remy Bohmer172a1132008-09-16 14:55:43 +0200374static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200375{
376 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200377 case OHCI_USB_RESET: return "reset";
378 case OHCI_USB_RESUME: return "resume";
379 case OHCI_USB_OPER: return "operational";
380 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200381 }
382 return "?";
383}
384
385/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200386static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200387{
388 struct ohci_regs *regs = controller->regs;
389 __u32 temp;
390
Becky Bruce4abaacb2010-06-30 13:05:44 -0500391 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200392 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200393 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200394
Becky Bruce4abaacb2010-06-30 13:05:44 -0500395 temp = ohci_readl(&regs->control);
Remy Bohmer172a1132008-09-16 14:55:43 +0200396 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200397 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
398 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
399 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200400 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200401 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
402 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
403 (temp & OHCI_CTRL_IE) ? " IE" : "",
404 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
405 temp & OHCI_CTRL_CBSR
406 );
407
Becky Bruce4abaacb2010-06-30 13:05:44 -0500408 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200409 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200410 (temp & OHCI_SOC) >> 16,
411 (temp & OHCI_OCR) ? " OCR" : "",
412 (temp & OHCI_BLF) ? " BLF" : "",
413 (temp & OHCI_CLF) ? " CLF" : "",
414 (temp & OHCI_HCR) ? " HCR" : ""
415 );
416
Becky Bruce4abaacb2010-06-30 13:05:44 -0500417 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
418 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200419
Becky Bruce4abaacb2010-06-30 13:05:44 -0500420 maybe_print_eds("ed_periodcurrent",
421 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200422
Becky Bruce4abaacb2010-06-30 13:05:44 -0500423 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
424 maybe_print_eds("ed_controlcurrent",
425 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200426
Becky Bruce4abaacb2010-06-30 13:05:44 -0500427 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
428 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200429
Becky Bruce4abaacb2010-06-30 13:05:44 -0500430 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200431}
432
Remy Bohmer172a1132008-09-16 14:55:43 +0200433static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200434{
435 __u32 temp, ndp, i;
436
Remy Bohmer172a1132008-09-16 14:55:43 +0200437 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200438 ndp = (temp & RH_A_NDP);
439#ifdef CONFIG_AT91C_PQFP_UHPBUG
440 ndp = (ndp == 2) ? 1:0;
441#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200442 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200443 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200444 ((temp & RH_A_POTPGT) >> 24) & 0xff,
445 (temp & RH_A_NOCP) ? " NOCP" : "",
446 (temp & RH_A_OCPM) ? " OCPM" : "",
447 (temp & RH_A_DT) ? " DT" : "",
448 (temp & RH_A_NPS) ? " NPS" : "",
449 (temp & RH_A_PSM) ? " PSM" : "",
450 ndp
451 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200452 temp = roothub_b(controller);
453 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200454 temp,
455 (temp & RH_B_PPCM) >> 16,
456 (temp & RH_B_DR)
457 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200458 temp = roothub_status(controller);
459 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200460 temp,
461 (temp & RH_HS_CRWE) ? " CRWE" : "",
462 (temp & RH_HS_OCIC) ? " OCIC" : "",
463 (temp & RH_HS_LPSC) ? " LPSC" : "",
464 (temp & RH_HS_DRWE) ? " DRWE" : "",
465 (temp & RH_HS_OCI) ? " OCI" : "",
466 (temp & RH_HS_LPS) ? " LPS" : ""
467 );
468 }
469
470 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200471 temp = roothub_portstatus(controller, i);
472 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 +0200473 i,
474 temp,
475 (temp & RH_PS_PRSC) ? " PRSC" : "",
476 (temp & RH_PS_OCIC) ? " OCIC" : "",
477 (temp & RH_PS_PSSC) ? " PSSC" : "",
478 (temp & RH_PS_PESC) ? " PESC" : "",
479 (temp & RH_PS_CSC) ? " CSC" : "",
480
481 (temp & RH_PS_LSDA) ? " LSDA" : "",
482 (temp & RH_PS_PPS) ? " PPS" : "",
483 (temp & RH_PS_PRS) ? " PRS" : "",
484 (temp & RH_PS_POCI) ? " POCI" : "",
485 (temp & RH_PS_PSS) ? " PSS" : "",
486
487 (temp & RH_PS_PES) ? " PES" : "",
488 (temp & RH_PS_CCS) ? " CCS" : ""
489 );
490 }
491}
492
Remy Bohmer172a1132008-09-16 14:55:43 +0200493static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200494{
Remy Bohmer172a1132008-09-16 14:55:43 +0200495 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200496
497 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200498 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200499 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200500 ep_print_int_eds(controller, "hcca");
Hans de Goede8e41f662015-05-05 23:56:13 +0200501 invalidate_dcache_hcca(controller->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200502 dbg("hcca frame #%04x", controller->hcca->frame_no);
503 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100504}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200505#endif /* DEBUG */
506
507/*-------------------------------------------------------------------------*
508 * Interface functions (URB)
509 *-------------------------------------------------------------------------*/
510
511/* get a transfer request */
512
Hans de Goede0c0e9602015-05-05 23:56:08 +0200513int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
514 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200515{
Remy Bohmer172a1132008-09-16 14:55:43 +0200516 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200517 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200518 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200519 struct usb_device *dev = urb->dev;
520 unsigned long pipe = urb->pipe;
521 void *buffer = urb->transfer_buffer;
522 int transfer_len = urb->transfer_buffer_length;
523 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200524
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200525 /* when controller's hung, permit only roothub cleanup attempts
526 * such as powering down ports */
527 if (ohci->disabled) {
528 err("sohci_submit_job: EPIPE");
529 return -1;
530 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200531
Remy Bohmer172a1132008-09-16 14:55:43 +0200532 /* we're about to begin a new transaction here so mark the
533 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200534 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200535
536 /* every endpoint has a ed, locate and fill it */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200537 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer172a1132008-09-16 14:55:43 +0200538 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200539 err("sohci_submit_job: ENOMEM");
540 return -1;
541 }
542
543 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200544 switch (usb_pipetype(pipe)) {
545 case PIPE_BULK: /* one TD for every 4096 Byte */
546 size = (transfer_len - 1) / 4096 + 1;
547 break;
548 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
549 size = (transfer_len == 0)? 2:
550 (transfer_len - 1) / 4096 + 3;
551 break;
552 case PIPE_INTERRUPT: /* 1 TD */
553 size = 1;
554 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200555 }
556
Zhang Wei8d15efa2007-06-06 10:08:14 +0200557 ed->purb = urb;
558
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200559 if (size >= (N_URB_TD - 1)) {
560 err("need %d TDs, only have %d", size, N_URB_TD);
561 return -1;
562 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200563 purb_priv->pipe = pipe;
564
565 /* fill the private part of the URB */
566 purb_priv->length = size;
567 purb_priv->ed = ed;
568 purb_priv->actual_length = 0;
569
570 /* allocate the TDs */
571 /* note that td[0] was allocated in ep_add_ed */
572 for (i = 0; i < size; i++) {
Hans de Goedee5ef4212015-05-05 23:56:09 +0200573 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200574 if (!purb_priv->td[i]) {
575 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200576 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200577 err("sohci_submit_job: ENOMEM");
578 return -1;
579 }
580 }
581
582 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200583 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200584 err("sohci_submit_job: EINVAL");
585 return -1;
586 }
587
588 /* link the ed into a chain if is not already */
589 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200590 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200591
592 /* fill the TDs and link it to the ed */
Hans de Goede2a240e72015-05-05 23:56:07 +0200593 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200594 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200595
596 return 0;
597}
598
599/*-------------------------------------------------------------------------*/
600
601#ifdef DEBUG
602/* tell us the current USB frame number */
Hans de Goede2a240e72015-05-05 23:56:07 +0200603static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200604{
Hans de Goede8e41f662015-05-05 23:56:13 +0200605 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200606 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200607}
608#endif
609
610/*-------------------------------------------------------------------------*
611 * ED handling functions
612 *-------------------------------------------------------------------------*/
613
Zhang Wei8d15efa2007-06-06 10:08:14 +0200614/* search for the right branch to insert an interrupt ed into the int tree
615 * do some load ballancing;
616 * returns the branch and
617 * sets the interval to interval = 2^integer (ld (interval)) */
618
Remy Bohmer172a1132008-09-16 14:55:43 +0200619static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200620{
621 int i, branch = 0;
622
623 /* search for the least loaded interrupt endpoint
624 * branch of all 32 branches
625 */
626 for (i = 0; i < 32; i++)
627 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
628 branch = i;
629
630 branch = branch % interval;
631 for (i = branch; i < 32; i += interval)
632 ohci->ohci_int_load [i] += load;
633
634 return branch;
635}
636
637/*-------------------------------------------------------------------------*/
638
639/* 2^int( ld (inter)) */
640
Remy Bohmer172a1132008-09-16 14:55:43 +0200641static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200642{
643 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200644 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200645 return 1 << i;
646}
647
648/*-------------------------------------------------------------------------*/
649
650/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200651 * in order to process it sequentially the indexes of the branches have to
652 * be mapped the mapping reverses the bits of a word of num_bits length */
653static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200654{
655 int i, wout = 0;
656
657 for (i = 0; i < num_bits; i++)
658 wout |= (((word >> i) & 1) << (num_bits - i - 1));
659 return wout;
660}
661
662/*-------------------------------------------------------------------------*
663 * ED handling functions
664 *-------------------------------------------------------------------------*/
665
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200666/* link an ed into one of the HC chains */
667
Remy Bohmer172a1132008-09-16 14:55:43 +0200668static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200669{
670 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200671 int int_branch;
672 int i;
673 int inter;
674 int interval;
675 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200676 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200677
678 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200679 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200680
681 switch (ed->type) {
682 case PIPE_CONTROL:
683 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200684 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200685 if (ohci->ed_controltail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100686 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200687 else
688 ohci->ed_controltail->hwNextED =
689 m32_swap((unsigned long)ed);
690
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200691 ed->ed_prev = ohci->ed_controltail;
692 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
693 !ohci->ed_rm_list[1] && !ohci->sleeping) {
694 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500695 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200696 }
697 ohci->ed_controltail = edi;
698 break;
699
700 case PIPE_BULK:
701 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200702 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200703 if (ohci->ed_bulktail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100704 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200705 else
706 ohci->ed_bulktail->hwNextED =
707 m32_swap((unsigned long)ed);
708
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200709 ed->ed_prev = ohci->ed_bulktail;
710 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
711 !ohci->ed_rm_list[1] && !ohci->sleeping) {
712 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500713 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200714 }
715 ohci->ed_bulktail = edi;
716 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200717
718 case PIPE_INTERRUPT:
719 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200720 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200721 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200722 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200723 ed->int_branch = int_branch;
724
Remy Bohmer172a1132008-09-16 14:55:43 +0200725 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200726 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200727 for (ed_p = &(ohci->hcca->int_table[\
728 ep_rev(5, i) + int_branch]);
729 (*ed_p != 0) &&
730 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200731 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200732 inter = ep_rev(6,
733 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200734 ed->hwNextED = *ed_p;
Hans de Goede8e41f662015-05-05 23:56:13 +0200735 flush_dcache_ed(ed);
Martin Krause8a588192007-08-21 12:40:34 +0200736 *ed_p = m32_swap((unsigned long)ed);
Hans de Goede8e41f662015-05-05 23:56:13 +0200737 flush_dcache_hcca(ohci->hcca);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200738 }
739 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200740 }
741 return 0;
742}
743
744/*-------------------------------------------------------------------------*/
745
Zhang Wei8d15efa2007-06-06 10:08:14 +0200746/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200747static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
748 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200749{
Hans de Goede8e41f662015-05-05 23:56:13 +0200750 __maybe_unused unsigned long aligned_ed_p;
751
Zhang Wei8d15efa2007-06-06 10:08:14 +0200752 for (; index < NUM_INTS; index += period) {
753 __u32 *ed_p = &ohci->hcca->int_table [index];
754
755 /* ED might have been unlinked through another path */
756 while (*ed_p != 0) {
Andre Przywarac114a982016-10-21 02:24:29 +0100757 if (((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200758 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200759 *ed_p = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200760 aligned_ed_p = (unsigned long)ed_p;
761 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
762 flush_dcache_range(aligned_ed_p,
763 aligned_ed_p + ARCH_DMA_MINALIGN);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200764 break;
765 }
Andre Przywarac114a982016-10-21 02:24:29 +0100766 ed_p = &(((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200767 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200768 }
769 }
770}
771
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200772/* unlink an ed from one of the HC chains.
773 * just the link to the ed is unlinked.
774 * the link from the ed still points to another operational ed or 0
775 * so the HC can eventually finish the processing of the unlinked ed */
776
Remy Bohmer172a1132008-09-16 14:55:43 +0200777static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200778{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100779 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200780 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100781
Remy Bohmer172a1132008-09-16 14:55:43 +0200782 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Hans de Goede8e41f662015-05-05 23:56:13 +0200783 flush_dcache_ed(ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200784
785 switch (ed->type) {
786 case PIPE_CONTROL:
787 if (ed->ed_prev == NULL) {
788 if (!ed->hwNextED) {
789 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500790 ohci_writel(ohci->hc_control,
791 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200792 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500793 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200794 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200795 } else {
796 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200797 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200798 }
799 if (ohci->ed_controltail == ed) {
800 ohci->ed_controltail = ed->ed_prev;
801 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100802 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200803 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200804 }
805 break;
806
807 case PIPE_BULK:
808 if (ed->ed_prev == NULL) {
809 if (!ed->hwNextED) {
810 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500811 ohci_writel(ohci->hc_control,
812 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200813 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500814 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200815 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200816 } else {
817 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200818 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200819 }
820 if (ohci->ed_bulktail == ed) {
821 ohci->ed_bulktail = ed->ed_prev;
822 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100823 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200824 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200825 }
826 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200827
828 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200829 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200830 for (i = ed->int_branch; i < 32; i += ed->int_interval)
831 ohci->ohci_int_load[i] -= ed->int_load;
832 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200833 }
834 ed->state = ED_UNLINK;
835 return 0;
836}
837
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200838/*-------------------------------------------------------------------------*/
839
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200840/* add/reinit an endpoint; this should be done once at the
841 * usb_set_configuration command, but the USB stack is a little bit
842 * stateless so we do it at every transaction if the state of the ed
843 * is ED_NEW then a dummy td is added and the state is changed to
844 * ED_UNLINK in all other cases the state is left unchanged the ed
845 * info fields are setted anyway even though most of them should not
846 * change
847 */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200848static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
849 unsigned long pipe, int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200850{
851 td_t *td;
852 ed_t *ed_ret;
853 volatile ed_t *ed;
854
Hans de Goede0c0e9602015-05-05 23:56:08 +0200855 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer172a1132008-09-16 14:55:43 +0200856 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200857
858 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
859 err("ep_add_ed: pending delete");
860 /* pending delete request */
861 return NULL;
862 }
863
864 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200865 /* dummy td; end of td list for ed */
Hans de Goedee5ef4212015-05-05 23:56:09 +0200866 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer172a1132008-09-16 14:55:43 +0200867 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200868 ed->hwHeadP = ed->hwTailP;
869 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200870 ed->type = usb_pipetype(pipe);
Hans de Goede0c0e9602015-05-05 23:56:08 +0200871 ohci_dev->ed_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200872 }
873
Remy Bohmer172a1132008-09-16 14:55:43 +0200874 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
875 | usb_pipeendpoint(pipe) << 7
876 | (usb_pipeisoc(pipe)? 0x8000: 0)
877 | (usb_pipecontrol(pipe)? 0: \
878 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000879 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer172a1132008-09-16 14:55:43 +0200880 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200881
Zhang Wei8d15efa2007-06-06 10:08:14 +0200882 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
883 ed->int_period = interval;
884 ed->int_load = load;
885 }
886
Hans de Goede8e41f662015-05-05 23:56:13 +0200887 flush_dcache_ed(ed);
888
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200889 return ed_ret;
890}
891
892/*-------------------------------------------------------------------------*
893 * TD handling functions
894 *-------------------------------------------------------------------------*/
895
896/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
897
Remy Bohmer172a1132008-09-16 14:55:43 +0200898static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200899 void *data, int len,
900 struct usb_device *dev, int index, urb_priv_t *urb_priv)
901{
902 volatile td_t *td, *td_pt;
903#ifdef OHCI_FILL_TRACE
904 int i;
905#endif
906
907 if (index > urb_priv->length) {
908 err("index > length");
909 return;
910 }
911 /* use this td as the next dummy */
912 td_pt = urb_priv->td [index];
913 td_pt->hwNextTD = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200914 flush_dcache_td(td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200915
916 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200917 td = urb_priv->td [index] =
Andre Przywarac114a982016-10-21 02:24:29 +0100918 (td_t *)(uintptr_t)
919 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200920
921 td->ed = urb_priv->ed;
922 td->next_dl_td = NULL;
923 td->index = index;
Andre Przywarac114a982016-10-21 02:24:29 +0100924 td->data = (uintptr_t)data;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200925#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200926 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200927 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200928 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200929 printf("\n");
930 }
931#endif
932 if (!len)
933 data = 0;
934
Remy Bohmer172a1132008-09-16 14:55:43 +0200935 td->hwINFO = m32_swap(info);
936 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200937 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200938 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200939 else
940 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200941
942 td->hwNextTD = m32_swap((unsigned long)td_pt);
Hans de Goede8e41f662015-05-05 23:56:13 +0200943 flush_dcache_td(td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200944
945 /* append to queue */
946 td->ed->hwTailP = td->hwNextTD;
Hans de Goede8e41f662015-05-05 23:56:13 +0200947 flush_dcache_ed(td->ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200948}
949
950/*-------------------------------------------------------------------------*/
951
952/* prepare all TDs of a transfer */
953
Hans de Goede2a240e72015-05-05 23:56:07 +0200954static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
955 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200956 struct devrequest *setup, urb_priv_t *urb,
957 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200958{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200959 int data_len = transfer_len;
960 void *data;
961 int cnt = 0;
962 __u32 info = 0;
963 unsigned int toggle = 0;
964
Hans de Goede8e41f662015-05-05 23:56:13 +0200965 flush_dcache_buffer(buffer, data_len);
966
Remy Bohmer172a1132008-09-16 14:55:43 +0200967 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700968 * bits for resetting */
Remy Bohmer172a1132008-09-16 14:55:43 +0200969 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200970 toggle = TD_T_TOGGLE;
971 } else {
972 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200973 usb_settoggle(dev, usb_pipeendpoint(pipe),
974 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200975 }
976 urb->td_cnt = 0;
977 if (data_len)
978 data = buffer;
979 else
980 data = 0;
981
Remy Bohmer172a1132008-09-16 14:55:43 +0200982 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200983 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200984 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200985 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200986 while (data_len > 4096) {
987 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
988 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200989 data += 4096; data_len -= 4096; cnt++;
990 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200991 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200992 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200993 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
994 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200995 cnt++;
996
Remy Bohmer172a1132008-09-16 14:55:43 +0200997 if (!ohci->sleeping) {
998 /* start bulk list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500999 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001000 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001001 break;
1002
1003 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +02001004 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001005 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001006 flush_dcache_buffer(setup, 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001007 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1008
1009 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001010 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001011 info = usb_pipeout(pipe)?
1012 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1013 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001014 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +02001015 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001016 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001017
1018 /* Status phase */
Hans de Goede60f4bd02015-05-05 23:56:12 +02001019 info = (usb_pipeout(pipe) || data_len == 0) ?
Remy Bohmer172a1132008-09-16 14:55:43 +02001020 TD_CC | TD_DP_IN | TD_T_DATA1:
1021 TD_CC | TD_DP_OUT | TD_T_DATA1;
1022 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1023
1024 if (!ohci->sleeping) {
1025 /* start Control list */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001026 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001027 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001028 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001029
1030 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +02001031 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +02001032 TD_CC | TD_DP_OUT | toggle:
1033 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +02001034 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001035 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001036 }
1037 if (urb->length != cnt)
1038 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1039}
1040
1041/*-------------------------------------------------------------------------*
1042 * Done List handling functions
1043 *-------------------------------------------------------------------------*/
1044
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001045/* calculate the transfer length and update the urb */
1046
Remy Bohmer172a1132008-09-16 14:55:43 +02001047static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001048{
Wolfgang Denk0ef5f622011-10-05 23:03:43 +02001049 __u32 tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001050 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001051
Remy Bohmer172a1132008-09-16 14:55:43 +02001052 tdBE = m32_swap(td->hwBE);
1053 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001054
Remy Bohmer47e72082008-10-10 10:23:21 +02001055 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001056 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1057 if (tdBE != 0) {
1058 if (td->hwCBP == 0)
1059 lurb_priv->actual_length += tdBE - td->data + 1;
1060 else
1061 lurb_priv->actual_length += tdCBP - td->data;
1062 }
1063 }
1064}
1065
1066/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001067static void check_status(td_t *td_list)
1068{
1069 urb_priv_t *lurb_priv = td_list->ed->purb;
1070 int urb_len = lurb_priv->length;
1071 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1072 int cc;
1073
1074 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1075 if (cc) {
1076 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1077
Hans de Goede8e41f662015-05-05 23:56:13 +02001078 invalidate_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001079 if (*phwHeadP & m32_swap(0x1)) {
1080 if (lurb_priv &&
1081 ((td_list->index + 1) < urb_len)) {
1082 *phwHeadP =
1083 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1084 m32_swap(0xfffffff0)) |
1085 (*phwHeadP & m32_swap(0x2));
1086
1087 lurb_priv->td_cnt += urb_len -
1088 td_list->index - 1;
1089 } else
1090 *phwHeadP &= m32_swap(0xfffffff2);
Hans de Goede8e41f662015-05-05 23:56:13 +02001091 flush_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001092 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001093 }
1094}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001095
1096/* replies to the request have to be on a FIFO basis so
1097 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +02001098static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001099{
Andre Przywarac114a982016-10-21 02:24:29 +01001100 uintptr_t td_list_hc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001101 td_t *td_rev = NULL;
1102 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001103
Hans de Goede8e41f662015-05-05 23:56:13 +02001104 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +02001105 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001106 ohci->hcca->done_head = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001107 flush_dcache_hcca(ohci->hcca);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001108
1109 while (td_list_hc) {
1110 td_list = (td_t *)td_list_hc;
Hans de Goede8e41f662015-05-05 23:56:13 +02001111 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001112 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001113 td_list->next_dl_td = td_rev;
1114 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001115 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001116 }
1117 return td_list;
1118}
1119
1120/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001121/*-------------------------------------------------------------------------*/
1122
1123static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1124{
1125 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
Hans de Goede27873372015-05-10 14:10:22 +02001126 urb->finished = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001127 else
1128 dbg("finish_urb: strange.., ED state %x, \n", status);
1129}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001130
Remy Bohmer172a1132008-09-16 14:55:43 +02001131/*
1132 * Used to take back a TD from the host controller. This would normally be
1133 * called from within dl_done_list, however it may be called directly if the
1134 * HC no longer sees the TD and it has not appeared on the donelist (after
1135 * two frames). This bug has been observed on ZF Micro systems.
1136 */
1137static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001138{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001139 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001140 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001141 int stat = 0;
1142 /* urb_t *urb; */
1143 urb_priv_t *lurb_priv;
1144 __u32 tdINFO, edHeadP, edTailP;
1145
Hans de Goede8e41f662015-05-05 23:56:13 +02001146 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001147 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001148
Remy Bohmer172a1132008-09-16 14:55:43 +02001149 ed = td_list->ed;
1150 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001151
Remy Bohmer172a1132008-09-16 14:55:43 +02001152 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001153
Remy Bohmer172a1132008-09-16 14:55:43 +02001154 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001155
Remy Bohmer172a1132008-09-16 14:55:43 +02001156 /* error code of transfer */
1157 cc = TD_CC_GET(tdINFO);
1158 if (cc) {
1159 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1160 stat = cc_to_error[cc];
1161 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001162
Remy Bohmer172a1132008-09-16 14:55:43 +02001163 /* see if this done list makes for all TD's of current URB,
1164 * and mark the URB finished if so */
1165 if (lurb_priv->td_cnt == lurb_priv->length)
1166 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001167
Remy Bohmer172a1132008-09-16 14:55:43 +02001168 dbg("dl_done_list: processing TD %x, len %x\n",
1169 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001170
Remy Bohmer47e72082008-10-10 10:23:21 +02001171 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Hans de Goede8e41f662015-05-05 23:56:13 +02001172 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001173 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1174 edTailP = m32_swap(ed->hwTailP);
1175
1176 /* unlink eds if they are not busy */
1177 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1178 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001179 }
1180 return stat;
1181}
1182
Remy Bohmer172a1132008-09-16 14:55:43 +02001183static int dl_done_list(ohci_t *ohci)
1184{
1185 int stat = 0;
1186 td_t *td_list = dl_reverse_done_list(ohci);
1187
1188 while (td_list) {
1189 td_t *td_next = td_list->next_dl_td;
1190 stat = takeback_td(ohci, td_list);
1191 td_list = td_next;
1192 }
1193 return stat;
1194}
1195
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001196/*-------------------------------------------------------------------------*
1197 * Virtual Root Hub
1198 *-------------------------------------------------------------------------*/
1199
Stephen Warren39c89682014-02-13 21:15:18 -07001200#include <usbroothubdes.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001201
1202/* Hub class-specific descriptor is constructed dynamically */
1203
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001204/*-------------------------------------------------------------------------*/
1205
1206#define OK(x) len = (x); break
1207#ifdef DEBUG
Becky Bruce4abaacb2010-06-30 13:05:44 -05001208#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001209 &ohci->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +02001210#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goede2a240e72015-05-05 23:56:07 +02001211 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001212#else
Hans de Goede2a240e72015-05-05 23:56:07 +02001213#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001214#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001215 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001216#endif
Hans de Goede2a240e72015-05-05 23:56:07 +02001217#define RD_RH_STAT roothub_status(ohci)
1218#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001219
1220/* request to virtual root hub */
1221
1222int rh_check_port_status(ohci_t *controller)
1223{
1224 __u32 temp, ndp, i;
1225 int res;
1226
1227 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001228 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001229 ndp = (temp & RH_A_NDP);
1230#ifdef CONFIG_AT91C_PQFP_UHPBUG
1231 ndp = (ndp == 2) ? 1:0;
1232#endif
1233 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001234 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001235 /* check for a device disconnect */
1236 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1237 (RH_PS_PESC | RH_PS_CSC)) &&
1238 ((temp & RH_PS_CCS) == 0)) {
1239 res = i;
1240 break;
1241 }
1242 }
1243 return res;
1244}
1245
Hans de Goede2a240e72015-05-05 23:56:07 +02001246static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1247 unsigned long pipe, void *buffer, int transfer_len,
1248 struct devrequest *cmd)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001249{
Remy Bohmer172a1132008-09-16 14:55:43 +02001250 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001251 int leni = transfer_len;
1252 int len = 0;
1253 int stat = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001254 __u16 bmRType_bReq;
1255 __u16 wValue;
1256 __u16 wIndex;
1257 __u16 wLength;
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001258 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasutfc5cf882011-10-07 02:00:13 +02001259
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001260#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001261pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001262 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001263#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001264 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001265#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001266 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001267 info("Root-Hub submit IRQ: NOT implemented");
1268 return 0;
1269 }
1270
1271 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001272 wValue = le16_to_cpu(cmd->value);
1273 wIndex = le16_to_cpu(cmd->index);
1274 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001275
1276 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1277 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1278
1279 switch (bmRType_bReq) {
1280 /* Request Destination:
1281 without flags: Device,
1282 RH_INTERFACE: interface,
1283 RH_ENDPOINT: endpoint,
1284 RH_CLASS means HUB here,
1285 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1286 */
1287
1288 case RH_GET_STATUS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001289 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001290 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001291 case RH_GET_STATUS | RH_INTERFACE:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001292 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001293 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001294 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001295 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001296 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001297 case RH_GET_STATUS | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001298 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001299 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001300 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001301 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001302 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer172a1132008-09-16 14:55:43 +02001303 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001304
1305 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1306 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001307 case (RH_ENDPOINT_STALL):
1308 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001309 }
1310 break;
1311
1312 case RH_CLEAR_FEATURE | RH_CLASS:
1313 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001314 case RH_C_HUB_LOCAL_POWER:
1315 OK(0);
1316 case (RH_C_HUB_OVER_CURRENT):
1317 WR_RH_STAT(RH_HS_OCIC);
1318 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001319 }
1320 break;
1321
1322 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1323 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001324 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1325 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1326 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1327 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1328 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1329 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1330 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1331 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001332 }
1333 break;
1334
1335 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1336 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001337 case (RH_PORT_SUSPEND):
1338 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1339 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1340 if (RD_RH_PORTSTAT & RH_PS_CCS)
1341 WR_RH_PORTSTAT(RH_PS_PRS);
1342 OK(0);
1343 case (RH_PORT_POWER):
1344 WR_RH_PORTSTAT(RH_PS_PPS);
Remy Bohmer172a1132008-09-16 14:55:43 +02001345 OK(0);
1346 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1347 if (RD_RH_PORTSTAT & RH_PS_CCS)
1348 WR_RH_PORTSTAT(RH_PS_PES);
1349 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001350 }
1351 break;
1352
Remy Bohmer172a1132008-09-16 14:55:43 +02001353 case RH_SET_ADDRESS:
Hans de Goede2a240e72015-05-05 23:56:07 +02001354 ohci->rh.devnum = wValue;
Remy Bohmer172a1132008-09-16 14:55:43 +02001355 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001356
1357 case RH_GET_DESCRIPTOR:
1358 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001359 case (0x01): /* device descriptor */
1360 len = min_t(unsigned int,
1361 leni,
1362 min_t(unsigned int,
1363 sizeof(root_hub_dev_des),
1364 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001365 databuf = root_hub_dev_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001366 case (0x02): /* configuration descriptor */
1367 len = min_t(unsigned int,
1368 leni,
1369 min_t(unsigned int,
1370 sizeof(root_hub_config_des),
1371 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001372 databuf = root_hub_config_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001373 case (0x03): /* string descriptors */
1374 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001375 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001376 leni,
1377 min_t(unsigned int,
1378 sizeof(root_hub_str_index0),
1379 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001380 databuf = root_hub_str_index0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001381 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001382 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001383 if (wValue == 0x0301) {
1384 len = min_t(unsigned int,
1385 leni,
1386 min_t(unsigned int,
1387 sizeof(root_hub_str_index1),
1388 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001389 databuf = root_hub_str_index1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001390 OK(len);
1391 }
1392 default:
1393 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001394 }
1395 break;
1396
1397 case RH_GET_DESCRIPTOR | RH_CLASS:
1398 {
Hans de Goede2a240e72015-05-05 23:56:07 +02001399 __u32 temp = roothub_a(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001400
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001401 databuf[0] = 9; /* min length; */
1402 databuf[1] = 0x29;
1403 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001404#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001405 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001406#endif
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001407 databuf[3] = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001408 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001409 databuf[3] |= 0x1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001410 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001411 databuf[3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001412 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001413 databuf[3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001414
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001415 databuf[4] = 0;
1416 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1417 databuf[6] = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001418 temp = roothub_b(ohci);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001419 databuf[7] = temp & RH_B_DR;
1420 if (databuf[2] < 7) {
1421 databuf[8] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001422 } else {
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001423 databuf[0] += 2;
1424 databuf[8] = (temp & RH_B_DR) >> 8;
1425 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001426 }
1427
1428 len = min_t(unsigned int, leni,
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001429 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001430 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001431 }
1432
Marek Vasutfc5cf882011-10-07 02:00:13 +02001433 case RH_GET_CONFIGURATION:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001434 databuf[0] = 0x01;
Marek Vasutfc5cf882011-10-07 02:00:13 +02001435 OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001436
Marek Vasutfc5cf882011-10-07 02:00:13 +02001437 case RH_SET_CONFIGURATION:
1438 WR_RH_STAT(0x10000);
1439 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001440
1441 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001442 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001443 stat = USB_ST_STALLED;
1444 }
1445
1446#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001447 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001448#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001449 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001450#endif
1451
1452 len = min_t(int, len, leni);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001453 if (data != databuf)
1454 memcpy(data, databuf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001455 dev->act_len = len;
1456 dev->status = stat;
1457
1458#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001459 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer172a1132008-09-16 14:55:43 +02001460 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001461#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001462 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001463#endif
1464
1465 return stat;
1466}
1467
1468/*-------------------------------------------------------------------------*/
1469
Hans de Goeded7a39422015-05-13 14:42:15 +02001470static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1471{
1472 int i;
1473
1474 if (!intr)
1475 return &ohci->ohci_dev;
1476
1477 /* First see if we already have an ohci_dev for this dev. */
1478 for (i = 0; i < NUM_INT_DEVS; i++) {
1479 if (ohci->int_dev[i].devnum == devnum)
1480 return &ohci->int_dev[i];
1481 }
1482
1483 /* If not then find a free one. */
1484 for (i = 0; i < NUM_INT_DEVS; i++) {
1485 if (ohci->int_dev[i].devnum == -1) {
1486 ohci->int_dev[i].devnum = devnum;
1487 return &ohci->int_dev[i];
1488 }
1489 }
1490
1491 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1492 return NULL;
1493}
1494
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001495/* common code for handling submit messages - used for all but root hub */
1496/* accesses. */
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001497static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1498 void *buffer, int transfer_len, int interval)
1499{
1500 urb_priv_t *urb;
1501
1502 urb = calloc(1, sizeof(urb_priv_t));
1503 if (!urb) {
1504 printf("ohci: Error out of memory allocating urb\n");
1505 return NULL;
1506 }
1507
1508 urb->dev = dev;
1509 urb->pipe = pipe;
1510 urb->transfer_buffer = buffer;
1511 urb->transfer_buffer_length = transfer_len;
1512 urb->interval = interval;
1513
1514 return urb;
1515}
1516
Hans de Goede2a240e72015-05-05 23:56:07 +02001517static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1518 unsigned long pipe, void *buffer, int transfer_len,
1519 struct devrequest *setup, int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001520{
1521 int stat = 0;
1522 int maxsize = usb_maxpacket(dev, pipe);
1523 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001524 urb_priv_t *urb;
Hans de Goeded7a39422015-05-13 14:42:15 +02001525 ohci_dev_t *ohci_dev;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001526
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001527 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1528 if (!urb)
1529 return -ENOMEM;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001530
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001531#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001532 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001533 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001534 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001535#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001536 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001537#endif
1538 if (!maxsize) {
1539 err("submit_common_message: pipesize for pipe %lx is zero",
1540 pipe);
1541 return -1;
1542 }
1543
Hans de Goeded7a39422015-05-13 14:42:15 +02001544 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1545 if (!ohci_dev)
1546 return -ENOMEM;
1547
1548 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001549 err("sohci_submit_job failed");
1550 return -1;
1551 }
1552
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001553 mdelay(10);
Hans de Goede2a240e72015-05-05 23:56:07 +02001554 /* ohci_dump_status(ohci); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001555
Simon Glassfd7f5132011-02-07 14:42:16 -08001556 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001557
1558 /* wait for it to complete */
1559 for (;;) {
1560 /* check whether the controller is done */
Hans de Goede2a240e72015-05-05 23:56:07 +02001561 stat = hc_interrupt(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001562 if (stat < 0) {
1563 stat = USB_ST_CRC_ERR;
1564 break;
1565 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001566
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001567 /* NOTE: since we are not interrupt driven in U-Boot and always
1568 * handle only one URB at a time, we cannot assume the
1569 * transaction finished on the first successful return from
1570 * hc_interrupt().. unless the flag for current URB is set,
1571 * meaning that all TD's to/from device got actually
1572 * transferred and processed. If the current URB is not
1573 * finished we need to re-iterate this loop so as
1574 * hc_interrupt() gets called again as there needs to be some
1575 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001576 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001577 /* 0xff is returned for an SF-interrupt */
1578 break;
1579 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001580
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001581 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001582 mdelay(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001583 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001584 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001585
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001586 } else {
Hans de Goeded98dc992015-05-05 23:56:14 +02001587 if (!usb_pipeint(pipe))
1588 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001589 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001590 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001591 stat = USB_ST_CRC_ERR;
1592 break;
1593 }
1594 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001595
1596 dev->status = stat;
Mateusz Kulikowski10b479d2013-10-23 20:26:27 +02001597 dev->act_len = urb->actual_length;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001598
Hans de Goede8e41f662015-05-05 23:56:13 +02001599 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1600 invalidate_dcache_buffer(buffer, dev->act_len);
1601
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001602#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001603 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001604 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001605#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001606 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001607#endif
Hans de Goede27873372015-05-10 14:10:22 +02001608 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001609 return 0;
1610}
1611
Hans de Goede5477e6f2015-05-13 14:42:17 +02001612#define MAX_INT_QUEUESIZE 8
1613
1614struct int_queue {
1615 int queuesize;
1616 int curr_urb;
1617 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1618};
1619
1620static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1621 struct usb_device *udev, unsigned long pipe, int queuesize,
1622 int elementsize, void *buffer, int interval)
1623{
1624 struct int_queue *queue;
1625 ohci_dev_t *ohci_dev;
1626 int i;
1627
1628 if (queuesize > MAX_INT_QUEUESIZE)
1629 return NULL;
1630
1631 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1632 if (!ohci_dev)
1633 return NULL;
1634
1635 queue = malloc(sizeof(*queue));
1636 if (!queue) {
1637 printf("ohci: Error out of memory allocating int queue\n");
1638 return NULL;
1639 }
1640
1641 for (i = 0; i < queuesize; i++) {
1642 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1643 buffer + i * elementsize,
1644 elementsize, interval);
1645 if (!queue->urb[i])
1646 break;
1647
1648 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1649 printf("ohci: Error submitting int queue job\n");
1650 urb_free_priv(queue->urb[i]);
1651 break;
1652 }
1653 }
1654 if (i == 0) {
1655 /* We did not succeed in submitting even 1 urb */
1656 free(queue);
1657 return NULL;
1658 }
1659
1660 queue->queuesize = i;
1661 queue->curr_urb = 0;
1662
1663 return queue;
1664}
1665
1666static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1667 struct int_queue *queue)
1668{
1669 if (queue->curr_urb == queue->queuesize)
1670 return NULL; /* Queue depleted */
1671
1672 if (hc_interrupt(ohci) < 0)
1673 return NULL;
1674
1675 if (queue->urb[queue->curr_urb]->finished) {
1676 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1677 queue->curr_urb++;
1678 return ret;
1679 }
1680
1681 return NULL;
1682}
1683
1684static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1685 struct int_queue *queue)
1686{
1687 int i;
1688
1689 for (i = 0; i < queue->queuesize; i++)
1690 urb_free_priv(queue->urb[i]);
1691
1692 free(queue);
1693
1694 return 0;
1695}
1696
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001697#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001698/* submit routines called from usb.c */
1699int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1700 int transfer_len)
1701{
1702 info("submit_bulk_msg");
Hans de Goede2a240e72015-05-05 23:56:07 +02001703 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1704 NULL, 0);
1705}
1706
1707int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001708 int transfer_len, int interval, bool nonblock)
Hans de Goede2a240e72015-05-05 23:56:07 +02001709{
1710 info("submit_int_msg");
1711 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1712 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001713}
Hans de Goede5477e6f2015-05-13 14:42:17 +02001714
1715struct int_queue *create_int_queue(struct usb_device *dev,
1716 unsigned long pipe, int queuesize, int elementsize,
1717 void *buffer, int interval)
1718{
1719 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1720 elementsize, buffer, interval);
1721}
1722
1723void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1724{
1725 return _ohci_poll_int_queue(&gohci, dev, queue);
1726}
1727
1728int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1729{
1730 return _ohci_destroy_int_queue(&gohci, dev, queue);
1731}
Hans de Goede8a62c502015-05-10 14:10:25 +02001732#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001733
Hans de Goede2a240e72015-05-05 23:56:07 +02001734static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1735 unsigned long pipe, void *buffer, int transfer_len,
1736 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001737{
1738 int maxsize = usb_maxpacket(dev, pipe);
1739
1740 info("submit_control_msg");
1741#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001742 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001743 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001744#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001745 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001746#endif
1747 if (!maxsize) {
1748 err("submit_control_message: pipesize for pipe %lx is zero",
1749 pipe);
1750 return -1;
1751 }
Hans de Goede2a240e72015-05-05 23:56:07 +02001752 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1753 ohci->rh.dev = dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001754 /* root hub - redirect */
Hans de Goede2a240e72015-05-05 23:56:07 +02001755 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1756 transfer_len, setup);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001757 }
1758
Hans de Goede2a240e72015-05-05 23:56:07 +02001759 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1760 setup, 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001761}
1762
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001763/*-------------------------------------------------------------------------*
1764 * HC functions
1765 *-------------------------------------------------------------------------*/
1766
1767/* reset the HC and BUS */
1768
Remy Bohmer172a1132008-09-16 14:55:43 +02001769static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001770{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001771#ifdef CONFIG_PCI_EHCI_DEVNO
1772 pci_dev_t pdev;
1773#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001774 int timeout = 30;
1775 int smm_timeout = 50; /* 0,5 sec */
1776
1777 dbg("%s\n", __FUNCTION__);
1778
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001779#ifdef CONFIG_PCI_EHCI_DEVNO
1780 /*
1781 * Some multi-function controllers (e.g. ISP1562) allow root hub
1782 * resetting via EHCI registers only.
1783 */
1784 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1785 if (pdev != -1) {
1786 u32 base;
1787 int timeout = 1000;
1788
1789 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001790 base += EHCI_USBCMD_OFF;
1791 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001792
Becky Bruce4abaacb2010-06-30 13:05:44 -05001793 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001794 if (timeout-- <= 0) {
1795 printf("USB RootHub reset timed out!");
1796 break;
1797 }
1798 udelay(1);
1799 }
1800 } else
1801 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1802#endif
Becky Bruce4abaacb2010-06-30 13:05:44 -05001803 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1804 /* SMM owns the HC, request ownership */
1805 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001806 info("USB HC TakeOver from SMM");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001807 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001808 mdelay(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001809 if (--smm_timeout == 0) {
1810 err("USB HC TakeOver failed!");
1811 return -1;
1812 }
1813 }
1814 }
1815
1816 /* Disable HC interrupts */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001817 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001818
1819 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1820 ohci->slot_name,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001821 ohci_readl(&ohci->regs->control));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001822
1823 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001824 ohci->hc_control = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001825 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001826
1827 /* HC Reset requires max 10 us delay */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001828 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1829 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001830 if (--timeout == 0) {
1831 err("USB HC reset timed out!");
1832 return -1;
1833 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001834 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001835 }
1836 return 0;
1837}
1838
1839/*-------------------------------------------------------------------------*/
1840
1841/* Start an OHCI controller, set the BUS operational
1842 * enable interrupts
1843 * connect the virtual root hub */
1844
Remy Bohmer172a1132008-09-16 14:55:43 +02001845static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001846{
1847 __u32 mask;
1848 unsigned int fminterval;
Hans de Goeded7a39422015-05-13 14:42:15 +02001849 int i;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001850
1851 ohci->disabled = 1;
Hans de Goeded7a39422015-05-13 14:42:15 +02001852 for (i = 0; i < NUM_INT_DEVS; i++)
1853 ohci->int_dev[i].devnum = -1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001854
1855 /* Tell the controller where the control and bulk lists are
1856 * The lists are empty now. */
1857
Becky Bruce4abaacb2010-06-30 13:05:44 -05001858 ohci_writel(0, &ohci->regs->ed_controlhead);
1859 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001860
Andre Przywarac114a982016-10-21 02:24:29 +01001861 ohci_writel((uintptr_t)ohci->hcca,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001862 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001863
1864 fminterval = 0x2edf;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001865 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001866 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001867 ohci_writel(fminterval, &ohci->regs->fminterval);
1868 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001869
1870 /* start controller operations */
1871 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1872 ohci->disabled = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001873 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001874
1875 /* disable all interrupts */
1876 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1877 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1878 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001879 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001880 /* clear all interrupts */
1881 mask &= ~OHCI_INTR_MIE;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001882 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001883 /* Choose the interrupts we care about now - but w/o MIE */
1884 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001885 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001886
1887#ifdef OHCI_USE_NPS
1888 /* required for AMD-756 and some Mac platforms */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001889 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001890 &ohci->regs->roothub.a);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001891 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001892#endif /* OHCI_USE_NPS */
1893
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001894 /* connect the virtual root hub */
1895 ohci->rh.devnum = 0;
1896
1897 return 0;
1898}
1899
1900/*-------------------------------------------------------------------------*/
1901
1902/* an interrupt happens */
1903
Hans de Goede2a240e72015-05-05 23:56:07 +02001904static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001905{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001906 struct ohci_regs *regs = ohci->regs;
1907 int ints;
1908 int stat = -1;
1909
Hans de Goede8e41f662015-05-05 23:56:13 +02001910 invalidate_dcache_hcca(ohci->hcca);
1911
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001912 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001913 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001914 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001915 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001916 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001917 if (ints == ~(u32)0) {
1918 ohci->disabled++;
1919 err("%s device removed!", ohci->slot_name);
1920 return -1;
1921 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001922 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer172a1132008-09-16 14:55:43 +02001923 if (ints == 0) {
1924 dbg("hc_interrupt: returning..\n");
1925 return 0xff;
1926 }
1927 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001928 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001929
Remy Bohmer172a1132008-09-16 14:55:43 +02001930 /* dbg("Interrupt: %x frame: %x", ints,
1931 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001932
Remy Bohmer172a1132008-09-16 14:55:43 +02001933 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001934 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001935
1936 if (ints & OHCI_INTR_UE) {
1937 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001938 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001939 ohci->slot_name);
1940 /* e.g. due to PCI Master/Target Abort */
1941
1942#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001943 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001944#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001945 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001946#endif
1947 /* FIXME: be optimistic, hope that bug won't repeat often. */
1948 /* Make some non-interrupt context restart the controller. */
1949 /* Count and limit the retries though; either hardware or */
1950 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001951 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001952 return -1;
1953 }
1954
1955 if (ints & OHCI_INTR_WDH) {
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001956 ohci_mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001957 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1958 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goede2a240e72015-05-05 23:56:07 +02001959 stat = dl_done_list(ohci);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001960 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1961 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001962 }
1963
1964 if (ints & OHCI_INTR_SO) {
1965 dbg("USB Schedule overrun\n");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001966 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001967 stat = -1;
1968 }
1969
1970 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1971 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001972 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001973 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001974 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001975 if (ohci->ed_rm_list[frame] != NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001976 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001977 stat = 0xff;
1978 }
1979
Becky Bruce4abaacb2010-06-30 13:05:44 -05001980 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001981 return stat;
1982}
1983
1984/*-------------------------------------------------------------------------*/
1985
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001986#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02001987
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001988/*-------------------------------------------------------------------------*/
1989
1990/* De-allocate all resources.. */
1991
Remy Bohmer172a1132008-09-16 14:55:43 +02001992static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001993{
Remy Bohmer172a1132008-09-16 14:55:43 +02001994 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001995
1996 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001997 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001998}
1999
2000/*-------------------------------------------------------------------------*/
2001
2002/*
2003 * low level initalisation routine, called from usb.c
2004 */
2005static char ohci_inited = 0;
2006
Troy Kisky8f9c49d2013-10-10 15:27:56 -07002007int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002008{
Zhang Wei8d15efa2007-06-06 10:08:14 +02002009#ifdef CONFIG_PCI_OHCI
2010 pci_dev_t pdev;
2011#endif
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002012
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002013#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002014 /* cpu dependant init */
Remy Bohmer172a1132008-09-16 14:55:43 +02002015 if (usb_cpu_init())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002016 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002017#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002018
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002019#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002020 /* board dependant init */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002021 if (board_usb_init(index, USB_INIT_HOST))
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002022 return -1;
2023#endif
Remy Bohmer172a1132008-09-16 14:55:43 +02002024 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002025
2026 /* align the storage */
2027 if ((__u32)&ghcca[0] & 0xff) {
2028 err("HCCA not aligned!!");
2029 return -1;
2030 }
Hans de Goedeeb509d32015-05-05 23:56:10 +02002031 gohci.hcca = &ghcca[0];
2032 info("aligned ghcca %p", gohci.hcca);
2033 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002034
2035 gohci.disabled = 1;
2036 gohci.sleeping = 0;
2037 gohci.irq = -1;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002038#ifdef CONFIG_PCI_OHCI
Sergei Poselenov0b4570f2008-05-22 01:15:53 +02002039 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02002040
2041 if (pdev != -1) {
2042 u16 vid, did;
2043 u32 base;
2044 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2045 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2046 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2047 vid, did, (pdev >> 16) & 0xff,
2048 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2049 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2050 printf("OHCI regs address 0x%08x\n", base);
2051 gohci.regs = (struct ohci_regs *)base;
Heiko Schocher882830a2019-07-16 10:49:06 +02002052 } else {
2053 printf("%s: OHCI devnr: %d not found\n", __func__,
2054 CONFIG_PCI_OHCI_DEVNO);
Zhang Wei8d15efa2007-06-06 10:08:14 +02002055 return -1;
Heiko Schocher882830a2019-07-16 10:49:06 +02002056 }
Zhang Wei8d15efa2007-06-06 10:08:14 +02002057#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002058 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
Zhang Wei8d15efa2007-06-06 10:08:14 +02002059#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002060
2061 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002062 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002063
2064 if (hc_reset (&gohci) < 0) {
2065 hc_release_ohci (&gohci);
2066 err ("can't reset usb-%s", gohci.slot_name);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002067#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002068 /* board dependant cleanup */
Mateusz Zalegad862f892013-10-04 19:22:26 +02002069 board_usb_cleanup(index, USB_INIT_HOST);
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002070#endif
2071
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002072#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002073 /* cpu dependant cleanup */
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02002074 usb_cpu_init_fail();
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002075#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002076 return -1;
2077 }
2078
Remy Bohmer172a1132008-09-16 14:55:43 +02002079 if (hc_start(&gohci) < 0) {
2080 err("can't start usb-%s", gohci.slot_name);
2081 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002082 /* Initialization failed */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002083#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002084 /* board dependant cleanup */
2085 usb_board_stop();
2086#endif
2087
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002088#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002089 /* cpu dependant cleanup */
2090 usb_cpu_stop();
2091#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002092 return -1;
2093 }
2094
2095#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02002096 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002097#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02002098 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002099#endif
2100 ohci_inited = 1;
2101 return 0;
2102}
2103
Lucas Stacha3231282012-09-26 00:14:34 +02002104int usb_lowlevel_stop(int index)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002105{
2106 /* this gets called really early - before the controller has */
2107 /* even been initialized! */
2108 if (!ohci_inited)
2109 return 0;
2110 /* TODO release any interrupts, etc. */
2111 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02002112 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002113
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002114#ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002115 /* board dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002116 if (usb_board_stop())
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002117 return -1;
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002118#endif
2119
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002120#ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002121 /* cpu dependant cleanup */
Remy Bohmer172a1132008-09-16 14:55:43 +02002122 if (usb_cpu_stop())
Markus Klotzbuecher98095512006-05-23 10:33:11 +02002123 return -1;
2124#endif
Remy Bohmer743dd492008-08-20 11:22:02 +02002125 /* This driver is no longer initialised. It needs a new low-level
2126 * init (board/cpu) before it can be used again. */
2127 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002128 return 0;
2129}
Hans de Goede2a240e72015-05-05 23:56:07 +02002130
2131int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2132 void *buffer, int transfer_len, struct devrequest *setup)
2133{
2134 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2135 transfer_len, setup);
2136}
Hans de Goede8a62c502015-05-10 14:10:25 +02002137#endif
2138
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01002139#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02002140static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2141 unsigned long pipe, void *buffer, int length,
2142 struct devrequest *setup)
2143{
2144 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2145
2146 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2147 length, setup);
2148}
2149
2150static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2151 unsigned long pipe, void *buffer, int length)
2152{
2153 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2154
2155 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2156}
2157
2158static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2159 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02002160 int interval, bool nonblock)
Hans de Goede8a62c502015-05-10 14:10:25 +02002161{
2162 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2163
2164 return submit_common_msg(ohci, udev, pipe, buffer, length,
2165 NULL, interval);
2166}
2167
Hans de Goede5477e6f2015-05-13 14:42:17 +02002168static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2169 struct usb_device *udev, unsigned long pipe, int queuesize,
2170 int elementsize, void *buffer, int interval)
2171{
2172 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2173
2174 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2175 buffer, interval);
2176}
2177
2178static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2179 struct int_queue *queue)
2180{
2181 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2182
2183 return _ohci_poll_int_queue(ohci, udev, queue);
2184}
2185
2186static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2187 struct int_queue *queue)
2188{
2189 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2190
2191 return _ohci_destroy_int_queue(ohci, udev, queue);
2192}
2193
Hans de Goede8a62c502015-05-10 14:10:25 +02002194int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2195{
2196 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2197 ohci_t *ohci = dev_get_priv(dev);
2198 u32 reg;
2199
2200 priv->desc_before_addr = true;
2201
2202 ohci->regs = regs;
2203 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2204 if (!ohci->hcca)
2205 return -ENOMEM;
2206 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
Hans de Goede66cb3e42015-10-20 18:39:29 +02002207 flush_dcache_hcca(ohci->hcca);
Hans de Goede8a62c502015-05-10 14:10:25 +02002208
2209 if (hc_reset(ohci) < 0)
2210 return -EIO;
2211
2212 if (hc_start(ohci) < 0)
2213 return -EIO;
2214
2215 reg = ohci_readl(&regs->revision);
2216 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2217
2218 return 0;
2219}
2220
2221int ohci_deregister(struct udevice *dev)
2222{
2223 ohci_t *ohci = dev_get_priv(dev);
2224
2225 if (hc_reset(ohci) < 0)
2226 return -EIO;
2227
2228 free(ohci->hcca);
2229
2230 return 0;
2231}
2232
2233struct dm_usb_ops ohci_usb_ops = {
2234 .control = ohci_submit_control_msg,
2235 .bulk = ohci_submit_bulk_msg,
2236 .interrupt = ohci_submit_int_msg,
Hans de Goede5477e6f2015-05-13 14:42:17 +02002237 .create_int_queue = ohci_create_int_queue,
2238 .poll_int_queue = ohci_poll_int_queue,
2239 .destroy_int_queue = ohci_destroy_int_queue,
Hans de Goede8a62c502015-05-10 14:10:25 +02002240};
2241
2242#endif