blob: 14c9238a5017d2719a74e7d63961ccf6a775d5ed [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 *
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020011 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
12 *
Sebastian Siewiordb11e472008-04-24 00:37:04 +020013 */
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020019#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020020#include <linux/debugfs.h>
21#include <linux/uaccess.h>
22#include <linux/io.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000023#include <linux/mm.h>
Arvid Brodin6d50c602011-08-21 08:29:26 +020024#include <linux/timer.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020025#include <asm/unaligned.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000026#include <asm/cacheflush.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020027
Sebastian Siewiordb11e472008-04-24 00:37:04 +020028#include "isp1760-hcd.h"
29
30static struct kmem_cache *qtd_cachep;
31static struct kmem_cache *qh_cachep;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020032static struct kmem_cache *urb_listitem_cachep;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020033
34struct isp1760_hcd {
35 u32 hcs_params;
36 spinlock_t lock;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020037 struct slotinfo atl_slots[32];
Arvid Brodind05b6ec2011-05-20 00:17:45 +020038 int atl_done_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020039 struct slotinfo int_slots[32];
Arvid Brodind05b6ec2011-05-20 00:17:45 +020040 int int_done_map;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020041 struct memory_chunk memory_pool[BLOCKS];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020042 struct list_head controlqhs, bulkqhs, interruptqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020043
44 /* periodic schedule support */
45#define DEFAULT_I_TDPS 1024
46 unsigned periodic_size;
47 unsigned i_thresh;
48 unsigned long reset_done;
49 unsigned long next_statechange;
Nate Case3faefc82008-06-17 11:11:38 -050050 unsigned int devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020051};
52
53static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
54{
55 return (struct isp1760_hcd *) (hcd->hcd_priv);
56}
Sebastian Siewiordb11e472008-04-24 00:37:04 +020057
58/* Section 2.2 Host Controller Capability Registers */
59#define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
60#define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
61#define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
62#define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
63#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
64#define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
65#define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
66
67/* Section 2.3 Host Controller Operational Registers */
68#define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
69#define CMD_RESET (1<<1) /* reset HC not bus */
70#define CMD_RUN (1<<0) /* start/stop HC */
71#define STS_PCD (1<<2) /* port change detect */
72#define FLAG_CF (1<<0) /* true: we'll support "high speed" */
73
74#define PORT_OWNER (1<<13) /* true: companion hc owns this port */
75#define PORT_POWER (1<<12) /* true: has power (see PPC) */
76#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
77#define PORT_RESET (1<<8) /* reset port */
78#define PORT_SUSPEND (1<<7) /* suspend port */
79#define PORT_RESUME (1<<6) /* resume it */
80#define PORT_PE (1<<2) /* port enable */
81#define PORT_CSC (1<<1) /* connect status change */
82#define PORT_CONNECT (1<<0) /* device connected */
83#define PORT_RWC_BITS (PORT_CSC)
84
85struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +020086 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020087 void *data_buffer;
Arvid Brodina041d8e2011-02-26 22:04:40 +010088 u32 payload_addr;
89
Sebastian Siewiordb11e472008-04-24 00:37:04 +020090 /* the rest is HCD-private */
91 struct list_head qtd_list;
92 struct urb *urb;
93 size_t length;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020094 size_t actual_length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020095
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020096 /* QTD_ENQUEUED: waiting for transfer (inactive) */
97 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
98 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
99 interrupt handler may touch this qtd! */
100 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
101 /* QTD_RETIRE: transfer error/abort qtd */
102#define QTD_ENQUEUED 0
103#define QTD_PAYLOAD_ALLOC 1
104#define QTD_XFER_STARTED 2
105#define QTD_XFER_COMPLETE 3
106#define QTD_RETIRE 4
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200107 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200108};
109
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200110/* Queue head, one for each active endpoint */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200111struct isp1760_qh {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200112 struct list_head qh_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200113 struct list_head qtd_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200114 u32 toggle;
115 u32 ping;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200116 int slot;
117};
118
119struct urb_listitem {
120 struct list_head urb_list;
121 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200122};
123
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100124/*
125 * Access functions for isp176x registers (addresses 0..0x03FF).
126 */
127static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200128{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100129 return readl(base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200130}
131
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100132static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200133{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100134 writel(val, base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200135}
136
137/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100138 * Access functions for isp176x memory (offset >= 0x0400).
139 *
140 * bank_reads8() reads memory locations prefetched by an earlier write to
141 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
142 * bank optimizations, you should use the more generic mem_reads8() below.
143 *
144 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
145 * below.
146 *
147 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200148 * doesn't quite work because some people have to enforce 32-bit access
149 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100150static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
151 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200152{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100153 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200154 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100155 __u8 *src_byteptr;
156 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200157
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100158 src = src_base + (bank_addr | src_offset);
159
160 if (src_offset < PAYLOAD_OFFSET) {
161 while (bytes >= 4) {
162 *dst = le32_to_cpu(__raw_readl(src));
163 bytes -= 4;
164 src++;
165 dst++;
166 }
167 } else {
168 while (bytes >= 4) {
169 *dst = __raw_readl(src);
170 bytes -= 4;
171 src++;
172 dst++;
173 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200174 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200175
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100176 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200177 return;
178
179 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
180 * allocated.
181 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100182 if (src_offset < PAYLOAD_OFFSET)
183 val = le32_to_cpu(__raw_readl(src));
184 else
185 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200186
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100187 dst_byteptr = (void *) dst;
188 src_byteptr = (void *) &val;
189 while (bytes > 0) {
190 *dst_byteptr = *src_byteptr;
191 dst_byteptr++;
192 src_byteptr++;
193 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200194 }
195}
196
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100197static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
198 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200199{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100200 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
201 ndelay(90);
202 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
203}
204
205static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
206 __u32 const *src, u32 bytes)
207{
208 __u32 __iomem *dst;
209
210 dst = dst_base + dst_offset;
211
212 if (dst_offset < PAYLOAD_OFFSET) {
213 while (bytes >= 4) {
214 __raw_writel(cpu_to_le32(*src), dst);
215 bytes -= 4;
216 src++;
217 dst++;
218 }
219 } else {
220 while (bytes >= 4) {
221 __raw_writel(*src, dst);
222 bytes -= 4;
223 src++;
224 dst++;
225 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200226 }
227
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100228 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200229 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100230 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
231 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200232 */
233
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100234 if (dst_offset < PAYLOAD_OFFSET)
235 __raw_writel(cpu_to_le32(*src), dst);
236 else
237 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200238}
239
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100240/*
241 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
242 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
243 */
244static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
245 struct ptd *ptd)
246{
247 reg_write32(base, HC_MEMORY_REG,
248 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
249 ndelay(90);
250 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
251 (void *) ptd, sizeof(*ptd));
252}
253
254static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
255 struct ptd *ptd)
256{
257 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
258 &ptd->dw1, 7*sizeof(ptd->dw1));
259 /* Make sure dw0 gets written last (after other dw's and after payload)
260 since it contains the enable bit */
261 wmb();
262 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
263 sizeof(ptd->dw0));
264}
265
266
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200267/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
268static void init_memory(struct isp1760_hcd *priv)
269{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100270 int i, curr;
271 u32 payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200272
Arvid Brodina041d8e2011-02-26 22:04:40 +0100273 payload_addr = PAYLOAD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200274 for (i = 0; i < BLOCK_1_NUM; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100275 priv->memory_pool[i].start = payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200276 priv->memory_pool[i].size = BLOCK_1_SIZE;
277 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100278 payload_addr += priv->memory_pool[i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200279 }
280
Arvid Brodina041d8e2011-02-26 22:04:40 +0100281 curr = i;
282 for (i = 0; i < BLOCK_2_NUM; i++) {
283 priv->memory_pool[curr + i].start = payload_addr;
284 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
285 priv->memory_pool[curr + i].free = 1;
286 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200287 }
288
Arvid Brodina041d8e2011-02-26 22:04:40 +0100289 curr = i;
290 for (i = 0; i < BLOCK_3_NUM; i++) {
291 priv->memory_pool[curr + i].start = payload_addr;
292 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
293 priv->memory_pool[curr + i].free = 1;
294 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200295 }
296
Arvid Brodin34537732011-04-26 21:47:37 +0200297 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200298}
299
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100300static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200301{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100302 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200303 int i;
304
Arvid Brodin34537732011-04-26 21:47:37 +0200305 WARN_ON(qtd->payload_addr);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100306
307 if (!qtd->length)
308 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200309
310 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100311 if (priv->memory_pool[i].size >= qtd->length &&
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200312 priv->memory_pool[i].free) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200313 priv->memory_pool[i].free = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100314 qtd->payload_addr = priv->memory_pool[i].start;
315 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200316 }
317 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200318}
319
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100320static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200321{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100322 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200323 int i;
324
Arvid Brodina041d8e2011-02-26 22:04:40 +0100325 if (!qtd->payload_addr)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200326 return;
327
328 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100329 if (priv->memory_pool[i].start == qtd->payload_addr) {
Arvid Brodin34537732011-04-26 21:47:37 +0200330 WARN_ON(priv->memory_pool[i].free);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200331 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100332 qtd->payload_addr = 0;
333 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200334 }
335 }
336
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100337 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
338 __func__, qtd->payload_addr);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200339 WARN_ON(1);
340 qtd->payload_addr = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200341}
342
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100343static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200344 u32 mask, u32 done, int usec)
345{
346 u32 result;
347
348 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100349 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200350 if (result == ~0)
351 return -ENODEV;
352 result &= mask;
353 if (result == done)
354 return 0;
355 udelay(1);
356 usec--;
357 } while (usec > 0);
358 return -ETIMEDOUT;
359}
360
361/* reset a non-running (STS_HALT == 1) controller */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100362static int ehci_reset(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200363{
364 int retval;
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100365 struct isp1760_hcd *priv = hcd_to_priv(hcd);
366
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100367 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200368
369 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100370 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200371 hcd->state = HC_STATE_HALT;
372 priv->next_statechange = jiffies;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100373 retval = handshake(hcd, HC_USBCMD,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200374 CMD_RESET, 0, 250 * 1000);
375 return retval;
376}
377
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200378static struct isp1760_qh *qh_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200379{
380 struct isp1760_qh *qh;
381
382 qh = kmem_cache_zalloc(qh_cachep, flags);
383 if (!qh)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200384 return NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200385
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200386 INIT_LIST_HEAD(&qh->qh_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200387 INIT_LIST_HEAD(&qh->qtd_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200388 qh->slot = -1;
389
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200390 return qh;
391}
392
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200393static void qh_free(struct isp1760_qh *qh)
394{
395 WARN_ON(!list_empty(&qh->qtd_list));
396 WARN_ON(qh->slot > -1);
397 kmem_cache_free(qh_cachep, qh);
398}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200399
400/* one-time init, only for memory state */
401static int priv_init(struct usb_hcd *hcd)
402{
403 struct isp1760_hcd *priv = hcd_to_priv(hcd);
404 u32 hcc_params;
405
406 spin_lock_init(&priv->lock);
407
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200408 INIT_LIST_HEAD(&priv->interruptqhs);
409 INIT_LIST_HEAD(&priv->controlqhs);
410 INIT_LIST_HEAD(&priv->bulkqhs);
411
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200412 /*
413 * hw default: 1K periodic list heads, one per frame.
414 * periodic_size can shrink by USBCMD update if hcc_params allows.
415 */
416 priv->periodic_size = DEFAULT_I_TDPS;
417
418 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100419 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200420 /* full frame cache */
421 if (HCC_ISOC_CACHE(hcc_params))
422 priv->i_thresh = 8;
423 else /* N microframes cached */
424 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
425
426 return 0;
427}
428
429static int isp1760_hc_setup(struct usb_hcd *hcd)
430{
431 struct isp1760_hcd *priv = hcd_to_priv(hcd);
432 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500433 u32 scratch, hwmode;
434
435 /* Setup HW Mode Control: This assumes a level active-low interrupt */
436 hwmode = HW_DATA_BUS_32BIT;
437
438 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
439 hwmode &= ~HW_DATA_BUS_32BIT;
440 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
441 hwmode |= HW_ANA_DIGI_OC;
442 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
443 hwmode |= HW_DACK_POL_HIGH;
444 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
445 hwmode |= HW_DREQ_POL_HIGH;
Michael Hennerich9da69c62009-07-15 23:22:54 -0400446 if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
447 hwmode |= HW_INTR_HIGH_ACT;
448 if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
449 hwmode |= HW_INTR_EDGE_TRIG;
Nate Case3faefc82008-06-17 11:11:38 -0500450
451 /*
452 * We have to set this first in case we're in 16-bit mode.
453 * Write it twice to ensure correct upper bits if switching
454 * to 16-bit mode.
455 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100456 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
457 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200458
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100459 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500460 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100461 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
462 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200463 if (scratch != 0xdeadbabe) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100464 dev_err(hcd->self.controller, "Scratch test failed.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200465 return -ENODEV;
466 }
467
468 /* pre reset */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200469 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
470 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
471 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
472 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200473
474 /* reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100475 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200476 mdelay(100);
477
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100478 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200479 mdelay(100);
480
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100481 result = ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200482 if (result)
483 return result;
484
485 /* Step 11 passed */
486
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100487 dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
Nate Case3faefc82008-06-17 11:11:38 -0500488 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
489 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
490 "analog" : "digital");
491
492 /* ATL reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100493 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500494 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100495 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500496
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100497 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
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200513static u32 base_to_chip(u32 base)
514{
515 return ((base - 0x400) >> 3);
516}
517
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100518static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
519{
520 struct urb *urb;
521
522 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
523 return 1;
524
525 urb = qtd->urb;
526 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
527 return (qtd->urb != urb);
528}
529
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200530/* magic numbers that can affect system performance */
531#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
532#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
533#define EHCI_TUNE_RL_TT 0
534#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
535#define EHCI_TUNE_MULT_TT 1
536#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
537
538static void create_ptd_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100539 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200540{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200541 u32 maxpacket;
542 u32 multi;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200543 u32 rl = RL_COUNTER;
544 u32 nak = NAK_COUNTER;
545
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100546 memset(ptd, 0, sizeof(*ptd));
547
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200548 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100549 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
550 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200551 multi = 1 + ((maxpacket >> 11) & 0x3);
552 maxpacket &= 0x7ff;
553
554 /* DW0 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200555 ptd->dw0 = DW0_VALID_BIT;
556 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
557 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
558 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200559
560 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100561 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200562 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
563 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200564
Arvid Brodina041d8e2011-02-26 22:04:40 +0100565 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200566 ptd->dw1 |= DW1_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100567 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200568 ptd->dw1 |= DW1_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200569
Arvid Brodina041d8e2011-02-26 22:04:40 +0100570 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200571 /* split transaction */
572
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200573 ptd->dw1 |= DW1_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100574 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200575 ptd->dw1 |= DW1_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200576
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200577 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
578 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200579
580 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100581 if (usb_pipeint(qtd->urb->pipe) &&
582 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100583 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200584
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200585 rl = 0;
586 nak = 0;
587 } else {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200588 ptd->dw0 |= TO_DW0_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100589 if (usb_pipecontrol(qtd->urb->pipe) ||
590 usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200591 ptd->dw3 |= TO_DW3_PING(qh->ping);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200592 }
593 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100594 ptd->dw2 = 0;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200595 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
596 ptd->dw2 |= TO_DW2_RL(rl);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200597
598 /* DW3 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200599 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
600 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100601 if (usb_pipecontrol(qtd->urb->pipe)) {
602 if (qtd->data_buffer == qtd->urb->setup_packet)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200603 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100604 else if (last_qtd_of_urb(qtd, qh))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200605 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100606 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200607
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200608 ptd->dw3 |= DW3_ACTIVE_BIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200609 /* Cerr */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200610 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200611}
612
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100613static void transform_add_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100614 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200615{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100616 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200617 u32 period;
618
Arvid Brodin65f1b522011-02-26 22:07:35 +0100619 /*
620 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
621 * the algorithm from the original Philips driver code, which was
622 * pretty much used in this driver before as well, is quite horrendous
623 * and, i believe, incorrect. The code below follows the datasheet and
624 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
625 * more reliable this way (fingers crossed...).
626 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200627
Arvid Brodin65f1b522011-02-26 22:07:35 +0100628 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
629 /* urb->interval is in units of microframes (1/8 ms) */
630 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200631
Arvid Brodin65f1b522011-02-26 22:07:35 +0100632 if (qtd->urb->interval > 4)
633 usof = 0x01; /* One bit set =>
634 interval 1 ms * uFrame-match */
635 else if (qtd->urb->interval > 2)
636 usof = 0x22; /* Two bits set => interval 1/2 ms */
637 else if (qtd->urb->interval > 1)
638 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200639 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100640 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200641 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100642 /* urb->interval is in units of frames (1 ms) */
643 period = qtd->urb->interval;
644 usof = 0x0f; /* Execute Start Split on any of the
645 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200646
Arvid Brodin65f1b522011-02-26 22:07:35 +0100647 /*
648 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
649 * complete split needs to be sent. Valid only for IN." Also,
650 * "All bits can be set to one for every transfer." (p 82,
651 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
652 * that number come from? 0xff seems to work fine...
653 */
654 /* ptd->dw5 = 0x1c; */
655 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200656 }
657
Arvid Brodin65f1b522011-02-26 22:07:35 +0100658 period = period >> 1;/* Ensure equal or shorter period than requested */
659 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
660
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100661 ptd->dw2 |= period;
662 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200663}
664
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200665static void create_ptd_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100666 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200667{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200668 create_ptd_atl(qh, qtd, ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100669 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200670}
671
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100672static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200673__releases(priv->lock)
674__acquires(priv->lock)
675{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100676 struct isp1760_hcd *priv = hcd_to_priv(hcd);
677
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200678 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100679 if (urb->status == -EINPROGRESS)
680 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200681 }
682
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000683 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
684 void *ptr;
685 for (ptr = urb->transfer_buffer;
686 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
687 ptr += PAGE_SIZE)
688 flush_dcache_page(virt_to_page(ptr));
689 }
690
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200691 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100692 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200693 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100694 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200695 spin_lock(&priv->lock);
696}
697
Arvid Brodin34537732011-04-26 21:47:37 +0200698static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
699 u8 packet_type)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200700{
Arvid Brodin34537732011-04-26 21:47:37 +0200701 struct isp1760_qtd *qtd;
702
703 qtd = kmem_cache_zalloc(qtd_cachep, flags);
704 if (!qtd)
705 return NULL;
706
707 INIT_LIST_HEAD(&qtd->qtd_list);
708 qtd->urb = urb;
709 qtd->packet_type = packet_type;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200710 qtd->status = QTD_ENQUEUED;
711 qtd->actual_length = 0;
Arvid Brodin34537732011-04-26 21:47:37 +0200712
713 return qtd;
714}
715
716static void qtd_free(struct isp1760_qtd *qtd)
717{
718 WARN_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200719 kmem_cache_free(qtd_cachep, qtd);
720}
721
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200722static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
723 struct slotinfo *slots, struct isp1760_qtd *qtd,
724 struct isp1760_qh *qh, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200725{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100726 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200727 int skip_map;
728
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200729 WARN_ON((slot < 0) || (slot > 31));
730 WARN_ON(qtd->length && !qtd->payload_addr);
731 WARN_ON(slots[slot].qtd);
732 WARN_ON(slots[slot].qh);
733 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200734
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200735 slots[slot].qtd = qtd;
736 slots[slot].qh = qh;
737 qh->slot = slot;
738 qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
739 interrupt routine may preempt and expects this value. */
Arvid Brodin6d50c602011-08-21 08:29:26 +0200740 slots[slot].timestamp = jiffies;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200741 ptd_write(hcd->regs, ptd_offset, slot, ptd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200742
743 /* Make sure done map has not triggered from some unlinked transfer */
744 if (ptd_offset == ATL_PTD_OFFSET) {
745 priv->atl_done_map |= reg_read32(hcd->regs,
746 HC_ATL_PTD_DONEMAP_REG);
747 priv->atl_done_map &= ~(1 << qh->slot);
748
749 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
750 skip_map &= ~(1 << qh->slot);
751 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
752 } else {
753 priv->int_done_map |= reg_read32(hcd->regs,
754 HC_INT_PTD_DONEMAP_REG);
755 priv->int_done_map &= ~(1 << qh->slot);
756
757 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
758 skip_map &= ~(1 << qh->slot);
759 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
760 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200761}
762
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200763static int is_short_bulk(struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200764{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200765 return (usb_pipebulk(qtd->urb->pipe) &&
766 (qtd->actual_length < qtd->length));
767}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200768
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200769static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
770 struct list_head *urb_list)
771{
772 int last_qtd;
773 struct isp1760_qtd *qtd, *qtd_next;
774 struct urb_listitem *urb_listitem;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200775
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200776 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
777 if (qtd->status < QTD_XFER_COMPLETE)
778 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200779
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200780 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
781 last_qtd = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200782 else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200783 last_qtd = qtd->urb != qtd_next->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200784
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200785 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
786 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200787
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200788 if (qtd->status == QTD_XFER_COMPLETE) {
789 if (qtd->actual_length) {
790 switch (qtd->packet_type) {
791 case IN_PID:
792 mem_reads8(hcd->regs, qtd->payload_addr,
793 qtd->data_buffer,
794 qtd->actual_length);
795 /* Fall through (?) */
796 case OUT_PID:
797 qtd->urb->actual_length +=
798 qtd->actual_length;
799 /* Fall through ... */
800 case SETUP_PID:
801 break;
802 }
803 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200804
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200805 if (is_short_bulk(qtd)) {
806 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
807 qtd->urb->status = -EREMOTEIO;
808 if (!last_qtd)
809 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200810 }
811 }
812
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200813 if (qtd->payload_addr)
814 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200815
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200816 if (last_qtd) {
817 if ((qtd->status == QTD_RETIRE) &&
818 (qtd->urb->status == -EINPROGRESS))
819 qtd->urb->status = -EPIPE;
820 /* Defer calling of urb_done() since it releases lock */
821 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
822 GFP_ATOMIC);
823 if (unlikely(!urb_listitem))
824 break;
825 urb_listitem->urb = qtd->urb;
826 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200827 }
828
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200829 list_del(&qtd->qtd_list);
830 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200831 }
832}
833
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200834#define ENQUEUE_DEPTH 2
835static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
836{
837 struct isp1760_hcd *priv = hcd_to_priv(hcd);
838 int ptd_offset;
839 struct slotinfo *slots;
840 int curr_slot, free_slot;
841 int n;
842 struct ptd ptd;
843 struct isp1760_qtd *qtd;
844
845 if (unlikely(list_empty(&qh->qtd_list))) {
846 WARN_ON(1);
847 return;
848 }
849
850 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
851 qtd_list)->urb->pipe)) {
852 ptd_offset = INT_PTD_OFFSET;
853 slots = priv->int_slots;
854 } else {
855 ptd_offset = ATL_PTD_OFFSET;
856 slots = priv->atl_slots;
857 }
858
859 free_slot = -1;
860 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
861 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
862 free_slot = curr_slot;
863 if (slots[curr_slot].qh == qh)
864 break;
865 }
866
867 n = 0;
868 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
869 if (qtd->status == QTD_ENQUEUED) {
870 WARN_ON(qtd->payload_addr);
871 alloc_mem(hcd, qtd);
872 if ((qtd->length) && (!qtd->payload_addr))
873 break;
874
875 if ((qtd->length) &&
876 ((qtd->packet_type == SETUP_PID) ||
877 (qtd->packet_type == OUT_PID))) {
878 mem_writes8(hcd->regs, qtd->payload_addr,
879 qtd->data_buffer, qtd->length);
880 }
881
882 qtd->status = QTD_PAYLOAD_ALLOC;
883 }
884
885 if (qtd->status == QTD_PAYLOAD_ALLOC) {
886/*
887 if ((curr_slot > 31) && (free_slot == -1))
888 dev_dbg(hcd->self.controller, "%s: No slot "
889 "available for transfer\n", __func__);
890*/
891 /* Start xfer for this endpoint if not already done */
892 if ((curr_slot > 31) && (free_slot > -1)) {
893 if (usb_pipeint(qtd->urb->pipe))
894 create_ptd_int(qh, qtd, &ptd);
895 else
896 create_ptd_atl(qh, qtd, &ptd);
897
898 start_bus_transfer(hcd, ptd_offset, free_slot,
899 slots, qtd, qh, &ptd);
900 curr_slot = free_slot;
901 }
902
903 n++;
904 if (n >= ENQUEUE_DEPTH)
905 break;
906 }
907 }
908}
909
910void schedule_ptds(struct usb_hcd *hcd)
911{
912 struct isp1760_hcd *priv;
913 struct isp1760_qh *qh, *qh_next;
914 struct list_head *ep_queue;
915 struct usb_host_endpoint *ep;
916 LIST_HEAD(urb_list);
917 struct urb_listitem *urb_listitem, *urb_listitem_next;
918
919 if (!hcd) {
920 WARN_ON(1);
921 return;
922 }
923
924 priv = hcd_to_priv(hcd);
925
926 /*
927 * check finished/retired xfers, transfer payloads, call urb_done()
928 */
929 ep_queue = &priv->interruptqhs;
930 while (ep_queue) {
931 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
932 ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
933 qtd_list)->urb->ep;
934 collect_qtds(hcd, qh, &urb_list);
935 if (list_empty(&qh->qtd_list)) {
936 list_del(&qh->qh_list);
937 if (ep->hcpriv == NULL) {
938 /* Endpoint has been disabled, so we
939 can free the associated queue head. */
940 qh_free(qh);
941 }
942 }
943 }
944
945 if (ep_queue == &priv->interruptqhs)
946 ep_queue = &priv->controlqhs;
947 else if (ep_queue == &priv->controlqhs)
948 ep_queue = &priv->bulkqhs;
949 else
950 ep_queue = NULL;
951 }
952
953 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
954 urb_list) {
955 isp1760_urb_done(hcd, urb_listitem->urb);
956 kmem_cache_free(urb_listitem_cachep, urb_listitem);
957 }
958
959 /*
960 * Schedule packets for transfer.
961 *
962 * According to USB2.0 specification:
963 *
964 * 1st prio: interrupt xfers, up to 80 % of bandwidth
965 * 2nd prio: control xfers
966 * 3rd prio: bulk xfers
967 *
968 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
969 * is very unclear on how to prioritize traffic):
970 *
971 * 1) Enqueue any queued control transfers, as long as payload chip mem
972 * and PTD ATL slots are available.
973 * 2) Enqueue any queued INT transfers, as long as payload chip mem
974 * and PTD INT slots are available.
975 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
976 * and PTD ATL slots are available.
977 *
978 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
979 * conservation of chip mem and performance.
980 *
981 * I'm sure this scheme could be improved upon!
982 */
983 ep_queue = &priv->controlqhs;
984 while (ep_queue) {
985 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
986 enqueue_qtds(hcd, qh);
987
988 if (ep_queue == &priv->controlqhs)
989 ep_queue = &priv->interruptqhs;
990 else if (ep_queue == &priv->interruptqhs)
991 ep_queue = &priv->bulkqhs;
992 else
993 ep_queue = NULL;
994 }
995}
996
997#define PTD_STATE_QTD_DONE 1
998#define PTD_STATE_QTD_RELOAD 2
999#define PTD_STATE_URB_RETIRE 3
1000
1001static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1002 struct urb *urb)
1003{
1004 __dw dw4;
1005 int i;
1006
1007 dw4 = ptd->dw4;
1008 dw4 >>= 8;
1009
1010 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1011 need to handle these errors? Is it done in hardware? */
1012
1013 if (ptd->dw3 & DW3_HALT_BIT) {
1014
1015 urb->status = -EPROTO; /* Default unknown error */
1016
1017 for (i = 0; i < 8; i++) {
1018 switch (dw4 & 0x7) {
1019 case INT_UNDERRUN:
1020 dev_dbg(hcd->self.controller, "%s: underrun "
1021 "during uFrame %d\n",
1022 __func__, i);
1023 urb->status = -ECOMM; /* Could not write data */
1024 break;
1025 case INT_EXACT:
1026 dev_dbg(hcd->self.controller, "%s: transaction "
1027 "error during uFrame %d\n",
1028 __func__, i);
1029 urb->status = -EPROTO; /* timeout, bad CRC, PID
1030 error etc. */
1031 break;
1032 case INT_BABBLE:
1033 dev_dbg(hcd->self.controller, "%s: babble "
1034 "error during uFrame %d\n",
1035 __func__, i);
1036 urb->status = -EOVERFLOW;
1037 break;
1038 }
1039 dw4 >>= 3;
1040 }
1041
1042 return PTD_STATE_URB_RETIRE;
1043 }
1044
1045 return PTD_STATE_QTD_DONE;
1046}
1047
1048static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1049 struct urb *urb)
1050{
1051 WARN_ON(!ptd);
1052 if (ptd->dw3 & DW3_HALT_BIT) {
1053 if (ptd->dw3 & DW3_BABBLE_BIT)
1054 urb->status = -EOVERFLOW;
1055 else if (FROM_DW3_CERR(ptd->dw3))
1056 urb->status = -EPIPE; /* Stall */
1057 else if (ptd->dw3 & DW3_ERROR_BIT)
1058 urb->status = -EPROTO; /* XactErr */
1059 else
1060 urb->status = -EPROTO; /* Unknown */
1061/*
1062 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1063 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1064 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1065 __func__,
1066 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1067 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1068*/
1069 return PTD_STATE_URB_RETIRE;
1070 }
1071
1072 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1073 /* Transfer Error, *but* active and no HALT -> reload */
1074 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1075 return PTD_STATE_QTD_RELOAD;
1076 }
1077
1078 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1079 /*
1080 * NAKs are handled in HW by the chip. Usually if the
1081 * device is not able to send data fast enough.
1082 * This happens mostly on slower hardware.
1083 */
1084 return PTD_STATE_QTD_RELOAD;
1085 }
1086
1087 return PTD_STATE_QTD_DONE;
1088}
1089
Arvid Brodin6d50c602011-08-21 08:29:26 +02001090static void handle_done_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001091{
1092 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001093 struct ptd ptd;
1094 struct isp1760_qh *qh;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001095 int slot;
1096 int state;
1097 struct slotinfo *slots;
1098 u32 ptd_offset;
1099 struct isp1760_qtd *qtd;
1100 int modified;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001101 int skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001102
Arvid Brodin6d50c602011-08-21 08:29:26 +02001103 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1104 priv->int_done_map &= ~skip_map;
1105 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1106 priv->atl_done_map &= ~skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001107
Arvid Brodin6d50c602011-08-21 08:29:26 +02001108 modified = priv->int_done_map || priv->atl_done_map;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001109
1110 while (priv->int_done_map || priv->atl_done_map) {
1111 if (priv->int_done_map) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001112 /* INT ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001113 slot = __ffs(priv->int_done_map);
1114 priv->int_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001115 slots = priv->int_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001116 /* This should not trigger, and could be removed if
1117 noone have any problems with it triggering: */
1118 if (!slots[slot].qh) {
1119 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001120 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001121 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001122 ptd_offset = INT_PTD_OFFSET;
1123 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1124 state = check_int_transfer(hcd, &ptd,
1125 slots[slot].qtd->urb);
1126 } else {
1127 /* ATL ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001128 slot = __ffs(priv->atl_done_map);
1129 priv->atl_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001130 slots = priv->atl_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001131 /* This should not trigger, and could be removed if
1132 noone have any problems with it triggering: */
1133 if (!slots[slot].qh) {
1134 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001135 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001136 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001137 ptd_offset = ATL_PTD_OFFSET;
1138 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1139 state = check_atl_transfer(hcd, &ptd,
1140 slots[slot].qtd->urb);
1141 }
1142
1143 qtd = slots[slot].qtd;
1144 slots[slot].qtd = NULL;
1145 qh = slots[slot].qh;
1146 slots[slot].qh = NULL;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001147 qh->slot = -1;
1148
1149 WARN_ON(qtd->status != QTD_XFER_STARTED);
1150
1151 switch (state) {
1152 case PTD_STATE_QTD_DONE:
1153 if ((usb_pipeint(qtd->urb->pipe)) &&
1154 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1155 qtd->actual_length =
1156 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1157 else
1158 qtd->actual_length =
1159 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1160
1161 qtd->status = QTD_XFER_COMPLETE;
1162 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1163 is_short_bulk(qtd))
1164 qtd = NULL;
1165 else
1166 qtd = list_entry(qtd->qtd_list.next,
1167 typeof(*qtd), qtd_list);
1168
1169 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1170 qh->ping = FROM_DW3_PING(ptd.dw3);
1171 break;
1172
1173 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1174 qtd->status = QTD_PAYLOAD_ALLOC;
1175 ptd.dw0 |= DW0_VALID_BIT;
1176 /* RL counter = ERR counter */
1177 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1178 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1179 ptd.dw3 &= ~TO_DW3_CERR(3);
1180 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1181 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1182 qh->ping = FROM_DW3_PING(ptd.dw3);
1183 break;
1184
1185 case PTD_STATE_URB_RETIRE:
1186 qtd->status = QTD_RETIRE;
1187 qtd = NULL;
1188 qh->toggle = 0;
1189 qh->ping = 0;
1190 break;
1191
1192 default:
1193 WARN_ON(1);
1194 continue;
1195 }
1196
1197 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1198 if (slots == priv->int_slots) {
1199 if (state == PTD_STATE_QTD_RELOAD)
1200 dev_err(hcd->self.controller,
1201 "%s: PTD_STATE_QTD_RELOAD on "
1202 "interrupt packet\n", __func__);
1203 if (state != PTD_STATE_QTD_RELOAD)
1204 create_ptd_int(qh, qtd, &ptd);
1205 } else {
1206 if (state != PTD_STATE_QTD_RELOAD)
1207 create_ptd_atl(qh, qtd, &ptd);
1208 }
1209
1210 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1211 qh, &ptd);
1212 }
1213 }
1214
1215 if (modified)
1216 schedule_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001217}
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001218
Arvid Brodin6d50c602011-08-21 08:29:26 +02001219static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1220{
1221 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1222 u32 imask;
1223 irqreturn_t irqret = IRQ_NONE;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001224
Arvid Brodin6d50c602011-08-21 08:29:26 +02001225 spin_lock(&priv->lock);
1226
1227 if (!(hcd->state & HC_STATE_RUNNING))
1228 goto leave;
1229
1230 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1231 if (unlikely(!imask))
1232 goto leave;
1233 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1234
1235 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1236 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1237
1238 handle_done_ptds(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001239
1240 irqret = IRQ_HANDLED;
1241leave:
1242 spin_unlock(&priv->lock);
1243
1244 return irqret;
1245}
1246
Arvid Brodin6d50c602011-08-21 08:29:26 +02001247/*
1248 * Workaround for problem described in chip errata 2:
1249 *
1250 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1251 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1252 * ATL done interrupts (the "instead of" might be important since it seems
1253 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1254 * to set the PTD's done bit in addition to not generating an interrupt!).
1255 *
1256 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1257 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1258 *
1259 * If we use SOF interrupts only, we get latency between ptd completion and the
1260 * actual handling. This is very noticeable in testusb runs which takes several
1261 * minutes longer without ATL interrupts.
1262 *
1263 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1264 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1265 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1266 * completed and its done map bit is set.
1267 *
1268 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1269 * not to cause too much lag when this HW bug occurs, while still hopefully
1270 * ensuring that the check does not falsely trigger.
1271 */
1272#define SLOT_TIMEOUT 180
1273#define SLOT_CHECK_PERIOD 200
1274static struct timer_list errata2_timer;
1275
1276void errata2_function(unsigned long data)
1277{
1278 struct usb_hcd *hcd = (struct usb_hcd *) data;
1279 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1280 int slot;
1281 struct ptd ptd;
1282 unsigned long spinflags;
1283
1284 spin_lock_irqsave(&priv->lock, spinflags);
1285
1286 for (slot = 0; slot < 32; slot++)
1287 if ((priv->atl_slots[slot].qh || priv->atl_slots[slot].qtd) &&
1288 time_after(jiffies + SLOT_TIMEOUT * HZ / 1000,
1289 priv->atl_slots[slot].timestamp)) {
1290 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1291 if (!FROM_DW0_VALID(ptd.dw0) &&
1292 !FROM_DW3_ACTIVE(ptd.dw3))
1293 priv->atl_done_map |= 1 << slot;
1294 }
1295
1296 handle_done_ptds(hcd);
1297
1298 spin_unlock_irqrestore(&priv->lock, spinflags);
1299
1300 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1301 add_timer(&errata2_timer);
1302}
1303
Arvid Brodin0ba79052011-08-21 08:29:25 +02001304static int isp1760_run(struct usb_hcd *hcd)
1305{
1306 int retval;
1307 u32 temp;
1308 u32 command;
1309 u32 chipid;
1310
1311 hcd->uses_new_polling = 1;
1312
1313 hcd->state = HC_STATE_RUNNING;
1314
1315 /* Set PTD interrupt AND & OR maps */
1316 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1317 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1318 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1319 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1320 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1321 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1322 /* step 23 passed */
1323
1324 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1325 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1326
1327 command = reg_read32(hcd->regs, HC_USBCMD);
1328 command &= ~(CMD_LRESET|CMD_RESET);
1329 command |= CMD_RUN;
1330 reg_write32(hcd->regs, HC_USBCMD, command);
1331
1332 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1333 if (retval)
1334 return retval;
1335
1336 /*
1337 * XXX
1338 * Spec says to write FLAG_CF as last config action, priv code grabs
1339 * the semaphore while doing so.
1340 */
1341 down_write(&ehci_cf_port_reset_rwsem);
1342 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1343
1344 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1345 up_write(&ehci_cf_port_reset_rwsem);
1346 if (retval)
1347 return retval;
1348
Arvid Brodin6d50c602011-08-21 08:29:26 +02001349 init_timer(&errata2_timer);
1350 errata2_timer.function = errata2_function;
1351 errata2_timer.data = (unsigned long) hcd;
1352 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1353 add_timer(&errata2_timer);
1354
Arvid Brodin0ba79052011-08-21 08:29:25 +02001355 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1356 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1357 chipid & 0xffff, chipid >> 16);
1358
1359 /* PTD Register Init Part 2, Step 28 */
1360
1361 /* Setup registers controlling PTD checking */
1362 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1363 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1364 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1365 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1366 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1367 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1368 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1369 ATL_BUF_FILL | INT_BUF_FILL);
1370
1371 /* GRR this is run-once init(), being done every time the HC starts.
1372 * So long as they're part of class devices, we can't do it init()
1373 * since the class device isn't created that early.
1374 */
1375 return 0;
1376}
1377
Arvid Brodin34537732011-04-26 21:47:37 +02001378static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001379{
Arvid Brodin34537732011-04-26 21:47:37 +02001380 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001381
Arvid Brodin34537732011-04-26 21:47:37 +02001382 if (len > MAX_PAYLOAD_SIZE)
1383 len = MAX_PAYLOAD_SIZE;
1384 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001385
Arvid Brodin34537732011-04-26 21:47:37 +02001386 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001387}
1388
Arvid Brodin34537732011-04-26 21:47:37 +02001389static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001390{
Arvid Brodin34537732011-04-26 21:47:37 +02001391 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001392
Arvid Brodin34537732011-04-26 21:47:37 +02001393 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001394 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001395 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001396 }
1397}
1398
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001399/*
Arvid Brodin34537732011-04-26 21:47:37 +02001400 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1401 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001402 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001403#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001404static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001405 struct urb *urb, struct list_head *head, gfp_t flags)
1406{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001407 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001408 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001409 int len, maxpacketsize;
1410 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001411
1412 /*
1413 * URBs map to sequences of QTDs: one logical transaction
1414 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001415
Arvid Brodin34537732011-04-26 21:47:37 +02001416 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1417 /* XXX This looks like usb storage / SCSI bug */
1418 dev_err(hcd->self.controller,
1419 "buf is null, dma is %08lx len is %d\n",
1420 (long unsigned)urb->transfer_dma,
1421 urb->transfer_buffer_length);
1422 WARN_ON(1);
1423 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001424
Arvid Brodin34537732011-04-26 21:47:37 +02001425 if (usb_pipein(urb->pipe))
1426 packet_type = IN_PID;
1427 else
1428 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001429
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001430 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001431 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001432 if (!qtd)
1433 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001434 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001435 list_add_tail(&qtd->qtd_list, head);
1436
1437 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001438 if (urb->transfer_buffer_length == 0)
1439 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001440 }
1441
Arvid Brodin34537732011-04-26 21:47:37 +02001442 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1443 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001444
1445 /*
1446 * buffer gets wrapped in one or more qtds;
1447 * last one may be "short" (including zero len)
1448 * and may serve as a control status ack
1449 */
Arvid Brodin34537732011-04-26 21:47:37 +02001450 buf = urb->transfer_buffer;
1451 len = urb->transfer_buffer_length;
1452
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001453 for (;;) {
1454 int this_qtd_len;
1455
Arvid Brodin34537732011-04-26 21:47:37 +02001456 qtd = qtd_alloc(flags, urb, packet_type);
1457 if (!qtd)
1458 goto cleanup;
1459 this_qtd_len = qtd_fill(qtd, buf, len);
1460 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001461
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001462 len -= this_qtd_len;
1463 buf += this_qtd_len;
1464
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001465 if (len <= 0)
1466 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001467 }
1468
1469 /*
1470 * control requests may need a terminating data "status" ack;
1471 * bulk ones may need a terminating short packet (zero length).
1472 */
1473 if (urb->transfer_buffer_length != 0) {
1474 int one_more = 0;
1475
1476 if (usb_pipecontrol(urb->pipe)) {
1477 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001478 if (packet_type == IN_PID)
1479 packet_type = OUT_PID;
1480 else
1481 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001482 } else if (usb_pipebulk(urb->pipe)
1483 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001484 && !(urb->transfer_buffer_length %
1485 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001486 one_more = 1;
1487 }
1488 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001489 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001490 if (!qtd)
1491 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001492
1493 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001494 qtd_fill(qtd, NULL, 0);
1495 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001496 }
1497 }
1498
Arvid Brodin34537732011-04-26 21:47:37 +02001499 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001500
1501cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001502 qtd_list_free(head);
1503}
1504
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001505static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1506 gfp_t mem_flags)
1507{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001508 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1509 struct list_head *ep_queue;
1510 struct isp1760_qh *qh, *qhit;
1511 unsigned long spinflags;
1512 LIST_HEAD(new_qtds);
1513 int retval;
1514 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001515
1516 switch (usb_pipetype(urb->pipe)) {
1517 case PIPE_CONTROL:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001518 ep_queue = &priv->controlqhs;
1519 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001520 case PIPE_BULK:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001521 ep_queue = &priv->bulkqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001522 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001523 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001524 if (urb->interval < 0)
1525 return -EINVAL;
1526 /* FIXME: Check bandwidth */
1527 ep_queue = &priv->interruptqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001528 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001529 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001530 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1531 "not yet supported\n",
1532 __func__);
1533 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001534 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001535 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1536 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001537 return -EPIPE;
1538 }
1539
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001540 if (usb_pipein(urb->pipe))
1541 urb->actual_length = 0;
1542
1543 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1544 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001545 return -ENOMEM;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001546 urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
Arvid Brodin34537732011-04-26 21:47:37 +02001547
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001548 retval = 0;
1549 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001550
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001551 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1552 retval = -ESHUTDOWN;
1553 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001554 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001555 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1556 if (retval)
1557 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001558
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001559 qh = urb->ep->hcpriv;
1560 if (qh) {
1561 qh_in_queue = 0;
1562 list_for_each_entry(qhit, ep_queue, qh_list) {
1563 if (qhit == qh) {
1564 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001565 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001566 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001567 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001568 if (!qh_in_queue)
1569 list_add_tail(&qh->qh_list, ep_queue);
1570 } else {
1571 qh = qh_alloc(GFP_ATOMIC);
1572 if (!qh) {
1573 retval = -ENOMEM;
1574 goto out;
1575 }
1576 list_add_tail(&qh->qh_list, ep_queue);
1577 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001578 }
1579
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001580 list_splice_tail(&new_qtds, &qh->qtd_list);
1581 schedule_ptds(hcd);
1582
1583out:
1584 spin_unlock_irqrestore(&priv->lock, spinflags);
1585 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001586}
1587
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001588static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1589 struct isp1760_qh *qh)
1590{
1591 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1592 int skip_map;
1593
1594 WARN_ON(qh->slot == -1);
1595
1596 /* We need to forcefully reclaim the slot since some transfers never
1597 return, e.g. interrupt transfers and NAKed bulk transfers. */
Arvid Brodin8b1ab602011-06-17 18:45:37 +02001598 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001599 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1600 skip_map |= (1 << qh->slot);
1601 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1602 priv->atl_slots[qh->slot].qh = NULL;
1603 priv->atl_slots[qh->slot].qtd = NULL;
1604 } else {
1605 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1606 skip_map |= (1 << qh->slot);
1607 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1608 priv->int_slots[qh->slot].qh = NULL;
1609 priv->int_slots[qh->slot].qtd = NULL;
1610 }
1611
1612 qh->slot = -1;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001613}
1614
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001615static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1616 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001617{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001618 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001619 unsigned long spinflags;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001620 struct isp1760_qh *qh;
1621 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001622 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001623
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001624 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodin17d3e142011-07-20 03:13:46 +02001625 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1626 if (retval)
1627 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001628
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001629 qh = urb->ep->hcpriv;
1630 if (!qh) {
1631 retval = -EINVAL;
1632 goto out;
1633 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001634
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001635 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1636 if (qtd->urb == urb) {
1637 if (qtd->status == QTD_XFER_STARTED)
1638 kill_transfer(hcd, urb, qh);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001639 qtd->status = QTD_RETIRE;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001640 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001641
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001642 urb->status = status;
1643 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001644
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001645out:
1646 spin_unlock_irqrestore(&priv->lock, spinflags);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001647 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001648}
1649
Arvid Brodin079cdb02011-05-20 00:17:34 +02001650static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1651 struct usb_host_endpoint *ep)
1652{
1653 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001654 unsigned long spinflags;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001655 struct isp1760_qh *qh;
1656 struct isp1760_qtd *qtd;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001657
1658 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001659
Arvid Brodin079cdb02011-05-20 00:17:34 +02001660 qh = ep->hcpriv;
1661 if (!qh)
1662 goto out;
1663
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001664 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1665 if (qtd->status == QTD_XFER_STARTED)
1666 kill_transfer(hcd, qtd->urb, qh);
1667 qtd->status = QTD_RETIRE;
1668 qtd->urb->status = -ECONNRESET;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001669 }
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001670
Arvid Brodin079cdb02011-05-20 00:17:34 +02001671 ep->hcpriv = NULL;
1672 /* Cannot free qh here since it will be parsed by schedule_ptds() */
1673
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001674 schedule_ptds(hcd);
1675
Arvid Brodin079cdb02011-05-20 00:17:34 +02001676out:
1677 spin_unlock_irqrestore(&priv->lock, spinflags);
1678}
1679
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001680static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1681{
1682 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1683 u32 temp, status = 0;
1684 u32 mask;
1685 int retval = 1;
1686 unsigned long flags;
1687
1688 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1689 if (!HC_IS_RUNNING(hcd->state))
1690 return 0;
1691
1692 /* init status to no-changes */
1693 buf[0] = 0;
1694 mask = PORT_CSC;
1695
1696 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001697 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001698
1699 if (temp & PORT_OWNER) {
1700 if (temp & PORT_CSC) {
1701 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001702 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001703 goto done;
1704 }
1705 }
1706
1707 /*
1708 * Return status information even for ports with OWNER set.
1709 * Otherwise khubd wouldn't see the disconnect event when a
1710 * high-speed device is switched over to the companion
1711 * controller by the user.
1712 */
1713
1714 if ((temp & mask) != 0
1715 || ((temp & PORT_RESUME) != 0
1716 && time_after_eq(jiffies,
1717 priv->reset_done))) {
1718 buf [0] |= 1 << (0 + 1);
1719 status = STS_PCD;
1720 }
1721 /* FIXME autosuspend idle root hubs */
1722done:
1723 spin_unlock_irqrestore(&priv->lock, flags);
1724 return status ? retval : 0;
1725}
1726
1727static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1728 struct usb_hub_descriptor *desc)
1729{
1730 int ports = HCS_N_PORTS(priv->hcs_params);
1731 u16 temp;
1732
1733 desc->bDescriptorType = 0x29;
1734 /* priv 1.0, 2.3.9 says 20ms max */
1735 desc->bPwrOn2PwrGood = 10;
1736 desc->bHubContrCurrent = 0;
1737
1738 desc->bNbrPorts = ports;
1739 temp = 1 + (ports / 8);
1740 desc->bDescLength = 7 + 2 * temp;
1741
Sarah Sharpda130512010-11-30 15:55:51 -08001742 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001743 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1744 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001745
1746 /* per-port overcurrent reporting */
1747 temp = 0x0008;
1748 if (HCS_PPC(priv->hcs_params))
1749 /* per-port power control */
1750 temp |= 0x0001;
1751 else
1752 /* no power switching */
1753 temp |= 0x0002;
1754 desc->wHubCharacteristics = cpu_to_le16(temp);
1755}
1756
1757#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1758
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001759static int check_reset_complete(struct usb_hcd *hcd, int index,
1760 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001761{
1762 if (!(port_status & PORT_CONNECT))
1763 return port_status;
1764
1765 /* if reset finished and it's still not enabled -- handoff */
1766 if (!(port_status & PORT_PE)) {
1767
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001768 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001769 "port %d full speed --> companion\n",
1770 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001771
1772 port_status |= PORT_OWNER;
1773 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001774 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001775
1776 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001777 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001778 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001779
1780 return port_status;
1781}
1782
1783static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1784 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1785{
1786 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1787 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001788 u32 temp, status;
1789 unsigned long flags;
1790 int retval = 0;
1791 unsigned selector;
1792
1793 /*
1794 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1795 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1796 * (track current state ourselves) ... blink for diagnostics,
1797 * power, "this is the one", etc. EHCI spec supports this.
1798 */
1799
1800 spin_lock_irqsave(&priv->lock, flags);
1801 switch (typeReq) {
1802 case ClearHubFeature:
1803 switch (wValue) {
1804 case C_HUB_LOCAL_POWER:
1805 case C_HUB_OVER_CURRENT:
1806 /* no hub-wide feature/status flags */
1807 break;
1808 default:
1809 goto error;
1810 }
1811 break;
1812 case ClearPortFeature:
1813 if (!wIndex || wIndex > ports)
1814 goto error;
1815 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001816 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001817
1818 /*
1819 * Even if OWNER is set, so the port is owned by the
1820 * companion controller, khubd needs to be able to clear
1821 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001822 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001823 */
1824
1825 switch (wValue) {
1826 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001827 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001828 break;
1829 case USB_PORT_FEAT_C_ENABLE:
1830 /* XXX error? */
1831 break;
1832 case USB_PORT_FEAT_SUSPEND:
1833 if (temp & PORT_RESET)
1834 goto error;
1835
1836 if (temp & PORT_SUSPEND) {
1837 if ((temp & PORT_PE) == 0)
1838 goto error;
1839 /* resume signaling for 20 msec */
1840 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001841 reg_write32(hcd->regs, HC_PORTSC1,
1842 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001843 priv->reset_done = jiffies +
1844 msecs_to_jiffies(20);
1845 }
1846 break;
1847 case USB_PORT_FEAT_C_SUSPEND:
1848 /* we auto-clear this feature */
1849 break;
1850 case USB_PORT_FEAT_POWER:
1851 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001852 reg_write32(hcd->regs, HC_PORTSC1,
1853 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001854 break;
1855 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001856 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001857 break;
1858 case USB_PORT_FEAT_C_OVER_CURRENT:
1859 /* XXX error ?*/
1860 break;
1861 case USB_PORT_FEAT_C_RESET:
1862 /* GetPortStatus clears reset */
1863 break;
1864 default:
1865 goto error;
1866 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001867 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001868 break;
1869 case GetHubDescriptor:
1870 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1871 buf);
1872 break;
1873 case GetHubStatus:
1874 /* no hub-wide feature/status flags */
1875 memset(buf, 0, 4);
1876 break;
1877 case GetPortStatus:
1878 if (!wIndex || wIndex > ports)
1879 goto error;
1880 wIndex--;
1881 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001882 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001883
1884 /* wPortChange bits */
1885 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001886 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001887
1888
1889 /* whoever resumes must GetPortStatus to complete it!! */
1890 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001891 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001892
1893 /* Remote Wakeup received? */
1894 if (!priv->reset_done) {
1895 /* resume signaling for 20 msec */
1896 priv->reset_done = jiffies
1897 + msecs_to_jiffies(20);
1898 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001899 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001900 }
1901
1902 /* resume completed? */
1903 else if (time_after_eq(jiffies,
1904 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001905 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001906 priv->reset_done = 0;
1907
1908 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001909 temp = reg_read32(hcd->regs, HC_PORTSC1);
1910 reg_write32(hcd->regs, HC_PORTSC1,
1911 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1912 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001913 PORT_RESUME, 0, 2000 /* 2msec */);
1914 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001915 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001916 "port %d resume error %d\n",
1917 wIndex + 1, retval);
1918 goto error;
1919 }
1920 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1921 }
1922 }
1923
1924 /* whoever resets must GetPortStatus to complete it!! */
1925 if ((temp & PORT_RESET)
1926 && time_after_eq(jiffies,
1927 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001928 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001929 priv->reset_done = 0;
1930
1931 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001932 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001933 /* REVISIT: some hardware needs 550+ usec to clear
1934 * this bit; seems too long to spin routinely...
1935 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001936 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001937 PORT_RESET, 0, 750);
1938 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001939 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001940 wIndex + 1, retval);
1941 goto error;
1942 }
1943
1944 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001945 temp = check_reset_complete(hcd, wIndex,
1946 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001947 }
1948 /*
1949 * Even if OWNER is set, there's no harm letting khubd
1950 * see the wPortStatus values (they should all be 0 except
1951 * for PORT_POWER anyway).
1952 */
1953
1954 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001955 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001956
1957 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001958 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001959 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001960 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001961 }
1962 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001963 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001964 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001965 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001966 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001967 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001968 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001969 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001970
1971 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1972 break;
1973 case SetHubFeature:
1974 switch (wValue) {
1975 case C_HUB_LOCAL_POWER:
1976 case C_HUB_OVER_CURRENT:
1977 /* no hub-wide feature/status flags */
1978 break;
1979 default:
1980 goto error;
1981 }
1982 break;
1983 case SetPortFeature:
1984 selector = wIndex >> 8;
1985 wIndex &= 0xff;
1986 if (!wIndex || wIndex > ports)
1987 goto error;
1988 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001989 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001990 if (temp & PORT_OWNER)
1991 break;
1992
1993/* temp &= ~PORT_RWC_BITS; */
1994 switch (wValue) {
1995 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001996 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001997 break;
1998
1999 case USB_PORT_FEAT_SUSPEND:
2000 if ((temp & PORT_PE) == 0
2001 || (temp & PORT_RESET) != 0)
2002 goto error;
2003
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002004 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002005 break;
2006 case USB_PORT_FEAT_POWER:
2007 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002008 reg_write32(hcd->regs, HC_PORTSC1,
2009 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002010 break;
2011 case USB_PORT_FEAT_RESET:
2012 if (temp & PORT_RESUME)
2013 goto error;
2014 /* line status bits may report this as low speed,
2015 * which can be fine if this root hub has a
2016 * transaction translator built in.
2017 */
2018 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2019 && PORT_USB11(temp)) {
2020 temp |= PORT_OWNER;
2021 } else {
2022 temp |= PORT_RESET;
2023 temp &= ~PORT_PE;
2024
2025 /*
2026 * caller must wait, then call GetPortStatus
2027 * usb 2.0 spec says 50 ms resets on root
2028 */
2029 priv->reset_done = jiffies +
2030 msecs_to_jiffies(50);
2031 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002032 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002033 break;
2034 default:
2035 goto error;
2036 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002037 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002038 break;
2039
2040 default:
2041error:
2042 /* "stall" on error */
2043 retval = -EPIPE;
2044 }
2045 spin_unlock_irqrestore(&priv->lock, flags);
2046 return retval;
2047}
2048
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002049static int isp1760_get_frame(struct usb_hcd *hcd)
2050{
2051 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2052 u32 fr;
2053
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002054 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002055 return (fr >> 3) % priv->periodic_size;
2056}
2057
2058static void isp1760_stop(struct usb_hcd *hcd)
2059{
2060 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002061 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002062
Arvid Brodin6d50c602011-08-21 08:29:26 +02002063 del_timer(&errata2_timer);
2064
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002065 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2066 NULL, 0);
2067 mdelay(20);
2068
2069 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002070 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002071 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002072 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2073 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002074 spin_unlock_irq(&priv->lock);
2075
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002076 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002077}
2078
2079static void isp1760_shutdown(struct usb_hcd *hcd)
2080{
Nate Case3faefc82008-06-17 11:11:38 -05002081 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002082
2083 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002084 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2085 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002086
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002087 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002088 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002089 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002090}
2091
2092static const struct hc_driver isp1760_hc_driver = {
2093 .description = "isp1760-hcd",
2094 .product_desc = "NXP ISP1760 USB Host Controller",
2095 .hcd_priv_size = sizeof(struct isp1760_hcd),
2096 .irq = isp1760_irq,
2097 .flags = HCD_MEMORY | HCD_USB2,
2098 .reset = isp1760_hc_setup,
2099 .start = isp1760_run,
2100 .stop = isp1760_stop,
2101 .shutdown = isp1760_shutdown,
2102 .urb_enqueue = isp1760_urb_enqueue,
2103 .urb_dequeue = isp1760_urb_dequeue,
2104 .endpoint_disable = isp1760_endpoint_disable,
2105 .get_frame_number = isp1760_get_frame,
2106 .hub_status_data = isp1760_hub_status_data,
2107 .hub_control = isp1760_hub_control,
2108};
2109
2110int __init init_kmem_once(void)
2111{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002112 urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
2113 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2114 SLAB_MEM_SPREAD, NULL);
2115
2116 if (!urb_listitem_cachep)
2117 return -ENOMEM;
2118
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002119 qtd_cachep = kmem_cache_create("isp1760_qtd",
2120 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2121 SLAB_MEM_SPREAD, NULL);
2122
2123 if (!qtd_cachep)
2124 return -ENOMEM;
2125
2126 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2127 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2128
2129 if (!qh_cachep) {
2130 kmem_cache_destroy(qtd_cachep);
2131 return -ENOMEM;
2132 }
2133
2134 return 0;
2135}
2136
2137void deinit_kmem_cache(void)
2138{
2139 kmem_cache_destroy(qtd_cachep);
2140 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002141 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002142}
2143
Catalin Marinasf9031f22009-02-10 16:55:45 +00002144struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2145 int irq, unsigned long irqflags,
2146 struct device *dev, const char *busname,
2147 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002148{
2149 struct usb_hcd *hcd;
2150 struct isp1760_hcd *priv;
2151 int ret;
2152
2153 if (usb_disabled())
2154 return ERR_PTR(-ENODEV);
2155
2156 /* prevent usb-core allocating DMA pages */
2157 dev->dma_mask = NULL;
2158
Kay Sievers0031a062008-05-02 06:02:41 +02002159 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002160 if (!hcd)
2161 return ERR_PTR(-ENOMEM);
2162
2163 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002164 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002165 init_memory(priv);
2166 hcd->regs = ioremap(res_start, res_len);
2167 if (!hcd->regs) {
2168 ret = -EIO;
2169 goto err_put;
2170 }
2171
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002172 hcd->irq = irq;
2173 hcd->rsrc_start = res_start;
2174 hcd->rsrc_len = res_len;
2175
Nate Casee6942d62008-05-21 16:28:20 -05002176 ret = usb_add_hcd(hcd, irq, irqflags);
2177 if (ret)
2178 goto err_unmap;
2179
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002180 return hcd;
2181
2182err_unmap:
2183 iounmap(hcd->regs);
2184
2185err_put:
2186 usb_put_hcd(hcd);
2187
2188 return ERR_PTR(ret);
2189}
2190
2191MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2192MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2193MODULE_LICENSE("GPL v2");