blob: 2342f11d0f30d16aaa58fe98d370ae91c5307f04 [file] [log] [blame]
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001/*
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 *
11 */
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/list.h>
16#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020017#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020018#include <linux/debugfs.h>
19#include <linux/uaccess.h>
20#include <linux/io.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000021#include <linux/mm.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020022#include <asm/unaligned.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000023#include <asm/cacheflush.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020024
Sebastian Siewiordb11e472008-04-24 00:37:04 +020025#include "isp1760-hcd.h"
26
27static struct kmem_cache *qtd_cachep;
28static struct kmem_cache *qh_cachep;
29
30struct isp1760_hcd {
31 u32 hcs_params;
32 spinlock_t lock;
33 struct inter_packet_info atl_ints[32];
34 struct inter_packet_info int_ints[32];
35 struct memory_chunk memory_pool[BLOCKS];
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +010036 u32 atl_queued;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020037
38 /* periodic schedule support */
39#define DEFAULT_I_TDPS 1024
40 unsigned periodic_size;
41 unsigned i_thresh;
42 unsigned long reset_done;
43 unsigned long next_statechange;
Nate Case3faefc82008-06-17 11:11:38 -050044 unsigned int devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020045};
46
47static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
48{
49 return (struct isp1760_hcd *) (hcd->hcd_priv);
50}
51static inline struct usb_hcd *priv_to_hcd(struct isp1760_hcd *priv)
52{
53 return container_of((void *) priv, struct usb_hcd, hcd_priv);
54}
55
56/* Section 2.2 Host Controller Capability Registers */
57#define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
58#define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
59#define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
60#define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
61#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
62#define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
63#define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
64
65/* Section 2.3 Host Controller Operational Registers */
66#define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
67#define CMD_RESET (1<<1) /* reset HC not bus */
68#define CMD_RUN (1<<0) /* start/stop HC */
69#define STS_PCD (1<<2) /* port change detect */
70#define FLAG_CF (1<<0) /* true: we'll support "high speed" */
71
72#define PORT_OWNER (1<<13) /* true: companion hc owns this port */
73#define PORT_POWER (1<<12) /* true: has power (see PPC) */
74#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
75#define PORT_RESET (1<<8) /* reset port */
76#define PORT_SUSPEND (1<<7) /* suspend port */
77#define PORT_RESUME (1<<6) /* resume it */
78#define PORT_PE (1<<2) /* port enable */
79#define PORT_CSC (1<<1) /* connect status change */
80#define PORT_CONNECT (1<<0) /* device connected */
81#define PORT_RWC_BITS (PORT_CSC)
82
83struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +020084 u8 packet_type;
85 u8 toggle;
86
87 void *data_buffer;
88 /* the rest is HCD-private */
89 struct list_head qtd_list;
90 struct urb *urb;
91 size_t length;
92
93 /* isp special*/
94 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020095#define URB_ENQUEUED (1 << 1)
Sebastian Siewiordb11e472008-04-24 00:37:04 +020096};
97
98struct isp1760_qh {
99 /* first part defined by EHCI spec */
100 struct list_head qtd_list;
101 struct isp1760_hcd *priv;
102
103 /* periodic schedule info */
104 unsigned short period; /* polling interval */
105 struct usb_device *dev;
106
107 u32 toggle;
108 u32 ping;
109};
110
Alan Stern288ead42010-03-04 11:32:30 -0500111#define ehci_port_speed(priv, portsc) USB_PORT_STAT_HIGH_SPEED
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200112
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100113/*
114 * Access functions for isp176x registers (addresses 0..0x03FF).
115 */
116static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200117{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100118 return readl(base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200119}
120
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100121static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200122{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100123 writel(val, base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200124}
125
126/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100127 * Access functions for isp176x memory (offset >= 0x0400).
128 *
129 * bank_reads8() reads memory locations prefetched by an earlier write to
130 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
131 * bank optimizations, you should use the more generic mem_reads8() below.
132 *
133 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
134 * below.
135 *
136 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200137 * doesn't quite work because some people have to enforce 32-bit access
138 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100139static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
140 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200141{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100142 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200143 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100144 __u8 *src_byteptr;
145 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200146
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100147 src = src_base + (bank_addr | src_offset);
148
149 if (src_offset < PAYLOAD_OFFSET) {
150 while (bytes >= 4) {
151 *dst = le32_to_cpu(__raw_readl(src));
152 bytes -= 4;
153 src++;
154 dst++;
155 }
156 } else {
157 while (bytes >= 4) {
158 *dst = __raw_readl(src);
159 bytes -= 4;
160 src++;
161 dst++;
162 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200163 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200164
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100165 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200166 return;
167
168 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
169 * allocated.
170 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100171 if (src_offset < PAYLOAD_OFFSET)
172 val = le32_to_cpu(__raw_readl(src));
173 else
174 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200175
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100176 dst_byteptr = (void *) dst;
177 src_byteptr = (void *) &val;
178 while (bytes > 0) {
179 *dst_byteptr = *src_byteptr;
180 dst_byteptr++;
181 src_byteptr++;
182 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200183 }
184}
185
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100186static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
187 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200188{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100189 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
190 ndelay(90);
191 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
192}
193
194static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
195 __u32 const *src, u32 bytes)
196{
197 __u32 __iomem *dst;
198
199 dst = dst_base + dst_offset;
200
201 if (dst_offset < PAYLOAD_OFFSET) {
202 while (bytes >= 4) {
203 __raw_writel(cpu_to_le32(*src), dst);
204 bytes -= 4;
205 src++;
206 dst++;
207 }
208 } else {
209 while (bytes >= 4) {
210 __raw_writel(*src, dst);
211 bytes -= 4;
212 src++;
213 dst++;
214 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200215 }
216
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100217 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200218 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100219 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
220 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200221 */
222
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100223 if (dst_offset < PAYLOAD_OFFSET)
224 __raw_writel(cpu_to_le32(*src), dst);
225 else
226 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200227}
228
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100229/*
230 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
231 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
232 */
233static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
234 struct ptd *ptd)
235{
236 reg_write32(base, HC_MEMORY_REG,
237 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
238 ndelay(90);
239 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
240 (void *) ptd, sizeof(*ptd));
241}
242
243static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
244 struct ptd *ptd)
245{
246 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
247 &ptd->dw1, 7*sizeof(ptd->dw1));
248 /* Make sure dw0 gets written last (after other dw's and after payload)
249 since it contains the enable bit */
250 wmb();
251 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
252 sizeof(ptd->dw0));
253}
254
255
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200256/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
257static void init_memory(struct isp1760_hcd *priv)
258{
259 int i;
260 u32 payload;
261
262 payload = 0x1000;
263 for (i = 0; i < BLOCK_1_NUM; i++) {
264 priv->memory_pool[i].start = payload;
265 priv->memory_pool[i].size = BLOCK_1_SIZE;
266 priv->memory_pool[i].free = 1;
267 payload += priv->memory_pool[i].size;
268 }
269
270
271 for (i = BLOCK_1_NUM; i < BLOCK_1_NUM + BLOCK_2_NUM; i++) {
272 priv->memory_pool[i].start = payload;
273 priv->memory_pool[i].size = BLOCK_2_SIZE;
274 priv->memory_pool[i].free = 1;
275 payload += priv->memory_pool[i].size;
276 }
277
278
279 for (i = BLOCK_1_NUM + BLOCK_2_NUM; i < BLOCKS; i++) {
280 priv->memory_pool[i].start = payload;
281 priv->memory_pool[i].size = BLOCK_3_SIZE;
282 priv->memory_pool[i].free = 1;
283 payload += priv->memory_pool[i].size;
284 }
285
286 BUG_ON(payload - priv->memory_pool[i - 1].size > PAYLOAD_SIZE);
287}
288
289static u32 alloc_mem(struct isp1760_hcd *priv, u32 size)
290{
291 int i;
292
293 if (!size)
294 return ISP1760_NULL_POINTER;
295
296 for (i = 0; i < BLOCKS; i++) {
297 if (priv->memory_pool[i].size >= size &&
298 priv->memory_pool[i].free) {
299
300 priv->memory_pool[i].free = 0;
301 return priv->memory_pool[i].start;
302 }
303 }
304
305 printk(KERN_ERR "ISP1760 MEM: can not allocate %d bytes of memory\n",
306 size);
307 printk(KERN_ERR "Current memory map:\n");
308 for (i = 0; i < BLOCKS; i++) {
309 printk(KERN_ERR "Pool %2d size %4d status: %d\n",
310 i, priv->memory_pool[i].size,
311 priv->memory_pool[i].free);
312 }
313 /* XXX maybe -ENOMEM could be possible */
314 BUG();
315 return 0;
316}
317
318static void free_mem(struct isp1760_hcd *priv, u32 mem)
319{
320 int i;
321
322 if (mem == ISP1760_NULL_POINTER)
323 return;
324
325 for (i = 0; i < BLOCKS; i++) {
326 if (priv->memory_pool[i].start == mem) {
327
328 BUG_ON(priv->memory_pool[i].free);
329
330 priv->memory_pool[i].free = 1;
331 return ;
332 }
333 }
334
335 printk(KERN_ERR "Trying to free not-here-allocated memory :%08x\n",
336 mem);
337 BUG();
338}
339
340static void isp1760_init_regs(struct usb_hcd *hcd)
341{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100342 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
343 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
344 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
345 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200346
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100347 reg_write32(hcd->regs, HC_ATL_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
348 reg_write32(hcd->regs, HC_INT_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
349 reg_write32(hcd->regs, HC_ISO_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200350}
351
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100352static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200353 u32 mask, u32 done, int usec)
354{
355 u32 result;
356
357 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100358 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200359 if (result == ~0)
360 return -ENODEV;
361 result &= mask;
362 if (result == done)
363 return 0;
364 udelay(1);
365 usec--;
366 } while (usec > 0);
367 return -ETIMEDOUT;
368}
369
370/* reset a non-running (STS_HALT == 1) controller */
371static int ehci_reset(struct isp1760_hcd *priv)
372{
373 int retval;
374 struct usb_hcd *hcd = priv_to_hcd(priv);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100375 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200376
377 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100378 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200379 hcd->state = HC_STATE_HALT;
380 priv->next_statechange = jiffies;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100381 retval = handshake(hcd, HC_USBCMD,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200382 CMD_RESET, 0, 250 * 1000);
383 return retval;
384}
385
386static void qh_destroy(struct isp1760_qh *qh)
387{
388 BUG_ON(!list_empty(&qh->qtd_list));
389 kmem_cache_free(qh_cachep, qh);
390}
391
392static struct isp1760_qh *isp1760_qh_alloc(struct isp1760_hcd *priv,
393 gfp_t flags)
394{
395 struct isp1760_qh *qh;
396
397 qh = kmem_cache_zalloc(qh_cachep, flags);
398 if (!qh)
399 return qh;
400
401 INIT_LIST_HEAD(&qh->qtd_list);
402 qh->priv = priv;
403 return qh;
404}
405
406/* magic numbers that can affect system performance */
407#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
408#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
409#define EHCI_TUNE_RL_TT 0
410#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
411#define EHCI_TUNE_MULT_TT 1
412#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
413
414/* one-time init, only for memory state */
415static int priv_init(struct usb_hcd *hcd)
416{
417 struct isp1760_hcd *priv = hcd_to_priv(hcd);
418 u32 hcc_params;
419
420 spin_lock_init(&priv->lock);
421
422 /*
423 * hw default: 1K periodic list heads, one per frame.
424 * periodic_size can shrink by USBCMD update if hcc_params allows.
425 */
426 priv->periodic_size = DEFAULT_I_TDPS;
427
428 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100429 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200430 /* full frame cache */
431 if (HCC_ISOC_CACHE(hcc_params))
432 priv->i_thresh = 8;
433 else /* N microframes cached */
434 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
435
436 return 0;
437}
438
439static int isp1760_hc_setup(struct usb_hcd *hcd)
440{
441 struct isp1760_hcd *priv = hcd_to_priv(hcd);
442 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500443 u32 scratch, hwmode;
444
445 /* Setup HW Mode Control: This assumes a level active-low interrupt */
446 hwmode = HW_DATA_BUS_32BIT;
447
448 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
449 hwmode &= ~HW_DATA_BUS_32BIT;
450 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
451 hwmode |= HW_ANA_DIGI_OC;
452 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
453 hwmode |= HW_DACK_POL_HIGH;
454 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
455 hwmode |= HW_DREQ_POL_HIGH;
Michael Hennerich9da69c62009-07-15 23:22:54 -0400456 if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
457 hwmode |= HW_INTR_HIGH_ACT;
458 if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
459 hwmode |= HW_INTR_EDGE_TRIG;
Nate Case3faefc82008-06-17 11:11:38 -0500460
461 /*
462 * We have to set this first in case we're in 16-bit mode.
463 * Write it twice to ensure correct upper bits if switching
464 * to 16-bit mode.
465 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100466 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
467 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200468
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100469 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500470 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100471 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
472 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200473 if (scratch != 0xdeadbabe) {
474 printk(KERN_ERR "ISP1760: Scratch test failed.\n");
475 return -ENODEV;
476 }
477
478 /* pre reset */
479 isp1760_init_regs(hcd);
480
481 /* reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100482 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200483 mdelay(100);
484
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100485 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200486 mdelay(100);
487
488 result = ehci_reset(priv);
489 if (result)
490 return result;
491
492 /* Step 11 passed */
493
Nate Case3faefc82008-06-17 11:11:38 -0500494 isp1760_info(priv, "bus width: %d, oc: %s\n",
495 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
496 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
497 "analog" : "digital");
498
499 /* ATL reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100500 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500501 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100502 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500503
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100504 reg_write32(hcd->regs, HC_INTERRUPT_REG, INTERRUPT_ENABLE_MASK);
505 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200506
Nate Case3faefc82008-06-17 11:11:38 -0500507 /*
508 * PORT 1 Control register of the ISP1760 is the OTG control
Thomas Hommel42c65392008-12-18 10:31:40 +0100509 * register on ISP1761. Since there is no OTG or device controller
510 * support in this driver, we use port 1 as a "normal" USB host port on
511 * both chips.
Nate Case3faefc82008-06-17 11:11:38 -0500512 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100513 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
Thomas Hommel42c65392008-12-18 10:31:40 +0100514 mdelay(10);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200515
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100516 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200517
518 return priv_init(hcd);
519}
520
521static void isp1760_init_maps(struct usb_hcd *hcd)
522{
523 /*set last maps, for iso its only 1, else 32 tds bitmap*/
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100524 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
525 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
526 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200527}
528
529static void isp1760_enable_interrupts(struct usb_hcd *hcd)
530{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100531 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
532 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0);
533 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
534 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0);
535 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
536 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200537 /* step 23 passed */
538}
539
540static int isp1760_run(struct usb_hcd *hcd)
541{
542 struct isp1760_hcd *priv = hcd_to_priv(hcd);
543 int retval;
544 u32 temp;
545 u32 command;
546 u32 chipid;
547
548 hcd->uses_new_polling = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200549
550 hcd->state = HC_STATE_RUNNING;
551 isp1760_enable_interrupts(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100552 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
553 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200554
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100555 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200556 command &= ~(CMD_LRESET|CMD_RESET);
557 command |= CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100558 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200559
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100560 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200561 250 * 1000);
562 if (retval)
563 return retval;
564
565 /*
566 * XXX
567 * Spec says to write FLAG_CF as last config action, priv code grabs
568 * the semaphore while doing so.
569 */
570 down_write(&ehci_cf_port_reset_rwsem);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100571 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200572
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100573 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200574 up_write(&ehci_cf_port_reset_rwsem);
575 if (retval)
576 return retval;
577
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100578 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200579 isp1760_info(priv, "USB ISP %04x HW rev. %d started\n", chipid & 0xffff,
580 chipid >> 16);
581
582 /* PTD Register Init Part 2, Step 28 */
583 /* enable INTs */
584 isp1760_init_maps(hcd);
585
586 /* GRR this is run-once init(), being done every time the HC starts.
587 * So long as they're part of class devices, we can't do it init()
588 * since the class device isn't created that early.
589 */
590 return 0;
591}
592
593static u32 base_to_chip(u32 base)
594{
595 return ((base - 0x400) >> 3);
596}
597
598static void transform_into_atl(struct isp1760_hcd *priv, struct isp1760_qh *qh,
599 struct isp1760_qtd *qtd, struct urb *urb,
600 u32 payload, struct ptd *ptd)
601{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200602 u32 maxpacket;
603 u32 multi;
604 u32 pid_code;
605 u32 rl = RL_COUNTER;
606 u32 nak = NAK_COUNTER;
607
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100608 memset(ptd, 0, sizeof(*ptd));
609
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200610 /* according to 3.6.2, max packet len can not be > 0x400 */
611 maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
612 multi = 1 + ((maxpacket >> 11) & 0x3);
613 maxpacket &= 0x7ff;
614
615 /* DW0 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100616 ptd->dw0 = PTD_VALID;
617 ptd->dw0 |= PTD_LENGTH(qtd->length);
618 ptd->dw0 |= PTD_MAXPACKET(maxpacket);
619 ptd->dw0 |= PTD_ENDPOINT(usb_pipeendpoint(urb->pipe));
620 ptd->dw1 = usb_pipeendpoint(urb->pipe) >> 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200621
622 /* DW1 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100623 ptd->dw1 |= PTD_DEVICE_ADDR(usb_pipedevice(urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200624
625 pid_code = qtd->packet_type;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100626 ptd->dw1 |= PTD_PID_TOKEN(pid_code);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200627
628 if (usb_pipebulk(urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100629 ptd->dw1 |= PTD_TRANS_BULK;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200630 else if (usb_pipeint(urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100631 ptd->dw1 |= PTD_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200632
633 if (urb->dev->speed != USB_SPEED_HIGH) {
634 /* split transaction */
635
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100636 ptd->dw1 |= PTD_TRANS_SPLIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200637 if (urb->dev->speed == USB_SPEED_LOW)
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100638 ptd->dw1 |= PTD_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200639
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100640 ptd->dw1 |= PTD_PORT_NUM(urb->dev->ttport);
641 ptd->dw1 |= PTD_HUB_NUM(urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200642
643 /* SE bit for Split INT transfers */
644 if (usb_pipeint(urb->pipe) &&
645 (urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100646 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200647
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100648 ptd->dw3 = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200649 rl = 0;
650 nak = 0;
651 } else {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100652 ptd->dw0 |= PTD_MULTI(multi);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200653 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100654 ptd->dw3 = qh->ping;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200655 else
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100656 ptd->dw3 = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200657 }
658 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100659 ptd->dw2 = 0;
660 ptd->dw2 |= PTD_DATA_START_ADDR(base_to_chip(payload));
661 ptd->dw2 |= PTD_RL_CNT(rl);
662 ptd->dw3 |= PTD_NAC_CNT(nak);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200663
664 /* DW3 */
665 if (usb_pipecontrol(urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100666 ptd->dw3 |= PTD_DATA_TOGGLE(qtd->toggle);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200667 else
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100668 ptd->dw3 |= qh->toggle;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200669
670
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100671 ptd->dw3 |= PTD_ACTIVE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200672 /* Cerr */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100673 ptd->dw3 |= PTD_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200674}
675
676static void transform_add_int(struct isp1760_hcd *priv, struct isp1760_qh *qh,
677 struct isp1760_qtd *qtd, struct urb *urb,
678 u32 payload, struct ptd *ptd)
679{
680 u32 maxpacket;
681 u32 multi;
682 u32 numberofusofs;
683 u32 i;
684 u32 usofmask, usof;
685 u32 period;
686
687 maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
688 multi = 1 + ((maxpacket >> 11) & 0x3);
689 maxpacket &= 0x7ff;
690 /* length of the data per uframe */
691 maxpacket = multi * maxpacket;
692
693 numberofusofs = urb->transfer_buffer_length / maxpacket;
694 if (urb->transfer_buffer_length % maxpacket)
695 numberofusofs += 1;
696
697 usofmask = 1;
698 usof = 0;
699 for (i = 0; i < numberofusofs; i++) {
700 usof |= usofmask;
701 usofmask <<= 1;
702 }
703
704 if (urb->dev->speed != USB_SPEED_HIGH) {
705 /* split */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100706 ptd->dw5 = 0x1c;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200707
708 if (qh->period >= 32)
709 period = qh->period / 2;
710 else
711 period = qh->period;
712
713 } else {
714
715 if (qh->period >= 8)
716 period = qh->period/8;
717 else
718 period = qh->period;
719
720 if (period >= 32)
721 period = 16;
722
723 if (qh->period >= 8) {
724 /* millisecond period */
725 period = (period << 3);
726 } else {
727 /* usof based tranmsfers */
728 /* minimum 4 usofs */
729 usof = 0x11;
730 }
731 }
732
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100733 ptd->dw2 |= period;
734 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200735}
736
737static void transform_into_int(struct isp1760_hcd *priv, struct isp1760_qh *qh,
738 struct isp1760_qtd *qtd, struct urb *urb,
739 u32 payload, struct ptd *ptd)
740{
741 transform_into_atl(priv, qh, qtd, urb, payload, ptd);
742 transform_add_int(priv, qh, qtd, urb, payload, ptd);
743}
744
745static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len,
746 u32 token)
747{
748 int count;
749
750 qtd->data_buffer = databuffer;
751 qtd->packet_type = GET_QTD_TOKEN_TYPE(token);
752 qtd->toggle = GET_DATA_TOGGLE(token);
753
754 if (len > HC_ATL_PL_SIZE)
755 count = HC_ATL_PL_SIZE;
756 else
757 count = len;
758
759 qtd->length = count;
760 return count;
761}
762
763static int check_error(struct ptd *ptd)
764{
765 int error = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200766
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100767 if (ptd->dw3 & DW3_HALT_BIT) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200768 error = -EPIPE;
769
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100770 if (ptd->dw3 & DW3_ERROR_BIT)
Anton Vorontsov0954e1c2010-05-07 01:09:19 +0400771 pr_err("error bit is set in DW3\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200772 }
773
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100774 if (ptd->dw3 & DW3_QTD_ACTIVE) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200775 printk(KERN_ERR "transfer active bit is set DW3\n");
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100776 printk(KERN_ERR "nak counter: %d, rl: %d\n",
777 (ptd->dw3 >> 19) & 0xf, (ptd->dw2 >> 25) & 0xf);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200778 }
779
780 return error;
781}
782
783static void check_int_err_status(u32 dw4)
784{
785 u32 i;
786
787 dw4 >>= 8;
788
789 for (i = 0; i < 8; i++) {
790 switch (dw4 & 0x7) {
791 case INT_UNDERRUN:
792 printk(KERN_ERR "ERROR: under run , %d\n", i);
793 break;
794
795 case INT_EXACT:
796 printk(KERN_ERR "ERROR: transaction error, %d\n", i);
797 break;
798
799 case INT_BABBLE:
800 printk(KERN_ERR "ERROR: babble error, %d\n", i);
801 break;
802 }
803 dw4 >>= 3;
804 }
805}
806
807static void enqueue_one_qtd(struct isp1760_qtd *qtd, struct isp1760_hcd *priv,
808 u32 payload)
809{
810 u32 token;
811 struct usb_hcd *hcd = priv_to_hcd(priv);
812
813 token = qtd->packet_type;
814
815 if (qtd->length && (qtd->length <= HC_ATL_PL_SIZE)) {
816 switch (token) {
817 case IN_PID:
818 break;
819 case OUT_PID:
820 case SETUP_PID:
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100821 mem_writes8(hcd->regs, payload, qtd->data_buffer,
822 qtd->length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200823 }
824 }
825}
826
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100827static void enqueue_one_atl_qtd(u32 payload,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200828 struct isp1760_hcd *priv, struct isp1760_qh *qh,
829 struct urb *urb, u32 slot, struct isp1760_qtd *qtd)
830{
831 struct ptd ptd;
832 struct usb_hcd *hcd = priv_to_hcd(priv);
833
834 transform_into_atl(priv, qh, qtd, urb, payload, &ptd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100835 ptd_write(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200836 enqueue_one_qtd(qtd, priv, payload);
837
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200838 priv->atl_ints[slot].qh = qh;
839 priv->atl_ints[slot].qtd = qtd;
840 priv->atl_ints[slot].data_buffer = qtd->data_buffer;
841 priv->atl_ints[slot].payload = payload;
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100842 qtd->status |= URB_ENQUEUED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200843 qtd->status |= slot << 16;
844}
845
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100846static void enqueue_one_int_qtd(u32 payload,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200847 struct isp1760_hcd *priv, struct isp1760_qh *qh,
848 struct urb *urb, u32 slot, struct isp1760_qtd *qtd)
849{
850 struct ptd ptd;
851 struct usb_hcd *hcd = priv_to_hcd(priv);
852
853 transform_into_int(priv, qh, qtd, urb, payload, &ptd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100854 ptd_write(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200855 enqueue_one_qtd(qtd, priv, payload);
856
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200857 priv->int_ints[slot].qh = qh;
858 priv->int_ints[slot].qtd = qtd;
859 priv->int_ints[slot].data_buffer = qtd->data_buffer;
860 priv->int_ints[slot].payload = payload;
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100861 qtd->status |= URB_ENQUEUED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200862 qtd->status |= slot << 16;
863}
864
Adrian Bunk473bca92008-05-05 21:25:33 +0300865static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
866 struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200867{
868 struct isp1760_hcd *priv = hcd_to_priv(hcd);
869 u32 skip_map, or_map;
870 u32 queue_entry;
871 u32 slot;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100872 u32 payload;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200873 u32 buffstatus;
874
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000875 /*
876 * When this function is called from the interrupt handler to enqueue
877 * a follow-up packet, the SKIP register gets written and read back
878 * almost immediately. With ISP1761, this register requires a delay of
879 * 195ns between a write and subsequent read (see section 15.1.1.3).
880 */
Michael Hennerichebb8a4e2010-08-05 17:53:57 -0400881 mmiowb();
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000882 ndelay(195);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100883 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200884
885 BUG_ON(!skip_map);
886 slot = __ffs(skip_map);
887 queue_entry = 1 << slot;
888
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200889 payload = alloc_mem(priv, qtd->length);
890
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100891 enqueue_one_atl_qtd(payload, priv, qh, qtd->urb, slot, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200892
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100893 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200894 or_map |= queue_entry;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100895 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200896
897 skip_map &= ~queue_entry;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100898 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200899
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +0100900 priv->atl_queued++;
901 if (priv->atl_queued == 2)
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100902 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
903 INTERRUPT_ENABLE_SOT_MASK);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +0100904
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100905 buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200906 buffstatus |= ATL_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100907 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200908}
909
Adrian Bunk473bca92008-05-05 21:25:33 +0300910static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
911 struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200912{
913 struct isp1760_hcd *priv = hcd_to_priv(hcd);
914 u32 skip_map, or_map;
915 u32 queue_entry;
916 u32 slot;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100917 u32 payload;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200918 u32 buffstatus;
919
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000920 /*
921 * When this function is called from the interrupt handler to enqueue
922 * a follow-up packet, the SKIP register gets written and read back
923 * almost immediately. With ISP1761, this register requires a delay of
924 * 195ns between a write and subsequent read (see section 15.1.1.3).
925 */
Michael Hennerichebb8a4e2010-08-05 17:53:57 -0400926 mmiowb();
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000927 ndelay(195);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100928 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200929
930 BUG_ON(!skip_map);
931 slot = __ffs(skip_map);
932 queue_entry = 1 << slot;
933
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200934 payload = alloc_mem(priv, qtd->length);
935
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100936 enqueue_one_int_qtd(payload, priv, qh, qtd->urb, slot, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200937
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100938 or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200939 or_map |= queue_entry;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100940 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200941
942 skip_map &= ~queue_entry;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100943 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200944
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100945 buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200946 buffstatus |= INT_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100947 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200948}
949
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100950static void isp1760_urb_done(struct isp1760_hcd *priv, struct urb *urb,
951 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200952__releases(priv->lock)
953__acquires(priv->lock)
954{
955 if (!urb->unlinked) {
956 if (status == -EINPROGRESS)
957 status = 0;
958 }
959
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000960 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
961 void *ptr;
962 for (ptr = urb->transfer_buffer;
963 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
964 ptr += PAGE_SIZE)
965 flush_dcache_page(virt_to_page(ptr));
966 }
967
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200968 /* complete() can reenter this HCD */
969 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv), urb);
970 spin_unlock(&priv->lock);
971 usb_hcd_giveback_urb(priv_to_hcd(priv), urb, status);
972 spin_lock(&priv->lock);
973}
974
975static void isp1760_qtd_free(struct isp1760_qtd *qtd)
976{
977 kmem_cache_free(qtd_cachep, qtd);
978}
979
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100980static struct isp1760_qtd *clean_this_qtd(struct isp1760_qtd *qtd,
981 struct isp1760_qh *qh)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200982{
983 struct isp1760_qtd *tmp_qtd;
984
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100985 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
986 tmp_qtd = NULL;
987 else
988 tmp_qtd = list_entry(qtd->qtd_list.next, struct isp1760_qtd,
989 qtd_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200990 list_del(&qtd->qtd_list);
991 isp1760_qtd_free(qtd);
992 return tmp_qtd;
993}
994
995/*
996 * Remove this QTD from the QH list and free its memory. If this QTD
997 * isn't the last one than remove also his successor(s).
998 * Returns the QTD which is part of an new URB and should be enqueued.
999 */
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001000static struct isp1760_qtd *clean_up_qtdlist(struct isp1760_qtd *qtd,
1001 struct isp1760_qh *qh)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001002{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001003 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001004
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001005 urb = qtd->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001006 do {
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001007 qtd = clean_this_qtd(qtd, qh);
1008 } while (qtd && (qtd->urb == urb));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001009
1010 return qtd;
1011}
1012
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001013static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
1014{
1015 struct urb *urb;
1016
1017 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
1018 return 1;
1019
1020 urb = qtd->urb;
1021 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
1022 return (qtd->urb != urb);
1023}
1024
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001025static void do_atl_int(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001026{
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001027 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001028 u32 done_map, skip_map;
1029 struct ptd ptd;
1030 struct urb *urb = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001031 u32 queue_entry;
1032 u32 payload;
1033 u32 length;
1034 u32 or_map;
1035 u32 status = -EINVAL;
1036 int error;
1037 struct isp1760_qtd *qtd;
1038 struct isp1760_qh *qh;
1039 u32 rl;
1040 u32 nakcount;
1041
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001042 done_map = reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1043 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001044
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001045 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001046 or_map &= ~done_map;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001047 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001048
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001049 while (done_map) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001050 status = 0;
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001051 priv->atl_queued--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001052
1053 queue_entry = __ffs(done_map);
1054 done_map &= ~(1 << queue_entry);
1055 skip_map |= 1 << queue_entry;
1056
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001057 qtd = priv->atl_ints[queue_entry].qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001058 urb = qtd->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001059 qh = priv->atl_ints[queue_entry].qh;
1060 payload = priv->atl_ints[queue_entry].payload;
1061
1062 if (!qh) {
1063 printk(KERN_ERR "qh is 0\n");
1064 continue;
1065 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001066 ptd_read(hcd->regs, ATL_PTD_OFFSET, queue_entry, &ptd);
Enrico Scholz3f02a952008-07-17 20:09:30 +02001067
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001068 rl = (ptd.dw2 >> 25) & 0x0f;
1069 nakcount = (ptd.dw3 >> 19) & 0xf;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001070
1071 /* Transfer Error, *but* active and no HALT -> reload */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001072 if ((ptd.dw3 & DW3_ERROR_BIT) && (ptd.dw3 & DW3_QTD_ACTIVE) &&
1073 !(ptd.dw3 & DW3_HALT_BIT)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001074
1075 /* according to ppriv code, we have to
1076 * reload this one if trasfered bytes != requested bytes
1077 * else act like everything went smooth..
1078 * XXX This just doesn't feel right and hasn't
1079 * triggered so far.
1080 */
1081
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001082 length = PTD_XFERRED_LENGTH(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001083 printk(KERN_ERR "Should reload now.... transfered %d "
1084 "of %zu\n", length, qtd->length);
1085 BUG();
1086 }
1087
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001088 if (!nakcount && (ptd.dw3 & DW3_QTD_ACTIVE)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001089 u32 buffstatus;
1090
Colin Tuckleyc0d74142010-01-07 11:22:47 +00001091 /*
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001092 * NAKs are handled in HW by the chip. Usually if the
1093 * device is not able to send data fast enough.
Colin Tuckleyc0d74142010-01-07 11:22:47 +00001094 * This happens mostly on slower hardware.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001095 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001096
1097 /* RL counter = ERR counter */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001098 ptd.dw3 &= ~(0xf << 19);
1099 ptd.dw3 |= rl << 19;
1100 ptd.dw3 &= ~(3 << (55 - 32));
1101 ptd.dw3 |= ERR_COUNTER << (55 - 32);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001102
1103 /*
1104 * It is not needed to write skip map back because it
1105 * is unchanged. Just make sure that this entry is
1106 * unskipped once it gets written to the HW.
1107 */
1108 skip_map &= ~(1 << queue_entry);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001109 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001110 or_map |= 1 << queue_entry;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001111 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001112
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001113 ptd.dw0 |= PTD_VALID;
1114 ptd_write(hcd->regs, ATL_PTD_OFFSET, queue_entry, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001115
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001116 priv->atl_queued++;
1117 if (priv->atl_queued == 2)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001118 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1119 INTERRUPT_ENABLE_SOT_MASK);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001120
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001121 buffstatus = reg_read32(hcd->regs,
1122 HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001123 buffstatus |= ATL_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001124 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1125 buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001126 continue;
1127 }
1128
1129 error = check_error(&ptd);
1130 if (error) {
1131 status = error;
1132 priv->atl_ints[queue_entry].qh->toggle = 0;
1133 priv->atl_ints[queue_entry].qh->ping = 0;
1134 urb->status = -EPIPE;
1135
1136#if 0
1137 printk(KERN_ERR "Error in %s().\n", __func__);
1138 printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1139 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1140 "%08x dw7: %08x\n",
1141 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1142 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1143#endif
1144 } else {
1145 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001146 priv->atl_ints[queue_entry].qh->toggle =
1147 ptd.dw3 & (1 << 25);
1148 priv->atl_ints[queue_entry].qh->ping =
1149 ptd.dw3 & (1 << 26);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001150 }
1151 }
1152
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001153 length = PTD_XFERRED_LENGTH(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001154 if (length) {
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001155 switch (DW1_GET_PID(ptd.dw1)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001156 case IN_PID:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001157 mem_reads8(hcd->regs, payload,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001158 priv->atl_ints[queue_entry].data_buffer,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001159 length);
1160
1161 case OUT_PID:
1162
1163 urb->actual_length += length;
1164
1165 case SETUP_PID:
1166 break;
1167 }
1168 }
1169
1170 priv->atl_ints[queue_entry].data_buffer = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001171 priv->atl_ints[queue_entry].qtd = NULL;
1172 priv->atl_ints[queue_entry].qh = NULL;
1173
1174 free_mem(priv, payload);
1175
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001176 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001177
1178 if (urb->status == -EPIPE) {
1179 /* HALT was received */
1180
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001181 qtd = clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001182 isp1760_urb_done(priv, urb, urb->status);
1183
1184 } else if (usb_pipebulk(urb->pipe) && (length < qtd->length)) {
1185 /* short BULK received */
1186
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001187 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1188 urb->status = -EREMOTEIO;
Sebastian Siewior7839b512008-07-17 20:09:29 +02001189 isp1760_dbg(priv, "short bulk, %d instead %zu "
1190 "with URB_SHORT_NOT_OK flag.\n",
1191 length, qtd->length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001192 }
1193
1194 if (urb->status == -EINPROGRESS)
1195 urb->status = 0;
1196
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001197 qtd = clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001198
1199 isp1760_urb_done(priv, urb, urb->status);
1200
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001201 } else if (last_qtd_of_urb(qtd, qh)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001202 /* that was the last qtd of that URB */
1203
1204 if (urb->status == -EINPROGRESS)
1205 urb->status = 0;
1206
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001207 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001208 isp1760_urb_done(priv, urb, urb->status);
1209
1210 } else {
1211 /* next QTD of this URB */
1212
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001213 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001214 BUG_ON(!qtd);
1215 }
1216
1217 if (qtd)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001218 enqueue_an_ATL_packet(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001219
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001220 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001221 }
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001222 if (priv->atl_queued <= 1)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001223 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1224 INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001225}
1226
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001227static void do_intl_int(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001228{
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001229 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001230 u32 done_map, skip_map;
1231 struct ptd ptd;
1232 struct urb *urb = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001233 u32 payload;
1234 u32 length;
1235 u32 or_map;
1236 int error;
1237 u32 queue_entry;
1238 struct isp1760_qtd *qtd;
1239 struct isp1760_qh *qh;
1240
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001241 done_map = reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1242 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001243
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001244 or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001245 or_map &= ~done_map;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001246 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001247
1248 while (done_map) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001249 queue_entry = __ffs(done_map);
1250 done_map &= ~(1 << queue_entry);
1251 skip_map |= 1 << queue_entry;
1252
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001253 qtd = priv->int_ints[queue_entry].qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001254 urb = qtd->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001255 qh = priv->int_ints[queue_entry].qh;
1256 payload = priv->int_ints[queue_entry].payload;
1257
1258 if (!qh) {
1259 printk(KERN_ERR "(INT) qh is 0\n");
1260 continue;
1261 }
1262
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001263 ptd_read(hcd->regs, INT_PTD_OFFSET, queue_entry, &ptd);
1264 check_int_err_status(ptd.dw4);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001265
1266 error = check_error(&ptd);
1267 if (error) {
1268#if 0
1269 printk(KERN_ERR "Error in %s().\n", __func__);
1270 printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1271 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1272 "%08x dw7: %08x\n",
1273 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1274 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1275#endif
1276 urb->status = -EPIPE;
1277 priv->int_ints[queue_entry].qh->toggle = 0;
1278 priv->int_ints[queue_entry].qh->ping = 0;
1279
1280 } else {
1281 priv->int_ints[queue_entry].qh->toggle =
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001282 ptd.dw3 & (1 << 25);
1283 priv->int_ints[queue_entry].qh->ping =
1284 ptd.dw3 & (1 << 26);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001285 }
1286
1287 if (urb->dev->speed != USB_SPEED_HIGH)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001288 length = PTD_XFERRED_LENGTH_LO(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001289 else
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001290 length = PTD_XFERRED_LENGTH(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001291
1292 if (length) {
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001293 switch (DW1_GET_PID(ptd.dw1)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001294 case IN_PID:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001295 mem_reads8(hcd->regs, payload,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001296 priv->int_ints[queue_entry].data_buffer,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001297 length);
1298 case OUT_PID:
1299
1300 urb->actual_length += length;
1301
1302 case SETUP_PID:
1303 break;
1304 }
1305 }
1306
1307 priv->int_ints[queue_entry].data_buffer = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001308 priv->int_ints[queue_entry].qtd = NULL;
1309 priv->int_ints[queue_entry].qh = NULL;
1310
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001311 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001312 free_mem(priv, payload);
1313
1314 if (urb->status == -EPIPE) {
1315 /* HALT received */
1316
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001317 qtd = clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001318 isp1760_urb_done(priv, urb, urb->status);
1319
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001320 } else if (last_qtd_of_urb(qtd, qh)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001321
1322 if (urb->status == -EINPROGRESS)
1323 urb->status = 0;
1324
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001325 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001326 isp1760_urb_done(priv, urb, urb->status);
1327
1328 } else {
1329 /* next QTD of this URB */
1330
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001331 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001332 BUG_ON(!qtd);
1333 }
1334
1335 if (qtd)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001336 enqueue_an_INT_packet(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001337
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001338 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001339 }
1340}
1341
1342#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1343static struct isp1760_qh *qh_make(struct isp1760_hcd *priv, struct urb *urb,
1344 gfp_t flags)
1345{
1346 struct isp1760_qh *qh;
1347 int is_input, type;
1348
1349 qh = isp1760_qh_alloc(priv, flags);
1350 if (!qh)
1351 return qh;
1352
1353 /*
1354 * init endpoint/device data for this QH
1355 */
1356 is_input = usb_pipein(urb->pipe);
1357 type = usb_pipetype(urb->pipe);
1358
1359 if (type == PIPE_INTERRUPT) {
1360
1361 if (urb->dev->speed == USB_SPEED_HIGH) {
1362
1363 qh->period = urb->interval >> 3;
1364 if (qh->period == 0 && urb->interval != 1) {
1365 /* NOTE interval 2 or 4 uframes could work.
1366 * But interval 1 scheduling is simpler, and
1367 * includes high bandwidth.
1368 */
1369 printk(KERN_ERR "intr period %d uframes, NYET!",
1370 urb->interval);
1371 qh_destroy(qh);
1372 return NULL;
1373 }
1374 } else {
1375 qh->period = urb->interval;
1376 }
1377 }
1378
1379 /* support for tt scheduling, and access to toggles */
1380 qh->dev = urb->dev;
1381
1382 if (!usb_pipecontrol(urb->pipe))
1383 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input,
1384 1);
1385 return qh;
1386}
1387
1388/*
1389 * For control/bulk/interrupt, return QH with these TDs appended.
1390 * Allocates and initializes the QH if necessary.
1391 * Returns null if it can't allocate a QH it needs to.
1392 * If the QH has TDs (urbs) already, that's great.
1393 */
1394static struct isp1760_qh *qh_append_tds(struct isp1760_hcd *priv,
1395 struct urb *urb, struct list_head *qtd_list, int epnum,
1396 void **ptr)
1397{
1398 struct isp1760_qh *qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001399
1400 qh = (struct isp1760_qh *)*ptr;
1401 if (!qh) {
1402 /* can't sleep here, we have priv->lock... */
1403 qh = qh_make(priv, urb, GFP_ATOMIC);
1404 if (!qh)
1405 return qh;
1406 *ptr = qh;
1407 }
1408
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001409 list_splice(qtd_list, qh->qtd_list.prev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001410
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001411 return qh;
1412}
1413
1414static void qtd_list_free(struct isp1760_hcd *priv, struct urb *urb,
1415 struct list_head *qtd_list)
1416{
1417 struct list_head *entry, *temp;
1418
1419 list_for_each_safe(entry, temp, qtd_list) {
1420 struct isp1760_qtd *qtd;
1421
1422 qtd = list_entry(entry, struct isp1760_qtd, qtd_list);
1423 list_del(&qtd->qtd_list);
1424 isp1760_qtd_free(qtd);
1425 }
1426}
1427
1428static int isp1760_prepare_enqueue(struct isp1760_hcd *priv, struct urb *urb,
1429 struct list_head *qtd_list, gfp_t mem_flags, packet_enqueue *p)
1430{
1431 struct isp1760_qtd *qtd;
1432 int epnum;
1433 unsigned long flags;
1434 struct isp1760_qh *qh = NULL;
1435 int rc;
1436 int qh_busy;
1437
1438 qtd = list_entry(qtd_list->next, struct isp1760_qtd, qtd_list);
1439 epnum = urb->ep->desc.bEndpointAddress;
1440
1441 spin_lock_irqsave(&priv->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001442 if (!HCD_HW_ACCESSIBLE(priv_to_hcd(priv))) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001443 rc = -ESHUTDOWN;
1444 goto done;
1445 }
1446 rc = usb_hcd_link_urb_to_ep(priv_to_hcd(priv), urb);
1447 if (rc)
1448 goto done;
1449
1450 qh = urb->ep->hcpriv;
1451 if (qh)
1452 qh_busy = !list_empty(&qh->qtd_list);
1453 else
1454 qh_busy = 0;
1455
1456 qh = qh_append_tds(priv, urb, qtd_list, epnum, &urb->ep->hcpriv);
1457 if (!qh) {
1458 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv), urb);
1459 rc = -ENOMEM;
1460 goto done;
1461 }
1462
1463 if (!qh_busy)
1464 p(priv_to_hcd(priv), qh, qtd);
1465
1466done:
1467 spin_unlock_irqrestore(&priv->lock, flags);
1468 if (!qh)
1469 qtd_list_free(priv, urb, qtd_list);
1470 return rc;
1471}
1472
1473static struct isp1760_qtd *isp1760_qtd_alloc(struct isp1760_hcd *priv,
1474 gfp_t flags)
1475{
1476 struct isp1760_qtd *qtd;
1477
1478 qtd = kmem_cache_zalloc(qtd_cachep, flags);
1479 if (qtd)
1480 INIT_LIST_HEAD(&qtd->qtd_list);
1481
1482 return qtd;
1483}
1484
1485/*
1486 * create a list of filled qtds for this URB; won't link into qh.
1487 */
1488static struct list_head *qh_urb_transaction(struct isp1760_hcd *priv,
1489 struct urb *urb, struct list_head *head, gfp_t flags)
1490{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001491 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001492 void *buf;
1493 int len, maxpacket;
1494 int is_input;
1495 u32 token;
1496
1497 /*
1498 * URBs map to sequences of QTDs: one logical transaction
1499 */
1500 qtd = isp1760_qtd_alloc(priv, flags);
1501 if (!qtd)
1502 return NULL;
1503
1504 list_add_tail(&qtd->qtd_list, head);
1505 qtd->urb = urb;
1506 urb->status = -EINPROGRESS;
1507
1508 token = 0;
1509 /* for split transactions, SplitXState initialized to zero */
1510
1511 len = urb->transfer_buffer_length;
1512 is_input = usb_pipein(urb->pipe);
1513 if (usb_pipecontrol(urb->pipe)) {
1514 /* SETUP pid */
1515 qtd_fill(qtd, urb->setup_packet,
1516 sizeof(struct usb_ctrlrequest),
1517 token | SETUP_PID);
1518
1519 /* ... and always at least one more pid */
1520 token ^= DATA_TOGGLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001521 qtd = isp1760_qtd_alloc(priv, flags);
1522 if (!qtd)
1523 goto cleanup;
1524 qtd->urb = urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001525 list_add_tail(&qtd->qtd_list, head);
1526
1527 /* for zero length DATA stages, STATUS is always IN */
1528 if (len == 0)
1529 token |= IN_PID;
1530 }
1531
1532 /*
1533 * data transfer stage: buffer setup
1534 */
1535 buf = urb->transfer_buffer;
1536
1537 if (is_input)
1538 token |= IN_PID;
1539 else
1540 token |= OUT_PID;
1541
1542 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
1543
1544 /*
1545 * buffer gets wrapped in one or more qtds;
1546 * last one may be "short" (including zero len)
1547 * and may serve as a control status ack
1548 */
1549 for (;;) {
1550 int this_qtd_len;
1551
1552 if (!buf && len) {
1553 /* XXX This looks like usb storage / SCSI bug */
1554 printk(KERN_ERR "buf is null, dma is %08lx len is %d\n",
1555 (long unsigned)urb->transfer_dma, len);
1556 WARN_ON(1);
1557 }
1558
1559 this_qtd_len = qtd_fill(qtd, buf, len, token);
1560 len -= this_qtd_len;
1561 buf += this_qtd_len;
1562
1563 /* qh makes control packets use qtd toggle; maybe switch it */
1564 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
1565 token ^= DATA_TOGGLE;
1566
1567 if (len <= 0)
1568 break;
1569
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001570 qtd = isp1760_qtd_alloc(priv, flags);
1571 if (!qtd)
1572 goto cleanup;
1573 qtd->urb = urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001574 list_add_tail(&qtd->qtd_list, head);
1575 }
1576
1577 /*
1578 * control requests may need a terminating data "status" ack;
1579 * bulk ones may need a terminating short packet (zero length).
1580 */
1581 if (urb->transfer_buffer_length != 0) {
1582 int one_more = 0;
1583
1584 if (usb_pipecontrol(urb->pipe)) {
1585 one_more = 1;
1586 /* "in" <--> "out" */
1587 token ^= IN_PID;
1588 /* force DATA1 */
1589 token |= DATA_TOGGLE;
1590 } else if (usb_pipebulk(urb->pipe)
1591 && (urb->transfer_flags & URB_ZERO_PACKET)
1592 && !(urb->transfer_buffer_length % maxpacket)) {
1593 one_more = 1;
1594 }
1595 if (one_more) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001596 qtd = isp1760_qtd_alloc(priv, flags);
1597 if (!qtd)
1598 goto cleanup;
1599 qtd->urb = urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001600 list_add_tail(&qtd->qtd_list, head);
1601
1602 /* never any data in such packets */
1603 qtd_fill(qtd, NULL, 0, token);
1604 }
1605 }
1606
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001607 qtd->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001608 return head;
1609
1610cleanup:
1611 qtd_list_free(priv, urb, head);
1612 return NULL;
1613}
1614
1615static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1616 gfp_t mem_flags)
1617{
1618 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1619 struct list_head qtd_list;
1620 packet_enqueue *pe;
1621
1622 INIT_LIST_HEAD(&qtd_list);
1623
1624 switch (usb_pipetype(urb->pipe)) {
1625 case PIPE_CONTROL:
1626 case PIPE_BULK:
1627
1628 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1629 return -ENOMEM;
1630 pe = enqueue_an_ATL_packet;
1631 break;
1632
1633 case PIPE_INTERRUPT:
1634 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1635 return -ENOMEM;
1636 pe = enqueue_an_INT_packet;
1637 break;
1638
1639 case PIPE_ISOCHRONOUS:
1640 printk(KERN_ERR "PIPE_ISOCHRONOUS ain't supported\n");
1641 default:
1642 return -EPIPE;
1643 }
1644
Sebastian Siewiora36c27d2008-07-17 20:09:28 +02001645 return isp1760_prepare_enqueue(priv, urb, &qtd_list, mem_flags, pe);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001646}
1647
1648static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1649 int status)
1650{
1651 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1652 struct inter_packet_info *ints;
1653 u32 i;
1654 u32 reg_base, or_reg, skip_reg;
Andrew Mortond249afd2008-06-09 16:39:52 -07001655 unsigned long flags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001656 struct ptd ptd;
Warren Free0afb20e2009-05-08 10:27:08 +02001657 packet_enqueue *pe;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001658
1659 switch (usb_pipetype(urb->pipe)) {
1660 case PIPE_ISOCHRONOUS:
1661 return -EPIPE;
1662 break;
1663
1664 case PIPE_INTERRUPT:
1665 ints = priv->int_ints;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001666 reg_base = INT_PTD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001667 or_reg = HC_INT_IRQ_MASK_OR_REG;
1668 skip_reg = HC_INT_PTD_SKIPMAP_REG;
Warren Free0afb20e2009-05-08 10:27:08 +02001669 pe = enqueue_an_INT_packet;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001670 break;
1671
1672 default:
1673 ints = priv->atl_ints;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001674 reg_base = ATL_PTD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001675 or_reg = HC_ATL_IRQ_MASK_OR_REG;
1676 skip_reg = HC_ATL_PTD_SKIPMAP_REG;
Warren Free0afb20e2009-05-08 10:27:08 +02001677 pe = enqueue_an_ATL_packet;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001678 break;
1679 }
1680
1681 memset(&ptd, 0, sizeof(ptd));
1682 spin_lock_irqsave(&priv->lock, flags);
1683
1684 for (i = 0; i < 32; i++) {
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001685 if (!ints[i].qh)
1686 continue;
1687 BUG_ON(!ints[i].qtd);
1688
1689 if (ints[i].qtd->urb == urb) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001690 u32 skip_map;
1691 u32 or_map;
1692 struct isp1760_qtd *qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001693 struct isp1760_qh *qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001694
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001695 skip_map = reg_read32(hcd->regs, skip_reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001696 skip_map |= 1 << i;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001697 reg_write32(hcd->regs, skip_reg, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001698
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001699 or_map = reg_read32(hcd->regs, or_reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001700 or_map &= ~(1 << i);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001701 reg_write32(hcd->regs, or_reg, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001702
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001703 ptd_write(hcd->regs, reg_base, i, &ptd);
1704
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001705 qtd = ints->qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001706 qh = ints[i].qh;
1707 qtd = clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001708
1709 free_mem(priv, ints->payload);
1710
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001711 ints->qh = NULL;
1712 ints->qtd = NULL;
1713 ints->data_buffer = NULL;
1714 ints->payload = 0;
1715
1716 isp1760_urb_done(priv, urb, status);
Warren Free0afb20e2009-05-08 10:27:08 +02001717 if (qtd)
1718 pe(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001719 break;
Warren Free0afb20e2009-05-08 10:27:08 +02001720
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001721 } else {
1722 struct isp1760_qtd *qtd;
Warren Free0afb20e2009-05-08 10:27:08 +02001723
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001724 list_for_each_entry(qtd, &ints[i].qtd->qtd_list,
1725 qtd_list) {
Warren Free0afb20e2009-05-08 10:27:08 +02001726 if (qtd->urb == urb) {
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001727 clean_up_qtdlist(qtd, ints[i].qh);
Warren Free0afb20e2009-05-08 10:27:08 +02001728 isp1760_urb_done(priv, urb, status);
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001729 qtd = NULL;
Warren Free0afb20e2009-05-08 10:27:08 +02001730 break;
1731 }
Warren Free0afb20e2009-05-08 10:27:08 +02001732 }
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001733
1734 /* We found the urb before the last slot */
1735 if (!qtd)
Warren Free0afb20e2009-05-08 10:27:08 +02001736 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001737 }
1738 ints++;
1739 }
1740
1741 spin_unlock_irqrestore(&priv->lock, flags);
1742 return 0;
1743}
1744
1745static irqreturn_t isp1760_irq(struct usb_hcd *usb_hcd)
1746{
1747 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
1748 u32 imask;
1749 irqreturn_t irqret = IRQ_NONE;
1750
1751 spin_lock(&priv->lock);
1752
1753 if (!(usb_hcd->state & HC_STATE_RUNNING))
1754 goto leave;
1755
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001756 imask = reg_read32(usb_hcd->regs, HC_INTERRUPT_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001757 if (unlikely(!imask))
1758 goto leave;
1759
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001760 reg_write32(usb_hcd->regs, HC_INTERRUPT_REG, imask);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001761 if (imask & (HC_ATL_INT | HC_SOT_INT))
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001762 do_atl_int(usb_hcd);
1763
1764 if (imask & HC_INTL_INT)
1765 do_intl_int(usb_hcd);
1766
1767 irqret = IRQ_HANDLED;
1768leave:
1769 spin_unlock(&priv->lock);
1770 return irqret;
1771}
1772
1773static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1774{
1775 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1776 u32 temp, status = 0;
1777 u32 mask;
1778 int retval = 1;
1779 unsigned long flags;
1780
1781 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1782 if (!HC_IS_RUNNING(hcd->state))
1783 return 0;
1784
1785 /* init status to no-changes */
1786 buf[0] = 0;
1787 mask = PORT_CSC;
1788
1789 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001790 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001791
1792 if (temp & PORT_OWNER) {
1793 if (temp & PORT_CSC) {
1794 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001795 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001796 goto done;
1797 }
1798 }
1799
1800 /*
1801 * Return status information even for ports with OWNER set.
1802 * Otherwise khubd wouldn't see the disconnect event when a
1803 * high-speed device is switched over to the companion
1804 * controller by the user.
1805 */
1806
1807 if ((temp & mask) != 0
1808 || ((temp & PORT_RESUME) != 0
1809 && time_after_eq(jiffies,
1810 priv->reset_done))) {
1811 buf [0] |= 1 << (0 + 1);
1812 status = STS_PCD;
1813 }
1814 /* FIXME autosuspend idle root hubs */
1815done:
1816 spin_unlock_irqrestore(&priv->lock, flags);
1817 return status ? retval : 0;
1818}
1819
1820static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1821 struct usb_hub_descriptor *desc)
1822{
1823 int ports = HCS_N_PORTS(priv->hcs_params);
1824 u16 temp;
1825
1826 desc->bDescriptorType = 0x29;
1827 /* priv 1.0, 2.3.9 says 20ms max */
1828 desc->bPwrOn2PwrGood = 10;
1829 desc->bHubContrCurrent = 0;
1830
1831 desc->bNbrPorts = ports;
1832 temp = 1 + (ports / 8);
1833 desc->bDescLength = 7 + 2 * temp;
1834
1835 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1836 memset(&desc->bitmap[0], 0, temp);
1837 memset(&desc->bitmap[temp], 0xff, temp);
1838
1839 /* per-port overcurrent reporting */
1840 temp = 0x0008;
1841 if (HCS_PPC(priv->hcs_params))
1842 /* per-port power control */
1843 temp |= 0x0001;
1844 else
1845 /* no power switching */
1846 temp |= 0x0002;
1847 desc->wHubCharacteristics = cpu_to_le16(temp);
1848}
1849
1850#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1851
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001852static int check_reset_complete(struct usb_hcd *hcd, int index,
1853 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001854{
1855 if (!(port_status & PORT_CONNECT))
1856 return port_status;
1857
1858 /* if reset finished and it's still not enabled -- handoff */
1859 if (!(port_status & PORT_PE)) {
1860
1861 printk(KERN_ERR "port %d full speed --> companion\n",
1862 index + 1);
1863
1864 port_status |= PORT_OWNER;
1865 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001866 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001867
1868 } else
1869 printk(KERN_ERR "port %d high speed\n", index + 1);
1870
1871 return port_status;
1872}
1873
1874static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1875 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1876{
1877 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1878 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001879 u32 temp, status;
1880 unsigned long flags;
1881 int retval = 0;
1882 unsigned selector;
1883
1884 /*
1885 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1886 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1887 * (track current state ourselves) ... blink for diagnostics,
1888 * power, "this is the one", etc. EHCI spec supports this.
1889 */
1890
1891 spin_lock_irqsave(&priv->lock, flags);
1892 switch (typeReq) {
1893 case ClearHubFeature:
1894 switch (wValue) {
1895 case C_HUB_LOCAL_POWER:
1896 case C_HUB_OVER_CURRENT:
1897 /* no hub-wide feature/status flags */
1898 break;
1899 default:
1900 goto error;
1901 }
1902 break;
1903 case ClearPortFeature:
1904 if (!wIndex || wIndex > ports)
1905 goto error;
1906 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001907 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001908
1909 /*
1910 * Even if OWNER is set, so the port is owned by the
1911 * companion controller, khubd needs to be able to clear
1912 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001913 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001914 */
1915
1916 switch (wValue) {
1917 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001918 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001919 break;
1920 case USB_PORT_FEAT_C_ENABLE:
1921 /* XXX error? */
1922 break;
1923 case USB_PORT_FEAT_SUSPEND:
1924 if (temp & PORT_RESET)
1925 goto error;
1926
1927 if (temp & PORT_SUSPEND) {
1928 if ((temp & PORT_PE) == 0)
1929 goto error;
1930 /* resume signaling for 20 msec */
1931 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001932 reg_write32(hcd->regs, HC_PORTSC1,
1933 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001934 priv->reset_done = jiffies +
1935 msecs_to_jiffies(20);
1936 }
1937 break;
1938 case USB_PORT_FEAT_C_SUSPEND:
1939 /* we auto-clear this feature */
1940 break;
1941 case USB_PORT_FEAT_POWER:
1942 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001943 reg_write32(hcd->regs, HC_PORTSC1,
1944 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001945 break;
1946 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001947 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001948 break;
1949 case USB_PORT_FEAT_C_OVER_CURRENT:
1950 /* XXX error ?*/
1951 break;
1952 case USB_PORT_FEAT_C_RESET:
1953 /* GetPortStatus clears reset */
1954 break;
1955 default:
1956 goto error;
1957 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001958 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001959 break;
1960 case GetHubDescriptor:
1961 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1962 buf);
1963 break;
1964 case GetHubStatus:
1965 /* no hub-wide feature/status flags */
1966 memset(buf, 0, 4);
1967 break;
1968 case GetPortStatus:
1969 if (!wIndex || wIndex > ports)
1970 goto error;
1971 wIndex--;
1972 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001973 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001974
1975 /* wPortChange bits */
1976 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001977 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001978
1979
1980 /* whoever resumes must GetPortStatus to complete it!! */
1981 if (temp & PORT_RESUME) {
1982 printk(KERN_ERR "Port resume should be skipped.\n");
1983
1984 /* Remote Wakeup received? */
1985 if (!priv->reset_done) {
1986 /* resume signaling for 20 msec */
1987 priv->reset_done = jiffies
1988 + msecs_to_jiffies(20);
1989 /* check the port again */
1990 mod_timer(&priv_to_hcd(priv)->rh_timer,
1991 priv->reset_done);
1992 }
1993
1994 /* resume completed? */
1995 else if (time_after_eq(jiffies,
1996 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001997 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001998 priv->reset_done = 0;
1999
2000 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002001 temp = reg_read32(hcd->regs, HC_PORTSC1);
2002 reg_write32(hcd->regs, HC_PORTSC1,
2003 temp & ~(PORT_RWC_BITS | PORT_RESUME));
2004 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002005 PORT_RESUME, 0, 2000 /* 2msec */);
2006 if (retval != 0) {
2007 isp1760_err(priv,
2008 "port %d resume error %d\n",
2009 wIndex + 1, retval);
2010 goto error;
2011 }
2012 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
2013 }
2014 }
2015
2016 /* whoever resets must GetPortStatus to complete it!! */
2017 if ((temp & PORT_RESET)
2018 && time_after_eq(jiffies,
2019 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05002020 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002021 priv->reset_done = 0;
2022
2023 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002024 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002025 /* REVISIT: some hardware needs 550+ usec to clear
2026 * this bit; seems too long to spin routinely...
2027 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002028 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002029 PORT_RESET, 0, 750);
2030 if (retval != 0) {
2031 isp1760_err(priv, "port %d reset error %d\n",
2032 wIndex + 1, retval);
2033 goto error;
2034 }
2035
2036 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002037 temp = check_reset_complete(hcd, wIndex,
2038 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002039 }
2040 /*
2041 * Even if OWNER is set, there's no harm letting khubd
2042 * see the wPortStatus values (they should all be 0 except
2043 * for PORT_POWER anyway).
2044 */
2045
2046 if (temp & PORT_OWNER)
2047 printk(KERN_ERR "Warning: PORT_OWNER is set\n");
2048
2049 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05002050 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002051 /* status may be from integrated TT */
2052 status |= ehci_port_speed(priv, temp);
2053 }
2054 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05002055 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002056 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05002057 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002058 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05002059 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002060 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05002061 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002062
2063 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2064 break;
2065 case SetHubFeature:
2066 switch (wValue) {
2067 case C_HUB_LOCAL_POWER:
2068 case C_HUB_OVER_CURRENT:
2069 /* no hub-wide feature/status flags */
2070 break;
2071 default:
2072 goto error;
2073 }
2074 break;
2075 case SetPortFeature:
2076 selector = wIndex >> 8;
2077 wIndex &= 0xff;
2078 if (!wIndex || wIndex > ports)
2079 goto error;
2080 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002081 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002082 if (temp & PORT_OWNER)
2083 break;
2084
2085/* temp &= ~PORT_RWC_BITS; */
2086 switch (wValue) {
2087 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002088 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002089 break;
2090
2091 case USB_PORT_FEAT_SUSPEND:
2092 if ((temp & PORT_PE) == 0
2093 || (temp & PORT_RESET) != 0)
2094 goto error;
2095
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002096 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002097 break;
2098 case USB_PORT_FEAT_POWER:
2099 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002100 reg_write32(hcd->regs, HC_PORTSC1,
2101 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002102 break;
2103 case USB_PORT_FEAT_RESET:
2104 if (temp & PORT_RESUME)
2105 goto error;
2106 /* line status bits may report this as low speed,
2107 * which can be fine if this root hub has a
2108 * transaction translator built in.
2109 */
2110 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2111 && PORT_USB11(temp)) {
2112 temp |= PORT_OWNER;
2113 } else {
2114 temp |= PORT_RESET;
2115 temp &= ~PORT_PE;
2116
2117 /*
2118 * caller must wait, then call GetPortStatus
2119 * usb 2.0 spec says 50 ms resets on root
2120 */
2121 priv->reset_done = jiffies +
2122 msecs_to_jiffies(50);
2123 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002124 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002125 break;
2126 default:
2127 goto error;
2128 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002129 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002130 break;
2131
2132 default:
2133error:
2134 /* "stall" on error */
2135 retval = -EPIPE;
2136 }
2137 spin_unlock_irqrestore(&priv->lock, flags);
2138 return retval;
2139}
2140
2141static void isp1760_endpoint_disable(struct usb_hcd *usb_hcd,
2142 struct usb_host_endpoint *ep)
2143{
2144 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
2145 struct isp1760_qh *qh;
2146 struct isp1760_qtd *qtd;
Andrew Mortond249afd2008-06-09 16:39:52 -07002147 unsigned long flags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002148
2149 spin_lock_irqsave(&priv->lock, flags);
2150 qh = ep->hcpriv;
2151 if (!qh)
2152 goto out;
2153
2154 ep->hcpriv = NULL;
2155 do {
2156 /* more than entry might get removed */
2157 if (list_empty(&qh->qtd_list))
2158 break;
2159
2160 qtd = list_first_entry(&qh->qtd_list, struct isp1760_qtd,
2161 qtd_list);
2162
2163 if (qtd->status & URB_ENQUEUED) {
2164
2165 spin_unlock_irqrestore(&priv->lock, flags);
2166 isp1760_urb_dequeue(usb_hcd, qtd->urb, -ECONNRESET);
2167 spin_lock_irqsave(&priv->lock, flags);
2168 } else {
2169 struct urb *urb;
2170
2171 urb = qtd->urb;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01002172 clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002173 isp1760_urb_done(priv, urb, -ECONNRESET);
2174 }
2175 } while (1);
2176
2177 qh_destroy(qh);
2178 /* remove requests and leak them.
2179 * ATL are pretty fast done, INT could take a while...
2180 * The latter shoule be removed
2181 */
2182out:
2183 spin_unlock_irqrestore(&priv->lock, flags);
2184}
2185
2186static int isp1760_get_frame(struct usb_hcd *hcd)
2187{
2188 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2189 u32 fr;
2190
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002191 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002192 return (fr >> 3) % priv->periodic_size;
2193}
2194
2195static void isp1760_stop(struct usb_hcd *hcd)
2196{
2197 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002198 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002199
2200 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2201 NULL, 0);
2202 mdelay(20);
2203
2204 spin_lock_irq(&priv->lock);
2205 ehci_reset(priv);
2206 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002207 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2208 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002209 spin_unlock_irq(&priv->lock);
2210
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002211 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002212}
2213
2214static void isp1760_shutdown(struct usb_hcd *hcd)
2215{
Nate Case3faefc82008-06-17 11:11:38 -05002216 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002217
2218 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002219 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2220 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002221
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002222 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002223 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002224 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002225}
2226
2227static const struct hc_driver isp1760_hc_driver = {
2228 .description = "isp1760-hcd",
2229 .product_desc = "NXP ISP1760 USB Host Controller",
2230 .hcd_priv_size = sizeof(struct isp1760_hcd),
2231 .irq = isp1760_irq,
2232 .flags = HCD_MEMORY | HCD_USB2,
2233 .reset = isp1760_hc_setup,
2234 .start = isp1760_run,
2235 .stop = isp1760_stop,
2236 .shutdown = isp1760_shutdown,
2237 .urb_enqueue = isp1760_urb_enqueue,
2238 .urb_dequeue = isp1760_urb_dequeue,
2239 .endpoint_disable = isp1760_endpoint_disable,
2240 .get_frame_number = isp1760_get_frame,
2241 .hub_status_data = isp1760_hub_status_data,
2242 .hub_control = isp1760_hub_control,
2243};
2244
2245int __init init_kmem_once(void)
2246{
2247 qtd_cachep = kmem_cache_create("isp1760_qtd",
2248 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2249 SLAB_MEM_SPREAD, NULL);
2250
2251 if (!qtd_cachep)
2252 return -ENOMEM;
2253
2254 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2255 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2256
2257 if (!qh_cachep) {
2258 kmem_cache_destroy(qtd_cachep);
2259 return -ENOMEM;
2260 }
2261
2262 return 0;
2263}
2264
2265void deinit_kmem_cache(void)
2266{
2267 kmem_cache_destroy(qtd_cachep);
2268 kmem_cache_destroy(qh_cachep);
2269}
2270
Catalin Marinasf9031f22009-02-10 16:55:45 +00002271struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2272 int irq, unsigned long irqflags,
2273 struct device *dev, const char *busname,
2274 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002275{
2276 struct usb_hcd *hcd;
2277 struct isp1760_hcd *priv;
2278 int ret;
2279
2280 if (usb_disabled())
2281 return ERR_PTR(-ENODEV);
2282
2283 /* prevent usb-core allocating DMA pages */
2284 dev->dma_mask = NULL;
2285
Kay Sievers0031a062008-05-02 06:02:41 +02002286 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002287 if (!hcd)
2288 return ERR_PTR(-ENOMEM);
2289
2290 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002291 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002292 init_memory(priv);
2293 hcd->regs = ioremap(res_start, res_len);
2294 if (!hcd->regs) {
2295 ret = -EIO;
2296 goto err_put;
2297 }
2298
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002299 hcd->irq = irq;
2300 hcd->rsrc_start = res_start;
2301 hcd->rsrc_len = res_len;
2302
Nate Casee6942d62008-05-21 16:28:20 -05002303 ret = usb_add_hcd(hcd, irq, irqflags);
2304 if (ret)
2305 goto err_unmap;
2306
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002307 return hcd;
2308
2309err_unmap:
2310 iounmap(hcd->regs);
2311
2312err_put:
2313 usb_put_hcd(hcd);
2314
2315 return ERR_PTR(ret);
2316}
2317
2318MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2319MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2320MODULE_LICENSE("GPL v2");