blob: 3f4418198ccd7723597625e3fef23d53cc8fe3f6 [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>
Simon Glassdbd79542020-05-10 11:40:11 -060036#include <linux/delay.h>
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020037
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020038#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060039#include <memalign.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020040#include <usb.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020041
42#include "ohci.h"
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020043
Wolfgang Denkdcac38b2007-11-19 12:59:14 +010044#ifdef CONFIG_AT91RM9200
45#include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
46#endif
47
Masahiro Yamada7a5b2922014-11-06 14:59:35 +090048#if defined(CONFIG_CPU_ARM920T) || \
Simon Glass869b59e2021-08-01 18:54:26 -060049 defined(CONFIG_PCI) || \
Heiko Schocher2e1134f2019-07-16 10:49:05 +020050 defined(CONFIG_SYS_OHCI_USE_NPS)
Markus Klotzbuecher98095512006-05-23 10:33:11 +020051# define OHCI_USE_NPS /* force NoPowerSwitching mode */
52#endif
53
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020054#undef OHCI_VERBOSE_DEBUG /* not always helpful */
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010055#undef DEBUG
56#undef SHOW_INFO
57#undef OHCI_FILL_TRACE
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020058
59/* For initializing controller (mask in an HCFS mode too) */
60#define OHCI_CONTROL_INIT \
61 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
62
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020063#ifdef DEBUG
64#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
65#else
Remy Bohmer172a1132008-09-16 14:55:43 +020066#define dbg(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020067#endif /* DEBUG */
68#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020069#ifdef SHOW_INFO
70#define info(format, arg...) printf("INFO: " format "\n", ## arg)
71#else
Remy Bohmer172a1132008-09-16 14:55:43 +020072#define info(format, arg...) do {} while (0)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020073#endif
74
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075#ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020076# define m16_swap(x) cpu_to_be16(x)
77# define m32_swap(x) cpu_to_be32(x)
Markus Klotzbuecherd209de62006-11-27 11:46:46 +010078#else
Markus Klotzbuecher18f5ccb2007-06-06 11:49:35 +020079# define m16_swap(x) cpu_to_le16(x)
80# define m32_swap(x) cpu_to_le32(x)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081#endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +020082
Wu, Joshc3857142015-07-27 11:40:18 +080083/* We really should do proper cache flushing everywhere */
Hans de Goede8e41f662015-05-05 23:56:13 +020084#define flush_dcache_buffer(addr, size) \
85 flush_dcache_range((unsigned long)(addr), \
86 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
87#define invalidate_dcache_buffer(addr, size) \
88 invalidate_dcache_range((unsigned long)(addr), \
89 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
Hans de Goede8e41f662015-05-05 23:56:13 +020090
91/* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
92#define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
93#define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
94#define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
95#define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
96#define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
97#define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
98#define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
99#define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
100
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100101#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goeded6cc1d12015-05-10 14:10:24 +0200102/*
103 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
104 * them around when building for older boards not yet converted to the dm
105 * just in case (to avoid regressions), for dm this turns them into nops.
106 */
107#define ohci_mdelay(x)
108#else
109#define ohci_mdelay(x) mdelay(x)
110#endif
111
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100112#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200113/* global ohci_t */
114static ohci_t gohci;
115/* this must be aligned to a 256 byte boundary */
116struct ohci_hcca ghcca[1];
Hans de Goede8a62c502015-05-10 14:10:25 +0200117#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200118
Hans de Goede516c9872015-05-05 23:56:11 +0200119/* mapping of the OHCI CC status to error codes */
120static int cc_to_error[16] = {
121 /* No Error */ 0,
122 /* CRC Error */ USB_ST_CRC_ERR,
123 /* Bit Stuff */ USB_ST_BIT_ERR,
124 /* Data Togg */ USB_ST_CRC_ERR,
125 /* Stall */ USB_ST_STALLED,
126 /* DevNotResp */ -1,
127 /* PIDCheck */ USB_ST_BIT_ERR,
128 /* UnExpPID */ USB_ST_BIT_ERR,
129 /* DataOver */ USB_ST_BUF_ERR,
130 /* DataUnder */ USB_ST_BUF_ERR,
131 /* reservd */ -1,
132 /* reservd */ -1,
133 /* BufferOver */ USB_ST_BUF_ERR,
134 /* BuffUnder */ USB_ST_BUF_ERR,
135 /* Not Access */ -1,
136 /* Not Access */ -1
137};
138
139static const char *cc_to_string[16] = {
140 "No Error",
141 "CRC: Last data packet from endpoint contained a CRC error.",
142 "BITSTUFFING: Last data packet from endpoint contained a bit " \
143 "stuffing violation",
144 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
145 "that did not match the expected value.",
146 "STALL: TD was moved to the Done Queue because the endpoint returned" \
147 " a STALL PID",
148 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
149 "not provide a handshake (OUT)",
150 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
151 "(IN) or handshake (OUT)",
152 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
153 "value is not defined.",
154 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
155 "either the size of the maximum data packet allowed\n" \
156 "from the endpoint (found in MaximumPacketSize field\n" \
157 "of ED) or the remaining buffer size.",
158 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
159 "and that amount was not sufficient to fill the\n" \
160 "specified buffer",
161 "reserved1",
162 "reserved2",
163 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
164 "than it could be written to system memory",
165 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
166 "system memory fast enough to keep up with data USB " \
167 "data rate.",
168 "NOT ACCESSED: This code is set by software before the TD is placed" \
169 "on a list to be processed by the HC.(1)",
170 "NOT ACCESSED: This code is set by software before the TD is placed" \
171 "on a list to be processed by the HC.(2)",
172};
173
Remy Bohmer172a1132008-09-16 14:55:43 +0200174static inline u32 roothub_a(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500175 { return ohci_readl(&hc->regs->roothub.a); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200176static inline u32 roothub_b(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500177 { return ohci_readl(&hc->regs->roothub.b); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200178static inline u32 roothub_status(struct ohci *hc)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500179 { return ohci_readl(&hc->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +0200180static inline u32 roothub_portstatus(struct ohci *hc, int i)
Becky Bruce4abaacb2010-06-30 13:05:44 -0500181 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200182
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200183/* forward declaration */
Hans de Goede2a240e72015-05-05 23:56:07 +0200184static int hc_interrupt(ohci_t *ohci);
185static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
186 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200187 struct devrequest *setup, urb_priv_t *urb,
188 int interval);
Hans de Goede516c9872015-05-05 23:56:11 +0200189static int ep_link(ohci_t * ohci, ed_t * ed);
190static int ep_unlink(ohci_t * ohci, ed_t * ed);
191static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
192 unsigned long pipe, int interval, int load);
193
194/*-------------------------------------------------------------------------*/
195
196/* TDs ... */
197static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
198{
199 int i;
200 struct td *td;
201
202 td = NULL;
203 for (i = 0; i < NUM_TD; i++)
204 {
205 if (ohci_dev->tds[i].usb_dev == NULL)
206 {
207 td = &ohci_dev->tds[i];
208 td->usb_dev = usb_dev;
209 break;
210 }
211 }
212
213 return td;
214}
215
216static inline void ed_free(struct ed *ed)
217{
218 ed->usb_dev = NULL;
219}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200220
221/*-------------------------------------------------------------------------*
222 * URB support functions
223 *-------------------------------------------------------------------------*/
224
225/* free HCD-private data associated with this URB */
226
Remy Bohmer172a1132008-09-16 14:55:43 +0200227static void urb_free_priv(urb_priv_t *urb)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200228{
229 int i;
230 int last;
Remy Bohmer172a1132008-09-16 14:55:43 +0200231 struct td *td;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200232
233 last = urb->length - 1;
234 if (last >= 0) {
235 for (i = 0; i <= last; i++) {
236 td = urb->td[i];
237 if (td) {
238 td->usb_dev = NULL;
239 urb->td[i] = NULL;
240 }
241 }
242 }
Zhang Wei8d15efa2007-06-06 10:08:14 +0200243 free(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200244}
245
246/*-------------------------------------------------------------------------*/
247
248#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +0200249static int sohci_get_current_frame_number(ohci_t *ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200250
251/* debug| print the main components of an URB
252 * small: 0) header + data packets 1) just header */
253
Hans de Goede2a240e72015-05-05 23:56:07 +0200254static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
Remy Bohmer172a1132008-09-16 14:55:43 +0200255 unsigned long pipe, void *buffer, int transfer_len,
256 struct devrequest *setup, char *str, int small)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200257{
Remy Bohmer172a1132008-09-16 14:55:43 +0200258 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200259 str,
Hans de Goede2a240e72015-05-05 23:56:07 +0200260 sohci_get_current_frame_number(ohci),
Remy Bohmer172a1132008-09-16 14:55:43 +0200261 usb_pipedevice(pipe),
262 usb_pipeendpoint(pipe),
263 usb_pipeout(pipe)? 'O': 'I',
264 usb_pipetype(pipe) < 2 ? \
265 (usb_pipeint(pipe)? "INTR": "ISOC"): \
266 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
Zhang Wei8d15efa2007-06-06 10:08:14 +0200267 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200268 transfer_len, dev->status);
269#ifdef OHCI_VERBOSE_DEBUG
270 if (!small) {
271 int i, len;
272
Remy Bohmer172a1132008-09-16 14:55:43 +0200273 if (usb_pipecontrol(pipe)) {
274 printf(__FILE__ ": cmd(8):");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200275 for (i = 0; i < 8 ; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200276 printf(" %02x", ((__u8 *) setup) [i]);
277 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200278 }
279 if (transfer_len > 0 && buffer) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200280 printf(__FILE__ ": data(%d/%d):",
Zhang Wei8d15efa2007-06-06 10:08:14 +0200281 (purb ? purb->actual_length : 0),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200282 transfer_len);
Remy Bohmer172a1132008-09-16 14:55:43 +0200283 len = usb_pipeout(pipe)? transfer_len:
Zhang Wei8d15efa2007-06-06 10:08:14 +0200284 (purb ? purb->actual_length : 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200285 for (i = 0; i < 16 && i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200286 printf(" %02x", ((__u8 *) buffer) [i]);
287 printf("%s\n", i < len? "...": "");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200288 }
289 }
290#endif
291}
292
Remy Bohmer172a1132008-09-16 14:55:43 +0200293/* just for debugging; prints non-empty branches of the int ed tree
294 * inclusive iso eds */
295void ep_print_int_eds(ohci_t *ohci, char *str)
296{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200297 int i, j;
Remy Bohmer172a1132008-09-16 14:55:43 +0200298 __u32 *ed_p;
299 for (i = 0; i < 32; i++) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200300 j = 5;
301 ed_p = &(ohci->hcca->int_table [i]);
302 if (*ed_p == 0)
303 continue;
Hans de Goede8e41f662015-05-05 23:56:13 +0200304 invalidate_dcache_ed(ed_p);
Remy Bohmer172a1132008-09-16 14:55:43 +0200305 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200306 while (*ed_p != 0 && j--) {
307 ed_t *ed = (ed_t *)m32_swap(ed_p);
Hans de Goede8e41f662015-05-05 23:56:13 +0200308 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200309 printf(" ed: %4x;", ed->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200310 ed_p = &ed->hwNextED;
311 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200312 printf("\n");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200313 }
314}
315
Remy Bohmer172a1132008-09-16 14:55:43 +0200316static void ohci_dump_intr_mask(char *label, __u32 mask)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200317{
Remy Bohmer172a1132008-09-16 14:55:43 +0200318 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200319 label,
320 mask,
321 (mask & OHCI_INTR_MIE) ? " MIE" : "",
322 (mask & OHCI_INTR_OC) ? " OC" : "",
323 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
324 (mask & OHCI_INTR_FNO) ? " FNO" : "",
325 (mask & OHCI_INTR_UE) ? " UE" : "",
326 (mask & OHCI_INTR_RD) ? " RD" : "",
327 (mask & OHCI_INTR_SF) ? " SF" : "",
328 (mask & OHCI_INTR_WDH) ? " WDH" : "",
329 (mask & OHCI_INTR_SO) ? " SO" : ""
330 );
331}
332
Remy Bohmer172a1132008-09-16 14:55:43 +0200333static void maybe_print_eds(char *label, __u32 value)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200334{
335 ed_t *edp = (ed_t *)value;
336
337 if (value) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200338 dbg("%s %08x", label, value);
Hans de Goede8e41f662015-05-05 23:56:13 +0200339 invalidate_dcache_ed(edp);
Remy Bohmer172a1132008-09-16 14:55:43 +0200340 dbg("%08x", edp->hwINFO);
341 dbg("%08x", edp->hwTailP);
342 dbg("%08x", edp->hwHeadP);
343 dbg("%08x", edp->hwNextED);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200344 }
345}
346
Remy Bohmer172a1132008-09-16 14:55:43 +0200347static char *hcfs2string(int state)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200348{
349 switch (state) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200350 case OHCI_USB_RESET: return "reset";
351 case OHCI_USB_RESUME: return "resume";
352 case OHCI_USB_OPER: return "operational";
353 case OHCI_USB_SUSPEND: return "suspend";
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200354 }
355 return "?";
356}
357
358/* dump control and status registers */
Remy Bohmer172a1132008-09-16 14:55:43 +0200359static void ohci_dump_status(ohci_t *controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200360{
361 struct ohci_regs *regs = controller->regs;
362 __u32 temp;
363
Becky Bruce4abaacb2010-06-30 13:05:44 -0500364 temp = ohci_readl(&regs->revision) & 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200365 if (temp != 0x10)
Remy Bohmer172a1132008-09-16 14:55:43 +0200366 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200367
Becky Bruce4abaacb2010-06-30 13:05:44 -0500368 temp = ohci_readl(&regs->control);
Remy Bohmer172a1132008-09-16 14:55:43 +0200369 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200370 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
371 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
372 (temp & OHCI_CTRL_IR) ? " IR" : "",
Remy Bohmer172a1132008-09-16 14:55:43 +0200373 hcfs2string(temp & OHCI_CTRL_HCFS),
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200374 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
375 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
376 (temp & OHCI_CTRL_IE) ? " IE" : "",
377 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
378 temp & OHCI_CTRL_CBSR
379 );
380
Becky Bruce4abaacb2010-06-30 13:05:44 -0500381 temp = ohci_readl(&regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200382 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200383 (temp & OHCI_SOC) >> 16,
384 (temp & OHCI_OCR) ? " OCR" : "",
385 (temp & OHCI_BLF) ? " BLF" : "",
386 (temp & OHCI_CLF) ? " CLF" : "",
387 (temp & OHCI_HCR) ? " HCR" : ""
388 );
389
Becky Bruce4abaacb2010-06-30 13:05:44 -0500390 ohci_dump_intr_mask("intrstatus", ohci_readl(&regs->intrstatus));
391 ohci_dump_intr_mask("intrenable", ohci_readl(&regs->intrenable));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200392
Becky Bruce4abaacb2010-06-30 13:05:44 -0500393 maybe_print_eds("ed_periodcurrent",
394 ohci_readl(&regs->ed_periodcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200395
Becky Bruce4abaacb2010-06-30 13:05:44 -0500396 maybe_print_eds("ed_controlhead", ohci_readl(&regs->ed_controlhead));
397 maybe_print_eds("ed_controlcurrent",
398 ohci_readl(&regs->ed_controlcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200399
Becky Bruce4abaacb2010-06-30 13:05:44 -0500400 maybe_print_eds("ed_bulkhead", ohci_readl(&regs->ed_bulkhead));
401 maybe_print_eds("ed_bulkcurrent", ohci_readl(&regs->ed_bulkcurrent));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200402
Becky Bruce4abaacb2010-06-30 13:05:44 -0500403 maybe_print_eds("donehead", ohci_readl(&regs->donehead));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200404}
405
Remy Bohmer172a1132008-09-16 14:55:43 +0200406static void ohci_dump_roothub(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200407{
408 __u32 temp, ndp, i;
409
Remy Bohmer172a1132008-09-16 14:55:43 +0200410 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200411 ndp = (temp & RH_A_NDP);
412#ifdef CONFIG_AT91C_PQFP_UHPBUG
413 ndp = (ndp == 2) ? 1:0;
414#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200415 if (verbose) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200416 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200417 ((temp & RH_A_POTPGT) >> 24) & 0xff,
418 (temp & RH_A_NOCP) ? " NOCP" : "",
419 (temp & RH_A_OCPM) ? " OCPM" : "",
420 (temp & RH_A_DT) ? " DT" : "",
421 (temp & RH_A_NPS) ? " NPS" : "",
422 (temp & RH_A_PSM) ? " PSM" : "",
423 ndp
424 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200425 temp = roothub_b(controller);
426 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200427 temp,
428 (temp & RH_B_PPCM) >> 16,
429 (temp & RH_B_DR)
430 );
Remy Bohmer172a1132008-09-16 14:55:43 +0200431 temp = roothub_status(controller);
432 dbg("roothub.status: %08x%s%s%s%s%s%s",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200433 temp,
434 (temp & RH_HS_CRWE) ? " CRWE" : "",
435 (temp & RH_HS_OCIC) ? " OCIC" : "",
436 (temp & RH_HS_LPSC) ? " LPSC" : "",
437 (temp & RH_HS_DRWE) ? " DRWE" : "",
438 (temp & RH_HS_OCI) ? " OCI" : "",
439 (temp & RH_HS_LPS) ? " LPS" : ""
440 );
441 }
442
443 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200444 temp = roothub_portstatus(controller, i);
445 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 +0200446 i,
447 temp,
448 (temp & RH_PS_PRSC) ? " PRSC" : "",
449 (temp & RH_PS_OCIC) ? " OCIC" : "",
450 (temp & RH_PS_PSSC) ? " PSSC" : "",
451 (temp & RH_PS_PESC) ? " PESC" : "",
452 (temp & RH_PS_CSC) ? " CSC" : "",
453
454 (temp & RH_PS_LSDA) ? " LSDA" : "",
455 (temp & RH_PS_PPS) ? " PPS" : "",
456 (temp & RH_PS_PRS) ? " PRS" : "",
457 (temp & RH_PS_POCI) ? " POCI" : "",
458 (temp & RH_PS_PSS) ? " PSS" : "",
459
460 (temp & RH_PS_PES) ? " PES" : "",
461 (temp & RH_PS_CCS) ? " CCS" : ""
462 );
463 }
464}
465
Remy Bohmer172a1132008-09-16 14:55:43 +0200466static void ohci_dump(ohci_t *controller, int verbose)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200467{
Remy Bohmer172a1132008-09-16 14:55:43 +0200468 dbg("OHCI controller usb-%s state", controller->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200469
470 /* dumps some of the state we know about */
Remy Bohmer172a1132008-09-16 14:55:43 +0200471 ohci_dump_status(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200472 if (verbose)
Remy Bohmer172a1132008-09-16 14:55:43 +0200473 ep_print_int_eds(controller, "hcca");
Hans de Goede8e41f662015-05-05 23:56:13 +0200474 invalidate_dcache_hcca(controller->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200475 dbg("hcca frame #%04x", controller->hcca->frame_no);
476 ohci_dump_roothub(controller, 1);
Stefan Roese41e74eb2008-03-05 12:29:32 +0100477}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200478#endif /* DEBUG */
479
480/*-------------------------------------------------------------------------*
481 * Interface functions (URB)
482 *-------------------------------------------------------------------------*/
483
484/* get a transfer request */
485
Hans de Goede0c0e9602015-05-05 23:56:08 +0200486int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
487 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200488{
Remy Bohmer172a1132008-09-16 14:55:43 +0200489 ed_t *ed;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200490 urb_priv_t *purb_priv = urb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200491 int i, size = 0;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200492 struct usb_device *dev = urb->dev;
493 unsigned long pipe = urb->pipe;
494 void *buffer = urb->transfer_buffer;
495 int transfer_len = urb->transfer_buffer_length;
496 int interval = urb->interval;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200497
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200498 /* when controller's hung, permit only roothub cleanup attempts
499 * such as powering down ports */
500 if (ohci->disabled) {
501 err("sohci_submit_job: EPIPE");
502 return -1;
503 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +0200504
Remy Bohmer172a1132008-09-16 14:55:43 +0200505 /* we're about to begin a new transaction here so mark the
506 * URB unfinished */
Zhang Wei8d15efa2007-06-06 10:08:14 +0200507 urb->finished = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200508
509 /* every endpoint has a ed, locate and fill it */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200510 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
Remy Bohmer172a1132008-09-16 14:55:43 +0200511 if (!ed) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200512 err("sohci_submit_job: ENOMEM");
513 return -1;
514 }
515
516 /* for the private part of the URB we need the number of TDs (size) */
Remy Bohmer172a1132008-09-16 14:55:43 +0200517 switch (usb_pipetype(pipe)) {
518 case PIPE_BULK: /* one TD for every 4096 Byte */
519 size = (transfer_len - 1) / 4096 + 1;
520 break;
521 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
522 size = (transfer_len == 0)? 2:
523 (transfer_len - 1) / 4096 + 3;
524 break;
525 case PIPE_INTERRUPT: /* 1 TD */
526 size = 1;
527 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200528 }
529
Zhang Wei8d15efa2007-06-06 10:08:14 +0200530 ed->purb = urb;
531
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200532 if (size >= (N_URB_TD - 1)) {
533 err("need %d TDs, only have %d", size, N_URB_TD);
534 return -1;
535 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200536 purb_priv->pipe = pipe;
537
538 /* fill the private part of the URB */
539 purb_priv->length = size;
540 purb_priv->ed = ed;
541 purb_priv->actual_length = 0;
542
543 /* allocate the TDs */
544 /* note that td[0] was allocated in ep_add_ed */
545 for (i = 0; i < size; i++) {
Hans de Goedee5ef4212015-05-05 23:56:09 +0200546 purb_priv->td[i] = td_alloc(ohci_dev, dev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200547 if (!purb_priv->td[i]) {
548 purb_priv->length = i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200549 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200550 err("sohci_submit_job: ENOMEM");
551 return -1;
552 }
553 }
554
555 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200556 urb_free_priv(purb_priv);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200557 err("sohci_submit_job: EINVAL");
558 return -1;
559 }
560
561 /* link the ed into a chain if is not already */
562 if (ed->state != ED_OPER)
Remy Bohmer172a1132008-09-16 14:55:43 +0200563 ep_link(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200564
565 /* fill the TDs and link it to the ed */
Hans de Goede2a240e72015-05-05 23:56:07 +0200566 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200567 setup, purb_priv, interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200568
569 return 0;
570}
571
572/*-------------------------------------------------------------------------*/
573
574#ifdef DEBUG
575/* tell us the current USB frame number */
Hans de Goede2a240e72015-05-05 23:56:07 +0200576static int sohci_get_current_frame_number(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200577{
Hans de Goede8e41f662015-05-05 23:56:13 +0200578 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +0200579 return m16_swap(ohci->hcca->frame_no);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200580}
581#endif
582
583/*-------------------------------------------------------------------------*
584 * ED handling functions
585 *-------------------------------------------------------------------------*/
586
Zhang Wei8d15efa2007-06-06 10:08:14 +0200587/* search for the right branch to insert an interrupt ed into the int tree
588 * do some load ballancing;
589 * returns the branch and
590 * sets the interval to interval = 2^integer (ld (interval)) */
591
Remy Bohmer172a1132008-09-16 14:55:43 +0200592static int ep_int_ballance(ohci_t *ohci, int interval, int load)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200593{
594 int i, branch = 0;
595
596 /* search for the least loaded interrupt endpoint
597 * branch of all 32 branches
598 */
599 for (i = 0; i < 32; i++)
600 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
601 branch = i;
602
603 branch = branch % interval;
604 for (i = branch; i < 32; i += interval)
605 ohci->ohci_int_load [i] += load;
606
607 return branch;
608}
609
610/*-------------------------------------------------------------------------*/
611
612/* 2^int( ld (inter)) */
613
Remy Bohmer172a1132008-09-16 14:55:43 +0200614static int ep_2_n_interval(int inter)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200615{
616 int i;
Remy Bohmer172a1132008-09-16 14:55:43 +0200617 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200618 return 1 << i;
619}
620
621/*-------------------------------------------------------------------------*/
622
623/* the int tree is a binary tree
Remy Bohmer172a1132008-09-16 14:55:43 +0200624 * in order to process it sequentially the indexes of the branches have to
625 * be mapped the mapping reverses the bits of a word of num_bits length */
626static int ep_rev(int num_bits, int word)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200627{
628 int i, wout = 0;
629
630 for (i = 0; i < num_bits; i++)
631 wout |= (((word >> i) & 1) << (num_bits - i - 1));
632 return wout;
633}
634
635/*-------------------------------------------------------------------------*
636 * ED handling functions
637 *-------------------------------------------------------------------------*/
638
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200639/* link an ed into one of the HC chains */
640
Remy Bohmer172a1132008-09-16 14:55:43 +0200641static int ep_link(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200642{
643 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200644 int int_branch;
645 int i;
646 int inter;
647 int interval;
648 int load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200649 __u32 *ed_p;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200650
651 ed->state = ED_OPER;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200652 ed->int_interval = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200653
654 switch (ed->type) {
655 case PIPE_CONTROL:
656 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200657 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200658 if (ohci->ed_controltail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100659 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200660 else
661 ohci->ed_controltail->hwNextED =
662 m32_swap((unsigned long)ed);
663
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200664 ed->ed_prev = ohci->ed_controltail;
665 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
666 !ohci->ed_rm_list[1] && !ohci->sleeping) {
667 ohci->hc_control |= OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500668 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200669 }
670 ohci->ed_controltail = edi;
671 break;
672
673 case PIPE_BULK:
674 ed->hwNextED = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200675 flush_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +0200676 if (ohci->ed_bulktail == NULL)
Andre Przywarac114a982016-10-21 02:24:29 +0100677 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
Remy Bohmer172a1132008-09-16 14:55:43 +0200678 else
679 ohci->ed_bulktail->hwNextED =
680 m32_swap((unsigned long)ed);
681
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200682 ed->ed_prev = ohci->ed_bulktail;
683 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
684 !ohci->ed_rm_list[1] && !ohci->sleeping) {
685 ohci->hc_control |= OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500686 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200687 }
688 ohci->ed_bulktail = edi;
689 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200690
691 case PIPE_INTERRUPT:
692 load = ed->int_load;
Remy Bohmer172a1132008-09-16 14:55:43 +0200693 interval = ep_2_n_interval(ed->int_period);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200694 ed->int_interval = interval;
Remy Bohmer172a1132008-09-16 14:55:43 +0200695 int_branch = ep_int_ballance(ohci, interval, load);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200696 ed->int_branch = int_branch;
697
Remy Bohmer172a1132008-09-16 14:55:43 +0200698 for (i = 0; i < ep_rev(6, interval); i += inter) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200699 inter = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +0200700 for (ed_p = &(ohci->hcca->int_table[\
701 ep_rev(5, i) + int_branch]);
702 (*ed_p != 0) &&
703 (((ed_t *)ed_p)->int_interval >= interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200704 ed_p = &(((ed_t *)ed_p)->hwNextED))
Remy Bohmer172a1132008-09-16 14:55:43 +0200705 inter = ep_rev(6,
706 ((ed_t *)ed_p)->int_interval);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200707 ed->hwNextED = *ed_p;
Hans de Goede8e41f662015-05-05 23:56:13 +0200708 flush_dcache_ed(ed);
Martin Krause8a588192007-08-21 12:40:34 +0200709 *ed_p = m32_swap((unsigned long)ed);
Hans de Goede8e41f662015-05-05 23:56:13 +0200710 flush_dcache_hcca(ohci->hcca);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200711 }
712 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200713 }
714 return 0;
715}
716
717/*-------------------------------------------------------------------------*/
718
Zhang Wei8d15efa2007-06-06 10:08:14 +0200719/* scan the periodic table to find and unlink this ED */
Remy Bohmer172a1132008-09-16 14:55:43 +0200720static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
721 unsigned index, unsigned period)
Zhang Wei8d15efa2007-06-06 10:08:14 +0200722{
Hans de Goede8e41f662015-05-05 23:56:13 +0200723 __maybe_unused unsigned long aligned_ed_p;
724
Zhang Wei8d15efa2007-06-06 10:08:14 +0200725 for (; index < NUM_INTS; index += period) {
726 __u32 *ed_p = &ohci->hcca->int_table [index];
727
728 /* ED might have been unlinked through another path */
729 while (*ed_p != 0) {
Andre Przywarac114a982016-10-21 02:24:29 +0100730 if (((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200731 m32_swap((unsigned long)ed_p)) == ed) {
Zhang Wei8d15efa2007-06-06 10:08:14 +0200732 *ed_p = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200733 aligned_ed_p = (unsigned long)ed_p;
734 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
735 flush_dcache_range(aligned_ed_p,
736 aligned_ed_p + ARCH_DMA_MINALIGN);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200737 break;
738 }
Andre Przywarac114a982016-10-21 02:24:29 +0100739 ed_p = &(((struct ed *)(uintptr_t)
Remy Bohmer172a1132008-09-16 14:55:43 +0200740 m32_swap((unsigned long)ed_p))->hwNextED);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200741 }
742 }
743}
744
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200745/* unlink an ed from one of the HC chains.
746 * just the link to the ed is unlinked.
747 * the link from the ed still points to another operational ed or 0
748 * so the HC can eventually finish the processing of the unlinked ed */
749
Remy Bohmer172a1132008-09-16 14:55:43 +0200750static int ep_unlink(ohci_t *ohci, ed_t *edi)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200751{
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100752 volatile ed_t *ed = edi;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200753 int i;
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +0100754
Remy Bohmer172a1132008-09-16 14:55:43 +0200755 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
Hans de Goede8e41f662015-05-05 23:56:13 +0200756 flush_dcache_ed(ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200757
758 switch (ed->type) {
759 case PIPE_CONTROL:
760 if (ed->ed_prev == NULL) {
761 if (!ed->hwNextED) {
762 ohci->hc_control &= ~OHCI_CTRL_CLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500763 ohci_writel(ohci->hc_control,
764 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200765 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500766 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200767 &ohci->regs->ed_controlhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200768 } else {
769 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200770 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200771 }
772 if (ohci->ed_controltail == ed) {
773 ohci->ed_controltail = ed->ed_prev;
774 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100775 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200776 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200777 }
778 break;
779
780 case PIPE_BULK:
781 if (ed->ed_prev == NULL) {
782 if (!ed->hwNextED) {
783 ohci->hc_control &= ~OHCI_CTRL_BLE;
Becky Bruce4abaacb2010-06-30 13:05:44 -0500784 ohci_writel(ohci->hc_control,
785 &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200786 }
Becky Bruce4abaacb2010-06-30 13:05:44 -0500787 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
Remy Bohmer172a1132008-09-16 14:55:43 +0200788 &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200789 } else {
790 ed->ed_prev->hwNextED = ed->hwNextED;
Hans de Goede8e41f662015-05-05 23:56:13 +0200791 flush_dcache_ed(ed->ed_prev);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200792 }
793 if (ohci->ed_bulktail == ed) {
794 ohci->ed_bulktail = ed->ed_prev;
795 } else {
Andre Przywarac114a982016-10-21 02:24:29 +0100796 ((ed_t *)(uintptr_t)m32_swap(
Remy Bohmer172a1132008-09-16 14:55:43 +0200797 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200798 }
799 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +0200800
801 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +0200802 periodic_unlink(ohci, ed, 0, 1);
Zhang Wei8d15efa2007-06-06 10:08:14 +0200803 for (i = ed->int_branch; i < 32; i += ed->int_interval)
804 ohci->ohci_int_load[i] -= ed->int_load;
805 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200806 }
807 ed->state = ED_UNLINK;
808 return 0;
809}
810
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200811/*-------------------------------------------------------------------------*/
812
Markus Klotzbuecheraa219212006-05-30 16:56:14 +0200813/* add/reinit an endpoint; this should be done once at the
814 * usb_set_configuration command, but the USB stack is a little bit
815 * stateless so we do it at every transaction if the state of the ed
816 * is ED_NEW then a dummy td is added and the state is changed to
817 * ED_UNLINK in all other cases the state is left unchanged the ed
818 * info fields are setted anyway even though most of them should not
819 * change
820 */
Hans de Goede0c0e9602015-05-05 23:56:08 +0200821static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
822 unsigned long pipe, int interval, int load)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200823{
824 td_t *td;
825 ed_t *ed_ret;
826 volatile ed_t *ed;
827
Hans de Goede0c0e9602015-05-05 23:56:08 +0200828 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
Remy Bohmer172a1132008-09-16 14:55:43 +0200829 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200830
831 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
832 err("ep_add_ed: pending delete");
833 /* pending delete request */
834 return NULL;
835 }
836
837 if (ed->state == ED_NEW) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200838 /* dummy td; end of td list for ed */
Hans de Goedee5ef4212015-05-05 23:56:09 +0200839 td = td_alloc(ohci_dev, usb_dev);
Remy Bohmer172a1132008-09-16 14:55:43 +0200840 ed->hwTailP = m32_swap((unsigned long)td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200841 ed->hwHeadP = ed->hwTailP;
842 ed->state = ED_UNLINK;
Remy Bohmer172a1132008-09-16 14:55:43 +0200843 ed->type = usb_pipetype(pipe);
Hans de Goede0c0e9602015-05-05 23:56:08 +0200844 ohci_dev->ed_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200845 }
846
Remy Bohmer172a1132008-09-16 14:55:43 +0200847 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
848 | usb_pipeendpoint(pipe) << 7
849 | (usb_pipeisoc(pipe)? 0x8000: 0)
850 | (usb_pipecontrol(pipe)? 0: \
851 (usb_pipeout(pipe)? 0x800: 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000852 | (usb_dev->speed == USB_SPEED_LOW) << 13
Remy Bohmer172a1132008-09-16 14:55:43 +0200853 | usb_maxpacket(usb_dev, pipe) << 16);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200854
Zhang Wei8d15efa2007-06-06 10:08:14 +0200855 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
856 ed->int_period = interval;
857 ed->int_load = load;
858 }
859
Hans de Goede8e41f662015-05-05 23:56:13 +0200860 flush_dcache_ed(ed);
861
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200862 return ed_ret;
863}
864
865/*-------------------------------------------------------------------------*
866 * TD handling functions
867 *-------------------------------------------------------------------------*/
868
869/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
870
Remy Bohmer172a1132008-09-16 14:55:43 +0200871static void td_fill(ohci_t *ohci, unsigned int info,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200872 void *data, int len,
873 struct usb_device *dev, int index, urb_priv_t *urb_priv)
874{
875 volatile td_t *td, *td_pt;
876#ifdef OHCI_FILL_TRACE
877 int i;
878#endif
879
880 if (index > urb_priv->length) {
881 err("index > length");
882 return;
883 }
884 /* use this td as the next dummy */
885 td_pt = urb_priv->td [index];
886 td_pt->hwNextTD = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200887 flush_dcache_td(td_pt);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200888
889 /* fill the old dummy TD */
Remy Bohmer172a1132008-09-16 14:55:43 +0200890 td = urb_priv->td [index] =
Andre Przywarac114a982016-10-21 02:24:29 +0100891 (td_t *)(uintptr_t)
892 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200893
894 td->ed = urb_priv->ed;
895 td->next_dl_td = NULL;
896 td->index = index;
Andre Przywarac114a982016-10-21 02:24:29 +0100897 td->data = (uintptr_t)data;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200898#ifdef OHCI_FILL_TRACE
Remy Bohmer47e72082008-10-10 10:23:21 +0200899 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200900 for (i = 0; i < len; i++)
Remy Bohmer172a1132008-09-16 14:55:43 +0200901 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200902 printf("\n");
903 }
904#endif
905 if (!len)
906 data = 0;
907
Remy Bohmer172a1132008-09-16 14:55:43 +0200908 td->hwINFO = m32_swap(info);
909 td->hwCBP = m32_swap((unsigned long)data);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200910 if (data)
Remy Bohmer172a1132008-09-16 14:55:43 +0200911 td->hwBE = m32_swap((unsigned long)(data + len - 1));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200912 else
913 td->hwBE = 0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200914
915 td->hwNextTD = m32_swap((unsigned long)td_pt);
Hans de Goede8e41f662015-05-05 23:56:13 +0200916 flush_dcache_td(td);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200917
918 /* append to queue */
919 td->ed->hwTailP = td->hwNextTD;
Hans de Goede8e41f662015-05-05 23:56:13 +0200920 flush_dcache_ed(td->ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200921}
922
923/*-------------------------------------------------------------------------*/
924
925/* prepare all TDs of a transfer */
926
Hans de Goede2a240e72015-05-05 23:56:07 +0200927static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
928 unsigned long pipe, void *buffer, int transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +0200929 struct devrequest *setup, urb_priv_t *urb,
930 int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200931{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200932 int data_len = transfer_len;
933 void *data;
934 int cnt = 0;
935 __u32 info = 0;
936 unsigned int toggle = 0;
937
Hans de Goede8e41f662015-05-05 23:56:13 +0200938 flush_dcache_buffer(buffer, data_len);
939
Remy Bohmer172a1132008-09-16 14:55:43 +0200940 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700941 * bits for resetting */
Remy Bohmer172a1132008-09-16 14:55:43 +0200942 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200943 toggle = TD_T_TOGGLE;
944 } else {
945 toggle = TD_T_DATA0;
Remy Bohmer172a1132008-09-16 14:55:43 +0200946 usb_settoggle(dev, usb_pipeendpoint(pipe),
947 usb_pipeout(pipe), 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200948 }
949 urb->td_cnt = 0;
950 if (data_len)
951 data = buffer;
952 else
953 data = 0;
954
Remy Bohmer172a1132008-09-16 14:55:43 +0200955 switch (usb_pipetype(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200956 case PIPE_BULK:
Remy Bohmer172a1132008-09-16 14:55:43 +0200957 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200958 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200959 while (data_len > 4096) {
960 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
961 data, 4096, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200962 data += 4096; data_len -= 4096; cnt++;
963 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200964 info = usb_pipeout(pipe)?
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200965 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
Remy Bohmer172a1132008-09-16 14:55:43 +0200966 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
967 data_len, dev, cnt, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200968 cnt++;
969
Remy Bohmer172a1132008-09-16 14:55:43 +0200970 if (!ohci->sleeping) {
971 /* start bulk list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500972 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +0200973 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200974 break;
975
976 case PIPE_CONTROL:
Remy Bohmer172a1132008-09-16 14:55:43 +0200977 /* Setup phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200978 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
Hans de Goede8e41f662015-05-05 23:56:13 +0200979 flush_dcache_buffer(setup, 8);
Remy Bohmer172a1132008-09-16 14:55:43 +0200980 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
981
982 /* Optional Data phase */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200983 if (data_len > 0) {
Remy Bohmer172a1132008-09-16 14:55:43 +0200984 info = usb_pipeout(pipe)?
985 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
986 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200987 /* NOTE: mishandles transfers >8K, some >4K */
Remy Bohmer172a1132008-09-16 14:55:43 +0200988 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +0200989 }
Remy Bohmer172a1132008-09-16 14:55:43 +0200990
991 /* Status phase */
Hans de Goede60f4bd02015-05-05 23:56:12 +0200992 info = (usb_pipeout(pipe) || data_len == 0) ?
Remy Bohmer172a1132008-09-16 14:55:43 +0200993 TD_CC | TD_DP_IN | TD_T_DATA1:
994 TD_CC | TD_DP_OUT | TD_T_DATA1;
995 td_fill(ohci, info, data, 0, dev, cnt++, urb);
996
997 if (!ohci->sleeping) {
998 /* start Control list */
Becky Bruce4abaacb2010-06-30 13:05:44 -0500999 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001000 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001001 break;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001002
1003 case PIPE_INTERRUPT:
Remy Bohmer172a1132008-09-16 14:55:43 +02001004 info = usb_pipeout(urb->pipe)?
Zhang Wei8d15efa2007-06-06 10:08:14 +02001005 TD_CC | TD_DP_OUT | toggle:
1006 TD_CC | TD_R | TD_DP_IN | toggle;
Remy Bohmer172a1132008-09-16 14:55:43 +02001007 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001008 break;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001009 }
1010 if (urb->length != cnt)
1011 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1012}
1013
1014/*-------------------------------------------------------------------------*
1015 * Done List handling functions
1016 *-------------------------------------------------------------------------*/
1017
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001018/* calculate the transfer length and update the urb */
1019
Remy Bohmer172a1132008-09-16 14:55:43 +02001020static void dl_transfer_length(td_t *td)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001021{
Wolfgang Denk0ef5f622011-10-05 23:03:43 +02001022 __u32 tdBE, tdCBP;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001023 urb_priv_t *lurb_priv = td->ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001024
Remy Bohmer172a1132008-09-16 14:55:43 +02001025 tdBE = m32_swap(td->hwBE);
1026 tdCBP = m32_swap(td->hwCBP);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001027
Remy Bohmer47e72082008-10-10 10:23:21 +02001028 if (!(usb_pipecontrol(lurb_priv->pipe) &&
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001029 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1030 if (tdBE != 0) {
1031 if (td->hwCBP == 0)
1032 lurb_priv->actual_length += tdBE - td->data + 1;
1033 else
1034 lurb_priv->actual_length += tdCBP - td->data;
1035 }
1036 }
1037}
1038
1039/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001040static void check_status(td_t *td_list)
1041{
1042 urb_priv_t *lurb_priv = td_list->ed->purb;
1043 int urb_len = lurb_priv->length;
1044 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1045 int cc;
1046
1047 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1048 if (cc) {
1049 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1050
Hans de Goede8e41f662015-05-05 23:56:13 +02001051 invalidate_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001052 if (*phwHeadP & m32_swap(0x1)) {
1053 if (lurb_priv &&
1054 ((td_list->index + 1) < urb_len)) {
1055 *phwHeadP =
1056 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1057 m32_swap(0xfffffff0)) |
1058 (*phwHeadP & m32_swap(0x2));
1059
1060 lurb_priv->td_cnt += urb_len -
1061 td_list->index - 1;
1062 } else
1063 *phwHeadP &= m32_swap(0xfffffff2);
Hans de Goede8e41f662015-05-05 23:56:13 +02001064 flush_dcache_ed(td_list->ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001065 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001066 }
1067}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001068
1069/* replies to the request have to be on a FIFO basis so
1070 * we reverse the reversed done-list */
Remy Bohmer172a1132008-09-16 14:55:43 +02001071static td_t *dl_reverse_done_list(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001072{
Andre Przywarac114a982016-10-21 02:24:29 +01001073 uintptr_t td_list_hc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001074 td_t *td_rev = NULL;
1075 td_t *td_list = NULL;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001076
Hans de Goede8e41f662015-05-05 23:56:13 +02001077 invalidate_dcache_hcca(ohci->hcca);
Remy Bohmer172a1132008-09-16 14:55:43 +02001078 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001079 ohci->hcca->done_head = 0;
Hans de Goede8e41f662015-05-05 23:56:13 +02001080 flush_dcache_hcca(ohci->hcca);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001081
1082 while (td_list_hc) {
1083 td_list = (td_t *)td_list_hc;
Hans de Goede8e41f662015-05-05 23:56:13 +02001084 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001085 check_status(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001086 td_list->next_dl_td = td_rev;
1087 td_rev = td_list;
Remy Bohmer172a1132008-09-16 14:55:43 +02001088 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001089 }
1090 return td_list;
1091}
1092
1093/*-------------------------------------------------------------------------*/
Remy Bohmer172a1132008-09-16 14:55:43 +02001094/*-------------------------------------------------------------------------*/
1095
1096static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1097{
1098 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
Hans de Goede27873372015-05-10 14:10:22 +02001099 urb->finished = 1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001100 else
1101 dbg("finish_urb: strange.., ED state %x, \n", status);
1102}
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001103
Remy Bohmer172a1132008-09-16 14:55:43 +02001104/*
1105 * Used to take back a TD from the host controller. This would normally be
1106 * called from within dl_done_list, however it may be called directly if the
1107 * HC no longer sees the TD and it has not appeared on the donelist (after
1108 * two frames). This bug has been observed on ZF Micro systems.
1109 */
1110static int takeback_td(ohci_t *ohci, td_t *td_list)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001111{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001112 ed_t *ed;
Remy Bohmer172a1132008-09-16 14:55:43 +02001113 int cc;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001114 int stat = 0;
1115 /* urb_t *urb; */
1116 urb_priv_t *lurb_priv;
1117 __u32 tdINFO, edHeadP, edTailP;
1118
Hans de Goede8e41f662015-05-05 23:56:13 +02001119 invalidate_dcache_td(td_list);
Remy Bohmer172a1132008-09-16 14:55:43 +02001120 tdINFO = m32_swap(td_list->hwINFO);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001121
Remy Bohmer172a1132008-09-16 14:55:43 +02001122 ed = td_list->ed;
1123 lurb_priv = ed->purb;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001124
Remy Bohmer172a1132008-09-16 14:55:43 +02001125 dl_transfer_length(td_list);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001126
Remy Bohmer172a1132008-09-16 14:55:43 +02001127 lurb_priv->td_cnt++;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001128
Remy Bohmer172a1132008-09-16 14:55:43 +02001129 /* error code of transfer */
1130 cc = TD_CC_GET(tdINFO);
1131 if (cc) {
1132 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1133 stat = cc_to_error[cc];
1134 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001135
Remy Bohmer172a1132008-09-16 14:55:43 +02001136 /* see if this done list makes for all TD's of current URB,
1137 * and mark the URB finished if so */
1138 if (lurb_priv->td_cnt == lurb_priv->length)
1139 finish_urb(ohci, lurb_priv, ed->state);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001140
Remy Bohmer172a1132008-09-16 14:55:43 +02001141 dbg("dl_done_list: processing TD %x, len %x\n",
1142 lurb_priv->td_cnt, lurb_priv->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001143
Remy Bohmer47e72082008-10-10 10:23:21 +02001144 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
Hans de Goede8e41f662015-05-05 23:56:13 +02001145 invalidate_dcache_ed(ed);
Remy Bohmer172a1132008-09-16 14:55:43 +02001146 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1147 edTailP = m32_swap(ed->hwTailP);
1148
1149 /* unlink eds if they are not busy */
1150 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1151 ep_unlink(ohci, ed);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001152 }
1153 return stat;
1154}
1155
Remy Bohmer172a1132008-09-16 14:55:43 +02001156static int dl_done_list(ohci_t *ohci)
1157{
1158 int stat = 0;
1159 td_t *td_list = dl_reverse_done_list(ohci);
1160
1161 while (td_list) {
1162 td_t *td_next = td_list->next_dl_td;
1163 stat = takeback_td(ohci, td_list);
1164 td_list = td_next;
1165 }
1166 return stat;
1167}
1168
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001169/*-------------------------------------------------------------------------*
1170 * Virtual Root Hub
1171 *-------------------------------------------------------------------------*/
1172
Stephen Warren39c89682014-02-13 21:15:18 -07001173#include <usbroothubdes.h>
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001174
1175/* Hub class-specific descriptor is constructed dynamically */
1176
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001177/*-------------------------------------------------------------------------*/
1178
1179#define OK(x) len = (x); break
1180#ifdef DEBUG
Becky Bruce4abaacb2010-06-30 13:05:44 -05001181#define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001182 &ohci->regs->roothub.status); }
Remy Bohmer172a1132008-09-16 14:55:43 +02001183#define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
Hans de Goede2a240e72015-05-05 23:56:07 +02001184 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001185#else
Hans de Goede2a240e72015-05-05 23:56:07 +02001186#define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001187#define WR_RH_PORTSTAT(x) ohci_writel((x), \
Hans de Goede2a240e72015-05-05 23:56:07 +02001188 &ohci->regs->roothub.portstatus[wIndex-1])
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001189#endif
Hans de Goede2a240e72015-05-05 23:56:07 +02001190#define RD_RH_STAT roothub_status(ohci)
1191#define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001192
1193/* request to virtual root hub */
1194
1195int rh_check_port_status(ohci_t *controller)
1196{
1197 __u32 temp, ndp, i;
1198 int res;
1199
1200 res = -1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001201 temp = roothub_a(controller);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001202 ndp = (temp & RH_A_NDP);
1203#ifdef CONFIG_AT91C_PQFP_UHPBUG
1204 ndp = (ndp == 2) ? 1:0;
1205#endif
1206 for (i = 0; i < ndp; i++) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001207 temp = roothub_portstatus(controller, i);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001208 /* check for a device disconnect */
1209 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1210 (RH_PS_PESC | RH_PS_CSC)) &&
1211 ((temp & RH_PS_CCS) == 0)) {
1212 res = i;
1213 break;
1214 }
1215 }
1216 return res;
1217}
1218
Hans de Goede2a240e72015-05-05 23:56:07 +02001219static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1220 unsigned long pipe, void *buffer, int transfer_len,
1221 struct devrequest *cmd)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001222{
Remy Bohmer172a1132008-09-16 14:55:43 +02001223 void *data = buffer;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001224 int leni = transfer_len;
1225 int len = 0;
1226 int stat = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001227 __u16 bmRType_bReq;
1228 __u16 wValue;
1229 __u16 wIndex;
1230 __u16 wLength;
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001231 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
Marek Vasutfc5cf882011-10-07 02:00:13 +02001232
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001233#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001234pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001235 cmd, "SUB(rh)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001236#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001237 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001238#endif
Remy Bohmer47e72082008-10-10 10:23:21 +02001239 if (usb_pipeint(pipe)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001240 info("Root-Hub submit IRQ: NOT implemented");
1241 return 0;
1242 }
1243
1244 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
Remy Bohmer172a1132008-09-16 14:55:43 +02001245 wValue = le16_to_cpu(cmd->value);
1246 wIndex = le16_to_cpu(cmd->index);
1247 wLength = le16_to_cpu(cmd->length);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001248
1249 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1250 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1251
1252 switch (bmRType_bReq) {
1253 /* Request Destination:
1254 without flags: Device,
1255 RH_INTERFACE: interface,
1256 RH_ENDPOINT: endpoint,
1257 RH_CLASS means HUB here,
1258 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1259 */
1260
1261 case RH_GET_STATUS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001262 *(u16 *)databuf = cpu_to_le16(1);
Remy Bohmer172a1132008-09-16 14:55:43 +02001263 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001264 case RH_GET_STATUS | RH_INTERFACE:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001265 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001266 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001267 case RH_GET_STATUS | RH_ENDPOINT:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001268 *(u16 *)databuf = cpu_to_le16(0);
Remy Bohmer172a1132008-09-16 14:55:43 +02001269 OK(2);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001270 case RH_GET_STATUS | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001271 *(u32 *)databuf = cpu_to_le32(
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001272 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
Remy Bohmer172a1132008-09-16 14:55:43 +02001273 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001274 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001275 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
Remy Bohmer172a1132008-09-16 14:55:43 +02001276 OK(4);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001277
1278 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1279 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001280 case (RH_ENDPOINT_STALL):
1281 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001282 }
1283 break;
1284
1285 case RH_CLEAR_FEATURE | RH_CLASS:
1286 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001287 case RH_C_HUB_LOCAL_POWER:
1288 OK(0);
1289 case (RH_C_HUB_OVER_CURRENT):
1290 WR_RH_STAT(RH_HS_OCIC);
1291 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001292 }
1293 break;
1294
1295 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1296 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001297 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1298 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1299 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1300 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1301 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1302 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1303 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1304 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001305 }
1306 break;
1307
1308 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1309 switch (wValue) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001310 case (RH_PORT_SUSPEND):
1311 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1312 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1313 if (RD_RH_PORTSTAT & RH_PS_CCS)
1314 WR_RH_PORTSTAT(RH_PS_PRS);
1315 OK(0);
1316 case (RH_PORT_POWER):
1317 WR_RH_PORTSTAT(RH_PS_PPS);
Remy Bohmer172a1132008-09-16 14:55:43 +02001318 OK(0);
1319 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1320 if (RD_RH_PORTSTAT & RH_PS_CCS)
1321 WR_RH_PORTSTAT(RH_PS_PES);
1322 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001323 }
1324 break;
1325
Remy Bohmer172a1132008-09-16 14:55:43 +02001326 case RH_SET_ADDRESS:
Hans de Goede2a240e72015-05-05 23:56:07 +02001327 ohci->rh.devnum = wValue;
Remy Bohmer172a1132008-09-16 14:55:43 +02001328 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001329
1330 case RH_GET_DESCRIPTOR:
1331 switch ((wValue & 0xff00) >> 8) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001332 case (0x01): /* device descriptor */
1333 len = min_t(unsigned int,
1334 leni,
1335 min_t(unsigned int,
1336 sizeof(root_hub_dev_des),
1337 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001338 databuf = root_hub_dev_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001339 case (0x02): /* configuration descriptor */
1340 len = min_t(unsigned int,
1341 leni,
1342 min_t(unsigned int,
1343 sizeof(root_hub_config_des),
1344 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001345 databuf = root_hub_config_des; OK(len);
Remy Bohmer172a1132008-09-16 14:55:43 +02001346 case (0x03): /* string descriptors */
1347 if (wValue == 0x0300) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001348 len = min_t(unsigned int,
Remy Bohmer172a1132008-09-16 14:55:43 +02001349 leni,
1350 min_t(unsigned int,
1351 sizeof(root_hub_str_index0),
1352 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001353 databuf = root_hub_str_index0;
Remy Bohmer172a1132008-09-16 14:55:43 +02001354 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001355 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001356 if (wValue == 0x0301) {
1357 len = min_t(unsigned int,
1358 leni,
1359 min_t(unsigned int,
1360 sizeof(root_hub_str_index1),
1361 wLength));
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001362 databuf = root_hub_str_index1;
Remy Bohmer172a1132008-09-16 14:55:43 +02001363 OK(len);
1364 }
1365 default:
1366 stat = USB_ST_STALLED;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001367 }
1368 break;
1369
1370 case RH_GET_DESCRIPTOR | RH_CLASS:
1371 {
Hans de Goede2a240e72015-05-05 23:56:07 +02001372 __u32 temp = roothub_a(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001373
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001374 databuf[0] = 9; /* min length; */
1375 databuf[1] = 0x29;
1376 databuf[2] = temp & RH_A_NDP;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001377#ifdef CONFIG_AT91C_PQFP_UHPBUG
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001378 databuf[2] = (databuf[2] == 2) ? 1 : 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001379#endif
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001380 databuf[3] = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001381 if (temp & RH_A_PSM) /* per-port power switching? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001382 databuf[3] |= 0x1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001383 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001384 databuf[3] |= 0x10;
Remy Bohmer172a1132008-09-16 14:55:43 +02001385 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001386 databuf[3] |= 0x8;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001387
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001388 databuf[4] = 0;
1389 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1390 databuf[6] = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001391 temp = roothub_b(ohci);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001392 databuf[7] = temp & RH_B_DR;
1393 if (databuf[2] < 7) {
1394 databuf[8] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001395 } else {
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001396 databuf[0] += 2;
1397 databuf[8] = (temp & RH_B_DR) >> 8;
1398 databuf[10] = databuf[9] = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001399 }
1400
1401 len = min_t(unsigned int, leni,
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001402 min_t(unsigned int, databuf[0], wLength));
Remy Bohmer172a1132008-09-16 14:55:43 +02001403 OK(len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001404 }
1405
Marek Vasutfc5cf882011-10-07 02:00:13 +02001406 case RH_GET_CONFIGURATION:
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001407 databuf[0] = 0x01;
Marek Vasutfc5cf882011-10-07 02:00:13 +02001408 OK(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001409
Marek Vasutfc5cf882011-10-07 02:00:13 +02001410 case RH_SET_CONFIGURATION:
1411 WR_RH_STAT(0x10000);
1412 OK(0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001413
1414 default:
Remy Bohmer172a1132008-09-16 14:55:43 +02001415 dbg("unsupported root hub command");
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001416 stat = USB_ST_STALLED;
1417 }
1418
1419#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001420 ohci_dump_roothub(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001421#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001422 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001423#endif
1424
1425 len = min_t(int, len, leni);
Troy Kisky62c1c7c2012-08-11 14:49:09 -07001426 if (data != databuf)
1427 memcpy(data, databuf, len);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001428 dev->act_len = len;
1429 dev->status = stat;
1430
1431#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001432 pkt_print(ohci, NULL, dev, pipe, buffer,
Remy Bohmer172a1132008-09-16 14:55:43 +02001433 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001434#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001435 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001436#endif
1437
1438 return stat;
1439}
1440
1441/*-------------------------------------------------------------------------*/
1442
Hans de Goeded7a39422015-05-13 14:42:15 +02001443static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1444{
1445 int i;
1446
1447 if (!intr)
1448 return &ohci->ohci_dev;
1449
1450 /* First see if we already have an ohci_dev for this dev. */
1451 for (i = 0; i < NUM_INT_DEVS; i++) {
1452 if (ohci->int_dev[i].devnum == devnum)
1453 return &ohci->int_dev[i];
1454 }
1455
1456 /* If not then find a free one. */
1457 for (i = 0; i < NUM_INT_DEVS; i++) {
1458 if (ohci->int_dev[i].devnum == -1) {
1459 ohci->int_dev[i].devnum = devnum;
1460 return &ohci->int_dev[i];
1461 }
1462 }
1463
1464 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1465 return NULL;
1466}
1467
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001468/* common code for handling submit messages - used for all but root hub */
1469/* accesses. */
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001470static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1471 void *buffer, int transfer_len, int interval)
1472{
1473 urb_priv_t *urb;
1474
1475 urb = calloc(1, sizeof(urb_priv_t));
1476 if (!urb) {
1477 printf("ohci: Error out of memory allocating urb\n");
1478 return NULL;
1479 }
1480
1481 urb->dev = dev;
1482 urb->pipe = pipe;
1483 urb->transfer_buffer = buffer;
1484 urb->transfer_buffer_length = transfer_len;
1485 urb->interval = interval;
1486
1487 return urb;
1488}
1489
Hans de Goede2a240e72015-05-05 23:56:07 +02001490static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1491 unsigned long pipe, void *buffer, int transfer_len,
1492 struct devrequest *setup, int interval)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001493{
1494 int stat = 0;
1495 int maxsize = usb_maxpacket(dev, pipe);
1496 int timeout;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001497 urb_priv_t *urb;
Hans de Goeded7a39422015-05-13 14:42:15 +02001498 ohci_dev_t *ohci_dev;
Zhang Wei8d15efa2007-06-06 10:08:14 +02001499
Hans de Goede66b1d6d2015-05-13 14:42:16 +02001500 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1501 if (!urb)
1502 return -ENOMEM;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001503
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001504#ifdef DEBUG
Zhang Wei8d15efa2007-06-06 10:08:14 +02001505 urb->actual_length = 0;
Hans de Goede2a240e72015-05-05 23:56:07 +02001506 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001507 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001508#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001509 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001510#endif
1511 if (!maxsize) {
1512 err("submit_common_message: pipesize for pipe %lx is zero",
1513 pipe);
1514 return -1;
1515 }
1516
Hans de Goeded7a39422015-05-13 14:42:15 +02001517 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1518 if (!ohci_dev)
1519 return -ENOMEM;
1520
1521 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001522 err("sohci_submit_job failed");
1523 return -1;
1524 }
1525
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001526 mdelay(10);
Hans de Goede2a240e72015-05-05 23:56:07 +02001527 /* ohci_dump_status(ohci); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001528
Simon Glassfd7f5132011-02-07 14:42:16 -08001529 timeout = USB_TIMEOUT_MS(pipe);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001530
1531 /* wait for it to complete */
1532 for (;;) {
1533 /* check whether the controller is done */
Hans de Goede2a240e72015-05-05 23:56:07 +02001534 stat = hc_interrupt(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001535 if (stat < 0) {
1536 stat = USB_ST_CRC_ERR;
1537 break;
1538 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001539
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001540 /* NOTE: since we are not interrupt driven in U-Boot and always
1541 * handle only one URB at a time, we cannot assume the
1542 * transaction finished on the first successful return from
1543 * hc_interrupt().. unless the flag for current URB is set,
1544 * meaning that all TD's to/from device got actually
1545 * transferred and processed. If the current URB is not
1546 * finished we need to re-iterate this loop so as
1547 * hc_interrupt() gets called again as there needs to be some
1548 * more TD's to process still */
Zhang Wei8d15efa2007-06-06 10:08:14 +02001549 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001550 /* 0xff is returned for an SF-interrupt */
1551 break;
1552 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001553
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001554 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001555 mdelay(1);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001556 if (!urb->finished)
Remy Bohmer172a1132008-09-16 14:55:43 +02001557 dbg("*");
Zhang Wei8d15efa2007-06-06 10:08:14 +02001558
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001559 } else {
Hans de Goeded98dc992015-05-05 23:56:14 +02001560 if (!usb_pipeint(pipe))
1561 err("CTL:TIMEOUT ");
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001562 dbg("submit_common_msg: TO status %x\n", stat);
Zhang Wei8d15efa2007-06-06 10:08:14 +02001563 urb->finished = 1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001564 stat = USB_ST_CRC_ERR;
1565 break;
1566 }
1567 }
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001568
1569 dev->status = stat;
Mateusz Kulikowski10b479d2013-10-23 20:26:27 +02001570 dev->act_len = urb->actual_length;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001571
Hans de Goede8e41f662015-05-05 23:56:13 +02001572 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1573 invalidate_dcache_buffer(buffer, dev->act_len);
1574
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001575#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001576 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001577 setup, "RET(ctlr)", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001578#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001579 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001580#endif
Hans de Goede27873372015-05-10 14:10:22 +02001581 urb_free_priv(urb);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001582 return 0;
1583}
1584
Hans de Goede5477e6f2015-05-13 14:42:17 +02001585#define MAX_INT_QUEUESIZE 8
1586
1587struct int_queue {
1588 int queuesize;
1589 int curr_urb;
1590 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1591};
1592
1593static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1594 struct usb_device *udev, unsigned long pipe, int queuesize,
1595 int elementsize, void *buffer, int interval)
1596{
1597 struct int_queue *queue;
1598 ohci_dev_t *ohci_dev;
1599 int i;
1600
1601 if (queuesize > MAX_INT_QUEUESIZE)
1602 return NULL;
1603
1604 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1605 if (!ohci_dev)
1606 return NULL;
1607
1608 queue = malloc(sizeof(*queue));
1609 if (!queue) {
1610 printf("ohci: Error out of memory allocating int queue\n");
1611 return NULL;
1612 }
1613
1614 for (i = 0; i < queuesize; i++) {
1615 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1616 buffer + i * elementsize,
1617 elementsize, interval);
1618 if (!queue->urb[i])
1619 break;
1620
1621 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1622 printf("ohci: Error submitting int queue job\n");
1623 urb_free_priv(queue->urb[i]);
1624 break;
1625 }
1626 }
1627 if (i == 0) {
1628 /* We did not succeed in submitting even 1 urb */
1629 free(queue);
1630 return NULL;
1631 }
1632
1633 queue->queuesize = i;
1634 queue->curr_urb = 0;
1635
1636 return queue;
1637}
1638
1639static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1640 struct int_queue *queue)
1641{
1642 if (queue->curr_urb == queue->queuesize)
1643 return NULL; /* Queue depleted */
1644
1645 if (hc_interrupt(ohci) < 0)
1646 return NULL;
1647
1648 if (queue->urb[queue->curr_urb]->finished) {
1649 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1650 queue->curr_urb++;
1651 return ret;
1652 }
1653
1654 return NULL;
1655}
1656
1657static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1658 struct int_queue *queue)
1659{
1660 int i;
1661
1662 for (i = 0; i < queue->queuesize; i++)
1663 urb_free_priv(queue->urb[i]);
1664
1665 free(queue);
1666
1667 return 0;
1668}
1669
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001670#if !CONFIG_IS_ENABLED(DM_USB)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001671/* submit routines called from usb.c */
1672int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1673 int transfer_len)
1674{
1675 info("submit_bulk_msg");
Hans de Goede2a240e72015-05-05 23:56:07 +02001676 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1677 NULL, 0);
1678}
1679
1680int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001681 int transfer_len, int interval, bool nonblock)
Hans de Goede2a240e72015-05-05 23:56:07 +02001682{
1683 info("submit_int_msg");
1684 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1685 interval);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001686}
Hans de Goede5477e6f2015-05-13 14:42:17 +02001687
1688struct int_queue *create_int_queue(struct usb_device *dev,
1689 unsigned long pipe, int queuesize, int elementsize,
1690 void *buffer, int interval)
1691{
1692 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1693 elementsize, buffer, interval);
1694}
1695
1696void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1697{
1698 return _ohci_poll_int_queue(&gohci, dev, queue);
1699}
1700
1701int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1702{
1703 return _ohci_destroy_int_queue(&gohci, dev, queue);
1704}
Hans de Goede8a62c502015-05-10 14:10:25 +02001705#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001706
Hans de Goede2a240e72015-05-05 23:56:07 +02001707static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1708 unsigned long pipe, void *buffer, int transfer_len,
1709 struct devrequest *setup)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001710{
1711 int maxsize = usb_maxpacket(dev, pipe);
1712
1713 info("submit_control_msg");
1714#ifdef DEBUG
Hans de Goede2a240e72015-05-05 23:56:07 +02001715 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
Remy Bohmer172a1132008-09-16 14:55:43 +02001716 setup, "SUB", usb_pipein(pipe));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001717#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001718 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001719#endif
1720 if (!maxsize) {
1721 err("submit_control_message: pipesize for pipe %lx is zero",
1722 pipe);
1723 return -1;
1724 }
Hans de Goede2a240e72015-05-05 23:56:07 +02001725 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1726 ohci->rh.dev = dev;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001727 /* root hub - redirect */
Hans de Goede2a240e72015-05-05 23:56:07 +02001728 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1729 transfer_len, setup);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001730 }
1731
Hans de Goede2a240e72015-05-05 23:56:07 +02001732 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1733 setup, 0);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001734}
1735
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001736/*-------------------------------------------------------------------------*
1737 * HC functions
1738 *-------------------------------------------------------------------------*/
1739
1740/* reset the HC and BUS */
1741
Remy Bohmer172a1132008-09-16 14:55:43 +02001742static int hc_reset(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001743{
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001744#ifdef CONFIG_PCI_EHCI_DEVNO
1745 pci_dev_t pdev;
1746#endif
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001747 int timeout = 30;
1748 int smm_timeout = 50; /* 0,5 sec */
1749
1750 dbg("%s\n", __FUNCTION__);
1751
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001752#ifdef CONFIG_PCI_EHCI_DEVNO
1753 /*
1754 * Some multi-function controllers (e.g. ISP1562) allow root hub
1755 * resetting via EHCI registers only.
1756 */
1757 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1758 if (pdev != -1) {
1759 u32 base;
1760 int timeout = 1000;
1761
1762 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001763 base += EHCI_USBCMD_OFF;
1764 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001765
Becky Bruce4abaacb2010-06-30 13:05:44 -05001766 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
Yuri Tikhonov11af42c2008-09-04 11:19:05 +02001767 if (timeout-- <= 0) {
1768 printf("USB RootHub reset timed out!");
1769 break;
1770 }
1771 udelay(1);
1772 }
1773 } else
1774 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1775#endif
Becky Bruce4abaacb2010-06-30 13:05:44 -05001776 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1777 /* SMM owns the HC, request ownership */
1778 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001779 info("USB HC TakeOver from SMM");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001780 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001781 mdelay(10);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001782 if (--smm_timeout == 0) {
1783 err("USB HC TakeOver failed!");
1784 return -1;
1785 }
1786 }
1787 }
1788
1789 /* Disable HC interrupts */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001790 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001791
1792 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1793 ohci->slot_name,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001794 ohci_readl(&ohci->regs->control));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001795
1796 /* Reset USB (needed by some controllers) */
Markus Klotzbuecher661ffe52006-11-27 11:43:09 +01001797 ohci->hc_control = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001798 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001799
1800 /* HC Reset requires max 10 us delay */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001801 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1802 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001803 if (--timeout == 0) {
1804 err("USB HC reset timed out!");
1805 return -1;
1806 }
Remy Bohmer172a1132008-09-16 14:55:43 +02001807 udelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001808 }
1809 return 0;
1810}
1811
1812/*-------------------------------------------------------------------------*/
1813
1814/* Start an OHCI controller, set the BUS operational
1815 * enable interrupts
1816 * connect the virtual root hub */
1817
Remy Bohmer172a1132008-09-16 14:55:43 +02001818static int hc_start(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001819{
1820 __u32 mask;
1821 unsigned int fminterval;
Hans de Goeded7a39422015-05-13 14:42:15 +02001822 int i;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001823
1824 ohci->disabled = 1;
Hans de Goeded7a39422015-05-13 14:42:15 +02001825 for (i = 0; i < NUM_INT_DEVS; i++)
1826 ohci->int_dev[i].devnum = -1;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001827
1828 /* Tell the controller where the control and bulk lists are
1829 * The lists are empty now. */
1830
Becky Bruce4abaacb2010-06-30 13:05:44 -05001831 ohci_writel(0, &ohci->regs->ed_controlhead);
1832 ohci_writel(0, &ohci->regs->ed_bulkhead);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001833
Andre Przywarac114a982016-10-21 02:24:29 +01001834 ohci_writel((uintptr_t)ohci->hcca,
Becky Bruce4abaacb2010-06-30 13:05:44 -05001835 &ohci->regs->hcca); /* reset clears this */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001836
1837 fminterval = 0x2edf;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001838 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001839 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001840 ohci_writel(fminterval, &ohci->regs->fminterval);
1841 ohci_writel(0x628, &ohci->regs->lsthresh);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001842
1843 /* start controller operations */
1844 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1845 ohci->disabled = 0;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001846 ohci_writel(ohci->hc_control, &ohci->regs->control);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001847
1848 /* disable all interrupts */
1849 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1850 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1851 OHCI_INTR_OC | OHCI_INTR_MIE);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001852 ohci_writel(mask, &ohci->regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001853 /* clear all interrupts */
1854 mask &= ~OHCI_INTR_MIE;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001855 ohci_writel(mask, &ohci->regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001856 /* Choose the interrupts we care about now - but w/o MIE */
1857 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
Becky Bruce4abaacb2010-06-30 13:05:44 -05001858 ohci_writel(mask, &ohci->regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001859
1860#ifdef OHCI_USE_NPS
1861 /* required for AMD-756 and some Mac platforms */
Becky Bruce4abaacb2010-06-30 13:05:44 -05001862 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001863 &ohci->regs->roothub.a);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001864 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001865#endif /* OHCI_USE_NPS */
1866
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001867 /* connect the virtual root hub */
1868 ohci->rh.devnum = 0;
1869
1870 return 0;
1871}
1872
1873/*-------------------------------------------------------------------------*/
1874
1875/* an interrupt happens */
1876
Hans de Goede2a240e72015-05-05 23:56:07 +02001877static int hc_interrupt(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001878{
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001879 struct ohci_regs *regs = ohci->regs;
1880 int ints;
1881 int stat = -1;
1882
Hans de Goede8e41f662015-05-05 23:56:13 +02001883 invalidate_dcache_hcca(ohci->hcca);
1884
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001885 if ((ohci->hcca->done_head != 0) &&
Remy Bohmer172a1132008-09-16 14:55:43 +02001886 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001887 ints = OHCI_INTR_WDH;
Remy Bohmer172a1132008-09-16 14:55:43 +02001888 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001889 ints = ohci_readl(&regs->intrstatus);
Remy Bohmer172a1132008-09-16 14:55:43 +02001890 if (ints == ~(u32)0) {
1891 ohci->disabled++;
1892 err("%s device removed!", ohci->slot_name);
1893 return -1;
1894 } else {
Becky Bruce4abaacb2010-06-30 13:05:44 -05001895 ints &= ohci_readl(&regs->intrenable);
Remy Bohmer172a1132008-09-16 14:55:43 +02001896 if (ints == 0) {
1897 dbg("hc_interrupt: returning..\n");
1898 return 0xff;
1899 }
1900 }
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001901 }
Markus Klotzbuechere3af6d52007-03-26 11:21:05 +02001902
Remy Bohmer172a1132008-09-16 14:55:43 +02001903 /* dbg("Interrupt: %x frame: %x", ints,
1904 le16_to_cpu(ohci->hcca->frame_no)); */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001905
Remy Bohmer172a1132008-09-16 14:55:43 +02001906 if (ints & OHCI_INTR_RHSC)
Markus Klotzbuecheraa219212006-05-30 16:56:14 +02001907 stat = 0xff;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001908
1909 if (ints & OHCI_INTR_UE) {
1910 ohci->disabled++;
Remy Bohmer172a1132008-09-16 14:55:43 +02001911 err("OHCI Unrecoverable Error, controller usb-%s disabled",
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001912 ohci->slot_name);
1913 /* e.g. due to PCI Master/Target Abort */
1914
1915#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02001916 ohci_dump(ohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001917#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001918 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001919#endif
1920 /* FIXME: be optimistic, hope that bug won't repeat often. */
1921 /* Make some non-interrupt context restart the controller. */
1922 /* Count and limit the retries though; either hardware or */
1923 /* software errors can go forever... */
Remy Bohmer172a1132008-09-16 14:55:43 +02001924 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001925 return -1;
1926 }
1927
1928 if (ints & OHCI_INTR_WDH) {
Hans de Goeded6cc1d12015-05-10 14:10:24 +02001929 ohci_mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001930 ohci_writel(OHCI_INTR_WDH, &regs->intrdisable);
1931 (void)ohci_readl(&regs->intrdisable); /* flush */
Hans de Goede2a240e72015-05-05 23:56:07 +02001932 stat = dl_done_list(ohci);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001933 ohci_writel(OHCI_INTR_WDH, &regs->intrenable);
1934 (void)ohci_readl(&regs->intrdisable); /* flush */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001935 }
1936
1937 if (ints & OHCI_INTR_SO) {
1938 dbg("USB Schedule overrun\n");
Becky Bruce4abaacb2010-06-30 13:05:44 -05001939 ohci_writel(OHCI_INTR_SO, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001940 stat = -1;
1941 }
1942
1943 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1944 if (ints & OHCI_INTR_SF) {
Remy Bohmer172a1132008-09-16 14:55:43 +02001945 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001946 mdelay(1);
Becky Bruce4abaacb2010-06-30 13:05:44 -05001947 ohci_writel(OHCI_INTR_SF, &regs->intrdisable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001948 if (ohci->ed_rm_list[frame] != NULL)
Becky Bruce4abaacb2010-06-30 13:05:44 -05001949 ohci_writel(OHCI_INTR_SF, &regs->intrenable);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001950 stat = 0xff;
1951 }
1952
Becky Bruce4abaacb2010-06-30 13:05:44 -05001953 ohci_writel(ints, &regs->intrstatus);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001954 return stat;
1955}
1956
1957/*-------------------------------------------------------------------------*/
1958
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001959#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02001960
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001961/*-------------------------------------------------------------------------*/
1962
1963/* De-allocate all resources.. */
1964
Remy Bohmer172a1132008-09-16 14:55:43 +02001965static void hc_release_ohci(ohci_t *ohci)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001966{
Remy Bohmer172a1132008-09-16 14:55:43 +02001967 dbg("USB HC release ohci usb-%s", ohci->slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001968
1969 if (!ohci->disabled)
Remy Bohmer172a1132008-09-16 14:55:43 +02001970 hc_reset(ohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001971}
1972
1973/*-------------------------------------------------------------------------*/
1974
1975/*
1976 * low level initalisation routine, called from usb.c
1977 */
1978static char ohci_inited = 0;
1979
Troy Kisky8f9c49d2013-10-10 15:27:56 -07001980int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001981{
Remy Bohmer172a1132008-09-16 14:55:43 +02001982 memset(&gohci, 0, sizeof(ohci_t));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001983
1984 /* align the storage */
1985 if ((__u32)&ghcca[0] & 0xff) {
1986 err("HCCA not aligned!!");
1987 return -1;
1988 }
Hans de Goedeeb509d32015-05-05 23:56:10 +02001989 gohci.hcca = &ghcca[0];
1990 info("aligned ghcca %p", gohci.hcca);
1991 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001992
1993 gohci.disabled = 1;
1994 gohci.sleeping = 0;
1995 gohci.irq = -1;
Tom Rini6a5dccc2022-11-16 13:10:41 -05001996 gohci.regs = (struct ohci_regs *)CFG_SYS_USB_OHCI_REGS_BASE;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02001997
1998 gohci.flags = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001999 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002000
2001 if (hc_reset (&gohci) < 0) {
2002 hc_release_ohci (&gohci);
2003 err ("can't reset usb-%s", gohci.slot_name);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002004 return -1;
2005 }
2006
Remy Bohmer172a1132008-09-16 14:55:43 +02002007 if (hc_start(&gohci) < 0) {
2008 err("can't start usb-%s", gohci.slot_name);
2009 hc_release_ohci(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002010 /* Initialization failed */
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002011 return -1;
2012 }
2013
2014#ifdef DEBUG
Remy Bohmer172a1132008-09-16 14:55:43 +02002015 ohci_dump(&gohci, 1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002016#else
Hans de Goeded6cc1d12015-05-10 14:10:24 +02002017 ohci_mdelay(1);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002018#endif
2019 ohci_inited = 1;
2020 return 0;
2021}
2022
Lucas Stacha3231282012-09-26 00:14:34 +02002023int usb_lowlevel_stop(int index)
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002024{
2025 /* this gets called really early - before the controller has */
2026 /* even been initialized! */
2027 if (!ohci_inited)
2028 return 0;
2029 /* TODO release any interrupts, etc. */
2030 /* call hc_release_ohci() here ? */
Remy Bohmer172a1132008-09-16 14:55:43 +02002031 hc_reset(&gohci);
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002032
Remy Bohmer743dd492008-08-20 11:22:02 +02002033 /* This driver is no longer initialised. It needs a new low-level
2034 * init (board/cpu) before it can be used again. */
2035 ohci_inited = 0;
Markus Klotzbuecherd8d023f2006-05-22 16:33:54 +02002036 return 0;
2037}
Hans de Goede2a240e72015-05-05 23:56:07 +02002038
2039int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2040 void *buffer, int transfer_len, struct devrequest *setup)
2041{
2042 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2043 transfer_len, setup);
2044}
Hans de Goede8a62c502015-05-10 14:10:25 +02002045#endif
2046
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01002047#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede8a62c502015-05-10 14:10:25 +02002048static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2049 unsigned long pipe, void *buffer, int length,
2050 struct devrequest *setup)
2051{
2052 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2053
2054 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2055 length, setup);
2056}
2057
2058static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2059 unsigned long pipe, void *buffer, int length)
2060{
2061 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2062
2063 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2064}
2065
2066static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2067 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02002068 int interval, bool nonblock)
Hans de Goede8a62c502015-05-10 14:10:25 +02002069{
2070 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2071
2072 return submit_common_msg(ohci, udev, pipe, buffer, length,
2073 NULL, interval);
2074}
2075
Hans de Goede5477e6f2015-05-13 14:42:17 +02002076static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2077 struct usb_device *udev, unsigned long pipe, int queuesize,
2078 int elementsize, void *buffer, int interval)
2079{
2080 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2081
2082 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2083 buffer, interval);
2084}
2085
2086static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2087 struct int_queue *queue)
2088{
2089 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2090
2091 return _ohci_poll_int_queue(ohci, udev, queue);
2092}
2093
2094static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2095 struct int_queue *queue)
2096{
2097 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2098
2099 return _ohci_destroy_int_queue(ohci, udev, queue);
2100}
2101
Hans de Goede8a62c502015-05-10 14:10:25 +02002102int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2103{
2104 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2105 ohci_t *ohci = dev_get_priv(dev);
2106 u32 reg;
2107
2108 priv->desc_before_addr = true;
2109
2110 ohci->regs = regs;
2111 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2112 if (!ohci->hcca)
2113 return -ENOMEM;
2114 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
Hans de Goede66cb3e42015-10-20 18:39:29 +02002115 flush_dcache_hcca(ohci->hcca);
Hans de Goede8a62c502015-05-10 14:10:25 +02002116
2117 if (hc_reset(ohci) < 0)
2118 return -EIO;
2119
2120 if (hc_start(ohci) < 0)
2121 return -EIO;
2122
2123 reg = ohci_readl(&regs->revision);
2124 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2125
2126 return 0;
2127}
2128
2129int ohci_deregister(struct udevice *dev)
2130{
2131 ohci_t *ohci = dev_get_priv(dev);
2132
2133 if (hc_reset(ohci) < 0)
2134 return -EIO;
2135
2136 free(ohci->hcca);
2137
2138 return 0;
2139}
2140
2141struct dm_usb_ops ohci_usb_ops = {
2142 .control = ohci_submit_control_msg,
2143 .bulk = ohci_submit_bulk_msg,
2144 .interrupt = ohci_submit_int_msg,
Hans de Goede5477e6f2015-05-13 14:42:17 +02002145 .create_int_queue = ohci_create_int_queue,
2146 .poll_int_queue = ohci_poll_int_queue,
2147 .destroy_int_queue = ohci_destroy_int_queue,
Hans de Goede8a62c502015-05-10 14:10:25 +02002148};
2149
2150#endif