blob: 3c659c60c97a5893309c3861ff75a06d1aefa2a8 [file] [log] [blame]
wdenkde887eb2003-09-10 18:20:28 +00001/*
2 * URB OHCI HCD (Host Controller Driver) for USB on the S3C2400.
3 *
4 * (C) Copyright 2003
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +02005 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
wdenkde887eb2003-09-10 18:20:28 +00006 *
wdenkbfad55d2005-03-14 23:56:42 +00007 * Note: Much of this code has been derived from Linux 2.4
8 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
9 * (C) Copyright 2000-2002 David Brownell
10 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
wdenkde887eb2003-09-10 18:20:28 +000012 */
13/*
14 * IMPORTANT NOTES
Mike Frysingercc93fc02009-01-01 18:27:27 -050015 * 1 - this driver is intended for use with USB Mass Storage Devices
wdenkde887eb2003-09-10 18:20:28 +000016 * (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
17 */
18
19#include <common.h>
wdenk4ea537d2003-12-07 18:32:37 +000020/* #include <pci.h> no PCI on the S3C24X0 */
wdenkde887eb2003-09-10 18:20:28 +000021
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090022#if defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0)
wdenkde887eb2003-09-10 18:20:28 +000023
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090024#include <asm/arch/s3c24x0_cpu.h>
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090025#include <asm/io.h>
wdenkde887eb2003-09-10 18:20:28 +000026#include <malloc.h>
27#include <usb.h>
Marek Vasute917a7c2012-07-21 05:02:22 +000028#include "ohci-s3c24xx.h"
wdenkde887eb2003-09-10 18:20:28 +000029
30#define OHCI_USE_NPS /* force NoPowerSwitching mode */
31#undef OHCI_VERBOSE_DEBUG /* not always helpful */
32
33
34/* For initializing controller (mask in an HCFS mode too) */
35#define OHCI_CONTROL_INIT \
36 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
37
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090038#define min_t(type, x, y) \
39 ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
wdenkde887eb2003-09-10 18:20:28 +000040
41#undef DEBUG
42#ifdef DEBUG
43#define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
44#else
45#define dbg(format, arg...) do {} while(0)
46#endif /* DEBUG */
47#define err(format, arg...) printf("ERROR: " format "\n", ## arg)
48#undef SHOW_INFO
49#ifdef SHOW_INFO
50#define info(format, arg...) printf("INFO: " format "\n", ## arg)
51#else
52#define info(format, arg...) do {} while(0)
53#endif
54
55#define m16_swap(x) swap_16(x)
56#define m32_swap(x) swap_32(x)
57
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090058/* global struct ohci */
59static struct ohci gohci;
wdenkde887eb2003-09-10 18:20:28 +000060/* this must be aligned to a 256 byte boundary */
61struct ohci_hcca ghcca[1];
62/* a pointer to the aligned storage */
63struct ohci_hcca *phcca;
64/* this allocates EDs for all possible endpoints */
65struct ohci_device ohci_dev;
66/* urb_priv */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090067struct urb_priv urb_priv;
dzu8d7e4d12003-09-29 21:55:54 +000068/* RHSC flag */
69int got_rhsc;
70/* device which was disconnected */
71struct usb_device *devgone;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +020072/* flag guarding URB transation */
73int urb_finished = 0;
wdenkde887eb2003-09-10 18:20:28 +000074
75/*-------------------------------------------------------------------------*/
76
77/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
78 * The erratum (#4) description is incorrect. AMD's workaround waits
79 * till some bits (mostly reserved) are clear; ok for all revs.
80 */
81#define OHCI_QUIRK_AMD756 0xabcd
82#define read_roothub(hc, register, mask) ({ \
83 u32 temp = readl (&hc->regs->roothub.register); \
84 if (hc->flags & OHCI_QUIRK_AMD756) \
85 while (temp & mask) \
86 temp = readl (&hc->regs->roothub.register); \
87 temp; })
88
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090089static u32 roothub_a(struct ohci *hc)
90{
91 return read_roothub(hc, a, 0xfc0fe000);
92}
93static inline u32 roothub_b(struct ohci *hc)
94{
95 return readl(&hc->regs->roothub.b);
96}
97static inline u32 roothub_status(struct ohci *hc)
98{
99 return readl(&hc->regs->roothub.status);
100}
101static u32 roothub_portstatus(struct ohci *hc, int i)
102{
103 return read_roothub(hc, portstatus[i], 0xffe0fce0);
104}
wdenkde887eb2003-09-10 18:20:28 +0000105
106/* forward declaration */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900107static int hc_interrupt(void);
108static void td_submit_job(struct usb_device *dev, unsigned long pipe,
109 void *buffer, int transfer_len,
110 struct devrequest *setup, struct urb_priv *urb,
111 int interval);
wdenkde887eb2003-09-10 18:20:28 +0000112
113/*-------------------------------------------------------------------------*
114 * URB support functions
115 *-------------------------------------------------------------------------*/
116
117/* free HCD-private data associated with this URB */
118
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900119static void urb_free_priv(struct urb_priv *urb)
wdenkde887eb2003-09-10 18:20:28 +0000120{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900121 int i;
122 int last;
123 struct td *td;
wdenkde887eb2003-09-10 18:20:28 +0000124
125 last = urb->length - 1;
126 if (last >= 0) {
127 for (i = 0; i <= last; i++) {
128 td = urb->td[i];
129 if (td) {
130 td->usb_dev = NULL;
131 urb->td[i] = NULL;
132 }
133 }
134 }
135}
136
137/*-------------------------------------------------------------------------*/
138
139#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900140static int sohci_get_current_frame_number(struct usb_device *dev);
wdenkde887eb2003-09-10 18:20:28 +0000141
142/* debug| print the main components of an URB
143 * small: 0) header + data packets 1) just header */
144
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900145static void pkt_print(struct usb_device *dev, unsigned long pipe, void *buffer,
146 int transfer_len, struct devrequest *setup, char *str,
147 int small)
wdenkde887eb2003-09-10 18:20:28 +0000148{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900149 struct urb_priv *purb = &urb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000150
151 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900152 str,
153 sohci_get_current_frame_number(dev),
154 usb_pipedevice(pipe),
155 usb_pipeendpoint(pipe),
156 usb_pipeout(pipe) ? 'O' : 'I',
157 usb_pipetype(pipe) < 2 ?
158 (usb_pipeint(pipe) ? "INTR" : "ISOC") :
159 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"),
160 purb->actual_length, transfer_len, dev->status);
wdenkde887eb2003-09-10 18:20:28 +0000161#ifdef OHCI_VERBOSE_DEBUG
162 if (!small) {
163 int i, len;
164
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900165 if (usb_pipecontrol(pipe)) {
166 printf(__FILE__ ": cmd(8):");
167 for (i = 0; i < 8; i++)
168 printf(" %02x", ((__u8 *) setup)[i]);
169 printf("\n");
wdenkde887eb2003-09-10 18:20:28 +0000170 }
171 if (transfer_len > 0 && buffer) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900172 printf(__FILE__ ": data(%d/%d):",
173 purb->actual_length, transfer_len);
174 len = usb_pipeout(pipe) ?
175 transfer_len : purb->actual_length;
wdenkde887eb2003-09-10 18:20:28 +0000176 for (i = 0; i < 16 && i < len; i++)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900177 printf(" %02x", ((__u8 *) buffer)[i]);
178 printf("%s\n", i < len ? "..." : "");
wdenkde887eb2003-09-10 18:20:28 +0000179 }
180 }
181#endif
182}
183
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900184/* just for debugging; prints non-empty branches of the
185 int ed tree inclusive iso eds*/
186void ep_print_int_eds(struct ohci *ohci, char *str)
187{
wdenkde887eb2003-09-10 18:20:28 +0000188 int i, j;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900189 __u32 *ed_p;
190 for (i = 0; i < 32; i++) {
wdenkde887eb2003-09-10 18:20:28 +0000191 j = 5;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900192 ed_p = &(ohci->hcca->int_table[i]);
wdenkde887eb2003-09-10 18:20:28 +0000193 if (*ed_p == 0)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900194 continue;
195 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
wdenkde887eb2003-09-10 18:20:28 +0000196 while (*ed_p != 0 && j--) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900197 struct ed *ed = (struct ed *) m32_swap(ed_p);
198 printf(" ed: %4x;", ed->hwINFO);
wdenkde887eb2003-09-10 18:20:28 +0000199 ed_p = &ed->hwNextED;
200 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900201 printf("\n");
wdenkde887eb2003-09-10 18:20:28 +0000202 }
203}
204
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900205static void ohci_dump_intr_mask(char *label, __u32 mask)
wdenkde887eb2003-09-10 18:20:28 +0000206{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900207 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
208 label,
209 mask,
210 (mask & OHCI_INTR_MIE) ? " MIE" : "",
211 (mask & OHCI_INTR_OC) ? " OC" : "",
212 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
213 (mask & OHCI_INTR_FNO) ? " FNO" : "",
214 (mask & OHCI_INTR_UE) ? " UE" : "",
215 (mask & OHCI_INTR_RD) ? " RD" : "",
216 (mask & OHCI_INTR_SF) ? " SF" : "",
217 (mask & OHCI_INTR_WDH) ? " WDH" : "",
218 (mask & OHCI_INTR_SO) ? " SO" : "");
wdenkde887eb2003-09-10 18:20:28 +0000219}
220
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900221static void maybe_print_eds(char *label, __u32 value)
wdenkde887eb2003-09-10 18:20:28 +0000222{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900223 struct ed *edp = (struct ed *) value;
wdenkde887eb2003-09-10 18:20:28 +0000224
225 if (value) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900226 dbg("%s %08x", label, value);
227 dbg("%08x", edp->hwINFO);
228 dbg("%08x", edp->hwTailP);
229 dbg("%08x", edp->hwHeadP);
230 dbg("%08x", edp->hwNextED);
wdenkde887eb2003-09-10 18:20:28 +0000231 }
232}
233
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900234static char *hcfs2string(int state)
wdenkde887eb2003-09-10 18:20:28 +0000235{
236 switch (state) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900237 case OHCI_USB_RESET:
238 return "reset";
239 case OHCI_USB_RESUME:
240 return "resume";
241 case OHCI_USB_OPER:
242 return "operational";
243 case OHCI_USB_SUSPEND:
244 return "suspend";
wdenkde887eb2003-09-10 18:20:28 +0000245 }
246 return "?";
247}
248
249/* dump control and status registers */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900250static void ohci_dump_status(struct ohci *controller)
wdenkde887eb2003-09-10 18:20:28 +0000251{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900252 struct ohci_regs *regs = controller->regs;
253 __u32 temp;
wdenkde887eb2003-09-10 18:20:28 +0000254
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900255 temp = readl(&regs->revision) & 0xff;
wdenkde887eb2003-09-10 18:20:28 +0000256 if (temp != 0x10)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900257 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
wdenkde887eb2003-09-10 18:20:28 +0000258
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900259 temp = readl(&regs->control);
260 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
261 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
262 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
263 (temp & OHCI_CTRL_IR) ? " IR" : "",
264 hcfs2string(temp & OHCI_CTRL_HCFS),
265 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
266 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
267 (temp & OHCI_CTRL_IE) ? " IE" : "",
268 (temp & OHCI_CTRL_PLE) ? " PLE" : "", temp & OHCI_CTRL_CBSR);
wdenkde887eb2003-09-10 18:20:28 +0000269
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900270 temp = readl(&regs->cmdstatus);
271 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
272 (temp & OHCI_SOC) >> 16,
273 (temp & OHCI_OCR) ? " OCR" : "",
274 (temp & OHCI_BLF) ? " BLF" : "",
275 (temp & OHCI_CLF) ? " CLF" : "", (temp & OHCI_HCR) ? " HCR" : "");
wdenkde887eb2003-09-10 18:20:28 +0000276
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900277 ohci_dump_intr_mask("intrstatus", readl(&regs->intrstatus));
278 ohci_dump_intr_mask("intrenable", readl(&regs->intrenable));
wdenkde887eb2003-09-10 18:20:28 +0000279
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900280 maybe_print_eds("ed_periodcurrent", readl(&regs->ed_periodcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000281
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900282 maybe_print_eds("ed_controlhead", readl(&regs->ed_controlhead));
283 maybe_print_eds("ed_controlcurrent", readl(&regs->ed_controlcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000284
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900285 maybe_print_eds("ed_bulkhead", readl(&regs->ed_bulkhead));
286 maybe_print_eds("ed_bulkcurrent", readl(&regs->ed_bulkcurrent));
wdenkde887eb2003-09-10 18:20:28 +0000287
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900288 maybe_print_eds("donehead", readl(&regs->donehead));
wdenkde887eb2003-09-10 18:20:28 +0000289}
290
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900291static void ohci_dump_roothub(struct ohci *controller, int verbose)
wdenkde887eb2003-09-10 18:20:28 +0000292{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900293 __u32 temp, ndp, i;
wdenkde887eb2003-09-10 18:20:28 +0000294
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900295 temp = roothub_a(controller);
wdenkde887eb2003-09-10 18:20:28 +0000296 ndp = (temp & RH_A_NDP);
297
298 if (verbose) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900299 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
300 ((temp & RH_A_POTPGT) >> 24) & 0xff,
301 (temp & RH_A_NOCP) ? " NOCP" : "",
302 (temp & RH_A_OCPM) ? " OCPM" : "",
303 (temp & RH_A_DT) ? " DT" : "",
304 (temp & RH_A_NPS) ? " NPS" : "",
305 (temp & RH_A_PSM) ? " PSM" : "", ndp);
306 temp = roothub_b(controller);
307 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
308 temp, (temp & RH_B_PPCM) >> 16, (temp & RH_B_DR)
309 );
310 temp = roothub_status(controller);
311 dbg("roothub.status: %08x%s%s%s%s%s%s",
312 temp,
313 (temp & RH_HS_CRWE) ? " CRWE" : "",
314 (temp & RH_HS_OCIC) ? " OCIC" : "",
315 (temp & RH_HS_LPSC) ? " LPSC" : "",
316 (temp & RH_HS_DRWE) ? " DRWE" : "",
317 (temp & RH_HS_OCI) ? " OCI" : "",
318 (temp & RH_HS_LPS) ? " LPS" : "");
wdenkde887eb2003-09-10 18:20:28 +0000319 }
320
321 for (i = 0; i < ndp; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900322 temp = roothub_portstatus(controller, i);
323 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
324 i,
325 temp,
326 (temp & RH_PS_PRSC) ? " PRSC" : "",
327 (temp & RH_PS_OCIC) ? " OCIC" : "",
328 (temp & RH_PS_PSSC) ? " PSSC" : "",
329 (temp & RH_PS_PESC) ? " PESC" : "",
330 (temp & RH_PS_CSC) ? " CSC" : "",
331 (temp & RH_PS_LSDA) ? " LSDA" : "",
332 (temp & RH_PS_PPS) ? " PPS" : "",
333 (temp & RH_PS_PRS) ? " PRS" : "",
334 (temp & RH_PS_POCI) ? " POCI" : "",
335 (temp & RH_PS_PSS) ? " PSS" : "",
336 (temp & RH_PS_PES) ? " PES" : "",
337 (temp & RH_PS_CCS) ? " CCS" : "");
wdenkde887eb2003-09-10 18:20:28 +0000338 }
339}
340
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900341static void ohci_dump(struct ohci *controller, int verbose)
wdenkde887eb2003-09-10 18:20:28 +0000342{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900343 dbg("OHCI controller usb-%s state", controller->slot_name);
wdenkde887eb2003-09-10 18:20:28 +0000344
345 /* dumps some of the state we know about */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900346 ohci_dump_status(controller);
wdenkde887eb2003-09-10 18:20:28 +0000347 if (verbose)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900348 ep_print_int_eds(controller, "hcca");
349 dbg("hcca frame #%04x", controller->hcca->frame_no);
350 ohci_dump_roothub(controller, 1);
wdenkde887eb2003-09-10 18:20:28 +0000351}
352
wdenkde887eb2003-09-10 18:20:28 +0000353#endif /* DEBUG */
354
355/*-------------------------------------------------------------------------*
356 * Interface functions (URB)
357 *-------------------------------------------------------------------------*/
358
359/* get a transfer request */
360
361int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900362 int transfer_len, struct devrequest *setup, int interval)
wdenkde887eb2003-09-10 18:20:28 +0000363{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900364 struct ohci *ohci;
365 struct ed *ed;
366 struct urb_priv *purb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000367 int i, size = 0;
368
369 ohci = &gohci;
370
371 /* when controller's hung, permit only roothub cleanup attempts
372 * such as powering down ports */
373 if (ohci->disabled) {
374 err("sohci_submit_job: EPIPE");
375 return -1;
376 }
377
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200378 /* if we have an unfinished URB from previous transaction let's
379 * fail and scream as quickly as possible so as not to corrupt
380 * further communication */
381 if (!urb_finished) {
382 err("sohci_submit_job: URB NOT FINISHED");
383 return -1;
384 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900385 /* we're about to begin a new transaction here
386 so mark the URB unfinished */
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200387 urb_finished = 0;
388
wdenkde887eb2003-09-10 18:20:28 +0000389 /* every endpoint has a ed, locate and fill it */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900390 ed = ep_add_ed(dev, pipe);
391 if (!ed) {
wdenkde887eb2003-09-10 18:20:28 +0000392 err("sohci_submit_job: ENOMEM");
393 return -1;
394 }
395
396 /* for the private part of the URB we need the number of TDs (size) */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900397 switch (usb_pipetype(pipe)) {
398 case PIPE_BULK:
399 /* one TD for every 4096 Byte */
400 size = (transfer_len - 1) / 4096 + 1;
401 break;
402 case PIPE_CONTROL:
403 /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
404 size = (transfer_len == 0) ? 2 : (transfer_len - 1) / 4096 + 3;
405 break;
wdenkde887eb2003-09-10 18:20:28 +0000406 }
407
408 if (size >= (N_URB_TD - 1)) {
409 err("need %d TDs, only have %d", size, N_URB_TD);
410 return -1;
411 }
412 purb_priv = &urb_priv;
413 purb_priv->pipe = pipe;
414
415 /* fill the private part of the URB */
416 purb_priv->length = size;
417 purb_priv->ed = ed;
418 purb_priv->actual_length = 0;
419
420 /* allocate the TDs */
421 /* note that td[0] was allocated in ep_add_ed */
422 for (i = 0; i < size; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900423 purb_priv->td[i] = td_alloc(dev);
wdenkde887eb2003-09-10 18:20:28 +0000424 if (!purb_priv->td[i]) {
425 purb_priv->length = i;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900426 urb_free_priv(purb_priv);
wdenkde887eb2003-09-10 18:20:28 +0000427 err("sohci_submit_job: ENOMEM");
428 return -1;
429 }
430 }
431
432 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900433 urb_free_priv(purb_priv);
wdenkde887eb2003-09-10 18:20:28 +0000434 err("sohci_submit_job: EINVAL");
435 return -1;
436 }
437
438 /* link the ed into a chain if is not already */
439 if (ed->state != ED_OPER)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900440 ep_link(ohci, ed);
wdenkde887eb2003-09-10 18:20:28 +0000441
442 /* fill the TDs and link it to the ed */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900443 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv,
444 interval);
wdenkde887eb2003-09-10 18:20:28 +0000445
446 return 0;
447}
448
449/*-------------------------------------------------------------------------*/
450
451#ifdef DEBUG
452/* tell us the current USB frame number */
453
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900454static int sohci_get_current_frame_number(struct usb_device *usb_dev)
wdenkde887eb2003-09-10 18:20:28 +0000455{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900456 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +0000457
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900458 return m16_swap(ohci->hcca->frame_no);
wdenkde887eb2003-09-10 18:20:28 +0000459}
460#endif
461
462/*-------------------------------------------------------------------------*
463 * ED handling functions
464 *-------------------------------------------------------------------------*/
465
466/* link an ed into one of the HC chains */
467
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900468static int ep_link(struct ohci *ohci, struct ed *edi)
wdenkde887eb2003-09-10 18:20:28 +0000469{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900470 struct ed *ed = edi;
wdenkde887eb2003-09-10 18:20:28 +0000471
472 ed->state = ED_OPER;
473
474 switch (ed->type) {
475 case PIPE_CONTROL:
476 ed->hwNextED = 0;
477 if (ohci->ed_controltail == NULL) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900478 writel((u32)ed, &ohci->regs->ed_controlhead);
wdenkde887eb2003-09-10 18:20:28 +0000479 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900480 ohci->ed_controltail->hwNextED = (__u32) m32_swap(ed);
wdenkde887eb2003-09-10 18:20:28 +0000481 }
482 ed->ed_prev = ohci->ed_controltail;
483 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900484 !ohci->ed_rm_list[1] && !ohci->sleeping) {
wdenkde887eb2003-09-10 18:20:28 +0000485 ohci->hc_control |= OHCI_CTRL_CLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900486 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000487 }
488 ohci->ed_controltail = edi;
489 break;
490
491 case PIPE_BULK:
492 ed->hwNextED = 0;
493 if (ohci->ed_bulktail == NULL) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900494 writel((u32)ed, &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +0000495 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900496 ohci->ed_bulktail->hwNextED = (__u32) m32_swap(ed);
wdenkde887eb2003-09-10 18:20:28 +0000497 }
498 ed->ed_prev = ohci->ed_bulktail;
499 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900500 !ohci->ed_rm_list[1] && !ohci->sleeping) {
wdenkde887eb2003-09-10 18:20:28 +0000501 ohci->hc_control |= OHCI_CTRL_BLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900502 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000503 }
504 ohci->ed_bulktail = edi;
505 break;
506 }
507 return 0;
508}
509
510/*-------------------------------------------------------------------------*/
511
512/* unlink an ed from one of the HC chains.
513 * just the link to the ed is unlinked.
514 * the link from the ed still points to another operational ed or 0
515 * so the HC can eventually finish the processing of the unlinked ed */
516
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900517static int ep_unlink(struct ohci *ohci, struct ed *ed)
wdenkde887eb2003-09-10 18:20:28 +0000518{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900519 struct ed *next;
520 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
wdenkde887eb2003-09-10 18:20:28 +0000521
522 switch (ed->type) {
523 case PIPE_CONTROL:
524 if (ed->ed_prev == NULL) {
525 if (!ed->hwNextED) {
526 ohci->hc_control &= ~OHCI_CTRL_CLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900527 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000528 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900529 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
530 &ohci->regs->ed_controlhead);
wdenkde887eb2003-09-10 18:20:28 +0000531 } else {
532 ed->ed_prev->hwNextED = ed->hwNextED;
533 }
534 if (ohci->ed_controltail == ed) {
535 ohci->ed_controltail = ed->ed_prev;
536 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900537 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
538 next->ed_prev = ed->ed_prev;
wdenkde887eb2003-09-10 18:20:28 +0000539 }
540 break;
541
542 case PIPE_BULK:
543 if (ed->ed_prev == NULL) {
544 if (!ed->hwNextED) {
545 ohci->hc_control &= ~OHCI_CTRL_BLE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900546 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +0000547 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900548 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
549 &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +0000550 } else {
551 ed->ed_prev->hwNextED = ed->hwNextED;
552 }
553 if (ohci->ed_bulktail == ed) {
554 ohci->ed_bulktail = ed->ed_prev;
555 } else {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900556 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
557 next->ed_prev = ed->ed_prev;
wdenkde887eb2003-09-10 18:20:28 +0000558 }
559 break;
560 }
561 ed->state = ED_UNLINK;
562 return 0;
563}
564
wdenkde887eb2003-09-10 18:20:28 +0000565/*-------------------------------------------------------------------------*/
566
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900567/* add/reinit an endpoint; this should be done once at the usb_set_configuration
568 * command, but the USB stack is a little bit stateless so we do it at every
569 * transaction. If the state of the ed is ED_NEW then a dummy td is added and
570 * the state is changed to ED_UNLINK. In all other cases the state is left
571 * unchanged. The ed info fields are setted anyway even though most of them
572 * should not change */
wdenkde887eb2003-09-10 18:20:28 +0000573
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900574static struct ed *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe)
wdenkde887eb2003-09-10 18:20:28 +0000575{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900576 struct td *td;
577 struct ed *ed_ret;
578 struct ed *ed;
wdenkde887eb2003-09-10 18:20:28 +0000579
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900580 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
581 (usb_pipecontrol(pipe) ? 0 :
582 usb_pipeout(pipe))];
wdenkde887eb2003-09-10 18:20:28 +0000583
584 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
585 err("ep_add_ed: pending delete");
586 /* pending delete request */
587 return NULL;
588 }
589
590 if (ed->state == ED_NEW) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900591 ed->hwINFO = m32_swap(OHCI_ED_SKIP); /* skip ed */
wdenk9c53f402003-10-15 23:53:47 +0000592 /* dummy td; end of td list for ed */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900593 td = td_alloc(usb_dev);
594 ed->hwTailP = (__u32) m32_swap(td);
wdenkde887eb2003-09-10 18:20:28 +0000595 ed->hwHeadP = ed->hwTailP;
596 ed->state = ED_UNLINK;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900597 ed->type = usb_pipetype(pipe);
wdenkde887eb2003-09-10 18:20:28 +0000598 ohci_dev.ed_cnt++;
599 }
600
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900601 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
602 | usb_pipeendpoint(pipe) << 7
603 | (usb_pipeisoc(pipe) ? 0x8000 : 0)
604 | (usb_pipecontrol(pipe) ? 0 :
605 (usb_pipeout(pipe) ? 0x800 : 0x1000))
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000606 | (usb_dev->speed == USB_SPEED_LOW) << 13 |
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900607 usb_maxpacket(usb_dev, pipe) << 16);
wdenkde887eb2003-09-10 18:20:28 +0000608
609 return ed_ret;
610}
611
612/*-------------------------------------------------------------------------*
613 * TD handling functions
614 *-------------------------------------------------------------------------*/
615
616/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
617
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900618static void td_fill(struct ohci *ohci, unsigned int info, void *data, int len,
619 struct usb_device *dev, int index,
620 struct urb_priv *urb_priv)
wdenkde887eb2003-09-10 18:20:28 +0000621{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900622 struct td *td, *td_pt;
wdenkde887eb2003-09-10 18:20:28 +0000623#ifdef OHCI_FILL_TRACE
624 int i;
625#endif
626
627 if (index > urb_priv->length) {
628 err("index > length");
629 return;
630 }
631 /* use this td as the next dummy */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900632 td_pt = urb_priv->td[index];
wdenkde887eb2003-09-10 18:20:28 +0000633 td_pt->hwNextTD = 0;
634
635 /* fill the old dummy TD */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900636 td = urb_priv->td[index] =
637 (struct td *) (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
wdenkde887eb2003-09-10 18:20:28 +0000638
639 td->ed = urb_priv->ed;
640 td->next_dl_td = NULL;
641 td->index = index;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900642 td->data = (__u32) data;
wdenkde887eb2003-09-10 18:20:28 +0000643#ifdef OHCI_FILL_TRACE
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200644 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
wdenkde887eb2003-09-10 18:20:28 +0000645 for (i = 0; i < len; i++)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900646 printf("td->data[%d] %#2x ", i,
647 ((unsigned char *)td->data)[i]);
wdenkde887eb2003-09-10 18:20:28 +0000648 printf("\n");
649 }
650#endif
651 if (!len)
652 data = 0;
653
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900654 td->hwINFO = (__u32) m32_swap(info);
655 td->hwCBP = (__u32) m32_swap(data);
wdenkde887eb2003-09-10 18:20:28 +0000656 if (data)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900657 td->hwBE = (__u32) m32_swap(data + len - 1);
wdenkde887eb2003-09-10 18:20:28 +0000658 else
659 td->hwBE = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900660 td->hwNextTD = (__u32) m32_swap(td_pt);
wdenkde887eb2003-09-10 18:20:28 +0000661
662 /* append to queue */
663 td->ed->hwTailP = td->hwNextTD;
664}
665
666/*-------------------------------------------------------------------------*/
667
668/* prepare all TDs of a transfer */
669
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900670static void td_submit_job(struct usb_device *dev, unsigned long pipe,
671 void *buffer, int transfer_len,
672 struct devrequest *setup, struct urb_priv *urb,
673 int interval)
wdenkde887eb2003-09-10 18:20:28 +0000674{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900675 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +0000676 int data_len = transfer_len;
677 void *data;
678 int cnt = 0;
679 __u32 info = 0;
wdenk9c53f402003-10-15 23:53:47 +0000680 unsigned int toggle = 0;
wdenkde887eb2003-09-10 18:20:28 +0000681
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900682 /* OHCI handles the DATA-toggles itself, we just
683 use the USB-toggle bits for reseting */
684 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
wdenk9c53f402003-10-15 23:53:47 +0000685 toggle = TD_T_TOGGLE;
wdenkde887eb2003-09-10 18:20:28 +0000686 } else {
wdenk9c53f402003-10-15 23:53:47 +0000687 toggle = TD_T_DATA0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900688 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe),
689 1);
wdenkde887eb2003-09-10 18:20:28 +0000690 }
691 urb->td_cnt = 0;
692 if (data_len)
693 data = buffer;
694 else
695 data = 0;
696
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900697 switch (usb_pipetype(pipe)) {
wdenkde887eb2003-09-10 18:20:28 +0000698 case PIPE_BULK:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900699 info = usb_pipeout(pipe) ? TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN;
700 while (data_len > 4096) {
701 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
702 4096, dev, cnt, urb);
703 data += 4096;
704 data_len -= 4096;
705 cnt++;
wdenkde887eb2003-09-10 18:20:28 +0000706 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900707 info = usb_pipeout(pipe) ?
708 TD_CC | TD_DP_OUT :
709 TD_CC | TD_R | TD_DP_IN;
710 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
711 data_len, dev, cnt, urb);
wdenkde887eb2003-09-10 18:20:28 +0000712 cnt++;
713
714 if (!ohci->sleeping)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900715 /* start bulk list */
716 writel(OHCI_BLF, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +0000717 break;
718
719 case PIPE_CONTROL:
720 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900721 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000722 if (data_len > 0) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900723 info = usb_pipeout(pipe) ?
724 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
725 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
wdenkde887eb2003-09-10 18:20:28 +0000726 /* NOTE: mishandles transfers >8K, some >4K */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900727 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000728 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900729 info = usb_pipeout(pipe) ?
730 TD_CC | TD_DP_IN | TD_T_DATA1 :
731 TD_CC | TD_DP_OUT | TD_T_DATA1;
732 td_fill(ohci, info, data, 0, dev, cnt++, urb);
wdenkde887eb2003-09-10 18:20:28 +0000733 if (!ohci->sleeping)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900734 /* start Control list */
735 writel(OHCI_CLF, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +0000736 break;
737 }
738 if (urb->length != cnt)
739 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
740}
741
742/*-------------------------------------------------------------------------*
743 * Done List handling functions
744 *-------------------------------------------------------------------------*/
745
746
747/* calculate the transfer length and update the urb */
748
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900749static void dl_transfer_length(struct td *td)
wdenkde887eb2003-09-10 18:20:28 +0000750{
Wolfgang Denka0ebb902011-10-06 09:22:52 +0200751 __u32 tdBE, tdCBP;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900752 struct urb_priv *lurb_priv = &urb_priv;
wdenkde887eb2003-09-10 18:20:28 +0000753
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900754 tdBE = m32_swap(td->hwBE);
755 tdCBP = m32_swap(td->hwCBP);
wdenkde887eb2003-09-10 18:20:28 +0000756
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200757 if (!(usb_pipecontrol(lurb_priv->pipe) &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900758 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
wdenkde887eb2003-09-10 18:20:28 +0000759 if (tdBE != 0) {
760 if (td->hwCBP == 0)
761 lurb_priv->actual_length += tdBE - td->data + 1;
762 else
763 lurb_priv->actual_length += tdCBP - td->data;
764 }
765 }
766}
767
768/*-------------------------------------------------------------------------*/
769
770/* replies to the request have to be on a FIFO basis so
771 * we reverse the reversed done-list */
772
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900773static struct td *dl_reverse_done_list(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +0000774{
775 __u32 td_list_hc;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900776 __u32 tmp;
777 struct td *td_rev = NULL;
778 struct td *td_list = NULL;
779 struct urb_priv *lurb_priv = NULL;
wdenkde887eb2003-09-10 18:20:28 +0000780
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900781 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
wdenkde887eb2003-09-10 18:20:28 +0000782 ohci->hcca->done_head = 0;
783
784 while (td_list_hc) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900785 td_list = (struct td *) td_list_hc;
wdenkde887eb2003-09-10 18:20:28 +0000786
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900787 if (TD_CC_GET(m32_swap(td_list->hwINFO))) {
wdenkde887eb2003-09-10 18:20:28 +0000788 lurb_priv = &urb_priv;
789 dbg(" USB-error/status: %x : %p",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900790 TD_CC_GET(m32_swap(td_list->hwINFO)), td_list);
791 if (td_list->ed->hwHeadP & m32_swap(0x1)) {
792 if (lurb_priv &&
793 ((td_list->index+1) < lurb_priv->length)) {
794 tmp = lurb_priv->length - 1;
wdenkde887eb2003-09-10 18:20:28 +0000795 td_list->ed->hwHeadP =
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900796 (lurb_priv->td[tmp]->hwNextTD &
797 m32_swap(0xfffffff0)) |
798 (td_list->ed->hwHeadP &
799 m32_swap(0x2));
800 lurb_priv->td_cnt += lurb_priv->length -
801 td_list->index - 1;
wdenkde887eb2003-09-10 18:20:28 +0000802 } else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900803 td_list->ed->hwHeadP &=
804 m32_swap(0xfffffff2);
wdenkde887eb2003-09-10 18:20:28 +0000805 }
806 }
807
808 td_list->next_dl_td = td_rev;
809 td_rev = td_list;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900810 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
wdenkde887eb2003-09-10 18:20:28 +0000811 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200812
wdenkde887eb2003-09-10 18:20:28 +0000813 return td_list;
814}
815
816/*-------------------------------------------------------------------------*/
817
818/* td done list */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900819static int dl_done_list(struct ohci *ohci, struct td *td_list)
wdenkde887eb2003-09-10 18:20:28 +0000820{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900821 struct td *td_list_next = NULL;
822 struct ed *ed;
wdenkde887eb2003-09-10 18:20:28 +0000823 int cc = 0;
824 int stat = 0;
825 /* urb_t *urb; */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900826 struct urb_priv *lurb_priv;
wdenk9c53f402003-10-15 23:53:47 +0000827 __u32 tdINFO, edHeadP, edTailP;
wdenkde887eb2003-09-10 18:20:28 +0000828
wdenk9c53f402003-10-15 23:53:47 +0000829 while (td_list) {
830 td_list_next = td_list->next_dl_td;
wdenkde887eb2003-09-10 18:20:28 +0000831
wdenk9c53f402003-10-15 23:53:47 +0000832 lurb_priv = &urb_priv;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900833 tdINFO = m32_swap(td_list->hwINFO);
wdenkde887eb2003-09-10 18:20:28 +0000834
wdenk9c53f402003-10-15 23:53:47 +0000835 ed = td_list->ed;
wdenkde887eb2003-09-10 18:20:28 +0000836
wdenk9c53f402003-10-15 23:53:47 +0000837 dl_transfer_length(td_list);
wdenkde887eb2003-09-10 18:20:28 +0000838
wdenk9c53f402003-10-15 23:53:47 +0000839 /* error code of transfer */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900840 cc = TD_CC_GET(tdINFO);
wdenkde887eb2003-09-10 18:20:28 +0000841 if (cc != 0) {
842 dbg("ConditionCode %#x", cc);
843 stat = cc_to_error[cc];
844 }
845
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200846 /* see if this done list makes for all TD's of current URB,
847 * and mark the URB finished if so */
848 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
849 if ((ed->state & (ED_OPER | ED_UNLINK)))
850 urb_finished = 1;
851 else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900852 dbg("dl_done_list: strange.., ED state %x, "
853 "ed->state\n");
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200854 } else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900855 dbg("dl_done_list: processing TD %x, len %x\n",
856 lurb_priv->td_cnt, lurb_priv->length);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200857
wdenk9c53f402003-10-15 23:53:47 +0000858 if (ed->state != ED_NEW) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900859 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
860 edTailP = m32_swap(ed->hwTailP);
wdenkde887eb2003-09-10 18:20:28 +0000861
862 /* unlink eds if they are not busy */
wdenk9c53f402003-10-15 23:53:47 +0000863 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900864 ep_unlink(ohci, ed);
wdenk9c53f402003-10-15 23:53:47 +0000865 }
wdenkde887eb2003-09-10 18:20:28 +0000866
wdenk9c53f402003-10-15 23:53:47 +0000867 td_list = td_list_next;
868 }
wdenkde887eb2003-09-10 18:20:28 +0000869 return stat;
870}
871
872/*-------------------------------------------------------------------------*
873 * Virtual Root Hub
874 *-------------------------------------------------------------------------*/
875
Stephen Warren39c89682014-02-13 21:15:18 -0700876#include <usbroothubdes.h>
wdenkde887eb2003-09-10 18:20:28 +0000877
878/* Hub class-specific descriptor is constructed dynamically */
879
880
881/*-------------------------------------------------------------------------*/
882
Wolfgang Denka1be4762008-05-20 16:00:29 +0200883#define OK(x) len = (x); break
wdenkde887eb2003-09-10 18:20:28 +0000884#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900885#define WR_RH_STAT(x) \
886{ \
887 info("WR:status %#8x", (x)); \
888 writel((x), &gohci.regs->roothub.status); \
889}
890#define WR_RH_PORTSTAT(x) \
891{ \
892 info("WR:portstatus[%d] %#8x", wIndex-1, (x)); \
893 writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); \
894}
wdenkde887eb2003-09-10 18:20:28 +0000895#else
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900896#define WR_RH_STAT(x) \
897 writel((x), &gohci.regs->roothub.status)
898#define WR_RH_PORTSTAT(x)\
899 writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
wdenkde887eb2003-09-10 18:20:28 +0000900#endif
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900901#define RD_RH_STAT roothub_status(&gohci)
902#define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
wdenkde887eb2003-09-10 18:20:28 +0000903
904/* request to virtual root hub */
905
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900906int rh_check_port_status(struct ohci *controller)
dzu8d7e4d12003-09-29 21:55:54 +0000907{
908 __u32 temp, ndp, i;
909 int res;
910
911 res = -1;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900912 temp = roothub_a(controller);
dzu8d7e4d12003-09-29 21:55:54 +0000913 ndp = (temp & RH_A_NDP);
914 for (i = 0; i < ndp; i++) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900915 temp = roothub_portstatus(controller, i);
dzu8d7e4d12003-09-29 21:55:54 +0000916 /* check for a device disconnect */
917 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900918 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
dzu8d7e4d12003-09-29 21:55:54 +0000919 res = i;
920 break;
921 }
922 }
923 return res;
924}
925
926static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900927 void *buffer, int transfer_len,
928 struct devrequest *cmd)
wdenkde887eb2003-09-10 18:20:28 +0000929{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900930 void *data = buffer;
wdenkde887eb2003-09-10 18:20:28 +0000931 int leni = transfer_len;
932 int len = 0;
933 int stat = 0;
Simon Glass28cace12011-11-15 18:17:06 +0000934 union {
935 __u32 word[4];
936 __u16 hword[8];
937 __u8 byte[16];
938 } datab;
939 __u8 *data_buf = datab.byte;
wdenk9c53f402003-10-15 23:53:47 +0000940 __u16 bmRType_bReq;
wdenkde887eb2003-09-10 18:20:28 +0000941 __u16 wValue;
942 __u16 wIndex;
943 __u16 wLength;
944
945#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900946 urb_priv.actual_length = 0;
947 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)",
948 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +0000949#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000950 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +0000951#endif
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200952 if (usb_pipeint(pipe)) {
wdenkde887eb2003-09-10 18:20:28 +0000953 info("Root-Hub submit IRQ: NOT implemented");
954 return 0;
955 }
956
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900957 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
958 wValue = m16_swap(cmd->value);
959 wIndex = m16_swap(cmd->index);
960 wLength = m16_swap(cmd->length);
wdenkde887eb2003-09-10 18:20:28 +0000961
962 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900963 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
wdenkde887eb2003-09-10 18:20:28 +0000964
965 switch (bmRType_bReq) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900966 /* Request Destination:
967 without flags: Device,
968 RH_INTERFACE: interface,
969 RH_ENDPOINT: endpoint,
970 RH_CLASS means HUB here,
971 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
972 */
wdenkde887eb2003-09-10 18:20:28 +0000973
974 case RH_GET_STATUS:
Simon Glass28cace12011-11-15 18:17:06 +0000975 datab.hword[0] = m16_swap(1);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900976 OK(2);
wdenkde887eb2003-09-10 18:20:28 +0000977 case RH_GET_STATUS | RH_INTERFACE:
Simon Glass28cace12011-11-15 18:17:06 +0000978 datab.hword[0] = m16_swap(0);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900979 OK(2);
wdenkde887eb2003-09-10 18:20:28 +0000980 case RH_GET_STATUS | RH_ENDPOINT:
Simon Glass28cace12011-11-15 18:17:06 +0000981 datab.hword[0] = m16_swap(0);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900982 OK(2);
wdenkde887eb2003-09-10 18:20:28 +0000983 case RH_GET_STATUS | RH_CLASS:
Simon Glass28cace12011-11-15 18:17:06 +0000984 datab.word[0] =
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900985 m32_swap(RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
986 OK(4);
wdenkde887eb2003-09-10 18:20:28 +0000987 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
Simon Glass28cace12011-11-15 18:17:06 +0000988 datab.word[0] = m32_swap(RD_RH_PORTSTAT);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900989 OK(4);
wdenkde887eb2003-09-10 18:20:28 +0000990
991 case RH_CLEAR_FEATURE | RH_ENDPOINT:
992 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900993 case (RH_ENDPOINT_STALL):
994 OK(0);
wdenkde887eb2003-09-10 18:20:28 +0000995 }
996 break;
997
998 case RH_CLEAR_FEATURE | RH_CLASS:
999 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001000 case RH_C_HUB_LOCAL_POWER:
1001 OK(0);
1002 case (RH_C_HUB_OVER_CURRENT):
1003 WR_RH_STAT(RH_HS_OCIC);
1004 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001005 }
1006 break;
1007
1008 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1009 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001010 case (RH_PORT_ENABLE):
1011 WR_RH_PORTSTAT(RH_PS_CCS);
1012 OK(0);
1013 case (RH_PORT_SUSPEND):
1014 WR_RH_PORTSTAT(RH_PS_POCI);
1015 OK(0);
1016 case (RH_PORT_POWER):
1017 WR_RH_PORTSTAT(RH_PS_LSDA);
1018 OK(0);
1019 case (RH_C_PORT_CONNECTION):
1020 WR_RH_PORTSTAT(RH_PS_CSC);
1021 OK(0);
1022 case (RH_C_PORT_ENABLE):
1023 WR_RH_PORTSTAT(RH_PS_PESC);
1024 OK(0);
1025 case (RH_C_PORT_SUSPEND):
1026 WR_RH_PORTSTAT(RH_PS_PSSC);
1027 OK(0);
1028 case (RH_C_PORT_OVER_CURRENT):
1029 WR_RH_PORTSTAT(RH_PS_OCIC);
1030 OK(0);
1031 case (RH_C_PORT_RESET):
1032 WR_RH_PORTSTAT(RH_PS_PRSC);
1033 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001034 }
1035 break;
1036
1037 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1038 switch (wValue) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001039 case (RH_PORT_SUSPEND):
1040 WR_RH_PORTSTAT(RH_PS_PSS);
1041 OK(0);
1042 case (RH_PORT_RESET): /* BUG IN HUP CODE ******** */
1043 if (RD_RH_PORTSTAT & RH_PS_CCS)
1044 WR_RH_PORTSTAT(RH_PS_PRS);
1045 OK(0);
1046 case (RH_PORT_POWER):
1047 WR_RH_PORTSTAT(RH_PS_PPS);
1048 OK(0);
1049 case (RH_PORT_ENABLE): /* BUG IN HUP CODE ******** */
1050 if (RD_RH_PORTSTAT & RH_PS_CCS)
1051 WR_RH_PORTSTAT(RH_PS_PES);
1052 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001053 }
1054 break;
1055
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001056 case RH_SET_ADDRESS:
1057 gohci.rh.devnum = wValue;
1058 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001059
1060 case RH_GET_DESCRIPTOR:
1061 switch ((wValue & 0xff00) >> 8) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001062 case (0x01): /* device descriptor */
1063 len = min_t(unsigned int,
1064 leni,
1065 min_t(unsigned int,
1066 sizeof(root_hub_dev_des), wLength));
1067 data_buf = root_hub_dev_des;
1068 OK(len);
1069 case (0x02): /* configuration descriptor */
1070 len = min_t(unsigned int,
1071 leni,
1072 min_t(unsigned int,
1073 sizeof(root_hub_config_des),
1074 wLength));
1075 data_buf = root_hub_config_des;
1076 OK(len);
1077 case (0x03): /* string descriptors */
1078 if (wValue == 0x0300) {
wdenkde887eb2003-09-10 18:20:28 +00001079 len = min_t(unsigned int,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001080 leni,
1081 min_t(unsigned int,
1082 sizeof(root_hub_str_index0),
1083 wLength));
1084 data_buf = root_hub_str_index0;
1085 OK(len);
1086 }
1087 if (wValue == 0x0301) {
wdenkde887eb2003-09-10 18:20:28 +00001088 len = min_t(unsigned int,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001089 leni,
1090 min_t(unsigned int,
1091 sizeof(root_hub_str_index1),
1092 wLength));
1093 data_buf = root_hub_str_index1;
1094 OK(len);
wdenkde887eb2003-09-10 18:20:28 +00001095 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001096 default:
1097 stat = USB_ST_STALLED;
wdenkde887eb2003-09-10 18:20:28 +00001098 }
1099 break;
1100
1101 case RH_GET_DESCRIPTOR | RH_CLASS:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001102 {
1103 __u32 temp = roothub_a(&gohci);
wdenkde887eb2003-09-10 18:20:28 +00001104
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001105 data_buf[0] = 9; /* min length; */
1106 data_buf[1] = 0x29;
1107 data_buf[2] = temp & RH_A_NDP;
1108 data_buf[3] = 0;
1109 if (temp & RH_A_PSM)
1110 /* per-port power switching? */
1111 data_buf[3] |= 0x1;
1112 if (temp & RH_A_NOCP)
1113 /* no overcurrent reporting? */
1114 data_buf[3] |= 0x10;
1115 else if (temp & RH_A_OCPM)
1116 /* per-port overcurrent reporting? */
1117 data_buf[3] |= 0x8;
wdenkde887eb2003-09-10 18:20:28 +00001118
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001119 /* corresponds to data_buf[4-7] */
Simon Glass28cace12011-11-15 18:17:06 +00001120 datab.word[1] = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001121 data_buf[5] = (temp & RH_A_POTPGT) >> 24;
1122 temp = roothub_b(&gohci);
1123 data_buf[7] = temp & RH_B_DR;
1124 if (data_buf[2] < 7) {
1125 data_buf[8] = 0xff;
1126 } else {
1127 data_buf[0] += 2;
1128 data_buf[8] = (temp & RH_B_DR) >> 8;
1129 data_buf[10] = data_buf[9] = 0xff;
1130 }
wdenkde887eb2003-09-10 18:20:28 +00001131
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001132 len = min_t(unsigned int, leni,
1133 min_t(unsigned int, data_buf[0], wLength));
1134 OK(len);
wdenkde887eb2003-09-10 18:20:28 +00001135 }
1136
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001137 case RH_GET_CONFIGURATION:
1138 *(__u8 *) data_buf = 0x01;
1139 OK(1);
wdenkde887eb2003-09-10 18:20:28 +00001140
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001141 case RH_SET_CONFIGURATION:
1142 WR_RH_STAT(0x10000);
1143 OK(0);
wdenkde887eb2003-09-10 18:20:28 +00001144
1145 default:
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001146 dbg("unsupported root hub command");
wdenkde887eb2003-09-10 18:20:28 +00001147 stat = USB_ST_STALLED;
1148 }
1149
1150#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001151 ohci_dump_roothub(&gohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001152#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001153 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001154#endif
1155
1156 len = min_t(int, len, leni);
1157 if (data != data_buf)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001158 memcpy(data, data_buf, len);
wdenk9c53f402003-10-15 23:53:47 +00001159 dev->act_len = len;
wdenkde887eb2003-09-10 18:20:28 +00001160 dev->status = stat;
1161
1162#ifdef DEBUG
1163 if (transfer_len)
1164 urb_priv.actual_length = transfer_len;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001165 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)",
1166 0 /*usb_pipein(pipe) */);
wdenkde887eb2003-09-10 18:20:28 +00001167#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001168 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001169#endif
1170
1171 return stat;
1172}
1173
1174/*-------------------------------------------------------------------------*/
1175
1176/* common code for handling submit messages - used for all but root hub */
1177/* accesses. */
1178int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001179 int transfer_len, struct devrequest *setup, int interval)
wdenkde887eb2003-09-10 18:20:28 +00001180{
1181 int stat = 0;
1182 int maxsize = usb_maxpacket(dev, pipe);
1183 int timeout;
1184
dzu8d7e4d12003-09-29 21:55:54 +00001185 /* device pulled? Shortcut the action. */
1186 if (devgone == dev) {
1187 dev->status = USB_ST_CRC_ERR;
1188 return 0;
1189 }
wdenkde887eb2003-09-10 18:20:28 +00001190#ifdef DEBUG
1191 urb_priv.actual_length = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001192 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1193 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001194#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001195 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001196#endif
1197 if (!maxsize) {
1198 err("submit_common_message: pipesize for pipe %lx is zero",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001199 pipe);
wdenkde887eb2003-09-10 18:20:28 +00001200 return -1;
1201 }
1202
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001203 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) <
1204 0) {
wdenkde887eb2003-09-10 18:20:28 +00001205 err("sohci_submit_job failed");
1206 return -1;
1207 }
1208
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001209 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001210 /* ohci_dump_status(&gohci); */
wdenk9c53f402003-10-15 23:53:47 +00001211
wdenk934c4f82003-09-11 19:48:06 +00001212 /* allow more time for a BULK device to react - some are slow */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001213#define BULK_TO 5000 /* timeout in milliseconds */
Remy Bohmerd8c55ab2008-10-10 10:23:22 +02001214 if (usb_pipebulk(pipe))
wdenk934c4f82003-09-11 19:48:06 +00001215 timeout = BULK_TO;
1216 else
1217 timeout = 100;
1218
wdenkde887eb2003-09-10 18:20:28 +00001219 /* wait for it to complete */
wdenkde887eb2003-09-10 18:20:28 +00001220 for (;;) {
1221 /* check whether the controller is done */
1222 stat = hc_interrupt();
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001223
wdenkde887eb2003-09-10 18:20:28 +00001224 if (stat < 0) {
dzu8d7e4d12003-09-29 21:55:54 +00001225 stat = USB_ST_CRC_ERR;
wdenkde887eb2003-09-10 18:20:28 +00001226 break;
1227 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001228
1229 /* NOTE: since we are not interrupt driven in U-Boot and always
1230 * handle only one URB at a time, we cannot assume the
1231 * transaction finished on the first successful return from
1232 * hc_interrupt().. unless the flag for current URB is set,
1233 * meaning that all TD's to/from device got actually
1234 * transferred and processed. If the current URB is not
1235 * finished we need to re-iterate this loop so as
1236 * hc_interrupt() gets called again as there needs to be some
1237 * more TD's to process still */
1238 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
wdenkde887eb2003-09-10 18:20:28 +00001239 /* 0xff is returned for an SF-interrupt */
1240 break;
1241 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001242
wdenkde887eb2003-09-10 18:20:28 +00001243 if (--timeout) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001244 mdelay(1);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001245 if (!urb_finished)
1246 dbg("\%");
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +02001247
wdenkde887eb2003-09-10 18:20:28 +00001248 } else {
dzu8d7e4d12003-09-29 21:55:54 +00001249 err("CTL:TIMEOUT ");
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001250 dbg("submit_common_msg: TO status %x\n", stat);
dzu8d7e4d12003-09-29 21:55:54 +00001251 stat = USB_ST_CRC_ERR;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001252 urb_finished = 1;
wdenkde887eb2003-09-10 18:20:28 +00001253 break;
1254 }
1255 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001256
1257#if 0
dzu8d7e4d12003-09-29 21:55:54 +00001258 /* we got an Root Hub Status Change interrupt */
1259 if (got_rhsc) {
1260#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001261 ohci_dump_roothub(&gohci, 1);
dzu8d7e4d12003-09-29 21:55:54 +00001262#endif
1263 got_rhsc = 0;
1264 /* abuse timeout */
1265 timeout = rh_check_port_status(&gohci);
1266 if (timeout >= 0) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001267#if 0 /* this does nothing useful, but leave it here
1268 in case that changes */
dzu8d7e4d12003-09-29 21:55:54 +00001269 /* the called routine adds 1 to the passed value */
1270 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1271#endif
1272 /*
1273 * XXX
1274 * This is potentially dangerous because it assumes
1275 * that only one device is ever plugged in!
1276 */
1277 devgone = dev;
1278 }
1279 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001280#endif
dzu8d7e4d12003-09-29 21:55:54 +00001281
wdenkde887eb2003-09-10 18:20:28 +00001282 dev->status = stat;
wdenk9c53f402003-10-15 23:53:47 +00001283 dev->act_len = transfer_len;
wdenkde887eb2003-09-10 18:20:28 +00001284
1285#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001286 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)",
1287 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001288#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001289 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001290#endif
1291
1292 /* free TDs in urb_priv */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001293 urb_free_priv(&urb_priv);
wdenkde887eb2003-09-10 18:20:28 +00001294 return 0;
1295}
1296
1297/* submit routines called from usb.c */
1298int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001299 int transfer_len)
wdenkde887eb2003-09-10 18:20:28 +00001300{
1301 info("submit_bulk_msg");
1302 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1303}
1304
1305int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001306 int transfer_len, struct devrequest *setup)
wdenkde887eb2003-09-10 18:20:28 +00001307{
1308 int maxsize = usb_maxpacket(dev, pipe);
1309
1310 info("submit_control_msg");
1311#ifdef DEBUG
1312 urb_priv.actual_length = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001313 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1314 usb_pipein(pipe));
wdenkde887eb2003-09-10 18:20:28 +00001315#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001316 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001317#endif
1318 if (!maxsize) {
1319 err("submit_control_message: pipesize for pipe %lx is zero",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001320 pipe);
wdenkde887eb2003-09-10 18:20:28 +00001321 return -1;
1322 }
dzu8d7e4d12003-09-29 21:55:54 +00001323 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1324 gohci.rh.dev = dev;
wdenkde887eb2003-09-10 18:20:28 +00001325 /* root hub - redirect */
1326 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001327 setup);
dzu8d7e4d12003-09-29 21:55:54 +00001328 }
wdenkde887eb2003-09-10 18:20:28 +00001329
1330 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1331}
1332
1333int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001334 int transfer_len, int interval)
wdenkde887eb2003-09-10 18:20:28 +00001335{
1336 info("submit_int_msg");
1337 return -1;
1338}
1339
1340/*-------------------------------------------------------------------------*
1341 * HC functions
1342 *-------------------------------------------------------------------------*/
1343
1344/* reset the HC and BUS */
1345
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001346static int hc_reset(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001347{
1348 int timeout = 30;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001349 int smm_timeout = 50; /* 0,5 sec */
wdenkde887eb2003-09-10 18:20:28 +00001350
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001351 if (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1352 /* SMM owns the HC - request ownership */
1353 writel(OHCI_OCR, &ohci->regs->cmdstatus);
wdenkde887eb2003-09-10 18:20:28 +00001354 info("USB HC TakeOver from SMM");
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001355 while (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001356 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001357 if (--smm_timeout == 0) {
1358 err("USB HC TakeOver failed!");
1359 return -1;
1360 }
1361 }
1362 }
1363
1364 /* Disable HC interrupts */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001365 writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
wdenkde887eb2003-09-10 18:20:28 +00001366
1367 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001368 ohci->slot_name, readl(&ohci->regs->control));
wdenkde887eb2003-09-10 18:20:28 +00001369
wdenk9c53f402003-10-15 23:53:47 +00001370 /* Reset USB (needed by some controllers) */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001371 writel(0, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +00001372
1373 /* HC Reset requires max 10 us delay */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001374 writel(OHCI_HCR, &ohci->regs->cmdstatus);
1375 while ((readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
wdenkde887eb2003-09-10 18:20:28 +00001376 if (--timeout == 0) {
1377 err("USB HC reset timed out!");
1378 return -1;
1379 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001380 udelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001381 }
1382 return 0;
1383}
1384
1385/*-------------------------------------------------------------------------*/
1386
1387/* Start an OHCI controller, set the BUS operational
1388 * enable interrupts
1389 * connect the virtual root hub */
1390
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001391static int hc_start(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001392{
wdenk9c53f402003-10-15 23:53:47 +00001393 __u32 mask;
1394 unsigned int fminterval;
wdenkde887eb2003-09-10 18:20:28 +00001395
1396 ohci->disabled = 1;
1397
1398 /* Tell the controller where the control and bulk lists are
1399 * The lists are empty now. */
1400
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001401 writel(0, &ohci->regs->ed_controlhead);
1402 writel(0, &ohci->regs->ed_bulkhead);
wdenkde887eb2003-09-10 18:20:28 +00001403
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001404 /* a reset clears this */
1405 writel((__u32) ohci->hcca, &ohci->regs->hcca);
wdenkde887eb2003-09-10 18:20:28 +00001406
wdenk9c53f402003-10-15 23:53:47 +00001407 fminterval = 0x2edf;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001408 writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
wdenkde887eb2003-09-10 18:20:28 +00001409 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001410 writel(fminterval, &ohci->regs->fminterval);
1411 writel(0x628, &ohci->regs->lsthresh);
wdenkde887eb2003-09-10 18:20:28 +00001412
wdenk9c53f402003-10-15 23:53:47 +00001413 /* start controller operations */
1414 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
wdenkde887eb2003-09-10 18:20:28 +00001415 ohci->disabled = 0;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001416 writel(ohci->hc_control, &ohci->regs->control);
wdenkde887eb2003-09-10 18:20:28 +00001417
dzu8d7e4d12003-09-29 21:55:54 +00001418 /* disable all interrupts */
1419 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001420 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1421 OHCI_INTR_OC | OHCI_INTR_MIE);
1422 writel(mask, &ohci->regs->intrdisable);
dzu8d7e4d12003-09-29 21:55:54 +00001423 /* clear all interrupts */
1424 mask &= ~OHCI_INTR_MIE;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001425 writel(mask, &ohci->regs->intrstatus);
dzu8d7e4d12003-09-29 21:55:54 +00001426 /* Choose the interrupts we care about now - but w/o MIE */
1427 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001428 writel(mask, &ohci->regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001429
1430#ifdef OHCI_USE_NPS
1431 /* required for AMD-756 and some Mac platforms */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001432 writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1433 &ohci->regs->roothub.a);
1434 writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1435#endif /* OHCI_USE_NPS */
wdenkde887eb2003-09-10 18:20:28 +00001436
wdenkde887eb2003-09-10 18:20:28 +00001437 /* POTPGT delay is bits 24-31, in 2 ms units. */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001438 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
wdenkde887eb2003-09-10 18:20:28 +00001439
1440 /* connect the virtual root hub */
1441 ohci->rh.devnum = 0;
1442
1443 return 0;
1444}
1445
1446/*-------------------------------------------------------------------------*/
1447
1448/* an interrupt happens */
1449
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001450static int hc_interrupt(void)
wdenkde887eb2003-09-10 18:20:28 +00001451{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001452 struct ohci *ohci = &gohci;
wdenkde887eb2003-09-10 18:20:28 +00001453 struct ohci_regs *regs = ohci->regs;
wdenk9c53f402003-10-15 23:53:47 +00001454 int ints;
wdenkde887eb2003-09-10 18:20:28 +00001455 int stat = -1;
1456
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001457 if ((ohci->hcca->done_head != 0) &&
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001458 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001459
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001460 ints = OHCI_INTR_WDH;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001461
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001462 } else {
1463 ints = readl(&regs->intrstatus);
1464 if (ints == ~(u32) 0) {
1465 ohci->disabled++;
1466 err("%s device removed!", ohci->slot_name);
1467 return -1;
1468 }
1469 ints &= readl(&regs->intrenable);
1470 if (ints == 0) {
1471 dbg("hc_interrupt: returning..\n");
1472 return 0xff;
1473 }
wdenkde887eb2003-09-10 18:20:28 +00001474 }
1475
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001476 /* dbg("Interrupt: %x frame: %x", ints,
1477 le16_to_cpu(ohci->hcca->frame_no)); */
wdenkde887eb2003-09-10 18:20:28 +00001478
dzu8d7e4d12003-09-29 21:55:54 +00001479 if (ints & OHCI_INTR_RHSC) {
1480 got_rhsc = 1;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001481 stat = 0xff;
dzu8d7e4d12003-09-29 21:55:54 +00001482 }
1483
wdenkde887eb2003-09-10 18:20:28 +00001484 if (ints & OHCI_INTR_UE) {
1485 ohci->disabled++;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001486 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1487 ohci->slot_name);
wdenkde887eb2003-09-10 18:20:28 +00001488 /* e.g. due to PCI Master/Target Abort */
1489
1490#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001491 ohci_dump(ohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001492#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001493 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001494#endif
1495 /* FIXME: be optimistic, hope that bug won't repeat often. */
1496 /* Make some non-interrupt context restart the controller. */
1497 /* Count and limit the retries though; either hardware or */
1498 /* software errors can go forever... */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001499 hc_reset(ohci);
wdenkde887eb2003-09-10 18:20:28 +00001500 return -1;
1501 }
1502
1503 if (ints & OHCI_INTR_WDH) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001504 mdelay(1);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001505
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001506 writel(OHCI_INTR_WDH, &regs->intrdisable);
1507 stat = dl_done_list(&gohci, dl_reverse_done_list(&gohci));
1508 writel(OHCI_INTR_WDH, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001509 }
1510
1511 if (ints & OHCI_INTR_SO) {
1512 dbg("USB Schedule overrun\n");
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001513 writel(OHCI_INTR_SO, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001514 stat = -1;
1515 }
1516
1517 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1518 if (ints & OHCI_INTR_SF) {
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001519 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001520 mdelay(1);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001521 writel(OHCI_INTR_SF, &regs->intrdisable);
wdenkde887eb2003-09-10 18:20:28 +00001522 if (ohci->ed_rm_list[frame] != NULL)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001523 writel(OHCI_INTR_SF, &regs->intrenable);
wdenkde887eb2003-09-10 18:20:28 +00001524 stat = 0xff;
1525 }
1526
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001527 writel(ints, &regs->intrstatus);
wdenkde887eb2003-09-10 18:20:28 +00001528 return stat;
1529}
1530
1531/*-------------------------------------------------------------------------*/
1532
1533/*-------------------------------------------------------------------------*/
1534
1535/* De-allocate all resources.. */
1536
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001537static void hc_release_ohci(struct ohci *ohci)
wdenkde887eb2003-09-10 18:20:28 +00001538{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001539 dbg("USB HC release ohci usb-%s", ohci->slot_name);
wdenkde887eb2003-09-10 18:20:28 +00001540
1541 if (!ohci->disabled)
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001542 hc_reset(ohci);
wdenkde887eb2003-09-10 18:20:28 +00001543}
1544
1545/*-------------------------------------------------------------------------*/
1546
1547/*
1548 * low level initalisation routine, called from usb.c
1549 */
1550static char ohci_inited = 0;
1551
Troy Kisky8f9c49d2013-10-10 15:27:56 -07001552int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
wdenkde887eb2003-09-10 18:20:28 +00001553{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001554 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1555 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenkde887eb2003-09-10 18:20:28 +00001556
wdenk9c53f402003-10-15 23:53:47 +00001557 /*
1558 * Set the 48 MHz UPLL clocking. Values are taken from
1559 * "PLL value selection guide", 6-23, s3c2400_UM.pdf.
1560 */
C Nauman383c43e2010-10-26 23:04:31 +09001561 clk_power->upllcon = ((40 << 12) + (1 << 4) + 2);
1562 gpio->misccr |= 0x8; /* 1 = use pads related USB for USB host */
wdenkde887eb2003-09-10 18:20:28 +00001563
wdenk9c53f402003-10-15 23:53:47 +00001564 /*
1565 * Enable USB host clock.
1566 */
C Nauman383c43e2010-10-26 23:04:31 +09001567 clk_power->clkcon |= (1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001568
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001569 memset(&gohci, 0, sizeof(struct ohci));
1570 memset(&urb_priv, 0, sizeof(struct urb_priv));
wdenkde887eb2003-09-10 18:20:28 +00001571
1572 /* align the storage */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001573 if ((__u32) &ghcca[0] & 0xff) {
wdenkde887eb2003-09-10 18:20:28 +00001574 err("HCCA not aligned!!");
1575 return -1;
1576 }
1577 phcca = &ghcca[0];
1578 info("aligned ghcca %p", phcca);
1579 memset(&ohci_dev, 0, sizeof(struct ohci_device));
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001580 if ((__u32) &ohci_dev.ed[0] & 0x7) {
wdenkde887eb2003-09-10 18:20:28 +00001581 err("EDs not aligned!!");
1582 return -1;
1583 }
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001584 memset(gtd, 0, sizeof(struct td) * (NUM_TD + 1));
1585 if ((__u32) gtd & 0x7) {
wdenkde887eb2003-09-10 18:20:28 +00001586 err("TDs not aligned!!");
1587 return -1;
1588 }
1589 ptd = gtd;
1590 gohci.hcca = phcca;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001591 memset(phcca, 0, sizeof(struct ohci_hcca));
wdenkde887eb2003-09-10 18:20:28 +00001592
1593 gohci.disabled = 1;
1594 gohci.sleeping = 0;
1595 gohci.irq = -1;
1596 gohci.regs = (struct ohci_regs *)S3C24X0_USB_HOST_BASE;
1597
1598 gohci.flags = 0;
1599 gohci.slot_name = "s3c2400";
1600
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001601 if (hc_reset(&gohci) < 0) {
1602 hc_release_ohci(&gohci);
wdenk9c53f402003-10-15 23:53:47 +00001603 /* Initialization failed */
C Nauman383c43e2010-10-26 23:04:31 +09001604 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001605 return -1;
1606 }
1607
1608 /* FIXME this is a second HC reset; why?? */
Wolfgang Denk10e4f542006-03-11 23:07:09 +01001609 gohci.hc_control = OHCI_USB_RESET;
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001610 writel(gohci.hc_control, &gohci.regs->control);
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001611 mdelay(10);
wdenkde887eb2003-09-10 18:20:28 +00001612
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001613 if (hc_start(&gohci) < 0) {
1614 err("can't start usb-%s", gohci.slot_name);
1615 hc_release_ohci(&gohci);
wdenk9c53f402003-10-15 23:53:47 +00001616 /* Initialization failed */
C Nauman383c43e2010-10-26 23:04:31 +09001617 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001618 return -1;
1619 }
wdenkde887eb2003-09-10 18:20:28 +00001620#ifdef DEBUG
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001621 ohci_dump(&gohci, 1);
wdenkde887eb2003-09-10 18:20:28 +00001622#else
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001623 mdelay(1);
wdenkde887eb2003-09-10 18:20:28 +00001624#endif
1625 ohci_inited = 1;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001626 urb_finished = 1;
1627
wdenkde887eb2003-09-10 18:20:28 +00001628 return 0;
1629}
1630
Lucas Stacha3231282012-09-26 00:14:34 +02001631int usb_lowlevel_stop(int index)
wdenkde887eb2003-09-10 18:20:28 +00001632{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001633 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
wdenkde887eb2003-09-10 18:20:28 +00001634
1635 /* this gets called really early - before the controller has */
1636 /* even been initialized! */
1637 if (!ohci_inited)
1638 return 0;
1639 /* TODO release any interrupts, etc. */
1640 /* call hc_release_ohci() here ? */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +09001641 hc_reset(&gohci);
wdenkde887eb2003-09-10 18:20:28 +00001642 /* may not want to do this */
C Nauman383c43e2010-10-26 23:04:31 +09001643 clk_power->clkcon &= ~(1 << 4);
wdenkde887eb2003-09-10 18:20:28 +00001644 return 0;
1645}
1646
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +09001647#endif /* defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0) */
Marek Vasute917a7c2012-07-21 05:02:22 +00001648
1649#if defined(CONFIG_USB_OHCI_NEW) && \
1650 defined(CONFIG_SYS_USB_OHCI_CPU_INIT) && \
1651 defined(CONFIG_S3C24X0)
1652
1653int usb_cpu_init(void)
1654{
1655 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1656 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
1657
1658 /*
1659 * Set the 48 MHz UPLL clocking. Values are taken from
1660 * "PLL value selection guide", 6-23, s3c2400_UM.pdf.
1661 */
1662 writel((40 << 12) + (1 << 4) + 2, &clk_power->upllcon);
1663 /* 1 = use pads related USB for USB host */
1664 writel(readl(&gpio->misccr) | 0x8, &gpio->misccr);
1665
1666 /*
1667 * Enable USB host clock.
1668 */
1669 writel(readl(&clk_power->clkcon) | (1 << 4), &clk_power->clkcon);
1670
1671 return 0;
1672}
1673
1674int usb_cpu_stop(void)
1675{
1676 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1677 /* may not want to do this */
1678 writel(readl(&clk_power->clkcon) & ~(1 << 4), &clk_power->clkcon);
1679 return 0;
1680}
1681
1682int usb_cpu_init_fail(void)
1683{
1684 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1685 writel(readl(&clk_power->clkcon) & ~(1 << 4), &clk_power->clkcon);
1686 return 0;
1687}
1688
1689#endif /* defined(CONFIG_USB_OHCI_NEW) && \
1690 defined(CONFIG_SYS_USB_OHCI_CPU_INIT) && \
1691 defined(CONFIG_S3C24X0) */