1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Interrupt support is added. Now, it has been tested
5*4882a593Smuzhiyun * on ULI1575 chip and works well with USB keyboard.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * (C) Copyright 2007
8*4882a593Smuzhiyun * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * (C) Copyright 2003
11*4882a593Smuzhiyun * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Note: Much of this code has been derived from Linux 2.4
14*4882a593Smuzhiyun * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
15*4882a593Smuzhiyun * (C) Copyright 2000-2002 David Brownell
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
18*4882a593Smuzhiyun * ebenard@eukrea.com - based on s3c24x0's driver
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * IMPORTANT NOTES
24*4882a593Smuzhiyun * 1 - Read doc/README.generic_usb_ohci
25*4882a593Smuzhiyun * 2 - this driver is intended for use with USB Mass Storage Devices
26*4882a593Smuzhiyun * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
27*4882a593Smuzhiyun * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
28*4882a593Smuzhiyun * to activate workaround for bug #41 or this driver will NOT work!
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <common.h>
32*4882a593Smuzhiyun #include <asm/byteorder.h>
33*4882a593Smuzhiyun #include <dm.h>
34*4882a593Smuzhiyun #include <errno.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #if defined(CONFIG_PCI_OHCI)
37*4882a593Smuzhiyun # include <pci.h>
38*4882a593Smuzhiyun #if !defined(CONFIG_PCI_OHCI_DEVNO)
39*4882a593Smuzhiyun #define CONFIG_PCI_OHCI_DEVNO 0
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <malloc.h>
44*4882a593Smuzhiyun #include <memalign.h>
45*4882a593Smuzhiyun #include <usb.h>
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #include "ohci.h"
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #ifdef CONFIG_AT91RM9200
50*4882a593Smuzhiyun #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #if defined(CONFIG_CPU_ARM920T) || \
54*4882a593Smuzhiyun defined(CONFIG_PCI_OHCI) || \
55*4882a593Smuzhiyun defined(CONFIG_DM_PCI) || \
56*4882a593Smuzhiyun defined(CONFIG_SYS_OHCI_USE_NPS)
57*4882a593Smuzhiyun # define OHCI_USE_NPS /* force NoPowerSwitching mode */
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #undef OHCI_VERBOSE_DEBUG /* not always helpful */
61*4882a593Smuzhiyun #undef DEBUG
62*4882a593Smuzhiyun #undef SHOW_INFO
63*4882a593Smuzhiyun #undef OHCI_FILL_TRACE
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* For initializing controller (mask in an HCFS mode too) */
66*4882a593Smuzhiyun #define OHCI_CONTROL_INIT \
67*4882a593Smuzhiyun (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
70*4882a593Smuzhiyun #ifdef CONFIG_PCI_OHCI
71*4882a593Smuzhiyun static struct pci_device_id ohci_pci_ids[] = {
72*4882a593Smuzhiyun {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
73*4882a593Smuzhiyun {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
74*4882a593Smuzhiyun {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
75*4882a593Smuzhiyun /* Please add supported PCI OHCI controller ids here */
76*4882a593Smuzhiyun {0, 0}
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #ifdef CONFIG_PCI_EHCI_DEVNO
82*4882a593Smuzhiyun static struct pci_device_id ehci_pci_ids[] = {
83*4882a593Smuzhiyun {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
84*4882a593Smuzhiyun /* Please add supported PCI EHCI controller ids here */
85*4882a593Smuzhiyun {0, 0}
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun #endif
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #ifdef DEBUG
90*4882a593Smuzhiyun #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
91*4882a593Smuzhiyun #else
92*4882a593Smuzhiyun #define dbg(format, arg...) do {} while (0)
93*4882a593Smuzhiyun #endif /* DEBUG */
94*4882a593Smuzhiyun #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
95*4882a593Smuzhiyun #ifdef SHOW_INFO
96*4882a593Smuzhiyun #define info(format, arg...) printf("INFO: " format "\n", ## arg)
97*4882a593Smuzhiyun #else
98*4882a593Smuzhiyun #define info(format, arg...) do {} while (0)
99*4882a593Smuzhiyun #endif
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
102*4882a593Smuzhiyun # define m16_swap(x) cpu_to_be16(x)
103*4882a593Smuzhiyun # define m32_swap(x) cpu_to_be32(x)
104*4882a593Smuzhiyun #else
105*4882a593Smuzhiyun # define m16_swap(x) cpu_to_le16(x)
106*4882a593Smuzhiyun # define m32_swap(x) cpu_to_le32(x)
107*4882a593Smuzhiyun #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* We really should do proper cache flushing everywhere */
110*4882a593Smuzhiyun #define flush_dcache_buffer(addr, size) \
111*4882a593Smuzhiyun flush_dcache_range((unsigned long)(addr), \
112*4882a593Smuzhiyun ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
113*4882a593Smuzhiyun #define invalidate_dcache_buffer(addr, size) \
114*4882a593Smuzhiyun invalidate_dcache_range((unsigned long)(addr), \
115*4882a593Smuzhiyun ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
118*4882a593Smuzhiyun #define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
119*4882a593Smuzhiyun #define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
120*4882a593Smuzhiyun #define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
121*4882a593Smuzhiyun #define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
122*4882a593Smuzhiyun #define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
123*4882a593Smuzhiyun #define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
124*4882a593Smuzhiyun #define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
125*4882a593Smuzhiyun #define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
130*4882a593Smuzhiyun * them around when building for older boards not yet converted to the dm
131*4882a593Smuzhiyun * just in case (to avoid regressions), for dm this turns them into nops.
132*4882a593Smuzhiyun */
133*4882a593Smuzhiyun #define ohci_mdelay(x)
134*4882a593Smuzhiyun #else
135*4882a593Smuzhiyun #define ohci_mdelay(x) mdelay(x)
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
139*4882a593Smuzhiyun /* global ohci_t */
140*4882a593Smuzhiyun static ohci_t gohci;
141*4882a593Smuzhiyun /* this must be aligned to a 256 byte boundary */
142*4882a593Smuzhiyun struct ohci_hcca ghcca[1];
143*4882a593Smuzhiyun #endif
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /* mapping of the OHCI CC status to error codes */
146*4882a593Smuzhiyun static int cc_to_error[16] = {
147*4882a593Smuzhiyun /* No Error */ 0,
148*4882a593Smuzhiyun /* CRC Error */ USB_ST_CRC_ERR,
149*4882a593Smuzhiyun /* Bit Stuff */ USB_ST_BIT_ERR,
150*4882a593Smuzhiyun /* Data Togg */ USB_ST_CRC_ERR,
151*4882a593Smuzhiyun /* Stall */ USB_ST_STALLED,
152*4882a593Smuzhiyun /* DevNotResp */ -1,
153*4882a593Smuzhiyun /* PIDCheck */ USB_ST_BIT_ERR,
154*4882a593Smuzhiyun /* UnExpPID */ USB_ST_BIT_ERR,
155*4882a593Smuzhiyun /* DataOver */ USB_ST_BUF_ERR,
156*4882a593Smuzhiyun /* DataUnder */ USB_ST_BUF_ERR,
157*4882a593Smuzhiyun /* reservd */ -1,
158*4882a593Smuzhiyun /* reservd */ -1,
159*4882a593Smuzhiyun /* BufferOver */ USB_ST_BUF_ERR,
160*4882a593Smuzhiyun /* BuffUnder */ USB_ST_BUF_ERR,
161*4882a593Smuzhiyun /* Not Access */ -1,
162*4882a593Smuzhiyun /* Not Access */ -1
163*4882a593Smuzhiyun };
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun static const char *cc_to_string[16] = {
166*4882a593Smuzhiyun "No Error",
167*4882a593Smuzhiyun "CRC: Last data packet from endpoint contained a CRC error.",
168*4882a593Smuzhiyun "BITSTUFFING: Last data packet from endpoint contained a bit " \
169*4882a593Smuzhiyun "stuffing violation",
170*4882a593Smuzhiyun "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
171*4882a593Smuzhiyun "that did not match the expected value.",
172*4882a593Smuzhiyun "STALL: TD was moved to the Done Queue because the endpoint returned" \
173*4882a593Smuzhiyun " a STALL PID",
174*4882a593Smuzhiyun "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
175*4882a593Smuzhiyun "not provide a handshake (OUT)",
176*4882a593Smuzhiyun "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
177*4882a593Smuzhiyun "(IN) or handshake (OUT)",
178*4882a593Smuzhiyun "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
179*4882a593Smuzhiyun "value is not defined.",
180*4882a593Smuzhiyun "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
181*4882a593Smuzhiyun "either the size of the maximum data packet allowed\n" \
182*4882a593Smuzhiyun "from the endpoint (found in MaximumPacketSize field\n" \
183*4882a593Smuzhiyun "of ED) or the remaining buffer size.",
184*4882a593Smuzhiyun "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
185*4882a593Smuzhiyun "and that amount was not sufficient to fill the\n" \
186*4882a593Smuzhiyun "specified buffer",
187*4882a593Smuzhiyun "reserved1",
188*4882a593Smuzhiyun "reserved2",
189*4882a593Smuzhiyun "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
190*4882a593Smuzhiyun "than it could be written to system memory",
191*4882a593Smuzhiyun "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
192*4882a593Smuzhiyun "system memory fast enough to keep up with data USB " \
193*4882a593Smuzhiyun "data rate.",
194*4882a593Smuzhiyun "NOT ACCESSED: This code is set by software before the TD is placed" \
195*4882a593Smuzhiyun "on a list to be processed by the HC.(1)",
196*4882a593Smuzhiyun "NOT ACCESSED: This code is set by software before the TD is placed" \
197*4882a593Smuzhiyun "on a list to be processed by the HC.(2)",
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun
roothub_a(struct ohci * hc)200*4882a593Smuzhiyun static inline u32 roothub_a(struct ohci *hc)
201*4882a593Smuzhiyun { return ohci_readl(&hc->regs->roothub.a); }
roothub_b(struct ohci * hc)202*4882a593Smuzhiyun static inline u32 roothub_b(struct ohci *hc)
203*4882a593Smuzhiyun { return ohci_readl(&hc->regs->roothub.b); }
roothub_status(struct ohci * hc)204*4882a593Smuzhiyun static inline u32 roothub_status(struct ohci *hc)
205*4882a593Smuzhiyun { return ohci_readl(&hc->regs->roothub.status); }
roothub_portstatus(struct ohci * hc,int i)206*4882a593Smuzhiyun static inline u32 roothub_portstatus(struct ohci *hc, int i)
207*4882a593Smuzhiyun { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* forward declaration */
210*4882a593Smuzhiyun static int hc_interrupt(ohci_t *ohci);
211*4882a593Smuzhiyun static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
212*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
213*4882a593Smuzhiyun struct devrequest *setup, urb_priv_t *urb,
214*4882a593Smuzhiyun int interval);
215*4882a593Smuzhiyun static int ep_link(ohci_t * ohci, ed_t * ed);
216*4882a593Smuzhiyun static int ep_unlink(ohci_t * ohci, ed_t * ed);
217*4882a593Smuzhiyun static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
218*4882a593Smuzhiyun unsigned long pipe, int interval, int load);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* TDs ... */
td_alloc(ohci_dev_t * ohci_dev,struct usb_device * usb_dev)223*4882a593Smuzhiyun static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun int i;
226*4882a593Smuzhiyun struct td *td;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun td = NULL;
229*4882a593Smuzhiyun for (i = 0; i < NUM_TD; i++)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun if (ohci_dev->tds[i].usb_dev == NULL)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun td = &ohci_dev->tds[i];
234*4882a593Smuzhiyun td->usb_dev = usb_dev;
235*4882a593Smuzhiyun break;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun return td;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
ed_free(struct ed * ed)242*4882a593Smuzhiyun static inline void ed_free(struct ed *ed)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun ed->usb_dev = NULL;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
248*4882a593Smuzhiyun * URB support functions
249*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* free HCD-private data associated with this URB */
252*4882a593Smuzhiyun
urb_free_priv(urb_priv_t * urb)253*4882a593Smuzhiyun static void urb_free_priv(urb_priv_t *urb)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun int i;
256*4882a593Smuzhiyun int last;
257*4882a593Smuzhiyun struct td *td;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun last = urb->length - 1;
260*4882a593Smuzhiyun if (last >= 0) {
261*4882a593Smuzhiyun for (i = 0; i <= last; i++) {
262*4882a593Smuzhiyun td = urb->td[i];
263*4882a593Smuzhiyun if (td) {
264*4882a593Smuzhiyun td->usb_dev = NULL;
265*4882a593Smuzhiyun urb->td[i] = NULL;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun free(urb);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun #ifdef DEBUG
275*4882a593Smuzhiyun static int sohci_get_current_frame_number(ohci_t *ohci);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* debug| print the main components of an URB
278*4882a593Smuzhiyun * small: 0) header + data packets 1) just header */
279*4882a593Smuzhiyun
pkt_print(ohci_t * ohci,urb_priv_t * purb,struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * setup,char * str,int small)280*4882a593Smuzhiyun static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
281*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
282*4882a593Smuzhiyun struct devrequest *setup, char *str, int small)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
285*4882a593Smuzhiyun str,
286*4882a593Smuzhiyun sohci_get_current_frame_number(ohci),
287*4882a593Smuzhiyun usb_pipedevice(pipe),
288*4882a593Smuzhiyun usb_pipeendpoint(pipe),
289*4882a593Smuzhiyun usb_pipeout(pipe)? 'O': 'I',
290*4882a593Smuzhiyun usb_pipetype(pipe) < 2 ? \
291*4882a593Smuzhiyun (usb_pipeint(pipe)? "INTR": "ISOC"): \
292*4882a593Smuzhiyun (usb_pipecontrol(pipe)? "CTRL": "BULK"),
293*4882a593Smuzhiyun (purb ? purb->actual_length : 0),
294*4882a593Smuzhiyun transfer_len, dev->status);
295*4882a593Smuzhiyun #ifdef OHCI_VERBOSE_DEBUG
296*4882a593Smuzhiyun if (!small) {
297*4882a593Smuzhiyun int i, len;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (usb_pipecontrol(pipe)) {
300*4882a593Smuzhiyun printf(__FILE__ ": cmd(8):");
301*4882a593Smuzhiyun for (i = 0; i < 8 ; i++)
302*4882a593Smuzhiyun printf(" %02x", ((__u8 *) setup) [i]);
303*4882a593Smuzhiyun printf("\n");
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun if (transfer_len > 0 && buffer) {
306*4882a593Smuzhiyun printf(__FILE__ ": data(%d/%d):",
307*4882a593Smuzhiyun (purb ? purb->actual_length : 0),
308*4882a593Smuzhiyun transfer_len);
309*4882a593Smuzhiyun len = usb_pipeout(pipe)? transfer_len:
310*4882a593Smuzhiyun (purb ? purb->actual_length : 0);
311*4882a593Smuzhiyun for (i = 0; i < 16 && i < len; i++)
312*4882a593Smuzhiyun printf(" %02x", ((__u8 *) buffer) [i]);
313*4882a593Smuzhiyun printf("%s\n", i < len? "...": "");
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun #endif
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /* just for debugging; prints non-empty branches of the int ed tree
320*4882a593Smuzhiyun * inclusive iso eds */
ep_print_int_eds(ohci_t * ohci,char * str)321*4882a593Smuzhiyun void ep_print_int_eds(ohci_t *ohci, char *str)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun int i, j;
324*4882a593Smuzhiyun __u32 *ed_p;
325*4882a593Smuzhiyun for (i = 0; i < 32; i++) {
326*4882a593Smuzhiyun j = 5;
327*4882a593Smuzhiyun ed_p = &(ohci->hcca->int_table [i]);
328*4882a593Smuzhiyun if (*ed_p == 0)
329*4882a593Smuzhiyun continue;
330*4882a593Smuzhiyun invalidate_dcache_ed(ed_p);
331*4882a593Smuzhiyun printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
332*4882a593Smuzhiyun while (*ed_p != 0 && j--) {
333*4882a593Smuzhiyun ed_t *ed = (ed_t *)m32_swap(ed_p);
334*4882a593Smuzhiyun invalidate_dcache_ed(ed);
335*4882a593Smuzhiyun printf(" ed: %4x;", ed->hwINFO);
336*4882a593Smuzhiyun ed_p = &ed->hwNextED;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun printf("\n");
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
ohci_dump_intr_mask(char * label,__u32 mask)342*4882a593Smuzhiyun static void ohci_dump_intr_mask(char *label, __u32 mask)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
345*4882a593Smuzhiyun label,
346*4882a593Smuzhiyun mask,
347*4882a593Smuzhiyun (mask & OHCI_INTR_MIE) ? " MIE" : "",
348*4882a593Smuzhiyun (mask & OHCI_INTR_OC) ? " OC" : "",
349*4882a593Smuzhiyun (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
350*4882a593Smuzhiyun (mask & OHCI_INTR_FNO) ? " FNO" : "",
351*4882a593Smuzhiyun (mask & OHCI_INTR_UE) ? " UE" : "",
352*4882a593Smuzhiyun (mask & OHCI_INTR_RD) ? " RD" : "",
353*4882a593Smuzhiyun (mask & OHCI_INTR_SF) ? " SF" : "",
354*4882a593Smuzhiyun (mask & OHCI_INTR_WDH) ? " WDH" : "",
355*4882a593Smuzhiyun (mask & OHCI_INTR_SO) ? " SO" : ""
356*4882a593Smuzhiyun );
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
maybe_print_eds(char * label,__u32 value)359*4882a593Smuzhiyun static void maybe_print_eds(char *label, __u32 value)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun ed_t *edp = (ed_t *)value;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (value) {
364*4882a593Smuzhiyun dbg("%s %08x", label, value);
365*4882a593Smuzhiyun invalidate_dcache_ed(edp);
366*4882a593Smuzhiyun dbg("%08x", edp->hwINFO);
367*4882a593Smuzhiyun dbg("%08x", edp->hwTailP);
368*4882a593Smuzhiyun dbg("%08x", edp->hwHeadP);
369*4882a593Smuzhiyun dbg("%08x", edp->hwNextED);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
hcfs2string(int state)373*4882a593Smuzhiyun static char *hcfs2string(int state)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun switch (state) {
376*4882a593Smuzhiyun case OHCI_USB_RESET: return "reset";
377*4882a593Smuzhiyun case OHCI_USB_RESUME: return "resume";
378*4882a593Smuzhiyun case OHCI_USB_OPER: return "operational";
379*4882a593Smuzhiyun case OHCI_USB_SUSPEND: return "suspend";
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun return "?";
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /* dump control and status registers */
ohci_dump_status(ohci_t * controller)385*4882a593Smuzhiyun static void ohci_dump_status(ohci_t *controller)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun struct ohci_regs *regs = controller->regs;
388*4882a593Smuzhiyun __u32 temp;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun temp = ohci_readl(®s->revision) & 0xff;
391*4882a593Smuzhiyun if (temp != 0x10)
392*4882a593Smuzhiyun dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun temp = ohci_readl(®s->control);
395*4882a593Smuzhiyun dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
396*4882a593Smuzhiyun (temp & OHCI_CTRL_RWE) ? " RWE" : "",
397*4882a593Smuzhiyun (temp & OHCI_CTRL_RWC) ? " RWC" : "",
398*4882a593Smuzhiyun (temp & OHCI_CTRL_IR) ? " IR" : "",
399*4882a593Smuzhiyun hcfs2string(temp & OHCI_CTRL_HCFS),
400*4882a593Smuzhiyun (temp & OHCI_CTRL_BLE) ? " BLE" : "",
401*4882a593Smuzhiyun (temp & OHCI_CTRL_CLE) ? " CLE" : "",
402*4882a593Smuzhiyun (temp & OHCI_CTRL_IE) ? " IE" : "",
403*4882a593Smuzhiyun (temp & OHCI_CTRL_PLE) ? " PLE" : "",
404*4882a593Smuzhiyun temp & OHCI_CTRL_CBSR
405*4882a593Smuzhiyun );
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun temp = ohci_readl(®s->cmdstatus);
408*4882a593Smuzhiyun dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
409*4882a593Smuzhiyun (temp & OHCI_SOC) >> 16,
410*4882a593Smuzhiyun (temp & OHCI_OCR) ? " OCR" : "",
411*4882a593Smuzhiyun (temp & OHCI_BLF) ? " BLF" : "",
412*4882a593Smuzhiyun (temp & OHCI_CLF) ? " CLF" : "",
413*4882a593Smuzhiyun (temp & OHCI_HCR) ? " HCR" : ""
414*4882a593Smuzhiyun );
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun ohci_dump_intr_mask("intrstatus", ohci_readl(®s->intrstatus));
417*4882a593Smuzhiyun ohci_dump_intr_mask("intrenable", ohci_readl(®s->intrenable));
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun maybe_print_eds("ed_periodcurrent",
420*4882a593Smuzhiyun ohci_readl(®s->ed_periodcurrent));
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun maybe_print_eds("ed_controlhead", ohci_readl(®s->ed_controlhead));
423*4882a593Smuzhiyun maybe_print_eds("ed_controlcurrent",
424*4882a593Smuzhiyun ohci_readl(®s->ed_controlcurrent));
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun maybe_print_eds("ed_bulkhead", ohci_readl(®s->ed_bulkhead));
427*4882a593Smuzhiyun maybe_print_eds("ed_bulkcurrent", ohci_readl(®s->ed_bulkcurrent));
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun maybe_print_eds("donehead", ohci_readl(®s->donehead));
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
ohci_dump_roothub(ohci_t * controller,int verbose)432*4882a593Smuzhiyun static void ohci_dump_roothub(ohci_t *controller, int verbose)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun __u32 temp, ndp, i;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun temp = roothub_a(controller);
437*4882a593Smuzhiyun ndp = (temp & RH_A_NDP);
438*4882a593Smuzhiyun #ifdef CONFIG_AT91C_PQFP_UHPBUG
439*4882a593Smuzhiyun ndp = (ndp == 2) ? 1:0;
440*4882a593Smuzhiyun #endif
441*4882a593Smuzhiyun if (verbose) {
442*4882a593Smuzhiyun dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
443*4882a593Smuzhiyun ((temp & RH_A_POTPGT) >> 24) & 0xff,
444*4882a593Smuzhiyun (temp & RH_A_NOCP) ? " NOCP" : "",
445*4882a593Smuzhiyun (temp & RH_A_OCPM) ? " OCPM" : "",
446*4882a593Smuzhiyun (temp & RH_A_DT) ? " DT" : "",
447*4882a593Smuzhiyun (temp & RH_A_NPS) ? " NPS" : "",
448*4882a593Smuzhiyun (temp & RH_A_PSM) ? " PSM" : "",
449*4882a593Smuzhiyun ndp
450*4882a593Smuzhiyun );
451*4882a593Smuzhiyun temp = roothub_b(controller);
452*4882a593Smuzhiyun dbg("roothub.b: %08x PPCM=%04x DR=%04x",
453*4882a593Smuzhiyun temp,
454*4882a593Smuzhiyun (temp & RH_B_PPCM) >> 16,
455*4882a593Smuzhiyun (temp & RH_B_DR)
456*4882a593Smuzhiyun );
457*4882a593Smuzhiyun temp = roothub_status(controller);
458*4882a593Smuzhiyun dbg("roothub.status: %08x%s%s%s%s%s%s",
459*4882a593Smuzhiyun temp,
460*4882a593Smuzhiyun (temp & RH_HS_CRWE) ? " CRWE" : "",
461*4882a593Smuzhiyun (temp & RH_HS_OCIC) ? " OCIC" : "",
462*4882a593Smuzhiyun (temp & RH_HS_LPSC) ? " LPSC" : "",
463*4882a593Smuzhiyun (temp & RH_HS_DRWE) ? " DRWE" : "",
464*4882a593Smuzhiyun (temp & RH_HS_OCI) ? " OCI" : "",
465*4882a593Smuzhiyun (temp & RH_HS_LPS) ? " LPS" : ""
466*4882a593Smuzhiyun );
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun for (i = 0; i < ndp; i++) {
470*4882a593Smuzhiyun temp = roothub_portstatus(controller, i);
471*4882a593Smuzhiyun dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
472*4882a593Smuzhiyun i,
473*4882a593Smuzhiyun temp,
474*4882a593Smuzhiyun (temp & RH_PS_PRSC) ? " PRSC" : "",
475*4882a593Smuzhiyun (temp & RH_PS_OCIC) ? " OCIC" : "",
476*4882a593Smuzhiyun (temp & RH_PS_PSSC) ? " PSSC" : "",
477*4882a593Smuzhiyun (temp & RH_PS_PESC) ? " PESC" : "",
478*4882a593Smuzhiyun (temp & RH_PS_CSC) ? " CSC" : "",
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun (temp & RH_PS_LSDA) ? " LSDA" : "",
481*4882a593Smuzhiyun (temp & RH_PS_PPS) ? " PPS" : "",
482*4882a593Smuzhiyun (temp & RH_PS_PRS) ? " PRS" : "",
483*4882a593Smuzhiyun (temp & RH_PS_POCI) ? " POCI" : "",
484*4882a593Smuzhiyun (temp & RH_PS_PSS) ? " PSS" : "",
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun (temp & RH_PS_PES) ? " PES" : "",
487*4882a593Smuzhiyun (temp & RH_PS_CCS) ? " CCS" : ""
488*4882a593Smuzhiyun );
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
ohci_dump(ohci_t * controller,int verbose)492*4882a593Smuzhiyun static void ohci_dump(ohci_t *controller, int verbose)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun dbg("OHCI controller usb-%s state", controller->slot_name);
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /* dumps some of the state we know about */
497*4882a593Smuzhiyun ohci_dump_status(controller);
498*4882a593Smuzhiyun if (verbose)
499*4882a593Smuzhiyun ep_print_int_eds(controller, "hcca");
500*4882a593Smuzhiyun invalidate_dcache_hcca(controller->hcca);
501*4882a593Smuzhiyun dbg("hcca frame #%04x", controller->hcca->frame_no);
502*4882a593Smuzhiyun ohci_dump_roothub(controller, 1);
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun #endif /* DEBUG */
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
507*4882a593Smuzhiyun * Interface functions (URB)
508*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* get a transfer request */
511*4882a593Smuzhiyun
sohci_submit_job(ohci_t * ohci,ohci_dev_t * ohci_dev,urb_priv_t * urb,struct devrequest * setup)512*4882a593Smuzhiyun int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
513*4882a593Smuzhiyun struct devrequest *setup)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun ed_t *ed;
516*4882a593Smuzhiyun urb_priv_t *purb_priv = urb;
517*4882a593Smuzhiyun int i, size = 0;
518*4882a593Smuzhiyun struct usb_device *dev = urb->dev;
519*4882a593Smuzhiyun unsigned long pipe = urb->pipe;
520*4882a593Smuzhiyun void *buffer = urb->transfer_buffer;
521*4882a593Smuzhiyun int transfer_len = urb->transfer_buffer_length;
522*4882a593Smuzhiyun int interval = urb->interval;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /* when controller's hung, permit only roothub cleanup attempts
525*4882a593Smuzhiyun * such as powering down ports */
526*4882a593Smuzhiyun if (ohci->disabled) {
527*4882a593Smuzhiyun err("sohci_submit_job: EPIPE");
528*4882a593Smuzhiyun return -1;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun /* we're about to begin a new transaction here so mark the
532*4882a593Smuzhiyun * URB unfinished */
533*4882a593Smuzhiyun urb->finished = 0;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /* every endpoint has a ed, locate and fill it */
536*4882a593Smuzhiyun ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
537*4882a593Smuzhiyun if (!ed) {
538*4882a593Smuzhiyun err("sohci_submit_job: ENOMEM");
539*4882a593Smuzhiyun return -1;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun /* for the private part of the URB we need the number of TDs (size) */
543*4882a593Smuzhiyun switch (usb_pipetype(pipe)) {
544*4882a593Smuzhiyun case PIPE_BULK: /* one TD for every 4096 Byte */
545*4882a593Smuzhiyun size = (transfer_len - 1) / 4096 + 1;
546*4882a593Smuzhiyun break;
547*4882a593Smuzhiyun case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
548*4882a593Smuzhiyun size = (transfer_len == 0)? 2:
549*4882a593Smuzhiyun (transfer_len - 1) / 4096 + 3;
550*4882a593Smuzhiyun break;
551*4882a593Smuzhiyun case PIPE_INTERRUPT: /* 1 TD */
552*4882a593Smuzhiyun size = 1;
553*4882a593Smuzhiyun break;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun ed->purb = urb;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun if (size >= (N_URB_TD - 1)) {
559*4882a593Smuzhiyun err("need %d TDs, only have %d", size, N_URB_TD);
560*4882a593Smuzhiyun return -1;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun purb_priv->pipe = pipe;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /* fill the private part of the URB */
565*4882a593Smuzhiyun purb_priv->length = size;
566*4882a593Smuzhiyun purb_priv->ed = ed;
567*4882a593Smuzhiyun purb_priv->actual_length = 0;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* allocate the TDs */
570*4882a593Smuzhiyun /* note that td[0] was allocated in ep_add_ed */
571*4882a593Smuzhiyun for (i = 0; i < size; i++) {
572*4882a593Smuzhiyun purb_priv->td[i] = td_alloc(ohci_dev, dev);
573*4882a593Smuzhiyun if (!purb_priv->td[i]) {
574*4882a593Smuzhiyun purb_priv->length = i;
575*4882a593Smuzhiyun urb_free_priv(purb_priv);
576*4882a593Smuzhiyun err("sohci_submit_job: ENOMEM");
577*4882a593Smuzhiyun return -1;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
582*4882a593Smuzhiyun urb_free_priv(purb_priv);
583*4882a593Smuzhiyun err("sohci_submit_job: EINVAL");
584*4882a593Smuzhiyun return -1;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun /* link the ed into a chain if is not already */
588*4882a593Smuzhiyun if (ed->state != ED_OPER)
589*4882a593Smuzhiyun ep_link(ohci, ed);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun /* fill the TDs and link it to the ed */
592*4882a593Smuzhiyun td_submit_job(ohci, dev, pipe, buffer, transfer_len,
593*4882a593Smuzhiyun setup, purb_priv, interval);
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun return 0;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun #ifdef DEBUG
601*4882a593Smuzhiyun /* tell us the current USB frame number */
sohci_get_current_frame_number(ohci_t * ohci)602*4882a593Smuzhiyun static int sohci_get_current_frame_number(ohci_t *ohci)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun invalidate_dcache_hcca(ohci->hcca);
605*4882a593Smuzhiyun return m16_swap(ohci->hcca->frame_no);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun #endif
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
610*4882a593Smuzhiyun * ED handling functions
611*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun /* search for the right branch to insert an interrupt ed into the int tree
614*4882a593Smuzhiyun * do some load ballancing;
615*4882a593Smuzhiyun * returns the branch and
616*4882a593Smuzhiyun * sets the interval to interval = 2^integer (ld (interval)) */
617*4882a593Smuzhiyun
ep_int_ballance(ohci_t * ohci,int interval,int load)618*4882a593Smuzhiyun static int ep_int_ballance(ohci_t *ohci, int interval, int load)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun int i, branch = 0;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun /* search for the least loaded interrupt endpoint
623*4882a593Smuzhiyun * branch of all 32 branches
624*4882a593Smuzhiyun */
625*4882a593Smuzhiyun for (i = 0; i < 32; i++)
626*4882a593Smuzhiyun if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
627*4882a593Smuzhiyun branch = i;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun branch = branch % interval;
630*4882a593Smuzhiyun for (i = branch; i < 32; i += interval)
631*4882a593Smuzhiyun ohci->ohci_int_load [i] += load;
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun return branch;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /* 2^int( ld (inter)) */
639*4882a593Smuzhiyun
ep_2_n_interval(int inter)640*4882a593Smuzhiyun static int ep_2_n_interval(int inter)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun int i;
643*4882a593Smuzhiyun for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
644*4882a593Smuzhiyun return 1 << i;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* the int tree is a binary tree
650*4882a593Smuzhiyun * in order to process it sequentially the indexes of the branches have to
651*4882a593Smuzhiyun * be mapped the mapping reverses the bits of a word of num_bits length */
ep_rev(int num_bits,int word)652*4882a593Smuzhiyun static int ep_rev(int num_bits, int word)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun int i, wout = 0;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun for (i = 0; i < num_bits; i++)
657*4882a593Smuzhiyun wout |= (((word >> i) & 1) << (num_bits - i - 1));
658*4882a593Smuzhiyun return wout;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
662*4882a593Smuzhiyun * ED handling functions
663*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun /* link an ed into one of the HC chains */
666*4882a593Smuzhiyun
ep_link(ohci_t * ohci,ed_t * edi)667*4882a593Smuzhiyun static int ep_link(ohci_t *ohci, ed_t *edi)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun volatile ed_t *ed = edi;
670*4882a593Smuzhiyun int int_branch;
671*4882a593Smuzhiyun int i;
672*4882a593Smuzhiyun int inter;
673*4882a593Smuzhiyun int interval;
674*4882a593Smuzhiyun int load;
675*4882a593Smuzhiyun __u32 *ed_p;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun ed->state = ED_OPER;
678*4882a593Smuzhiyun ed->int_interval = 0;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun switch (ed->type) {
681*4882a593Smuzhiyun case PIPE_CONTROL:
682*4882a593Smuzhiyun ed->hwNextED = 0;
683*4882a593Smuzhiyun flush_dcache_ed(ed);
684*4882a593Smuzhiyun if (ohci->ed_controltail == NULL)
685*4882a593Smuzhiyun ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
686*4882a593Smuzhiyun else
687*4882a593Smuzhiyun ohci->ed_controltail->hwNextED =
688*4882a593Smuzhiyun m32_swap((unsigned long)ed);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun ed->ed_prev = ohci->ed_controltail;
691*4882a593Smuzhiyun if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
692*4882a593Smuzhiyun !ohci->ed_rm_list[1] && !ohci->sleeping) {
693*4882a593Smuzhiyun ohci->hc_control |= OHCI_CTRL_CLE;
694*4882a593Smuzhiyun ohci_writel(ohci->hc_control, &ohci->regs->control);
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun ohci->ed_controltail = edi;
697*4882a593Smuzhiyun break;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun case PIPE_BULK:
700*4882a593Smuzhiyun ed->hwNextED = 0;
701*4882a593Smuzhiyun flush_dcache_ed(ed);
702*4882a593Smuzhiyun if (ohci->ed_bulktail == NULL)
703*4882a593Smuzhiyun ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
704*4882a593Smuzhiyun else
705*4882a593Smuzhiyun ohci->ed_bulktail->hwNextED =
706*4882a593Smuzhiyun m32_swap((unsigned long)ed);
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun ed->ed_prev = ohci->ed_bulktail;
709*4882a593Smuzhiyun if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
710*4882a593Smuzhiyun !ohci->ed_rm_list[1] && !ohci->sleeping) {
711*4882a593Smuzhiyun ohci->hc_control |= OHCI_CTRL_BLE;
712*4882a593Smuzhiyun ohci_writel(ohci->hc_control, &ohci->regs->control);
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun ohci->ed_bulktail = edi;
715*4882a593Smuzhiyun break;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun case PIPE_INTERRUPT:
718*4882a593Smuzhiyun load = ed->int_load;
719*4882a593Smuzhiyun interval = ep_2_n_interval(ed->int_period);
720*4882a593Smuzhiyun ed->int_interval = interval;
721*4882a593Smuzhiyun int_branch = ep_int_ballance(ohci, interval, load);
722*4882a593Smuzhiyun ed->int_branch = int_branch;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun for (i = 0; i < ep_rev(6, interval); i += inter) {
725*4882a593Smuzhiyun inter = 1;
726*4882a593Smuzhiyun for (ed_p = &(ohci->hcca->int_table[\
727*4882a593Smuzhiyun ep_rev(5, i) + int_branch]);
728*4882a593Smuzhiyun (*ed_p != 0) &&
729*4882a593Smuzhiyun (((ed_t *)ed_p)->int_interval >= interval);
730*4882a593Smuzhiyun ed_p = &(((ed_t *)ed_p)->hwNextED))
731*4882a593Smuzhiyun inter = ep_rev(6,
732*4882a593Smuzhiyun ((ed_t *)ed_p)->int_interval);
733*4882a593Smuzhiyun ed->hwNextED = *ed_p;
734*4882a593Smuzhiyun flush_dcache_ed(ed);
735*4882a593Smuzhiyun *ed_p = m32_swap((unsigned long)ed);
736*4882a593Smuzhiyun flush_dcache_hcca(ohci->hcca);
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun break;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun return 0;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun /* scan the periodic table to find and unlink this ED */
periodic_unlink(struct ohci * ohci,volatile struct ed * ed,unsigned index,unsigned period)746*4882a593Smuzhiyun static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
747*4882a593Smuzhiyun unsigned index, unsigned period)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun __maybe_unused unsigned long aligned_ed_p;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun for (; index < NUM_INTS; index += period) {
752*4882a593Smuzhiyun __u32 *ed_p = &ohci->hcca->int_table [index];
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /* ED might have been unlinked through another path */
755*4882a593Smuzhiyun while (*ed_p != 0) {
756*4882a593Smuzhiyun if (((struct ed *)(uintptr_t)
757*4882a593Smuzhiyun m32_swap((unsigned long)ed_p)) == ed) {
758*4882a593Smuzhiyun *ed_p = ed->hwNextED;
759*4882a593Smuzhiyun aligned_ed_p = (unsigned long)ed_p;
760*4882a593Smuzhiyun aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
761*4882a593Smuzhiyun flush_dcache_range(aligned_ed_p,
762*4882a593Smuzhiyun aligned_ed_p + ARCH_DMA_MINALIGN);
763*4882a593Smuzhiyun break;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun ed_p = &(((struct ed *)(uintptr_t)
766*4882a593Smuzhiyun m32_swap((unsigned long)ed_p))->hwNextED);
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun /* unlink an ed from one of the HC chains.
772*4882a593Smuzhiyun * just the link to the ed is unlinked.
773*4882a593Smuzhiyun * the link from the ed still points to another operational ed or 0
774*4882a593Smuzhiyun * so the HC can eventually finish the processing of the unlinked ed */
775*4882a593Smuzhiyun
ep_unlink(ohci_t * ohci,ed_t * edi)776*4882a593Smuzhiyun static int ep_unlink(ohci_t *ohci, ed_t *edi)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun volatile ed_t *ed = edi;
779*4882a593Smuzhiyun int i;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
782*4882a593Smuzhiyun flush_dcache_ed(ed);
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun switch (ed->type) {
785*4882a593Smuzhiyun case PIPE_CONTROL:
786*4882a593Smuzhiyun if (ed->ed_prev == NULL) {
787*4882a593Smuzhiyun if (!ed->hwNextED) {
788*4882a593Smuzhiyun ohci->hc_control &= ~OHCI_CTRL_CLE;
789*4882a593Smuzhiyun ohci_writel(ohci->hc_control,
790*4882a593Smuzhiyun &ohci->regs->control);
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
793*4882a593Smuzhiyun &ohci->regs->ed_controlhead);
794*4882a593Smuzhiyun } else {
795*4882a593Smuzhiyun ed->ed_prev->hwNextED = ed->hwNextED;
796*4882a593Smuzhiyun flush_dcache_ed(ed->ed_prev);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun if (ohci->ed_controltail == ed) {
799*4882a593Smuzhiyun ohci->ed_controltail = ed->ed_prev;
800*4882a593Smuzhiyun } else {
801*4882a593Smuzhiyun ((ed_t *)(uintptr_t)m32_swap(
802*4882a593Smuzhiyun *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun break;
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun case PIPE_BULK:
807*4882a593Smuzhiyun if (ed->ed_prev == NULL) {
808*4882a593Smuzhiyun if (!ed->hwNextED) {
809*4882a593Smuzhiyun ohci->hc_control &= ~OHCI_CTRL_BLE;
810*4882a593Smuzhiyun ohci_writel(ohci->hc_control,
811*4882a593Smuzhiyun &ohci->regs->control);
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
814*4882a593Smuzhiyun &ohci->regs->ed_bulkhead);
815*4882a593Smuzhiyun } else {
816*4882a593Smuzhiyun ed->ed_prev->hwNextED = ed->hwNextED;
817*4882a593Smuzhiyun flush_dcache_ed(ed->ed_prev);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun if (ohci->ed_bulktail == ed) {
820*4882a593Smuzhiyun ohci->ed_bulktail = ed->ed_prev;
821*4882a593Smuzhiyun } else {
822*4882a593Smuzhiyun ((ed_t *)(uintptr_t)m32_swap(
823*4882a593Smuzhiyun *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun break;
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun case PIPE_INTERRUPT:
828*4882a593Smuzhiyun periodic_unlink(ohci, ed, 0, 1);
829*4882a593Smuzhiyun for (i = ed->int_branch; i < 32; i += ed->int_interval)
830*4882a593Smuzhiyun ohci->ohci_int_load[i] -= ed->int_load;
831*4882a593Smuzhiyun break;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun ed->state = ED_UNLINK;
834*4882a593Smuzhiyun return 0;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun /* add/reinit an endpoint; this should be done once at the
840*4882a593Smuzhiyun * usb_set_configuration command, but the USB stack is a little bit
841*4882a593Smuzhiyun * stateless so we do it at every transaction if the state of the ed
842*4882a593Smuzhiyun * is ED_NEW then a dummy td is added and the state is changed to
843*4882a593Smuzhiyun * ED_UNLINK in all other cases the state is left unchanged the ed
844*4882a593Smuzhiyun * info fields are setted anyway even though most of them should not
845*4882a593Smuzhiyun * change
846*4882a593Smuzhiyun */
ep_add_ed(ohci_dev_t * ohci_dev,struct usb_device * usb_dev,unsigned long pipe,int interval,int load)847*4882a593Smuzhiyun static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
848*4882a593Smuzhiyun unsigned long pipe, int interval, int load)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun td_t *td;
851*4882a593Smuzhiyun ed_t *ed_ret;
852*4882a593Smuzhiyun volatile ed_t *ed;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
855*4882a593Smuzhiyun (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
858*4882a593Smuzhiyun err("ep_add_ed: pending delete");
859*4882a593Smuzhiyun /* pending delete request */
860*4882a593Smuzhiyun return NULL;
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun if (ed->state == ED_NEW) {
864*4882a593Smuzhiyun /* dummy td; end of td list for ed */
865*4882a593Smuzhiyun td = td_alloc(ohci_dev, usb_dev);
866*4882a593Smuzhiyun ed->hwTailP = m32_swap((unsigned long)td);
867*4882a593Smuzhiyun ed->hwHeadP = ed->hwTailP;
868*4882a593Smuzhiyun ed->state = ED_UNLINK;
869*4882a593Smuzhiyun ed->type = usb_pipetype(pipe);
870*4882a593Smuzhiyun ohci_dev->ed_cnt++;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun ed->hwINFO = m32_swap(usb_pipedevice(pipe)
874*4882a593Smuzhiyun | usb_pipeendpoint(pipe) << 7
875*4882a593Smuzhiyun | (usb_pipeisoc(pipe)? 0x8000: 0)
876*4882a593Smuzhiyun | (usb_pipecontrol(pipe)? 0: \
877*4882a593Smuzhiyun (usb_pipeout(pipe)? 0x800: 0x1000))
878*4882a593Smuzhiyun | (usb_dev->speed == USB_SPEED_LOW) << 13
879*4882a593Smuzhiyun | usb_maxpacket(usb_dev, pipe) << 16);
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
882*4882a593Smuzhiyun ed->int_period = interval;
883*4882a593Smuzhiyun ed->int_load = load;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun flush_dcache_ed(ed);
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun return ed_ret;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
892*4882a593Smuzhiyun * TD handling functions
893*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
896*4882a593Smuzhiyun
td_fill(ohci_t * ohci,unsigned int info,void * data,int len,struct usb_device * dev,int index,urb_priv_t * urb_priv)897*4882a593Smuzhiyun static void td_fill(ohci_t *ohci, unsigned int info,
898*4882a593Smuzhiyun void *data, int len,
899*4882a593Smuzhiyun struct usb_device *dev, int index, urb_priv_t *urb_priv)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun volatile td_t *td, *td_pt;
902*4882a593Smuzhiyun #ifdef OHCI_FILL_TRACE
903*4882a593Smuzhiyun int i;
904*4882a593Smuzhiyun #endif
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun if (index > urb_priv->length) {
907*4882a593Smuzhiyun err("index > length");
908*4882a593Smuzhiyun return;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun /* use this td as the next dummy */
911*4882a593Smuzhiyun td_pt = urb_priv->td [index];
912*4882a593Smuzhiyun td_pt->hwNextTD = 0;
913*4882a593Smuzhiyun flush_dcache_td(td_pt);
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /* fill the old dummy TD */
916*4882a593Smuzhiyun td = urb_priv->td [index] =
917*4882a593Smuzhiyun (td_t *)(uintptr_t)
918*4882a593Smuzhiyun (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun td->ed = urb_priv->ed;
921*4882a593Smuzhiyun td->next_dl_td = NULL;
922*4882a593Smuzhiyun td->index = index;
923*4882a593Smuzhiyun td->data = (uintptr_t)data;
924*4882a593Smuzhiyun #ifdef OHCI_FILL_TRACE
925*4882a593Smuzhiyun if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
926*4882a593Smuzhiyun for (i = 0; i < len; i++)
927*4882a593Smuzhiyun printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
928*4882a593Smuzhiyun printf("\n");
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun #endif
931*4882a593Smuzhiyun if (!len)
932*4882a593Smuzhiyun data = 0;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun td->hwINFO = m32_swap(info);
935*4882a593Smuzhiyun td->hwCBP = m32_swap((unsigned long)data);
936*4882a593Smuzhiyun if (data)
937*4882a593Smuzhiyun td->hwBE = m32_swap((unsigned long)(data + len - 1));
938*4882a593Smuzhiyun else
939*4882a593Smuzhiyun td->hwBE = 0;
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun td->hwNextTD = m32_swap((unsigned long)td_pt);
942*4882a593Smuzhiyun flush_dcache_td(td);
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun /* append to queue */
945*4882a593Smuzhiyun td->ed->hwTailP = td->hwNextTD;
946*4882a593Smuzhiyun flush_dcache_ed(td->ed);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /* prepare all TDs of a transfer */
952*4882a593Smuzhiyun
td_submit_job(ohci_t * ohci,struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * setup,urb_priv_t * urb,int interval)953*4882a593Smuzhiyun static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
954*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
955*4882a593Smuzhiyun struct devrequest *setup, urb_priv_t *urb,
956*4882a593Smuzhiyun int interval)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun int data_len = transfer_len;
959*4882a593Smuzhiyun void *data;
960*4882a593Smuzhiyun int cnt = 0;
961*4882a593Smuzhiyun __u32 info = 0;
962*4882a593Smuzhiyun unsigned int toggle = 0;
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun flush_dcache_buffer(buffer, data_len);
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
967*4882a593Smuzhiyun * bits for resetting */
968*4882a593Smuzhiyun if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
969*4882a593Smuzhiyun toggle = TD_T_TOGGLE;
970*4882a593Smuzhiyun } else {
971*4882a593Smuzhiyun toggle = TD_T_DATA0;
972*4882a593Smuzhiyun usb_settoggle(dev, usb_pipeendpoint(pipe),
973*4882a593Smuzhiyun usb_pipeout(pipe), 1);
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun urb->td_cnt = 0;
976*4882a593Smuzhiyun if (data_len)
977*4882a593Smuzhiyun data = buffer;
978*4882a593Smuzhiyun else
979*4882a593Smuzhiyun data = 0;
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun switch (usb_pipetype(pipe)) {
982*4882a593Smuzhiyun case PIPE_BULK:
983*4882a593Smuzhiyun info = usb_pipeout(pipe)?
984*4882a593Smuzhiyun TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
985*4882a593Smuzhiyun while (data_len > 4096) {
986*4882a593Smuzhiyun td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
987*4882a593Smuzhiyun data, 4096, dev, cnt, urb);
988*4882a593Smuzhiyun data += 4096; data_len -= 4096; cnt++;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun info = usb_pipeout(pipe)?
991*4882a593Smuzhiyun TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
992*4882a593Smuzhiyun td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
993*4882a593Smuzhiyun data_len, dev, cnt, urb);
994*4882a593Smuzhiyun cnt++;
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun if (!ohci->sleeping) {
997*4882a593Smuzhiyun /* start bulk list */
998*4882a593Smuzhiyun ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun break;
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun case PIPE_CONTROL:
1003*4882a593Smuzhiyun /* Setup phase */
1004*4882a593Smuzhiyun info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1005*4882a593Smuzhiyun flush_dcache_buffer(setup, 8);
1006*4882a593Smuzhiyun td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun /* Optional Data phase */
1009*4882a593Smuzhiyun if (data_len > 0) {
1010*4882a593Smuzhiyun info = usb_pipeout(pipe)?
1011*4882a593Smuzhiyun TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1012*4882a593Smuzhiyun TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1013*4882a593Smuzhiyun /* NOTE: mishandles transfers >8K, some >4K */
1014*4882a593Smuzhiyun td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun /* Status phase */
1018*4882a593Smuzhiyun info = (usb_pipeout(pipe) || data_len == 0) ?
1019*4882a593Smuzhiyun TD_CC | TD_DP_IN | TD_T_DATA1:
1020*4882a593Smuzhiyun TD_CC | TD_DP_OUT | TD_T_DATA1;
1021*4882a593Smuzhiyun td_fill(ohci, info, data, 0, dev, cnt++, urb);
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun if (!ohci->sleeping) {
1024*4882a593Smuzhiyun /* start Control list */
1025*4882a593Smuzhiyun ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun break;
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun case PIPE_INTERRUPT:
1030*4882a593Smuzhiyun info = usb_pipeout(urb->pipe)?
1031*4882a593Smuzhiyun TD_CC | TD_DP_OUT | toggle:
1032*4882a593Smuzhiyun TD_CC | TD_R | TD_DP_IN | toggle;
1033*4882a593Smuzhiyun td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1034*4882a593Smuzhiyun break;
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun if (urb->length != cnt)
1037*4882a593Smuzhiyun dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
1041*4882a593Smuzhiyun * Done List handling functions
1042*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun /* calculate the transfer length and update the urb */
1045*4882a593Smuzhiyun
dl_transfer_length(td_t * td)1046*4882a593Smuzhiyun static void dl_transfer_length(td_t *td)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun __u32 tdBE, tdCBP;
1049*4882a593Smuzhiyun urb_priv_t *lurb_priv = td->ed->purb;
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun tdBE = m32_swap(td->hwBE);
1052*4882a593Smuzhiyun tdCBP = m32_swap(td->hwCBP);
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun if (!(usb_pipecontrol(lurb_priv->pipe) &&
1055*4882a593Smuzhiyun ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1056*4882a593Smuzhiyun if (tdBE != 0) {
1057*4882a593Smuzhiyun if (td->hwCBP == 0)
1058*4882a593Smuzhiyun lurb_priv->actual_length += tdBE - td->data + 1;
1059*4882a593Smuzhiyun else
1060*4882a593Smuzhiyun lurb_priv->actual_length += tdCBP - td->data;
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
check_status(td_t * td_list)1066*4882a593Smuzhiyun static void check_status(td_t *td_list)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun urb_priv_t *lurb_priv = td_list->ed->purb;
1069*4882a593Smuzhiyun int urb_len = lurb_priv->length;
1070*4882a593Smuzhiyun __u32 *phwHeadP = &td_list->ed->hwHeadP;
1071*4882a593Smuzhiyun int cc;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1074*4882a593Smuzhiyun if (cc) {
1075*4882a593Smuzhiyun err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun invalidate_dcache_ed(td_list->ed);
1078*4882a593Smuzhiyun if (*phwHeadP & m32_swap(0x1)) {
1079*4882a593Smuzhiyun if (lurb_priv &&
1080*4882a593Smuzhiyun ((td_list->index + 1) < urb_len)) {
1081*4882a593Smuzhiyun *phwHeadP =
1082*4882a593Smuzhiyun (lurb_priv->td[urb_len - 1]->hwNextTD &\
1083*4882a593Smuzhiyun m32_swap(0xfffffff0)) |
1084*4882a593Smuzhiyun (*phwHeadP & m32_swap(0x2));
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun lurb_priv->td_cnt += urb_len -
1087*4882a593Smuzhiyun td_list->index - 1;
1088*4882a593Smuzhiyun } else
1089*4882a593Smuzhiyun *phwHeadP &= m32_swap(0xfffffff2);
1090*4882a593Smuzhiyun flush_dcache_ed(td_list->ed);
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun /* replies to the request have to be on a FIFO basis so
1096*4882a593Smuzhiyun * we reverse the reversed done-list */
dl_reverse_done_list(ohci_t * ohci)1097*4882a593Smuzhiyun static td_t *dl_reverse_done_list(ohci_t *ohci)
1098*4882a593Smuzhiyun {
1099*4882a593Smuzhiyun uintptr_t td_list_hc;
1100*4882a593Smuzhiyun td_t *td_rev = NULL;
1101*4882a593Smuzhiyun td_t *td_list = NULL;
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun invalidate_dcache_hcca(ohci->hcca);
1104*4882a593Smuzhiyun td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
1105*4882a593Smuzhiyun ohci->hcca->done_head = 0;
1106*4882a593Smuzhiyun flush_dcache_hcca(ohci->hcca);
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun while (td_list_hc) {
1109*4882a593Smuzhiyun td_list = (td_t *)td_list_hc;
1110*4882a593Smuzhiyun invalidate_dcache_td(td_list);
1111*4882a593Smuzhiyun check_status(td_list);
1112*4882a593Smuzhiyun td_list->next_dl_td = td_rev;
1113*4882a593Smuzhiyun td_rev = td_list;
1114*4882a593Smuzhiyun td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun return td_list;
1117*4882a593Smuzhiyun }
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1120*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1121*4882a593Smuzhiyun
finish_urb(ohci_t * ohci,urb_priv_t * urb,int status)1122*4882a593Smuzhiyun static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1123*4882a593Smuzhiyun {
1124*4882a593Smuzhiyun if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1125*4882a593Smuzhiyun urb->finished = 1;
1126*4882a593Smuzhiyun else
1127*4882a593Smuzhiyun dbg("finish_urb: strange.., ED state %x, \n", status);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun /*
1131*4882a593Smuzhiyun * Used to take back a TD from the host controller. This would normally be
1132*4882a593Smuzhiyun * called from within dl_done_list, however it may be called directly if the
1133*4882a593Smuzhiyun * HC no longer sees the TD and it has not appeared on the donelist (after
1134*4882a593Smuzhiyun * two frames). This bug has been observed on ZF Micro systems.
1135*4882a593Smuzhiyun */
takeback_td(ohci_t * ohci,td_t * td_list)1136*4882a593Smuzhiyun static int takeback_td(ohci_t *ohci, td_t *td_list)
1137*4882a593Smuzhiyun {
1138*4882a593Smuzhiyun ed_t *ed;
1139*4882a593Smuzhiyun int cc;
1140*4882a593Smuzhiyun int stat = 0;
1141*4882a593Smuzhiyun /* urb_t *urb; */
1142*4882a593Smuzhiyun urb_priv_t *lurb_priv;
1143*4882a593Smuzhiyun __u32 tdINFO, edHeadP, edTailP;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun invalidate_dcache_td(td_list);
1146*4882a593Smuzhiyun tdINFO = m32_swap(td_list->hwINFO);
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun ed = td_list->ed;
1149*4882a593Smuzhiyun lurb_priv = ed->purb;
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun dl_transfer_length(td_list);
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun lurb_priv->td_cnt++;
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun /* error code of transfer */
1156*4882a593Smuzhiyun cc = TD_CC_GET(tdINFO);
1157*4882a593Smuzhiyun if (cc) {
1158*4882a593Smuzhiyun err("USB-error: %s (%x)", cc_to_string[cc], cc);
1159*4882a593Smuzhiyun stat = cc_to_error[cc];
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun /* see if this done list makes for all TD's of current URB,
1163*4882a593Smuzhiyun * and mark the URB finished if so */
1164*4882a593Smuzhiyun if (lurb_priv->td_cnt == lurb_priv->length)
1165*4882a593Smuzhiyun finish_urb(ohci, lurb_priv, ed->state);
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun dbg("dl_done_list: processing TD %x, len %x\n",
1168*4882a593Smuzhiyun lurb_priv->td_cnt, lurb_priv->length);
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
1171*4882a593Smuzhiyun invalidate_dcache_ed(ed);
1172*4882a593Smuzhiyun edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1173*4882a593Smuzhiyun edTailP = m32_swap(ed->hwTailP);
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun /* unlink eds if they are not busy */
1176*4882a593Smuzhiyun if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1177*4882a593Smuzhiyun ep_unlink(ohci, ed);
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun return stat;
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
dl_done_list(ohci_t * ohci)1182*4882a593Smuzhiyun static int dl_done_list(ohci_t *ohci)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun int stat = 0;
1185*4882a593Smuzhiyun td_t *td_list = dl_reverse_done_list(ohci);
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun while (td_list) {
1188*4882a593Smuzhiyun td_t *td_next = td_list->next_dl_td;
1189*4882a593Smuzhiyun stat = takeback_td(ohci, td_list);
1190*4882a593Smuzhiyun td_list = td_next;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun return stat;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
1196*4882a593Smuzhiyun * Virtual Root Hub
1197*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun #include <usbroothubdes.h>
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun /* Hub class-specific descriptor is constructed dynamically */
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun #define OK(x) len = (x); break
1206*4882a593Smuzhiyun #ifdef DEBUG
1207*4882a593Smuzhiyun #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1208*4882a593Smuzhiyun &ohci->regs->roothub.status); }
1209*4882a593Smuzhiyun #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1210*4882a593Smuzhiyun (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
1211*4882a593Smuzhiyun #else
1212*4882a593Smuzhiyun #define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
1213*4882a593Smuzhiyun #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1214*4882a593Smuzhiyun &ohci->regs->roothub.portstatus[wIndex-1])
1215*4882a593Smuzhiyun #endif
1216*4882a593Smuzhiyun #define RD_RH_STAT roothub_status(ohci)
1217*4882a593Smuzhiyun #define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun /* request to virtual root hub */
1220*4882a593Smuzhiyun
rh_check_port_status(ohci_t * controller)1221*4882a593Smuzhiyun int rh_check_port_status(ohci_t *controller)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun __u32 temp, ndp, i;
1224*4882a593Smuzhiyun int res;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun res = -1;
1227*4882a593Smuzhiyun temp = roothub_a(controller);
1228*4882a593Smuzhiyun ndp = (temp & RH_A_NDP);
1229*4882a593Smuzhiyun #ifdef CONFIG_AT91C_PQFP_UHPBUG
1230*4882a593Smuzhiyun ndp = (ndp == 2) ? 1:0;
1231*4882a593Smuzhiyun #endif
1232*4882a593Smuzhiyun for (i = 0; i < ndp; i++) {
1233*4882a593Smuzhiyun temp = roothub_portstatus(controller, i);
1234*4882a593Smuzhiyun /* check for a device disconnect */
1235*4882a593Smuzhiyun if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1236*4882a593Smuzhiyun (RH_PS_PESC | RH_PS_CSC)) &&
1237*4882a593Smuzhiyun ((temp & RH_PS_CCS) == 0)) {
1238*4882a593Smuzhiyun res = i;
1239*4882a593Smuzhiyun break;
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun }
1242*4882a593Smuzhiyun return res;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun
ohci_submit_rh_msg(ohci_t * ohci,struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * cmd)1245*4882a593Smuzhiyun static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1246*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
1247*4882a593Smuzhiyun struct devrequest *cmd)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun void *data = buffer;
1250*4882a593Smuzhiyun int leni = transfer_len;
1251*4882a593Smuzhiyun int len = 0;
1252*4882a593Smuzhiyun int stat = 0;
1253*4882a593Smuzhiyun __u16 bmRType_bReq;
1254*4882a593Smuzhiyun __u16 wValue;
1255*4882a593Smuzhiyun __u16 wIndex;
1256*4882a593Smuzhiyun __u16 wLength;
1257*4882a593Smuzhiyun ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun #ifdef DEBUG
1260*4882a593Smuzhiyun pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1261*4882a593Smuzhiyun cmd, "SUB(rh)", usb_pipein(pipe));
1262*4882a593Smuzhiyun #else
1263*4882a593Smuzhiyun ohci_mdelay(1);
1264*4882a593Smuzhiyun #endif
1265*4882a593Smuzhiyun if (usb_pipeint(pipe)) {
1266*4882a593Smuzhiyun info("Root-Hub submit IRQ: NOT implemented");
1267*4882a593Smuzhiyun return 0;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1271*4882a593Smuzhiyun wValue = le16_to_cpu(cmd->value);
1272*4882a593Smuzhiyun wIndex = le16_to_cpu(cmd->index);
1273*4882a593Smuzhiyun wLength = le16_to_cpu(cmd->length);
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1276*4882a593Smuzhiyun dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun switch (bmRType_bReq) {
1279*4882a593Smuzhiyun /* Request Destination:
1280*4882a593Smuzhiyun without flags: Device,
1281*4882a593Smuzhiyun RH_INTERFACE: interface,
1282*4882a593Smuzhiyun RH_ENDPOINT: endpoint,
1283*4882a593Smuzhiyun RH_CLASS means HUB here,
1284*4882a593Smuzhiyun RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1285*4882a593Smuzhiyun */
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun case RH_GET_STATUS:
1288*4882a593Smuzhiyun *(u16 *)databuf = cpu_to_le16(1);
1289*4882a593Smuzhiyun OK(2);
1290*4882a593Smuzhiyun case RH_GET_STATUS | RH_INTERFACE:
1291*4882a593Smuzhiyun *(u16 *)databuf = cpu_to_le16(0);
1292*4882a593Smuzhiyun OK(2);
1293*4882a593Smuzhiyun case RH_GET_STATUS | RH_ENDPOINT:
1294*4882a593Smuzhiyun *(u16 *)databuf = cpu_to_le16(0);
1295*4882a593Smuzhiyun OK(2);
1296*4882a593Smuzhiyun case RH_GET_STATUS | RH_CLASS:
1297*4882a593Smuzhiyun *(u32 *)databuf = cpu_to_le32(
1298*4882a593Smuzhiyun RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1299*4882a593Smuzhiyun OK(4);
1300*4882a593Smuzhiyun case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1301*4882a593Smuzhiyun *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
1302*4882a593Smuzhiyun OK(4);
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun case RH_CLEAR_FEATURE | RH_ENDPOINT:
1305*4882a593Smuzhiyun switch (wValue) {
1306*4882a593Smuzhiyun case (RH_ENDPOINT_STALL):
1307*4882a593Smuzhiyun OK(0);
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun break;
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun case RH_CLEAR_FEATURE | RH_CLASS:
1312*4882a593Smuzhiyun switch (wValue) {
1313*4882a593Smuzhiyun case RH_C_HUB_LOCAL_POWER:
1314*4882a593Smuzhiyun OK(0);
1315*4882a593Smuzhiyun case (RH_C_HUB_OVER_CURRENT):
1316*4882a593Smuzhiyun WR_RH_STAT(RH_HS_OCIC);
1317*4882a593Smuzhiyun OK(0);
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun break;
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1322*4882a593Smuzhiyun switch (wValue) {
1323*4882a593Smuzhiyun case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1324*4882a593Smuzhiyun case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1325*4882a593Smuzhiyun case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1326*4882a593Smuzhiyun case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1327*4882a593Smuzhiyun case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1328*4882a593Smuzhiyun case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1329*4882a593Smuzhiyun case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1330*4882a593Smuzhiyun case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun break;
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1335*4882a593Smuzhiyun switch (wValue) {
1336*4882a593Smuzhiyun case (RH_PORT_SUSPEND):
1337*4882a593Smuzhiyun WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1338*4882a593Smuzhiyun case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1339*4882a593Smuzhiyun if (RD_RH_PORTSTAT & RH_PS_CCS)
1340*4882a593Smuzhiyun WR_RH_PORTSTAT(RH_PS_PRS);
1341*4882a593Smuzhiyun OK(0);
1342*4882a593Smuzhiyun case (RH_PORT_POWER):
1343*4882a593Smuzhiyun WR_RH_PORTSTAT(RH_PS_PPS);
1344*4882a593Smuzhiyun OK(0);
1345*4882a593Smuzhiyun case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1346*4882a593Smuzhiyun if (RD_RH_PORTSTAT & RH_PS_CCS)
1347*4882a593Smuzhiyun WR_RH_PORTSTAT(RH_PS_PES);
1348*4882a593Smuzhiyun OK(0);
1349*4882a593Smuzhiyun }
1350*4882a593Smuzhiyun break;
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun case RH_SET_ADDRESS:
1353*4882a593Smuzhiyun ohci->rh.devnum = wValue;
1354*4882a593Smuzhiyun OK(0);
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun case RH_GET_DESCRIPTOR:
1357*4882a593Smuzhiyun switch ((wValue & 0xff00) >> 8) {
1358*4882a593Smuzhiyun case (0x01): /* device descriptor */
1359*4882a593Smuzhiyun len = min_t(unsigned int,
1360*4882a593Smuzhiyun leni,
1361*4882a593Smuzhiyun min_t(unsigned int,
1362*4882a593Smuzhiyun sizeof(root_hub_dev_des),
1363*4882a593Smuzhiyun wLength));
1364*4882a593Smuzhiyun databuf = root_hub_dev_des; OK(len);
1365*4882a593Smuzhiyun case (0x02): /* configuration descriptor */
1366*4882a593Smuzhiyun len = min_t(unsigned int,
1367*4882a593Smuzhiyun leni,
1368*4882a593Smuzhiyun min_t(unsigned int,
1369*4882a593Smuzhiyun sizeof(root_hub_config_des),
1370*4882a593Smuzhiyun wLength));
1371*4882a593Smuzhiyun databuf = root_hub_config_des; OK(len);
1372*4882a593Smuzhiyun case (0x03): /* string descriptors */
1373*4882a593Smuzhiyun if (wValue == 0x0300) {
1374*4882a593Smuzhiyun len = min_t(unsigned int,
1375*4882a593Smuzhiyun leni,
1376*4882a593Smuzhiyun min_t(unsigned int,
1377*4882a593Smuzhiyun sizeof(root_hub_str_index0),
1378*4882a593Smuzhiyun wLength));
1379*4882a593Smuzhiyun databuf = root_hub_str_index0;
1380*4882a593Smuzhiyun OK(len);
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun if (wValue == 0x0301) {
1383*4882a593Smuzhiyun len = min_t(unsigned int,
1384*4882a593Smuzhiyun leni,
1385*4882a593Smuzhiyun min_t(unsigned int,
1386*4882a593Smuzhiyun sizeof(root_hub_str_index1),
1387*4882a593Smuzhiyun wLength));
1388*4882a593Smuzhiyun databuf = root_hub_str_index1;
1389*4882a593Smuzhiyun OK(len);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun default:
1392*4882a593Smuzhiyun stat = USB_ST_STALLED;
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun break;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun case RH_GET_DESCRIPTOR | RH_CLASS:
1397*4882a593Smuzhiyun {
1398*4882a593Smuzhiyun __u32 temp = roothub_a(ohci);
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun databuf[0] = 9; /* min length; */
1401*4882a593Smuzhiyun databuf[1] = 0x29;
1402*4882a593Smuzhiyun databuf[2] = temp & RH_A_NDP;
1403*4882a593Smuzhiyun #ifdef CONFIG_AT91C_PQFP_UHPBUG
1404*4882a593Smuzhiyun databuf[2] = (databuf[2] == 2) ? 1 : 0;
1405*4882a593Smuzhiyun #endif
1406*4882a593Smuzhiyun databuf[3] = 0;
1407*4882a593Smuzhiyun if (temp & RH_A_PSM) /* per-port power switching? */
1408*4882a593Smuzhiyun databuf[3] |= 0x1;
1409*4882a593Smuzhiyun if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1410*4882a593Smuzhiyun databuf[3] |= 0x10;
1411*4882a593Smuzhiyun else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
1412*4882a593Smuzhiyun databuf[3] |= 0x8;
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun databuf[4] = 0;
1415*4882a593Smuzhiyun databuf[5] = (temp & RH_A_POTPGT) >> 24;
1416*4882a593Smuzhiyun databuf[6] = 0;
1417*4882a593Smuzhiyun temp = roothub_b(ohci);
1418*4882a593Smuzhiyun databuf[7] = temp & RH_B_DR;
1419*4882a593Smuzhiyun if (databuf[2] < 7) {
1420*4882a593Smuzhiyun databuf[8] = 0xff;
1421*4882a593Smuzhiyun } else {
1422*4882a593Smuzhiyun databuf[0] += 2;
1423*4882a593Smuzhiyun databuf[8] = (temp & RH_B_DR) >> 8;
1424*4882a593Smuzhiyun databuf[10] = databuf[9] = 0xff;
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun len = min_t(unsigned int, leni,
1428*4882a593Smuzhiyun min_t(unsigned int, databuf[0], wLength));
1429*4882a593Smuzhiyun OK(len);
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun case RH_GET_CONFIGURATION:
1433*4882a593Smuzhiyun databuf[0] = 0x01;
1434*4882a593Smuzhiyun OK(1);
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun case RH_SET_CONFIGURATION:
1437*4882a593Smuzhiyun WR_RH_STAT(0x10000);
1438*4882a593Smuzhiyun OK(0);
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun default:
1441*4882a593Smuzhiyun dbg("unsupported root hub command");
1442*4882a593Smuzhiyun stat = USB_ST_STALLED;
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun
1445*4882a593Smuzhiyun #ifdef DEBUG
1446*4882a593Smuzhiyun ohci_dump_roothub(ohci, 1);
1447*4882a593Smuzhiyun #else
1448*4882a593Smuzhiyun ohci_mdelay(1);
1449*4882a593Smuzhiyun #endif
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun len = min_t(int, len, leni);
1452*4882a593Smuzhiyun if (data != databuf)
1453*4882a593Smuzhiyun memcpy(data, databuf, len);
1454*4882a593Smuzhiyun dev->act_len = len;
1455*4882a593Smuzhiyun dev->status = stat;
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun #ifdef DEBUG
1458*4882a593Smuzhiyun pkt_print(ohci, NULL, dev, pipe, buffer,
1459*4882a593Smuzhiyun transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1460*4882a593Smuzhiyun #else
1461*4882a593Smuzhiyun ohci_mdelay(1);
1462*4882a593Smuzhiyun #endif
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun return stat;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1468*4882a593Smuzhiyun
ohci_get_ohci_dev(ohci_t * ohci,int devnum,int intr)1469*4882a593Smuzhiyun static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun int i;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun if (!intr)
1474*4882a593Smuzhiyun return &ohci->ohci_dev;
1475*4882a593Smuzhiyun
1476*4882a593Smuzhiyun /* First see if we already have an ohci_dev for this dev. */
1477*4882a593Smuzhiyun for (i = 0; i < NUM_INT_DEVS; i++) {
1478*4882a593Smuzhiyun if (ohci->int_dev[i].devnum == devnum)
1479*4882a593Smuzhiyun return &ohci->int_dev[i];
1480*4882a593Smuzhiyun }
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun /* If not then find a free one. */
1483*4882a593Smuzhiyun for (i = 0; i < NUM_INT_DEVS; i++) {
1484*4882a593Smuzhiyun if (ohci->int_dev[i].devnum == -1) {
1485*4882a593Smuzhiyun ohci->int_dev[i].devnum = devnum;
1486*4882a593Smuzhiyun return &ohci->int_dev[i];
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1491*4882a593Smuzhiyun return NULL;
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun /* common code for handling submit messages - used for all but root hub */
1495*4882a593Smuzhiyun /* accesses. */
ohci_alloc_urb(struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,int interval)1496*4882a593Smuzhiyun static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1497*4882a593Smuzhiyun void *buffer, int transfer_len, int interval)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun urb_priv_t *urb;
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun urb = calloc(1, sizeof(urb_priv_t));
1502*4882a593Smuzhiyun if (!urb) {
1503*4882a593Smuzhiyun printf("ohci: Error out of memory allocating urb\n");
1504*4882a593Smuzhiyun return NULL;
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun urb->dev = dev;
1508*4882a593Smuzhiyun urb->pipe = pipe;
1509*4882a593Smuzhiyun urb->transfer_buffer = buffer;
1510*4882a593Smuzhiyun urb->transfer_buffer_length = transfer_len;
1511*4882a593Smuzhiyun urb->interval = interval;
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun return urb;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun
submit_common_msg(ohci_t * ohci,struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * setup,int interval)1516*4882a593Smuzhiyun static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1517*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
1518*4882a593Smuzhiyun struct devrequest *setup, int interval)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun int stat = 0;
1521*4882a593Smuzhiyun int maxsize = usb_maxpacket(dev, pipe);
1522*4882a593Smuzhiyun int timeout;
1523*4882a593Smuzhiyun urb_priv_t *urb;
1524*4882a593Smuzhiyun ohci_dev_t *ohci_dev;
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1527*4882a593Smuzhiyun if (!urb)
1528*4882a593Smuzhiyun return -ENOMEM;
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun #ifdef DEBUG
1531*4882a593Smuzhiyun urb->actual_length = 0;
1532*4882a593Smuzhiyun pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1533*4882a593Smuzhiyun setup, "SUB", usb_pipein(pipe));
1534*4882a593Smuzhiyun #else
1535*4882a593Smuzhiyun ohci_mdelay(1);
1536*4882a593Smuzhiyun #endif
1537*4882a593Smuzhiyun if (!maxsize) {
1538*4882a593Smuzhiyun err("submit_common_message: pipesize for pipe %lx is zero",
1539*4882a593Smuzhiyun pipe);
1540*4882a593Smuzhiyun return -1;
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1544*4882a593Smuzhiyun if (!ohci_dev)
1545*4882a593Smuzhiyun return -ENOMEM;
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
1548*4882a593Smuzhiyun err("sohci_submit_job failed");
1549*4882a593Smuzhiyun return -1;
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun mdelay(10);
1553*4882a593Smuzhiyun /* ohci_dump_status(ohci); */
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun timeout = USB_TIMEOUT_MS(pipe);
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun /* wait for it to complete */
1558*4882a593Smuzhiyun for (;;) {
1559*4882a593Smuzhiyun /* check whether the controller is done */
1560*4882a593Smuzhiyun stat = hc_interrupt(ohci);
1561*4882a593Smuzhiyun if (stat < 0) {
1562*4882a593Smuzhiyun stat = USB_ST_CRC_ERR;
1563*4882a593Smuzhiyun break;
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun /* NOTE: since we are not interrupt driven in U-Boot and always
1567*4882a593Smuzhiyun * handle only one URB at a time, we cannot assume the
1568*4882a593Smuzhiyun * transaction finished on the first successful return from
1569*4882a593Smuzhiyun * hc_interrupt().. unless the flag for current URB is set,
1570*4882a593Smuzhiyun * meaning that all TD's to/from device got actually
1571*4882a593Smuzhiyun * transferred and processed. If the current URB is not
1572*4882a593Smuzhiyun * finished we need to re-iterate this loop so as
1573*4882a593Smuzhiyun * hc_interrupt() gets called again as there needs to be some
1574*4882a593Smuzhiyun * more TD's to process still */
1575*4882a593Smuzhiyun if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1576*4882a593Smuzhiyun /* 0xff is returned for an SF-interrupt */
1577*4882a593Smuzhiyun break;
1578*4882a593Smuzhiyun }
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun if (--timeout) {
1581*4882a593Smuzhiyun mdelay(1);
1582*4882a593Smuzhiyun if (!urb->finished)
1583*4882a593Smuzhiyun dbg("*");
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun } else {
1586*4882a593Smuzhiyun if (!usb_pipeint(pipe))
1587*4882a593Smuzhiyun err("CTL:TIMEOUT ");
1588*4882a593Smuzhiyun dbg("submit_common_msg: TO status %x\n", stat);
1589*4882a593Smuzhiyun urb->finished = 1;
1590*4882a593Smuzhiyun stat = USB_ST_CRC_ERR;
1591*4882a593Smuzhiyun break;
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun dev->status = stat;
1596*4882a593Smuzhiyun dev->act_len = urb->actual_length;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1599*4882a593Smuzhiyun invalidate_dcache_buffer(buffer, dev->act_len);
1600*4882a593Smuzhiyun
1601*4882a593Smuzhiyun #ifdef DEBUG
1602*4882a593Smuzhiyun pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1603*4882a593Smuzhiyun setup, "RET(ctlr)", usb_pipein(pipe));
1604*4882a593Smuzhiyun #else
1605*4882a593Smuzhiyun ohci_mdelay(1);
1606*4882a593Smuzhiyun #endif
1607*4882a593Smuzhiyun urb_free_priv(urb);
1608*4882a593Smuzhiyun return 0;
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun #define MAX_INT_QUEUESIZE 8
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun struct int_queue {
1614*4882a593Smuzhiyun int queuesize;
1615*4882a593Smuzhiyun int curr_urb;
1616*4882a593Smuzhiyun urb_priv_t *urb[MAX_INT_QUEUESIZE];
1617*4882a593Smuzhiyun };
1618*4882a593Smuzhiyun
_ohci_create_int_queue(ohci_t * ohci,struct usb_device * udev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)1619*4882a593Smuzhiyun static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1620*4882a593Smuzhiyun struct usb_device *udev, unsigned long pipe, int queuesize,
1621*4882a593Smuzhiyun int elementsize, void *buffer, int interval)
1622*4882a593Smuzhiyun {
1623*4882a593Smuzhiyun struct int_queue *queue;
1624*4882a593Smuzhiyun ohci_dev_t *ohci_dev;
1625*4882a593Smuzhiyun int i;
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun if (queuesize > MAX_INT_QUEUESIZE)
1628*4882a593Smuzhiyun return NULL;
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1631*4882a593Smuzhiyun if (!ohci_dev)
1632*4882a593Smuzhiyun return NULL;
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun queue = malloc(sizeof(*queue));
1635*4882a593Smuzhiyun if (!queue) {
1636*4882a593Smuzhiyun printf("ohci: Error out of memory allocating int queue\n");
1637*4882a593Smuzhiyun return NULL;
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun for (i = 0; i < queuesize; i++) {
1641*4882a593Smuzhiyun queue->urb[i] = ohci_alloc_urb(udev, pipe,
1642*4882a593Smuzhiyun buffer + i * elementsize,
1643*4882a593Smuzhiyun elementsize, interval);
1644*4882a593Smuzhiyun if (!queue->urb[i])
1645*4882a593Smuzhiyun break;
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1648*4882a593Smuzhiyun printf("ohci: Error submitting int queue job\n");
1649*4882a593Smuzhiyun urb_free_priv(queue->urb[i]);
1650*4882a593Smuzhiyun break;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun if (i == 0) {
1654*4882a593Smuzhiyun /* We did not succeed in submitting even 1 urb */
1655*4882a593Smuzhiyun free(queue);
1656*4882a593Smuzhiyun return NULL;
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun queue->queuesize = i;
1660*4882a593Smuzhiyun queue->curr_urb = 0;
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun return queue;
1663*4882a593Smuzhiyun }
1664*4882a593Smuzhiyun
_ohci_poll_int_queue(ohci_t * ohci,struct usb_device * udev,struct int_queue * queue)1665*4882a593Smuzhiyun static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1666*4882a593Smuzhiyun struct int_queue *queue)
1667*4882a593Smuzhiyun {
1668*4882a593Smuzhiyun if (queue->curr_urb == queue->queuesize)
1669*4882a593Smuzhiyun return NULL; /* Queue depleted */
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun if (hc_interrupt(ohci) < 0)
1672*4882a593Smuzhiyun return NULL;
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun if (queue->urb[queue->curr_urb]->finished) {
1675*4882a593Smuzhiyun void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1676*4882a593Smuzhiyun queue->curr_urb++;
1677*4882a593Smuzhiyun return ret;
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun return NULL;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun
_ohci_destroy_int_queue(ohci_t * ohci,struct usb_device * dev,struct int_queue * queue)1683*4882a593Smuzhiyun static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1684*4882a593Smuzhiyun struct int_queue *queue)
1685*4882a593Smuzhiyun {
1686*4882a593Smuzhiyun int i;
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun for (i = 0; i < queue->queuesize; i++)
1689*4882a593Smuzhiyun urb_free_priv(queue->urb[i]);
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun free(queue);
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun return 0;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
1697*4882a593Smuzhiyun /* submit routines called from usb.c */
submit_bulk_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len)1698*4882a593Smuzhiyun int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1699*4882a593Smuzhiyun int transfer_len)
1700*4882a593Smuzhiyun {
1701*4882a593Smuzhiyun info("submit_bulk_msg");
1702*4882a593Smuzhiyun return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1703*4882a593Smuzhiyun NULL, 0);
1704*4882a593Smuzhiyun }
1705*4882a593Smuzhiyun
submit_int_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,int interval,bool nonblock)1706*4882a593Smuzhiyun int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1707*4882a593Smuzhiyun int transfer_len, int interval, bool nonblock)
1708*4882a593Smuzhiyun {
1709*4882a593Smuzhiyun info("submit_int_msg");
1710*4882a593Smuzhiyun return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1711*4882a593Smuzhiyun interval);
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun
create_int_queue(struct usb_device * dev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)1714*4882a593Smuzhiyun struct int_queue *create_int_queue(struct usb_device *dev,
1715*4882a593Smuzhiyun unsigned long pipe, int queuesize, int elementsize,
1716*4882a593Smuzhiyun void *buffer, int interval)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1719*4882a593Smuzhiyun elementsize, buffer, interval);
1720*4882a593Smuzhiyun }
1721*4882a593Smuzhiyun
poll_int_queue(struct usb_device * dev,struct int_queue * queue)1722*4882a593Smuzhiyun void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1723*4882a593Smuzhiyun {
1724*4882a593Smuzhiyun return _ohci_poll_int_queue(&gohci, dev, queue);
1725*4882a593Smuzhiyun }
1726*4882a593Smuzhiyun
destroy_int_queue(struct usb_device * dev,struct int_queue * queue)1727*4882a593Smuzhiyun int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun return _ohci_destroy_int_queue(&gohci, dev, queue);
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun #endif
1732*4882a593Smuzhiyun
_ohci_submit_control_msg(ohci_t * ohci,struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * setup)1733*4882a593Smuzhiyun static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1734*4882a593Smuzhiyun unsigned long pipe, void *buffer, int transfer_len,
1735*4882a593Smuzhiyun struct devrequest *setup)
1736*4882a593Smuzhiyun {
1737*4882a593Smuzhiyun int maxsize = usb_maxpacket(dev, pipe);
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun info("submit_control_msg");
1740*4882a593Smuzhiyun #ifdef DEBUG
1741*4882a593Smuzhiyun pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1742*4882a593Smuzhiyun setup, "SUB", usb_pipein(pipe));
1743*4882a593Smuzhiyun #else
1744*4882a593Smuzhiyun ohci_mdelay(1);
1745*4882a593Smuzhiyun #endif
1746*4882a593Smuzhiyun if (!maxsize) {
1747*4882a593Smuzhiyun err("submit_control_message: pipesize for pipe %lx is zero",
1748*4882a593Smuzhiyun pipe);
1749*4882a593Smuzhiyun return -1;
1750*4882a593Smuzhiyun }
1751*4882a593Smuzhiyun if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1752*4882a593Smuzhiyun ohci->rh.dev = dev;
1753*4882a593Smuzhiyun /* root hub - redirect */
1754*4882a593Smuzhiyun return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1755*4882a593Smuzhiyun transfer_len, setup);
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1759*4882a593Smuzhiyun setup, 0);
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun /*-------------------------------------------------------------------------*
1763*4882a593Smuzhiyun * HC functions
1764*4882a593Smuzhiyun *-------------------------------------------------------------------------*/
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun /* reset the HC and BUS */
1767*4882a593Smuzhiyun
hc_reset(ohci_t * ohci)1768*4882a593Smuzhiyun static int hc_reset(ohci_t *ohci)
1769*4882a593Smuzhiyun {
1770*4882a593Smuzhiyun #ifdef CONFIG_PCI_EHCI_DEVNO
1771*4882a593Smuzhiyun pci_dev_t pdev;
1772*4882a593Smuzhiyun #endif
1773*4882a593Smuzhiyun int timeout = 30;
1774*4882a593Smuzhiyun int smm_timeout = 50; /* 0,5 sec */
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun dbg("%s\n", __FUNCTION__);
1777*4882a593Smuzhiyun
1778*4882a593Smuzhiyun #ifdef CONFIG_PCI_EHCI_DEVNO
1779*4882a593Smuzhiyun /*
1780*4882a593Smuzhiyun * Some multi-function controllers (e.g. ISP1562) allow root hub
1781*4882a593Smuzhiyun * resetting via EHCI registers only.
1782*4882a593Smuzhiyun */
1783*4882a593Smuzhiyun pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1784*4882a593Smuzhiyun if (pdev != -1) {
1785*4882a593Smuzhiyun u32 base;
1786*4882a593Smuzhiyun int timeout = 1000;
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1789*4882a593Smuzhiyun base += EHCI_USBCMD_OFF;
1790*4882a593Smuzhiyun ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
1791*4882a593Smuzhiyun
1792*4882a593Smuzhiyun while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
1793*4882a593Smuzhiyun if (timeout-- <= 0) {
1794*4882a593Smuzhiyun printf("USB RootHub reset timed out!");
1795*4882a593Smuzhiyun break;
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun udelay(1);
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun } else
1800*4882a593Smuzhiyun printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1801*4882a593Smuzhiyun #endif
1802*4882a593Smuzhiyun if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1803*4882a593Smuzhiyun /* SMM owns the HC, request ownership */
1804*4882a593Smuzhiyun ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
1805*4882a593Smuzhiyun info("USB HC TakeOver from SMM");
1806*4882a593Smuzhiyun while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1807*4882a593Smuzhiyun mdelay(10);
1808*4882a593Smuzhiyun if (--smm_timeout == 0) {
1809*4882a593Smuzhiyun err("USB HC TakeOver failed!");
1810*4882a593Smuzhiyun return -1;
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun }
1814*4882a593Smuzhiyun
1815*4882a593Smuzhiyun /* Disable HC interrupts */
1816*4882a593Smuzhiyun ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1819*4882a593Smuzhiyun ohci->slot_name,
1820*4882a593Smuzhiyun ohci_readl(&ohci->regs->control));
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun /* Reset USB (needed by some controllers) */
1823*4882a593Smuzhiyun ohci->hc_control = 0;
1824*4882a593Smuzhiyun ohci_writel(ohci->hc_control, &ohci->regs->control);
1825*4882a593Smuzhiyun
1826*4882a593Smuzhiyun /* HC Reset requires max 10 us delay */
1827*4882a593Smuzhiyun ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1828*4882a593Smuzhiyun while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1829*4882a593Smuzhiyun if (--timeout == 0) {
1830*4882a593Smuzhiyun err("USB HC reset timed out!");
1831*4882a593Smuzhiyun return -1;
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun udelay(1);
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun return 0;
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1839*4882a593Smuzhiyun
1840*4882a593Smuzhiyun /* Start an OHCI controller, set the BUS operational
1841*4882a593Smuzhiyun * enable interrupts
1842*4882a593Smuzhiyun * connect the virtual root hub */
1843*4882a593Smuzhiyun
hc_start(ohci_t * ohci)1844*4882a593Smuzhiyun static int hc_start(ohci_t *ohci)
1845*4882a593Smuzhiyun {
1846*4882a593Smuzhiyun __u32 mask;
1847*4882a593Smuzhiyun unsigned int fminterval;
1848*4882a593Smuzhiyun int i;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun ohci->disabled = 1;
1851*4882a593Smuzhiyun for (i = 0; i < NUM_INT_DEVS; i++)
1852*4882a593Smuzhiyun ohci->int_dev[i].devnum = -1;
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun /* Tell the controller where the control and bulk lists are
1855*4882a593Smuzhiyun * The lists are empty now. */
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun ohci_writel(0, &ohci->regs->ed_controlhead);
1858*4882a593Smuzhiyun ohci_writel(0, &ohci->regs->ed_bulkhead);
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun ohci_writel((uintptr_t)ohci->hcca,
1861*4882a593Smuzhiyun &ohci->regs->hcca); /* reset clears this */
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun fminterval = 0x2edf;
1864*4882a593Smuzhiyun ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1865*4882a593Smuzhiyun fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1866*4882a593Smuzhiyun ohci_writel(fminterval, &ohci->regs->fminterval);
1867*4882a593Smuzhiyun ohci_writel(0x628, &ohci->regs->lsthresh);
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun /* start controller operations */
1870*4882a593Smuzhiyun ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1871*4882a593Smuzhiyun ohci->disabled = 0;
1872*4882a593Smuzhiyun ohci_writel(ohci->hc_control, &ohci->regs->control);
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun /* disable all interrupts */
1875*4882a593Smuzhiyun mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1876*4882a593Smuzhiyun OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1877*4882a593Smuzhiyun OHCI_INTR_OC | OHCI_INTR_MIE);
1878*4882a593Smuzhiyun ohci_writel(mask, &ohci->regs->intrdisable);
1879*4882a593Smuzhiyun /* clear all interrupts */
1880*4882a593Smuzhiyun mask &= ~OHCI_INTR_MIE;
1881*4882a593Smuzhiyun ohci_writel(mask, &ohci->regs->intrstatus);
1882*4882a593Smuzhiyun /* Choose the interrupts we care about now - but w/o MIE */
1883*4882a593Smuzhiyun mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1884*4882a593Smuzhiyun ohci_writel(mask, &ohci->regs->intrenable);
1885*4882a593Smuzhiyun
1886*4882a593Smuzhiyun #ifdef OHCI_USE_NPS
1887*4882a593Smuzhiyun /* required for AMD-756 and some Mac platforms */
1888*4882a593Smuzhiyun ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1889*4882a593Smuzhiyun &ohci->regs->roothub.a);
1890*4882a593Smuzhiyun ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1891*4882a593Smuzhiyun #endif /* OHCI_USE_NPS */
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun /* connect the virtual root hub */
1894*4882a593Smuzhiyun ohci->rh.devnum = 0;
1895*4882a593Smuzhiyun
1896*4882a593Smuzhiyun return 0;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun /* an interrupt happens */
1902*4882a593Smuzhiyun
hc_interrupt(ohci_t * ohci)1903*4882a593Smuzhiyun static int hc_interrupt(ohci_t *ohci)
1904*4882a593Smuzhiyun {
1905*4882a593Smuzhiyun struct ohci_regs *regs = ohci->regs;
1906*4882a593Smuzhiyun int ints;
1907*4882a593Smuzhiyun int stat = -1;
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun invalidate_dcache_hcca(ohci->hcca);
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun if ((ohci->hcca->done_head != 0) &&
1912*4882a593Smuzhiyun !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1913*4882a593Smuzhiyun ints = OHCI_INTR_WDH;
1914*4882a593Smuzhiyun } else {
1915*4882a593Smuzhiyun ints = ohci_readl(®s->intrstatus);
1916*4882a593Smuzhiyun if (ints == ~(u32)0) {
1917*4882a593Smuzhiyun ohci->disabled++;
1918*4882a593Smuzhiyun err("%s device removed!", ohci->slot_name);
1919*4882a593Smuzhiyun return -1;
1920*4882a593Smuzhiyun } else {
1921*4882a593Smuzhiyun ints &= ohci_readl(®s->intrenable);
1922*4882a593Smuzhiyun if (ints == 0) {
1923*4882a593Smuzhiyun dbg("hc_interrupt: returning..\n");
1924*4882a593Smuzhiyun return 0xff;
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun
1929*4882a593Smuzhiyun /* dbg("Interrupt: %x frame: %x", ints,
1930*4882a593Smuzhiyun le16_to_cpu(ohci->hcca->frame_no)); */
1931*4882a593Smuzhiyun
1932*4882a593Smuzhiyun if (ints & OHCI_INTR_RHSC)
1933*4882a593Smuzhiyun stat = 0xff;
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun if (ints & OHCI_INTR_UE) {
1936*4882a593Smuzhiyun ohci->disabled++;
1937*4882a593Smuzhiyun err("OHCI Unrecoverable Error, controller usb-%s disabled",
1938*4882a593Smuzhiyun ohci->slot_name);
1939*4882a593Smuzhiyun /* e.g. due to PCI Master/Target Abort */
1940*4882a593Smuzhiyun
1941*4882a593Smuzhiyun #ifdef DEBUG
1942*4882a593Smuzhiyun ohci_dump(ohci, 1);
1943*4882a593Smuzhiyun #else
1944*4882a593Smuzhiyun ohci_mdelay(1);
1945*4882a593Smuzhiyun #endif
1946*4882a593Smuzhiyun /* FIXME: be optimistic, hope that bug won't repeat often. */
1947*4882a593Smuzhiyun /* Make some non-interrupt context restart the controller. */
1948*4882a593Smuzhiyun /* Count and limit the retries though; either hardware or */
1949*4882a593Smuzhiyun /* software errors can go forever... */
1950*4882a593Smuzhiyun hc_reset(ohci);
1951*4882a593Smuzhiyun return -1;
1952*4882a593Smuzhiyun }
1953*4882a593Smuzhiyun
1954*4882a593Smuzhiyun if (ints & OHCI_INTR_WDH) {
1955*4882a593Smuzhiyun ohci_mdelay(1);
1956*4882a593Smuzhiyun ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
1957*4882a593Smuzhiyun (void)ohci_readl(®s->intrdisable); /* flush */
1958*4882a593Smuzhiyun stat = dl_done_list(ohci);
1959*4882a593Smuzhiyun ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
1960*4882a593Smuzhiyun (void)ohci_readl(®s->intrdisable); /* flush */
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun if (ints & OHCI_INTR_SO) {
1964*4882a593Smuzhiyun dbg("USB Schedule overrun\n");
1965*4882a593Smuzhiyun ohci_writel(OHCI_INTR_SO, ®s->intrenable);
1966*4882a593Smuzhiyun stat = -1;
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1970*4882a593Smuzhiyun if (ints & OHCI_INTR_SF) {
1971*4882a593Smuzhiyun unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1972*4882a593Smuzhiyun mdelay(1);
1973*4882a593Smuzhiyun ohci_writel(OHCI_INTR_SF, ®s->intrdisable);
1974*4882a593Smuzhiyun if (ohci->ed_rm_list[frame] != NULL)
1975*4882a593Smuzhiyun ohci_writel(OHCI_INTR_SF, ®s->intrenable);
1976*4882a593Smuzhiyun stat = 0xff;
1977*4882a593Smuzhiyun }
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun ohci_writel(ints, ®s->intrstatus);
1980*4882a593Smuzhiyun return stat;
1981*4882a593Smuzhiyun }
1982*4882a593Smuzhiyun
1983*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1988*4882a593Smuzhiyun
1989*4882a593Smuzhiyun /* De-allocate all resources.. */
1990*4882a593Smuzhiyun
hc_release_ohci(ohci_t * ohci)1991*4882a593Smuzhiyun static void hc_release_ohci(ohci_t *ohci)
1992*4882a593Smuzhiyun {
1993*4882a593Smuzhiyun dbg("USB HC release ohci usb-%s", ohci->slot_name);
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun if (!ohci->disabled)
1996*4882a593Smuzhiyun hc_reset(ohci);
1997*4882a593Smuzhiyun }
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun /*
2002*4882a593Smuzhiyun * low level initalisation routine, called from usb.c
2003*4882a593Smuzhiyun */
2004*4882a593Smuzhiyun static char ohci_inited = 0;
2005*4882a593Smuzhiyun
usb_lowlevel_init(int index,enum usb_init_type init,void ** controller)2006*4882a593Smuzhiyun int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun #ifdef CONFIG_PCI_OHCI
2009*4882a593Smuzhiyun pci_dev_t pdev;
2010*4882a593Smuzhiyun #endif
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2013*4882a593Smuzhiyun /* cpu dependant init */
2014*4882a593Smuzhiyun if (usb_cpu_init())
2015*4882a593Smuzhiyun return -1;
2016*4882a593Smuzhiyun #endif
2017*4882a593Smuzhiyun
2018*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2019*4882a593Smuzhiyun /* board dependant init */
2020*4882a593Smuzhiyun if (board_usb_init(index, USB_INIT_HOST))
2021*4882a593Smuzhiyun return -1;
2022*4882a593Smuzhiyun #endif
2023*4882a593Smuzhiyun memset(&gohci, 0, sizeof(ohci_t));
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun /* align the storage */
2026*4882a593Smuzhiyun if ((__u32)&ghcca[0] & 0xff) {
2027*4882a593Smuzhiyun err("HCCA not aligned!!");
2028*4882a593Smuzhiyun return -1;
2029*4882a593Smuzhiyun }
2030*4882a593Smuzhiyun gohci.hcca = &ghcca[0];
2031*4882a593Smuzhiyun info("aligned ghcca %p", gohci.hcca);
2032*4882a593Smuzhiyun memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun gohci.disabled = 1;
2035*4882a593Smuzhiyun gohci.sleeping = 0;
2036*4882a593Smuzhiyun gohci.irq = -1;
2037*4882a593Smuzhiyun #ifdef CONFIG_PCI_OHCI
2038*4882a593Smuzhiyun pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
2039*4882a593Smuzhiyun
2040*4882a593Smuzhiyun if (pdev != -1) {
2041*4882a593Smuzhiyun u16 vid, did;
2042*4882a593Smuzhiyun u32 base;
2043*4882a593Smuzhiyun pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2044*4882a593Smuzhiyun pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2045*4882a593Smuzhiyun printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2046*4882a593Smuzhiyun vid, did, (pdev >> 16) & 0xff,
2047*4882a593Smuzhiyun (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2048*4882a593Smuzhiyun pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2049*4882a593Smuzhiyun printf("OHCI regs address 0x%08x\n", base);
2050*4882a593Smuzhiyun gohci.regs = (struct ohci_regs *)base;
2051*4882a593Smuzhiyun } else {
2052*4882a593Smuzhiyun printf("%s: OHCI devnr: %d not found\n", __func__,
2053*4882a593Smuzhiyun CONFIG_PCI_OHCI_DEVNO);
2054*4882a593Smuzhiyun return -1;
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun #else
2057*4882a593Smuzhiyun gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
2058*4882a593Smuzhiyun #endif
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun gohci.flags = 0;
2061*4882a593Smuzhiyun gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
2062*4882a593Smuzhiyun
2063*4882a593Smuzhiyun if (hc_reset (&gohci) < 0) {
2064*4882a593Smuzhiyun hc_release_ohci (&gohci);
2065*4882a593Smuzhiyun err ("can't reset usb-%s", gohci.slot_name);
2066*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2067*4882a593Smuzhiyun /* board dependant cleanup */
2068*4882a593Smuzhiyun board_usb_cleanup(index, USB_INIT_HOST);
2069*4882a593Smuzhiyun #endif
2070*4882a593Smuzhiyun
2071*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2072*4882a593Smuzhiyun /* cpu dependant cleanup */
2073*4882a593Smuzhiyun usb_cpu_init_fail();
2074*4882a593Smuzhiyun #endif
2075*4882a593Smuzhiyun return -1;
2076*4882a593Smuzhiyun }
2077*4882a593Smuzhiyun
2078*4882a593Smuzhiyun if (hc_start(&gohci) < 0) {
2079*4882a593Smuzhiyun err("can't start usb-%s", gohci.slot_name);
2080*4882a593Smuzhiyun hc_release_ohci(&gohci);
2081*4882a593Smuzhiyun /* Initialization failed */
2082*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2083*4882a593Smuzhiyun /* board dependant cleanup */
2084*4882a593Smuzhiyun usb_board_stop();
2085*4882a593Smuzhiyun #endif
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2088*4882a593Smuzhiyun /* cpu dependant cleanup */
2089*4882a593Smuzhiyun usb_cpu_stop();
2090*4882a593Smuzhiyun #endif
2091*4882a593Smuzhiyun return -1;
2092*4882a593Smuzhiyun }
2093*4882a593Smuzhiyun
2094*4882a593Smuzhiyun #ifdef DEBUG
2095*4882a593Smuzhiyun ohci_dump(&gohci, 1);
2096*4882a593Smuzhiyun #else
2097*4882a593Smuzhiyun ohci_mdelay(1);
2098*4882a593Smuzhiyun #endif
2099*4882a593Smuzhiyun ohci_inited = 1;
2100*4882a593Smuzhiyun return 0;
2101*4882a593Smuzhiyun }
2102*4882a593Smuzhiyun
usb_lowlevel_stop(int index)2103*4882a593Smuzhiyun int usb_lowlevel_stop(int index)
2104*4882a593Smuzhiyun {
2105*4882a593Smuzhiyun /* this gets called really early - before the controller has */
2106*4882a593Smuzhiyun /* even been initialized! */
2107*4882a593Smuzhiyun if (!ohci_inited)
2108*4882a593Smuzhiyun return 0;
2109*4882a593Smuzhiyun /* TODO release any interrupts, etc. */
2110*4882a593Smuzhiyun /* call hc_release_ohci() here ? */
2111*4882a593Smuzhiyun hc_reset(&gohci);
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2114*4882a593Smuzhiyun /* board dependant cleanup */
2115*4882a593Smuzhiyun if (usb_board_stop())
2116*4882a593Smuzhiyun return -1;
2117*4882a593Smuzhiyun #endif
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2120*4882a593Smuzhiyun /* cpu dependant cleanup */
2121*4882a593Smuzhiyun if (usb_cpu_stop())
2122*4882a593Smuzhiyun return -1;
2123*4882a593Smuzhiyun #endif
2124*4882a593Smuzhiyun /* This driver is no longer initialised. It needs a new low-level
2125*4882a593Smuzhiyun * init (board/cpu) before it can be used again. */
2126*4882a593Smuzhiyun ohci_inited = 0;
2127*4882a593Smuzhiyun return 0;
2128*4882a593Smuzhiyun }
2129*4882a593Smuzhiyun
submit_control_msg(struct usb_device * dev,unsigned long pipe,void * buffer,int transfer_len,struct devrequest * setup)2130*4882a593Smuzhiyun int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2131*4882a593Smuzhiyun void *buffer, int transfer_len, struct devrequest *setup)
2132*4882a593Smuzhiyun {
2133*4882a593Smuzhiyun return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2134*4882a593Smuzhiyun transfer_len, setup);
2135*4882a593Smuzhiyun }
2136*4882a593Smuzhiyun #endif
2137*4882a593Smuzhiyun
2138*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
ohci_submit_control_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)2139*4882a593Smuzhiyun static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2140*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length,
2141*4882a593Smuzhiyun struct devrequest *setup)
2142*4882a593Smuzhiyun {
2143*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2144*4882a593Smuzhiyun
2145*4882a593Smuzhiyun return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2146*4882a593Smuzhiyun length, setup);
2147*4882a593Smuzhiyun }
2148*4882a593Smuzhiyun
ohci_submit_bulk_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length)2149*4882a593Smuzhiyun static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2150*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length)
2151*4882a593Smuzhiyun {
2152*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun
ohci_submit_int_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)2157*4882a593Smuzhiyun static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2158*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length,
2159*4882a593Smuzhiyun int interval, bool nonblock)
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2162*4882a593Smuzhiyun
2163*4882a593Smuzhiyun return submit_common_msg(ohci, udev, pipe, buffer, length,
2164*4882a593Smuzhiyun NULL, interval);
2165*4882a593Smuzhiyun }
2166*4882a593Smuzhiyun
ohci_create_int_queue(struct udevice * dev,struct usb_device * udev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)2167*4882a593Smuzhiyun static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2168*4882a593Smuzhiyun struct usb_device *udev, unsigned long pipe, int queuesize,
2169*4882a593Smuzhiyun int elementsize, void *buffer, int interval)
2170*4882a593Smuzhiyun {
2171*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2174*4882a593Smuzhiyun buffer, interval);
2175*4882a593Smuzhiyun }
2176*4882a593Smuzhiyun
ohci_poll_int_queue(struct udevice * dev,struct usb_device * udev,struct int_queue * queue)2177*4882a593Smuzhiyun static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2178*4882a593Smuzhiyun struct int_queue *queue)
2179*4882a593Smuzhiyun {
2180*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun return _ohci_poll_int_queue(ohci, udev, queue);
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun
ohci_destroy_int_queue(struct udevice * dev,struct usb_device * udev,struct int_queue * queue)2185*4882a593Smuzhiyun static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2186*4882a593Smuzhiyun struct int_queue *queue)
2187*4882a593Smuzhiyun {
2188*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun return _ohci_destroy_int_queue(ohci, udev, queue);
2191*4882a593Smuzhiyun }
2192*4882a593Smuzhiyun
ohci_register(struct udevice * dev,struct ohci_regs * regs)2193*4882a593Smuzhiyun int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2194*4882a593Smuzhiyun {
2195*4882a593Smuzhiyun struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2196*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(dev);
2197*4882a593Smuzhiyun u32 reg;
2198*4882a593Smuzhiyun
2199*4882a593Smuzhiyun priv->desc_before_addr = true;
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun ohci->regs = regs;
2202*4882a593Smuzhiyun ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2203*4882a593Smuzhiyun if (!ohci->hcca)
2204*4882a593Smuzhiyun return -ENOMEM;
2205*4882a593Smuzhiyun memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
2206*4882a593Smuzhiyun flush_dcache_hcca(ohci->hcca);
2207*4882a593Smuzhiyun
2208*4882a593Smuzhiyun if (hc_reset(ohci) < 0)
2209*4882a593Smuzhiyun return -EIO;
2210*4882a593Smuzhiyun
2211*4882a593Smuzhiyun if (hc_start(ohci) < 0)
2212*4882a593Smuzhiyun return -EIO;
2213*4882a593Smuzhiyun
2214*4882a593Smuzhiyun reg = ohci_readl(®s->revision);
2215*4882a593Smuzhiyun printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2216*4882a593Smuzhiyun
2217*4882a593Smuzhiyun return 0;
2218*4882a593Smuzhiyun }
2219*4882a593Smuzhiyun
ohci_deregister(struct udevice * dev)2220*4882a593Smuzhiyun int ohci_deregister(struct udevice *dev)
2221*4882a593Smuzhiyun {
2222*4882a593Smuzhiyun ohci_t *ohci = dev_get_priv(dev);
2223*4882a593Smuzhiyun
2224*4882a593Smuzhiyun if (hc_reset(ohci) < 0)
2225*4882a593Smuzhiyun return -EIO;
2226*4882a593Smuzhiyun
2227*4882a593Smuzhiyun free(ohci->hcca);
2228*4882a593Smuzhiyun
2229*4882a593Smuzhiyun return 0;
2230*4882a593Smuzhiyun }
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun struct dm_usb_ops ohci_usb_ops = {
2233*4882a593Smuzhiyun .control = ohci_submit_control_msg,
2234*4882a593Smuzhiyun .bulk = ohci_submit_bulk_msg,
2235*4882a593Smuzhiyun .interrupt = ohci_submit_int_msg,
2236*4882a593Smuzhiyun .create_int_queue = ohci_create_int_queue,
2237*4882a593Smuzhiyun .poll_int_queue = ohci_poll_int_queue,
2238*4882a593Smuzhiyun .destroy_int_queue = ohci_destroy_int_queue,
2239*4882a593Smuzhiyun };
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun #endif
2242