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