]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/host/isp1760-hcd.c
usb/isp1760: Simpler queue head list code.
[mirror_ubuntu-artful-kernel.git] / drivers / usb / host / isp1760-hcd.c
CommitLineData
db11e47d
SS
1/*
2 * Driver for the NXP ISP1760 chip
3 *
4 * However, the code might contain some bugs. What doesn't work for sure is:
5 * - ISO
6 * - OTG
7 e The interrupt line is configured as active low, level.
8 *
9 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
10 *
71a9f9d2
AB
11 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
12 *
db11e47d
SS
13 */
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/usb.h>
27729aad 19#include <linux/usb/hcd.h>
db11e47d
SS
20#include <linux/debugfs.h>
21#include <linux/uaccess.h>
22#include <linux/io.h>
db8516f6 23#include <linux/mm.h>
6d50c60e 24#include <linux/timer.h>
db11e47d 25#include <asm/unaligned.h>
db8516f6 26#include <asm/cacheflush.h>
3a7655fc 27#include <linux/gpio.h>
db11e47d 28
db11e47d
SS
29#include "isp1760-hcd.h"
30
31static struct kmem_cache *qtd_cachep;
32static struct kmem_cache *qh_cachep;
71a9f9d2 33static struct kmem_cache *urb_listitem_cachep;
db11e47d 34
e08f6a27
AB
35enum queue_head_types {
36 QH_CONTROL,
37 QH_BULK,
38 QH_INTERRUPT,
39 QH_END
40};
41
db11e47d
SS
42struct isp1760_hcd {
43 u32 hcs_params;
44 spinlock_t lock;
71a9f9d2 45 struct slotinfo atl_slots[32];
d05b6ec0 46 int atl_done_map;
71a9f9d2 47 struct slotinfo int_slots[32];
d05b6ec0 48 int int_done_map;
db11e47d 49 struct memory_chunk memory_pool[BLOCKS];
e08f6a27 50 struct list_head qh_list[QH_END];
db11e47d
SS
51
52 /* periodic schedule support */
53#define DEFAULT_I_TDPS 1024
54 unsigned periodic_size;
55 unsigned i_thresh;
56 unsigned long reset_done;
57 unsigned long next_statechange;
3faefc88 58 unsigned int devflags;
3a7655fc
JF
59
60 int rst_gpio;
db11e47d
SS
61};
62
63static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
64{
65 return (struct isp1760_hcd *) (hcd->hcd_priv);
66}
db11e47d
SS
67
68/* Section 2.2 Host Controller Capability Registers */
69#define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
70#define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
71#define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
72#define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
73#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
74#define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
75#define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
76
77/* Section 2.3 Host Controller Operational Registers */
78#define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
79#define CMD_RESET (1<<1) /* reset HC not bus */
80#define CMD_RUN (1<<0) /* start/stop HC */
81#define STS_PCD (1<<2) /* port change detect */
82#define FLAG_CF (1<<0) /* true: we'll support "high speed" */
83
84#define PORT_OWNER (1<<13) /* true: companion hc owns this port */
85#define PORT_POWER (1<<12) /* true: has power (see PPC) */
86#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
87#define PORT_RESET (1<<8) /* reset port */
88#define PORT_SUSPEND (1<<7) /* suspend port */
89#define PORT_RESUME (1<<6) /* resume it */
90#define PORT_PE (1<<2) /* port enable */
91#define PORT_CSC (1<<1) /* connect status change */
92#define PORT_CONNECT (1<<0) /* device connected */
93#define PORT_RWC_BITS (PORT_CSC)
94
95struct isp1760_qtd {
db11e47d 96 u8 packet_type;
db11e47d 97 void *data_buffer;
a041d8e4
AB
98 u32 payload_addr;
99
db11e47d
SS
100 /* the rest is HCD-private */
101 struct list_head qtd_list;
102 struct urb *urb;
103 size_t length;
71a9f9d2
AB
104 size_t actual_length;
105
106 /* QTD_ENQUEUED: waiting for transfer (inactive) */
107 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
108 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
109 interrupt handler may touch this qtd! */
110 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
111 /* QTD_RETIRE: transfer error/abort qtd */
112#define QTD_ENQUEUED 0
113#define QTD_PAYLOAD_ALLOC 1
114#define QTD_XFER_STARTED 2
115#define QTD_XFER_COMPLETE 3
116#define QTD_RETIRE 4
db11e47d 117 u32 status;
db11e47d
SS
118};
119
71a9f9d2 120/* Queue head, one for each active endpoint */
db11e47d 121struct isp1760_qh {
71a9f9d2 122 struct list_head qh_list;
db11e47d 123 struct list_head qtd_list;
db11e47d
SS
124 u32 toggle;
125 u32 ping;
71a9f9d2 126 int slot;
74ad6029 127 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
71a9f9d2
AB
128};
129
130struct urb_listitem {
131 struct list_head urb_list;
132 struct urb *urb;
db11e47d
SS
133};
134
bedc0c31
AB
135/*
136 * Access functions for isp176x registers (addresses 0..0x03FF).
137 */
138static u32 reg_read32(void __iomem *base, u32 reg)
db11e47d 139{
bedc0c31 140 return readl(base + reg);
db11e47d
SS
141}
142
bedc0c31 143static void reg_write32(void __iomem *base, u32 reg, u32 val)
db11e47d 144{
bedc0c31 145 writel(val, base + reg);
db11e47d
SS
146}
147
148/*
bedc0c31
AB
149 * Access functions for isp176x memory (offset >= 0x0400).
150 *
151 * bank_reads8() reads memory locations prefetched by an earlier write to
152 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
153 * bank optimizations, you should use the more generic mem_reads8() below.
154 *
155 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
156 * below.
157 *
158 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
db11e47d
SS
159 * doesn't quite work because some people have to enforce 32-bit access
160 */
bedc0c31
AB
161static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
162 __u32 *dst, u32 bytes)
db11e47d 163{
bedc0c31 164 __u32 __iomem *src;
db11e47d 165 u32 val;
bedc0c31
AB
166 __u8 *src_byteptr;
167 __u8 *dst_byteptr;
db11e47d 168
bedc0c31 169 src = src_base + (bank_addr | src_offset);
db11e47d 170
bedc0c31
AB
171 if (src_offset < PAYLOAD_OFFSET) {
172 while (bytes >= 4) {
173 *dst = le32_to_cpu(__raw_readl(src));
174 bytes -= 4;
175 src++;
176 dst++;
177 }
178 } else {
179 while (bytes >= 4) {
180 *dst = __raw_readl(src);
181 bytes -= 4;
182 src++;
183 dst++;
184 }
db11e47d
SS
185 }
186
bedc0c31 187 if (!bytes)
db11e47d
SS
188 return;
189
190 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
191 * allocated.
192 */
bedc0c31
AB
193 if (src_offset < PAYLOAD_OFFSET)
194 val = le32_to_cpu(__raw_readl(src));
195 else
196 val = __raw_readl(src);
197
198 dst_byteptr = (void *) dst;
199 src_byteptr = (void *) &val;
200 while (bytes > 0) {
201 *dst_byteptr = *src_byteptr;
202 dst_byteptr++;
203 src_byteptr++;
204 bytes--;
db11e47d
SS
205 }
206}
207
bedc0c31
AB
208static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
209 u32 bytes)
db11e47d 210{
bedc0c31
AB
211 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
212 ndelay(90);
213 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
214}
215
216static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
217 __u32 const *src, u32 bytes)
218{
219 __u32 __iomem *dst;
220
221 dst = dst_base + dst_offset;
222
223 if (dst_offset < PAYLOAD_OFFSET) {
224 while (bytes >= 4) {
225 __raw_writel(cpu_to_le32(*src), dst);
226 bytes -= 4;
227 src++;
228 dst++;
229 }
230 } else {
231 while (bytes >= 4) {
232 __raw_writel(*src, dst);
233 bytes -= 4;
234 src++;
235 dst++;
236 }
db11e47d
SS
237 }
238
bedc0c31 239 if (!bytes)
db11e47d 240 return;
bedc0c31
AB
241 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
242 * extra bytes should not be read by the HW.
db11e47d
SS
243 */
244
bedc0c31
AB
245 if (dst_offset < PAYLOAD_OFFSET)
246 __raw_writel(cpu_to_le32(*src), dst);
247 else
248 __raw_writel(*src, dst);
db11e47d
SS
249}
250
bedc0c31
AB
251/*
252 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
253 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
254 */
255static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
256 struct ptd *ptd)
257{
258 reg_write32(base, HC_MEMORY_REG,
259 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
260 ndelay(90);
261 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
262 (void *) ptd, sizeof(*ptd));
263}
264
265static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
266 struct ptd *ptd)
267{
268 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
269 &ptd->dw1, 7*sizeof(ptd->dw1));
270 /* Make sure dw0 gets written last (after other dw's and after payload)
271 since it contains the enable bit */
272 wmb();
273 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
274 sizeof(ptd->dw0));
275}
276
277
db11e47d
SS
278/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
279static void init_memory(struct isp1760_hcd *priv)
280{
a041d8e4
AB
281 int i, curr;
282 u32 payload_addr;
db11e47d 283
a041d8e4 284 payload_addr = PAYLOAD_OFFSET;
db11e47d 285 for (i = 0; i < BLOCK_1_NUM; i++) {
a041d8e4 286 priv->memory_pool[i].start = payload_addr;
db11e47d
SS
287 priv->memory_pool[i].size = BLOCK_1_SIZE;
288 priv->memory_pool[i].free = 1;
a041d8e4 289 payload_addr += priv->memory_pool[i].size;
db11e47d
SS
290 }
291
a041d8e4
AB
292 curr = i;
293 for (i = 0; i < BLOCK_2_NUM; i++) {
294 priv->memory_pool[curr + i].start = payload_addr;
295 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
296 priv->memory_pool[curr + i].free = 1;
297 payload_addr += priv->memory_pool[curr + i].size;
db11e47d
SS
298 }
299
a041d8e4
AB
300 curr = i;
301 for (i = 0; i < BLOCK_3_NUM; i++) {
302 priv->memory_pool[curr + i].start = payload_addr;
303 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
304 priv->memory_pool[curr + i].free = 1;
305 payload_addr += priv->memory_pool[curr + i].size;
db11e47d
SS
306 }
307
34537731 308 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
db11e47d
SS
309}
310
6bda21bc 311static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
db11e47d 312{
6bda21bc 313 struct isp1760_hcd *priv = hcd_to_priv(hcd);
db11e47d
SS
314 int i;
315
34537731 316 WARN_ON(qtd->payload_addr);
a041d8e4
AB
317
318 if (!qtd->length)
319 return;
db11e47d
SS
320
321 for (i = 0; i < BLOCKS; i++) {
a041d8e4 322 if (priv->memory_pool[i].size >= qtd->length &&
db11e47d 323 priv->memory_pool[i].free) {
db11e47d 324 priv->memory_pool[i].free = 0;
a041d8e4
AB
325 qtd->payload_addr = priv->memory_pool[i].start;
326 return;
db11e47d
SS
327 }
328 }
db11e47d
SS
329}
330
6bda21bc 331static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
db11e47d 332{
6bda21bc 333 struct isp1760_hcd *priv = hcd_to_priv(hcd);
db11e47d
SS
334 int i;
335
a041d8e4 336 if (!qtd->payload_addr)
db11e47d
SS
337 return;
338
339 for (i = 0; i < BLOCKS; i++) {
a041d8e4 340 if (priv->memory_pool[i].start == qtd->payload_addr) {
34537731 341 WARN_ON(priv->memory_pool[i].free);
db11e47d 342 priv->memory_pool[i].free = 1;
a041d8e4
AB
343 qtd->payload_addr = 0;
344 return;
db11e47d
SS
345 }
346 }
347
6bda21bc
AB
348 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
349 __func__, qtd->payload_addr);
71a9f9d2
AB
350 WARN_ON(1);
351 qtd->payload_addr = 0;
db11e47d
SS
352}
353
bedc0c31 354static int handshake(struct usb_hcd *hcd, u32 reg,
db11e47d
SS
355 u32 mask, u32 done, int usec)
356{
357 u32 result;
358
359 do {
bedc0c31 360 result = reg_read32(hcd->regs, reg);
db11e47d
SS
361 if (result == ~0)
362 return -ENODEV;
363 result &= mask;
364 if (result == done)
365 return 0;
366 udelay(1);
367 usec--;
368 } while (usec > 0);
369 return -ETIMEDOUT;
370}
371
372/* reset a non-running (STS_HALT == 1) controller */
6bda21bc 373static int ehci_reset(struct usb_hcd *hcd)
db11e47d
SS
374{
375 int retval;
6bda21bc
AB
376 struct isp1760_hcd *priv = hcd_to_priv(hcd);
377
bedc0c31 378 u32 command = reg_read32(hcd->regs, HC_USBCMD);
db11e47d
SS
379
380 command |= CMD_RESET;
bedc0c31 381 reg_write32(hcd->regs, HC_USBCMD, command);
db11e47d
SS
382 hcd->state = HC_STATE_HALT;
383 priv->next_statechange = jiffies;
bedc0c31 384 retval = handshake(hcd, HC_USBCMD,
db11e47d
SS
385 CMD_RESET, 0, 250 * 1000);
386 return retval;
387}
388
71a9f9d2 389static struct isp1760_qh *qh_alloc(gfp_t flags)
db11e47d
SS
390{
391 struct isp1760_qh *qh;
392
393 qh = kmem_cache_zalloc(qh_cachep, flags);
394 if (!qh)
71a9f9d2 395 return NULL;
db11e47d 396
71a9f9d2 397 INIT_LIST_HEAD(&qh->qh_list);
db11e47d 398 INIT_LIST_HEAD(&qh->qtd_list);
71a9f9d2
AB
399 qh->slot = -1;
400
db11e47d
SS
401 return qh;
402}
403
71a9f9d2
AB
404static void qh_free(struct isp1760_qh *qh)
405{
406 WARN_ON(!list_empty(&qh->qtd_list));
407 WARN_ON(qh->slot > -1);
408 kmem_cache_free(qh_cachep, qh);
409}
db11e47d
SS
410
411/* one-time init, only for memory state */
412static int priv_init(struct usb_hcd *hcd)
413{
414 struct isp1760_hcd *priv = hcd_to_priv(hcd);
415 u32 hcc_params;
e08f6a27 416 int i;
db11e47d
SS
417
418 spin_lock_init(&priv->lock);
419
e08f6a27
AB
420 for (i = 0; i < QH_END; i++)
421 INIT_LIST_HEAD(&priv->qh_list[i]);
71a9f9d2 422
db11e47d
SS
423 /*
424 * hw default: 1K periodic list heads, one per frame.
425 * periodic_size can shrink by USBCMD update if hcc_params allows.
426 */
427 priv->periodic_size = DEFAULT_I_TDPS;
428
429 /* controllers may cache some of the periodic schedule ... */
bedc0c31 430 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
db11e47d
SS
431 /* full frame cache */
432 if (HCC_ISOC_CACHE(hcc_params))
433 priv->i_thresh = 8;
434 else /* N microframes cached */
435 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
436
437 return 0;
438}
439
440static int isp1760_hc_setup(struct usb_hcd *hcd)
441{
442 struct isp1760_hcd *priv = hcd_to_priv(hcd);
443 int result;
3faefc88
NC
444 u32 scratch, hwmode;
445
3a7655fc
JF
446 /* low-level chip reset */
447 if (gpio_is_valid(priv->rst_gpio)) {
448 unsigned int rst_lvl;
449
450 rst_lvl = (priv->devflags &
451 ISP1760_FLAG_RESET_ACTIVE_HIGH) ? 1 : 0;
452
453 gpio_set_value(priv->rst_gpio, rst_lvl);
454 mdelay(50);
455 gpio_set_value(priv->rst_gpio, !rst_lvl);
456 }
457
3faefc88
NC
458 /* Setup HW Mode Control: This assumes a level active-low interrupt */
459 hwmode = HW_DATA_BUS_32BIT;
460
461 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
462 hwmode &= ~HW_DATA_BUS_32BIT;
463 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
464 hwmode |= HW_ANA_DIGI_OC;
465 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
466 hwmode |= HW_DACK_POL_HIGH;
467 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
468 hwmode |= HW_DREQ_POL_HIGH;
9da69c60
MH
469 if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
470 hwmode |= HW_INTR_HIGH_ACT;
471 if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
472 hwmode |= HW_INTR_EDGE_TRIG;
3faefc88
NC
473
474 /*
475 * We have to set this first in case we're in 16-bit mode.
476 * Write it twice to ensure correct upper bits if switching
477 * to 16-bit mode.
478 */
bedc0c31
AB
479 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
480 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
db11e47d 481
bedc0c31 482 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
3faefc88 483 /* Change bus pattern */
bedc0c31
AB
484 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
485 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
db11e47d 486 if (scratch != 0xdeadbabe) {
6bda21bc 487 dev_err(hcd->self.controller, "Scratch test failed.\n");
db11e47d
SS
488 return -ENODEV;
489 }
490
491 /* pre reset */
71a9f9d2
AB
492 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
493 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
494 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
495 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
db11e47d
SS
496
497 /* reset */
bedc0c31 498 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
db11e47d
SS
499 mdelay(100);
500
bedc0c31 501 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
db11e47d
SS
502 mdelay(100);
503
6bda21bc 504 result = ehci_reset(hcd);
db11e47d
SS
505 if (result)
506 return result;
507
508 /* Step 11 passed */
509
6bda21bc 510 dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
3faefc88
NC
511 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
512 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
513 "analog" : "digital");
db11e47d
SS
514
515 /* ATL reset */
bedc0c31 516 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
db11e47d 517 mdelay(10);
bedc0c31 518 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
db11e47d 519
bedc0c31 520 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
3faefc88
NC
521
522 /*
523 * PORT 1 Control register of the ISP1760 is the OTG control
42c65396
TH
524 * register on ISP1761. Since there is no OTG or device controller
525 * support in this driver, we use port 1 as a "normal" USB host port on
526 * both chips.
3faefc88 527 */
bedc0c31 528 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
42c65396 529 mdelay(10);
db11e47d 530
bedc0c31 531 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
db11e47d
SS
532
533 return priv_init(hcd);
534}
535
db11e47d
SS
536static u32 base_to_chip(u32 base)
537{
538 return ((base - 0x400) >> 3);
539}
540
7adc14b1
AB
541static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
542{
543 struct urb *urb;
544
545 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
546 return 1;
547
548 urb = qtd->urb;
549 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
550 return (qtd->urb != urb);
551}
552
71a9f9d2
AB
553/* magic numbers that can affect system performance */
554#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
555#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
556#define EHCI_TUNE_RL_TT 0
557#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
558#define EHCI_TUNE_MULT_TT 1
559#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
560
561static void create_ptd_atl(struct isp1760_qh *qh,
a041d8e4 562 struct isp1760_qtd *qtd, struct ptd *ptd)
db11e47d 563{
db11e47d
SS
564 u32 maxpacket;
565 u32 multi;
db11e47d
SS
566 u32 rl = RL_COUNTER;
567 u32 nak = NAK_COUNTER;
568
bedc0c31
AB
569 memset(ptd, 0, sizeof(*ptd));
570
db11e47d 571 /* according to 3.6.2, max packet len can not be > 0x400 */
a041d8e4
AB
572 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
573 usb_pipeout(qtd->urb->pipe));
db11e47d
SS
574 multi = 1 + ((maxpacket >> 11) & 0x3);
575 maxpacket &= 0x7ff;
576
577 /* DW0 */
71a9f9d2
AB
578 ptd->dw0 = DW0_VALID_BIT;
579 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
580 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
581 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
db11e47d
SS
582
583 /* DW1 */
a041d8e4 584 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
71a9f9d2
AB
585 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
586 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
db11e47d 587
a041d8e4 588 if (usb_pipebulk(qtd->urb->pipe))
71a9f9d2 589 ptd->dw1 |= DW1_TRANS_BULK;
a041d8e4 590 else if (usb_pipeint(qtd->urb->pipe))
71a9f9d2 591 ptd->dw1 |= DW1_TRANS_INT;
db11e47d 592
a041d8e4 593 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
db11e47d
SS
594 /* split transaction */
595
71a9f9d2 596 ptd->dw1 |= DW1_TRANS_SPLIT;
a041d8e4 597 if (qtd->urb->dev->speed == USB_SPEED_LOW)
71a9f9d2 598 ptd->dw1 |= DW1_SE_USB_LOSPEED;
db11e47d 599
71a9f9d2
AB
600 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
601 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
db11e47d
SS
602
603 /* SE bit for Split INT transfers */
a041d8e4
AB
604 if (usb_pipeint(qtd->urb->pipe) &&
605 (qtd->urb->dev->speed == USB_SPEED_LOW))
bedc0c31 606 ptd->dw1 |= 2 << 16;
db11e47d 607
db11e47d
SS
608 rl = 0;
609 nak = 0;
610 } else {
71a9f9d2 611 ptd->dw0 |= TO_DW0_MULTI(multi);
a041d8e4
AB
612 if (usb_pipecontrol(qtd->urb->pipe) ||
613 usb_pipebulk(qtd->urb->pipe))
71a9f9d2 614 ptd->dw3 |= TO_DW3_PING(qh->ping);
db11e47d
SS
615 }
616 /* DW2 */
bedc0c31 617 ptd->dw2 = 0;
71a9f9d2
AB
618 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
619 ptd->dw2 |= TO_DW2_RL(rl);
db11e47d
SS
620
621 /* DW3 */
71a9f9d2
AB
622 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
623 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
7adc14b1
AB
624 if (usb_pipecontrol(qtd->urb->pipe)) {
625 if (qtd->data_buffer == qtd->urb->setup_packet)
71a9f9d2 626 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
7adc14b1 627 else if (last_qtd_of_urb(qtd, qh))
71a9f9d2 628 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
7adc14b1 629 }
db11e47d 630
71a9f9d2 631 ptd->dw3 |= DW3_ACTIVE_BIT;
db11e47d 632 /* Cerr */
71a9f9d2 633 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
db11e47d
SS
634}
635
6bda21bc 636static void transform_add_int(struct isp1760_qh *qh,
a041d8e4 637 struct isp1760_qtd *qtd, struct ptd *ptd)
db11e47d 638{
65f1b525 639 u32 usof;
db11e47d
SS
640 u32 period;
641
65f1b525
AB
642 /*
643 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
644 * the algorithm from the original Philips driver code, which was
645 * pretty much used in this driver before as well, is quite horrendous
646 * and, i believe, incorrect. The code below follows the datasheet and
647 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
648 * more reliable this way (fingers crossed...).
649 */
db11e47d 650
65f1b525
AB
651 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
652 /* urb->interval is in units of microframes (1/8 ms) */
653 period = qtd->urb->interval >> 3;
654
655 if (qtd->urb->interval > 4)
656 usof = 0x01; /* One bit set =>
657 interval 1 ms * uFrame-match */
658 else if (qtd->urb->interval > 2)
659 usof = 0x22; /* Two bits set => interval 1/2 ms */
660 else if (qtd->urb->interval > 1)
661 usof = 0x55; /* Four bits set => interval 1/4 ms */
db11e47d 662 else
65f1b525 663 usof = 0xff; /* All bits set => interval 1/8 ms */
db11e47d 664 } else {
65f1b525
AB
665 /* urb->interval is in units of frames (1 ms) */
666 period = qtd->urb->interval;
667 usof = 0x0f; /* Execute Start Split on any of the
668 four first uFrames */
669
670 /*
671 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
672 * complete split needs to be sent. Valid only for IN." Also,
673 * "All bits can be set to one for every transfer." (p 82,
674 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
675 * that number come from? 0xff seems to work fine...
676 */
677 /* ptd->dw5 = 0x1c; */
678 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
db11e47d
SS
679 }
680
65f1b525
AB
681 period = period >> 1;/* Ensure equal or shorter period than requested */
682 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
683
bedc0c31
AB
684 ptd->dw2 |= period;
685 ptd->dw4 = usof;
db11e47d
SS
686}
687
71a9f9d2 688static void create_ptd_int(struct isp1760_qh *qh,
a041d8e4 689 struct isp1760_qtd *qtd, struct ptd *ptd)
db11e47d 690{
71a9f9d2 691 create_ptd_atl(qh, qtd, ptd);
6bda21bc 692 transform_add_int(qh, qtd, ptd);
db11e47d
SS
693}
694
6bda21bc 695static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
db11e47d
SS
696__releases(priv->lock)
697__acquires(priv->lock)
698{
6bda21bc
AB
699 struct isp1760_hcd *priv = hcd_to_priv(hcd);
700
db11e47d 701 if (!urb->unlinked) {
6bda21bc
AB
702 if (urb->status == -EINPROGRESS)
703 urb->status = 0;
db11e47d
SS
704 }
705
db8516f6
CM
706 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
707 void *ptr;
708 for (ptr = urb->transfer_buffer;
709 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
710 ptr += PAGE_SIZE)
711 flush_dcache_page(virt_to_page(ptr));
712 }
713
db11e47d 714 /* complete() can reenter this HCD */
6bda21bc 715 usb_hcd_unlink_urb_from_ep(hcd, urb);
db11e47d 716 spin_unlock(&priv->lock);
6bda21bc 717 usb_hcd_giveback_urb(hcd, urb, urb->status);
db11e47d
SS
718 spin_lock(&priv->lock);
719}
720
34537731
AB
721static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
722 u8 packet_type)
db11e47d 723{
34537731
AB
724 struct isp1760_qtd *qtd;
725
726 qtd = kmem_cache_zalloc(qtd_cachep, flags);
727 if (!qtd)
728 return NULL;
729
730 INIT_LIST_HEAD(&qtd->qtd_list);
731 qtd->urb = urb;
732 qtd->packet_type = packet_type;
71a9f9d2
AB
733 qtd->status = QTD_ENQUEUED;
734 qtd->actual_length = 0;
34537731
AB
735
736 return qtd;
737}
738
739static void qtd_free(struct isp1760_qtd *qtd)
740{
741 WARN_ON(qtd->payload_addr);
db11e47d
SS
742 kmem_cache_free(qtd_cachep, qtd);
743}
744
71a9f9d2
AB
745static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
746 struct slotinfo *slots, struct isp1760_qtd *qtd,
747 struct isp1760_qh *qh, struct ptd *ptd)
db11e47d 748{
71a9f9d2 749 struct isp1760_hcd *priv = hcd_to_priv(hcd);
d05b6ec0
AB
750 int skip_map;
751
71a9f9d2
AB
752 WARN_ON((slot < 0) || (slot > 31));
753 WARN_ON(qtd->length && !qtd->payload_addr);
754 WARN_ON(slots[slot].qtd);
755 WARN_ON(slots[slot].qh);
756 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
757
d05b6ec0
AB
758 /* Make sure done map has not triggered from some unlinked transfer */
759 if (ptd_offset == ATL_PTD_OFFSET) {
760 priv->atl_done_map |= reg_read32(hcd->regs,
761 HC_ATL_PTD_DONEMAP_REG);
6477acc0
AB
762 priv->atl_done_map &= ~(1 << slot);
763 } else {
764 priv->int_done_map |= reg_read32(hcd->regs,
765 HC_INT_PTD_DONEMAP_REG);
766 priv->int_done_map &= ~(1 << slot);
767 }
d05b6ec0 768
6477acc0
AB
769 qh->slot = slot;
770 qtd->status = QTD_XFER_STARTED;
771 slots[slot].timestamp = jiffies;
772 slots[slot].qtd = qtd;
773 slots[slot].qh = qh;
774 ptd_write(hcd->regs, ptd_offset, slot, ptd);
775
776 if (ptd_offset == ATL_PTD_OFFSET) {
d05b6ec0
AB
777 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
778 skip_map &= ~(1 << qh->slot);
779 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
780 } else {
d05b6ec0
AB
781 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
782 skip_map &= ~(1 << qh->slot);
783 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
784 }
db11e47d
SS
785}
786
71a9f9d2 787static int is_short_bulk(struct isp1760_qtd *qtd)
db11e47d 788{
71a9f9d2
AB
789 return (usb_pipebulk(qtd->urb->pipe) &&
790 (qtd->actual_length < qtd->length));
db11e47d
SS
791}
792
71a9f9d2
AB
793static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
794 struct list_head *urb_list)
db11e47d 795{
71a9f9d2
AB
796 int last_qtd;
797 struct isp1760_qtd *qtd, *qtd_next;
798 struct urb_listitem *urb_listitem;
db11e47d 799
71a9f9d2
AB
800 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
801 if (qtd->status < QTD_XFER_COMPLETE)
802 break;
db11e47d 803
38679b72 804 last_qtd = last_qtd_of_urb(qtd, qh);
71a9f9d2
AB
805
806 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
807 qtd_next->status = QTD_RETIRE;
808
809 if (qtd->status == QTD_XFER_COMPLETE) {
810 if (qtd->actual_length) {
811 switch (qtd->packet_type) {
812 case IN_PID:
813 mem_reads8(hcd->regs, qtd->payload_addr,
814 qtd->data_buffer,
815 qtd->actual_length);
816 /* Fall through (?) */
817 case OUT_PID:
818 qtd->urb->actual_length +=
819 qtd->actual_length;
820 /* Fall through ... */
821 case SETUP_PID:
822 break;
823 }
824 }
db11e47d 825
71a9f9d2
AB
826 if (is_short_bulk(qtd)) {
827 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
828 qtd->urb->status = -EREMOTEIO;
829 if (!last_qtd)
830 qtd_next->status = QTD_RETIRE;
831 }
832 }
db11e47d 833
71a9f9d2
AB
834 if (qtd->payload_addr)
835 free_mem(hcd, qtd);
db11e47d 836
71a9f9d2
AB
837 if (last_qtd) {
838 if ((qtd->status == QTD_RETIRE) &&
839 (qtd->urb->status == -EINPROGRESS))
840 qtd->urb->status = -EPIPE;
841 /* Defer calling of urb_done() since it releases lock */
842 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
843 GFP_ATOMIC);
844 if (unlikely(!urb_listitem))
38679b72 845 break; /* Try again on next call */
71a9f9d2
AB
846 urb_listitem->urb = qtd->urb;
847 list_add_tail(&urb_listitem->urb_list, urb_list);
848 }
847ed3e8 849
71a9f9d2
AB
850 list_del(&qtd->qtd_list);
851 qtd_free(qtd);
852 }
853}
3f02a957 854
71a9f9d2
AB
855#define ENQUEUE_DEPTH 2
856static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
857{
858 struct isp1760_hcd *priv = hcd_to_priv(hcd);
859 int ptd_offset;
860 struct slotinfo *slots;
861 int curr_slot, free_slot;
862 int n;
863 struct ptd ptd;
864 struct isp1760_qtd *qtd;
db11e47d 865
71a9f9d2
AB
866 if (unlikely(list_empty(&qh->qtd_list))) {
867 WARN_ON(1);
868 return;
869 }
db11e47d 870
74ad6029
AB
871 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
872 if (qh->tt_buffer_dirty)
873 return;
874
71a9f9d2
AB
875 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
876 qtd_list)->urb->pipe)) {
877 ptd_offset = INT_PTD_OFFSET;
878 slots = priv->int_slots;
879 } else {
880 ptd_offset = ATL_PTD_OFFSET;
881 slots = priv->atl_slots;
882 }
db11e47d 883
71a9f9d2
AB
884 free_slot = -1;
885 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
886 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
887 free_slot = curr_slot;
888 if (slots[curr_slot].qh == qh)
889 break;
890 }
db11e47d 891
71a9f9d2
AB
892 n = 0;
893 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
894 if (qtd->status == QTD_ENQUEUED) {
895 WARN_ON(qtd->payload_addr);
896 alloc_mem(hcd, qtd);
897 if ((qtd->length) && (!qtd->payload_addr))
898 break;
db11e47d 899
71a9f9d2
AB
900 if ((qtd->length) &&
901 ((qtd->packet_type == SETUP_PID) ||
902 (qtd->packet_type == OUT_PID))) {
903 mem_writes8(hcd->regs, qtd->payload_addr,
904 qtd->data_buffer, qtd->length);
905 }
db11e47d 906
71a9f9d2 907 qtd->status = QTD_PAYLOAD_ALLOC;
db11e47d
SS
908 }
909
71a9f9d2
AB
910 if (qtd->status == QTD_PAYLOAD_ALLOC) {
911/*
912 if ((curr_slot > 31) && (free_slot == -1))
913 dev_dbg(hcd->self.controller, "%s: No slot "
914 "available for transfer\n", __func__);
915*/
916 /* Start xfer for this endpoint if not already done */
917 if ((curr_slot > 31) && (free_slot > -1)) {
918 if (usb_pipeint(qtd->urb->pipe))
919 create_ptd_int(qh, qtd, &ptd);
920 else
921 create_ptd_atl(qh, qtd, &ptd);
922
923 start_bus_transfer(hcd, ptd_offset, free_slot,
924 slots, qtd, qh, &ptd);
925 curr_slot = free_slot;
926 }
db11e47d 927
71a9f9d2
AB
928 n++;
929 if (n >= ENQUEUE_DEPTH)
930 break;
931 }
932 }
933}
db11e47d 934
71a9f9d2
AB
935void schedule_ptds(struct usb_hcd *hcd)
936{
937 struct isp1760_hcd *priv;
938 struct isp1760_qh *qh, *qh_next;
939 struct list_head *ep_queue;
940 struct usb_host_endpoint *ep;
941 LIST_HEAD(urb_list);
942 struct urb_listitem *urb_listitem, *urb_listitem_next;
e08f6a27 943 int i;
71a9f9d2
AB
944
945 if (!hcd) {
946 WARN_ON(1);
947 return;
948 }
db11e47d 949
71a9f9d2 950 priv = hcd_to_priv(hcd);
db11e47d 951
71a9f9d2
AB
952 /*
953 * check finished/retired xfers, transfer payloads, call urb_done()
954 */
e08f6a27
AB
955 for (i = 0; i < QH_END; i++) {
956 ep_queue = &priv->qh_list[i];
71a9f9d2
AB
957 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
958 ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
959 qtd_list)->urb->ep;
960 collect_qtds(hcd, qh, &urb_list);
961 if (list_empty(&qh->qtd_list)) {
962 list_del(&qh->qh_list);
963 if (ep->hcpriv == NULL) {
964 /* Endpoint has been disabled, so we
965 can free the associated queue head. */
966 qh_free(qh);
967 }
db11e47d
SS
968 }
969 }
71a9f9d2 970 }
db11e47d 971
71a9f9d2
AB
972 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
973 urb_list) {
974 isp1760_urb_done(hcd, urb_listitem->urb);
975 kmem_cache_free(urb_listitem_cachep, urb_listitem);
976 }
db11e47d 977
71a9f9d2
AB
978 /*
979 * Schedule packets for transfer.
980 *
981 * According to USB2.0 specification:
982 *
983 * 1st prio: interrupt xfers, up to 80 % of bandwidth
984 * 2nd prio: control xfers
985 * 3rd prio: bulk xfers
986 *
987 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
988 * is very unclear on how to prioritize traffic):
989 *
990 * 1) Enqueue any queued control transfers, as long as payload chip mem
991 * and PTD ATL slots are available.
992 * 2) Enqueue any queued INT transfers, as long as payload chip mem
993 * and PTD INT slots are available.
994 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
995 * and PTD ATL slots are available.
996 *
997 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
998 * conservation of chip mem and performance.
999 *
1000 * I'm sure this scheme could be improved upon!
1001 */
e08f6a27
AB
1002 for (i = 0; i < QH_END; i++) {
1003 ep_queue = &priv->qh_list[i];
71a9f9d2
AB
1004 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1005 enqueue_qtds(hcd, qh);
71a9f9d2
AB
1006 }
1007}
db11e47d 1008
71a9f9d2
AB
1009#define PTD_STATE_QTD_DONE 1
1010#define PTD_STATE_QTD_RELOAD 2
1011#define PTD_STATE_URB_RETIRE 3
db11e47d 1012
71a9f9d2
AB
1013static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1014 struct urb *urb)
1015{
1016 __dw dw4;
1017 int i;
db11e47d 1018
71a9f9d2
AB
1019 dw4 = ptd->dw4;
1020 dw4 >>= 8;
db11e47d 1021
71a9f9d2
AB
1022 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1023 need to handle these errors? Is it done in hardware? */
db11e47d 1024
71a9f9d2 1025 if (ptd->dw3 & DW3_HALT_BIT) {
db11e47d 1026
71a9f9d2 1027 urb->status = -EPROTO; /* Default unknown error */
db11e47d 1028
71a9f9d2
AB
1029 for (i = 0; i < 8; i++) {
1030 switch (dw4 & 0x7) {
1031 case INT_UNDERRUN:
1032 dev_dbg(hcd->self.controller, "%s: underrun "
1033 "during uFrame %d\n",
1034 __func__, i);
1035 urb->status = -ECOMM; /* Could not write data */
1036 break;
1037 case INT_EXACT:
1038 dev_dbg(hcd->self.controller, "%s: transaction "
1039 "error during uFrame %d\n",
1040 __func__, i);
1041 urb->status = -EPROTO; /* timeout, bad CRC, PID
1042 error etc. */
1043 break;
1044 case INT_BABBLE:
1045 dev_dbg(hcd->self.controller, "%s: babble "
1046 "error during uFrame %d\n",
1047 __func__, i);
1048 urb->status = -EOVERFLOW;
1049 break;
1050 }
1051 dw4 >>= 3;
1052 }
db11e47d 1053
71a9f9d2
AB
1054 return PTD_STATE_URB_RETIRE;
1055 }
db11e47d 1056
71a9f9d2
AB
1057 return PTD_STATE_QTD_DONE;
1058}
db11e47d 1059
71a9f9d2
AB
1060static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1061 struct urb *urb)
1062{
1063 WARN_ON(!ptd);
1064 if (ptd->dw3 & DW3_HALT_BIT) {
1065 if (ptd->dw3 & DW3_BABBLE_BIT)
1066 urb->status = -EOVERFLOW;
1067 else if (FROM_DW3_CERR(ptd->dw3))
1068 urb->status = -EPIPE; /* Stall */
1069 else if (ptd->dw3 & DW3_ERROR_BIT)
1070 urb->status = -EPROTO; /* XactErr */
1071 else
1072 urb->status = -EPROTO; /* Unknown */
1073/*
1074 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1075 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1076 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1077 __func__,
1078 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1079 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1080*/
1081 return PTD_STATE_URB_RETIRE;
1082 }
db11e47d 1083
71a9f9d2
AB
1084 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1085 /* Transfer Error, *but* active and no HALT -> reload */
1086 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1087 return PTD_STATE_QTD_RELOAD;
1088 }
db11e47d 1089
71a9f9d2
AB
1090 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1091 /*
1092 * NAKs are handled in HW by the chip. Usually if the
1093 * device is not able to send data fast enough.
1094 * This happens mostly on slower hardware.
1095 */
1096 return PTD_STATE_QTD_RELOAD;
db11e47d 1097 }
71a9f9d2
AB
1098
1099 return PTD_STATE_QTD_DONE;
db11e47d
SS
1100}
1101
6d50c60e 1102static void handle_done_ptds(struct usb_hcd *hcd)
db11e47d 1103{
bedc0c31 1104 struct isp1760_hcd *priv = hcd_to_priv(hcd);
db11e47d 1105 struct ptd ptd;
db11e47d 1106 struct isp1760_qh *qh;
71a9f9d2
AB
1107 int slot;
1108 int state;
1109 struct slotinfo *slots;
1110 u32 ptd_offset;
1111 struct isp1760_qtd *qtd;
1112 int modified;
6d50c60e 1113 int skip_map;
71a9f9d2 1114
6d50c60e
AB
1115 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1116 priv->int_done_map &= ~skip_map;
1117 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1118 priv->atl_done_map &= ~skip_map;
71a9f9d2 1119
6d50c60e 1120 modified = priv->int_done_map || priv->atl_done_map;
d05b6ec0
AB
1121
1122 while (priv->int_done_map || priv->atl_done_map) {
1123 if (priv->int_done_map) {
71a9f9d2 1124 /* INT ptd */
d05b6ec0
AB
1125 slot = __ffs(priv->int_done_map);
1126 priv->int_done_map &= ~(1 << slot);
71a9f9d2 1127 slots = priv->int_slots;
d05b6ec0
AB
1128 /* This should not trigger, and could be removed if
1129 noone have any problems with it triggering: */
1130 if (!slots[slot].qh) {
1131 WARN_ON(1);
71a9f9d2 1132 continue;
d05b6ec0 1133 }
71a9f9d2
AB
1134 ptd_offset = INT_PTD_OFFSET;
1135 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1136 state = check_int_transfer(hcd, &ptd,
1137 slots[slot].qtd->urb);
db11e47d 1138 } else {
71a9f9d2 1139 /* ATL ptd */
d05b6ec0
AB
1140 slot = __ffs(priv->atl_done_map);
1141 priv->atl_done_map &= ~(1 << slot);
71a9f9d2 1142 slots = priv->atl_slots;
d05b6ec0
AB
1143 /* This should not trigger, and could be removed if
1144 noone have any problems with it triggering: */
1145 if (!slots[slot].qh) {
1146 WARN_ON(1);
71a9f9d2 1147 continue;
d05b6ec0 1148 }
71a9f9d2
AB
1149 ptd_offset = ATL_PTD_OFFSET;
1150 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1151 state = check_atl_transfer(hcd, &ptd,
1152 slots[slot].qtd->urb);
db11e47d
SS
1153 }
1154
71a9f9d2
AB
1155 qtd = slots[slot].qtd;
1156 slots[slot].qtd = NULL;
1157 qh = slots[slot].qh;
1158 slots[slot].qh = NULL;
71a9f9d2
AB
1159 qh->slot = -1;
1160
1161 WARN_ON(qtd->status != QTD_XFER_STARTED);
1162
1163 switch (state) {
1164 case PTD_STATE_QTD_DONE:
1165 if ((usb_pipeint(qtd->urb->pipe)) &&
1166 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1167 qtd->actual_length =
1168 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1169 else
1170 qtd->actual_length =
1171 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
db11e47d 1172
71a9f9d2
AB
1173 qtd->status = QTD_XFER_COMPLETE;
1174 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1175 is_short_bulk(qtd))
1176 qtd = NULL;
1177 else
1178 qtd = list_entry(qtd->qtd_list.next,
1179 typeof(*qtd), qtd_list);
db11e47d 1180
71a9f9d2
AB
1181 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1182 qh->ping = FROM_DW3_PING(ptd.dw3);
1183 break;
db11e47d 1184
71a9f9d2
AB
1185 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1186 qtd->status = QTD_PAYLOAD_ALLOC;
1187 ptd.dw0 |= DW0_VALID_BIT;
1188 /* RL counter = ERR counter */
1189 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1190 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1191 ptd.dw3 &= ~TO_DW3_CERR(3);
1192 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1193 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1194 qh->ping = FROM_DW3_PING(ptd.dw3);
1195 break;
db11e47d 1196
71a9f9d2
AB
1197 case PTD_STATE_URB_RETIRE:
1198 qtd->status = QTD_RETIRE;
74ad6029
AB
1199 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1200 (qtd->urb->status != -EPIPE) &&
1201 (qtd->urb->status != -EREMOTEIO)) {
1202 qh->tt_buffer_dirty = 1;
1203 if (usb_hub_clear_tt_buffer(qtd->urb))
1204 /* Clear failed; let's hope things work
1205 anyway */
1206 qh->tt_buffer_dirty = 0;
1207 }
71a9f9d2
AB
1208 qtd = NULL;
1209 qh->toggle = 0;
1210 qh->ping = 0;
1211 break;
db11e47d 1212
71a9f9d2
AB
1213 default:
1214 WARN_ON(1);
1215 continue;
1216 }
db11e47d 1217
71a9f9d2
AB
1218 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1219 if (slots == priv->int_slots) {
1220 if (state == PTD_STATE_QTD_RELOAD)
1221 dev_err(hcd->self.controller,
1222 "%s: PTD_STATE_QTD_RELOAD on "
1223 "interrupt packet\n", __func__);
1224 if (state != PTD_STATE_QTD_RELOAD)
1225 create_ptd_int(qh, qtd, &ptd);
1226 } else {
1227 if (state != PTD_STATE_QTD_RELOAD)
1228 create_ptd_atl(qh, qtd, &ptd);
1229 }
db11e47d 1230
71a9f9d2
AB
1231 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1232 qh, &ptd);
1233 }
1234 }
db11e47d 1235
71a9f9d2
AB
1236 if (modified)
1237 schedule_ptds(hcd);
6d50c60e 1238}
db11e47d 1239
6d50c60e
AB
1240static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1241{
1242 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1243 u32 imask;
1244 irqreturn_t irqret = IRQ_NONE;
db11e47d 1245
6d50c60e
AB
1246 spin_lock(&priv->lock);
1247
1248 if (!(hcd->state & HC_STATE_RUNNING))
1249 goto leave;
1250
1251 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1252 if (unlikely(!imask))
1253 goto leave;
1254 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1255
1256 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1257 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1258
1259 handle_done_ptds(hcd);
db11e47d 1260
71a9f9d2
AB
1261 irqret = IRQ_HANDLED;
1262leave:
1263 spin_unlock(&priv->lock);
db11e47d 1264
71a9f9d2 1265 return irqret;
db11e47d
SS
1266}
1267
6d50c60e
AB
1268/*
1269 * Workaround for problem described in chip errata 2:
1270 *
1271 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1272 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1273 * ATL done interrupts (the "instead of" might be important since it seems
1274 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1275 * to set the PTD's done bit in addition to not generating an interrupt!).
1276 *
1277 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1278 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1279 *
1280 * If we use SOF interrupts only, we get latency between ptd completion and the
1281 * actual handling. This is very noticeable in testusb runs which takes several
1282 * minutes longer without ATL interrupts.
1283 *
1284 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1285 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1286 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1287 * completed and its done map bit is set.
1288 *
1289 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1290 * not to cause too much lag when this HW bug occurs, while still hopefully
1291 * ensuring that the check does not falsely trigger.
1292 */
6477acc0 1293#define SLOT_TIMEOUT 300
6d50c60e
AB
1294#define SLOT_CHECK_PERIOD 200
1295static struct timer_list errata2_timer;
1296
1297void errata2_function(unsigned long data)
1298{
1299 struct usb_hcd *hcd = (struct usb_hcd *) data;
1300 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1301 int slot;
1302 struct ptd ptd;
1303 unsigned long spinflags;
1304
1305 spin_lock_irqsave(&priv->lock, spinflags);
1306
1307 for (slot = 0; slot < 32; slot++)
6477acc0
AB
1308 if (priv->atl_slots[slot].qh && time_after(jiffies,
1309 priv->atl_slots[slot].timestamp +
1310 SLOT_TIMEOUT * HZ / 1000)) {
6d50c60e
AB
1311 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1312 if (!FROM_DW0_VALID(ptd.dw0) &&
1313 !FROM_DW3_ACTIVE(ptd.dw3))
1314 priv->atl_done_map |= 1 << slot;
1315 }
1316
6477acc0
AB
1317 if (priv->atl_done_map)
1318 handle_done_ptds(hcd);
6d50c60e
AB
1319
1320 spin_unlock_irqrestore(&priv->lock, spinflags);
1321
1322 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1323 add_timer(&errata2_timer);
1324}
1325
0ba7905e
AB
1326static int isp1760_run(struct usb_hcd *hcd)
1327{
1328 int retval;
1329 u32 temp;
1330 u32 command;
1331 u32 chipid;
1332
1333 hcd->uses_new_polling = 1;
1334
1335 hcd->state = HC_STATE_RUNNING;
1336
1337 /* Set PTD interrupt AND & OR maps */
1338 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1339 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1340 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1341 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1342 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1343 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1344 /* step 23 passed */
1345
1346 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1347 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1348
1349 command = reg_read32(hcd->regs, HC_USBCMD);
1350 command &= ~(CMD_LRESET|CMD_RESET);
1351 command |= CMD_RUN;
1352 reg_write32(hcd->regs, HC_USBCMD, command);
1353
1354 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1355 if (retval)
1356 return retval;
1357
1358 /*
1359 * XXX
1360 * Spec says to write FLAG_CF as last config action, priv code grabs
1361 * the semaphore while doing so.
1362 */
1363 down_write(&ehci_cf_port_reset_rwsem);
1364 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1365
1366 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1367 up_write(&ehci_cf_port_reset_rwsem);
1368 if (retval)
1369 return retval;
1370
6d50c60e
AB
1371 init_timer(&errata2_timer);
1372 errata2_timer.function = errata2_function;
1373 errata2_timer.data = (unsigned long) hcd;
1374 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1375 add_timer(&errata2_timer);
1376
0ba7905e
AB
1377 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1378 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1379 chipid & 0xffff, chipid >> 16);
1380
1381 /* PTD Register Init Part 2, Step 28 */
1382
1383 /* Setup registers controlling PTD checking */
1384 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1385 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1386 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1387 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1388 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1389 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1390 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1391 ATL_BUF_FILL | INT_BUF_FILL);
1392
1393 /* GRR this is run-once init(), being done every time the HC starts.
1394 * So long as they're part of class devices, we can't do it init()
1395 * since the class device isn't created that early.
1396 */
1397 return 0;
1398}
1399
34537731 1400static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
db11e47d 1401{
34537731 1402 qtd->data_buffer = databuffer;
db11e47d 1403
34537731
AB
1404 if (len > MAX_PAYLOAD_SIZE)
1405 len = MAX_PAYLOAD_SIZE;
1406 qtd->length = len;
db11e47d 1407
34537731 1408 return qtd->length;
db11e47d
SS
1409}
1410
34537731 1411static void qtd_list_free(struct list_head *qtd_list)
db11e47d 1412{
34537731 1413 struct isp1760_qtd *qtd, *qtd_next;
db11e47d 1414
34537731 1415 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
db11e47d 1416 list_del(&qtd->qtd_list);
34537731 1417 qtd_free(qtd);
db11e47d
SS
1418 }
1419}
1420
db11e47d 1421/*
34537731
AB
1422 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1423 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
db11e47d 1424 */
6bda21bc 1425#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
34537731 1426static void packetize_urb(struct usb_hcd *hcd,
db11e47d
SS
1427 struct urb *urb, struct list_head *head, gfp_t flags)
1428{
fd436aee 1429 struct isp1760_qtd *qtd;
db11e47d 1430 void *buf;
34537731
AB
1431 int len, maxpacketsize;
1432 u8 packet_type;
db11e47d
SS
1433
1434 /*
1435 * URBs map to sequences of QTDs: one logical transaction
1436 */
db11e47d 1437
34537731
AB
1438 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1439 /* XXX This looks like usb storage / SCSI bug */
1440 dev_err(hcd->self.controller,
1441 "buf is null, dma is %08lx len is %d\n",
1442 (long unsigned)urb->transfer_dma,
1443 urb->transfer_buffer_length);
1444 WARN_ON(1);
1445 }
db11e47d 1446
34537731
AB
1447 if (usb_pipein(urb->pipe))
1448 packet_type = IN_PID;
1449 else
1450 packet_type = OUT_PID;
db11e47d 1451
db11e47d 1452 if (usb_pipecontrol(urb->pipe)) {
34537731 1453 qtd = qtd_alloc(flags, urb, SETUP_PID);
db11e47d
SS
1454 if (!qtd)
1455 goto cleanup;
34537731 1456 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
db11e47d
SS
1457 list_add_tail(&qtd->qtd_list, head);
1458
1459 /* for zero length DATA stages, STATUS is always IN */
34537731
AB
1460 if (urb->transfer_buffer_length == 0)
1461 packet_type = IN_PID;
db11e47d
SS
1462 }
1463
34537731
AB
1464 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1465 usb_pipeout(urb->pipe)));
db11e47d
SS
1466
1467 /*
1468 * buffer gets wrapped in one or more qtds;
1469 * last one may be "short" (including zero len)
1470 * and may serve as a control status ack
1471 */
34537731
AB
1472 buf = urb->transfer_buffer;
1473 len = urb->transfer_buffer_length;
1474
db11e47d
SS
1475 for (;;) {
1476 int this_qtd_len;
1477
34537731
AB
1478 qtd = qtd_alloc(flags, urb, packet_type);
1479 if (!qtd)
1480 goto cleanup;
1481 this_qtd_len = qtd_fill(qtd, buf, len);
1482 list_add_tail(&qtd->qtd_list, head);
db11e47d 1483
db11e47d
SS
1484 len -= this_qtd_len;
1485 buf += this_qtd_len;
1486
db11e47d
SS
1487 if (len <= 0)
1488 break;
db11e47d
SS
1489 }
1490
1491 /*
1492 * control requests may need a terminating data "status" ack;
1493 * bulk ones may need a terminating short packet (zero length).
1494 */
1495 if (urb->transfer_buffer_length != 0) {
1496 int one_more = 0;
1497
1498 if (usb_pipecontrol(urb->pipe)) {
1499 one_more = 1;
34537731
AB
1500 if (packet_type == IN_PID)
1501 packet_type = OUT_PID;
1502 else
1503 packet_type = IN_PID;
db11e47d
SS
1504 } else if (usb_pipebulk(urb->pipe)
1505 && (urb->transfer_flags & URB_ZERO_PACKET)
34537731
AB
1506 && !(urb->transfer_buffer_length %
1507 maxpacketsize)) {
db11e47d
SS
1508 one_more = 1;
1509 }
1510 if (one_more) {
34537731 1511 qtd = qtd_alloc(flags, urb, packet_type);
db11e47d
SS
1512 if (!qtd)
1513 goto cleanup;
db11e47d
SS
1514
1515 /* never any data in such packets */
34537731
AB
1516 qtd_fill(qtd, NULL, 0);
1517 list_add_tail(&qtd->qtd_list, head);
db11e47d
SS
1518 }
1519 }
1520
34537731 1521 return;
db11e47d
SS
1522
1523cleanup:
34537731
AB
1524 qtd_list_free(head);
1525}
1526
db11e47d
SS
1527static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1528 gfp_t mem_flags)
1529{
71a9f9d2
AB
1530 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1531 struct list_head *ep_queue;
1532 struct isp1760_qh *qh, *qhit;
1533 unsigned long spinflags;
1534 LIST_HEAD(new_qtds);
1535 int retval;
1536 int qh_in_queue;
db11e47d
SS
1537
1538 switch (usb_pipetype(urb->pipe)) {
1539 case PIPE_CONTROL:
e08f6a27 1540 ep_queue = &priv->qh_list[QH_CONTROL];
71a9f9d2 1541 break;
db11e47d 1542 case PIPE_BULK:
e08f6a27 1543 ep_queue = &priv->qh_list[QH_BULK];
db11e47d 1544 break;
db11e47d 1545 case PIPE_INTERRUPT:
71a9f9d2
AB
1546 if (urb->interval < 0)
1547 return -EINVAL;
1548 /* FIXME: Check bandwidth */
e08f6a27 1549 ep_queue = &priv->qh_list[QH_INTERRUPT];
db11e47d 1550 break;
db11e47d 1551 case PIPE_ISOCHRONOUS:
71a9f9d2
AB
1552 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1553 "not yet supported\n",
1554 __func__);
1555 return -EPIPE;
db11e47d 1556 default:
71a9f9d2
AB
1557 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1558 __func__);
db11e47d
SS
1559 return -EPIPE;
1560 }
1561
71a9f9d2
AB
1562 if (usb_pipein(urb->pipe))
1563 urb->actual_length = 0;
db11e47d 1564
71a9f9d2
AB
1565 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1566 if (list_empty(&new_qtds))
1567 return -ENOMEM;
db11e47d 1568
71a9f9d2
AB
1569 retval = 0;
1570 spin_lock_irqsave(&priv->lock, spinflags);
db11e47d 1571
71a9f9d2
AB
1572 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1573 retval = -ESHUTDOWN;
1574 goto out;
db11e47d 1575 }
71a9f9d2
AB
1576 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1577 if (retval)
1578 goto out;
db11e47d 1579
71a9f9d2
AB
1580 qh = urb->ep->hcpriv;
1581 if (qh) {
1582 qh_in_queue = 0;
1583 list_for_each_entry(qhit, ep_queue, qh_list) {
1584 if (qhit == qh) {
1585 qh_in_queue = 1;
0afb20e0 1586 break;
71a9f9d2
AB
1587 }
1588 }
1589 if (!qh_in_queue)
1590 list_add_tail(&qh->qh_list, ep_queue);
1591 } else {
1592 qh = qh_alloc(GFP_ATOMIC);
1593 if (!qh) {
1594 retval = -ENOMEM;
38679b72 1595 usb_hcd_unlink_urb_from_ep(hcd, urb);
71a9f9d2 1596 goto out;
db11e47d 1597 }
71a9f9d2
AB
1598 list_add_tail(&qh->qh_list, ep_queue);
1599 urb->ep->hcpriv = qh;
db11e47d
SS
1600 }
1601
71a9f9d2
AB
1602 list_splice_tail(&new_qtds, &qh->qtd_list);
1603 schedule_ptds(hcd);
1604
1605out:
1606 spin_unlock_irqrestore(&priv->lock, spinflags);
1607 return retval;
db11e47d
SS
1608}
1609
d05b6ec0
AB
1610static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1611 struct isp1760_qh *qh)
1612{
1613 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1614 int skip_map;
1615
1616 WARN_ON(qh->slot == -1);
1617
1618 /* We need to forcefully reclaim the slot since some transfers never
1619 return, e.g. interrupt transfers and NAKed bulk transfers. */
8b1ab60c 1620 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
d05b6ec0
AB
1621 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1622 skip_map |= (1 << qh->slot);
1623 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1624 priv->atl_slots[qh->slot].qh = NULL;
1625 priv->atl_slots[qh->slot].qtd = NULL;
1626 } else {
1627 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1628 skip_map |= (1 << qh->slot);
1629 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1630 priv->int_slots[qh->slot].qh = NULL;
1631 priv->int_slots[qh->slot].qtd = NULL;
1632 }
1633
1634 qh->slot = -1;
d05b6ec0
AB
1635}
1636
74ad6029
AB
1637/*
1638 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1639 * any active transfer belonging to the urb in the process.
1640 */
1641static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1642 struct isp1760_qtd *qtd)
1643{
1644 struct urb *urb;
1645 int urb_was_running;
1646
1647 urb = qtd->urb;
1648 urb_was_running = 0;
1649 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
1650 if (qtd->urb != urb)
1651 break;
1652
1653 if (qtd->status >= QTD_XFER_STARTED)
1654 urb_was_running = 1;
1655 if (last_qtd_of_urb(qtd, qh) &&
1656 (qtd->status >= QTD_XFER_COMPLETE))
1657 urb_was_running = 0;
1658
1659 if (qtd->status == QTD_XFER_STARTED)
1660 kill_transfer(hcd, urb, qh);
1661 qtd->status = QTD_RETIRE;
1662 }
1663
1664 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
1665 qh->tt_buffer_dirty = 1;
1666 if (usb_hub_clear_tt_buffer(urb))
1667 /* Clear failed; let's hope things work anyway */
1668 qh->tt_buffer_dirty = 0;
1669 }
1670}
1671
71a9f9d2
AB
1672static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1673 int status)
db11e47d 1674{
6bda21bc 1675 struct isp1760_hcd *priv = hcd_to_priv(hcd);
d05b6ec0 1676 unsigned long spinflags;
71a9f9d2
AB
1677 struct isp1760_qh *qh;
1678 struct isp1760_qtd *qtd;
71a9f9d2 1679 int retval = 0;
db11e47d 1680
71a9f9d2 1681 spin_lock_irqsave(&priv->lock, spinflags);
17d3e145
AB
1682 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1683 if (retval)
1684 goto out;
db11e47d 1685
71a9f9d2
AB
1686 qh = urb->ep->hcpriv;
1687 if (!qh) {
1688 retval = -EINVAL;
1689 goto out;
1690 }
db11e47d 1691
d05b6ec0
AB
1692 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1693 if (qtd->urb == urb) {
74ad6029
AB
1694 dequeue_urb_from_qtd(hcd, qh, qtd);
1695 break;
d05b6ec0 1696 }
db11e47d 1697
71a9f9d2
AB
1698 urb->status = status;
1699 schedule_ptds(hcd);
db11e47d 1700
71a9f9d2
AB
1701out:
1702 spin_unlock_irqrestore(&priv->lock, spinflags);
71a9f9d2 1703 return retval;
db11e47d
SS
1704}
1705
079cdb09
AB
1706static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1707 struct usb_host_endpoint *ep)
1708{
1709 struct isp1760_hcd *priv = hcd_to_priv(hcd);
d05b6ec0 1710 unsigned long spinflags;
079cdb09
AB
1711 struct isp1760_qh *qh;
1712 struct isp1760_qtd *qtd;
079cdb09
AB
1713
1714 spin_lock_irqsave(&priv->lock, spinflags);
d05b6ec0 1715
079cdb09
AB
1716 qh = ep->hcpriv;
1717 if (!qh)
1718 goto out;
1719
74ad6029
AB
1720 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1721 if (qtd->status != QTD_RETIRE) {
1722 dequeue_urb_from_qtd(hcd, qh, qtd);
1723 qtd->urb->status = -ECONNRESET;
1724 }
d05b6ec0 1725
079cdb09
AB
1726 ep->hcpriv = NULL;
1727 /* Cannot free qh here since it will be parsed by schedule_ptds() */
1728
d05b6ec0
AB
1729 schedule_ptds(hcd);
1730
079cdb09
AB
1731out:
1732 spin_unlock_irqrestore(&priv->lock, spinflags);
1733}
1734
db11e47d
SS
1735static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1736{
1737 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1738 u32 temp, status = 0;
1739 u32 mask;
1740 int retval = 1;
1741 unsigned long flags;
1742
1743 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1744 if (!HC_IS_RUNNING(hcd->state))
1745 return 0;
1746
1747 /* init status to no-changes */
1748 buf[0] = 0;
1749 mask = PORT_CSC;
1750
1751 spin_lock_irqsave(&priv->lock, flags);
bedc0c31 1752 temp = reg_read32(hcd->regs, HC_PORTSC1);
db11e47d
SS
1753
1754 if (temp & PORT_OWNER) {
1755 if (temp & PORT_CSC) {
1756 temp &= ~PORT_CSC;
bedc0c31 1757 reg_write32(hcd->regs, HC_PORTSC1, temp);
db11e47d
SS
1758 goto done;
1759 }
1760 }
1761
1762 /*
1763 * Return status information even for ports with OWNER set.
1764 * Otherwise khubd wouldn't see the disconnect event when a
1765 * high-speed device is switched over to the companion
1766 * controller by the user.
1767 */
1768
1769 if ((temp & mask) != 0
1770 || ((temp & PORT_RESUME) != 0
1771 && time_after_eq(jiffies,
1772 priv->reset_done))) {
1773 buf [0] |= 1 << (0 + 1);
1774 status = STS_PCD;
1775 }
1776 /* FIXME autosuspend idle root hubs */
1777done:
1778 spin_unlock_irqrestore(&priv->lock, flags);
1779 return status ? retval : 0;
1780}
1781
1782static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1783 struct usb_hub_descriptor *desc)
1784{
1785 int ports = HCS_N_PORTS(priv->hcs_params);
1786 u16 temp;
1787
1788 desc->bDescriptorType = 0x29;
1789 /* priv 1.0, 2.3.9 says 20ms max */
1790 desc->bPwrOn2PwrGood = 10;
1791 desc->bHubContrCurrent = 0;
1792
1793 desc->bNbrPorts = ports;
1794 temp = 1 + (ports / 8);
1795 desc->bDescLength = 7 + 2 * temp;
1796
da13051c 1797 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
dbe79bbe
JY
1798 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1799 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
db11e47d
SS
1800
1801 /* per-port overcurrent reporting */
1802 temp = 0x0008;
1803 if (HCS_PPC(priv->hcs_params))
1804 /* per-port power control */
1805 temp |= 0x0001;
1806 else
1807 /* no power switching */
1808 temp |= 0x0002;
1809 desc->wHubCharacteristics = cpu_to_le16(temp);
1810}
1811
1812#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1813
bedc0c31
AB
1814static int check_reset_complete(struct usb_hcd *hcd, int index,
1815 int port_status)
db11e47d
SS
1816{
1817 if (!(port_status & PORT_CONNECT))
1818 return port_status;
1819
1820 /* if reset finished and it's still not enabled -- handoff */
1821 if (!(port_status & PORT_PE)) {
1822
71a9f9d2 1823 dev_info(hcd->self.controller,
6bda21bc
AB
1824 "port %d full speed --> companion\n",
1825 index + 1);
db11e47d
SS
1826
1827 port_status |= PORT_OWNER;
1828 port_status &= ~PORT_RWC_BITS;
bedc0c31 1829 reg_write32(hcd->regs, HC_PORTSC1, port_status);
db11e47d
SS
1830
1831 } else
71a9f9d2 1832 dev_info(hcd->self.controller, "port %d high speed\n",
6bda21bc 1833 index + 1);
db11e47d
SS
1834
1835 return port_status;
1836}
1837
1838static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1839 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1840{
1841 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1842 int ports = HCS_N_PORTS(priv->hcs_params);
db11e47d
SS
1843 u32 temp, status;
1844 unsigned long flags;
1845 int retval = 0;
1846 unsigned selector;
1847
1848 /*
1849 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1850 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1851 * (track current state ourselves) ... blink for diagnostics,
1852 * power, "this is the one", etc. EHCI spec supports this.
1853 */
1854
1855 spin_lock_irqsave(&priv->lock, flags);
1856 switch (typeReq) {
1857 case ClearHubFeature:
1858 switch (wValue) {
1859 case C_HUB_LOCAL_POWER:
1860 case C_HUB_OVER_CURRENT:
1861 /* no hub-wide feature/status flags */
1862 break;
1863 default:
1864 goto error;
1865 }
1866 break;
1867 case ClearPortFeature:
1868 if (!wIndex || wIndex > ports)
1869 goto error;
1870 wIndex--;
bedc0c31 1871 temp = reg_read32(hcd->regs, HC_PORTSC1);
db11e47d
SS
1872
1873 /*
1874 * Even if OWNER is set, so the port is owned by the
1875 * companion controller, khubd needs to be able to clear
1876 * the port-change status bits (especially
749da5f8 1877 * USB_PORT_STAT_C_CONNECTION).
db11e47d
SS
1878 */
1879
1880 switch (wValue) {
1881 case USB_PORT_FEAT_ENABLE:
bedc0c31 1882 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
db11e47d
SS
1883 break;
1884 case USB_PORT_FEAT_C_ENABLE:
1885 /* XXX error? */
1886 break;
1887 case USB_PORT_FEAT_SUSPEND:
1888 if (temp & PORT_RESET)
1889 goto error;
1890
1891 if (temp & PORT_SUSPEND) {
1892 if ((temp & PORT_PE) == 0)
1893 goto error;
1894 /* resume signaling for 20 msec */
1895 temp &= ~(PORT_RWC_BITS);
bedc0c31
AB
1896 reg_write32(hcd->regs, HC_PORTSC1,
1897 temp | PORT_RESUME);
db11e47d
SS
1898 priv->reset_done = jiffies +
1899 msecs_to_jiffies(20);
1900 }
1901 break;
1902 case USB_PORT_FEAT_C_SUSPEND:
1903 /* we auto-clear this feature */
1904 break;
1905 case USB_PORT_FEAT_POWER:
1906 if (HCS_PPC(priv->hcs_params))
bedc0c31
AB
1907 reg_write32(hcd->regs, HC_PORTSC1,
1908 temp & ~PORT_POWER);
db11e47d
SS
1909 break;
1910 case USB_PORT_FEAT_C_CONNECTION:
bedc0c31 1911 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
db11e47d
SS
1912 break;
1913 case USB_PORT_FEAT_C_OVER_CURRENT:
1914 /* XXX error ?*/
1915 break;
1916 case USB_PORT_FEAT_C_RESET:
1917 /* GetPortStatus clears reset */
1918 break;
1919 default:
1920 goto error;
1921 }
bedc0c31 1922 reg_read32(hcd->regs, HC_USBCMD);
db11e47d
SS
1923 break;
1924 case GetHubDescriptor:
1925 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1926 buf);
1927 break;
1928 case GetHubStatus:
1929 /* no hub-wide feature/status flags */
1930 memset(buf, 0, 4);
1931 break;
1932 case GetPortStatus:
1933 if (!wIndex || wIndex > ports)
1934 goto error;
1935 wIndex--;
1936 status = 0;
bedc0c31 1937 temp = reg_read32(hcd->regs, HC_PORTSC1);
db11e47d
SS
1938
1939 /* wPortChange bits */
1940 if (temp & PORT_CSC)
749da5f8 1941 status |= USB_PORT_STAT_C_CONNECTION << 16;
db11e47d
SS
1942
1943
1944 /* whoever resumes must GetPortStatus to complete it!! */
1945 if (temp & PORT_RESUME) {
6bda21bc 1946 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
db11e47d
SS
1947
1948 /* Remote Wakeup received? */
1949 if (!priv->reset_done) {
1950 /* resume signaling for 20 msec */
1951 priv->reset_done = jiffies
1952 + msecs_to_jiffies(20);
1953 /* check the port again */
6bda21bc 1954 mod_timer(&hcd->rh_timer, priv->reset_done);
db11e47d
SS
1955 }
1956
1957 /* resume completed? */
1958 else if (time_after_eq(jiffies,
1959 priv->reset_done)) {
749da5f8 1960 status |= USB_PORT_STAT_C_SUSPEND << 16;
db11e47d
SS
1961 priv->reset_done = 0;
1962
1963 /* stop resume signaling */
bedc0c31
AB
1964 temp = reg_read32(hcd->regs, HC_PORTSC1);
1965 reg_write32(hcd->regs, HC_PORTSC1,
1966 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1967 retval = handshake(hcd, HC_PORTSC1,
db11e47d
SS
1968 PORT_RESUME, 0, 2000 /* 2msec */);
1969 if (retval != 0) {
6bda21bc 1970 dev_err(hcd->self.controller,
db11e47d
SS
1971 "port %d resume error %d\n",
1972 wIndex + 1, retval);
1973 goto error;
1974 }
1975 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1976 }
1977 }
1978
1979 /* whoever resets must GetPortStatus to complete it!! */
1980 if ((temp & PORT_RESET)
1981 && time_after_eq(jiffies,
1982 priv->reset_done)) {
749da5f8 1983 status |= USB_PORT_STAT_C_RESET << 16;
db11e47d
SS
1984 priv->reset_done = 0;
1985
1986 /* force reset to complete */
bedc0c31 1987 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
db11e47d
SS
1988 /* REVISIT: some hardware needs 550+ usec to clear
1989 * this bit; seems too long to spin routinely...
1990 */
bedc0c31 1991 retval = handshake(hcd, HC_PORTSC1,
db11e47d
SS
1992 PORT_RESET, 0, 750);
1993 if (retval != 0) {
6bda21bc 1994 dev_err(hcd->self.controller, "port %d reset error %d\n",
db11e47d
SS
1995 wIndex + 1, retval);
1996 goto error;
1997 }
1998
1999 /* see what we found out */
bedc0c31
AB
2000 temp = check_reset_complete(hcd, wIndex,
2001 reg_read32(hcd->regs, HC_PORTSC1));
db11e47d
SS
2002 }
2003 /*
2004 * Even if OWNER is set, there's no harm letting khubd
2005 * see the wPortStatus values (they should all be 0 except
2006 * for PORT_POWER anyway).
2007 */
2008
2009 if (temp & PORT_OWNER)
6bda21bc 2010 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
db11e47d
SS
2011
2012 if (temp & PORT_CONNECT) {
749da5f8 2013 status |= USB_PORT_STAT_CONNECTION;
db11e47d 2014 /* status may be from integrated TT */
6bda21bc 2015 status |= USB_PORT_STAT_HIGH_SPEED;
db11e47d
SS
2016 }
2017 if (temp & PORT_PE)
749da5f8 2018 status |= USB_PORT_STAT_ENABLE;
db11e47d 2019 if (temp & (PORT_SUSPEND|PORT_RESUME))
749da5f8 2020 status |= USB_PORT_STAT_SUSPEND;
db11e47d 2021 if (temp & PORT_RESET)
749da5f8 2022 status |= USB_PORT_STAT_RESET;
db11e47d 2023 if (temp & PORT_POWER)
749da5f8 2024 status |= USB_PORT_STAT_POWER;
db11e47d
SS
2025
2026 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2027 break;
2028 case SetHubFeature:
2029 switch (wValue) {
2030 case C_HUB_LOCAL_POWER:
2031 case C_HUB_OVER_CURRENT:
2032 /* no hub-wide feature/status flags */
2033 break;
2034 default:
2035 goto error;
2036 }
2037 break;
2038 case SetPortFeature:
2039 selector = wIndex >> 8;
2040 wIndex &= 0xff;
2041 if (!wIndex || wIndex > ports)
2042 goto error;
2043 wIndex--;
bedc0c31 2044 temp = reg_read32(hcd->regs, HC_PORTSC1);
db11e47d
SS
2045 if (temp & PORT_OWNER)
2046 break;
2047
2048/* temp &= ~PORT_RWC_BITS; */
2049 switch (wValue) {
2050 case USB_PORT_FEAT_ENABLE:
bedc0c31 2051 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
db11e47d
SS
2052 break;
2053
2054 case USB_PORT_FEAT_SUSPEND:
2055 if ((temp & PORT_PE) == 0
2056 || (temp & PORT_RESET) != 0)
2057 goto error;
2058
bedc0c31 2059 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
db11e47d
SS
2060 break;
2061 case USB_PORT_FEAT_POWER:
2062 if (HCS_PPC(priv->hcs_params))
bedc0c31
AB
2063 reg_write32(hcd->regs, HC_PORTSC1,
2064 temp | PORT_POWER);
db11e47d
SS
2065 break;
2066 case USB_PORT_FEAT_RESET:
2067 if (temp & PORT_RESUME)
2068 goto error;
2069 /* line status bits may report this as low speed,
2070 * which can be fine if this root hub has a
2071 * transaction translator built in.
2072 */
2073 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2074 && PORT_USB11(temp)) {
2075 temp |= PORT_OWNER;
2076 } else {
2077 temp |= PORT_RESET;
2078 temp &= ~PORT_PE;
2079
2080 /*
2081 * caller must wait, then call GetPortStatus
2082 * usb 2.0 spec says 50 ms resets on root
2083 */
2084 priv->reset_done = jiffies +
2085 msecs_to_jiffies(50);
2086 }
bedc0c31 2087 reg_write32(hcd->regs, HC_PORTSC1, temp);
db11e47d
SS
2088 break;
2089 default:
2090 goto error;
2091 }
bedc0c31 2092 reg_read32(hcd->regs, HC_USBCMD);
db11e47d
SS
2093 break;
2094
2095 default:
2096error:
2097 /* "stall" on error */
2098 retval = -EPIPE;
2099 }
2100 spin_unlock_irqrestore(&priv->lock, flags);
2101 return retval;
2102}
2103
db11e47d
SS
2104static int isp1760_get_frame(struct usb_hcd *hcd)
2105{
2106 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2107 u32 fr;
2108
bedc0c31 2109 fr = reg_read32(hcd->regs, HC_FRINDEX);
db11e47d
SS
2110 return (fr >> 3) % priv->periodic_size;
2111}
2112
2113static void isp1760_stop(struct usb_hcd *hcd)
2114{
2115 struct isp1760_hcd *priv = hcd_to_priv(hcd);
3faefc88 2116 u32 temp;
db11e47d 2117
6d50c60e
AB
2118 del_timer(&errata2_timer);
2119
db11e47d
SS
2120 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2121 NULL, 0);
2122 mdelay(20);
2123
2124 spin_lock_irq(&priv->lock);
6bda21bc 2125 ehci_reset(hcd);
db11e47d 2126 /* Disable IRQ */
bedc0c31
AB
2127 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2128 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
db11e47d
SS
2129 spin_unlock_irq(&priv->lock);
2130
bedc0c31 2131 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
db11e47d
SS
2132}
2133
2134static void isp1760_shutdown(struct usb_hcd *hcd)
2135{
3faefc88 2136 u32 command, temp;
db11e47d
SS
2137
2138 isp1760_stop(hcd);
bedc0c31
AB
2139 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2140 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
db11e47d 2141
bedc0c31 2142 command = reg_read32(hcd->regs, HC_USBCMD);
db11e47d 2143 command &= ~CMD_RUN;
bedc0c31 2144 reg_write32(hcd->regs, HC_USBCMD, command);
db11e47d
SS
2145}
2146
74ad6029
AB
2147static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2148 struct usb_host_endpoint *ep)
2149{
2150 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2151 struct isp1760_qh *qh = ep->hcpriv;
2152 unsigned long spinflags;
2153
2154 if (!qh)
2155 return;
2156
2157 spin_lock_irqsave(&priv->lock, spinflags);
2158 qh->tt_buffer_dirty = 0;
2159 schedule_ptds(hcd);
2160 spin_unlock_irqrestore(&priv->lock, spinflags);
2161}
2162
2163
db11e47d
SS
2164static const struct hc_driver isp1760_hc_driver = {
2165 .description = "isp1760-hcd",
2166 .product_desc = "NXP ISP1760 USB Host Controller",
2167 .hcd_priv_size = sizeof(struct isp1760_hcd),
2168 .irq = isp1760_irq,
2169 .flags = HCD_MEMORY | HCD_USB2,
2170 .reset = isp1760_hc_setup,
2171 .start = isp1760_run,
2172 .stop = isp1760_stop,
2173 .shutdown = isp1760_shutdown,
2174 .urb_enqueue = isp1760_urb_enqueue,
2175 .urb_dequeue = isp1760_urb_dequeue,
2176 .endpoint_disable = isp1760_endpoint_disable,
2177 .get_frame_number = isp1760_get_frame,
2178 .hub_status_data = isp1760_hub_status_data,
2179 .hub_control = isp1760_hub_control,
74ad6029 2180 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
db11e47d
SS
2181};
2182
2183int __init init_kmem_once(void)
2184{
71a9f9d2
AB
2185 urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
2186 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2187 SLAB_MEM_SPREAD, NULL);
2188
2189 if (!urb_listitem_cachep)
2190 return -ENOMEM;
2191
db11e47d
SS
2192 qtd_cachep = kmem_cache_create("isp1760_qtd",
2193 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2194 SLAB_MEM_SPREAD, NULL);
2195
2196 if (!qtd_cachep)
2197 return -ENOMEM;
2198
2199 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2200 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2201
2202 if (!qh_cachep) {
2203 kmem_cache_destroy(qtd_cachep);
2204 return -ENOMEM;
2205 }
2206
2207 return 0;
2208}
2209
2210void deinit_kmem_cache(void)
2211{
2212 kmem_cache_destroy(qtd_cachep);
2213 kmem_cache_destroy(qh_cachep);
71a9f9d2 2214 kmem_cache_destroy(urb_listitem_cachep);
db11e47d
SS
2215}
2216
f9031f2c
CM
2217struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2218 int irq, unsigned long irqflags,
3a7655fc 2219 int rst_gpio,
f9031f2c
CM
2220 struct device *dev, const char *busname,
2221 unsigned int devflags)
db11e47d
SS
2222{
2223 struct usb_hcd *hcd;
2224 struct isp1760_hcd *priv;
2225 int ret;
2226
2227 if (usb_disabled())
2228 return ERR_PTR(-ENODEV);
2229
2230 /* prevent usb-core allocating DMA pages */
2231 dev->dma_mask = NULL;
2232
0031a06e 2233 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
db11e47d
SS
2234 if (!hcd)
2235 return ERR_PTR(-ENOMEM);
2236
2237 priv = hcd_to_priv(hcd);
3faefc88 2238 priv->devflags = devflags;
3a7655fc 2239 priv->rst_gpio = rst_gpio;
db11e47d
SS
2240 init_memory(priv);
2241 hcd->regs = ioremap(res_start, res_len);
2242 if (!hcd->regs) {
2243 ret = -EIO;
2244 goto err_put;
2245 }
2246
db11e47d
SS
2247 hcd->irq = irq;
2248 hcd->rsrc_start = res_start;
2249 hcd->rsrc_len = res_len;
2250
e6942d63
NC
2251 ret = usb_add_hcd(hcd, irq, irqflags);
2252 if (ret)
2253 goto err_unmap;
2254
db11e47d
SS
2255 return hcd;
2256
2257err_unmap:
2258 iounmap(hcd->regs);
2259
2260err_put:
2261 usb_put_hcd(hcd);
2262
2263 return ERR_PTR(ret);
2264}
2265
2266MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2267MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2268MODULE_LICENSE("GPL v2");