blob: 7b2e69aa2e98fdf2e803bb9d1c26d25ba74cb63e [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}
Sebastian Siewiordb11e472008-04-24 00:37:04 +020051
52/* Section 2.2 Host Controller Capability Registers */
53#define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
54#define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
55#define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
56#define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
57#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
58#define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
59#define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
60
61/* Section 2.3 Host Controller Operational Registers */
62#define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
63#define CMD_RESET (1<<1) /* reset HC not bus */
64#define CMD_RUN (1<<0) /* start/stop HC */
65#define STS_PCD (1<<2) /* port change detect */
66#define FLAG_CF (1<<0) /* true: we'll support "high speed" */
67
68#define PORT_OWNER (1<<13) /* true: companion hc owns this port */
69#define PORT_POWER (1<<12) /* true: has power (see PPC) */
70#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
71#define PORT_RESET (1<<8) /* reset port */
72#define PORT_SUSPEND (1<<7) /* suspend port */
73#define PORT_RESUME (1<<6) /* resume it */
74#define PORT_PE (1<<2) /* port enable */
75#define PORT_CSC (1<<1) /* connect status change */
76#define PORT_CONNECT (1<<0) /* device connected */
77#define PORT_RWC_BITS (PORT_CSC)
78
79struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +020080 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020081 void *data_buffer;
Arvid Brodina041d8e2011-02-26 22:04:40 +010082 u32 payload_addr;
83
Sebastian Siewiordb11e472008-04-24 00:37:04 +020084 /* the rest is HCD-private */
85 struct list_head qtd_list;
86 struct urb *urb;
87 size_t length;
88
89 /* isp special*/
90 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020091#define URB_ENQUEUED (1 << 1)
Sebastian Siewiordb11e472008-04-24 00:37:04 +020092};
93
94struct isp1760_qh {
95 /* first part defined by EHCI spec */
96 struct list_head qtd_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020097
Sebastian Siewiordb11e472008-04-24 00:37:04 +020098 u32 toggle;
99 u32 ping;
100};
101
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100102/*
103 * Access functions for isp176x registers (addresses 0..0x03FF).
104 */
105static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200106{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100107 return readl(base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200108}
109
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100110static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200111{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100112 writel(val, base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200113}
114
115/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100116 * Access functions for isp176x memory (offset >= 0x0400).
117 *
118 * bank_reads8() reads memory locations prefetched by an earlier write to
119 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
120 * bank optimizations, you should use the more generic mem_reads8() below.
121 *
122 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
123 * below.
124 *
125 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200126 * doesn't quite work because some people have to enforce 32-bit access
127 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100128static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
129 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200130{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100131 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200132 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100133 __u8 *src_byteptr;
134 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200135
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100136 src = src_base + (bank_addr | src_offset);
137
138 if (src_offset < PAYLOAD_OFFSET) {
139 while (bytes >= 4) {
140 *dst = le32_to_cpu(__raw_readl(src));
141 bytes -= 4;
142 src++;
143 dst++;
144 }
145 } else {
146 while (bytes >= 4) {
147 *dst = __raw_readl(src);
148 bytes -= 4;
149 src++;
150 dst++;
151 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200152 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200153
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100154 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200155 return;
156
157 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
158 * allocated.
159 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100160 if (src_offset < PAYLOAD_OFFSET)
161 val = le32_to_cpu(__raw_readl(src));
162 else
163 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200164
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100165 dst_byteptr = (void *) dst;
166 src_byteptr = (void *) &val;
167 while (bytes > 0) {
168 *dst_byteptr = *src_byteptr;
169 dst_byteptr++;
170 src_byteptr++;
171 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200172 }
173}
174
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100175static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
176 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200177{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100178 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
179 ndelay(90);
180 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
181}
182
183static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
184 __u32 const *src, u32 bytes)
185{
186 __u32 __iomem *dst;
187
188 dst = dst_base + dst_offset;
189
190 if (dst_offset < PAYLOAD_OFFSET) {
191 while (bytes >= 4) {
192 __raw_writel(cpu_to_le32(*src), dst);
193 bytes -= 4;
194 src++;
195 dst++;
196 }
197 } else {
198 while (bytes >= 4) {
199 __raw_writel(*src, dst);
200 bytes -= 4;
201 src++;
202 dst++;
203 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200204 }
205
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100206 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200207 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100208 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
209 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200210 */
211
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100212 if (dst_offset < PAYLOAD_OFFSET)
213 __raw_writel(cpu_to_le32(*src), dst);
214 else
215 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200216}
217
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100218/*
219 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
220 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
221 */
222static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
223 struct ptd *ptd)
224{
225 reg_write32(base, HC_MEMORY_REG,
226 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
227 ndelay(90);
228 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
229 (void *) ptd, sizeof(*ptd));
230}
231
232static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
233 struct ptd *ptd)
234{
235 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
236 &ptd->dw1, 7*sizeof(ptd->dw1));
237 /* Make sure dw0 gets written last (after other dw's and after payload)
238 since it contains the enable bit */
239 wmb();
240 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
241 sizeof(ptd->dw0));
242}
243
244
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200245/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
246static void init_memory(struct isp1760_hcd *priv)
247{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100248 int i, curr;
249 u32 payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200250
Arvid Brodina041d8e2011-02-26 22:04:40 +0100251 payload_addr = PAYLOAD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200252 for (i = 0; i < BLOCK_1_NUM; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100253 priv->memory_pool[i].start = payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200254 priv->memory_pool[i].size = BLOCK_1_SIZE;
255 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100256 payload_addr += priv->memory_pool[i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200257 }
258
Arvid Brodina041d8e2011-02-26 22:04:40 +0100259 curr = i;
260 for (i = 0; i < BLOCK_2_NUM; i++) {
261 priv->memory_pool[curr + i].start = payload_addr;
262 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
263 priv->memory_pool[curr + i].free = 1;
264 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200265 }
266
Arvid Brodina041d8e2011-02-26 22:04:40 +0100267 curr = i;
268 for (i = 0; i < BLOCK_3_NUM; i++) {
269 priv->memory_pool[curr + i].start = payload_addr;
270 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
271 priv->memory_pool[curr + i].free = 1;
272 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200273 }
274
Arvid Brodina041d8e2011-02-26 22:04:40 +0100275 BUG_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200276}
277
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100278static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200279{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100280 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200281 int i;
282
Arvid Brodina041d8e2011-02-26 22:04:40 +0100283 BUG_ON(qtd->payload_addr);
284
285 if (!qtd->length)
286 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200287
288 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100289 if (priv->memory_pool[i].size >= qtd->length &&
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200290 priv->memory_pool[i].free) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200291 priv->memory_pool[i].free = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100292 qtd->payload_addr = priv->memory_pool[i].start;
293 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200294 }
295 }
296
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100297 dev_err(hcd->self.controller,
Randy Dunlapd06847f2011-03-15 17:09:10 -0700298 "%s: Cannot allocate %zu bytes of memory\n"
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100299 "Current memory map:\n",
300 __func__, qtd->length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200301 for (i = 0; i < BLOCKS; i++) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100302 dev_err(hcd->self.controller, "Pool %2d size %4d status: %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200303 i, priv->memory_pool[i].size,
304 priv->memory_pool[i].free);
305 }
306 /* XXX maybe -ENOMEM could be possible */
307 BUG();
Arvid Brodina041d8e2011-02-26 22:04:40 +0100308 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200309}
310
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100311static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200312{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100313 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200314 int i;
315
Arvid Brodina041d8e2011-02-26 22:04:40 +0100316 if (!qtd->payload_addr)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200317 return;
318
319 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100320 if (priv->memory_pool[i].start == qtd->payload_addr) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200321 BUG_ON(priv->memory_pool[i].free);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200322 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100323 qtd->payload_addr = 0;
324 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200325 }
326 }
327
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100328 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
329 __func__, qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200330 BUG();
331}
332
333static void isp1760_init_regs(struct usb_hcd *hcd)
334{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100335 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
336 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
337 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
338 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200339
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100340 reg_write32(hcd->regs, HC_ATL_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
341 reg_write32(hcd->regs, HC_INT_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
342 reg_write32(hcd->regs, HC_ISO_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200343}
344
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100345static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200346 u32 mask, u32 done, int usec)
347{
348 u32 result;
349
350 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100351 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200352 if (result == ~0)
353 return -ENODEV;
354 result &= mask;
355 if (result == done)
356 return 0;
357 udelay(1);
358 usec--;
359 } while (usec > 0);
360 return -ETIMEDOUT;
361}
362
363/* reset a non-running (STS_HALT == 1) controller */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100364static int ehci_reset(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200365{
366 int retval;
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100367 struct isp1760_hcd *priv = hcd_to_priv(hcd);
368
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100369 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200370
371 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100372 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200373 hcd->state = HC_STATE_HALT;
374 priv->next_statechange = jiffies;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100375 retval = handshake(hcd, HC_USBCMD,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200376 CMD_RESET, 0, 250 * 1000);
377 return retval;
378}
379
380static void qh_destroy(struct isp1760_qh *qh)
381{
382 BUG_ON(!list_empty(&qh->qtd_list));
383 kmem_cache_free(qh_cachep, qh);
384}
385
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100386static struct isp1760_qh *isp1760_qh_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200387{
388 struct isp1760_qh *qh;
389
390 qh = kmem_cache_zalloc(qh_cachep, flags);
391 if (!qh)
392 return qh;
393
394 INIT_LIST_HEAD(&qh->qtd_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200395 return qh;
396}
397
398/* magic numbers that can affect system performance */
399#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
400#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
401#define EHCI_TUNE_RL_TT 0
402#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
403#define EHCI_TUNE_MULT_TT 1
404#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
405
406/* one-time init, only for memory state */
407static int priv_init(struct usb_hcd *hcd)
408{
409 struct isp1760_hcd *priv = hcd_to_priv(hcd);
410 u32 hcc_params;
411
412 spin_lock_init(&priv->lock);
413
414 /*
415 * hw default: 1K periodic list heads, one per frame.
416 * periodic_size can shrink by USBCMD update if hcc_params allows.
417 */
418 priv->periodic_size = DEFAULT_I_TDPS;
419
420 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100421 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200422 /* full frame cache */
423 if (HCC_ISOC_CACHE(hcc_params))
424 priv->i_thresh = 8;
425 else /* N microframes cached */
426 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
427
428 return 0;
429}
430
431static int isp1760_hc_setup(struct usb_hcd *hcd)
432{
433 struct isp1760_hcd *priv = hcd_to_priv(hcd);
434 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500435 u32 scratch, hwmode;
436
437 /* Setup HW Mode Control: This assumes a level active-low interrupt */
438 hwmode = HW_DATA_BUS_32BIT;
439
440 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
441 hwmode &= ~HW_DATA_BUS_32BIT;
442 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
443 hwmode |= HW_ANA_DIGI_OC;
444 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
445 hwmode |= HW_DACK_POL_HIGH;
446 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
447 hwmode |= HW_DREQ_POL_HIGH;
Michael Hennerich9da69c62009-07-15 23:22:54 -0400448 if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
449 hwmode |= HW_INTR_HIGH_ACT;
450 if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
451 hwmode |= HW_INTR_EDGE_TRIG;
Nate Case3faefc82008-06-17 11:11:38 -0500452
453 /*
454 * We have to set this first in case we're in 16-bit mode.
455 * Write it twice to ensure correct upper bits if switching
456 * to 16-bit mode.
457 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100458 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
459 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200460
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100461 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500462 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100463 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
464 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200465 if (scratch != 0xdeadbabe) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100466 dev_err(hcd->self.controller, "Scratch test failed.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200467 return -ENODEV;
468 }
469
470 /* pre reset */
471 isp1760_init_regs(hcd);
472
473 /* reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100474 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200475 mdelay(100);
476
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100477 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200478 mdelay(100);
479
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100480 result = ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200481 if (result)
482 return result;
483
484 /* Step 11 passed */
485
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100486 dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
Nate Case3faefc82008-06-17 11:11:38 -0500487 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
488 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
489 "analog" : "digital");
490
491 /* ATL reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100492 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500493 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100494 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500495
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100496 reg_write32(hcd->regs, HC_INTERRUPT_REG, INTERRUPT_ENABLE_MASK);
497 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200498
Nate Case3faefc82008-06-17 11:11:38 -0500499 /*
500 * PORT 1 Control register of the ISP1760 is the OTG control
Thomas Hommel42c65392008-12-18 10:31:40 +0100501 * register on ISP1761. Since there is no OTG or device controller
502 * support in this driver, we use port 1 as a "normal" USB host port on
503 * both chips.
Nate Case3faefc82008-06-17 11:11:38 -0500504 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100505 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
Thomas Hommel42c65392008-12-18 10:31:40 +0100506 mdelay(10);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200507
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100508 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200509
510 return priv_init(hcd);
511}
512
513static void isp1760_init_maps(struct usb_hcd *hcd)
514{
515 /*set last maps, for iso its only 1, else 32 tds bitmap*/
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100516 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
517 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
518 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200519}
520
521static void isp1760_enable_interrupts(struct usb_hcd *hcd)
522{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100523 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
524 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0);
525 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
526 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0);
527 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
528 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200529 /* step 23 passed */
530}
531
532static int isp1760_run(struct usb_hcd *hcd)
533{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200534 int retval;
535 u32 temp;
536 u32 command;
537 u32 chipid;
538
539 hcd->uses_new_polling = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200540
541 hcd->state = HC_STATE_RUNNING;
542 isp1760_enable_interrupts(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100543 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
544 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200545
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100546 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200547 command &= ~(CMD_LRESET|CMD_RESET);
548 command |= CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100549 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200550
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100551 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200552 250 * 1000);
553 if (retval)
554 return retval;
555
556 /*
557 * XXX
558 * Spec says to write FLAG_CF as last config action, priv code grabs
559 * the semaphore while doing so.
560 */
561 down_write(&ehci_cf_port_reset_rwsem);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100562 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200563
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100564 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200565 up_write(&ehci_cf_port_reset_rwsem);
566 if (retval)
567 return retval;
568
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100569 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100570 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
571 chipid & 0xffff, chipid >> 16);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200572
573 /* PTD Register Init Part 2, Step 28 */
574 /* enable INTs */
575 isp1760_init_maps(hcd);
576
577 /* GRR this is run-once init(), being done every time the HC starts.
578 * So long as they're part of class devices, we can't do it init()
579 * since the class device isn't created that early.
580 */
581 return 0;
582}
583
584static u32 base_to_chip(u32 base)
585{
586 return ((base - 0x400) >> 3);
587}
588
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100589static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
590{
591 struct urb *urb;
592
593 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
594 return 1;
595
596 urb = qtd->urb;
597 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
598 return (qtd->urb != urb);
599}
600
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100601static void transform_into_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100602 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200603{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200604 u32 maxpacket;
605 u32 multi;
606 u32 pid_code;
607 u32 rl = RL_COUNTER;
608 u32 nak = NAK_COUNTER;
609
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100610 memset(ptd, 0, sizeof(*ptd));
611
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200612 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100613 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
614 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200615 multi = 1 + ((maxpacket >> 11) & 0x3);
616 maxpacket &= 0x7ff;
617
618 /* DW0 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100619 ptd->dw0 = PTD_VALID;
620 ptd->dw0 |= PTD_LENGTH(qtd->length);
621 ptd->dw0 |= PTD_MAXPACKET(maxpacket);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100622 ptd->dw0 |= PTD_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200623
624 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100625 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
626 ptd->dw1 |= PTD_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200627
628 pid_code = qtd->packet_type;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100629 ptd->dw1 |= PTD_PID_TOKEN(pid_code);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200630
Arvid Brodina041d8e2011-02-26 22:04:40 +0100631 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100632 ptd->dw1 |= PTD_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100633 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100634 ptd->dw1 |= PTD_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200635
Arvid Brodina041d8e2011-02-26 22:04:40 +0100636 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200637 /* split transaction */
638
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100639 ptd->dw1 |= PTD_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100640 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100641 ptd->dw1 |= PTD_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200642
Arvid Brodina041d8e2011-02-26 22:04:40 +0100643 ptd->dw1 |= PTD_PORT_NUM(qtd->urb->dev->ttport);
644 ptd->dw1 |= PTD_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200645
646 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100647 if (usb_pipeint(qtd->urb->pipe) &&
648 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100649 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200650
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100651 ptd->dw3 = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200652 rl = 0;
653 nak = 0;
654 } else {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100655 ptd->dw0 |= PTD_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100656 if (usb_pipecontrol(qtd->urb->pipe) ||
657 usb_pipebulk(qtd->urb->pipe))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100658 ptd->dw3 = qh->ping;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200659 else
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100660 ptd->dw3 = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200661 }
662 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100663 ptd->dw2 = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100664 ptd->dw2 |= PTD_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100665 ptd->dw2 |= PTD_RL_CNT(rl);
666 ptd->dw3 |= PTD_NAC_CNT(nak);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200667
668 /* DW3 */
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100669 ptd->dw3 |= qh->toggle;
670 if (usb_pipecontrol(qtd->urb->pipe)) {
671 if (qtd->data_buffer == qtd->urb->setup_packet)
672 ptd->dw3 &= ~PTD_DATA_TOGGLE(1);
673 else if (last_qtd_of_urb(qtd, qh))
674 ptd->dw3 |= PTD_DATA_TOGGLE(1);
675 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200676
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100677 ptd->dw3 |= PTD_ACTIVE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200678 /* Cerr */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100679 ptd->dw3 |= PTD_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200680}
681
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100682static void transform_add_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100683 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200684{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100685 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200686 u32 period;
687
Arvid Brodin65f1b522011-02-26 22:07:35 +0100688 /*
689 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
690 * the algorithm from the original Philips driver code, which was
691 * pretty much used in this driver before as well, is quite horrendous
692 * and, i believe, incorrect. The code below follows the datasheet and
693 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
694 * more reliable this way (fingers crossed...).
695 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200696
Arvid Brodin65f1b522011-02-26 22:07:35 +0100697 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
698 /* urb->interval is in units of microframes (1/8 ms) */
699 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200700
Arvid Brodin65f1b522011-02-26 22:07:35 +0100701 if (qtd->urb->interval > 4)
702 usof = 0x01; /* One bit set =>
703 interval 1 ms * uFrame-match */
704 else if (qtd->urb->interval > 2)
705 usof = 0x22; /* Two bits set => interval 1/2 ms */
706 else if (qtd->urb->interval > 1)
707 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200708 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100709 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200710 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100711 /* urb->interval is in units of frames (1 ms) */
712 period = qtd->urb->interval;
713 usof = 0x0f; /* Execute Start Split on any of the
714 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200715
Arvid Brodin65f1b522011-02-26 22:07:35 +0100716 /*
717 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
718 * complete split needs to be sent. Valid only for IN." Also,
719 * "All bits can be set to one for every transfer." (p 82,
720 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
721 * that number come from? 0xff seems to work fine...
722 */
723 /* ptd->dw5 = 0x1c; */
724 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200725 }
726
Arvid Brodin65f1b522011-02-26 22:07:35 +0100727 period = period >> 1;/* Ensure equal or shorter period than requested */
728 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
729
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100730 ptd->dw2 |= period;
731 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200732}
733
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100734static void transform_into_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100735 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200736{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100737 transform_into_atl(qh, qtd, ptd);
738 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200739}
740
741static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len,
742 u32 token)
743{
744 int count;
745
746 qtd->data_buffer = databuffer;
747 qtd->packet_type = GET_QTD_TOKEN_TYPE(token);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200748
Arvid Brodina041d8e2011-02-26 22:04:40 +0100749 if (len > MAX_PAYLOAD_SIZE)
750 count = MAX_PAYLOAD_SIZE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200751 else
752 count = len;
753
754 qtd->length = count;
755 return count;
756}
757
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100758static int check_error(struct usb_hcd *hcd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200759{
760 int error = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200761
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100762 if (ptd->dw3 & DW3_HALT_BIT) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200763 error = -EPIPE;
764
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100765 if (ptd->dw3 & DW3_ERROR_BIT)
Anton Vorontsov0954e1c2010-05-07 01:09:19 +0400766 pr_err("error bit is set in DW3\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200767 }
768
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100769 if (ptd->dw3 & DW3_QTD_ACTIVE) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100770 dev_err(hcd->self.controller, "Transfer active bit is set DW3\n"
771 "nak counter: %d, rl: %d\n",
772 (ptd->dw3 >> 19) & 0xf, (ptd->dw2 >> 25) & 0xf);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200773 }
774
775 return error;
776}
777
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100778static void check_int_err_status(struct usb_hcd *hcd, u32 dw4)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200779{
780 u32 i;
781
782 dw4 >>= 8;
783
784 for (i = 0; i < 8; i++) {
785 switch (dw4 & 0x7) {
786 case INT_UNDERRUN:
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100787 dev_err(hcd->self.controller, "Underrun (%d)\n", i);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200788 break;
789
790 case INT_EXACT:
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100791 dev_err(hcd->self.controller,
792 "Transaction error (%d)\n", i);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200793 break;
794
795 case INT_BABBLE:
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100796 dev_err(hcd->self.controller, "Babble error (%d)\n", i);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200797 break;
798 }
799 dw4 >>= 3;
800 }
801}
802
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100803static void enqueue_one_qtd(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200804{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100805 if (qtd->length && (qtd->length <= MAX_PAYLOAD_SIZE)) {
806 switch (qtd->packet_type) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200807 case IN_PID:
808 break;
809 case OUT_PID:
810 case SETUP_PID:
Arvid Brodina041d8e2011-02-26 22:04:40 +0100811 mem_writes8(hcd->regs, qtd->payload_addr,
812 qtd->data_buffer, qtd->length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200813 }
814 }
815}
816
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100817static void enqueue_one_atl_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
818 u32 slot, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200819{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100820 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200821 struct ptd ptd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200822
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100823 alloc_mem(hcd, qtd);
824 transform_into_atl(qh, qtd, &ptd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100825 ptd_write(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100826 enqueue_one_qtd(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200827
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200828 priv->atl_ints[slot].qh = qh;
829 priv->atl_ints[slot].qtd = qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100830 qtd->status |= URB_ENQUEUED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200831 qtd->status |= slot << 16;
832}
833
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100834static void enqueue_one_int_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
835 u32 slot, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200836{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100837 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200838 struct ptd ptd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200839
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100840 alloc_mem(hcd, qtd);
841 transform_into_int(qh, qtd, &ptd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100842 ptd_write(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100843 enqueue_one_qtd(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200844
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200845 priv->int_ints[slot].qh = qh;
846 priv->int_ints[slot].qtd = qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100847 qtd->status |= URB_ENQUEUED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200848 qtd->status |= slot << 16;
849}
850
Adrian Bunk473bca92008-05-05 21:25:33 +0300851static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
852 struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200853{
854 struct isp1760_hcd *priv = hcd_to_priv(hcd);
855 u32 skip_map, or_map;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200856 u32 slot;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200857 u32 buffstatus;
858
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000859 /*
860 * When this function is called from the interrupt handler to enqueue
861 * a follow-up packet, the SKIP register gets written and read back
862 * almost immediately. With ISP1761, this register requires a delay of
863 * 195ns between a write and subsequent read (see section 15.1.1.3).
864 */
Michael Hennerichebb8a4e2010-08-05 17:53:57 -0400865 mmiowb();
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000866 ndelay(195);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100867 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200868
869 BUG_ON(!skip_map);
870 slot = __ffs(skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200871
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100872 enqueue_one_atl_qtd(hcd, qh, slot, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200873
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100874 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100875 or_map |= (1 << slot);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100876 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200877
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100878 skip_map &= ~(1 << slot);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100879 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200880
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +0100881 priv->atl_queued++;
882 if (priv->atl_queued == 2)
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100883 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
884 INTERRUPT_ENABLE_SOT_MASK);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +0100885
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100886 buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200887 buffstatus |= ATL_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100888 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200889}
890
Adrian Bunk473bca92008-05-05 21:25:33 +0300891static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
892 struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200893{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200894 u32 skip_map, or_map;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200895 u32 slot;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200896 u32 buffstatus;
897
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000898 /*
899 * When this function is called from the interrupt handler to enqueue
900 * a follow-up packet, the SKIP register gets written and read back
901 * almost immediately. With ISP1761, this register requires a delay of
902 * 195ns between a write and subsequent read (see section 15.1.1.3).
903 */
Michael Hennerichebb8a4e2010-08-05 17:53:57 -0400904 mmiowb();
Catalin Marinase6bdfe32009-03-23 12:38:16 +0000905 ndelay(195);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100906 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200907
908 BUG_ON(!skip_map);
909 slot = __ffs(skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200910
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100911 enqueue_one_int_qtd(hcd, qh, slot, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200912
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100913 or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100914 or_map |= (1 << slot);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100915 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200916
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100917 skip_map &= ~(1 << slot);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100918 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200919
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100920 buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200921 buffstatus |= INT_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100922 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200923}
924
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100925static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200926__releases(priv->lock)
927__acquires(priv->lock)
928{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100929 struct isp1760_hcd *priv = hcd_to_priv(hcd);
930
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200931 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100932 if (urb->status == -EINPROGRESS)
933 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200934 }
935
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000936 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
937 void *ptr;
938 for (ptr = urb->transfer_buffer;
939 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
940 ptr += PAGE_SIZE)
941 flush_dcache_page(virt_to_page(ptr));
942 }
943
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200944 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100945 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200946 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100947 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200948 spin_lock(&priv->lock);
949}
950
951static void isp1760_qtd_free(struct isp1760_qtd *qtd)
952{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100953 BUG_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200954 kmem_cache_free(qtd_cachep, qtd);
955}
956
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100957static struct isp1760_qtd *clean_this_qtd(struct isp1760_qtd *qtd,
958 struct isp1760_qh *qh)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200959{
960 struct isp1760_qtd *tmp_qtd;
961
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100962 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
963 tmp_qtd = NULL;
964 else
965 tmp_qtd = list_entry(qtd->qtd_list.next, struct isp1760_qtd,
966 qtd_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200967 list_del(&qtd->qtd_list);
968 isp1760_qtd_free(qtd);
969 return tmp_qtd;
970}
971
972/*
973 * Remove this QTD from the QH list and free its memory. If this QTD
974 * isn't the last one than remove also his successor(s).
975 * Returns the QTD which is part of an new URB and should be enqueued.
976 */
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100977static struct isp1760_qtd *clean_up_qtdlist(struct isp1760_qtd *qtd,
978 struct isp1760_qh *qh)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200979{
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100980 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200981
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100982 urb = qtd->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200983 do {
Arvid Brodinfd436ae2011-02-26 22:03:49 +0100984 qtd = clean_this_qtd(qtd, qh);
985 } while (qtd && (qtd->urb == urb));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200986
987 return qtd;
988}
989
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100990static void do_atl_int(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200991{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100992 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200993 u32 done_map, skip_map;
994 struct ptd ptd;
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100995 struct urb *urb;
996 u32 slot;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200997 u32 length;
998 u32 or_map;
999 u32 status = -EINVAL;
1000 int error;
1001 struct isp1760_qtd *qtd;
1002 struct isp1760_qh *qh;
1003 u32 rl;
1004 u32 nakcount;
1005
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001006 done_map = reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1007 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001008
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001009 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001010 or_map &= ~done_map;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001011 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001012
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001013 while (done_map) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001014 status = 0;
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001015 priv->atl_queued--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001016
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001017 slot = __ffs(done_map);
1018 done_map &= ~(1 << slot);
1019 skip_map |= (1 << slot);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001020
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001021 qtd = priv->atl_ints[slot].qtd;
1022 qh = priv->atl_ints[slot].qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001023
1024 if (!qh) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001025 dev_err(hcd->self.controller, "qh is 0\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001026 continue;
1027 }
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001028 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
Enrico Scholz3f02a952008-07-17 20:09:30 +02001029
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001030 rl = (ptd.dw2 >> 25) & 0x0f;
1031 nakcount = (ptd.dw3 >> 19) & 0xf;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001032
1033 /* Transfer Error, *but* active and no HALT -> reload */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001034 if ((ptd.dw3 & DW3_ERROR_BIT) && (ptd.dw3 & DW3_QTD_ACTIVE) &&
1035 !(ptd.dw3 & DW3_HALT_BIT)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001036
1037 /* according to ppriv code, we have to
1038 * reload this one if trasfered bytes != requested bytes
1039 * else act like everything went smooth..
1040 * XXX This just doesn't feel right and hasn't
1041 * triggered so far.
1042 */
1043
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001044 length = PTD_XFERRED_LENGTH(ptd.dw3);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001045 dev_err(hcd->self.controller,
1046 "Should reload now... transferred %d "
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001047 "of %zu\n", length, qtd->length);
1048 BUG();
1049 }
1050
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001051 if (!nakcount && (ptd.dw3 & DW3_QTD_ACTIVE)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001052 u32 buffstatus;
1053
Colin Tuckleyc0d74142010-01-07 11:22:47 +00001054 /*
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001055 * NAKs are handled in HW by the chip. Usually if the
1056 * device is not able to send data fast enough.
Colin Tuckleyc0d74142010-01-07 11:22:47 +00001057 * This happens mostly on slower hardware.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001058 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001059
1060 /* RL counter = ERR counter */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001061 ptd.dw3 &= ~(0xf << 19);
1062 ptd.dw3 |= rl << 19;
1063 ptd.dw3 &= ~(3 << (55 - 32));
1064 ptd.dw3 |= ERR_COUNTER << (55 - 32);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001065
1066 /*
1067 * It is not needed to write skip map back because it
1068 * is unchanged. Just make sure that this entry is
1069 * unskipped once it gets written to the HW.
1070 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001071 skip_map &= ~(1 << slot);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001072 or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001073 or_map |= 1 << slot;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001074 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001075
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001076 ptd.dw0 |= PTD_VALID;
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001077 ptd_write(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001078
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001079 priv->atl_queued++;
1080 if (priv->atl_queued == 2)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001081 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1082 INTERRUPT_ENABLE_SOT_MASK);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001083
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001084 buffstatus = reg_read32(hcd->regs,
1085 HC_BUFFER_STATUS_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001086 buffstatus |= ATL_BUFFER;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001087 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1088 buffstatus);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001089 continue;
1090 }
1091
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001092 error = check_error(hcd, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001093 if (error) {
1094 status = error;
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001095 priv->atl_ints[slot].qh->toggle = 0;
1096 priv->atl_ints[slot].qh->ping = 0;
1097 qtd->urb->status = -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001098
1099#if 0
1100 printk(KERN_ERR "Error in %s().\n", __func__);
1101 printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1102 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1103 "%08x dw7: %08x\n",
1104 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1105 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1106#endif
1107 } else {
Arvid Brodin7adc14b2011-02-26 22:08:47 +01001108 priv->atl_ints[slot].qh->toggle = ptd.dw3 & (1 << 25);
1109 priv->atl_ints[slot].qh->ping = ptd.dw3 & (1 << 26);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001110 }
1111
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001112 length = PTD_XFERRED_LENGTH(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001113 if (length) {
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001114 switch (DW1_GET_PID(ptd.dw1)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001115 case IN_PID:
Arvid Brodina041d8e2011-02-26 22:04:40 +01001116 mem_reads8(hcd->regs, qtd->payload_addr,
Arvid Brodinbbaa3872011-02-26 22:05:26 +01001117 qtd->data_buffer, length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001118
1119 case OUT_PID:
1120
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001121 qtd->urb->actual_length += length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001122
1123 case SETUP_PID:
1124 break;
1125 }
1126 }
1127
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001128 priv->atl_ints[slot].qtd = NULL;
1129 priv->atl_ints[slot].qh = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001130
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001131 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001132
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001133 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001134
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001135 if (qtd->urb->status == -EPIPE) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001136 /* HALT was received */
1137
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001138 urb = qtd->urb;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001139 qtd = clean_up_qtdlist(qtd, qh);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001140 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001141
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001142 } else if (usb_pipebulk(qtd->urb->pipe) &&
1143 (length < qtd->length)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001144 /* short BULK received */
1145
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001146 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK) {
1147 qtd->urb->status = -EREMOTEIO;
1148 dev_dbg(hcd->self.controller,
1149 "short bulk, %d instead %zu "
1150 "with URB_SHORT_NOT_OK flag.\n",
1151 length, qtd->length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001152 }
1153
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001154 if (qtd->urb->status == -EINPROGRESS)
1155 qtd->urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001156
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001157 urb = qtd->urb;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001158 qtd = clean_up_qtdlist(qtd, qh);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001159 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001160
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001161 } else if (last_qtd_of_urb(qtd, qh)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001162 /* that was the last qtd of that URB */
1163
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001164 if (qtd->urb->status == -EINPROGRESS)
1165 qtd->urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001166
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001167 urb = qtd->urb;
1168 qtd = clean_up_qtdlist(qtd, qh);
1169 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001170
1171 } else {
1172 /* next QTD of this URB */
1173
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001174 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001175 BUG_ON(!qtd);
1176 }
1177
1178 if (qtd)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001179 enqueue_an_ATL_packet(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001180
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001181 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001182 }
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001183 if (priv->atl_queued <= 1)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001184 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1185 INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001186}
1187
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001188static void do_intl_int(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001189{
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001190 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001191 u32 done_map, skip_map;
1192 struct ptd ptd;
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001193 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001194 u32 length;
1195 u32 or_map;
1196 int error;
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001197 u32 slot;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001198 struct isp1760_qtd *qtd;
1199 struct isp1760_qh *qh;
1200
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001201 done_map = reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1202 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001203
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001204 or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001205 or_map &= ~done_map;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001206 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001207
1208 while (done_map) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001209 slot = __ffs(done_map);
1210 done_map &= ~(1 << slot);
1211 skip_map |= (1 << slot);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001212
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001213 qtd = priv->int_ints[slot].qtd;
1214 qh = priv->int_ints[slot].qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001215
1216 if (!qh) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001217 dev_err(hcd->self.controller, "(INT) qh is 0\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001218 continue;
1219 }
1220
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001221 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1222 check_int_err_status(hcd, ptd.dw4);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001223
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001224 error = check_error(hcd, &ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001225 if (error) {
1226#if 0
1227 printk(KERN_ERR "Error in %s().\n", __func__);
1228 printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1229 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1230 "%08x dw7: %08x\n",
1231 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1232 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1233#endif
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001234 qtd->urb->status = -EPIPE;
1235 priv->int_ints[slot].qh->toggle = 0;
1236 priv->int_ints[slot].qh->ping = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001237
1238 } else {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001239 priv->int_ints[slot].qh->toggle = ptd.dw3 & (1 << 25);
1240 priv->int_ints[slot].qh->ping = ptd.dw3 & (1 << 26);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001241 }
1242
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001243 if (qtd->urb->dev->speed != USB_SPEED_HIGH)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001244 length = PTD_XFERRED_LENGTH_LO(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001245 else
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001246 length = PTD_XFERRED_LENGTH(ptd.dw3);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001247
1248 if (length) {
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001249 switch (DW1_GET_PID(ptd.dw1)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001250 case IN_PID:
Arvid Brodina041d8e2011-02-26 22:04:40 +01001251 mem_reads8(hcd->regs, qtd->payload_addr,
Arvid Brodinbbaa3872011-02-26 22:05:26 +01001252 qtd->data_buffer, length);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001253 case OUT_PID:
1254
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001255 qtd->urb->actual_length += length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001256
1257 case SETUP_PID:
1258 break;
1259 }
1260 }
1261
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001262 priv->int_ints[slot].qtd = NULL;
1263 priv->int_ints[slot].qh = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001264
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001265 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001266 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001267
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001268 if (qtd->urb->status == -EPIPE) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001269 /* HALT received */
1270
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001271 urb = qtd->urb;
1272 qtd = clean_up_qtdlist(qtd, qh);
1273 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001274
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001275 } else if (last_qtd_of_urb(qtd, qh)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001276
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001277 if (qtd->urb->status == -EINPROGRESS)
1278 qtd->urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001279
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001280 urb = qtd->urb;
1281 qtd = clean_up_qtdlist(qtd, qh);
1282 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001283
1284 } else {
1285 /* next QTD of this URB */
1286
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001287 qtd = clean_this_qtd(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001288 BUG_ON(!qtd);
1289 }
1290
1291 if (qtd)
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001292 enqueue_an_INT_packet(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001293
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001294 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001295 }
1296}
1297
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001298static struct isp1760_qh *qh_make(struct usb_hcd *hcd, struct urb *urb,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001299 gfp_t flags)
1300{
1301 struct isp1760_qh *qh;
1302 int is_input, type;
1303
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001304 qh = isp1760_qh_alloc(flags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001305 if (!qh)
1306 return qh;
1307
1308 /*
1309 * init endpoint/device data for this QH
1310 */
1311 is_input = usb_pipein(urb->pipe);
1312 type = usb_pipetype(urb->pipe);
1313
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001314 if (!usb_pipecontrol(urb->pipe))
1315 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input,
1316 1);
1317 return qh;
1318}
1319
1320/*
1321 * For control/bulk/interrupt, return QH with these TDs appended.
1322 * Allocates and initializes the QH if necessary.
1323 * Returns null if it can't allocate a QH it needs to.
1324 * If the QH has TDs (urbs) already, that's great.
1325 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001326static struct isp1760_qh *qh_append_tds(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001327 struct urb *urb, struct list_head *qtd_list, int epnum,
1328 void **ptr)
1329{
1330 struct isp1760_qh *qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001331
1332 qh = (struct isp1760_qh *)*ptr;
1333 if (!qh) {
1334 /* can't sleep here, we have priv->lock... */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001335 qh = qh_make(hcd, urb, GFP_ATOMIC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001336 if (!qh)
1337 return qh;
1338 *ptr = qh;
1339 }
1340
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001341 list_splice(qtd_list, qh->qtd_list.prev);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001342
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001343 return qh;
1344}
1345
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001346static void qtd_list_free(struct urb *urb, struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001347{
1348 struct list_head *entry, *temp;
1349
1350 list_for_each_safe(entry, temp, qtd_list) {
1351 struct isp1760_qtd *qtd;
1352
1353 qtd = list_entry(entry, struct isp1760_qtd, qtd_list);
1354 list_del(&qtd->qtd_list);
1355 isp1760_qtd_free(qtd);
1356 }
1357}
1358
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001359static int isp1760_prepare_enqueue(struct usb_hcd *hcd, struct urb *urb,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001360 struct list_head *qtd_list, gfp_t mem_flags, packet_enqueue *p)
1361{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001362 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001363 struct isp1760_qtd *qtd;
1364 int epnum;
1365 unsigned long flags;
1366 struct isp1760_qh *qh = NULL;
1367 int rc;
1368 int qh_busy;
1369
1370 qtd = list_entry(qtd_list->next, struct isp1760_qtd, qtd_list);
1371 epnum = urb->ep->desc.bEndpointAddress;
1372
1373 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001374 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001375 rc = -ESHUTDOWN;
1376 goto done;
1377 }
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001378 rc = usb_hcd_link_urb_to_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001379 if (rc)
1380 goto done;
1381
1382 qh = urb->ep->hcpriv;
1383 if (qh)
1384 qh_busy = !list_empty(&qh->qtd_list);
1385 else
1386 qh_busy = 0;
1387
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001388 qh = qh_append_tds(hcd, urb, qtd_list, epnum, &urb->ep->hcpriv);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001389 if (!qh) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001390 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001391 rc = -ENOMEM;
1392 goto done;
1393 }
1394
1395 if (!qh_busy)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001396 p(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001397
1398done:
1399 spin_unlock_irqrestore(&priv->lock, flags);
1400 if (!qh)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001401 qtd_list_free(urb, qtd_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001402 return rc;
1403}
1404
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001405static struct isp1760_qtd *isp1760_qtd_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001406{
1407 struct isp1760_qtd *qtd;
1408
1409 qtd = kmem_cache_zalloc(qtd_cachep, flags);
1410 if (qtd)
1411 INIT_LIST_HEAD(&qtd->qtd_list);
1412
1413 return qtd;
1414}
1415
1416/*
1417 * create a list of filled qtds for this URB; won't link into qh.
1418 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001419#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1420static struct list_head *qh_urb_transaction(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001421 struct urb *urb, struct list_head *head, gfp_t flags)
1422{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001423 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001424 void *buf;
1425 int len, maxpacket;
1426 int is_input;
1427 u32 token;
1428
1429 /*
1430 * URBs map to sequences of QTDs: one logical transaction
1431 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001432 qtd = isp1760_qtd_alloc(flags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001433 if (!qtd)
1434 return NULL;
1435
1436 list_add_tail(&qtd->qtd_list, head);
1437 qtd->urb = urb;
1438 urb->status = -EINPROGRESS;
1439
1440 token = 0;
1441 /* for split transactions, SplitXState initialized to zero */
1442
1443 len = urb->transfer_buffer_length;
1444 is_input = usb_pipein(urb->pipe);
1445 if (usb_pipecontrol(urb->pipe)) {
1446 /* SETUP pid */
1447 qtd_fill(qtd, urb->setup_packet,
1448 sizeof(struct usb_ctrlrequest),
1449 token | SETUP_PID);
1450
1451 /* ... and always at least one more pid */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001452 qtd = isp1760_qtd_alloc(flags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001453 if (!qtd)
1454 goto cleanup;
1455 qtd->urb = urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001456 list_add_tail(&qtd->qtd_list, head);
1457
1458 /* for zero length DATA stages, STATUS is always IN */
1459 if (len == 0)
1460 token |= IN_PID;
1461 }
1462
1463 /*
1464 * data transfer stage: buffer setup
1465 */
1466 buf = urb->transfer_buffer;
1467
1468 if (is_input)
1469 token |= IN_PID;
1470 else
1471 token |= OUT_PID;
1472
1473 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
1474
1475 /*
1476 * buffer gets wrapped in one or more qtds;
1477 * last one may be "short" (including zero len)
1478 * and may serve as a control status ack
1479 */
1480 for (;;) {
1481 int this_qtd_len;
1482
1483 if (!buf && len) {
1484 /* XXX This looks like usb storage / SCSI bug */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001485 dev_err(hcd->self.controller, "buf is null, dma is %08lx len is %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001486 (long unsigned)urb->transfer_dma, len);
1487 WARN_ON(1);
1488 }
1489
1490 this_qtd_len = qtd_fill(qtd, buf, len, token);
1491 len -= this_qtd_len;
1492 buf += this_qtd_len;
1493
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001494 if (len <= 0)
1495 break;
1496
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001497 qtd = isp1760_qtd_alloc(flags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001498 if (!qtd)
1499 goto cleanup;
1500 qtd->urb = urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001501 list_add_tail(&qtd->qtd_list, head);
1502 }
1503
1504 /*
1505 * control requests may need a terminating data "status" ack;
1506 * bulk ones may need a terminating short packet (zero length).
1507 */
1508 if (urb->transfer_buffer_length != 0) {
1509 int one_more = 0;
1510
1511 if (usb_pipecontrol(urb->pipe)) {
1512 one_more = 1;
1513 /* "in" <--> "out" */
1514 token ^= IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001515 } else if (usb_pipebulk(urb->pipe)
1516 && (urb->transfer_flags & URB_ZERO_PACKET)
1517 && !(urb->transfer_buffer_length % maxpacket)) {
1518 one_more = 1;
1519 }
1520 if (one_more) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001521 qtd = isp1760_qtd_alloc(flags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001522 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 /* never any data in such packets */
1528 qtd_fill(qtd, NULL, 0, token);
1529 }
1530 }
1531
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001532 qtd->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001533 return head;
1534
1535cleanup:
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001536 qtd_list_free(urb, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001537 return NULL;
1538}
1539
1540static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1541 gfp_t mem_flags)
1542{
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001543 struct list_head qtd_list;
1544 packet_enqueue *pe;
1545
1546 INIT_LIST_HEAD(&qtd_list);
1547
1548 switch (usb_pipetype(urb->pipe)) {
1549 case PIPE_CONTROL:
1550 case PIPE_BULK:
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001551 if (!qh_urb_transaction(hcd, urb, &qtd_list, mem_flags))
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001552 return -ENOMEM;
1553 pe = enqueue_an_ATL_packet;
1554 break;
1555
1556 case PIPE_INTERRUPT:
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001557 if (!qh_urb_transaction(hcd, urb, &qtd_list, mem_flags))
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001558 return -ENOMEM;
1559 pe = enqueue_an_INT_packet;
1560 break;
1561
1562 case PIPE_ISOCHRONOUS:
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001563 dev_err(hcd->self.controller, "PIPE_ISOCHRONOUS ain't supported\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001564 default:
1565 return -EPIPE;
1566 }
1567
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001568 return isp1760_prepare_enqueue(hcd, urb, &qtd_list, mem_flags, pe);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001569}
1570
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001571static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001572{
1573 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1574 struct inter_packet_info *ints;
1575 u32 i;
1576 u32 reg_base, or_reg, skip_reg;
Andrew Mortond249afd2008-06-09 16:39:52 -07001577 unsigned long flags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001578 struct ptd ptd;
Warren Free0afb20e2009-05-08 10:27:08 +02001579 packet_enqueue *pe;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001580
1581 switch (usb_pipetype(urb->pipe)) {
1582 case PIPE_ISOCHRONOUS:
1583 return -EPIPE;
1584 break;
1585
1586 case PIPE_INTERRUPT:
1587 ints = priv->int_ints;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001588 reg_base = INT_PTD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001589 or_reg = HC_INT_IRQ_MASK_OR_REG;
1590 skip_reg = HC_INT_PTD_SKIPMAP_REG;
Warren Free0afb20e2009-05-08 10:27:08 +02001591 pe = enqueue_an_INT_packet;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001592 break;
1593
1594 default:
1595 ints = priv->atl_ints;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001596 reg_base = ATL_PTD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001597 or_reg = HC_ATL_IRQ_MASK_OR_REG;
1598 skip_reg = HC_ATL_PTD_SKIPMAP_REG;
Warren Free0afb20e2009-05-08 10:27:08 +02001599 pe = enqueue_an_ATL_packet;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001600 break;
1601 }
1602
1603 memset(&ptd, 0, sizeof(ptd));
1604 spin_lock_irqsave(&priv->lock, flags);
1605
1606 for (i = 0; i < 32; i++) {
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001607 if (!ints[i].qh)
1608 continue;
1609 BUG_ON(!ints[i].qtd);
1610
1611 if (ints[i].qtd->urb == urb) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001612 u32 skip_map;
1613 u32 or_map;
1614 struct isp1760_qtd *qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001615 struct isp1760_qh *qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001616
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001617 skip_map = reg_read32(hcd->regs, skip_reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001618 skip_map |= 1 << i;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001619 reg_write32(hcd->regs, skip_reg, skip_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001620
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001621 or_map = reg_read32(hcd->regs, or_reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001622 or_map &= ~(1 << i);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001623 reg_write32(hcd->regs, or_reg, or_map);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001624
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001625 ptd_write(hcd->regs, reg_base, i, &ptd);
1626
Arvid Brodind3cf2a82011-03-07 15:36:21 +01001627 qtd = ints[i].qtd;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001628 qh = ints[i].qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001629
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001630 free_mem(hcd, qtd);
Arvid Brodina041d8e2011-02-26 22:04:40 +01001631 qtd = clean_up_qtdlist(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001632
Arvid Brodind3cf2a82011-03-07 15:36:21 +01001633 ints[i].qh = NULL;
1634 ints[i].qtd = NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001635
Arvid Brodind93da492011-04-26 21:46:47 +02001636 urb->status = status;
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001637 isp1760_urb_done(hcd, urb);
Warren Free0afb20e2009-05-08 10:27:08 +02001638 if (qtd)
1639 pe(hcd, qh, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001640 break;
Warren Free0afb20e2009-05-08 10:27:08 +02001641
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001642 } else {
1643 struct isp1760_qtd *qtd;
Warren Free0afb20e2009-05-08 10:27:08 +02001644
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001645 list_for_each_entry(qtd, &ints[i].qtd->qtd_list,
1646 qtd_list) {
Warren Free0afb20e2009-05-08 10:27:08 +02001647 if (qtd->urb == urb) {
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001648 clean_up_qtdlist(qtd, ints[i].qh);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001649 isp1760_urb_done(hcd, urb);
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001650 qtd = NULL;
Warren Free0afb20e2009-05-08 10:27:08 +02001651 break;
1652 }
Warren Free0afb20e2009-05-08 10:27:08 +02001653 }
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001654
1655 /* We found the urb before the last slot */
1656 if (!qtd)
Warren Free0afb20e2009-05-08 10:27:08 +02001657 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001658 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001659 }
1660
1661 spin_unlock_irqrestore(&priv->lock, flags);
1662 return 0;
1663}
1664
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001665static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001666{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001667 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001668 u32 imask;
1669 irqreturn_t irqret = IRQ_NONE;
1670
1671 spin_lock(&priv->lock);
1672
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001673 if (!(hcd->state & HC_STATE_RUNNING))
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001674 goto leave;
1675
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001676 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001677 if (unlikely(!imask))
1678 goto leave;
1679
Greg Kroah-Hartman753d8532011-04-14 13:37:07 -07001680 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask);
Sebastian Andrzej Siewiorb14e8402011-02-08 21:07:40 +01001681 if (imask & (HC_ATL_INT | HC_SOT_INT))
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001682 do_atl_int(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001683
1684 if (imask & HC_INTL_INT)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001685 do_intl_int(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001686
1687 irqret = IRQ_HANDLED;
1688leave:
1689 spin_unlock(&priv->lock);
1690 return irqret;
1691}
1692
1693static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1694{
1695 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1696 u32 temp, status = 0;
1697 u32 mask;
1698 int retval = 1;
1699 unsigned long flags;
1700
1701 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1702 if (!HC_IS_RUNNING(hcd->state))
1703 return 0;
1704
1705 /* init status to no-changes */
1706 buf[0] = 0;
1707 mask = PORT_CSC;
1708
1709 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001710 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001711
1712 if (temp & PORT_OWNER) {
1713 if (temp & PORT_CSC) {
1714 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001715 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001716 goto done;
1717 }
1718 }
1719
1720 /*
1721 * Return status information even for ports with OWNER set.
1722 * Otherwise khubd wouldn't see the disconnect event when a
1723 * high-speed device is switched over to the companion
1724 * controller by the user.
1725 */
1726
1727 if ((temp & mask) != 0
1728 || ((temp & PORT_RESUME) != 0
1729 && time_after_eq(jiffies,
1730 priv->reset_done))) {
1731 buf [0] |= 1 << (0 + 1);
1732 status = STS_PCD;
1733 }
1734 /* FIXME autosuspend idle root hubs */
1735done:
1736 spin_unlock_irqrestore(&priv->lock, flags);
1737 return status ? retval : 0;
1738}
1739
1740static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1741 struct usb_hub_descriptor *desc)
1742{
1743 int ports = HCS_N_PORTS(priv->hcs_params);
1744 u16 temp;
1745
1746 desc->bDescriptorType = 0x29;
1747 /* priv 1.0, 2.3.9 says 20ms max */
1748 desc->bPwrOn2PwrGood = 10;
1749 desc->bHubContrCurrent = 0;
1750
1751 desc->bNbrPorts = ports;
1752 temp = 1 + (ports / 8);
1753 desc->bDescLength = 7 + 2 * temp;
1754
Sarah Sharpda130512010-11-30 15:55:51 -08001755 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001756 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1757 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001758
1759 /* per-port overcurrent reporting */
1760 temp = 0x0008;
1761 if (HCS_PPC(priv->hcs_params))
1762 /* per-port power control */
1763 temp |= 0x0001;
1764 else
1765 /* no power switching */
1766 temp |= 0x0002;
1767 desc->wHubCharacteristics = cpu_to_le16(temp);
1768}
1769
1770#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1771
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001772static int check_reset_complete(struct usb_hcd *hcd, int index,
1773 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001774{
1775 if (!(port_status & PORT_CONNECT))
1776 return port_status;
1777
1778 /* if reset finished and it's still not enabled -- handoff */
1779 if (!(port_status & PORT_PE)) {
1780
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001781 dev_err(hcd->self.controller,
1782 "port %d full speed --> companion\n",
1783 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001784
1785 port_status |= PORT_OWNER;
1786 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001787 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001788
1789 } else
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001790 dev_err(hcd->self.controller, "port %d high speed\n",
1791 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001792
1793 return port_status;
1794}
1795
1796static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1797 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1798{
1799 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1800 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001801 u32 temp, status;
1802 unsigned long flags;
1803 int retval = 0;
1804 unsigned selector;
1805
1806 /*
1807 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1808 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1809 * (track current state ourselves) ... blink for diagnostics,
1810 * power, "this is the one", etc. EHCI spec supports this.
1811 */
1812
1813 spin_lock_irqsave(&priv->lock, flags);
1814 switch (typeReq) {
1815 case ClearHubFeature:
1816 switch (wValue) {
1817 case C_HUB_LOCAL_POWER:
1818 case C_HUB_OVER_CURRENT:
1819 /* no hub-wide feature/status flags */
1820 break;
1821 default:
1822 goto error;
1823 }
1824 break;
1825 case ClearPortFeature:
1826 if (!wIndex || wIndex > ports)
1827 goto error;
1828 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001829 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001830
1831 /*
1832 * Even if OWNER is set, so the port is owned by the
1833 * companion controller, khubd needs to be able to clear
1834 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001835 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001836 */
1837
1838 switch (wValue) {
1839 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001840 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001841 break;
1842 case USB_PORT_FEAT_C_ENABLE:
1843 /* XXX error? */
1844 break;
1845 case USB_PORT_FEAT_SUSPEND:
1846 if (temp & PORT_RESET)
1847 goto error;
1848
1849 if (temp & PORT_SUSPEND) {
1850 if ((temp & PORT_PE) == 0)
1851 goto error;
1852 /* resume signaling for 20 msec */
1853 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001854 reg_write32(hcd->regs, HC_PORTSC1,
1855 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001856 priv->reset_done = jiffies +
1857 msecs_to_jiffies(20);
1858 }
1859 break;
1860 case USB_PORT_FEAT_C_SUSPEND:
1861 /* we auto-clear this feature */
1862 break;
1863 case USB_PORT_FEAT_POWER:
1864 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001865 reg_write32(hcd->regs, HC_PORTSC1,
1866 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001867 break;
1868 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001869 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001870 break;
1871 case USB_PORT_FEAT_C_OVER_CURRENT:
1872 /* XXX error ?*/
1873 break;
1874 case USB_PORT_FEAT_C_RESET:
1875 /* GetPortStatus clears reset */
1876 break;
1877 default:
1878 goto error;
1879 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001880 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001881 break;
1882 case GetHubDescriptor:
1883 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1884 buf);
1885 break;
1886 case GetHubStatus:
1887 /* no hub-wide feature/status flags */
1888 memset(buf, 0, 4);
1889 break;
1890 case GetPortStatus:
1891 if (!wIndex || wIndex > ports)
1892 goto error;
1893 wIndex--;
1894 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001895 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001896
1897 /* wPortChange bits */
1898 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001899 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001900
1901
1902 /* whoever resumes must GetPortStatus to complete it!! */
1903 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001904 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001905
1906 /* Remote Wakeup received? */
1907 if (!priv->reset_done) {
1908 /* resume signaling for 20 msec */
1909 priv->reset_done = jiffies
1910 + msecs_to_jiffies(20);
1911 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001912 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001913 }
1914
1915 /* resume completed? */
1916 else if (time_after_eq(jiffies,
1917 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001918 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001919 priv->reset_done = 0;
1920
1921 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001922 temp = reg_read32(hcd->regs, HC_PORTSC1);
1923 reg_write32(hcd->regs, HC_PORTSC1,
1924 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1925 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001926 PORT_RESUME, 0, 2000 /* 2msec */);
1927 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001928 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001929 "port %d resume error %d\n",
1930 wIndex + 1, retval);
1931 goto error;
1932 }
1933 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1934 }
1935 }
1936
1937 /* whoever resets must GetPortStatus to complete it!! */
1938 if ((temp & PORT_RESET)
1939 && time_after_eq(jiffies,
1940 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001941 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001942 priv->reset_done = 0;
1943
1944 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001945 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001946 /* REVISIT: some hardware needs 550+ usec to clear
1947 * this bit; seems too long to spin routinely...
1948 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001949 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001950 PORT_RESET, 0, 750);
1951 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001952 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001953 wIndex + 1, retval);
1954 goto error;
1955 }
1956
1957 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001958 temp = check_reset_complete(hcd, wIndex,
1959 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001960 }
1961 /*
1962 * Even if OWNER is set, there's no harm letting khubd
1963 * see the wPortStatus values (they should all be 0 except
1964 * for PORT_POWER anyway).
1965 */
1966
1967 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001968 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001969
1970 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001971 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001972 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001973 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001974 }
1975 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001976 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001977 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001978 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001979 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001980 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001981 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001982 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001983
1984 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1985 break;
1986 case SetHubFeature:
1987 switch (wValue) {
1988 case C_HUB_LOCAL_POWER:
1989 case C_HUB_OVER_CURRENT:
1990 /* no hub-wide feature/status flags */
1991 break;
1992 default:
1993 goto error;
1994 }
1995 break;
1996 case SetPortFeature:
1997 selector = wIndex >> 8;
1998 wIndex &= 0xff;
1999 if (!wIndex || wIndex > ports)
2000 goto error;
2001 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002002 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002003 if (temp & PORT_OWNER)
2004 break;
2005
2006/* temp &= ~PORT_RWC_BITS; */
2007 switch (wValue) {
2008 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002009 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002010 break;
2011
2012 case USB_PORT_FEAT_SUSPEND:
2013 if ((temp & PORT_PE) == 0
2014 || (temp & PORT_RESET) != 0)
2015 goto error;
2016
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002017 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002018 break;
2019 case USB_PORT_FEAT_POWER:
2020 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002021 reg_write32(hcd->regs, HC_PORTSC1,
2022 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002023 break;
2024 case USB_PORT_FEAT_RESET:
2025 if (temp & PORT_RESUME)
2026 goto error;
2027 /* line status bits may report this as low speed,
2028 * which can be fine if this root hub has a
2029 * transaction translator built in.
2030 */
2031 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2032 && PORT_USB11(temp)) {
2033 temp |= PORT_OWNER;
2034 } else {
2035 temp |= PORT_RESET;
2036 temp &= ~PORT_PE;
2037
2038 /*
2039 * caller must wait, then call GetPortStatus
2040 * usb 2.0 spec says 50 ms resets on root
2041 */
2042 priv->reset_done = jiffies +
2043 msecs_to_jiffies(50);
2044 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002045 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002046 break;
2047 default:
2048 goto error;
2049 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002050 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002051 break;
2052
2053 default:
2054error:
2055 /* "stall" on error */
2056 retval = -EPIPE;
2057 }
2058 spin_unlock_irqrestore(&priv->lock, flags);
2059 return retval;
2060}
2061
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002062static void isp1760_endpoint_disable(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002063 struct usb_host_endpoint *ep)
2064{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002065 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002066 struct isp1760_qh *qh;
2067 struct isp1760_qtd *qtd;
Andrew Mortond249afd2008-06-09 16:39:52 -07002068 unsigned long flags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002069
2070 spin_lock_irqsave(&priv->lock, flags);
2071 qh = ep->hcpriv;
2072 if (!qh)
2073 goto out;
2074
2075 ep->hcpriv = NULL;
2076 do {
2077 /* more than entry might get removed */
2078 if (list_empty(&qh->qtd_list))
2079 break;
2080
2081 qtd = list_first_entry(&qh->qtd_list, struct isp1760_qtd,
2082 qtd_list);
2083
2084 if (qtd->status & URB_ENQUEUED) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002085 spin_unlock_irqrestore(&priv->lock, flags);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002086 isp1760_urb_dequeue(hcd, qtd->urb, -ECONNRESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002087 spin_lock_irqsave(&priv->lock, flags);
2088 } else {
2089 struct urb *urb;
2090
2091 urb = qtd->urb;
Arvid Brodinfd436ae2011-02-26 22:03:49 +01002092 clean_up_qtdlist(qtd, qh);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002093 urb->status = -ECONNRESET;
2094 isp1760_urb_done(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002095 }
2096 } while (1);
2097
2098 qh_destroy(qh);
2099 /* remove requests and leak them.
2100 * ATL are pretty fast done, INT could take a while...
2101 * The latter shoule be removed
2102 */
2103out:
2104 spin_unlock_irqrestore(&priv->lock, flags);
2105}
2106
2107static int isp1760_get_frame(struct usb_hcd *hcd)
2108{
2109 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2110 u32 fr;
2111
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002112 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002113 return (fr >> 3) % priv->periodic_size;
2114}
2115
2116static void isp1760_stop(struct usb_hcd *hcd)
2117{
2118 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002119 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002120
2121 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2122 NULL, 0);
2123 mdelay(20);
2124
2125 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002126 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002127 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002128 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2129 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002130 spin_unlock_irq(&priv->lock);
2131
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002132 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002133}
2134
2135static void isp1760_shutdown(struct usb_hcd *hcd)
2136{
Nate Case3faefc82008-06-17 11:11:38 -05002137 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002138
2139 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002140 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2141 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002142
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002143 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002144 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002145 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002146}
2147
2148static const struct hc_driver isp1760_hc_driver = {
2149 .description = "isp1760-hcd",
2150 .product_desc = "NXP ISP1760 USB Host Controller",
2151 .hcd_priv_size = sizeof(struct isp1760_hcd),
2152 .irq = isp1760_irq,
2153 .flags = HCD_MEMORY | HCD_USB2,
2154 .reset = isp1760_hc_setup,
2155 .start = isp1760_run,
2156 .stop = isp1760_stop,
2157 .shutdown = isp1760_shutdown,
2158 .urb_enqueue = isp1760_urb_enqueue,
2159 .urb_dequeue = isp1760_urb_dequeue,
2160 .endpoint_disable = isp1760_endpoint_disable,
2161 .get_frame_number = isp1760_get_frame,
2162 .hub_status_data = isp1760_hub_status_data,
2163 .hub_control = isp1760_hub_control,
2164};
2165
2166int __init init_kmem_once(void)
2167{
2168 qtd_cachep = kmem_cache_create("isp1760_qtd",
2169 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2170 SLAB_MEM_SPREAD, NULL);
2171
2172 if (!qtd_cachep)
2173 return -ENOMEM;
2174
2175 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2176 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2177
2178 if (!qh_cachep) {
2179 kmem_cache_destroy(qtd_cachep);
2180 return -ENOMEM;
2181 }
2182
2183 return 0;
2184}
2185
2186void deinit_kmem_cache(void)
2187{
2188 kmem_cache_destroy(qtd_cachep);
2189 kmem_cache_destroy(qh_cachep);
2190}
2191
Catalin Marinasf9031f22009-02-10 16:55:45 +00002192struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2193 int irq, unsigned long irqflags,
2194 struct device *dev, const char *busname,
2195 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002196{
2197 struct usb_hcd *hcd;
2198 struct isp1760_hcd *priv;
2199 int ret;
2200
2201 if (usb_disabled())
2202 return ERR_PTR(-ENODEV);
2203
2204 /* prevent usb-core allocating DMA pages */
2205 dev->dma_mask = NULL;
2206
Kay Sievers0031a062008-05-02 06:02:41 +02002207 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002208 if (!hcd)
2209 return ERR_PTR(-ENOMEM);
2210
2211 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002212 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002213 init_memory(priv);
2214 hcd->regs = ioremap(res_start, res_len);
2215 if (!hcd->regs) {
2216 ret = -EIO;
2217 goto err_put;
2218 }
2219
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002220 hcd->irq = irq;
2221 hcd->rsrc_start = res_start;
2222 hcd->rsrc_len = res_len;
2223
Nate Casee6942d62008-05-21 16:28:20 -05002224 ret = usb_add_hcd(hcd, irq, irqflags);
2225 if (ret)
2226 goto err_unmap;
2227
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002228 return hcd;
2229
2230err_unmap:
2231 iounmap(hcd->regs);
2232
2233err_put:
2234 usb_put_hcd(hcd);
2235
2236 return ERR_PTR(ret);
2237}
2238
2239MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2240MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2241MODULE_LICENSE("GPL v2");