blob: 1eb69e050941d96dd3edfd2621fbc633f79400d3 [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 Brodin7adc14b2011-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 Brodin7adc14b2011-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 Brodin7adc14b2011-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 Brodin7adc14b2011-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 Brodin38679b72011-08-21 08:29:27 +0200780 last_qtd = last_qtd_of_urb(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200781
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200782 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
783 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200784
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200785 if (qtd->status == QTD_XFER_COMPLETE) {
786 if (qtd->actual_length) {
787 switch (qtd->packet_type) {
788 case IN_PID:
789 mem_reads8(hcd->regs, qtd->payload_addr,
790 qtd->data_buffer,
791 qtd->actual_length);
792 /* Fall through (?) */
793 case OUT_PID:
794 qtd->urb->actual_length +=
795 qtd->actual_length;
796 /* Fall through ... */
797 case SETUP_PID:
798 break;
799 }
800 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200801
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200802 if (is_short_bulk(qtd)) {
803 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
804 qtd->urb->status = -EREMOTEIO;
805 if (!last_qtd)
806 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200807 }
808 }
809
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200810 if (qtd->payload_addr)
811 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200812
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200813 if (last_qtd) {
814 if ((qtd->status == QTD_RETIRE) &&
815 (qtd->urb->status == -EINPROGRESS))
816 qtd->urb->status = -EPIPE;
817 /* Defer calling of urb_done() since it releases lock */
818 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
819 GFP_ATOMIC);
820 if (unlikely(!urb_listitem))
Arvid Brodin38679b72011-08-21 08:29:27 +0200821 break; /* Try again on next call */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200822 urb_listitem->urb = qtd->urb;
823 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200824 }
825
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200826 list_del(&qtd->qtd_list);
827 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200828 }
829}
830
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200831#define ENQUEUE_DEPTH 2
832static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
833{
834 struct isp1760_hcd *priv = hcd_to_priv(hcd);
835 int ptd_offset;
836 struct slotinfo *slots;
837 int curr_slot, free_slot;
838 int n;
839 struct ptd ptd;
840 struct isp1760_qtd *qtd;
841
842 if (unlikely(list_empty(&qh->qtd_list))) {
843 WARN_ON(1);
844 return;
845 }
846
847 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
848 qtd_list)->urb->pipe)) {
849 ptd_offset = INT_PTD_OFFSET;
850 slots = priv->int_slots;
851 } else {
852 ptd_offset = ATL_PTD_OFFSET;
853 slots = priv->atl_slots;
854 }
855
856 free_slot = -1;
857 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
858 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
859 free_slot = curr_slot;
860 if (slots[curr_slot].qh == qh)
861 break;
862 }
863
864 n = 0;
865 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
866 if (qtd->status == QTD_ENQUEUED) {
867 WARN_ON(qtd->payload_addr);
868 alloc_mem(hcd, qtd);
869 if ((qtd->length) && (!qtd->payload_addr))
870 break;
871
872 if ((qtd->length) &&
873 ((qtd->packet_type == SETUP_PID) ||
874 (qtd->packet_type == OUT_PID))) {
875 mem_writes8(hcd->regs, qtd->payload_addr,
876 qtd->data_buffer, qtd->length);
877 }
878
879 qtd->status = QTD_PAYLOAD_ALLOC;
880 }
881
882 if (qtd->status == QTD_PAYLOAD_ALLOC) {
883/*
884 if ((curr_slot > 31) && (free_slot == -1))
885 dev_dbg(hcd->self.controller, "%s: No slot "
886 "available for transfer\n", __func__);
887*/
888 /* Start xfer for this endpoint if not already done */
889 if ((curr_slot > 31) && (free_slot > -1)) {
890 if (usb_pipeint(qtd->urb->pipe))
891 create_ptd_int(qh, qtd, &ptd);
892 else
893 create_ptd_atl(qh, qtd, &ptd);
894
895 start_bus_transfer(hcd, ptd_offset, free_slot,
896 slots, qtd, qh, &ptd);
897 curr_slot = free_slot;
898 }
899
900 n++;
901 if (n >= ENQUEUE_DEPTH)
902 break;
903 }
904 }
905}
906
907void schedule_ptds(struct usb_hcd *hcd)
908{
909 struct isp1760_hcd *priv;
910 struct isp1760_qh *qh, *qh_next;
911 struct list_head *ep_queue;
912 struct usb_host_endpoint *ep;
913 LIST_HEAD(urb_list);
914 struct urb_listitem *urb_listitem, *urb_listitem_next;
915
916 if (!hcd) {
917 WARN_ON(1);
918 return;
919 }
920
921 priv = hcd_to_priv(hcd);
922
923 /*
924 * check finished/retired xfers, transfer payloads, call urb_done()
925 */
926 ep_queue = &priv->interruptqhs;
927 while (ep_queue) {
928 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
929 ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
930 qtd_list)->urb->ep;
931 collect_qtds(hcd, qh, &urb_list);
932 if (list_empty(&qh->qtd_list)) {
933 list_del(&qh->qh_list);
934 if (ep->hcpriv == NULL) {
935 /* Endpoint has been disabled, so we
936 can free the associated queue head. */
937 qh_free(qh);
938 }
939 }
940 }
941
942 if (ep_queue == &priv->interruptqhs)
943 ep_queue = &priv->controlqhs;
944 else if (ep_queue == &priv->controlqhs)
945 ep_queue = &priv->bulkqhs;
946 else
947 ep_queue = NULL;
948 }
949
950 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
951 urb_list) {
952 isp1760_urb_done(hcd, urb_listitem->urb);
953 kmem_cache_free(urb_listitem_cachep, urb_listitem);
954 }
955
956 /*
957 * Schedule packets for transfer.
958 *
959 * According to USB2.0 specification:
960 *
961 * 1st prio: interrupt xfers, up to 80 % of bandwidth
962 * 2nd prio: control xfers
963 * 3rd prio: bulk xfers
964 *
965 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
966 * is very unclear on how to prioritize traffic):
967 *
968 * 1) Enqueue any queued control transfers, as long as payload chip mem
969 * and PTD ATL slots are available.
970 * 2) Enqueue any queued INT transfers, as long as payload chip mem
971 * and PTD INT slots are available.
972 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
973 * and PTD ATL slots are available.
974 *
975 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
976 * conservation of chip mem and performance.
977 *
978 * I'm sure this scheme could be improved upon!
979 */
980 ep_queue = &priv->controlqhs;
981 while (ep_queue) {
982 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
983 enqueue_qtds(hcd, qh);
984
985 if (ep_queue == &priv->controlqhs)
986 ep_queue = &priv->interruptqhs;
987 else if (ep_queue == &priv->interruptqhs)
988 ep_queue = &priv->bulkqhs;
989 else
990 ep_queue = NULL;
991 }
992}
993
994#define PTD_STATE_QTD_DONE 1
995#define PTD_STATE_QTD_RELOAD 2
996#define PTD_STATE_URB_RETIRE 3
997
998static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
999 struct urb *urb)
1000{
1001 __dw dw4;
1002 int i;
1003
1004 dw4 = ptd->dw4;
1005 dw4 >>= 8;
1006
1007 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1008 need to handle these errors? Is it done in hardware? */
1009
1010 if (ptd->dw3 & DW3_HALT_BIT) {
1011
1012 urb->status = -EPROTO; /* Default unknown error */
1013
1014 for (i = 0; i < 8; i++) {
1015 switch (dw4 & 0x7) {
1016 case INT_UNDERRUN:
1017 dev_dbg(hcd->self.controller, "%s: underrun "
1018 "during uFrame %d\n",
1019 __func__, i);
1020 urb->status = -ECOMM; /* Could not write data */
1021 break;
1022 case INT_EXACT:
1023 dev_dbg(hcd->self.controller, "%s: transaction "
1024 "error during uFrame %d\n",
1025 __func__, i);
1026 urb->status = -EPROTO; /* timeout, bad CRC, PID
1027 error etc. */
1028 break;
1029 case INT_BABBLE:
1030 dev_dbg(hcd->self.controller, "%s: babble "
1031 "error during uFrame %d\n",
1032 __func__, i);
1033 urb->status = -EOVERFLOW;
1034 break;
1035 }
1036 dw4 >>= 3;
1037 }
1038
1039 return PTD_STATE_URB_RETIRE;
1040 }
1041
1042 return PTD_STATE_QTD_DONE;
1043}
1044
1045static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1046 struct urb *urb)
1047{
1048 WARN_ON(!ptd);
1049 if (ptd->dw3 & DW3_HALT_BIT) {
1050 if (ptd->dw3 & DW3_BABBLE_BIT)
1051 urb->status = -EOVERFLOW;
1052 else if (FROM_DW3_CERR(ptd->dw3))
1053 urb->status = -EPIPE; /* Stall */
1054 else if (ptd->dw3 & DW3_ERROR_BIT)
1055 urb->status = -EPROTO; /* XactErr */
1056 else
1057 urb->status = -EPROTO; /* Unknown */
1058/*
1059 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1060 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1061 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1062 __func__,
1063 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1064 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1065*/
1066 return PTD_STATE_URB_RETIRE;
1067 }
1068
1069 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1070 /* Transfer Error, *but* active and no HALT -> reload */
1071 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1072 return PTD_STATE_QTD_RELOAD;
1073 }
1074
1075 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1076 /*
1077 * NAKs are handled in HW by the chip. Usually if the
1078 * device is not able to send data fast enough.
1079 * This happens mostly on slower hardware.
1080 */
1081 return PTD_STATE_QTD_RELOAD;
1082 }
1083
1084 return PTD_STATE_QTD_DONE;
1085}
1086
Arvid Brodin6d50c602011-08-21 08:29:26 +02001087static void handle_done_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001088{
1089 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001090 struct ptd ptd;
1091 struct isp1760_qh *qh;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001092 int slot;
1093 int state;
1094 struct slotinfo *slots;
1095 u32 ptd_offset;
1096 struct isp1760_qtd *qtd;
1097 int modified;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001098 int skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001099
Arvid Brodin6d50c602011-08-21 08:29:26 +02001100 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1101 priv->int_done_map &= ~skip_map;
1102 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1103 priv->atl_done_map &= ~skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001104
Arvid Brodin6d50c602011-08-21 08:29:26 +02001105 modified = priv->int_done_map || priv->atl_done_map;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001106
1107 while (priv->int_done_map || priv->atl_done_map) {
1108 if (priv->int_done_map) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001109 /* INT ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001110 slot = __ffs(priv->int_done_map);
1111 priv->int_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001112 slots = priv->int_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001113 /* This should not trigger, and could be removed if
1114 noone have any problems with it triggering: */
1115 if (!slots[slot].qh) {
1116 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001117 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001118 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001119 ptd_offset = INT_PTD_OFFSET;
1120 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1121 state = check_int_transfer(hcd, &ptd,
1122 slots[slot].qtd->urb);
1123 } else {
1124 /* ATL ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001125 slot = __ffs(priv->atl_done_map);
1126 priv->atl_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001127 slots = priv->atl_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001128 /* This should not trigger, and could be removed if
1129 noone have any problems with it triggering: */
1130 if (!slots[slot].qh) {
1131 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001132 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001133 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001134 ptd_offset = ATL_PTD_OFFSET;
1135 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1136 state = check_atl_transfer(hcd, &ptd,
1137 slots[slot].qtd->urb);
1138 }
1139
1140 qtd = slots[slot].qtd;
1141 slots[slot].qtd = NULL;
1142 qh = slots[slot].qh;
1143 slots[slot].qh = NULL;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001144 qh->slot = -1;
1145
1146 WARN_ON(qtd->status != QTD_XFER_STARTED);
1147
1148 switch (state) {
1149 case PTD_STATE_QTD_DONE:
1150 if ((usb_pipeint(qtd->urb->pipe)) &&
1151 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1152 qtd->actual_length =
1153 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1154 else
1155 qtd->actual_length =
1156 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1157
1158 qtd->status = QTD_XFER_COMPLETE;
1159 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1160 is_short_bulk(qtd))
1161 qtd = NULL;
1162 else
1163 qtd = list_entry(qtd->qtd_list.next,
1164 typeof(*qtd), qtd_list);
1165
1166 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1167 qh->ping = FROM_DW3_PING(ptd.dw3);
1168 break;
1169
1170 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1171 qtd->status = QTD_PAYLOAD_ALLOC;
1172 ptd.dw0 |= DW0_VALID_BIT;
1173 /* RL counter = ERR counter */
1174 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1175 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1176 ptd.dw3 &= ~TO_DW3_CERR(3);
1177 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1178 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1179 qh->ping = FROM_DW3_PING(ptd.dw3);
1180 break;
1181
1182 case PTD_STATE_URB_RETIRE:
1183 qtd->status = QTD_RETIRE;
1184 qtd = NULL;
1185 qh->toggle = 0;
1186 qh->ping = 0;
1187 break;
1188
1189 default:
1190 WARN_ON(1);
1191 continue;
1192 }
1193
1194 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1195 if (slots == priv->int_slots) {
1196 if (state == PTD_STATE_QTD_RELOAD)
1197 dev_err(hcd->self.controller,
1198 "%s: PTD_STATE_QTD_RELOAD on "
1199 "interrupt packet\n", __func__);
1200 if (state != PTD_STATE_QTD_RELOAD)
1201 create_ptd_int(qh, qtd, &ptd);
1202 } else {
1203 if (state != PTD_STATE_QTD_RELOAD)
1204 create_ptd_atl(qh, qtd, &ptd);
1205 }
1206
1207 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1208 qh, &ptd);
1209 }
1210 }
1211
1212 if (modified)
1213 schedule_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001214}
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001215
Arvid Brodin6d50c602011-08-21 08:29:26 +02001216static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1217{
1218 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1219 u32 imask;
1220 irqreturn_t irqret = IRQ_NONE;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001221
Arvid Brodin6d50c602011-08-21 08:29:26 +02001222 spin_lock(&priv->lock);
1223
1224 if (!(hcd->state & HC_STATE_RUNNING))
1225 goto leave;
1226
1227 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1228 if (unlikely(!imask))
1229 goto leave;
1230 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1231
1232 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1233 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1234
1235 handle_done_ptds(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001236
1237 irqret = IRQ_HANDLED;
1238leave:
1239 spin_unlock(&priv->lock);
1240
1241 return irqret;
1242}
1243
Arvid Brodin6d50c602011-08-21 08:29:26 +02001244/*
1245 * Workaround for problem described in chip errata 2:
1246 *
1247 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1248 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1249 * ATL done interrupts (the "instead of" might be important since it seems
1250 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1251 * to set the PTD's done bit in addition to not generating an interrupt!).
1252 *
1253 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1254 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1255 *
1256 * If we use SOF interrupts only, we get latency between ptd completion and the
1257 * actual handling. This is very noticeable in testusb runs which takes several
1258 * minutes longer without ATL interrupts.
1259 *
1260 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1261 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1262 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1263 * completed and its done map bit is set.
1264 *
1265 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1266 * not to cause too much lag when this HW bug occurs, while still hopefully
1267 * ensuring that the check does not falsely trigger.
1268 */
1269#define SLOT_TIMEOUT 180
1270#define SLOT_CHECK_PERIOD 200
1271static struct timer_list errata2_timer;
1272
1273void errata2_function(unsigned long data)
1274{
1275 struct usb_hcd *hcd = (struct usb_hcd *) data;
1276 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1277 int slot;
1278 struct ptd ptd;
1279 unsigned long spinflags;
1280
1281 spin_lock_irqsave(&priv->lock, spinflags);
1282
1283 for (slot = 0; slot < 32; slot++)
1284 if ((priv->atl_slots[slot].qh || priv->atl_slots[slot].qtd) &&
1285 time_after(jiffies + SLOT_TIMEOUT * HZ / 1000,
1286 priv->atl_slots[slot].timestamp)) {
1287 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1288 if (!FROM_DW0_VALID(ptd.dw0) &&
1289 !FROM_DW3_ACTIVE(ptd.dw3))
1290 priv->atl_done_map |= 1 << slot;
1291 }
1292
1293 handle_done_ptds(hcd);
1294
1295 spin_unlock_irqrestore(&priv->lock, spinflags);
1296
1297 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1298 add_timer(&errata2_timer);
1299}
1300
Arvid Brodin0ba79052011-08-21 08:29:25 +02001301static int isp1760_run(struct usb_hcd *hcd)
1302{
1303 int retval;
1304 u32 temp;
1305 u32 command;
1306 u32 chipid;
1307
1308 hcd->uses_new_polling = 1;
1309
1310 hcd->state = HC_STATE_RUNNING;
1311
1312 /* Set PTD interrupt AND & OR maps */
1313 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1314 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1315 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1316 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1317 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1318 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1319 /* step 23 passed */
1320
1321 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1322 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1323
1324 command = reg_read32(hcd->regs, HC_USBCMD);
1325 command &= ~(CMD_LRESET|CMD_RESET);
1326 command |= CMD_RUN;
1327 reg_write32(hcd->regs, HC_USBCMD, command);
1328
1329 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1330 if (retval)
1331 return retval;
1332
1333 /*
1334 * XXX
1335 * Spec says to write FLAG_CF as last config action, priv code grabs
1336 * the semaphore while doing so.
1337 */
1338 down_write(&ehci_cf_port_reset_rwsem);
1339 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1340
1341 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1342 up_write(&ehci_cf_port_reset_rwsem);
1343 if (retval)
1344 return retval;
1345
Arvid Brodin6d50c602011-08-21 08:29:26 +02001346 init_timer(&errata2_timer);
1347 errata2_timer.function = errata2_function;
1348 errata2_timer.data = (unsigned long) hcd;
1349 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1350 add_timer(&errata2_timer);
1351
Arvid Brodin0ba79052011-08-21 08:29:25 +02001352 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1353 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1354 chipid & 0xffff, chipid >> 16);
1355
1356 /* PTD Register Init Part 2, Step 28 */
1357
1358 /* Setup registers controlling PTD checking */
1359 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1360 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1361 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1362 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1363 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1364 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1365 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1366 ATL_BUF_FILL | INT_BUF_FILL);
1367
1368 /* GRR this is run-once init(), being done every time the HC starts.
1369 * So long as they're part of class devices, we can't do it init()
1370 * since the class device isn't created that early.
1371 */
1372 return 0;
1373}
1374
Arvid Brodin34537732011-04-26 21:47:37 +02001375static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001376{
Arvid Brodin34537732011-04-26 21:47:37 +02001377 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001378
Arvid Brodin34537732011-04-26 21:47:37 +02001379 if (len > MAX_PAYLOAD_SIZE)
1380 len = MAX_PAYLOAD_SIZE;
1381 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001382
Arvid Brodin34537732011-04-26 21:47:37 +02001383 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001384}
1385
Arvid Brodin34537732011-04-26 21:47:37 +02001386static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001387{
Arvid Brodin34537732011-04-26 21:47:37 +02001388 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001389
Arvid Brodin34537732011-04-26 21:47:37 +02001390 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001391 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001392 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001393 }
1394}
1395
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001396/*
Arvid Brodin34537732011-04-26 21:47:37 +02001397 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1398 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001399 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001400#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001401static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001402 struct urb *urb, struct list_head *head, gfp_t flags)
1403{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001404 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001405 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001406 int len, maxpacketsize;
1407 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001408
1409 /*
1410 * URBs map to sequences of QTDs: one logical transaction
1411 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001412
Arvid Brodin34537732011-04-26 21:47:37 +02001413 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1414 /* XXX This looks like usb storage / SCSI bug */
1415 dev_err(hcd->self.controller,
1416 "buf is null, dma is %08lx len is %d\n",
1417 (long unsigned)urb->transfer_dma,
1418 urb->transfer_buffer_length);
1419 WARN_ON(1);
1420 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001421
Arvid Brodin34537732011-04-26 21:47:37 +02001422 if (usb_pipein(urb->pipe))
1423 packet_type = IN_PID;
1424 else
1425 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001426
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001427 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001428 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001429 if (!qtd)
1430 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001431 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001432 list_add_tail(&qtd->qtd_list, head);
1433
1434 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001435 if (urb->transfer_buffer_length == 0)
1436 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001437 }
1438
Arvid Brodin34537732011-04-26 21:47:37 +02001439 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1440 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001441
1442 /*
1443 * buffer gets wrapped in one or more qtds;
1444 * last one may be "short" (including zero len)
1445 * and may serve as a control status ack
1446 */
Arvid Brodin34537732011-04-26 21:47:37 +02001447 buf = urb->transfer_buffer;
1448 len = urb->transfer_buffer_length;
1449
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001450 for (;;) {
1451 int this_qtd_len;
1452
Arvid Brodin34537732011-04-26 21:47:37 +02001453 qtd = qtd_alloc(flags, urb, packet_type);
1454 if (!qtd)
1455 goto cleanup;
1456 this_qtd_len = qtd_fill(qtd, buf, len);
1457 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001458
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001459 len -= this_qtd_len;
1460 buf += this_qtd_len;
1461
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001462 if (len <= 0)
1463 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001464 }
1465
1466 /*
1467 * control requests may need a terminating data "status" ack;
1468 * bulk ones may need a terminating short packet (zero length).
1469 */
1470 if (urb->transfer_buffer_length != 0) {
1471 int one_more = 0;
1472
1473 if (usb_pipecontrol(urb->pipe)) {
1474 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001475 if (packet_type == IN_PID)
1476 packet_type = OUT_PID;
1477 else
1478 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001479 } else if (usb_pipebulk(urb->pipe)
1480 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001481 && !(urb->transfer_buffer_length %
1482 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001483 one_more = 1;
1484 }
1485 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001486 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001487 if (!qtd)
1488 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001489
1490 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001491 qtd_fill(qtd, NULL, 0);
1492 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001493 }
1494 }
1495
Arvid Brodin34537732011-04-26 21:47:37 +02001496 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001497
1498cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001499 qtd_list_free(head);
1500}
1501
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001502static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1503 gfp_t mem_flags)
1504{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001505 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1506 struct list_head *ep_queue;
1507 struct isp1760_qh *qh, *qhit;
1508 unsigned long spinflags;
1509 LIST_HEAD(new_qtds);
1510 int retval;
1511 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001512
1513 switch (usb_pipetype(urb->pipe)) {
1514 case PIPE_CONTROL:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001515 ep_queue = &priv->controlqhs;
1516 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001517 case PIPE_BULK:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001518 ep_queue = &priv->bulkqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001519 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001520 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001521 if (urb->interval < 0)
1522 return -EINVAL;
1523 /* FIXME: Check bandwidth */
1524 ep_queue = &priv->interruptqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001525 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001526 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001527 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1528 "not yet supported\n",
1529 __func__);
1530 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001531 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001532 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1533 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001534 return -EPIPE;
1535 }
1536
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001537 if (usb_pipein(urb->pipe))
1538 urb->actual_length = 0;
1539
1540 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1541 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001542 return -ENOMEM;
1543
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001544 retval = 0;
1545 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001546
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001547 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1548 retval = -ESHUTDOWN;
1549 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001550 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001551 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1552 if (retval)
1553 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001554
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001555 qh = urb->ep->hcpriv;
1556 if (qh) {
1557 qh_in_queue = 0;
1558 list_for_each_entry(qhit, ep_queue, qh_list) {
1559 if (qhit == qh) {
1560 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001561 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001562 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001563 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001564 if (!qh_in_queue)
1565 list_add_tail(&qh->qh_list, ep_queue);
1566 } else {
1567 qh = qh_alloc(GFP_ATOMIC);
1568 if (!qh) {
1569 retval = -ENOMEM;
Arvid Brodin38679b72011-08-21 08:29:27 +02001570 usb_hcd_unlink_urb_from_ep(hcd, urb);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001571 goto out;
1572 }
1573 list_add_tail(&qh->qh_list, ep_queue);
1574 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001575 }
1576
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001577 list_splice_tail(&new_qtds, &qh->qtd_list);
1578 schedule_ptds(hcd);
1579
1580out:
1581 spin_unlock_irqrestore(&priv->lock, spinflags);
1582 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001583}
1584
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001585static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1586 struct isp1760_qh *qh)
1587{
1588 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1589 int skip_map;
1590
1591 WARN_ON(qh->slot == -1);
1592
1593 /* We need to forcefully reclaim the slot since some transfers never
1594 return, e.g. interrupt transfers and NAKed bulk transfers. */
Arvid Brodin8b1ab602011-06-17 18:45:37 +02001595 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001596 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1597 skip_map |= (1 << qh->slot);
1598 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1599 priv->atl_slots[qh->slot].qh = NULL;
1600 priv->atl_slots[qh->slot].qtd = NULL;
1601 } else {
1602 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1603 skip_map |= (1 << qh->slot);
1604 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1605 priv->int_slots[qh->slot].qh = NULL;
1606 priv->int_slots[qh->slot].qtd = NULL;
1607 }
1608
1609 qh->slot = -1;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001610}
1611
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001612static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1613 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001614{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001615 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001616 unsigned long spinflags;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001617 struct isp1760_qh *qh;
1618 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001619 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001620
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001621 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodin17d3e142011-07-20 03:13:46 +02001622 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1623 if (retval)
1624 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001625
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001626 qh = urb->ep->hcpriv;
1627 if (!qh) {
1628 retval = -EINVAL;
1629 goto out;
1630 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001631
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001632 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1633 if (qtd->urb == urb) {
1634 if (qtd->status == QTD_XFER_STARTED)
1635 kill_transfer(hcd, urb, qh);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001636 qtd->status = QTD_RETIRE;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001637 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001638
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001639 urb->status = status;
1640 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001641
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001642out:
1643 spin_unlock_irqrestore(&priv->lock, spinflags);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001644 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001645}
1646
Arvid Brodin079cdb02011-05-20 00:17:34 +02001647static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1648 struct usb_host_endpoint *ep)
1649{
1650 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001651 unsigned long spinflags;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001652 struct isp1760_qh *qh;
1653 struct isp1760_qtd *qtd;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001654
1655 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001656
Arvid Brodin079cdb02011-05-20 00:17:34 +02001657 qh = ep->hcpriv;
1658 if (!qh)
1659 goto out;
1660
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001661 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1662 if (qtd->status == QTD_XFER_STARTED)
1663 kill_transfer(hcd, qtd->urb, qh);
1664 qtd->status = QTD_RETIRE;
1665 qtd->urb->status = -ECONNRESET;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001666 }
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001667
Arvid Brodin079cdb02011-05-20 00:17:34 +02001668 ep->hcpriv = NULL;
1669 /* Cannot free qh here since it will be parsed by schedule_ptds() */
1670
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001671 schedule_ptds(hcd);
1672
Arvid Brodin079cdb02011-05-20 00:17:34 +02001673out:
1674 spin_unlock_irqrestore(&priv->lock, spinflags);
1675}
1676
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001677static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1678{
1679 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1680 u32 temp, status = 0;
1681 u32 mask;
1682 int retval = 1;
1683 unsigned long flags;
1684
1685 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1686 if (!HC_IS_RUNNING(hcd->state))
1687 return 0;
1688
1689 /* init status to no-changes */
1690 buf[0] = 0;
1691 mask = PORT_CSC;
1692
1693 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001694 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001695
1696 if (temp & PORT_OWNER) {
1697 if (temp & PORT_CSC) {
1698 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001699 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001700 goto done;
1701 }
1702 }
1703
1704 /*
1705 * Return status information even for ports with OWNER set.
1706 * Otherwise khubd wouldn't see the disconnect event when a
1707 * high-speed device is switched over to the companion
1708 * controller by the user.
1709 */
1710
1711 if ((temp & mask) != 0
1712 || ((temp & PORT_RESUME) != 0
1713 && time_after_eq(jiffies,
1714 priv->reset_done))) {
1715 buf [0] |= 1 << (0 + 1);
1716 status = STS_PCD;
1717 }
1718 /* FIXME autosuspend idle root hubs */
1719done:
1720 spin_unlock_irqrestore(&priv->lock, flags);
1721 return status ? retval : 0;
1722}
1723
1724static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1725 struct usb_hub_descriptor *desc)
1726{
1727 int ports = HCS_N_PORTS(priv->hcs_params);
1728 u16 temp;
1729
1730 desc->bDescriptorType = 0x29;
1731 /* priv 1.0, 2.3.9 says 20ms max */
1732 desc->bPwrOn2PwrGood = 10;
1733 desc->bHubContrCurrent = 0;
1734
1735 desc->bNbrPorts = ports;
1736 temp = 1 + (ports / 8);
1737 desc->bDescLength = 7 + 2 * temp;
1738
Sarah Sharpda130512010-11-30 15:55:51 -08001739 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001740 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1741 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001742
1743 /* per-port overcurrent reporting */
1744 temp = 0x0008;
1745 if (HCS_PPC(priv->hcs_params))
1746 /* per-port power control */
1747 temp |= 0x0001;
1748 else
1749 /* no power switching */
1750 temp |= 0x0002;
1751 desc->wHubCharacteristics = cpu_to_le16(temp);
1752}
1753
1754#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1755
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001756static int check_reset_complete(struct usb_hcd *hcd, int index,
1757 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001758{
1759 if (!(port_status & PORT_CONNECT))
1760 return port_status;
1761
1762 /* if reset finished and it's still not enabled -- handoff */
1763 if (!(port_status & PORT_PE)) {
1764
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001765 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001766 "port %d full speed --> companion\n",
1767 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001768
1769 port_status |= PORT_OWNER;
1770 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001771 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001772
1773 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001774 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001775 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001776
1777 return port_status;
1778}
1779
1780static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1781 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1782{
1783 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1784 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001785 u32 temp, status;
1786 unsigned long flags;
1787 int retval = 0;
1788 unsigned selector;
1789
1790 /*
1791 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1792 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1793 * (track current state ourselves) ... blink for diagnostics,
1794 * power, "this is the one", etc. EHCI spec supports this.
1795 */
1796
1797 spin_lock_irqsave(&priv->lock, flags);
1798 switch (typeReq) {
1799 case ClearHubFeature:
1800 switch (wValue) {
1801 case C_HUB_LOCAL_POWER:
1802 case C_HUB_OVER_CURRENT:
1803 /* no hub-wide feature/status flags */
1804 break;
1805 default:
1806 goto error;
1807 }
1808 break;
1809 case ClearPortFeature:
1810 if (!wIndex || wIndex > ports)
1811 goto error;
1812 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001813 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001814
1815 /*
1816 * Even if OWNER is set, so the port is owned by the
1817 * companion controller, khubd needs to be able to clear
1818 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001819 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001820 */
1821
1822 switch (wValue) {
1823 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001824 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001825 break;
1826 case USB_PORT_FEAT_C_ENABLE:
1827 /* XXX error? */
1828 break;
1829 case USB_PORT_FEAT_SUSPEND:
1830 if (temp & PORT_RESET)
1831 goto error;
1832
1833 if (temp & PORT_SUSPEND) {
1834 if ((temp & PORT_PE) == 0)
1835 goto error;
1836 /* resume signaling for 20 msec */
1837 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001838 reg_write32(hcd->regs, HC_PORTSC1,
1839 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001840 priv->reset_done = jiffies +
1841 msecs_to_jiffies(20);
1842 }
1843 break;
1844 case USB_PORT_FEAT_C_SUSPEND:
1845 /* we auto-clear this feature */
1846 break;
1847 case USB_PORT_FEAT_POWER:
1848 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001849 reg_write32(hcd->regs, HC_PORTSC1,
1850 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001851 break;
1852 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001853 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001854 break;
1855 case USB_PORT_FEAT_C_OVER_CURRENT:
1856 /* XXX error ?*/
1857 break;
1858 case USB_PORT_FEAT_C_RESET:
1859 /* GetPortStatus clears reset */
1860 break;
1861 default:
1862 goto error;
1863 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001864 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001865 break;
1866 case GetHubDescriptor:
1867 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1868 buf);
1869 break;
1870 case GetHubStatus:
1871 /* no hub-wide feature/status flags */
1872 memset(buf, 0, 4);
1873 break;
1874 case GetPortStatus:
1875 if (!wIndex || wIndex > ports)
1876 goto error;
1877 wIndex--;
1878 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001879 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001880
1881 /* wPortChange bits */
1882 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001883 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001884
1885
1886 /* whoever resumes must GetPortStatus to complete it!! */
1887 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001888 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001889
1890 /* Remote Wakeup received? */
1891 if (!priv->reset_done) {
1892 /* resume signaling for 20 msec */
1893 priv->reset_done = jiffies
1894 + msecs_to_jiffies(20);
1895 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001896 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001897 }
1898
1899 /* resume completed? */
1900 else if (time_after_eq(jiffies,
1901 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001902 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001903 priv->reset_done = 0;
1904
1905 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001906 temp = reg_read32(hcd->regs, HC_PORTSC1);
1907 reg_write32(hcd->regs, HC_PORTSC1,
1908 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1909 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001910 PORT_RESUME, 0, 2000 /* 2msec */);
1911 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001912 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001913 "port %d resume error %d\n",
1914 wIndex + 1, retval);
1915 goto error;
1916 }
1917 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1918 }
1919 }
1920
1921 /* whoever resets must GetPortStatus to complete it!! */
1922 if ((temp & PORT_RESET)
1923 && time_after_eq(jiffies,
1924 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001925 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001926 priv->reset_done = 0;
1927
1928 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001929 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001930 /* REVISIT: some hardware needs 550+ usec to clear
1931 * this bit; seems too long to spin routinely...
1932 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001933 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001934 PORT_RESET, 0, 750);
1935 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001936 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001937 wIndex + 1, retval);
1938 goto error;
1939 }
1940
1941 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001942 temp = check_reset_complete(hcd, wIndex,
1943 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001944 }
1945 /*
1946 * Even if OWNER is set, there's no harm letting khubd
1947 * see the wPortStatus values (they should all be 0 except
1948 * for PORT_POWER anyway).
1949 */
1950
1951 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001952 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001953
1954 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001955 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001956 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001957 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001958 }
1959 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001960 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001961 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001962 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001963 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001964 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001965 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001966 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001967
1968 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1969 break;
1970 case SetHubFeature:
1971 switch (wValue) {
1972 case C_HUB_LOCAL_POWER:
1973 case C_HUB_OVER_CURRENT:
1974 /* no hub-wide feature/status flags */
1975 break;
1976 default:
1977 goto error;
1978 }
1979 break;
1980 case SetPortFeature:
1981 selector = wIndex >> 8;
1982 wIndex &= 0xff;
1983 if (!wIndex || wIndex > ports)
1984 goto error;
1985 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001986 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001987 if (temp & PORT_OWNER)
1988 break;
1989
1990/* temp &= ~PORT_RWC_BITS; */
1991 switch (wValue) {
1992 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001993 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001994 break;
1995
1996 case USB_PORT_FEAT_SUSPEND:
1997 if ((temp & PORT_PE) == 0
1998 || (temp & PORT_RESET) != 0)
1999 goto error;
2000
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002001 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002002 break;
2003 case USB_PORT_FEAT_POWER:
2004 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002005 reg_write32(hcd->regs, HC_PORTSC1,
2006 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002007 break;
2008 case USB_PORT_FEAT_RESET:
2009 if (temp & PORT_RESUME)
2010 goto error;
2011 /* line status bits may report this as low speed,
2012 * which can be fine if this root hub has a
2013 * transaction translator built in.
2014 */
2015 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2016 && PORT_USB11(temp)) {
2017 temp |= PORT_OWNER;
2018 } else {
2019 temp |= PORT_RESET;
2020 temp &= ~PORT_PE;
2021
2022 /*
2023 * caller must wait, then call GetPortStatus
2024 * usb 2.0 spec says 50 ms resets on root
2025 */
2026 priv->reset_done = jiffies +
2027 msecs_to_jiffies(50);
2028 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002029 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002030 break;
2031 default:
2032 goto error;
2033 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002034 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002035 break;
2036
2037 default:
2038error:
2039 /* "stall" on error */
2040 retval = -EPIPE;
2041 }
2042 spin_unlock_irqrestore(&priv->lock, flags);
2043 return retval;
2044}
2045
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002046static int isp1760_get_frame(struct usb_hcd *hcd)
2047{
2048 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2049 u32 fr;
2050
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002051 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002052 return (fr >> 3) % priv->periodic_size;
2053}
2054
2055static void isp1760_stop(struct usb_hcd *hcd)
2056{
2057 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002058 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002059
Arvid Brodin6d50c602011-08-21 08:29:26 +02002060 del_timer(&errata2_timer);
2061
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002062 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2063 NULL, 0);
2064 mdelay(20);
2065
2066 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002067 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002068 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002069 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2070 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002071 spin_unlock_irq(&priv->lock);
2072
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002073 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002074}
2075
2076static void isp1760_shutdown(struct usb_hcd *hcd)
2077{
Nate Case3faefc82008-06-17 11:11:38 -05002078 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002079
2080 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002081 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2082 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002083
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002084 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002085 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002086 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002087}
2088
2089static const struct hc_driver isp1760_hc_driver = {
2090 .description = "isp1760-hcd",
2091 .product_desc = "NXP ISP1760 USB Host Controller",
2092 .hcd_priv_size = sizeof(struct isp1760_hcd),
2093 .irq = isp1760_irq,
2094 .flags = HCD_MEMORY | HCD_USB2,
2095 .reset = isp1760_hc_setup,
2096 .start = isp1760_run,
2097 .stop = isp1760_stop,
2098 .shutdown = isp1760_shutdown,
2099 .urb_enqueue = isp1760_urb_enqueue,
2100 .urb_dequeue = isp1760_urb_dequeue,
2101 .endpoint_disable = isp1760_endpoint_disable,
2102 .get_frame_number = isp1760_get_frame,
2103 .hub_status_data = isp1760_hub_status_data,
2104 .hub_control = isp1760_hub_control,
2105};
2106
2107int __init init_kmem_once(void)
2108{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002109 urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
2110 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2111 SLAB_MEM_SPREAD, NULL);
2112
2113 if (!urb_listitem_cachep)
2114 return -ENOMEM;
2115
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002116 qtd_cachep = kmem_cache_create("isp1760_qtd",
2117 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2118 SLAB_MEM_SPREAD, NULL);
2119
2120 if (!qtd_cachep)
2121 return -ENOMEM;
2122
2123 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2124 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2125
2126 if (!qh_cachep) {
2127 kmem_cache_destroy(qtd_cachep);
2128 return -ENOMEM;
2129 }
2130
2131 return 0;
2132}
2133
2134void deinit_kmem_cache(void)
2135{
2136 kmem_cache_destroy(qtd_cachep);
2137 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002138 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002139}
2140
Catalin Marinasf9031f22009-02-10 16:55:45 +00002141struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2142 int irq, unsigned long irqflags,
2143 struct device *dev, const char *busname,
2144 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002145{
2146 struct usb_hcd *hcd;
2147 struct isp1760_hcd *priv;
2148 int ret;
2149
2150 if (usb_disabled())
2151 return ERR_PTR(-ENODEV);
2152
2153 /* prevent usb-core allocating DMA pages */
2154 dev->dma_mask = NULL;
2155
Kay Sievers0031a062008-05-02 06:02:41 +02002156 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002157 if (!hcd)
2158 return ERR_PTR(-ENOMEM);
2159
2160 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002161 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002162 init_memory(priv);
2163 hcd->regs = ioremap(res_start, res_len);
2164 if (!hcd->regs) {
2165 ret = -EIO;
2166 goto err_put;
2167 }
2168
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002169 hcd->irq = irq;
2170 hcd->rsrc_start = res_start;
2171 hcd->rsrc_len = res_len;
2172
Nate Casee6942d62008-05-21 16:28:20 -05002173 ret = usb_add_hcd(hcd, irq, irqflags);
2174 if (ret)
2175 goto err_unmap;
2176
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002177 return hcd;
2178
2179err_unmap:
2180 iounmap(hcd->regs);
2181
2182err_put:
2183 usb_put_hcd(hcd);
2184
2185 return ERR_PTR(ret);
2186}
2187
2188MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2189MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2190MODULE_LICENSE("GPL v2");