Commit | Line | Data |
---|---|---|
b92a78e5 RG |
1 | /* |
2 | * Copyright (c) 2008 Rodolfo Giometti <giometti@linux.it> | |
3 | * Copyright (c) 2008 Eurotech S.p.A. <info@eurtech.it> | |
4 | * | |
5 | * This code is *strongly* based on EHCI-HCD code by David Brownell since | |
6 | * the chip is a quasi-EHCI compatible. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | * for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software Foundation, | |
20 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/pci.h> | |
25 | #include <linux/dmapool.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/ioport.h> | |
29 | #include <linux/sched.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/errno.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/timer.h> | |
34 | #include <linux/list.h> | |
35 | #include <linux/interrupt.h> | |
b92a78e5 | 36 | #include <linux/usb.h> |
27729aad | 37 | #include <linux/usb/hcd.h> |
b92a78e5 RG |
38 | #include <linux/moduleparam.h> |
39 | #include <linux/dma-mapping.h> | |
40 | #include <linux/io.h> | |
41 | ||
b92a78e5 | 42 | #include <asm/irq.h> |
b92a78e5 RG |
43 | #include <asm/unaligned.h> |
44 | ||
45 | #include <linux/irq.h> | |
46 | #include <linux/platform_device.h> | |
47 | ||
48 | #include "oxu210hp.h" | |
49 | ||
50 | #define DRIVER_VERSION "0.0.50" | |
51 | ||
52 | /* | |
53 | * Main defines | |
54 | */ | |
55 | ||
56 | #define oxu_dbg(oxu, fmt, args...) \ | |
57 | dev_dbg(oxu_to_hcd(oxu)->self.controller , fmt , ## args) | |
58 | #define oxu_err(oxu, fmt, args...) \ | |
59 | dev_err(oxu_to_hcd(oxu)->self.controller , fmt , ## args) | |
60 | #define oxu_info(oxu, fmt, args...) \ | |
61 | dev_info(oxu_to_hcd(oxu)->self.controller , fmt , ## args) | |
62 | ||
63 | static inline struct usb_hcd *oxu_to_hcd(struct oxu_hcd *oxu) | |
64 | { | |
65 | return container_of((void *) oxu, struct usb_hcd, hcd_priv); | |
66 | } | |
67 | ||
68 | static inline struct oxu_hcd *hcd_to_oxu(struct usb_hcd *hcd) | |
69 | { | |
70 | return (struct oxu_hcd *) (hcd->hcd_priv); | |
71 | } | |
72 | ||
73 | /* | |
74 | * Debug stuff | |
75 | */ | |
76 | ||
77 | #undef OXU_URB_TRACE | |
78 | #undef OXU_VERBOSE_DEBUG | |
79 | ||
80 | #ifdef OXU_VERBOSE_DEBUG | |
81 | #define oxu_vdbg oxu_dbg | |
82 | #else | |
83 | #define oxu_vdbg(oxu, fmt, args...) /* Nop */ | |
84 | #endif | |
85 | ||
86 | #ifdef DEBUG | |
87 | ||
88 | static int __attribute__((__unused__)) | |
89 | dbg_status_buf(char *buf, unsigned len, const char *label, u32 status) | |
90 | { | |
91 | return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s", | |
92 | label, label[0] ? " " : "", status, | |
93 | (status & STS_ASS) ? " Async" : "", | |
94 | (status & STS_PSS) ? " Periodic" : "", | |
95 | (status & STS_RECL) ? " Recl" : "", | |
96 | (status & STS_HALT) ? " Halt" : "", | |
97 | (status & STS_IAA) ? " IAA" : "", | |
98 | (status & STS_FATAL) ? " FATAL" : "", | |
99 | (status & STS_FLR) ? " FLR" : "", | |
100 | (status & STS_PCD) ? " PCD" : "", | |
101 | (status & STS_ERR) ? " ERR" : "", | |
102 | (status & STS_INT) ? " INT" : "" | |
103 | ); | |
104 | } | |
105 | ||
106 | static int __attribute__((__unused__)) | |
107 | dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable) | |
108 | { | |
109 | return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s", | |
110 | label, label[0] ? " " : "", enable, | |
111 | (enable & STS_IAA) ? " IAA" : "", | |
112 | (enable & STS_FATAL) ? " FATAL" : "", | |
113 | (enable & STS_FLR) ? " FLR" : "", | |
114 | (enable & STS_PCD) ? " PCD" : "", | |
115 | (enable & STS_ERR) ? " ERR" : "", | |
116 | (enable & STS_INT) ? " INT" : "" | |
117 | ); | |
118 | } | |
119 | ||
120 | static const char *const fls_strings[] = | |
121 | { "1024", "512", "256", "??" }; | |
122 | ||
123 | static int dbg_command_buf(char *buf, unsigned len, | |
124 | const char *label, u32 command) | |
125 | { | |
126 | return scnprintf(buf, len, | |
127 | "%s%scommand %06x %s=%d ithresh=%d%s%s%s%s period=%s%s %s", | |
128 | label, label[0] ? " " : "", command, | |
129 | (command & CMD_PARK) ? "park" : "(park)", | |
130 | CMD_PARK_CNT(command), | |
131 | (command >> 16) & 0x3f, | |
132 | (command & CMD_LRESET) ? " LReset" : "", | |
133 | (command & CMD_IAAD) ? " IAAD" : "", | |
134 | (command & CMD_ASE) ? " Async" : "", | |
135 | (command & CMD_PSE) ? " Periodic" : "", | |
136 | fls_strings[(command >> 2) & 0x3], | |
137 | (command & CMD_RESET) ? " Reset" : "", | |
138 | (command & CMD_RUN) ? "RUN" : "HALT" | |
139 | ); | |
140 | } | |
141 | ||
142 | static int dbg_port_buf(char *buf, unsigned len, const char *label, | |
143 | int port, u32 status) | |
144 | { | |
145 | char *sig; | |
146 | ||
147 | /* signaling state */ | |
148 | switch (status & (3 << 10)) { | |
149 | case 0 << 10: | |
150 | sig = "se0"; | |
151 | break; | |
152 | case 1 << 10: | |
153 | sig = "k"; /* low speed */ | |
154 | break; | |
155 | case 2 << 10: | |
156 | sig = "j"; | |
157 | break; | |
158 | default: | |
159 | sig = "?"; | |
160 | break; | |
161 | } | |
162 | ||
163 | return scnprintf(buf, len, | |
164 | "%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s", | |
165 | label, label[0] ? " " : "", port, status, | |
166 | (status & PORT_POWER) ? " POWER" : "", | |
167 | (status & PORT_OWNER) ? " OWNER" : "", | |
168 | sig, | |
169 | (status & PORT_RESET) ? " RESET" : "", | |
170 | (status & PORT_SUSPEND) ? " SUSPEND" : "", | |
171 | (status & PORT_RESUME) ? " RESUME" : "", | |
172 | (status & PORT_OCC) ? " OCC" : "", | |
173 | (status & PORT_OC) ? " OC" : "", | |
174 | (status & PORT_PEC) ? " PEC" : "", | |
175 | (status & PORT_PE) ? " PE" : "", | |
176 | (status & PORT_CSC) ? " CSC" : "", | |
177 | (status & PORT_CONNECT) ? " CONNECT" : "" | |
178 | ); | |
179 | } | |
180 | ||
181 | #else | |
182 | ||
183 | static inline int __attribute__((__unused__)) | |
184 | dbg_status_buf(char *buf, unsigned len, const char *label, u32 status) | |
185 | { return 0; } | |
186 | ||
187 | static inline int __attribute__((__unused__)) | |
188 | dbg_command_buf(char *buf, unsigned len, const char *label, u32 command) | |
189 | { return 0; } | |
190 | ||
191 | static inline int __attribute__((__unused__)) | |
192 | dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable) | |
193 | { return 0; } | |
194 | ||
195 | static inline int __attribute__((__unused__)) | |
196 | dbg_port_buf(char *buf, unsigned len, const char *label, int port, u32 status) | |
197 | { return 0; } | |
198 | ||
199 | #endif /* DEBUG */ | |
200 | ||
201 | /* functions have the "wrong" filename when they're output... */ | |
202 | #define dbg_status(oxu, label, status) { \ | |
203 | char _buf[80]; \ | |
204 | dbg_status_buf(_buf, sizeof _buf, label, status); \ | |
205 | oxu_dbg(oxu, "%s\n", _buf); \ | |
206 | } | |
207 | ||
208 | #define dbg_cmd(oxu, label, command) { \ | |
209 | char _buf[80]; \ | |
210 | dbg_command_buf(_buf, sizeof _buf, label, command); \ | |
211 | oxu_dbg(oxu, "%s\n", _buf); \ | |
212 | } | |
213 | ||
214 | #define dbg_port(oxu, label, port, status) { \ | |
215 | char _buf[80]; \ | |
216 | dbg_port_buf(_buf, sizeof _buf, label, port, status); \ | |
217 | oxu_dbg(oxu, "%s\n", _buf); \ | |
218 | } | |
219 | ||
220 | /* | |
221 | * Module parameters | |
222 | */ | |
223 | ||
224 | /* Initial IRQ latency: faster than hw default */ | |
225 | static int log2_irq_thresh; /* 0 to 6 */ | |
226 | module_param(log2_irq_thresh, int, S_IRUGO); | |
227 | MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes"); | |
228 | ||
229 | /* Initial park setting: slower than hw default */ | |
230 | static unsigned park; | |
231 | module_param(park, uint, S_IRUGO); | |
232 | MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets"); | |
233 | ||
234 | /* For flakey hardware, ignore overcurrent indicators */ | |
90ab5ee9 | 235 | static bool ignore_oc; |
b92a78e5 RG |
236 | module_param(ignore_oc, bool, S_IRUGO); |
237 | MODULE_PARM_DESC(ignore_oc, "ignore bogus hardware overcurrent indications"); | |
238 | ||
239 | ||
240 | static void ehci_work(struct oxu_hcd *oxu); | |
241 | static int oxu_hub_control(struct usb_hcd *hcd, | |
242 | u16 typeReq, u16 wValue, u16 wIndex, | |
243 | char *buf, u16 wLength); | |
244 | ||
245 | /* | |
246 | * Local functions | |
247 | */ | |
248 | ||
249 | /* Low level read/write registers functions */ | |
250 | static inline u32 oxu_readl(void *base, u32 reg) | |
251 | { | |
252 | return readl(base + reg); | |
253 | } | |
254 | ||
255 | static inline void oxu_writel(void *base, u32 reg, u32 val) | |
256 | { | |
257 | writel(val, base + reg); | |
258 | } | |
259 | ||
260 | static inline void timer_action_done(struct oxu_hcd *oxu, | |
261 | enum ehci_timer_action action) | |
262 | { | |
263 | clear_bit(action, &oxu->actions); | |
264 | } | |
265 | ||
266 | static inline void timer_action(struct oxu_hcd *oxu, | |
267 | enum ehci_timer_action action) | |
268 | { | |
269 | if (!test_and_set_bit(action, &oxu->actions)) { | |
270 | unsigned long t; | |
271 | ||
272 | switch (action) { | |
273 | case TIMER_IAA_WATCHDOG: | |
274 | t = EHCI_IAA_JIFFIES; | |
275 | break; | |
276 | case TIMER_IO_WATCHDOG: | |
277 | t = EHCI_IO_JIFFIES; | |
278 | break; | |
279 | case TIMER_ASYNC_OFF: | |
280 | t = EHCI_ASYNC_JIFFIES; | |
281 | break; | |
282 | case TIMER_ASYNC_SHRINK: | |
283 | default: | |
284 | t = EHCI_SHRINK_JIFFIES; | |
285 | break; | |
286 | } | |
287 | t += jiffies; | |
288 | /* all timings except IAA watchdog can be overridden. | |
289 | * async queue SHRINK often precedes IAA. while it's ready | |
290 | * to go OFF neither can matter, and afterwards the IO | |
291 | * watchdog stops unless there's still periodic traffic. | |
292 | */ | |
293 | if (action != TIMER_IAA_WATCHDOG | |
294 | && t > oxu->watchdog.expires | |
295 | && timer_pending(&oxu->watchdog)) | |
296 | return; | |
297 | mod_timer(&oxu->watchdog, t); | |
298 | } | |
299 | } | |
300 | ||
301 | /* | |
302 | * handshake - spin reading hc until handshake completes or fails | |
303 | * @ptr: address of hc register to be read | |
304 | * @mask: bits to look at in result of read | |
305 | * @done: value of those bits when handshake succeeds | |
306 | * @usec: timeout in microseconds | |
307 | * | |
308 | * Returns negative errno, or zero on success | |
309 | * | |
310 | * Success happens when the "mask" bits have the specified value (hardware | |
311 | * handshake done). There are two failure modes: "usec" have passed (major | |
312 | * hardware flakeout), or the register reads as all-ones (hardware removed). | |
313 | * | |
314 | * That last failure should_only happen in cases like physical cardbus eject | |
315 | * before driver shutdown. But it also seems to be caused by bugs in cardbus | |
316 | * bridge shutdown: shutting down the bridge before the devices using it. | |
317 | */ | |
318 | static int handshake(struct oxu_hcd *oxu, void __iomem *ptr, | |
319 | u32 mask, u32 done, int usec) | |
320 | { | |
321 | u32 result; | |
322 | ||
323 | do { | |
324 | result = readl(ptr); | |
325 | if (result == ~(u32)0) /* card removed */ | |
326 | return -ENODEV; | |
327 | result &= mask; | |
328 | if (result == done) | |
329 | return 0; | |
330 | udelay(1); | |
331 | usec--; | |
332 | } while (usec > 0); | |
333 | return -ETIMEDOUT; | |
334 | } | |
335 | ||
336 | /* Force HC to halt state from unknown (EHCI spec section 2.3) */ | |
337 | static int ehci_halt(struct oxu_hcd *oxu) | |
338 | { | |
339 | u32 temp = readl(&oxu->regs->status); | |
340 | ||
341 | /* disable any irqs left enabled by previous code */ | |
342 | writel(0, &oxu->regs->intr_enable); | |
343 | ||
344 | if ((temp & STS_HALT) != 0) | |
345 | return 0; | |
346 | ||
347 | temp = readl(&oxu->regs->command); | |
348 | temp &= ~CMD_RUN; | |
349 | writel(temp, &oxu->regs->command); | |
350 | return handshake(oxu, &oxu->regs->status, | |
351 | STS_HALT, STS_HALT, 16 * 125); | |
352 | } | |
353 | ||
354 | /* Put TDI/ARC silicon into EHCI mode */ | |
355 | static void tdi_reset(struct oxu_hcd *oxu) | |
356 | { | |
357 | u32 __iomem *reg_ptr; | |
358 | u32 tmp; | |
359 | ||
360 | reg_ptr = (u32 __iomem *)(((u8 __iomem *)oxu->regs) + 0x68); | |
361 | tmp = readl(reg_ptr); | |
362 | tmp |= 0x3; | |
363 | writel(tmp, reg_ptr); | |
364 | } | |
365 | ||
366 | /* Reset a non-running (STS_HALT == 1) controller */ | |
367 | static int ehci_reset(struct oxu_hcd *oxu) | |
368 | { | |
369 | int retval; | |
370 | u32 command = readl(&oxu->regs->command); | |
371 | ||
372 | command |= CMD_RESET; | |
373 | dbg_cmd(oxu, "reset", command); | |
374 | writel(command, &oxu->regs->command); | |
375 | oxu_to_hcd(oxu)->state = HC_STATE_HALT; | |
376 | oxu->next_statechange = jiffies; | |
377 | retval = handshake(oxu, &oxu->regs->command, | |
378 | CMD_RESET, 0, 250 * 1000); | |
379 | ||
380 | if (retval) | |
381 | return retval; | |
382 | ||
383 | tdi_reset(oxu); | |
384 | ||
385 | return retval; | |
386 | } | |
387 | ||
388 | /* Idle the controller (from running) */ | |
389 | static void ehci_quiesce(struct oxu_hcd *oxu) | |
390 | { | |
391 | u32 temp; | |
392 | ||
393 | #ifdef DEBUG | |
394 | if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) | |
395 | BUG(); | |
396 | #endif | |
397 | ||
398 | /* wait for any schedule enables/disables to take effect */ | |
399 | temp = readl(&oxu->regs->command) << 10; | |
400 | temp &= STS_ASS | STS_PSS; | |
401 | if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS, | |
402 | temp, 16 * 125) != 0) { | |
403 | oxu_to_hcd(oxu)->state = HC_STATE_HALT; | |
404 | return; | |
405 | } | |
406 | ||
407 | /* then disable anything that's still active */ | |
408 | temp = readl(&oxu->regs->command); | |
409 | temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE); | |
410 | writel(temp, &oxu->regs->command); | |
411 | ||
412 | /* hardware can take 16 microframes to turn off ... */ | |
413 | if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS, | |
414 | 0, 16 * 125) != 0) { | |
415 | oxu_to_hcd(oxu)->state = HC_STATE_HALT; | |
416 | return; | |
417 | } | |
418 | } | |
419 | ||
420 | static int check_reset_complete(struct oxu_hcd *oxu, int index, | |
421 | u32 __iomem *status_reg, int port_status) | |
422 | { | |
423 | if (!(port_status & PORT_CONNECT)) { | |
424 | oxu->reset_done[index] = 0; | |
425 | return port_status; | |
426 | } | |
427 | ||
428 | /* if reset finished and it's still not enabled -- handoff */ | |
429 | if (!(port_status & PORT_PE)) { | |
430 | oxu_dbg(oxu, "Failed to enable port %d on root hub TT\n", | |
431 | index+1); | |
432 | return port_status; | |
433 | } else | |
434 | oxu_dbg(oxu, "port %d high speed\n", index + 1); | |
435 | ||
436 | return port_status; | |
437 | } | |
438 | ||
439 | static void ehci_hub_descriptor(struct oxu_hcd *oxu, | |
440 | struct usb_hub_descriptor *desc) | |
441 | { | |
442 | int ports = HCS_N_PORTS(oxu->hcs_params); | |
443 | u16 temp; | |
444 | ||
445 | desc->bDescriptorType = 0x29; | |
446 | desc->bPwrOn2PwrGood = 10; /* oxu 1.0, 2.3.9 says 20ms max */ | |
447 | desc->bHubContrCurrent = 0; | |
448 | ||
449 | desc->bNbrPorts = ports; | |
450 | temp = 1 + (ports / 8); | |
451 | desc->bDescLength = 7 + 2 * temp; | |
452 | ||
da13051c | 453 | /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */ |
dbe79bbe JY |
454 | memset(&desc->u.hs.DeviceRemovable[0], 0, temp); |
455 | memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp); | |
b92a78e5 RG |
456 | |
457 | temp = 0x0008; /* per-port overcurrent reporting */ | |
458 | if (HCS_PPC(oxu->hcs_params)) | |
459 | temp |= 0x0001; /* per-port power control */ | |
460 | else | |
461 | temp |= 0x0002; /* no power switching */ | |
462 | desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp); | |
463 | } | |
464 | ||
465 | ||
466 | /* Allocate an OXU210HP on-chip memory data buffer | |
467 | * | |
468 | * An on-chip memory data buffer is required for each OXU210HP USB transfer. | |
469 | * Each transfer descriptor has one or more on-chip memory data buffers. | |
470 | * | |
471 | * Data buffers are allocated from a fix sized pool of data blocks. | |
472 | * To minimise fragmentation and give reasonable memory utlisation, | |
473 | * data buffers are allocated with sizes the power of 2 multiples of | |
474 | * the block size, starting on an address a multiple of the allocated size. | |
475 | * | |
476 | * FIXME: callers of this function require a buffer to be allocated for | |
477 | * len=0. This is a waste of on-chip memory and should be fix. Then this | |
478 | * function should be changed to not allocate a buffer for len=0. | |
479 | */ | |
480 | static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len) | |
481 | { | |
482 | int n_blocks; /* minium blocks needed to hold len */ | |
483 | int a_blocks; /* blocks allocated */ | |
484 | int i, j; | |
485 | ||
486 | /* Don't allocte bigger than supported */ | |
487 | if (len > BUFFER_SIZE * BUFFER_NUM) { | |
488 | oxu_err(oxu, "buffer too big (%d)\n", len); | |
489 | return -ENOMEM; | |
490 | } | |
491 | ||
492 | spin_lock(&oxu->mem_lock); | |
493 | ||
494 | /* Number of blocks needed to hold len */ | |
495 | n_blocks = (len + BUFFER_SIZE - 1) / BUFFER_SIZE; | |
496 | ||
497 | /* Round the number of blocks up to the power of 2 */ | |
498 | for (a_blocks = 1; a_blocks < n_blocks; a_blocks <<= 1) | |
499 | ; | |
500 | ||
501 | /* Find a suitable available data buffer */ | |
502 | for (i = 0; i < BUFFER_NUM; | |
503 | i += max(a_blocks, (int)oxu->db_used[i])) { | |
504 | ||
505 | /* Check all the required blocks are available */ | |
506 | for (j = 0; j < a_blocks; j++) | |
507 | if (oxu->db_used[i + j]) | |
508 | break; | |
509 | ||
510 | if (j != a_blocks) | |
511 | continue; | |
512 | ||
513 | /* Allocate blocks found! */ | |
514 | qtd->buffer = (void *) &oxu->mem->db_pool[i]; | |
515 | qtd->buffer_dma = virt_to_phys(qtd->buffer); | |
516 | ||
517 | qtd->qtd_buffer_len = BUFFER_SIZE * a_blocks; | |
518 | oxu->db_used[i] = a_blocks; | |
519 | ||
520 | spin_unlock(&oxu->mem_lock); | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
525 | /* Failed */ | |
526 | ||
527 | spin_unlock(&oxu->mem_lock); | |
528 | ||
529 | return -ENOMEM; | |
530 | } | |
531 | ||
532 | static void oxu_buf_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd) | |
533 | { | |
534 | int index; | |
535 | ||
536 | spin_lock(&oxu->mem_lock); | |
537 | ||
538 | index = (qtd->buffer - (void *) &oxu->mem->db_pool[0]) | |
539 | / BUFFER_SIZE; | |
540 | oxu->db_used[index] = 0; | |
541 | qtd->qtd_buffer_len = 0; | |
542 | qtd->buffer_dma = 0; | |
543 | qtd->buffer = NULL; | |
544 | ||
545 | spin_unlock(&oxu->mem_lock); | |
b92a78e5 RG |
546 | } |
547 | ||
548 | static inline void ehci_qtd_init(struct ehci_qtd *qtd, dma_addr_t dma) | |
549 | { | |
550 | memset(qtd, 0, sizeof *qtd); | |
551 | qtd->qtd_dma = dma; | |
552 | qtd->hw_token = cpu_to_le32(QTD_STS_HALT); | |
553 | qtd->hw_next = EHCI_LIST_END; | |
554 | qtd->hw_alt_next = EHCI_LIST_END; | |
555 | INIT_LIST_HEAD(&qtd->qtd_list); | |
556 | } | |
557 | ||
558 | static inline void oxu_qtd_free(struct oxu_hcd *oxu, struct ehci_qtd *qtd) | |
559 | { | |
560 | int index; | |
561 | ||
562 | if (qtd->buffer) | |
563 | oxu_buf_free(oxu, qtd); | |
564 | ||
565 | spin_lock(&oxu->mem_lock); | |
566 | ||
567 | index = qtd - &oxu->mem->qtd_pool[0]; | |
568 | oxu->qtd_used[index] = 0; | |
569 | ||
570 | spin_unlock(&oxu->mem_lock); | |
b92a78e5 RG |
571 | } |
572 | ||
573 | static struct ehci_qtd *ehci_qtd_alloc(struct oxu_hcd *oxu) | |
574 | { | |
575 | int i; | |
576 | struct ehci_qtd *qtd = NULL; | |
577 | ||
578 | spin_lock(&oxu->mem_lock); | |
579 | ||
580 | for (i = 0; i < QTD_NUM; i++) | |
581 | if (!oxu->qtd_used[i]) | |
582 | break; | |
583 | ||
584 | if (i < QTD_NUM) { | |
585 | qtd = (struct ehci_qtd *) &oxu->mem->qtd_pool[i]; | |
586 | memset(qtd, 0, sizeof *qtd); | |
587 | ||
588 | qtd->hw_token = cpu_to_le32(QTD_STS_HALT); | |
589 | qtd->hw_next = EHCI_LIST_END; | |
590 | qtd->hw_alt_next = EHCI_LIST_END; | |
591 | INIT_LIST_HEAD(&qtd->qtd_list); | |
592 | ||
593 | qtd->qtd_dma = virt_to_phys(qtd); | |
594 | ||
595 | oxu->qtd_used[i] = 1; | |
596 | } | |
597 | ||
598 | spin_unlock(&oxu->mem_lock); | |
599 | ||
600 | return qtd; | |
601 | } | |
602 | ||
603 | static void oxu_qh_free(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
604 | { | |
605 | int index; | |
606 | ||
607 | spin_lock(&oxu->mem_lock); | |
608 | ||
609 | index = qh - &oxu->mem->qh_pool[0]; | |
610 | oxu->qh_used[index] = 0; | |
611 | ||
612 | spin_unlock(&oxu->mem_lock); | |
b92a78e5 RG |
613 | } |
614 | ||
615 | static void qh_destroy(struct kref *kref) | |
616 | { | |
617 | struct ehci_qh *qh = container_of(kref, struct ehci_qh, kref); | |
618 | struct oxu_hcd *oxu = qh->oxu; | |
619 | ||
620 | /* clean qtds first, and know this is not linked */ | |
621 | if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) { | |
622 | oxu_dbg(oxu, "unused qh not empty!\n"); | |
623 | BUG(); | |
624 | } | |
625 | if (qh->dummy) | |
626 | oxu_qtd_free(oxu, qh->dummy); | |
627 | oxu_qh_free(oxu, qh); | |
628 | } | |
629 | ||
630 | static struct ehci_qh *oxu_qh_alloc(struct oxu_hcd *oxu) | |
631 | { | |
632 | int i; | |
633 | struct ehci_qh *qh = NULL; | |
634 | ||
635 | spin_lock(&oxu->mem_lock); | |
636 | ||
637 | for (i = 0; i < QHEAD_NUM; i++) | |
638 | if (!oxu->qh_used[i]) | |
639 | break; | |
640 | ||
641 | if (i < QHEAD_NUM) { | |
642 | qh = (struct ehci_qh *) &oxu->mem->qh_pool[i]; | |
643 | memset(qh, 0, sizeof *qh); | |
644 | ||
645 | kref_init(&qh->kref); | |
646 | qh->oxu = oxu; | |
647 | qh->qh_dma = virt_to_phys(qh); | |
648 | INIT_LIST_HEAD(&qh->qtd_list); | |
649 | ||
650 | /* dummy td enables safe urb queuing */ | |
651 | qh->dummy = ehci_qtd_alloc(oxu); | |
652 | if (qh->dummy == NULL) { | |
653 | oxu_dbg(oxu, "no dummy td\n"); | |
654 | oxu->qh_used[i] = 0; | |
82a5eeb9 DC |
655 | qh = NULL; |
656 | goto unlock; | |
b92a78e5 RG |
657 | } |
658 | ||
659 | oxu->qh_used[i] = 1; | |
660 | } | |
82a5eeb9 | 661 | unlock: |
b92a78e5 RG |
662 | spin_unlock(&oxu->mem_lock); |
663 | ||
664 | return qh; | |
665 | } | |
666 | ||
667 | /* to share a qh (cpu threads, or hc) */ | |
668 | static inline struct ehci_qh *qh_get(struct ehci_qh *qh) | |
669 | { | |
670 | kref_get(&qh->kref); | |
671 | return qh; | |
672 | } | |
673 | ||
674 | static inline void qh_put(struct ehci_qh *qh) | |
675 | { | |
676 | kref_put(&qh->kref, qh_destroy); | |
677 | } | |
678 | ||
679 | static void oxu_murb_free(struct oxu_hcd *oxu, struct oxu_murb *murb) | |
680 | { | |
681 | int index; | |
682 | ||
683 | spin_lock(&oxu->mem_lock); | |
684 | ||
685 | index = murb - &oxu->murb_pool[0]; | |
686 | oxu->murb_used[index] = 0; | |
687 | ||
688 | spin_unlock(&oxu->mem_lock); | |
b92a78e5 RG |
689 | } |
690 | ||
691 | static struct oxu_murb *oxu_murb_alloc(struct oxu_hcd *oxu) | |
692 | ||
693 | { | |
694 | int i; | |
695 | struct oxu_murb *murb = NULL; | |
696 | ||
697 | spin_lock(&oxu->mem_lock); | |
698 | ||
699 | for (i = 0; i < MURB_NUM; i++) | |
700 | if (!oxu->murb_used[i]) | |
701 | break; | |
702 | ||
703 | if (i < MURB_NUM) { | |
704 | murb = &(oxu->murb_pool)[i]; | |
705 | ||
706 | oxu->murb_used[i] = 1; | |
707 | } | |
708 | ||
709 | spin_unlock(&oxu->mem_lock); | |
710 | ||
711 | return murb; | |
712 | } | |
713 | ||
714 | /* The queue heads and transfer descriptors are managed from pools tied | |
715 | * to each of the "per device" structures. | |
716 | * This is the initialisation and cleanup code. | |
717 | */ | |
718 | static void ehci_mem_cleanup(struct oxu_hcd *oxu) | |
719 | { | |
720 | kfree(oxu->murb_pool); | |
721 | oxu->murb_pool = NULL; | |
722 | ||
723 | if (oxu->async) | |
724 | qh_put(oxu->async); | |
725 | oxu->async = NULL; | |
726 | ||
727 | del_timer(&oxu->urb_timer); | |
728 | ||
729 | oxu->periodic = NULL; | |
730 | ||
731 | /* shadow periodic table */ | |
732 | kfree(oxu->pshadow); | |
733 | oxu->pshadow = NULL; | |
734 | } | |
735 | ||
736 | /* Remember to add cleanup code (above) if you add anything here. | |
737 | */ | |
738 | static int ehci_mem_init(struct oxu_hcd *oxu, gfp_t flags) | |
739 | { | |
740 | int i; | |
741 | ||
742 | for (i = 0; i < oxu->periodic_size; i++) | |
743 | oxu->mem->frame_list[i] = EHCI_LIST_END; | |
744 | for (i = 0; i < QHEAD_NUM; i++) | |
745 | oxu->qh_used[i] = 0; | |
746 | for (i = 0; i < QTD_NUM; i++) | |
747 | oxu->qtd_used[i] = 0; | |
748 | ||
749 | oxu->murb_pool = kcalloc(MURB_NUM, sizeof(struct oxu_murb), flags); | |
750 | if (!oxu->murb_pool) | |
751 | goto fail; | |
752 | ||
753 | for (i = 0; i < MURB_NUM; i++) | |
754 | oxu->murb_used[i] = 0; | |
755 | ||
756 | oxu->async = oxu_qh_alloc(oxu); | |
757 | if (!oxu->async) | |
758 | goto fail; | |
759 | ||
760 | oxu->periodic = (__le32 *) &oxu->mem->frame_list; | |
761 | oxu->periodic_dma = virt_to_phys(oxu->periodic); | |
762 | ||
763 | for (i = 0; i < oxu->periodic_size; i++) | |
764 | oxu->periodic[i] = EHCI_LIST_END; | |
765 | ||
766 | /* software shadow of hardware table */ | |
767 | oxu->pshadow = kcalloc(oxu->periodic_size, sizeof(void *), flags); | |
768 | if (oxu->pshadow != NULL) | |
769 | return 0; | |
770 | ||
771 | fail: | |
772 | oxu_dbg(oxu, "couldn't init memory\n"); | |
773 | ehci_mem_cleanup(oxu); | |
774 | return -ENOMEM; | |
775 | } | |
776 | ||
777 | /* Fill a qtd, returning how much of the buffer we were able to queue up. | |
778 | */ | |
779 | static int qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf, size_t len, | |
780 | int token, int maxpacket) | |
781 | { | |
782 | int i, count; | |
783 | u64 addr = buf; | |
784 | ||
785 | /* one buffer entry per 4K ... first might be short or unaligned */ | |
786 | qtd->hw_buf[0] = cpu_to_le32((u32)addr); | |
787 | qtd->hw_buf_hi[0] = cpu_to_le32((u32)(addr >> 32)); | |
788 | count = 0x1000 - (buf & 0x0fff); /* rest of that page */ | |
789 | if (likely(len < count)) /* ... iff needed */ | |
790 | count = len; | |
791 | else { | |
792 | buf += 0x1000; | |
793 | buf &= ~0x0fff; | |
794 | ||
795 | /* per-qtd limit: from 16K to 20K (best alignment) */ | |
796 | for (i = 1; count < len && i < 5; i++) { | |
797 | addr = buf; | |
798 | qtd->hw_buf[i] = cpu_to_le32((u32)addr); | |
799 | qtd->hw_buf_hi[i] = cpu_to_le32((u32)(addr >> 32)); | |
800 | buf += 0x1000; | |
801 | if ((count + 0x1000) < len) | |
802 | count += 0x1000; | |
803 | else | |
804 | count = len; | |
805 | } | |
806 | ||
807 | /* short packets may only terminate transfers */ | |
808 | if (count != len) | |
809 | count -= (count % maxpacket); | |
810 | } | |
811 | qtd->hw_token = cpu_to_le32((count << 16) | token); | |
812 | qtd->length = count; | |
813 | ||
814 | return count; | |
815 | } | |
816 | ||
817 | static inline void qh_update(struct oxu_hcd *oxu, | |
818 | struct ehci_qh *qh, struct ehci_qtd *qtd) | |
819 | { | |
820 | /* writes to an active overlay are unsafe */ | |
821 | BUG_ON(qh->qh_state != QH_STATE_IDLE); | |
822 | ||
823 | qh->hw_qtd_next = QTD_NEXT(qtd->qtd_dma); | |
824 | qh->hw_alt_next = EHCI_LIST_END; | |
825 | ||
826 | /* Except for control endpoints, we make hardware maintain data | |
827 | * toggle (like OHCI) ... here (re)initialize the toggle in the QH, | |
828 | * and set the pseudo-toggle in udev. Only usb_clear_halt() will | |
829 | * ever clear it. | |
830 | */ | |
831 | if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) { | |
832 | unsigned is_out, epnum; | |
833 | ||
834 | is_out = !(qtd->hw_token & cpu_to_le32(1 << 8)); | |
835 | epnum = (le32_to_cpup(&qh->hw_info1) >> 8) & 0x0f; | |
836 | if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) { | |
551509d2 | 837 | qh->hw_token &= ~cpu_to_le32(QTD_TOGGLE); |
b92a78e5 RG |
838 | usb_settoggle(qh->dev, epnum, is_out, 1); |
839 | } | |
840 | } | |
841 | ||
842 | /* HC must see latest qtd and qh data before we clear ACTIVE+HALT */ | |
843 | wmb(); | |
551509d2 | 844 | qh->hw_token &= cpu_to_le32(QTD_TOGGLE | QTD_STS_PING); |
b92a78e5 RG |
845 | } |
846 | ||
847 | /* If it weren't for a common silicon quirk (writing the dummy into the qh | |
848 | * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault | |
849 | * recovery (including urb dequeue) would need software changes to a QH... | |
850 | */ | |
851 | static void qh_refresh(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
852 | { | |
853 | struct ehci_qtd *qtd; | |
854 | ||
855 | if (list_empty(&qh->qtd_list)) | |
856 | qtd = qh->dummy; | |
857 | else { | |
858 | qtd = list_entry(qh->qtd_list.next, | |
859 | struct ehci_qtd, qtd_list); | |
860 | /* first qtd may already be partially processed */ | |
861 | if (cpu_to_le32(qtd->qtd_dma) == qh->hw_current) | |
862 | qtd = NULL; | |
863 | } | |
864 | ||
865 | if (qtd) | |
866 | qh_update(oxu, qh, qtd); | |
867 | } | |
868 | ||
869 | static void qtd_copy_status(struct oxu_hcd *oxu, struct urb *urb, | |
870 | size_t length, u32 token) | |
871 | { | |
872 | /* count IN/OUT bytes, not SETUP (even short packets) */ | |
873 | if (likely(QTD_PID(token) != 2)) | |
874 | urb->actual_length += length - QTD_LENGTH(token); | |
875 | ||
876 | /* don't modify error codes */ | |
877 | if (unlikely(urb->status != -EINPROGRESS)) | |
878 | return; | |
879 | ||
880 | /* force cleanup after short read; not always an error */ | |
881 | if (unlikely(IS_SHORT_READ(token))) | |
882 | urb->status = -EREMOTEIO; | |
883 | ||
884 | /* serious "can't proceed" faults reported by the hardware */ | |
885 | if (token & QTD_STS_HALT) { | |
886 | if (token & QTD_STS_BABBLE) { | |
887 | /* FIXME "must" disable babbling device's port too */ | |
888 | urb->status = -EOVERFLOW; | |
889 | } else if (token & QTD_STS_MMF) { | |
890 | /* fs/ls interrupt xfer missed the complete-split */ | |
891 | urb->status = -EPROTO; | |
892 | } else if (token & QTD_STS_DBE) { | |
893 | urb->status = (QTD_PID(token) == 1) /* IN ? */ | |
894 | ? -ENOSR /* hc couldn't read data */ | |
895 | : -ECOMM; /* hc couldn't write data */ | |
896 | } else if (token & QTD_STS_XACT) { | |
897 | /* timeout, bad crc, wrong PID, etc; retried */ | |
898 | if (QTD_CERR(token)) | |
899 | urb->status = -EPIPE; | |
900 | else { | |
901 | oxu_dbg(oxu, "devpath %s ep%d%s 3strikes\n", | |
902 | urb->dev->devpath, | |
903 | usb_pipeendpoint(urb->pipe), | |
904 | usb_pipein(urb->pipe) ? "in" : "out"); | |
905 | urb->status = -EPROTO; | |
906 | } | |
907 | /* CERR nonzero + no errors + halt --> stall */ | |
908 | } else if (QTD_CERR(token)) | |
909 | urb->status = -EPIPE; | |
910 | else /* unknown */ | |
911 | urb->status = -EPROTO; | |
912 | ||
913 | oxu_vdbg(oxu, "dev%d ep%d%s qtd token %08x --> status %d\n", | |
914 | usb_pipedevice(urb->pipe), | |
915 | usb_pipeendpoint(urb->pipe), | |
916 | usb_pipein(urb->pipe) ? "in" : "out", | |
917 | token, urb->status); | |
918 | } | |
919 | } | |
920 | ||
921 | static void ehci_urb_done(struct oxu_hcd *oxu, struct urb *urb) | |
922 | __releases(oxu->lock) | |
923 | __acquires(oxu->lock) | |
924 | { | |
925 | if (likely(urb->hcpriv != NULL)) { | |
926 | struct ehci_qh *qh = (struct ehci_qh *) urb->hcpriv; | |
927 | ||
928 | /* S-mask in a QH means it's an interrupt urb */ | |
551509d2 | 929 | if ((qh->hw_info2 & cpu_to_le32(QH_SMASK)) != 0) { |
b92a78e5 RG |
930 | |
931 | /* ... update hc-wide periodic stats (for usbfs) */ | |
932 | oxu_to_hcd(oxu)->self.bandwidth_int_reqs--; | |
933 | } | |
934 | qh_put(qh); | |
935 | } | |
936 | ||
937 | urb->hcpriv = NULL; | |
938 | switch (urb->status) { | |
939 | case -EINPROGRESS: /* success */ | |
940 | urb->status = 0; | |
941 | default: /* fault */ | |
942 | break; | |
943 | case -EREMOTEIO: /* fault or normal */ | |
944 | if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) | |
945 | urb->status = 0; | |
946 | break; | |
947 | case -ECONNRESET: /* canceled */ | |
948 | case -ENOENT: | |
949 | break; | |
950 | } | |
951 | ||
952 | #ifdef OXU_URB_TRACE | |
953 | oxu_dbg(oxu, "%s %s urb %p ep%d%s status %d len %d/%d\n", | |
954 | __func__, urb->dev->devpath, urb, | |
955 | usb_pipeendpoint(urb->pipe), | |
956 | usb_pipein(urb->pipe) ? "in" : "out", | |
957 | urb->status, | |
958 | urb->actual_length, urb->transfer_buffer_length); | |
959 | #endif | |
960 | ||
961 | /* complete() can reenter this HCD */ | |
962 | spin_unlock(&oxu->lock); | |
963 | usb_hcd_giveback_urb(oxu_to_hcd(oxu), urb, urb->status); | |
964 | spin_lock(&oxu->lock); | |
965 | } | |
966 | ||
967 | static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh); | |
968 | static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh); | |
969 | ||
970 | static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh); | |
971 | static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh); | |
972 | ||
551509d2 | 973 | #define HALT_BIT cpu_to_le32(QTD_STS_HALT) |
b92a78e5 RG |
974 | |
975 | /* Process and free completed qtds for a qh, returning URBs to drivers. | |
976 | * Chases up to qh->hw_current. Returns number of completions called, | |
977 | * indicating how much "real" work we did. | |
978 | */ | |
979 | static unsigned qh_completions(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
980 | { | |
981 | struct ehci_qtd *last = NULL, *end = qh->dummy; | |
982 | struct list_head *entry, *tmp; | |
983 | int stopped; | |
984 | unsigned count = 0; | |
985 | int do_status = 0; | |
986 | u8 state; | |
987 | struct oxu_murb *murb = NULL; | |
988 | ||
989 | if (unlikely(list_empty(&qh->qtd_list))) | |
990 | return count; | |
991 | ||
992 | /* completions (or tasks on other cpus) must never clobber HALT | |
993 | * till we've gone through and cleaned everything up, even when | |
994 | * they add urbs to this qh's queue or mark them for unlinking. | |
995 | * | |
996 | * NOTE: unlinking expects to be done in queue order. | |
997 | */ | |
998 | state = qh->qh_state; | |
999 | qh->qh_state = QH_STATE_COMPLETING; | |
1000 | stopped = (state == QH_STATE_IDLE); | |
1001 | ||
1002 | /* remove de-activated QTDs from front of queue. | |
1003 | * after faults (including short reads), cleanup this urb | |
1004 | * then let the queue advance. | |
1005 | * if queue is stopped, handles unlinks. | |
1006 | */ | |
1007 | list_for_each_safe(entry, tmp, &qh->qtd_list) { | |
1008 | struct ehci_qtd *qtd; | |
1009 | struct urb *urb; | |
1010 | u32 token = 0; | |
1011 | ||
1012 | qtd = list_entry(entry, struct ehci_qtd, qtd_list); | |
1013 | urb = qtd->urb; | |
1014 | ||
1015 | /* Clean up any state from previous QTD ...*/ | |
1016 | if (last) { | |
1017 | if (likely(last->urb != urb)) { | |
1018 | if (last->urb->complete == NULL) { | |
1019 | murb = (struct oxu_murb *) last->urb; | |
1020 | last->urb = murb->main; | |
1021 | if (murb->last) { | |
1022 | ehci_urb_done(oxu, last->urb); | |
1023 | count++; | |
1024 | } | |
1025 | oxu_murb_free(oxu, murb); | |
1026 | } else { | |
1027 | ehci_urb_done(oxu, last->urb); | |
1028 | count++; | |
1029 | } | |
1030 | } | |
1031 | oxu_qtd_free(oxu, last); | |
1032 | last = NULL; | |
1033 | } | |
1034 | ||
1035 | /* ignore urbs submitted during completions we reported */ | |
1036 | if (qtd == end) | |
1037 | break; | |
1038 | ||
1039 | /* hardware copies qtd out of qh overlay */ | |
1040 | rmb(); | |
1041 | token = le32_to_cpu(qtd->hw_token); | |
1042 | ||
1043 | /* always clean up qtds the hc de-activated */ | |
1044 | if ((token & QTD_STS_ACTIVE) == 0) { | |
1045 | ||
1046 | if ((token & QTD_STS_HALT) != 0) { | |
1047 | stopped = 1; | |
1048 | ||
1049 | /* magic dummy for some short reads; qh won't advance. | |
1050 | * that silicon quirk can kick in with this dummy too. | |
1051 | */ | |
1052 | } else if (IS_SHORT_READ(token) && | |
1053 | !(qtd->hw_alt_next & EHCI_LIST_END)) { | |
1054 | stopped = 1; | |
1055 | goto halt; | |
1056 | } | |
1057 | ||
1058 | /* stop scanning when we reach qtds the hc is using */ | |
1059 | } else if (likely(!stopped && | |
1060 | HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) { | |
1061 | break; | |
1062 | ||
1063 | } else { | |
1064 | stopped = 1; | |
1065 | ||
1066 | if (unlikely(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) | |
1067 | urb->status = -ESHUTDOWN; | |
1068 | ||
1069 | /* ignore active urbs unless some previous qtd | |
1070 | * for the urb faulted (including short read) or | |
1071 | * its urb was canceled. we may patch qh or qtds. | |
1072 | */ | |
1073 | if (likely(urb->status == -EINPROGRESS)) | |
1074 | continue; | |
1075 | ||
1076 | /* issue status after short control reads */ | |
1077 | if (unlikely(do_status != 0) | |
1078 | && QTD_PID(token) == 0 /* OUT */) { | |
1079 | do_status = 0; | |
1080 | continue; | |
1081 | } | |
1082 | ||
1083 | /* token in overlay may be most current */ | |
1084 | if (state == QH_STATE_IDLE | |
1085 | && cpu_to_le32(qtd->qtd_dma) | |
1086 | == qh->hw_current) | |
1087 | token = le32_to_cpu(qh->hw_token); | |
1088 | ||
1089 | /* force halt for unlinked or blocked qh, so we'll | |
1090 | * patch the qh later and so that completions can't | |
1091 | * activate it while we "know" it's stopped. | |
1092 | */ | |
1093 | if ((HALT_BIT & qh->hw_token) == 0) { | |
1094 | halt: | |
1095 | qh->hw_token |= HALT_BIT; | |
1096 | wmb(); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | /* Remove it from the queue */ | |
1101 | qtd_copy_status(oxu, urb->complete ? | |
1102 | urb : ((struct oxu_murb *) urb)->main, | |
1103 | qtd->length, token); | |
1104 | if ((usb_pipein(qtd->urb->pipe)) && | |
1105 | (NULL != qtd->transfer_buffer)) | |
1106 | memcpy(qtd->transfer_buffer, qtd->buffer, qtd->length); | |
1107 | do_status = (urb->status == -EREMOTEIO) | |
1108 | && usb_pipecontrol(urb->pipe); | |
1109 | ||
1110 | if (stopped && qtd->qtd_list.prev != &qh->qtd_list) { | |
1111 | last = list_entry(qtd->qtd_list.prev, | |
1112 | struct ehci_qtd, qtd_list); | |
1113 | last->hw_next = qtd->hw_next; | |
1114 | } | |
1115 | list_del(&qtd->qtd_list); | |
1116 | last = qtd; | |
1117 | } | |
1118 | ||
1119 | /* last urb's completion might still need calling */ | |
1120 | if (likely(last != NULL)) { | |
1121 | if (last->urb->complete == NULL) { | |
1122 | murb = (struct oxu_murb *) last->urb; | |
1123 | last->urb = murb->main; | |
1124 | if (murb->last) { | |
1125 | ehci_urb_done(oxu, last->urb); | |
1126 | count++; | |
1127 | } | |
1128 | oxu_murb_free(oxu, murb); | |
1129 | } else { | |
1130 | ehci_urb_done(oxu, last->urb); | |
1131 | count++; | |
1132 | } | |
1133 | oxu_qtd_free(oxu, last); | |
1134 | } | |
1135 | ||
1136 | /* restore original state; caller must unlink or relink */ | |
1137 | qh->qh_state = state; | |
1138 | ||
1139 | /* be sure the hardware's done with the qh before refreshing | |
1140 | * it after fault cleanup, or recovering from silicon wrongly | |
1141 | * overlaying the dummy qtd (which reduces DMA chatter). | |
1142 | */ | |
1143 | if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) { | |
1144 | switch (state) { | |
1145 | case QH_STATE_IDLE: | |
1146 | qh_refresh(oxu, qh); | |
1147 | break; | |
1148 | case QH_STATE_LINKED: | |
1149 | /* should be rare for periodic transfers, | |
1150 | * except maybe high bandwidth ... | |
1151 | */ | |
551509d2 | 1152 | if ((cpu_to_le32(QH_SMASK) |
b92a78e5 RG |
1153 | & qh->hw_info2) != 0) { |
1154 | intr_deschedule(oxu, qh); | |
1155 | (void) qh_schedule(oxu, qh); | |
1156 | } else | |
1157 | unlink_async(oxu, qh); | |
1158 | break; | |
1159 | /* otherwise, unlink already started */ | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | return count; | |
1164 | } | |
1165 | ||
1166 | /* High bandwidth multiplier, as encoded in highspeed endpoint descriptors */ | |
1167 | #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03)) | |
1168 | /* ... and packet size, for any kind of endpoint descriptor */ | |
1169 | #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff) | |
1170 | ||
1171 | /* Reverse of qh_urb_transaction: free a list of TDs. | |
1172 | * used for cleanup after errors, before HC sees an URB's TDs. | |
1173 | */ | |
1174 | static void qtd_list_free(struct oxu_hcd *oxu, | |
1175 | struct urb *urb, struct list_head *qtd_list) | |
1176 | { | |
1177 | struct list_head *entry, *temp; | |
1178 | ||
1179 | list_for_each_safe(entry, temp, qtd_list) { | |
1180 | struct ehci_qtd *qtd; | |
1181 | ||
1182 | qtd = list_entry(entry, struct ehci_qtd, qtd_list); | |
1183 | list_del(&qtd->qtd_list); | |
1184 | oxu_qtd_free(oxu, qtd); | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | /* Create a list of filled qtds for this URB; won't link into qh. | |
1189 | */ | |
1190 | static struct list_head *qh_urb_transaction(struct oxu_hcd *oxu, | |
1191 | struct urb *urb, | |
1192 | struct list_head *head, | |
1193 | gfp_t flags) | |
1194 | { | |
1195 | struct ehci_qtd *qtd, *qtd_prev; | |
1196 | dma_addr_t buf; | |
1197 | int len, maxpacket; | |
1198 | int is_input; | |
1199 | u32 token; | |
1200 | void *transfer_buf = NULL; | |
1201 | int ret; | |
1202 | ||
1203 | /* | |
1204 | * URBs map to sequences of QTDs: one logical transaction | |
1205 | */ | |
1206 | qtd = ehci_qtd_alloc(oxu); | |
1207 | if (unlikely(!qtd)) | |
1208 | return NULL; | |
1209 | list_add_tail(&qtd->qtd_list, head); | |
1210 | qtd->urb = urb; | |
1211 | ||
1212 | token = QTD_STS_ACTIVE; | |
1213 | token |= (EHCI_TUNE_CERR << 10); | |
1214 | /* for split transactions, SplitXState initialized to zero */ | |
1215 | ||
1216 | len = urb->transfer_buffer_length; | |
1217 | is_input = usb_pipein(urb->pipe); | |
1218 | if (!urb->transfer_buffer && urb->transfer_buffer_length && is_input) | |
1219 | urb->transfer_buffer = phys_to_virt(urb->transfer_dma); | |
1220 | ||
1221 | if (usb_pipecontrol(urb->pipe)) { | |
1222 | /* SETUP pid */ | |
1223 | ret = oxu_buf_alloc(oxu, qtd, sizeof(struct usb_ctrlrequest)); | |
1224 | if (ret) | |
1225 | goto cleanup; | |
1226 | ||
1227 | qtd_fill(qtd, qtd->buffer_dma, sizeof(struct usb_ctrlrequest), | |
1228 | token | (2 /* "setup" */ << 8), 8); | |
1229 | memcpy(qtd->buffer, qtd->urb->setup_packet, | |
1230 | sizeof(struct usb_ctrlrequest)); | |
1231 | ||
1232 | /* ... and always at least one more pid */ | |
1233 | token ^= QTD_TOGGLE; | |
1234 | qtd_prev = qtd; | |
1235 | qtd = ehci_qtd_alloc(oxu); | |
1236 | if (unlikely(!qtd)) | |
1237 | goto cleanup; | |
1238 | qtd->urb = urb; | |
1239 | qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma); | |
1240 | list_add_tail(&qtd->qtd_list, head); | |
1241 | ||
1242 | /* for zero length DATA stages, STATUS is always IN */ | |
1243 | if (len == 0) | |
1244 | token |= (1 /* "in" */ << 8); | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * Data transfer stage: buffer setup | |
1249 | */ | |
1250 | ||
1251 | ret = oxu_buf_alloc(oxu, qtd, len); | |
1252 | if (ret) | |
1253 | goto cleanup; | |
1254 | ||
1255 | buf = qtd->buffer_dma; | |
1256 | transfer_buf = urb->transfer_buffer; | |
1257 | ||
1258 | if (!is_input) | |
1259 | memcpy(qtd->buffer, qtd->urb->transfer_buffer, len); | |
1260 | ||
1261 | if (is_input) | |
1262 | token |= (1 /* "in" */ << 8); | |
1263 | /* else it's already initted to "out" pid (0 << 8) */ | |
1264 | ||
1265 | maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input)); | |
1266 | ||
1267 | /* | |
1268 | * buffer gets wrapped in one or more qtds; | |
1269 | * last one may be "short" (including zero len) | |
1270 | * and may serve as a control status ack | |
1271 | */ | |
1272 | for (;;) { | |
1273 | int this_qtd_len; | |
1274 | ||
1275 | this_qtd_len = qtd_fill(qtd, buf, len, token, maxpacket); | |
1276 | qtd->transfer_buffer = transfer_buf; | |
1277 | len -= this_qtd_len; | |
1278 | buf += this_qtd_len; | |
1279 | transfer_buf += this_qtd_len; | |
1280 | if (is_input) | |
1281 | qtd->hw_alt_next = oxu->async->hw_alt_next; | |
1282 | ||
1283 | /* qh makes control packets use qtd toggle; maybe switch it */ | |
1284 | if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0) | |
1285 | token ^= QTD_TOGGLE; | |
1286 | ||
1287 | if (likely(len <= 0)) | |
1288 | break; | |
1289 | ||
1290 | qtd_prev = qtd; | |
1291 | qtd = ehci_qtd_alloc(oxu); | |
1292 | if (unlikely(!qtd)) | |
1293 | goto cleanup; | |
1294 | if (likely(len > 0)) { | |
1295 | ret = oxu_buf_alloc(oxu, qtd, len); | |
1296 | if (ret) | |
1297 | goto cleanup; | |
1298 | } | |
1299 | qtd->urb = urb; | |
1300 | qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma); | |
1301 | list_add_tail(&qtd->qtd_list, head); | |
1302 | } | |
1303 | ||
1304 | /* unless the bulk/interrupt caller wants a chance to clean | |
1305 | * up after short reads, hc should advance qh past this urb | |
1306 | */ | |
1307 | if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 | |
1308 | || usb_pipecontrol(urb->pipe))) | |
1309 | qtd->hw_alt_next = EHCI_LIST_END; | |
1310 | ||
1311 | /* | |
1312 | * control requests may need a terminating data "status" ack; | |
1313 | * bulk ones may need a terminating short packet (zero length). | |
1314 | */ | |
1315 | if (likely(urb->transfer_buffer_length != 0)) { | |
1316 | int one_more = 0; | |
1317 | ||
1318 | if (usb_pipecontrol(urb->pipe)) { | |
1319 | one_more = 1; | |
1320 | token ^= 0x0100; /* "in" <--> "out" */ | |
1321 | token |= QTD_TOGGLE; /* force DATA1 */ | |
1322 | } else if (usb_pipebulk(urb->pipe) | |
1323 | && (urb->transfer_flags & URB_ZERO_PACKET) | |
1324 | && !(urb->transfer_buffer_length % maxpacket)) { | |
1325 | one_more = 1; | |
1326 | } | |
1327 | if (one_more) { | |
1328 | qtd_prev = qtd; | |
1329 | qtd = ehci_qtd_alloc(oxu); | |
1330 | if (unlikely(!qtd)) | |
1331 | goto cleanup; | |
1332 | qtd->urb = urb; | |
1333 | qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma); | |
1334 | list_add_tail(&qtd->qtd_list, head); | |
1335 | ||
1336 | /* never any data in such packets */ | |
1337 | qtd_fill(qtd, 0, 0, token, 0); | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | /* by default, enable interrupt on urb completion */ | |
551509d2 | 1342 | qtd->hw_token |= cpu_to_le32(QTD_IOC); |
b92a78e5 RG |
1343 | return head; |
1344 | ||
1345 | cleanup: | |
1346 | qtd_list_free(oxu, urb, head); | |
1347 | return NULL; | |
1348 | } | |
1349 | ||
1350 | /* Each QH holds a qtd list; a QH is used for everything except iso. | |
1351 | * | |
1352 | * For interrupt urbs, the scheduler must set the microframe scheduling | |
1353 | * mask(s) each time the QH gets scheduled. For highspeed, that's | |
1354 | * just one microframe in the s-mask. For split interrupt transactions | |
1355 | * there are additional complications: c-mask, maybe FSTNs. | |
1356 | */ | |
1357 | static struct ehci_qh *qh_make(struct oxu_hcd *oxu, | |
1358 | struct urb *urb, gfp_t flags) | |
1359 | { | |
1360 | struct ehci_qh *qh = oxu_qh_alloc(oxu); | |
1361 | u32 info1 = 0, info2 = 0; | |
1362 | int is_input, type; | |
1363 | int maxp = 0; | |
1364 | ||
1365 | if (!qh) | |
1366 | return qh; | |
1367 | ||
1368 | /* | |
1369 | * init endpoint/device data for this QH | |
1370 | */ | |
1371 | info1 |= usb_pipeendpoint(urb->pipe) << 8; | |
1372 | info1 |= usb_pipedevice(urb->pipe) << 0; | |
1373 | ||
1374 | is_input = usb_pipein(urb->pipe); | |
1375 | type = usb_pipetype(urb->pipe); | |
1376 | maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input); | |
1377 | ||
1378 | /* Compute interrupt scheduling parameters just once, and save. | |
1379 | * - allowing for high bandwidth, how many nsec/uframe are used? | |
1380 | * - split transactions need a second CSPLIT uframe; same question | |
1381 | * - splits also need a schedule gap (for full/low speed I/O) | |
1382 | * - qh has a polling interval | |
1383 | * | |
1384 | * For control/bulk requests, the HC or TT handles these. | |
1385 | */ | |
1386 | if (type == PIPE_INTERRUPT) { | |
1387 | qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH, | |
1388 | is_input, 0, | |
1389 | hb_mult(maxp) * max_packet(maxp))); | |
1390 | qh->start = NO_FRAME; | |
1391 | ||
1392 | if (urb->dev->speed == USB_SPEED_HIGH) { | |
1393 | qh->c_usecs = 0; | |
1394 | qh->gap_uf = 0; | |
1395 | ||
1396 | qh->period = urb->interval >> 3; | |
1397 | if (qh->period == 0 && urb->interval != 1) { | |
1398 | /* NOTE interval 2 or 4 uframes could work. | |
1399 | * But interval 1 scheduling is simpler, and | |
1400 | * includes high bandwidth. | |
1401 | */ | |
b5f5bfe0 GKH |
1402 | oxu_dbg(oxu, "intr period %d uframes, NYET!\n", |
1403 | urb->interval); | |
b92a78e5 RG |
1404 | goto done; |
1405 | } | |
1406 | } else { | |
1407 | struct usb_tt *tt = urb->dev->tt; | |
1408 | int think_time; | |
1409 | ||
1410 | /* gap is f(FS/LS transfer times) */ | |
1411 | qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed, | |
1412 | is_input, 0, maxp) / (125 * 1000); | |
1413 | ||
1414 | /* FIXME this just approximates SPLIT/CSPLIT times */ | |
1415 | if (is_input) { /* SPLIT, gap, CSPLIT+DATA */ | |
1416 | qh->c_usecs = qh->usecs + HS_USECS(0); | |
1417 | qh->usecs = HS_USECS(1); | |
1418 | } else { /* SPLIT+DATA, gap, CSPLIT */ | |
1419 | qh->usecs += HS_USECS(1); | |
1420 | qh->c_usecs = HS_USECS(0); | |
1421 | } | |
1422 | ||
1423 | think_time = tt ? tt->think_time : 0; | |
1424 | qh->tt_usecs = NS_TO_US(think_time + | |
1425 | usb_calc_bus_time(urb->dev->speed, | |
1426 | is_input, 0, max_packet(maxp))); | |
1427 | qh->period = urb->interval; | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | /* support for tt scheduling, and access to toggles */ | |
1432 | qh->dev = urb->dev; | |
1433 | ||
1434 | /* using TT? */ | |
1435 | switch (urb->dev->speed) { | |
1436 | case USB_SPEED_LOW: | |
1437 | info1 |= (1 << 12); /* EPS "low" */ | |
1438 | /* FALL THROUGH */ | |
1439 | ||
1440 | case USB_SPEED_FULL: | |
1441 | /* EPS 0 means "full" */ | |
1442 | if (type != PIPE_INTERRUPT) | |
1443 | info1 |= (EHCI_TUNE_RL_TT << 28); | |
1444 | if (type == PIPE_CONTROL) { | |
1445 | info1 |= (1 << 27); /* for TT */ | |
1446 | info1 |= 1 << 14; /* toggle from qtd */ | |
1447 | } | |
1448 | info1 |= maxp << 16; | |
1449 | ||
1450 | info2 |= (EHCI_TUNE_MULT_TT << 30); | |
1451 | info2 |= urb->dev->ttport << 23; | |
1452 | ||
1453 | /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */ | |
1454 | ||
1455 | break; | |
1456 | ||
1457 | case USB_SPEED_HIGH: /* no TT involved */ | |
1458 | info1 |= (2 << 12); /* EPS "high" */ | |
1459 | if (type == PIPE_CONTROL) { | |
1460 | info1 |= (EHCI_TUNE_RL_HS << 28); | |
1461 | info1 |= 64 << 16; /* usb2 fixed maxpacket */ | |
1462 | info1 |= 1 << 14; /* toggle from qtd */ | |
1463 | info2 |= (EHCI_TUNE_MULT_HS << 30); | |
1464 | } else if (type == PIPE_BULK) { | |
1465 | info1 |= (EHCI_TUNE_RL_HS << 28); | |
1466 | info1 |= 512 << 16; /* usb2 fixed maxpacket */ | |
1467 | info2 |= (EHCI_TUNE_MULT_HS << 30); | |
1468 | } else { /* PIPE_INTERRUPT */ | |
1469 | info1 |= max_packet(maxp) << 16; | |
1470 | info2 |= hb_mult(maxp) << 30; | |
1471 | } | |
1472 | break; | |
1473 | default: | |
b5f5bfe0 | 1474 | oxu_dbg(oxu, "bogus dev %p speed %d\n", urb->dev, urb->dev->speed); |
b92a78e5 RG |
1475 | done: |
1476 | qh_put(qh); | |
1477 | return NULL; | |
1478 | } | |
1479 | ||
1480 | /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */ | |
1481 | ||
1482 | /* init as live, toggle clear, advance to dummy */ | |
1483 | qh->qh_state = QH_STATE_IDLE; | |
1484 | qh->hw_info1 = cpu_to_le32(info1); | |
1485 | qh->hw_info2 = cpu_to_le32(info2); | |
1486 | usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1); | |
1487 | qh_refresh(oxu, qh); | |
1488 | return qh; | |
1489 | } | |
1490 | ||
1491 | /* Move qh (and its qtds) onto async queue; maybe enable queue. | |
1492 | */ | |
1493 | static void qh_link_async(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
1494 | { | |
1495 | __le32 dma = QH_NEXT(qh->qh_dma); | |
1496 | struct ehci_qh *head; | |
1497 | ||
1498 | /* (re)start the async schedule? */ | |
1499 | head = oxu->async; | |
1500 | timer_action_done(oxu, TIMER_ASYNC_OFF); | |
1501 | if (!head->qh_next.qh) { | |
1502 | u32 cmd = readl(&oxu->regs->command); | |
1503 | ||
1504 | if (!(cmd & CMD_ASE)) { | |
1505 | /* in case a clear of CMD_ASE didn't take yet */ | |
1506 | (void)handshake(oxu, &oxu->regs->status, | |
1507 | STS_ASS, 0, 150); | |
1508 | cmd |= CMD_ASE | CMD_RUN; | |
1509 | writel(cmd, &oxu->regs->command); | |
1510 | oxu_to_hcd(oxu)->state = HC_STATE_RUNNING; | |
1511 | /* posted write need not be known to HC yet ... */ | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | /* clear halt and/or toggle; and maybe recover from silicon quirk */ | |
1516 | if (qh->qh_state == QH_STATE_IDLE) | |
1517 | qh_refresh(oxu, qh); | |
1518 | ||
1519 | /* splice right after start */ | |
1520 | qh->qh_next = head->qh_next; | |
1521 | qh->hw_next = head->hw_next; | |
1522 | wmb(); | |
1523 | ||
1524 | head->qh_next.qh = qh; | |
1525 | head->hw_next = dma; | |
1526 | ||
1527 | qh->qh_state = QH_STATE_LINKED; | |
1528 | /* qtd completions reported later by interrupt */ | |
1529 | } | |
1530 | ||
551509d2 | 1531 | #define QH_ADDR_MASK cpu_to_le32(0x7f) |
b92a78e5 RG |
1532 | |
1533 | /* | |
1534 | * For control/bulk/interrupt, return QH with these TDs appended. | |
1535 | * Allocates and initializes the QH if necessary. | |
1536 | * Returns null if it can't allocate a QH it needs to. | |
1537 | * If the QH has TDs (urbs) already, that's great. | |
1538 | */ | |
1539 | static struct ehci_qh *qh_append_tds(struct oxu_hcd *oxu, | |
1540 | struct urb *urb, struct list_head *qtd_list, | |
1541 | int epnum, void **ptr) | |
1542 | { | |
1543 | struct ehci_qh *qh = NULL; | |
1544 | ||
1545 | qh = (struct ehci_qh *) *ptr; | |
1546 | if (unlikely(qh == NULL)) { | |
1547 | /* can't sleep here, we have oxu->lock... */ | |
1548 | qh = qh_make(oxu, urb, GFP_ATOMIC); | |
1549 | *ptr = qh; | |
1550 | } | |
1551 | if (likely(qh != NULL)) { | |
1552 | struct ehci_qtd *qtd; | |
1553 | ||
1554 | if (unlikely(list_empty(qtd_list))) | |
1555 | qtd = NULL; | |
1556 | else | |
1557 | qtd = list_entry(qtd_list->next, struct ehci_qtd, | |
1558 | qtd_list); | |
1559 | ||
1560 | /* control qh may need patching ... */ | |
1561 | if (unlikely(epnum == 0)) { | |
1562 | ||
1563 | /* usb_reset_device() briefly reverts to address 0 */ | |
1564 | if (usb_pipedevice(urb->pipe) == 0) | |
1565 | qh->hw_info1 &= ~QH_ADDR_MASK; | |
1566 | } | |
1567 | ||
1568 | /* just one way to queue requests: swap with the dummy qtd. | |
1569 | * only hc or qh_refresh() ever modify the overlay. | |
1570 | */ | |
1571 | if (likely(qtd != NULL)) { | |
1572 | struct ehci_qtd *dummy; | |
1573 | dma_addr_t dma; | |
1574 | __le32 token; | |
1575 | ||
1576 | /* to avoid racing the HC, use the dummy td instead of | |
1577 | * the first td of our list (becomes new dummy). both | |
1578 | * tds stay deactivated until we're done, when the | |
1579 | * HC is allowed to fetch the old dummy (4.10.2). | |
1580 | */ | |
1581 | token = qtd->hw_token; | |
1582 | qtd->hw_token = HALT_BIT; | |
1583 | wmb(); | |
1584 | dummy = qh->dummy; | |
1585 | ||
1586 | dma = dummy->qtd_dma; | |
1587 | *dummy = *qtd; | |
1588 | dummy->qtd_dma = dma; | |
1589 | ||
1590 | list_del(&qtd->qtd_list); | |
1591 | list_add(&dummy->qtd_list, qtd_list); | |
1592 | list_splice(qtd_list, qh->qtd_list.prev); | |
1593 | ||
1594 | ehci_qtd_init(qtd, qtd->qtd_dma); | |
1595 | qh->dummy = qtd; | |
1596 | ||
1597 | /* hc must see the new dummy at list end */ | |
1598 | dma = qtd->qtd_dma; | |
1599 | qtd = list_entry(qh->qtd_list.prev, | |
1600 | struct ehci_qtd, qtd_list); | |
1601 | qtd->hw_next = QTD_NEXT(dma); | |
1602 | ||
1603 | /* let the hc process these next qtds */ | |
1604 | dummy->hw_token = (token & ~(0x80)); | |
1605 | wmb(); | |
1606 | dummy->hw_token = token; | |
1607 | ||
1608 | urb->hcpriv = qh_get(qh); | |
1609 | } | |
1610 | } | |
1611 | return qh; | |
1612 | } | |
1613 | ||
1614 | static int submit_async(struct oxu_hcd *oxu, struct urb *urb, | |
1615 | struct list_head *qtd_list, gfp_t mem_flags) | |
1616 | { | |
1617 | struct ehci_qtd *qtd; | |
1618 | int epnum; | |
1619 | unsigned long flags; | |
1620 | struct ehci_qh *qh = NULL; | |
1621 | int rc = 0; | |
1622 | ||
1623 | qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list); | |
1624 | epnum = urb->ep->desc.bEndpointAddress; | |
1625 | ||
1626 | #ifdef OXU_URB_TRACE | |
1627 | oxu_dbg(oxu, "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n", | |
1628 | __func__, urb->dev->devpath, urb, | |
1629 | epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out", | |
1630 | urb->transfer_buffer_length, | |
1631 | qtd, urb->ep->hcpriv); | |
1632 | #endif | |
1633 | ||
1634 | spin_lock_irqsave(&oxu->lock, flags); | |
541c7d43 | 1635 | if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) { |
b92a78e5 RG |
1636 | rc = -ESHUTDOWN; |
1637 | goto done; | |
1638 | } | |
1639 | ||
1640 | qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv); | |
1641 | if (unlikely(qh == NULL)) { | |
1642 | rc = -ENOMEM; | |
1643 | goto done; | |
1644 | } | |
1645 | ||
1646 | /* Control/bulk operations through TTs don't need scheduling, | |
1647 | * the HC and TT handle it when the TT has a buffer ready. | |
1648 | */ | |
1649 | if (likely(qh->qh_state == QH_STATE_IDLE)) | |
1650 | qh_link_async(oxu, qh_get(qh)); | |
1651 | done: | |
1652 | spin_unlock_irqrestore(&oxu->lock, flags); | |
1653 | if (unlikely(qh == NULL)) | |
1654 | qtd_list_free(oxu, urb, qtd_list); | |
1655 | return rc; | |
1656 | } | |
1657 | ||
1658 | /* The async qh for the qtds being reclaimed are now unlinked from the HC */ | |
1659 | ||
1660 | static void end_unlink_async(struct oxu_hcd *oxu) | |
1661 | { | |
1662 | struct ehci_qh *qh = oxu->reclaim; | |
1663 | struct ehci_qh *next; | |
1664 | ||
1665 | timer_action_done(oxu, TIMER_IAA_WATCHDOG); | |
1666 | ||
1667 | qh->qh_state = QH_STATE_IDLE; | |
1668 | qh->qh_next.qh = NULL; | |
1669 | qh_put(qh); /* refcount from reclaim */ | |
1670 | ||
1671 | /* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */ | |
1672 | next = qh->reclaim; | |
1673 | oxu->reclaim = next; | |
1674 | oxu->reclaim_ready = 0; | |
1675 | qh->reclaim = NULL; | |
1676 | ||
1677 | qh_completions(oxu, qh); | |
1678 | ||
1679 | if (!list_empty(&qh->qtd_list) | |
1680 | && HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) | |
1681 | qh_link_async(oxu, qh); | |
1682 | else { | |
1683 | qh_put(qh); /* refcount from async list */ | |
1684 | ||
1685 | /* it's not free to turn the async schedule on/off; leave it | |
1686 | * active but idle for a while once it empties. | |
1687 | */ | |
1688 | if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) | |
1689 | && oxu->async->qh_next.qh == NULL) | |
1690 | timer_action(oxu, TIMER_ASYNC_OFF); | |
1691 | } | |
1692 | ||
1693 | if (next) { | |
1694 | oxu->reclaim = NULL; | |
1695 | start_unlink_async(oxu, next); | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | /* makes sure the async qh will become idle */ | |
1700 | /* caller must own oxu->lock */ | |
1701 | ||
1702 | static void start_unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
1703 | { | |
1704 | int cmd = readl(&oxu->regs->command); | |
1705 | struct ehci_qh *prev; | |
1706 | ||
1707 | #ifdef DEBUG | |
1708 | assert_spin_locked(&oxu->lock); | |
1709 | if (oxu->reclaim || (qh->qh_state != QH_STATE_LINKED | |
1710 | && qh->qh_state != QH_STATE_UNLINK_WAIT)) | |
1711 | BUG(); | |
1712 | #endif | |
1713 | ||
1714 | /* stop async schedule right now? */ | |
1715 | if (unlikely(qh == oxu->async)) { | |
1716 | /* can't get here without STS_ASS set */ | |
1717 | if (oxu_to_hcd(oxu)->state != HC_STATE_HALT | |
1718 | && !oxu->reclaim) { | |
1719 | /* ... and CMD_IAAD clear */ | |
1720 | writel(cmd & ~CMD_ASE, &oxu->regs->command); | |
1721 | wmb(); | |
1722 | /* handshake later, if we need to */ | |
1723 | timer_action_done(oxu, TIMER_ASYNC_OFF); | |
1724 | } | |
1725 | return; | |
1726 | } | |
1727 | ||
1728 | qh->qh_state = QH_STATE_UNLINK; | |
1729 | oxu->reclaim = qh = qh_get(qh); | |
1730 | ||
1731 | prev = oxu->async; | |
1732 | while (prev->qh_next.qh != qh) | |
1733 | prev = prev->qh_next.qh; | |
1734 | ||
1735 | prev->hw_next = qh->hw_next; | |
1736 | prev->qh_next = qh->qh_next; | |
1737 | wmb(); | |
1738 | ||
1739 | if (unlikely(oxu_to_hcd(oxu)->state == HC_STATE_HALT)) { | |
1740 | /* if (unlikely(qh->reclaim != 0)) | |
1741 | * this will recurse, probably not much | |
1742 | */ | |
1743 | end_unlink_async(oxu); | |
1744 | return; | |
1745 | } | |
1746 | ||
1747 | oxu->reclaim_ready = 0; | |
1748 | cmd |= CMD_IAAD; | |
1749 | writel(cmd, &oxu->regs->command); | |
1750 | (void) readl(&oxu->regs->command); | |
1751 | timer_action(oxu, TIMER_IAA_WATCHDOG); | |
1752 | } | |
1753 | ||
1754 | static void scan_async(struct oxu_hcd *oxu) | |
1755 | { | |
1756 | struct ehci_qh *qh; | |
1757 | enum ehci_timer_action action = TIMER_IO_WATCHDOG; | |
1758 | ||
1759 | if (!++(oxu->stamp)) | |
1760 | oxu->stamp++; | |
1761 | timer_action_done(oxu, TIMER_ASYNC_SHRINK); | |
1762 | rescan: | |
1763 | qh = oxu->async->qh_next.qh; | |
1764 | if (likely(qh != NULL)) { | |
1765 | do { | |
1766 | /* clean any finished work for this qh */ | |
1767 | if (!list_empty(&qh->qtd_list) | |
1768 | && qh->stamp != oxu->stamp) { | |
1769 | int temp; | |
1770 | ||
1771 | /* unlinks could happen here; completion | |
1772 | * reporting drops the lock. rescan using | |
1773 | * the latest schedule, but don't rescan | |
1774 | * qhs we already finished (no looping). | |
1775 | */ | |
1776 | qh = qh_get(qh); | |
1777 | qh->stamp = oxu->stamp; | |
1778 | temp = qh_completions(oxu, qh); | |
1779 | qh_put(qh); | |
1780 | if (temp != 0) | |
1781 | goto rescan; | |
1782 | } | |
1783 | ||
1784 | /* unlink idle entries, reducing HC PCI usage as well | |
1785 | * as HCD schedule-scanning costs. delay for any qh | |
1786 | * we just scanned, there's a not-unusual case that it | |
1787 | * doesn't stay idle for long. | |
1788 | * (plus, avoids some kind of re-activation race.) | |
1789 | */ | |
1790 | if (list_empty(&qh->qtd_list)) { | |
1791 | if (qh->stamp == oxu->stamp) | |
1792 | action = TIMER_ASYNC_SHRINK; | |
1793 | else if (!oxu->reclaim | |
1794 | && qh->qh_state == QH_STATE_LINKED) | |
1795 | start_unlink_async(oxu, qh); | |
1796 | } | |
1797 | ||
1798 | qh = qh->qh_next.qh; | |
1799 | } while (qh); | |
1800 | } | |
1801 | if (action == TIMER_ASYNC_SHRINK) | |
1802 | timer_action(oxu, TIMER_ASYNC_SHRINK); | |
1803 | } | |
1804 | ||
1805 | /* | |
1806 | * periodic_next_shadow - return "next" pointer on shadow list | |
1807 | * @periodic: host pointer to qh/itd/sitd | |
1808 | * @tag: hardware tag for type of this record | |
1809 | */ | |
1810 | static union ehci_shadow *periodic_next_shadow(union ehci_shadow *periodic, | |
1811 | __le32 tag) | |
1812 | { | |
1813 | switch (tag) { | |
1814 | default: | |
1815 | case Q_TYPE_QH: | |
1816 | return &periodic->qh->qh_next; | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | /* caller must hold oxu->lock */ | |
1821 | static void periodic_unlink(struct oxu_hcd *oxu, unsigned frame, void *ptr) | |
1822 | { | |
1823 | union ehci_shadow *prev_p = &oxu->pshadow[frame]; | |
1824 | __le32 *hw_p = &oxu->periodic[frame]; | |
1825 | union ehci_shadow here = *prev_p; | |
1826 | ||
1827 | /* find predecessor of "ptr"; hw and shadow lists are in sync */ | |
1828 | while (here.ptr && here.ptr != ptr) { | |
1829 | prev_p = periodic_next_shadow(prev_p, Q_NEXT_TYPE(*hw_p)); | |
1830 | hw_p = here.hw_next; | |
1831 | here = *prev_p; | |
1832 | } | |
1833 | /* an interrupt entry (at list end) could have been shared */ | |
1834 | if (!here.ptr) | |
1835 | return; | |
1836 | ||
1837 | /* update shadow and hardware lists ... the old "next" pointers | |
1838 | * from ptr may still be in use, the caller updates them. | |
1839 | */ | |
1840 | *prev_p = *periodic_next_shadow(&here, Q_NEXT_TYPE(*hw_p)); | |
1841 | *hw_p = *here.hw_next; | |
1842 | } | |
1843 | ||
1844 | /* how many of the uframe's 125 usecs are allocated? */ | |
1845 | static unsigned short periodic_usecs(struct oxu_hcd *oxu, | |
1846 | unsigned frame, unsigned uframe) | |
1847 | { | |
1848 | __le32 *hw_p = &oxu->periodic[frame]; | |
1849 | union ehci_shadow *q = &oxu->pshadow[frame]; | |
1850 | unsigned usecs = 0; | |
1851 | ||
1852 | while (q->ptr) { | |
1853 | switch (Q_NEXT_TYPE(*hw_p)) { | |
1854 | case Q_TYPE_QH: | |
1855 | default: | |
1856 | /* is it in the S-mask? */ | |
1857 | if (q->qh->hw_info2 & cpu_to_le32(1 << uframe)) | |
1858 | usecs += q->qh->usecs; | |
1859 | /* ... or C-mask? */ | |
1860 | if (q->qh->hw_info2 & cpu_to_le32(1 << (8 + uframe))) | |
1861 | usecs += q->qh->c_usecs; | |
1862 | hw_p = &q->qh->hw_next; | |
1863 | q = &q->qh->qh_next; | |
1864 | break; | |
1865 | } | |
1866 | } | |
1867 | #ifdef DEBUG | |
1868 | if (usecs > 100) | |
1869 | oxu_err(oxu, "uframe %d sched overrun: %d usecs\n", | |
1870 | frame * 8 + uframe, usecs); | |
1871 | #endif | |
1872 | return usecs; | |
1873 | } | |
1874 | ||
1875 | static int enable_periodic(struct oxu_hcd *oxu) | |
1876 | { | |
1877 | u32 cmd; | |
1878 | int status; | |
1879 | ||
1880 | /* did clearing PSE did take effect yet? | |
1881 | * takes effect only at frame boundaries... | |
1882 | */ | |
1883 | status = handshake(oxu, &oxu->regs->status, STS_PSS, 0, 9 * 125); | |
1884 | if (status != 0) { | |
1885 | oxu_to_hcd(oxu)->state = HC_STATE_HALT; | |
69fff59d | 1886 | usb_hc_died(oxu_to_hcd(oxu)); |
b92a78e5 RG |
1887 | return status; |
1888 | } | |
1889 | ||
1890 | cmd = readl(&oxu->regs->command) | CMD_PSE; | |
1891 | writel(cmd, &oxu->regs->command); | |
1892 | /* posted write ... PSS happens later */ | |
1893 | oxu_to_hcd(oxu)->state = HC_STATE_RUNNING; | |
1894 | ||
1895 | /* make sure ehci_work scans these */ | |
1896 | oxu->next_uframe = readl(&oxu->regs->frame_index) | |
1897 | % (oxu->periodic_size << 3); | |
1898 | return 0; | |
1899 | } | |
1900 | ||
1901 | static int disable_periodic(struct oxu_hcd *oxu) | |
1902 | { | |
1903 | u32 cmd; | |
1904 | int status; | |
1905 | ||
1906 | /* did setting PSE not take effect yet? | |
1907 | * takes effect only at frame boundaries... | |
1908 | */ | |
1909 | status = handshake(oxu, &oxu->regs->status, STS_PSS, STS_PSS, 9 * 125); | |
1910 | if (status != 0) { | |
1911 | oxu_to_hcd(oxu)->state = HC_STATE_HALT; | |
69fff59d | 1912 | usb_hc_died(oxu_to_hcd(oxu)); |
b92a78e5 RG |
1913 | return status; |
1914 | } | |
1915 | ||
1916 | cmd = readl(&oxu->regs->command) & ~CMD_PSE; | |
1917 | writel(cmd, &oxu->regs->command); | |
1918 | /* posted write ... */ | |
1919 | ||
1920 | oxu->next_uframe = -1; | |
1921 | return 0; | |
1922 | } | |
1923 | ||
1924 | /* periodic schedule slots have iso tds (normal or split) first, then a | |
1925 | * sparse tree for active interrupt transfers. | |
1926 | * | |
1927 | * this just links in a qh; caller guarantees uframe masks are set right. | |
1928 | * no FSTN support (yet; oxu 0.96+) | |
1929 | */ | |
1930 | static int qh_link_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
1931 | { | |
1932 | unsigned i; | |
1933 | unsigned period = qh->period; | |
1934 | ||
1935 | dev_dbg(&qh->dev->dev, | |
1936 | "link qh%d-%04x/%p start %d [%d/%d us]\n", | |
1937 | period, le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK), | |
1938 | qh, qh->start, qh->usecs, qh->c_usecs); | |
1939 | ||
1940 | /* high bandwidth, or otherwise every microframe */ | |
1941 | if (period == 0) | |
1942 | period = 1; | |
1943 | ||
1944 | for (i = qh->start; i < oxu->periodic_size; i += period) { | |
1945 | union ehci_shadow *prev = &oxu->pshadow[i]; | |
1946 | __le32 *hw_p = &oxu->periodic[i]; | |
1947 | union ehci_shadow here = *prev; | |
1948 | __le32 type = 0; | |
1949 | ||
1950 | /* skip the iso nodes at list head */ | |
1951 | while (here.ptr) { | |
1952 | type = Q_NEXT_TYPE(*hw_p); | |
1953 | if (type == Q_TYPE_QH) | |
1954 | break; | |
1955 | prev = periodic_next_shadow(prev, type); | |
1956 | hw_p = &here.qh->hw_next; | |
1957 | here = *prev; | |
1958 | } | |
1959 | ||
1960 | /* sorting each branch by period (slow-->fast) | |
1961 | * enables sharing interior tree nodes | |
1962 | */ | |
1963 | while (here.ptr && qh != here.qh) { | |
1964 | if (qh->period > here.qh->period) | |
1965 | break; | |
1966 | prev = &here.qh->qh_next; | |
1967 | hw_p = &here.qh->hw_next; | |
1968 | here = *prev; | |
1969 | } | |
1970 | /* link in this qh, unless some earlier pass did that */ | |
1971 | if (qh != here.qh) { | |
1972 | qh->qh_next = here; | |
1973 | if (here.qh) | |
1974 | qh->hw_next = *hw_p; | |
1975 | wmb(); | |
1976 | prev->qh = qh; | |
1977 | *hw_p = QH_NEXT(qh->qh_dma); | |
1978 | } | |
1979 | } | |
1980 | qh->qh_state = QH_STATE_LINKED; | |
1981 | qh_get(qh); | |
1982 | ||
1983 | /* update per-qh bandwidth for usbfs */ | |
1984 | oxu_to_hcd(oxu)->self.bandwidth_allocated += qh->period | |
1985 | ? ((qh->usecs + qh->c_usecs) / qh->period) | |
1986 | : (qh->usecs * 8); | |
1987 | ||
1988 | /* maybe enable periodic schedule processing */ | |
1989 | if (!oxu->periodic_sched++) | |
1990 | return enable_periodic(oxu); | |
1991 | ||
1992 | return 0; | |
1993 | } | |
1994 | ||
1995 | static void qh_unlink_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
1996 | { | |
1997 | unsigned i; | |
1998 | unsigned period; | |
1999 | ||
2000 | /* FIXME: | |
2001 | * IF this isn't high speed | |
2002 | * and this qh is active in the current uframe | |
2003 | * (and overlay token SplitXstate is false?) | |
2004 | * THEN | |
551509d2 | 2005 | * qh->hw_info1 |= cpu_to_le32(1 << 7 "ignore"); |
b92a78e5 RG |
2006 | */ |
2007 | ||
2008 | /* high bandwidth, or otherwise part of every microframe */ | |
2009 | period = qh->period; | |
2010 | if (period == 0) | |
2011 | period = 1; | |
2012 | ||
2013 | for (i = qh->start; i < oxu->periodic_size; i += period) | |
2014 | periodic_unlink(oxu, i, qh); | |
2015 | ||
2016 | /* update per-qh bandwidth for usbfs */ | |
2017 | oxu_to_hcd(oxu)->self.bandwidth_allocated -= qh->period | |
2018 | ? ((qh->usecs + qh->c_usecs) / qh->period) | |
2019 | : (qh->usecs * 8); | |
2020 | ||
2021 | dev_dbg(&qh->dev->dev, | |
2022 | "unlink qh%d-%04x/%p start %d [%d/%d us]\n", | |
2023 | qh->period, | |
2024 | le32_to_cpup(&qh->hw_info2) & (QH_CMASK | QH_SMASK), | |
2025 | qh, qh->start, qh->usecs, qh->c_usecs); | |
2026 | ||
2027 | /* qh->qh_next still "live" to HC */ | |
2028 | qh->qh_state = QH_STATE_UNLINK; | |
2029 | qh->qh_next.ptr = NULL; | |
2030 | qh_put(qh); | |
2031 | ||
2032 | /* maybe turn off periodic schedule */ | |
2033 | oxu->periodic_sched--; | |
2034 | if (!oxu->periodic_sched) | |
2035 | (void) disable_periodic(oxu); | |
2036 | } | |
2037 | ||
2038 | static void intr_deschedule(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
2039 | { | |
2040 | unsigned wait; | |
2041 | ||
2042 | qh_unlink_periodic(oxu, qh); | |
2043 | ||
2044 | /* simple/paranoid: always delay, expecting the HC needs to read | |
2045 | * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and | |
2046 | * expect khubd to clean up after any CSPLITs we won't issue. | |
2047 | * active high speed queues may need bigger delays... | |
2048 | */ | |
2049 | if (list_empty(&qh->qtd_list) | |
551509d2 | 2050 | || (cpu_to_le32(QH_CMASK) & qh->hw_info2) != 0) |
b92a78e5 RG |
2051 | wait = 2; |
2052 | else | |
2053 | wait = 55; /* worst case: 3 * 1024 */ | |
2054 | ||
2055 | udelay(wait); | |
2056 | qh->qh_state = QH_STATE_IDLE; | |
2057 | qh->hw_next = EHCI_LIST_END; | |
2058 | wmb(); | |
2059 | } | |
2060 | ||
2061 | static int check_period(struct oxu_hcd *oxu, | |
2062 | unsigned frame, unsigned uframe, | |
2063 | unsigned period, unsigned usecs) | |
2064 | { | |
2065 | int claimed; | |
2066 | ||
2067 | /* complete split running into next frame? | |
2068 | * given FSTN support, we could sometimes check... | |
2069 | */ | |
2070 | if (uframe >= 8) | |
2071 | return 0; | |
2072 | ||
2073 | /* | |
2074 | * 80% periodic == 100 usec/uframe available | |
2075 | * convert "usecs we need" to "max already claimed" | |
2076 | */ | |
2077 | usecs = 100 - usecs; | |
2078 | ||
2079 | /* we "know" 2 and 4 uframe intervals were rejected; so | |
2080 | * for period 0, check _every_ microframe in the schedule. | |
2081 | */ | |
2082 | if (unlikely(period == 0)) { | |
2083 | do { | |
2084 | for (uframe = 0; uframe < 7; uframe++) { | |
2085 | claimed = periodic_usecs(oxu, frame, uframe); | |
2086 | if (claimed > usecs) | |
2087 | return 0; | |
2088 | } | |
2089 | } while ((frame += 1) < oxu->periodic_size); | |
2090 | ||
2091 | /* just check the specified uframe, at that period */ | |
2092 | } else { | |
2093 | do { | |
2094 | claimed = periodic_usecs(oxu, frame, uframe); | |
2095 | if (claimed > usecs) | |
2096 | return 0; | |
2097 | } while ((frame += period) < oxu->periodic_size); | |
2098 | } | |
2099 | ||
2100 | return 1; | |
2101 | } | |
2102 | ||
2103 | static int check_intr_schedule(struct oxu_hcd *oxu, | |
2104 | unsigned frame, unsigned uframe, | |
2105 | const struct ehci_qh *qh, __le32 *c_maskp) | |
2106 | { | |
2107 | int retval = -ENOSPC; | |
2108 | ||
2109 | if (qh->c_usecs && uframe >= 6) /* FSTN territory? */ | |
2110 | goto done; | |
2111 | ||
2112 | if (!check_period(oxu, frame, uframe, qh->period, qh->usecs)) | |
2113 | goto done; | |
2114 | if (!qh->c_usecs) { | |
2115 | retval = 0; | |
2116 | *c_maskp = 0; | |
2117 | goto done; | |
2118 | } | |
2119 | ||
2120 | done: | |
2121 | return retval; | |
2122 | } | |
2123 | ||
2124 | /* "first fit" scheduling policy used the first time through, | |
2125 | * or when the previous schedule slot can't be re-used. | |
2126 | */ | |
2127 | static int qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
2128 | { | |
2129 | int status; | |
2130 | unsigned uframe; | |
2131 | __le32 c_mask; | |
2132 | unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */ | |
2133 | ||
2134 | qh_refresh(oxu, qh); | |
2135 | qh->hw_next = EHCI_LIST_END; | |
2136 | frame = qh->start; | |
2137 | ||
2138 | /* reuse the previous schedule slots, if we can */ | |
2139 | if (frame < qh->period) { | |
2140 | uframe = ffs(le32_to_cpup(&qh->hw_info2) & QH_SMASK); | |
2141 | status = check_intr_schedule(oxu, frame, --uframe, | |
2142 | qh, &c_mask); | |
2143 | } else { | |
2144 | uframe = 0; | |
2145 | c_mask = 0; | |
2146 | status = -ENOSPC; | |
2147 | } | |
2148 | ||
2149 | /* else scan the schedule to find a group of slots such that all | |
2150 | * uframes have enough periodic bandwidth available. | |
2151 | */ | |
2152 | if (status) { | |
2153 | /* "normal" case, uframing flexible except with splits */ | |
2154 | if (qh->period) { | |
2155 | frame = qh->period - 1; | |
2156 | do { | |
2157 | for (uframe = 0; uframe < 8; uframe++) { | |
2158 | status = check_intr_schedule(oxu, | |
2159 | frame, uframe, qh, | |
2160 | &c_mask); | |
2161 | if (status == 0) | |
2162 | break; | |
2163 | } | |
2164 | } while (status && frame--); | |
2165 | ||
2166 | /* qh->period == 0 means every uframe */ | |
2167 | } else { | |
2168 | frame = 0; | |
2169 | status = check_intr_schedule(oxu, 0, 0, qh, &c_mask); | |
2170 | } | |
2171 | if (status) | |
2172 | goto done; | |
2173 | qh->start = frame; | |
2174 | ||
2175 | /* reset S-frame and (maybe) C-frame masks */ | |
551509d2 | 2176 | qh->hw_info2 &= cpu_to_le32(~(QH_CMASK | QH_SMASK)); |
b92a78e5 RG |
2177 | qh->hw_info2 |= qh->period |
2178 | ? cpu_to_le32(1 << uframe) | |
551509d2 | 2179 | : cpu_to_le32(QH_SMASK); |
b92a78e5 RG |
2180 | qh->hw_info2 |= c_mask; |
2181 | } else | |
2182 | oxu_dbg(oxu, "reused qh %p schedule\n", qh); | |
2183 | ||
2184 | /* stuff into the periodic schedule */ | |
2185 | status = qh_link_periodic(oxu, qh); | |
2186 | done: | |
2187 | return status; | |
2188 | } | |
2189 | ||
2190 | static int intr_submit(struct oxu_hcd *oxu, struct urb *urb, | |
2191 | struct list_head *qtd_list, gfp_t mem_flags) | |
2192 | { | |
2193 | unsigned epnum; | |
2194 | unsigned long flags; | |
2195 | struct ehci_qh *qh; | |
2196 | int status = 0; | |
2197 | struct list_head empty; | |
2198 | ||
2199 | /* get endpoint and transfer/schedule data */ | |
2200 | epnum = urb->ep->desc.bEndpointAddress; | |
2201 | ||
2202 | spin_lock_irqsave(&oxu->lock, flags); | |
2203 | ||
541c7d43 | 2204 | if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) { |
b92a78e5 RG |
2205 | status = -ESHUTDOWN; |
2206 | goto done; | |
2207 | } | |
2208 | ||
2209 | /* get qh and force any scheduling errors */ | |
2210 | INIT_LIST_HEAD(&empty); | |
2211 | qh = qh_append_tds(oxu, urb, &empty, epnum, &urb->ep->hcpriv); | |
2212 | if (qh == NULL) { | |
2213 | status = -ENOMEM; | |
2214 | goto done; | |
2215 | } | |
2216 | if (qh->qh_state == QH_STATE_IDLE) { | |
2217 | status = qh_schedule(oxu, qh); | |
2218 | if (status != 0) | |
2219 | goto done; | |
2220 | } | |
2221 | ||
2222 | /* then queue the urb's tds to the qh */ | |
2223 | qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv); | |
2224 | BUG_ON(qh == NULL); | |
2225 | ||
2226 | /* ... update usbfs periodic stats */ | |
2227 | oxu_to_hcd(oxu)->self.bandwidth_int_reqs++; | |
2228 | ||
2229 | done: | |
2230 | spin_unlock_irqrestore(&oxu->lock, flags); | |
2231 | if (status) | |
2232 | qtd_list_free(oxu, urb, qtd_list); | |
2233 | ||
2234 | return status; | |
2235 | } | |
2236 | ||
2237 | static inline int itd_submit(struct oxu_hcd *oxu, struct urb *urb, | |
2238 | gfp_t mem_flags) | |
2239 | { | |
2240 | oxu_dbg(oxu, "iso support is missing!\n"); | |
2241 | return -ENOSYS; | |
2242 | } | |
2243 | ||
2244 | static inline int sitd_submit(struct oxu_hcd *oxu, struct urb *urb, | |
2245 | gfp_t mem_flags) | |
2246 | { | |
2247 | oxu_dbg(oxu, "split iso support is missing!\n"); | |
2248 | return -ENOSYS; | |
2249 | } | |
2250 | ||
2251 | static void scan_periodic(struct oxu_hcd *oxu) | |
2252 | { | |
2253 | unsigned frame, clock, now_uframe, mod; | |
2254 | unsigned modified; | |
2255 | ||
2256 | mod = oxu->periodic_size << 3; | |
2257 | ||
2258 | /* | |
2259 | * When running, scan from last scan point up to "now" | |
2260 | * else clean up by scanning everything that's left. | |
2261 | * Touches as few pages as possible: cache-friendly. | |
2262 | */ | |
2263 | now_uframe = oxu->next_uframe; | |
2264 | if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) | |
2265 | clock = readl(&oxu->regs->frame_index); | |
2266 | else | |
2267 | clock = now_uframe + mod - 1; | |
2268 | clock %= mod; | |
2269 | ||
2270 | for (;;) { | |
2271 | union ehci_shadow q, *q_p; | |
2272 | __le32 type, *hw_p; | |
2273 | unsigned uframes; | |
2274 | ||
2275 | /* don't scan past the live uframe */ | |
2276 | frame = now_uframe >> 3; | |
2277 | if (frame == (clock >> 3)) | |
2278 | uframes = now_uframe & 0x07; | |
2279 | else { | |
2280 | /* safe to scan the whole frame at once */ | |
2281 | now_uframe |= 0x07; | |
2282 | uframes = 8; | |
2283 | } | |
2284 | ||
2285 | restart: | |
2286 | /* scan each element in frame's queue for completions */ | |
2287 | q_p = &oxu->pshadow[frame]; | |
2288 | hw_p = &oxu->periodic[frame]; | |
2289 | q.ptr = q_p->ptr; | |
2290 | type = Q_NEXT_TYPE(*hw_p); | |
2291 | modified = 0; | |
2292 | ||
2293 | while (q.ptr != NULL) { | |
2294 | union ehci_shadow temp; | |
2295 | int live; | |
2296 | ||
2297 | live = HC_IS_RUNNING(oxu_to_hcd(oxu)->state); | |
2298 | switch (type) { | |
2299 | case Q_TYPE_QH: | |
2300 | /* handle any completions */ | |
2301 | temp.qh = qh_get(q.qh); | |
2302 | type = Q_NEXT_TYPE(q.qh->hw_next); | |
2303 | q = q.qh->qh_next; | |
2304 | modified = qh_completions(oxu, temp.qh); | |
2305 | if (unlikely(list_empty(&temp.qh->qtd_list))) | |
2306 | intr_deschedule(oxu, temp.qh); | |
2307 | qh_put(temp.qh); | |
2308 | break; | |
2309 | default: | |
b5f5bfe0 | 2310 | oxu_dbg(oxu, "corrupt type %d frame %d shadow %p\n", |
b92a78e5 RG |
2311 | type, frame, q.ptr); |
2312 | q.ptr = NULL; | |
2313 | } | |
2314 | ||
2315 | /* assume completion callbacks modify the queue */ | |
2316 | if (unlikely(modified)) | |
2317 | goto restart; | |
2318 | } | |
2319 | ||
2320 | /* Stop when we catch up to the HC */ | |
2321 | ||
2322 | /* FIXME: this assumes we won't get lapped when | |
2323 | * latencies climb; that should be rare, but... | |
2324 | * detect it, and just go all the way around. | |
2325 | * FLR might help detect this case, so long as latencies | |
2326 | * don't exceed periodic_size msec (default 1.024 sec). | |
2327 | */ | |
2328 | ||
2329 | /* FIXME: likewise assumes HC doesn't halt mid-scan */ | |
2330 | ||
2331 | if (now_uframe == clock) { | |
2332 | unsigned now; | |
2333 | ||
2334 | if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) | |
2335 | break; | |
2336 | oxu->next_uframe = now_uframe; | |
2337 | now = readl(&oxu->regs->frame_index) % mod; | |
2338 | if (now_uframe == now) | |
2339 | break; | |
2340 | ||
2341 | /* rescan the rest of this frame, then ... */ | |
2342 | clock = now; | |
2343 | } else { | |
2344 | now_uframe++; | |
2345 | now_uframe %= mod; | |
2346 | } | |
2347 | } | |
2348 | } | |
2349 | ||
2350 | /* On some systems, leaving remote wakeup enabled prevents system shutdown. | |
2351 | * The firmware seems to think that powering off is a wakeup event! | |
2352 | * This routine turns off remote wakeup and everything else, on all ports. | |
2353 | */ | |
2354 | static void ehci_turn_off_all_ports(struct oxu_hcd *oxu) | |
2355 | { | |
2356 | int port = HCS_N_PORTS(oxu->hcs_params); | |
2357 | ||
2358 | while (port--) | |
2359 | writel(PORT_RWC_BITS, &oxu->regs->port_status[port]); | |
2360 | } | |
2361 | ||
2362 | static void ehci_port_power(struct oxu_hcd *oxu, int is_on) | |
2363 | { | |
2364 | unsigned port; | |
2365 | ||
2366 | if (!HCS_PPC(oxu->hcs_params)) | |
2367 | return; | |
2368 | ||
2369 | oxu_dbg(oxu, "...power%s ports...\n", is_on ? "up" : "down"); | |
2370 | for (port = HCS_N_PORTS(oxu->hcs_params); port > 0; ) | |
2371 | (void) oxu_hub_control(oxu_to_hcd(oxu), | |
2372 | is_on ? SetPortFeature : ClearPortFeature, | |
2373 | USB_PORT_FEAT_POWER, | |
2374 | port--, NULL, 0); | |
2375 | msleep(20); | |
2376 | } | |
2377 | ||
2378 | /* Called from some interrupts, timers, and so on. | |
2379 | * It calls driver completion functions, after dropping oxu->lock. | |
2380 | */ | |
2381 | static void ehci_work(struct oxu_hcd *oxu) | |
2382 | { | |
2383 | timer_action_done(oxu, TIMER_IO_WATCHDOG); | |
2384 | if (oxu->reclaim_ready) | |
2385 | end_unlink_async(oxu); | |
2386 | ||
2387 | /* another CPU may drop oxu->lock during a schedule scan while | |
2388 | * it reports urb completions. this flag guards against bogus | |
2389 | * attempts at re-entrant schedule scanning. | |
2390 | */ | |
2391 | if (oxu->scanning) | |
2392 | return; | |
2393 | oxu->scanning = 1; | |
2394 | scan_async(oxu); | |
2395 | if (oxu->next_uframe != -1) | |
2396 | scan_periodic(oxu); | |
2397 | oxu->scanning = 0; | |
2398 | ||
2399 | /* the IO watchdog guards against hardware or driver bugs that | |
2400 | * misplace IRQs, and should let us run completely without IRQs. | |
2401 | * such lossage has been observed on both VT6202 and VT8235. | |
2402 | */ | |
2403 | if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && | |
2404 | (oxu->async->qh_next.ptr != NULL || | |
2405 | oxu->periodic_sched != 0)) | |
2406 | timer_action(oxu, TIMER_IO_WATCHDOG); | |
2407 | } | |
2408 | ||
2409 | static void unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh) | |
2410 | { | |
2411 | /* if we need to use IAA and it's busy, defer */ | |
2412 | if (qh->qh_state == QH_STATE_LINKED | |
2413 | && oxu->reclaim | |
2414 | && HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) { | |
2415 | struct ehci_qh *last; | |
2416 | ||
2417 | for (last = oxu->reclaim; | |
2418 | last->reclaim; | |
2419 | last = last->reclaim) | |
2420 | continue; | |
2421 | qh->qh_state = QH_STATE_UNLINK_WAIT; | |
2422 | last->reclaim = qh; | |
2423 | ||
2424 | /* bypass IAA if the hc can't care */ | |
2425 | } else if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && oxu->reclaim) | |
2426 | end_unlink_async(oxu); | |
2427 | ||
2428 | /* something else might have unlinked the qh by now */ | |
2429 | if (qh->qh_state == QH_STATE_LINKED) | |
2430 | start_unlink_async(oxu, qh); | |
2431 | } | |
2432 | ||
2433 | /* | |
2434 | * USB host controller methods | |
2435 | */ | |
2436 | ||
2437 | static irqreturn_t oxu210_hcd_irq(struct usb_hcd *hcd) | |
2438 | { | |
2439 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2440 | u32 status, pcd_status = 0; | |
2441 | int bh; | |
2442 | ||
2443 | spin_lock(&oxu->lock); | |
2444 | ||
2445 | status = readl(&oxu->regs->status); | |
2446 | ||
2447 | /* e.g. cardbus physical eject */ | |
2448 | if (status == ~(u32) 0) { | |
2449 | oxu_dbg(oxu, "device removed\n"); | |
2450 | goto dead; | |
2451 | } | |
2452 | ||
69fff59d | 2453 | /* Shared IRQ? */ |
b92a78e5 | 2454 | status &= INTR_MASK; |
69fff59d | 2455 | if (!status || unlikely(hcd->state == HC_STATE_HALT)) { |
b92a78e5 RG |
2456 | spin_unlock(&oxu->lock); |
2457 | return IRQ_NONE; | |
2458 | } | |
2459 | ||
2460 | /* clear (just) interrupts */ | |
2461 | writel(status, &oxu->regs->status); | |
2462 | readl(&oxu->regs->command); /* unblock posted write */ | |
2463 | bh = 0; | |
2464 | ||
2465 | #ifdef OXU_VERBOSE_DEBUG | |
2466 | /* unrequested/ignored: Frame List Rollover */ | |
2467 | dbg_status(oxu, "irq", status); | |
2468 | #endif | |
2469 | ||
2470 | /* INT, ERR, and IAA interrupt rates can be throttled */ | |
2471 | ||
2472 | /* normal [4.15.1.2] or error [4.15.1.1] completion */ | |
2473 | if (likely((status & (STS_INT|STS_ERR)) != 0)) | |
2474 | bh = 1; | |
2475 | ||
2476 | /* complete the unlinking of some qh [4.15.2.3] */ | |
2477 | if (status & STS_IAA) { | |
2478 | oxu->reclaim_ready = 1; | |
2479 | bh = 1; | |
2480 | } | |
2481 | ||
2482 | /* remote wakeup [4.3.1] */ | |
2483 | if (status & STS_PCD) { | |
2484 | unsigned i = HCS_N_PORTS(oxu->hcs_params); | |
2485 | pcd_status = status; | |
2486 | ||
2487 | /* resume root hub? */ | |
2488 | if (!(readl(&oxu->regs->command) & CMD_RUN)) | |
2489 | usb_hcd_resume_root_hub(hcd); | |
2490 | ||
2491 | while (i--) { | |
2492 | int pstatus = readl(&oxu->regs->port_status[i]); | |
2493 | ||
2494 | if (pstatus & PORT_OWNER) | |
2495 | continue; | |
2496 | if (!(pstatus & PORT_RESUME) | |
2497 | || oxu->reset_done[i] != 0) | |
2498 | continue; | |
2499 | ||
228dae65 FB |
2500 | /* start USB_RESUME_TIMEOUT resume signaling from this |
2501 | * port, and make hub_wq collect PORT_STAT_C_SUSPEND to | |
b92a78e5 RG |
2502 | * stop that signaling. |
2503 | */ | |
228dae65 FB |
2504 | oxu->reset_done[i] = jiffies + |
2505 | msecs_to_jiffies(USB_RESUME_TIMEOUT); | |
b92a78e5 RG |
2506 | oxu_dbg(oxu, "port %d remote wakeup\n", i + 1); |
2507 | mod_timer(&hcd->rh_timer, oxu->reset_done[i]); | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | /* PCI errors [4.15.2.4] */ | |
2512 | if (unlikely((status & STS_FATAL) != 0)) { | |
2513 | /* bogus "fatal" IRQs appear on some chips... why? */ | |
2514 | status = readl(&oxu->regs->status); | |
2515 | dbg_cmd(oxu, "fatal", readl(&oxu->regs->command)); | |
2516 | dbg_status(oxu, "fatal", status); | |
2517 | if (status & STS_HALT) { | |
2518 | oxu_err(oxu, "fatal error\n"); | |
2519 | dead: | |
2520 | ehci_reset(oxu); | |
2521 | writel(0, &oxu->regs->configured_flag); | |
69fff59d | 2522 | usb_hc_died(hcd); |
b92a78e5 RG |
2523 | /* generic layer kills/unlinks all urbs, then |
2524 | * uses oxu_stop to clean up the rest | |
2525 | */ | |
2526 | bh = 1; | |
2527 | } | |
2528 | } | |
2529 | ||
2530 | if (bh) | |
2531 | ehci_work(oxu); | |
2532 | spin_unlock(&oxu->lock); | |
2533 | if (pcd_status & STS_PCD) | |
2534 | usb_hcd_poll_rh_status(hcd); | |
2535 | return IRQ_HANDLED; | |
2536 | } | |
2537 | ||
2538 | static irqreturn_t oxu_irq(struct usb_hcd *hcd) | |
2539 | { | |
2540 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2541 | int ret = IRQ_HANDLED; | |
2542 | ||
2543 | u32 status = oxu_readl(hcd->regs, OXU_CHIPIRQSTATUS); | |
2544 | u32 enable = oxu_readl(hcd->regs, OXU_CHIPIRQEN_SET); | |
2545 | ||
2546 | /* Disable all interrupt */ | |
2547 | oxu_writel(hcd->regs, OXU_CHIPIRQEN_CLR, enable); | |
2548 | ||
2549 | if ((oxu->is_otg && (status & OXU_USBOTGI)) || | |
2550 | (!oxu->is_otg && (status & OXU_USBSPHI))) | |
2551 | oxu210_hcd_irq(hcd); | |
2552 | else | |
2553 | ret = IRQ_NONE; | |
2554 | ||
2555 | /* Enable all interrupt back */ | |
2556 | oxu_writel(hcd->regs, OXU_CHIPIRQEN_SET, enable); | |
2557 | ||
2558 | return ret; | |
2559 | } | |
2560 | ||
2561 | static void oxu_watchdog(unsigned long param) | |
2562 | { | |
2563 | struct oxu_hcd *oxu = (struct oxu_hcd *) param; | |
2564 | unsigned long flags; | |
2565 | ||
2566 | spin_lock_irqsave(&oxu->lock, flags); | |
2567 | ||
2568 | /* lost IAA irqs wedge things badly; seen with a vt8235 */ | |
2569 | if (oxu->reclaim) { | |
2570 | u32 status = readl(&oxu->regs->status); | |
2571 | if (status & STS_IAA) { | |
2572 | oxu_vdbg(oxu, "lost IAA\n"); | |
2573 | writel(STS_IAA, &oxu->regs->status); | |
2574 | oxu->reclaim_ready = 1; | |
2575 | } | |
2576 | } | |
2577 | ||
2578 | /* stop async processing after it's idled a bit */ | |
2579 | if (test_bit(TIMER_ASYNC_OFF, &oxu->actions)) | |
2580 | start_unlink_async(oxu, oxu->async); | |
2581 | ||
2582 | /* oxu could run by timer, without IRQs ... */ | |
2583 | ehci_work(oxu); | |
2584 | ||
2585 | spin_unlock_irqrestore(&oxu->lock, flags); | |
2586 | } | |
2587 | ||
2588 | /* One-time init, only for memory state. | |
2589 | */ | |
2590 | static int oxu_hcd_init(struct usb_hcd *hcd) | |
2591 | { | |
2592 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2593 | u32 temp; | |
2594 | int retval; | |
2595 | u32 hcc_params; | |
2596 | ||
2597 | spin_lock_init(&oxu->lock); | |
2598 | ||
2599 | init_timer(&oxu->watchdog); | |
2600 | oxu->watchdog.function = oxu_watchdog; | |
2601 | oxu->watchdog.data = (unsigned long) oxu; | |
2602 | ||
2603 | /* | |
2604 | * hw default: 1K periodic list heads, one per frame. | |
2605 | * periodic_size can shrink by USBCMD update if hcc_params allows. | |
2606 | */ | |
2607 | oxu->periodic_size = DEFAULT_I_TDPS; | |
2608 | retval = ehci_mem_init(oxu, GFP_KERNEL); | |
2609 | if (retval < 0) | |
2610 | return retval; | |
2611 | ||
2612 | /* controllers may cache some of the periodic schedule ... */ | |
2613 | hcc_params = readl(&oxu->caps->hcc_params); | |
2614 | if (HCC_ISOC_CACHE(hcc_params)) /* full frame cache */ | |
2615 | oxu->i_thresh = 8; | |
2616 | else /* N microframes cached */ | |
2617 | oxu->i_thresh = 2 + HCC_ISOC_THRES(hcc_params); | |
2618 | ||
2619 | oxu->reclaim = NULL; | |
2620 | oxu->reclaim_ready = 0; | |
2621 | oxu->next_uframe = -1; | |
2622 | ||
2623 | /* | |
2624 | * dedicate a qh for the async ring head, since we couldn't unlink | |
2625 | * a 'real' qh without stopping the async schedule [4.8]. use it | |
2626 | * as the 'reclamation list head' too. | |
2627 | * its dummy is used in hw_alt_next of many tds, to prevent the qh | |
2628 | * from automatically advancing to the next td after short reads. | |
2629 | */ | |
2630 | oxu->async->qh_next.qh = NULL; | |
2631 | oxu->async->hw_next = QH_NEXT(oxu->async->qh_dma); | |
2632 | oxu->async->hw_info1 = cpu_to_le32(QH_HEAD); | |
2633 | oxu->async->hw_token = cpu_to_le32(QTD_STS_HALT); | |
2634 | oxu->async->hw_qtd_next = EHCI_LIST_END; | |
2635 | oxu->async->qh_state = QH_STATE_LINKED; | |
2636 | oxu->async->hw_alt_next = QTD_NEXT(oxu->async->dummy->qtd_dma); | |
2637 | ||
2638 | /* clear interrupt enables, set irq latency */ | |
2639 | if (log2_irq_thresh < 0 || log2_irq_thresh > 6) | |
2640 | log2_irq_thresh = 0; | |
2641 | temp = 1 << (16 + log2_irq_thresh); | |
2642 | if (HCC_CANPARK(hcc_params)) { | |
2643 | /* HW default park == 3, on hardware that supports it (like | |
2644 | * NVidia and ALI silicon), maximizes throughput on the async | |
2645 | * schedule by avoiding QH fetches between transfers. | |
2646 | * | |
2647 | * With fast usb storage devices and NForce2, "park" seems to | |
2648 | * make problems: throughput reduction (!), data errors... | |
2649 | */ | |
2650 | if (park) { | |
2651 | park = min(park, (unsigned) 3); | |
2652 | temp |= CMD_PARK; | |
2653 | temp |= park << 8; | |
2654 | } | |
2655 | oxu_dbg(oxu, "park %d\n", park); | |
2656 | } | |
2657 | if (HCC_PGM_FRAMELISTLEN(hcc_params)) { | |
2658 | /* periodic schedule size can be smaller than default */ | |
2659 | temp &= ~(3 << 2); | |
2660 | temp |= (EHCI_TUNE_FLS << 2); | |
2661 | } | |
2662 | oxu->command = temp; | |
2663 | ||
2664 | return 0; | |
2665 | } | |
2666 | ||
2667 | /* Called during probe() after chip reset completes. | |
2668 | */ | |
2669 | static int oxu_reset(struct usb_hcd *hcd) | |
2670 | { | |
2671 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2672 | int ret; | |
2673 | ||
2674 | spin_lock_init(&oxu->mem_lock); | |
2675 | INIT_LIST_HEAD(&oxu->urb_list); | |
2676 | oxu->urb_len = 0; | |
2677 | ||
2678 | /* FIMXE */ | |
a9f8ec4d | 2679 | hcd->self.controller->dma_mask = NULL; |
b92a78e5 RG |
2680 | |
2681 | if (oxu->is_otg) { | |
2682 | oxu->caps = hcd->regs + OXU_OTG_CAP_OFFSET; | |
2683 | oxu->regs = hcd->regs + OXU_OTG_CAP_OFFSET + \ | |
2684 | HC_LENGTH(readl(&oxu->caps->hc_capbase)); | |
2685 | ||
2686 | oxu->mem = hcd->regs + OXU_SPH_MEM; | |
2687 | } else { | |
2688 | oxu->caps = hcd->regs + OXU_SPH_CAP_OFFSET; | |
2689 | oxu->regs = hcd->regs + OXU_SPH_CAP_OFFSET + \ | |
2690 | HC_LENGTH(readl(&oxu->caps->hc_capbase)); | |
2691 | ||
2692 | oxu->mem = hcd->regs + OXU_OTG_MEM; | |
2693 | } | |
2694 | ||
2695 | oxu->hcs_params = readl(&oxu->caps->hcs_params); | |
2696 | oxu->sbrn = 0x20; | |
2697 | ||
2698 | ret = oxu_hcd_init(hcd); | |
2699 | if (ret) | |
2700 | return ret; | |
2701 | ||
2702 | return 0; | |
2703 | } | |
2704 | ||
2705 | static int oxu_run(struct usb_hcd *hcd) | |
2706 | { | |
2707 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2708 | int retval; | |
2709 | u32 temp, hcc_params; | |
2710 | ||
2711 | hcd->uses_new_polling = 1; | |
b92a78e5 RG |
2712 | |
2713 | /* EHCI spec section 4.1 */ | |
2714 | retval = ehci_reset(oxu); | |
2715 | if (retval != 0) { | |
2716 | ehci_mem_cleanup(oxu); | |
2717 | return retval; | |
2718 | } | |
2719 | writel(oxu->periodic_dma, &oxu->regs->frame_list); | |
2720 | writel((u32) oxu->async->qh_dma, &oxu->regs->async_next); | |
2721 | ||
2722 | /* hcc_params controls whether oxu->regs->segment must (!!!) | |
2723 | * be used; it constrains QH/ITD/SITD and QTD locations. | |
2724 | * pci_pool consistent memory always uses segment zero. | |
2725 | * streaming mappings for I/O buffers, like pci_map_single(), | |
2726 | * can return segments above 4GB, if the device allows. | |
2727 | * | |
2728 | * NOTE: the dma mask is visible through dma_supported(), so | |
2729 | * drivers can pass this info along ... like NETIF_F_HIGHDMA, | |
2730 | * Scsi_Host.highmem_io, and so forth. It's readonly to all | |
2731 | * host side drivers though. | |
2732 | */ | |
2733 | hcc_params = readl(&oxu->caps->hcc_params); | |
2734 | if (HCC_64BIT_ADDR(hcc_params)) | |
2735 | writel(0, &oxu->regs->segment); | |
2736 | ||
2737 | oxu->command &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | | |
2738 | CMD_ASE | CMD_RESET); | |
2739 | oxu->command |= CMD_RUN; | |
2740 | writel(oxu->command, &oxu->regs->command); | |
2741 | dbg_cmd(oxu, "init", oxu->command); | |
2742 | ||
2743 | /* | |
2744 | * Start, enabling full USB 2.0 functionality ... usb 1.1 devices | |
2745 | * are explicitly handed to companion controller(s), so no TT is | |
2746 | * involved with the root hub. (Except where one is integrated, | |
2747 | * and there's no companion controller unless maybe for USB OTG.) | |
2748 | */ | |
2749 | hcd->state = HC_STATE_RUNNING; | |
2750 | writel(FLAG_CF, &oxu->regs->configured_flag); | |
2751 | readl(&oxu->regs->command); /* unblock posted writes */ | |
2752 | ||
2753 | temp = HC_VERSION(readl(&oxu->caps->hc_capbase)); | |
2754 | oxu_info(oxu, "USB %x.%x started, quasi-EHCI %x.%02x, driver %s%s\n", | |
2755 | ((oxu->sbrn & 0xf0)>>4), (oxu->sbrn & 0x0f), | |
2756 | temp >> 8, temp & 0xff, DRIVER_VERSION, | |
2757 | ignore_oc ? ", overcurrent ignored" : ""); | |
2758 | ||
2759 | writel(INTR_MASK, &oxu->regs->intr_enable); /* Turn On Interrupts */ | |
2760 | ||
2761 | return 0; | |
2762 | } | |
2763 | ||
2764 | static void oxu_stop(struct usb_hcd *hcd) | |
2765 | { | |
2766 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2767 | ||
2768 | /* Turn off port power on all root hub ports. */ | |
2769 | ehci_port_power(oxu, 0); | |
2770 | ||
2771 | /* no more interrupts ... */ | |
2772 | del_timer_sync(&oxu->watchdog); | |
2773 | ||
2774 | spin_lock_irq(&oxu->lock); | |
2775 | if (HC_IS_RUNNING(hcd->state)) | |
2776 | ehci_quiesce(oxu); | |
2777 | ||
2778 | ehci_reset(oxu); | |
2779 | writel(0, &oxu->regs->intr_enable); | |
2780 | spin_unlock_irq(&oxu->lock); | |
2781 | ||
2782 | /* let companion controllers work when we aren't */ | |
2783 | writel(0, &oxu->regs->configured_flag); | |
2784 | ||
2785 | /* root hub is shut down separately (first, when possible) */ | |
2786 | spin_lock_irq(&oxu->lock); | |
2787 | if (oxu->async) | |
2788 | ehci_work(oxu); | |
2789 | spin_unlock_irq(&oxu->lock); | |
2790 | ehci_mem_cleanup(oxu); | |
2791 | ||
2792 | dbg_status(oxu, "oxu_stop completed", readl(&oxu->regs->status)); | |
2793 | } | |
2794 | ||
2795 | /* Kick in for silicon on any bus (not just pci, etc). | |
2796 | * This forcibly disables dma and IRQs, helping kexec and other cases | |
2797 | * where the next system software may expect clean state. | |
2798 | */ | |
2799 | static void oxu_shutdown(struct usb_hcd *hcd) | |
2800 | { | |
2801 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2802 | ||
2803 | (void) ehci_halt(oxu); | |
2804 | ehci_turn_off_all_ports(oxu); | |
2805 | ||
2806 | /* make BIOS/etc use companion controller during reboot */ | |
2807 | writel(0, &oxu->regs->configured_flag); | |
2808 | ||
2809 | /* unblock posted writes */ | |
2810 | readl(&oxu->regs->configured_flag); | |
2811 | } | |
2812 | ||
2813 | /* Non-error returns are a promise to giveback() the urb later | |
2814 | * we drop ownership so next owner (or urb unlink) can get it | |
2815 | * | |
2816 | * urb + dev is in hcd.self.controller.urb_list | |
2817 | * we're queueing TDs onto software and hardware lists | |
2818 | * | |
2819 | * hcd-specific init for hcpriv hasn't been done yet | |
2820 | * | |
2821 | * NOTE: control, bulk, and interrupt share the same code to append TDs | |
2822 | * to a (possibly active) QH, and the same QH scanning code. | |
2823 | */ | |
2824 | static int __oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, | |
2825 | gfp_t mem_flags) | |
2826 | { | |
2827 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2828 | struct list_head qtd_list; | |
2829 | ||
2830 | INIT_LIST_HEAD(&qtd_list); | |
2831 | ||
2832 | switch (usb_pipetype(urb->pipe)) { | |
2833 | case PIPE_CONTROL: | |
2834 | case PIPE_BULK: | |
2835 | default: | |
2836 | if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags)) | |
2837 | return -ENOMEM; | |
2838 | return submit_async(oxu, urb, &qtd_list, mem_flags); | |
2839 | ||
2840 | case PIPE_INTERRUPT: | |
2841 | if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags)) | |
2842 | return -ENOMEM; | |
2843 | return intr_submit(oxu, urb, &qtd_list, mem_flags); | |
2844 | ||
2845 | case PIPE_ISOCHRONOUS: | |
2846 | if (urb->dev->speed == USB_SPEED_HIGH) | |
2847 | return itd_submit(oxu, urb, mem_flags); | |
2848 | else | |
2849 | return sitd_submit(oxu, urb, mem_flags); | |
2850 | } | |
2851 | } | |
2852 | ||
2853 | /* This function is responsible for breaking URBs with big data size | |
2854 | * into smaller size and processing small urbs in sequence. | |
2855 | */ | |
2856 | static int oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, | |
2857 | gfp_t mem_flags) | |
2858 | { | |
2859 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2860 | int num, rem; | |
2861 | int transfer_buffer_length; | |
2862 | void *transfer_buffer; | |
2863 | struct urb *murb; | |
2864 | int i, ret; | |
2865 | ||
2866 | /* If not bulk pipe just enqueue the URB */ | |
2867 | if (!usb_pipebulk(urb->pipe)) | |
2868 | return __oxu_urb_enqueue(hcd, urb, mem_flags); | |
2869 | ||
2870 | /* Otherwise we should verify the USB transfer buffer size! */ | |
2871 | transfer_buffer = urb->transfer_buffer; | |
2872 | transfer_buffer_length = urb->transfer_buffer_length; | |
2873 | ||
2874 | num = urb->transfer_buffer_length / 4096; | |
2875 | rem = urb->transfer_buffer_length % 4096; | |
2876 | if (rem != 0) | |
2877 | num++; | |
2878 | ||
2879 | /* If URB is smaller than 4096 bytes just enqueue it! */ | |
2880 | if (num == 1) | |
2881 | return __oxu_urb_enqueue(hcd, urb, mem_flags); | |
2882 | ||
2883 | /* Ok, we have more job to do! :) */ | |
2884 | ||
2885 | for (i = 0; i < num - 1; i++) { | |
25985edc | 2886 | /* Get free micro URB poll till a free urb is received */ |
b92a78e5 RG |
2887 | |
2888 | do { | |
2889 | murb = (struct urb *) oxu_murb_alloc(oxu); | |
2890 | if (!murb) | |
2891 | schedule(); | |
2892 | } while (!murb); | |
2893 | ||
2894 | /* Coping the urb */ | |
2895 | memcpy(murb, urb, sizeof(struct urb)); | |
2896 | ||
2897 | murb->transfer_buffer_length = 4096; | |
2898 | murb->transfer_buffer = transfer_buffer + i * 4096; | |
2899 | ||
2900 | /* Null pointer for the encodes that this is a micro urb */ | |
2901 | murb->complete = NULL; | |
2902 | ||
2903 | ((struct oxu_murb *) murb)->main = urb; | |
2904 | ((struct oxu_murb *) murb)->last = 0; | |
2905 | ||
2906 | /* This loop is to guarantee urb to be processed when there's | |
2907 | * not enough resources at a particular time by retrying. | |
2908 | */ | |
2909 | do { | |
2910 | ret = __oxu_urb_enqueue(hcd, murb, mem_flags); | |
2911 | if (ret) | |
2912 | schedule(); | |
2913 | } while (ret); | |
2914 | } | |
2915 | ||
2916 | /* Last urb requires special handling */ | |
2917 | ||
25985edc | 2918 | /* Get free micro URB poll till a free urb is received */ |
b92a78e5 RG |
2919 | do { |
2920 | murb = (struct urb *) oxu_murb_alloc(oxu); | |
2921 | if (!murb) | |
2922 | schedule(); | |
2923 | } while (!murb); | |
2924 | ||
2925 | /* Coping the urb */ | |
2926 | memcpy(murb, urb, sizeof(struct urb)); | |
2927 | ||
2928 | murb->transfer_buffer_length = rem > 0 ? rem : 4096; | |
2929 | murb->transfer_buffer = transfer_buffer + (num - 1) * 4096; | |
2930 | ||
2931 | /* Null pointer for the encodes that this is a micro urb */ | |
2932 | murb->complete = NULL; | |
2933 | ||
2934 | ((struct oxu_murb *) murb)->main = urb; | |
2935 | ((struct oxu_murb *) murb)->last = 1; | |
2936 | ||
2937 | do { | |
2938 | ret = __oxu_urb_enqueue(hcd, murb, mem_flags); | |
2939 | if (ret) | |
2940 | schedule(); | |
2941 | } while (ret); | |
2942 | ||
2943 | return ret; | |
2944 | } | |
2945 | ||
2946 | /* Remove from hardware lists. | |
2947 | * Completions normally happen asynchronously | |
2948 | */ | |
2949 | static int oxu_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) | |
2950 | { | |
2951 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
2952 | struct ehci_qh *qh; | |
2953 | unsigned long flags; | |
2954 | ||
2955 | spin_lock_irqsave(&oxu->lock, flags); | |
2956 | switch (usb_pipetype(urb->pipe)) { | |
2957 | case PIPE_CONTROL: | |
2958 | case PIPE_BULK: | |
2959 | default: | |
2960 | qh = (struct ehci_qh *) urb->hcpriv; | |
2961 | if (!qh) | |
2962 | break; | |
2963 | unlink_async(oxu, qh); | |
2964 | break; | |
2965 | ||
2966 | case PIPE_INTERRUPT: | |
2967 | qh = (struct ehci_qh *) urb->hcpriv; | |
2968 | if (!qh) | |
2969 | break; | |
2970 | switch (qh->qh_state) { | |
2971 | case QH_STATE_LINKED: | |
2972 | intr_deschedule(oxu, qh); | |
2973 | /* FALL THROUGH */ | |
2974 | case QH_STATE_IDLE: | |
2975 | qh_completions(oxu, qh); | |
2976 | break; | |
2977 | default: | |
2978 | oxu_dbg(oxu, "bogus qh %p state %d\n", | |
2979 | qh, qh->qh_state); | |
2980 | goto done; | |
2981 | } | |
2982 | ||
2983 | /* reschedule QH iff another request is queued */ | |
2984 | if (!list_empty(&qh->qtd_list) | |
2985 | && HC_IS_RUNNING(hcd->state)) { | |
2986 | int status; | |
2987 | ||
2988 | status = qh_schedule(oxu, qh); | |
2989 | spin_unlock_irqrestore(&oxu->lock, flags); | |
2990 | ||
2991 | if (status != 0) { | |
2992 | /* shouldn't happen often, but ... | |
2993 | * FIXME kill those tds' urbs | |
2994 | */ | |
68980793 GKH |
2995 | dev_err(hcd->self.controller, |
2996 | "can't reschedule qh %p, err %d\n", qh, | |
2997 | status); | |
b92a78e5 RG |
2998 | } |
2999 | return status; | |
3000 | } | |
3001 | break; | |
3002 | } | |
3003 | done: | |
3004 | spin_unlock_irqrestore(&oxu->lock, flags); | |
3005 | return 0; | |
3006 | } | |
3007 | ||
3008 | /* Bulk qh holds the data toggle */ | |
3009 | static void oxu_endpoint_disable(struct usb_hcd *hcd, | |
3010 | struct usb_host_endpoint *ep) | |
3011 | { | |
3012 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3013 | unsigned long flags; | |
3014 | struct ehci_qh *qh, *tmp; | |
3015 | ||
3016 | /* ASSERT: any requests/urbs are being unlinked */ | |
3017 | /* ASSERT: nobody can be submitting urbs for this any more */ | |
3018 | ||
3019 | rescan: | |
3020 | spin_lock_irqsave(&oxu->lock, flags); | |
3021 | qh = ep->hcpriv; | |
3022 | if (!qh) | |
3023 | goto done; | |
3024 | ||
3025 | /* endpoints can be iso streams. for now, we don't | |
3026 | * accelerate iso completions ... so spin a while. | |
3027 | */ | |
3028 | if (qh->hw_info1 == 0) { | |
3029 | oxu_vdbg(oxu, "iso delay\n"); | |
3030 | goto idle_timeout; | |
3031 | } | |
3032 | ||
3033 | if (!HC_IS_RUNNING(hcd->state)) | |
3034 | qh->qh_state = QH_STATE_IDLE; | |
3035 | switch (qh->qh_state) { | |
3036 | case QH_STATE_LINKED: | |
3037 | for (tmp = oxu->async->qh_next.qh; | |
3038 | tmp && tmp != qh; | |
3039 | tmp = tmp->qh_next.qh) | |
3040 | continue; | |
3041 | /* periodic qh self-unlinks on empty */ | |
3042 | if (!tmp) | |
3043 | goto nogood; | |
3044 | unlink_async(oxu, qh); | |
3045 | /* FALL THROUGH */ | |
3046 | case QH_STATE_UNLINK: /* wait for hw to finish? */ | |
3047 | idle_timeout: | |
3048 | spin_unlock_irqrestore(&oxu->lock, flags); | |
3049 | schedule_timeout_uninterruptible(1); | |
3050 | goto rescan; | |
3051 | case QH_STATE_IDLE: /* fully unlinked */ | |
3052 | if (list_empty(&qh->qtd_list)) { | |
3053 | qh_put(qh); | |
3054 | break; | |
3055 | } | |
3056 | /* else FALL THROUGH */ | |
3057 | default: | |
3058 | nogood: | |
3059 | /* caller was supposed to have unlinked any requests; | |
3060 | * that's not our job. just leak this memory. | |
3061 | */ | |
3062 | oxu_err(oxu, "qh %p (#%02x) state %d%s\n", | |
3063 | qh, ep->desc.bEndpointAddress, qh->qh_state, | |
3064 | list_empty(&qh->qtd_list) ? "" : "(has tds)"); | |
3065 | break; | |
3066 | } | |
3067 | ep->hcpriv = NULL; | |
3068 | done: | |
3069 | spin_unlock_irqrestore(&oxu->lock, flags); | |
b92a78e5 RG |
3070 | } |
3071 | ||
3072 | static int oxu_get_frame(struct usb_hcd *hcd) | |
3073 | { | |
3074 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3075 | ||
3076 | return (readl(&oxu->regs->frame_index) >> 3) % | |
3077 | oxu->periodic_size; | |
3078 | } | |
3079 | ||
3080 | /* Build "status change" packet (one or two bytes) from HC registers */ | |
3081 | static int oxu_hub_status_data(struct usb_hcd *hcd, char *buf) | |
3082 | { | |
3083 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3084 | u32 temp, mask, status = 0; | |
3085 | int ports, i, retval = 1; | |
3086 | unsigned long flags; | |
3087 | ||
98f541c6 | 3088 | /* if !PM_RUNTIME, root hub timers won't get shut down ... */ |
b92a78e5 RG |
3089 | if (!HC_IS_RUNNING(hcd->state)) |
3090 | return 0; | |
3091 | ||
3092 | /* init status to no-changes */ | |
3093 | buf[0] = 0; | |
3094 | ports = HCS_N_PORTS(oxu->hcs_params); | |
3095 | if (ports > 7) { | |
3096 | buf[1] = 0; | |
3097 | retval++; | |
3098 | } | |
3099 | ||
3100 | /* Some boards (mostly VIA?) report bogus overcurrent indications, | |
3101 | * causing massive log spam unless we completely ignore them. It | |
b595076a | 3102 | * may be relevant that VIA VT8235 controllers, where PORT_POWER is |
b92a78e5 RG |
3103 | * always set, seem to clear PORT_OCC and PORT_CSC when writing to |
3104 | * PORT_POWER; that's surprising, but maybe within-spec. | |
3105 | */ | |
3106 | if (!ignore_oc) | |
3107 | mask = PORT_CSC | PORT_PEC | PORT_OCC; | |
3108 | else | |
3109 | mask = PORT_CSC | PORT_PEC; | |
3110 | ||
3111 | /* no hub change reports (bit 0) for now (power, ...) */ | |
3112 | ||
3113 | /* port N changes (bit N)? */ | |
3114 | spin_lock_irqsave(&oxu->lock, flags); | |
3115 | for (i = 0; i < ports; i++) { | |
3116 | temp = readl(&oxu->regs->port_status[i]); | |
3117 | ||
3118 | /* | |
3119 | * Return status information even for ports with OWNER set. | |
3120 | * Otherwise khubd wouldn't see the disconnect event when a | |
3121 | * high-speed device is switched over to the companion | |
3122 | * controller by the user. | |
3123 | */ | |
3124 | ||
3125 | if (!(temp & PORT_CONNECT)) | |
3126 | oxu->reset_done[i] = 0; | |
3127 | if ((temp & mask) != 0 || ((temp & PORT_RESUME) != 0 && | |
3128 | time_after_eq(jiffies, oxu->reset_done[i]))) { | |
3129 | if (i < 7) | |
3130 | buf[0] |= 1 << (i + 1); | |
3131 | else | |
3132 | buf[1] |= 1 << (i - 7); | |
3133 | status = STS_PCD; | |
3134 | } | |
3135 | } | |
3136 | /* FIXME autosuspend idle root hubs */ | |
3137 | spin_unlock_irqrestore(&oxu->lock, flags); | |
3138 | return status ? retval : 0; | |
3139 | } | |
3140 | ||
3141 | /* Returns the speed of a device attached to a port on the root hub. */ | |
3142 | static inline unsigned int oxu_port_speed(struct oxu_hcd *oxu, | |
3143 | unsigned int portsc) | |
3144 | { | |
3145 | switch ((portsc >> 26) & 3) { | |
3146 | case 0: | |
3147 | return 0; | |
3148 | case 1: | |
288ead45 | 3149 | return USB_PORT_STAT_LOW_SPEED; |
b92a78e5 RG |
3150 | case 2: |
3151 | default: | |
288ead45 | 3152 | return USB_PORT_STAT_HIGH_SPEED; |
b92a78e5 RG |
3153 | } |
3154 | } | |
3155 | ||
3156 | #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E) | |
3157 | static int oxu_hub_control(struct usb_hcd *hcd, u16 typeReq, | |
3158 | u16 wValue, u16 wIndex, char *buf, u16 wLength) | |
3159 | { | |
3160 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3161 | int ports = HCS_N_PORTS(oxu->hcs_params); | |
3162 | u32 __iomem *status_reg = &oxu->regs->port_status[wIndex - 1]; | |
3163 | u32 temp, status; | |
3164 | unsigned long flags; | |
3165 | int retval = 0; | |
3166 | unsigned selector; | |
3167 | ||
3168 | /* | |
3169 | * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. | |
3170 | * HCS_INDICATOR may say we can change LEDs to off/amber/green. | |
3171 | * (track current state ourselves) ... blink for diagnostics, | |
3172 | * power, "this is the one", etc. EHCI spec supports this. | |
3173 | */ | |
3174 | ||
3175 | spin_lock_irqsave(&oxu->lock, flags); | |
3176 | switch (typeReq) { | |
3177 | case ClearHubFeature: | |
3178 | switch (wValue) { | |
3179 | case C_HUB_LOCAL_POWER: | |
3180 | case C_HUB_OVER_CURRENT: | |
3181 | /* no hub-wide feature/status flags */ | |
3182 | break; | |
3183 | default: | |
3184 | goto error; | |
3185 | } | |
3186 | break; | |
3187 | case ClearPortFeature: | |
3188 | if (!wIndex || wIndex > ports) | |
3189 | goto error; | |
3190 | wIndex--; | |
3191 | temp = readl(status_reg); | |
3192 | ||
3193 | /* | |
3194 | * Even if OWNER is set, so the port is owned by the | |
3195 | * companion controller, khubd needs to be able to clear | |
3196 | * the port-change status bits (especially | |
749da5f8 | 3197 | * USB_PORT_STAT_C_CONNECTION). |
b92a78e5 RG |
3198 | */ |
3199 | ||
3200 | switch (wValue) { | |
3201 | case USB_PORT_FEAT_ENABLE: | |
3202 | writel(temp & ~PORT_PE, status_reg); | |
3203 | break; | |
3204 | case USB_PORT_FEAT_C_ENABLE: | |
3205 | writel((temp & ~PORT_RWC_BITS) | PORT_PEC, status_reg); | |
3206 | break; | |
3207 | case USB_PORT_FEAT_SUSPEND: | |
3208 | if (temp & PORT_RESET) | |
3209 | goto error; | |
3210 | if (temp & PORT_SUSPEND) { | |
3211 | if ((temp & PORT_PE) == 0) | |
3212 | goto error; | |
3213 | /* resume signaling for 20 msec */ | |
3214 | temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS); | |
3215 | writel(temp | PORT_RESUME, status_reg); | |
3216 | oxu->reset_done[wIndex] = jiffies | |
3217 | + msecs_to_jiffies(20); | |
3218 | } | |
3219 | break; | |
3220 | case USB_PORT_FEAT_C_SUSPEND: | |
3221 | /* we auto-clear this feature */ | |
3222 | break; | |
3223 | case USB_PORT_FEAT_POWER: | |
3224 | if (HCS_PPC(oxu->hcs_params)) | |
3225 | writel(temp & ~(PORT_RWC_BITS | PORT_POWER), | |
3226 | status_reg); | |
3227 | break; | |
3228 | case USB_PORT_FEAT_C_CONNECTION: | |
3229 | writel((temp & ~PORT_RWC_BITS) | PORT_CSC, status_reg); | |
3230 | break; | |
3231 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
3232 | writel((temp & ~PORT_RWC_BITS) | PORT_OCC, status_reg); | |
3233 | break; | |
3234 | case USB_PORT_FEAT_C_RESET: | |
3235 | /* GetPortStatus clears reset */ | |
3236 | break; | |
3237 | default: | |
3238 | goto error; | |
3239 | } | |
3240 | readl(&oxu->regs->command); /* unblock posted write */ | |
3241 | break; | |
3242 | case GetHubDescriptor: | |
3243 | ehci_hub_descriptor(oxu, (struct usb_hub_descriptor *) | |
3244 | buf); | |
3245 | break; | |
3246 | case GetHubStatus: | |
3247 | /* no hub-wide feature/status flags */ | |
3248 | memset(buf, 0, 4); | |
3249 | break; | |
3250 | case GetPortStatus: | |
3251 | if (!wIndex || wIndex > ports) | |
3252 | goto error; | |
3253 | wIndex--; | |
3254 | status = 0; | |
3255 | temp = readl(status_reg); | |
3256 | ||
3257 | /* wPortChange bits */ | |
3258 | if (temp & PORT_CSC) | |
749da5f8 | 3259 | status |= USB_PORT_STAT_C_CONNECTION << 16; |
b92a78e5 | 3260 | if (temp & PORT_PEC) |
749da5f8 | 3261 | status |= USB_PORT_STAT_C_ENABLE << 16; |
b92a78e5 | 3262 | if ((temp & PORT_OCC) && !ignore_oc) |
749da5f8 | 3263 | status |= USB_PORT_STAT_C_OVERCURRENT << 16; |
b92a78e5 RG |
3264 | |
3265 | /* whoever resumes must GetPortStatus to complete it!! */ | |
3266 | if (temp & PORT_RESUME) { | |
3267 | ||
3268 | /* Remote Wakeup received? */ | |
3269 | if (!oxu->reset_done[wIndex]) { | |
3270 | /* resume signaling for 20 msec */ | |
3271 | oxu->reset_done[wIndex] = jiffies | |
3272 | + msecs_to_jiffies(20); | |
3273 | /* check the port again */ | |
3274 | mod_timer(&oxu_to_hcd(oxu)->rh_timer, | |
3275 | oxu->reset_done[wIndex]); | |
3276 | } | |
3277 | ||
3278 | /* resume completed? */ | |
3279 | else if (time_after_eq(jiffies, | |
3280 | oxu->reset_done[wIndex])) { | |
749da5f8 | 3281 | status |= USB_PORT_STAT_C_SUSPEND << 16; |
b92a78e5 RG |
3282 | oxu->reset_done[wIndex] = 0; |
3283 | ||
3284 | /* stop resume signaling */ | |
3285 | temp = readl(status_reg); | |
3286 | writel(temp & ~(PORT_RWC_BITS | PORT_RESUME), | |
3287 | status_reg); | |
3288 | retval = handshake(oxu, status_reg, | |
3289 | PORT_RESUME, 0, 2000 /* 2msec */); | |
3290 | if (retval != 0) { | |
3291 | oxu_err(oxu, | |
3292 | "port %d resume error %d\n", | |
3293 | wIndex + 1, retval); | |
3294 | goto error; | |
3295 | } | |
3296 | temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10)); | |
3297 | } | |
3298 | } | |
3299 | ||
3300 | /* whoever resets must GetPortStatus to complete it!! */ | |
3301 | if ((temp & PORT_RESET) | |
3302 | && time_after_eq(jiffies, | |
3303 | oxu->reset_done[wIndex])) { | |
749da5f8 | 3304 | status |= USB_PORT_STAT_C_RESET << 16; |
b92a78e5 RG |
3305 | oxu->reset_done[wIndex] = 0; |
3306 | ||
3307 | /* force reset to complete */ | |
3308 | writel(temp & ~(PORT_RWC_BITS | PORT_RESET), | |
3309 | status_reg); | |
3310 | /* REVISIT: some hardware needs 550+ usec to clear | |
3311 | * this bit; seems too long to spin routinely... | |
3312 | */ | |
3313 | retval = handshake(oxu, status_reg, | |
3314 | PORT_RESET, 0, 750); | |
3315 | if (retval != 0) { | |
3316 | oxu_err(oxu, "port %d reset error %d\n", | |
3317 | wIndex + 1, retval); | |
3318 | goto error; | |
3319 | } | |
3320 | ||
3321 | /* see what we found out */ | |
3322 | temp = check_reset_complete(oxu, wIndex, status_reg, | |
3323 | readl(status_reg)); | |
3324 | } | |
3325 | ||
3326 | /* transfer dedicated ports to the companion hc */ | |
3327 | if ((temp & PORT_CONNECT) && | |
3328 | test_bit(wIndex, &oxu->companion_ports)) { | |
3329 | temp &= ~PORT_RWC_BITS; | |
3330 | temp |= PORT_OWNER; | |
3331 | writel(temp, status_reg); | |
3332 | oxu_dbg(oxu, "port %d --> companion\n", wIndex + 1); | |
3333 | temp = readl(status_reg); | |
3334 | } | |
3335 | ||
3336 | /* | |
3337 | * Even if OWNER is set, there's no harm letting khubd | |
3338 | * see the wPortStatus values (they should all be 0 except | |
3339 | * for PORT_POWER anyway). | |
3340 | */ | |
3341 | ||
3342 | if (temp & PORT_CONNECT) { | |
749da5f8 | 3343 | status |= USB_PORT_STAT_CONNECTION; |
b92a78e5 RG |
3344 | /* status may be from integrated TT */ |
3345 | status |= oxu_port_speed(oxu, temp); | |
3346 | } | |
3347 | if (temp & PORT_PE) | |
749da5f8 | 3348 | status |= USB_PORT_STAT_ENABLE; |
b92a78e5 | 3349 | if (temp & (PORT_SUSPEND|PORT_RESUME)) |
749da5f8 | 3350 | status |= USB_PORT_STAT_SUSPEND; |
b92a78e5 | 3351 | if (temp & PORT_OC) |
749da5f8 | 3352 | status |= USB_PORT_STAT_OVERCURRENT; |
b92a78e5 | 3353 | if (temp & PORT_RESET) |
749da5f8 | 3354 | status |= USB_PORT_STAT_RESET; |
b92a78e5 | 3355 | if (temp & PORT_POWER) |
749da5f8 | 3356 | status |= USB_PORT_STAT_POWER; |
b92a78e5 RG |
3357 | |
3358 | #ifndef OXU_VERBOSE_DEBUG | |
3359 | if (status & ~0xffff) /* only if wPortChange is interesting */ | |
3360 | #endif | |
3361 | dbg_port(oxu, "GetStatus", wIndex + 1, temp); | |
3362 | put_unaligned(cpu_to_le32(status), (__le32 *) buf); | |
3363 | break; | |
3364 | case SetHubFeature: | |
3365 | switch (wValue) { | |
3366 | case C_HUB_LOCAL_POWER: | |
3367 | case C_HUB_OVER_CURRENT: | |
3368 | /* no hub-wide feature/status flags */ | |
3369 | break; | |
3370 | default: | |
3371 | goto error; | |
3372 | } | |
3373 | break; | |
3374 | case SetPortFeature: | |
3375 | selector = wIndex >> 8; | |
3376 | wIndex &= 0xff; | |
3377 | if (!wIndex || wIndex > ports) | |
3378 | goto error; | |
3379 | wIndex--; | |
3380 | temp = readl(status_reg); | |
3381 | if (temp & PORT_OWNER) | |
3382 | break; | |
3383 | ||
3384 | temp &= ~PORT_RWC_BITS; | |
3385 | switch (wValue) { | |
3386 | case USB_PORT_FEAT_SUSPEND: | |
3387 | if ((temp & PORT_PE) == 0 | |
3388 | || (temp & PORT_RESET) != 0) | |
3389 | goto error; | |
3390 | if (device_may_wakeup(&hcd->self.root_hub->dev)) | |
3391 | temp |= PORT_WAKE_BITS; | |
3392 | writel(temp | PORT_SUSPEND, status_reg); | |
3393 | break; | |
3394 | case USB_PORT_FEAT_POWER: | |
3395 | if (HCS_PPC(oxu->hcs_params)) | |
3396 | writel(temp | PORT_POWER, status_reg); | |
3397 | break; | |
3398 | case USB_PORT_FEAT_RESET: | |
3399 | if (temp & PORT_RESUME) | |
3400 | goto error; | |
3401 | /* line status bits may report this as low speed, | |
3402 | * which can be fine if this root hub has a | |
3403 | * transaction translator built in. | |
3404 | */ | |
3405 | oxu_vdbg(oxu, "port %d reset\n", wIndex + 1); | |
3406 | temp |= PORT_RESET; | |
3407 | temp &= ~PORT_PE; | |
3408 | ||
3409 | /* | |
3410 | * caller must wait, then call GetPortStatus | |
3411 | * usb 2.0 spec says 50 ms resets on root | |
3412 | */ | |
3413 | oxu->reset_done[wIndex] = jiffies | |
3414 | + msecs_to_jiffies(50); | |
3415 | writel(temp, status_reg); | |
3416 | break; | |
3417 | ||
3418 | /* For downstream facing ports (these): one hub port is put | |
3419 | * into test mode according to USB2 11.24.2.13, then the hub | |
3420 | * must be reset (which for root hub now means rmmod+modprobe, | |
3421 | * or else system reboot). See EHCI 2.3.9 and 4.14 for info | |
3422 | * about the EHCI-specific stuff. | |
3423 | */ | |
3424 | case USB_PORT_FEAT_TEST: | |
3425 | if (!selector || selector > 5) | |
3426 | goto error; | |
3427 | ehci_quiesce(oxu); | |
3428 | ehci_halt(oxu); | |
3429 | temp |= selector << 16; | |
3430 | writel(temp, status_reg); | |
3431 | break; | |
3432 | ||
3433 | default: | |
3434 | goto error; | |
3435 | } | |
3436 | readl(&oxu->regs->command); /* unblock posted writes */ | |
3437 | break; | |
3438 | ||
3439 | default: | |
3440 | error: | |
3441 | /* "stall" on error */ | |
3442 | retval = -EPIPE; | |
3443 | } | |
3444 | spin_unlock_irqrestore(&oxu->lock, flags); | |
3445 | return retval; | |
3446 | } | |
3447 | ||
3448 | #ifdef CONFIG_PM | |
3449 | ||
3450 | static int oxu_bus_suspend(struct usb_hcd *hcd) | |
3451 | { | |
3452 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3453 | int port; | |
3454 | int mask; | |
3455 | ||
3456 | oxu_dbg(oxu, "suspend root hub\n"); | |
3457 | ||
3458 | if (time_before(jiffies, oxu->next_statechange)) | |
3459 | msleep(5); | |
3460 | ||
3461 | port = HCS_N_PORTS(oxu->hcs_params); | |
3462 | spin_lock_irq(&oxu->lock); | |
3463 | ||
3464 | /* stop schedules, clean any completed work */ | |
3465 | if (HC_IS_RUNNING(hcd->state)) { | |
3466 | ehci_quiesce(oxu); | |
3467 | hcd->state = HC_STATE_QUIESCING; | |
3468 | } | |
3469 | oxu->command = readl(&oxu->regs->command); | |
3470 | if (oxu->reclaim) | |
3471 | oxu->reclaim_ready = 1; | |
3472 | ehci_work(oxu); | |
3473 | ||
3474 | /* Unlike other USB host controller types, EHCI doesn't have | |
3475 | * any notion of "global" or bus-wide suspend. The driver has | |
3476 | * to manually suspend all the active unsuspended ports, and | |
3477 | * then manually resume them in the bus_resume() routine. | |
3478 | */ | |
3479 | oxu->bus_suspended = 0; | |
3480 | while (port--) { | |
3481 | u32 __iomem *reg = &oxu->regs->port_status[port]; | |
3482 | u32 t1 = readl(reg) & ~PORT_RWC_BITS; | |
3483 | u32 t2 = t1; | |
3484 | ||
3485 | /* keep track of which ports we suspend */ | |
3486 | if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) && | |
3487 | !(t1 & PORT_SUSPEND)) { | |
3488 | t2 |= PORT_SUSPEND; | |
3489 | set_bit(port, &oxu->bus_suspended); | |
3490 | } | |
3491 | ||
3492 | /* enable remote wakeup on all ports */ | |
3493 | if (device_may_wakeup(&hcd->self.root_hub->dev)) | |
3494 | t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E; | |
3495 | else | |
3496 | t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E); | |
3497 | ||
3498 | if (t1 != t2) { | |
3499 | oxu_vdbg(oxu, "port %d, %08x -> %08x\n", | |
3500 | port + 1, t1, t2); | |
3501 | writel(t2, reg); | |
3502 | } | |
3503 | } | |
3504 | ||
3505 | /* turn off now-idle HC */ | |
3506 | del_timer_sync(&oxu->watchdog); | |
3507 | ehci_halt(oxu); | |
3508 | hcd->state = HC_STATE_SUSPENDED; | |
3509 | ||
3510 | /* allow remote wakeup */ | |
3511 | mask = INTR_MASK; | |
3512 | if (!device_may_wakeup(&hcd->self.root_hub->dev)) | |
3513 | mask &= ~STS_PCD; | |
3514 | writel(mask, &oxu->regs->intr_enable); | |
3515 | readl(&oxu->regs->intr_enable); | |
3516 | ||
3517 | oxu->next_statechange = jiffies + msecs_to_jiffies(10); | |
3518 | spin_unlock_irq(&oxu->lock); | |
3519 | return 0; | |
3520 | } | |
3521 | ||
3522 | /* Caller has locked the root hub, and should reset/reinit on error */ | |
3523 | static int oxu_bus_resume(struct usb_hcd *hcd) | |
3524 | { | |
3525 | struct oxu_hcd *oxu = hcd_to_oxu(hcd); | |
3526 | u32 temp; | |
3527 | int i; | |
3528 | ||
3529 | if (time_before(jiffies, oxu->next_statechange)) | |
3530 | msleep(5); | |
3531 | spin_lock_irq(&oxu->lock); | |
3532 | ||
3533 | /* Ideally and we've got a real resume here, and no port's power | |
3534 | * was lost. (For PCI, that means Vaux was maintained.) But we | |
3535 | * could instead be restoring a swsusp snapshot -- so that BIOS was | |
3536 | * the last user of the controller, not reset/pm hardware keeping | |
3537 | * state we gave to it. | |
3538 | */ | |
3539 | temp = readl(&oxu->regs->intr_enable); | |
3540 | oxu_dbg(oxu, "resume root hub%s\n", temp ? "" : " after power loss"); | |
3541 | ||
3542 | /* at least some APM implementations will try to deliver | |
3543 | * IRQs right away, so delay them until we're ready. | |
3544 | */ | |
3545 | writel(0, &oxu->regs->intr_enable); | |
3546 | ||
3547 | /* re-init operational registers */ | |
3548 | writel(0, &oxu->regs->segment); | |
3549 | writel(oxu->periodic_dma, &oxu->regs->frame_list); | |
3550 | writel((u32) oxu->async->qh_dma, &oxu->regs->async_next); | |
3551 | ||
3552 | /* restore CMD_RUN, framelist size, and irq threshold */ | |
3553 | writel(oxu->command, &oxu->regs->command); | |
3554 | ||
3555 | /* Some controller/firmware combinations need a delay during which | |
3556 | * they set up the port statuses. See Bugzilla #8190. */ | |
3557 | mdelay(8); | |
3558 | ||
3559 | /* manually resume the ports we suspended during bus_suspend() */ | |
3560 | i = HCS_N_PORTS(oxu->hcs_params); | |
3561 | while (i--) { | |
3562 | temp = readl(&oxu->regs->port_status[i]); | |
3563 | temp &= ~(PORT_RWC_BITS | |
3564 | | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E); | |
3565 | if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) { | |
3566 | oxu->reset_done[i] = jiffies + msecs_to_jiffies(20); | |
3567 | temp |= PORT_RESUME; | |
3568 | } | |
3569 | writel(temp, &oxu->regs->port_status[i]); | |
3570 | } | |
3571 | i = HCS_N_PORTS(oxu->hcs_params); | |
3572 | mdelay(20); | |
3573 | while (i--) { | |
3574 | temp = readl(&oxu->regs->port_status[i]); | |
3575 | if (test_bit(i, &oxu->bus_suspended) && (temp & PORT_SUSPEND)) { | |
3576 | temp &= ~(PORT_RWC_BITS | PORT_RESUME); | |
3577 | writel(temp, &oxu->regs->port_status[i]); | |
3578 | oxu_vdbg(oxu, "resumed port %d\n", i + 1); | |
3579 | } | |
3580 | } | |
3581 | (void) readl(&oxu->regs->command); | |
3582 | ||
3583 | /* maybe re-activate the schedule(s) */ | |
3584 | temp = 0; | |
3585 | if (oxu->async->qh_next.qh) | |
3586 | temp |= CMD_ASE; | |
3587 | if (oxu->periodic_sched) | |
3588 | temp |= CMD_PSE; | |
3589 | if (temp) { | |
3590 | oxu->command |= temp; | |
3591 | writel(oxu->command, &oxu->regs->command); | |
3592 | } | |
3593 | ||
3594 | oxu->next_statechange = jiffies + msecs_to_jiffies(5); | |
3595 | hcd->state = HC_STATE_RUNNING; | |
3596 | ||
3597 | /* Now we can safely re-enable irqs */ | |
3598 | writel(INTR_MASK, &oxu->regs->intr_enable); | |
3599 | ||
3600 | spin_unlock_irq(&oxu->lock); | |
3601 | return 0; | |
3602 | } | |
3603 | ||
3604 | #else | |
3605 | ||
3606 | static int oxu_bus_suspend(struct usb_hcd *hcd) | |
3607 | { | |
3608 | return 0; | |
3609 | } | |
3610 | ||
3611 | static int oxu_bus_resume(struct usb_hcd *hcd) | |
3612 | { | |
3613 | return 0; | |
3614 | } | |
3615 | ||
3616 | #endif /* CONFIG_PM */ | |
3617 | ||
3618 | static const struct hc_driver oxu_hc_driver = { | |
3619 | .description = "oxu210hp_hcd", | |
3620 | .product_desc = "oxu210hp HCD", | |
3621 | .hcd_priv_size = sizeof(struct oxu_hcd), | |
3622 | ||
3623 | /* | |
3624 | * Generic hardware linkage | |
3625 | */ | |
3626 | .irq = oxu_irq, | |
3627 | .flags = HCD_MEMORY | HCD_USB2, | |
3628 | ||
3629 | /* | |
3630 | * Basic lifecycle operations | |
3631 | */ | |
3632 | .reset = oxu_reset, | |
3633 | .start = oxu_run, | |
3634 | .stop = oxu_stop, | |
3635 | .shutdown = oxu_shutdown, | |
3636 | ||
3637 | /* | |
3638 | * Managing i/o requests and associated device resources | |
3639 | */ | |
3640 | .urb_enqueue = oxu_urb_enqueue, | |
3641 | .urb_dequeue = oxu_urb_dequeue, | |
3642 | .endpoint_disable = oxu_endpoint_disable, | |
3643 | ||
3644 | /* | |
3645 | * Scheduling support | |
3646 | */ | |
3647 | .get_frame_number = oxu_get_frame, | |
3648 | ||
3649 | /* | |
3650 | * Root hub support | |
3651 | */ | |
3652 | .hub_status_data = oxu_hub_status_data, | |
3653 | .hub_control = oxu_hub_control, | |
3654 | .bus_suspend = oxu_bus_suspend, | |
3655 | .bus_resume = oxu_bus_resume, | |
3656 | }; | |
3657 | ||
3658 | /* | |
3659 | * Module stuff | |
3660 | */ | |
3661 | ||
3662 | static void oxu_configuration(struct platform_device *pdev, void *base) | |
3663 | { | |
3664 | u32 tmp; | |
3665 | ||
3666 | /* Initialize top level registers. | |
3667 | * First write ever | |
3668 | */ | |
3669 | oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D); | |
3670 | oxu_writel(base, OXU_SOFTRESET, OXU_SRESET); | |
3671 | oxu_writel(base, OXU_HOSTIFCONFIG, 0x0000037D); | |
3672 | ||
3673 | tmp = oxu_readl(base, OXU_PIOBURSTREADCTRL); | |
3674 | oxu_writel(base, OXU_PIOBURSTREADCTRL, tmp | 0x0040); | |
3675 | ||
3676 | oxu_writel(base, OXU_ASO, OXU_SPHPOEN | OXU_OVRCCURPUPDEN | | |
3677 | OXU_COMPARATOR | OXU_ASO_OP); | |
3678 | ||
3679 | tmp = oxu_readl(base, OXU_CLKCTRL_SET); | |
3680 | oxu_writel(base, OXU_CLKCTRL_SET, tmp | OXU_SYSCLKEN | OXU_USBOTGCLKEN); | |
3681 | ||
3682 | /* Clear all top interrupt enable */ | |
3683 | oxu_writel(base, OXU_CHIPIRQEN_CLR, 0xff); | |
3684 | ||
3685 | /* Clear all top interrupt status */ | |
3686 | oxu_writel(base, OXU_CHIPIRQSTATUS, 0xff); | |
3687 | ||
3688 | /* Enable all needed top interrupt except OTG SPH core */ | |
3689 | oxu_writel(base, OXU_CHIPIRQEN_SET, OXU_USBSPHLPWUI | OXU_USBOTGLPWUI); | |
3690 | } | |
3691 | ||
3692 | static int oxu_verify_id(struct platform_device *pdev, void *base) | |
3693 | { | |
3694 | u32 id; | |
82cef0b8 | 3695 | static const char * const bo[] = { |
b92a78e5 RG |
3696 | "reserved", |
3697 | "128-pin LQFP", | |
3698 | "84-pin TFBGA", | |
3699 | "reserved", | |
3700 | }; | |
3701 | ||
3702 | /* Read controller signature register to find a match */ | |
3703 | id = oxu_readl(base, OXU_DEVICEID); | |
3704 | dev_info(&pdev->dev, "device ID %x\n", id); | |
3705 | if ((id & OXU_REV_MASK) != (OXU_REV_2100 << OXU_REV_SHIFT)) | |
3706 | return -1; | |
3707 | ||
3708 | dev_info(&pdev->dev, "found device %x %s (%04x:%04x)\n", | |
3709 | id >> OXU_REV_SHIFT, | |
3710 | bo[(id & OXU_BO_MASK) >> OXU_BO_SHIFT], | |
3711 | (id & OXU_MAJ_REV_MASK) >> OXU_MAJ_REV_SHIFT, | |
3712 | (id & OXU_MIN_REV_MASK) >> OXU_MIN_REV_SHIFT); | |
3713 | ||
3714 | return 0; | |
3715 | } | |
3716 | ||
3717 | static const struct hc_driver oxu_hc_driver; | |
3718 | static struct usb_hcd *oxu_create(struct platform_device *pdev, | |
3719 | unsigned long memstart, unsigned long memlen, | |
3720 | void *base, int irq, int otg) | |
3721 | { | |
3722 | struct device *dev = &pdev->dev; | |
3723 | ||
3724 | struct usb_hcd *hcd; | |
3725 | struct oxu_hcd *oxu; | |
3726 | int ret; | |
3727 | ||
3728 | /* Set endian mode and host mode */ | |
3729 | oxu_writel(base + (otg ? OXU_OTG_CORE_OFFSET : OXU_SPH_CORE_OFFSET), | |
3730 | OXU_USBMODE, | |
3731 | OXU_CM_HOST_ONLY | OXU_ES_LITTLE | OXU_VBPS); | |
3732 | ||
3733 | hcd = usb_create_hcd(&oxu_hc_driver, dev, | |
3734 | otg ? "oxu210hp_otg" : "oxu210hp_sph"); | |
3735 | if (!hcd) | |
3736 | return ERR_PTR(-ENOMEM); | |
3737 | ||
3738 | hcd->rsrc_start = memstart; | |
3739 | hcd->rsrc_len = memlen; | |
3740 | hcd->regs = base; | |
3741 | hcd->irq = irq; | |
3742 | hcd->state = HC_STATE_HALT; | |
3743 | ||
3744 | oxu = hcd_to_oxu(hcd); | |
3745 | oxu->is_otg = otg; | |
3746 | ||
3747 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); | |
3748 | if (ret < 0) | |
3749 | return ERR_PTR(ret); | |
3750 | ||
3751 | return hcd; | |
3752 | } | |
3753 | ||
3754 | static int oxu_init(struct platform_device *pdev, | |
3755 | unsigned long memstart, unsigned long memlen, | |
3756 | void *base, int irq) | |
3757 | { | |
3758 | struct oxu_info *info = platform_get_drvdata(pdev); | |
3759 | struct usb_hcd *hcd; | |
3760 | int ret; | |
3761 | ||
3762 | /* First time configuration at start up */ | |
3763 | oxu_configuration(pdev, base); | |
3764 | ||
3765 | ret = oxu_verify_id(pdev, base); | |
3766 | if (ret) { | |
3767 | dev_err(&pdev->dev, "no devices found!\n"); | |
3768 | return -ENODEV; | |
3769 | } | |
3770 | ||
3771 | /* Create the OTG controller */ | |
3772 | hcd = oxu_create(pdev, memstart, memlen, base, irq, 1); | |
3773 | if (IS_ERR(hcd)) { | |
3774 | dev_err(&pdev->dev, "cannot create OTG controller!\n"); | |
3775 | ret = PTR_ERR(hcd); | |
3776 | goto error_create_otg; | |
3777 | } | |
3778 | info->hcd[0] = hcd; | |
3779 | ||
3780 | /* Create the SPH host controller */ | |
3781 | hcd = oxu_create(pdev, memstart, memlen, base, irq, 0); | |
3782 | if (IS_ERR(hcd)) { | |
3783 | dev_err(&pdev->dev, "cannot create SPH controller!\n"); | |
3784 | ret = PTR_ERR(hcd); | |
3785 | goto error_create_sph; | |
3786 | } | |
3787 | info->hcd[1] = hcd; | |
3788 | ||
3789 | oxu_writel(base, OXU_CHIPIRQEN_SET, | |
3790 | oxu_readl(base, OXU_CHIPIRQEN_SET) | 3); | |
3791 | ||
3792 | return 0; | |
3793 | ||
3794 | error_create_sph: | |
3795 | usb_remove_hcd(info->hcd[0]); | |
3796 | usb_put_hcd(info->hcd[0]); | |
3797 | ||
3798 | error_create_otg: | |
3799 | return ret; | |
3800 | } | |
3801 | ||
3802 | static int oxu_drv_probe(struct platform_device *pdev) | |
3803 | { | |
3804 | struct resource *res; | |
3805 | void *base; | |
3806 | unsigned long memstart, memlen; | |
3807 | int irq, ret; | |
3808 | struct oxu_info *info; | |
3809 | ||
3810 | if (usb_disabled()) | |
3811 | return -ENODEV; | |
3812 | ||
3813 | /* | |
3814 | * Get the platform resources | |
3815 | */ | |
3816 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
3817 | if (!res) { | |
3818 | dev_err(&pdev->dev, | |
74c71ebd | 3819 | "no IRQ! Check %s setup!\n", dev_name(&pdev->dev)); |
b92a78e5 RG |
3820 | return -ENODEV; |
3821 | } | |
3822 | irq = res->start; | |
3823 | dev_dbg(&pdev->dev, "IRQ resource %d\n", irq); | |
3824 | ||
3825 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
3826 | if (!res) { | |
3827 | dev_err(&pdev->dev, "no registers address! Check %s setup!\n", | |
74c71ebd | 3828 | dev_name(&pdev->dev)); |
b92a78e5 RG |
3829 | return -ENODEV; |
3830 | } | |
3831 | memstart = res->start; | |
28f65c11 | 3832 | memlen = resource_size(res); |
b92a78e5 RG |
3833 | dev_dbg(&pdev->dev, "MEM resource %lx-%lx\n", memstart, memlen); |
3834 | if (!request_mem_region(memstart, memlen, | |
3835 | oxu_hc_driver.description)) { | |
3836 | dev_dbg(&pdev->dev, "memory area already in use\n"); | |
3837 | return -EBUSY; | |
3838 | } | |
3839 | ||
dced35ae | 3840 | ret = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING); |
b92a78e5 RG |
3841 | if (ret) { |
3842 | dev_err(&pdev->dev, "error setting irq type\n"); | |
3843 | ret = -EFAULT; | |
3844 | goto error_set_irq_type; | |
3845 | } | |
3846 | ||
3847 | base = ioremap(memstart, memlen); | |
3848 | if (!base) { | |
3849 | dev_dbg(&pdev->dev, "error mapping memory\n"); | |
3850 | ret = -EFAULT; | |
3851 | goto error_ioremap; | |
3852 | } | |
3853 | ||
3854 | /* Allocate a driver data struct to hold useful info for both | |
3855 | * SPH & OTG devices | |
3856 | */ | |
3857 | info = kzalloc(sizeof(struct oxu_info), GFP_KERNEL); | |
3858 | if (!info) { | |
3859 | dev_dbg(&pdev->dev, "error allocating memory\n"); | |
3860 | ret = -EFAULT; | |
3861 | goto error_alloc; | |
3862 | } | |
3863 | platform_set_drvdata(pdev, info); | |
3864 | ||
3865 | ret = oxu_init(pdev, memstart, memlen, base, irq); | |
3866 | if (ret < 0) { | |
3867 | dev_dbg(&pdev->dev, "cannot init USB devices\n"); | |
3868 | goto error_init; | |
3869 | } | |
3870 | ||
3871 | dev_info(&pdev->dev, "devices enabled and running\n"); | |
3872 | platform_set_drvdata(pdev, info); | |
3873 | ||
3874 | return 0; | |
3875 | ||
3876 | error_init: | |
3877 | kfree(info); | |
3878 | platform_set_drvdata(pdev, NULL); | |
3879 | ||
3880 | error_alloc: | |
3881 | iounmap(base); | |
3882 | ||
3883 | error_set_irq_type: | |
3884 | error_ioremap: | |
3885 | release_mem_region(memstart, memlen); | |
3886 | ||
74c71ebd | 3887 | dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), ret); |
b92a78e5 RG |
3888 | return ret; |
3889 | } | |
3890 | ||
3891 | static void oxu_remove(struct platform_device *pdev, struct usb_hcd *hcd) | |
3892 | { | |
3893 | usb_remove_hcd(hcd); | |
3894 | usb_put_hcd(hcd); | |
3895 | } | |
3896 | ||
3897 | static int oxu_drv_remove(struct platform_device *pdev) | |
3898 | { | |
3899 | struct oxu_info *info = platform_get_drvdata(pdev); | |
3900 | unsigned long memstart = info->hcd[0]->rsrc_start, | |
3901 | memlen = info->hcd[0]->rsrc_len; | |
3902 | void *base = info->hcd[0]->regs; | |
3903 | ||
3904 | oxu_remove(pdev, info->hcd[0]); | |
3905 | oxu_remove(pdev, info->hcd[1]); | |
3906 | ||
3907 | iounmap(base); | |
3908 | release_mem_region(memstart, memlen); | |
3909 | ||
3910 | kfree(info); | |
3911 | platform_set_drvdata(pdev, NULL); | |
3912 | ||
3913 | return 0; | |
3914 | } | |
3915 | ||
3916 | static void oxu_drv_shutdown(struct platform_device *pdev) | |
3917 | { | |
3918 | oxu_drv_remove(pdev); | |
3919 | } | |
3920 | ||
3921 | #if 0 | |
3922 | /* FIXME: TODO */ | |
3923 | static int oxu_drv_suspend(struct device *dev) | |
3924 | { | |
3925 | struct platform_device *pdev = to_platform_device(dev); | |
3926 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
3927 | ||
3928 | return 0; | |
3929 | } | |
3930 | ||
3931 | static int oxu_drv_resume(struct device *dev) | |
3932 | { | |
3933 | struct platform_device *pdev = to_platform_device(dev); | |
3934 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
3935 | ||
3936 | return 0; | |
3937 | } | |
3938 | #else | |
3939 | #define oxu_drv_suspend NULL | |
3940 | #define oxu_drv_resume NULL | |
3941 | #endif | |
3942 | ||
3943 | static struct platform_driver oxu_driver = { | |
3944 | .probe = oxu_drv_probe, | |
3945 | .remove = oxu_drv_remove, | |
3946 | .shutdown = oxu_drv_shutdown, | |
3947 | .suspend = oxu_drv_suspend, | |
3948 | .resume = oxu_drv_resume, | |
3949 | .driver = { | |
3950 | .name = "oxu210hp-hcd", | |
3951 | .bus = &platform_bus_type | |
3952 | } | |
3953 | }; | |
3954 | ||
cc27c96c | 3955 | module_platform_driver(oxu_driver); |
b92a78e5 RG |
3956 | |
3957 | MODULE_DESCRIPTION("Oxford OXU210HP HCD driver - ver. " DRIVER_VERSION); | |
3958 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
3959 | MODULE_LICENSE("GPL"); |