blob: dd98a966b58bd262d375e1e7c04fc54f0febe476 [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>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020024#include <asm/unaligned.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000025#include <asm/cacheflush.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020026
Sebastian Siewiordb11e472008-04-24 00:37:04 +020027#include "isp1760-hcd.h"
28
29static struct kmem_cache *qtd_cachep;
30static struct kmem_cache *qh_cachep;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020031static struct kmem_cache *urb_listitem_cachep;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020032
33struct isp1760_hcd {
34 u32 hcs_params;
35 spinlock_t lock;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020036 struct slotinfo atl_slots[32];
37 struct slotinfo int_slots[32];
Sebastian Siewiordb11e472008-04-24 00:37:04 +020038 struct memory_chunk memory_pool[BLOCKS];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020039 struct list_head controlqhs, bulkqhs, interruptqhs;
40 int active_ptds;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020041
42 /* periodic schedule support */
43#define DEFAULT_I_TDPS 1024
44 unsigned periodic_size;
45 unsigned i_thresh;
46 unsigned long reset_done;
47 unsigned long next_statechange;
Nate Case3faefc82008-06-17 11:11:38 -050048 unsigned int devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020049};
50
51static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
52{
53 return (struct isp1760_hcd *) (hcd->hcd_priv);
54}
Sebastian Siewiordb11e472008-04-24 00:37:04 +020055
56/* Section 2.2 Host Controller Capability Registers */
57#define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
58#define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
59#define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
60#define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
61#define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
62#define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
63#define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
64
65/* Section 2.3 Host Controller Operational Registers */
66#define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
67#define CMD_RESET (1<<1) /* reset HC not bus */
68#define CMD_RUN (1<<0) /* start/stop HC */
69#define STS_PCD (1<<2) /* port change detect */
70#define FLAG_CF (1<<0) /* true: we'll support "high speed" */
71
72#define PORT_OWNER (1<<13) /* true: companion hc owns this port */
73#define PORT_POWER (1<<12) /* true: has power (see PPC) */
74#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
75#define PORT_RESET (1<<8) /* reset port */
76#define PORT_SUSPEND (1<<7) /* suspend port */
77#define PORT_RESUME (1<<6) /* resume it */
78#define PORT_PE (1<<2) /* port enable */
79#define PORT_CSC (1<<1) /* connect status change */
80#define PORT_CONNECT (1<<0) /* device connected */
81#define PORT_RWC_BITS (PORT_CSC)
82
83struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +020084 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020085 void *data_buffer;
Arvid Brodina041d8e2011-02-26 22:04:40 +010086 u32 payload_addr;
87
Sebastian Siewiordb11e472008-04-24 00:37:04 +020088 /* the rest is HCD-private */
89 struct list_head qtd_list;
90 struct urb *urb;
91 size_t length;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020092 size_t actual_length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020093
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020094 /* QTD_ENQUEUED: waiting for transfer (inactive) */
95 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
96 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
97 interrupt handler may touch this qtd! */
98 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
99 /* QTD_RETIRE: transfer error/abort qtd */
100#define QTD_ENQUEUED 0
101#define QTD_PAYLOAD_ALLOC 1
102#define QTD_XFER_STARTED 2
103#define QTD_XFER_COMPLETE 3
104#define QTD_RETIRE 4
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200105 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200106};
107
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200108/* Queue head, one for each active endpoint */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200109struct isp1760_qh {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200110 struct list_head qh_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200111 struct list_head qtd_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200112 u32 toggle;
113 u32 ping;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200114 int slot;
115};
116
117struct urb_listitem {
118 struct list_head urb_list;
119 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200120};
121
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100122/*
123 * Access functions for isp176x registers (addresses 0..0x03FF).
124 */
125static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200126{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100127 return readl(base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200128}
129
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100130static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200131{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100132 writel(val, base + reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200133}
134
135/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100136 * Access functions for isp176x memory (offset >= 0x0400).
137 *
138 * bank_reads8() reads memory locations prefetched by an earlier write to
139 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
140 * bank optimizations, you should use the more generic mem_reads8() below.
141 *
142 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
143 * below.
144 *
145 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200146 * doesn't quite work because some people have to enforce 32-bit access
147 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100148static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
149 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200150{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100151 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200152 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100153 __u8 *src_byteptr;
154 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200155
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100156 src = src_base + (bank_addr | src_offset);
157
158 if (src_offset < PAYLOAD_OFFSET) {
159 while (bytes >= 4) {
160 *dst = le32_to_cpu(__raw_readl(src));
161 bytes -= 4;
162 src++;
163 dst++;
164 }
165 } else {
166 while (bytes >= 4) {
167 *dst = __raw_readl(src);
168 bytes -= 4;
169 src++;
170 dst++;
171 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200172 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200173
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100174 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200175 return;
176
177 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
178 * allocated.
179 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100180 if (src_offset < PAYLOAD_OFFSET)
181 val = le32_to_cpu(__raw_readl(src));
182 else
183 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200184
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100185 dst_byteptr = (void *) dst;
186 src_byteptr = (void *) &val;
187 while (bytes > 0) {
188 *dst_byteptr = *src_byteptr;
189 dst_byteptr++;
190 src_byteptr++;
191 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200192 }
193}
194
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100195static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
196 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200197{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100198 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
199 ndelay(90);
200 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
201}
202
203static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
204 __u32 const *src, u32 bytes)
205{
206 __u32 __iomem *dst;
207
208 dst = dst_base + dst_offset;
209
210 if (dst_offset < PAYLOAD_OFFSET) {
211 while (bytes >= 4) {
212 __raw_writel(cpu_to_le32(*src), dst);
213 bytes -= 4;
214 src++;
215 dst++;
216 }
217 } else {
218 while (bytes >= 4) {
219 __raw_writel(*src, dst);
220 bytes -= 4;
221 src++;
222 dst++;
223 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200224 }
225
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100226 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200227 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100228 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
229 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200230 */
231
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100232 if (dst_offset < PAYLOAD_OFFSET)
233 __raw_writel(cpu_to_le32(*src), dst);
234 else
235 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200236}
237
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100238/*
239 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
240 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
241 */
242static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
243 struct ptd *ptd)
244{
245 reg_write32(base, HC_MEMORY_REG,
246 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
247 ndelay(90);
248 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
249 (void *) ptd, sizeof(*ptd));
250}
251
252static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
253 struct ptd *ptd)
254{
255 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
256 &ptd->dw1, 7*sizeof(ptd->dw1));
257 /* Make sure dw0 gets written last (after other dw's and after payload)
258 since it contains the enable bit */
259 wmb();
260 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
261 sizeof(ptd->dw0));
262}
263
264
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200265/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
266static void init_memory(struct isp1760_hcd *priv)
267{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100268 int i, curr;
269 u32 payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200270
Arvid Brodina041d8e2011-02-26 22:04:40 +0100271 payload_addr = PAYLOAD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200272 for (i = 0; i < BLOCK_1_NUM; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100273 priv->memory_pool[i].start = payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200274 priv->memory_pool[i].size = BLOCK_1_SIZE;
275 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100276 payload_addr += priv->memory_pool[i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200277 }
278
Arvid Brodina041d8e2011-02-26 22:04:40 +0100279 curr = i;
280 for (i = 0; i < BLOCK_2_NUM; i++) {
281 priv->memory_pool[curr + i].start = payload_addr;
282 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
283 priv->memory_pool[curr + i].free = 1;
284 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200285 }
286
Arvid Brodina041d8e2011-02-26 22:04:40 +0100287 curr = i;
288 for (i = 0; i < BLOCK_3_NUM; i++) {
289 priv->memory_pool[curr + i].start = payload_addr;
290 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
291 priv->memory_pool[curr + i].free = 1;
292 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200293 }
294
Arvid Brodin34537732011-04-26 21:47:37 +0200295 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200296}
297
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100298static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200299{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100300 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200301 int i;
302
Arvid Brodin34537732011-04-26 21:47:37 +0200303 WARN_ON(qtd->payload_addr);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100304
305 if (!qtd->length)
306 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200307
308 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100309 if (priv->memory_pool[i].size >= qtd->length &&
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200310 priv->memory_pool[i].free) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200311 priv->memory_pool[i].free = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100312 qtd->payload_addr = priv->memory_pool[i].start;
313 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200314 }
315 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200316}
317
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100318static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200319{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100320 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200321 int i;
322
Arvid Brodina041d8e2011-02-26 22:04:40 +0100323 if (!qtd->payload_addr)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200324 return;
325
326 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100327 if (priv->memory_pool[i].start == qtd->payload_addr) {
Arvid Brodin34537732011-04-26 21:47:37 +0200328 WARN_ON(priv->memory_pool[i].free);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200329 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100330 qtd->payload_addr = 0;
331 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200332 }
333 }
334
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100335 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
336 __func__, qtd->payload_addr);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200337 WARN_ON(1);
338 qtd->payload_addr = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200339}
340
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100341static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200342 u32 mask, u32 done, int usec)
343{
344 u32 result;
345
346 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100347 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200348 if (result == ~0)
349 return -ENODEV;
350 result &= mask;
351 if (result == done)
352 return 0;
353 udelay(1);
354 usec--;
355 } while (usec > 0);
356 return -ETIMEDOUT;
357}
358
359/* reset a non-running (STS_HALT == 1) controller */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100360static int ehci_reset(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200361{
362 int retval;
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100363 struct isp1760_hcd *priv = hcd_to_priv(hcd);
364
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100365 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200366
367 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100368 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200369 hcd->state = HC_STATE_HALT;
370 priv->next_statechange = jiffies;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100371 retval = handshake(hcd, HC_USBCMD,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200372 CMD_RESET, 0, 250 * 1000);
373 return retval;
374}
375
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200376static struct isp1760_qh *qh_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200377{
378 struct isp1760_qh *qh;
379
380 qh = kmem_cache_zalloc(qh_cachep, flags);
381 if (!qh)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200382 return NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200383
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200384 INIT_LIST_HEAD(&qh->qh_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200385 INIT_LIST_HEAD(&qh->qtd_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200386 qh->slot = -1;
387
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200388 return qh;
389}
390
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200391static void qh_free(struct isp1760_qh *qh)
392{
393 WARN_ON(!list_empty(&qh->qtd_list));
394 WARN_ON(qh->slot > -1);
395 kmem_cache_free(qh_cachep, qh);
396}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200397
398/* one-time init, only for memory state */
399static int priv_init(struct usb_hcd *hcd)
400{
401 struct isp1760_hcd *priv = hcd_to_priv(hcd);
402 u32 hcc_params;
403
404 spin_lock_init(&priv->lock);
405
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200406 INIT_LIST_HEAD(&priv->interruptqhs);
407 INIT_LIST_HEAD(&priv->controlqhs);
408 INIT_LIST_HEAD(&priv->bulkqhs);
409
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200410 /*
411 * hw default: 1K periodic list heads, one per frame.
412 * periodic_size can shrink by USBCMD update if hcc_params allows.
413 */
414 priv->periodic_size = DEFAULT_I_TDPS;
415
416 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100417 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200418 /* full frame cache */
419 if (HCC_ISOC_CACHE(hcc_params))
420 priv->i_thresh = 8;
421 else /* N microframes cached */
422 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
423
424 return 0;
425}
426
427static int isp1760_hc_setup(struct usb_hcd *hcd)
428{
429 struct isp1760_hcd *priv = hcd_to_priv(hcd);
430 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500431 u32 scratch, hwmode;
432
433 /* Setup HW Mode Control: This assumes a level active-low interrupt */
434 hwmode = HW_DATA_BUS_32BIT;
435
436 if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
437 hwmode &= ~HW_DATA_BUS_32BIT;
438 if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
439 hwmode |= HW_ANA_DIGI_OC;
440 if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
441 hwmode |= HW_DACK_POL_HIGH;
442 if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
443 hwmode |= HW_DREQ_POL_HIGH;
Michael Hennerich9da69c62009-07-15 23:22:54 -0400444 if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
445 hwmode |= HW_INTR_HIGH_ACT;
446 if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
447 hwmode |= HW_INTR_EDGE_TRIG;
Nate Case3faefc82008-06-17 11:11:38 -0500448
449 /*
450 * We have to set this first in case we're in 16-bit mode.
451 * Write it twice to ensure correct upper bits if switching
452 * to 16-bit mode.
453 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100454 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
455 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200456
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100457 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500458 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100459 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
460 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200461 if (scratch != 0xdeadbabe) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100462 dev_err(hcd->self.controller, "Scratch test failed.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200463 return -ENODEV;
464 }
465
466 /* pre reset */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200467 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
468 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
469 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
470 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200471
472 /* reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100473 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200474 mdelay(100);
475
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100476 reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200477 mdelay(100);
478
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100479 result = ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200480 if (result)
481 return result;
482
483 /* Step 11 passed */
484
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100485 dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
Nate Case3faefc82008-06-17 11:11:38 -0500486 (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
487 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
488 "analog" : "digital");
489
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200490 /* This is weird: at the first plug-in of a device there seems to be
491 one packet queued that never gets returned? */
492 priv->active_ptds = -1;
493
Nate Case3faefc82008-06-17 11:11:38 -0500494 /* ATL reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100495 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500496 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100497 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500498
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100499 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200500
Nate Case3faefc82008-06-17 11:11:38 -0500501 /*
502 * PORT 1 Control register of the ISP1760 is the OTG control
Thomas Hommel42c65392008-12-18 10:31:40 +0100503 * register on ISP1761. Since there is no OTG or device controller
504 * support in this driver, we use port 1 as a "normal" USB host port on
505 * both chips.
Nate Case3faefc82008-06-17 11:11:38 -0500506 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100507 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
Thomas Hommel42c65392008-12-18 10:31:40 +0100508 mdelay(10);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200509
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100510 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200511
512 return priv_init(hcd);
513}
514
515static void isp1760_init_maps(struct usb_hcd *hcd)
516{
517 /*set last maps, for iso its only 1, else 32 tds bitmap*/
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100518 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
519 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
520 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
Arvid Brodineb1a7962011-04-26 21:48:02 +0200521
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200522 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0);
523 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0);
524 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0);
525
Arvid Brodineb1a7962011-04-26 21:48:02 +0200526 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
527 ATL_BUF_FILL | INT_BUF_FILL);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200528}
529
530static void isp1760_enable_interrupts(struct usb_hcd *hcd)
531{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100532 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
Arvid Brodineb1a7962011-04-26 21:48:02 +0200533 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100534 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
Arvid Brodineb1a7962011-04-26 21:48:02 +0200535 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100536 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
537 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200538 /* step 23 passed */
539}
540
541static int isp1760_run(struct usb_hcd *hcd)
542{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200543 int retval;
544 u32 temp;
545 u32 command;
546 u32 chipid;
547
548 hcd->uses_new_polling = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200549
550 hcd->state = HC_STATE_RUNNING;
551 isp1760_enable_interrupts(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100552 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
553 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200554
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100555 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200556 command &= ~(CMD_LRESET|CMD_RESET);
557 command |= CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100558 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200559
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200560 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200561 if (retval)
562 return retval;
563
564 /*
565 * XXX
566 * Spec says to write FLAG_CF as last config action, priv code grabs
567 * the semaphore while doing so.
568 */
569 down_write(&ehci_cf_port_reset_rwsem);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100570 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200571
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100572 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200573 up_write(&ehci_cf_port_reset_rwsem);
574 if (retval)
575 return retval;
576
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100577 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100578 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
579 chipid & 0xffff, chipid >> 16);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200580
581 /* PTD Register Init Part 2, Step 28 */
582 /* enable INTs */
583 isp1760_init_maps(hcd);
584
585 /* GRR this is run-once init(), being done every time the HC starts.
586 * So long as they're part of class devices, we can't do it init()
587 * since the class device isn't created that early.
588 */
589 return 0;
590}
591
592static u32 base_to_chip(u32 base)
593{
594 return ((base - 0x400) >> 3);
595}
596
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100597static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
598{
599 struct urb *urb;
600
601 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
602 return 1;
603
604 urb = qtd->urb;
605 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
606 return (qtd->urb != urb);
607}
608
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200609/* magic numbers that can affect system performance */
610#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
611#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
612#define EHCI_TUNE_RL_TT 0
613#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
614#define EHCI_TUNE_MULT_TT 1
615#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
616
617static void create_ptd_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100618 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200619{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200620 u32 maxpacket;
621 u32 multi;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200622 u32 rl = RL_COUNTER;
623 u32 nak = NAK_COUNTER;
624
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100625 memset(ptd, 0, sizeof(*ptd));
626
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200627 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100628 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
629 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200630 multi = 1 + ((maxpacket >> 11) & 0x3);
631 maxpacket &= 0x7ff;
632
633 /* DW0 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200634 ptd->dw0 = DW0_VALID_BIT;
635 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
636 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
637 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200638
639 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100640 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200641 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
642 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200643
Arvid Brodina041d8e2011-02-26 22:04:40 +0100644 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200645 ptd->dw1 |= DW1_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100646 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200647 ptd->dw1 |= DW1_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200648
Arvid Brodina041d8e2011-02-26 22:04:40 +0100649 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200650 /* split transaction */
651
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200652 ptd->dw1 |= DW1_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100653 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200654 ptd->dw1 |= DW1_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200655
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200656 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
657 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200658
659 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100660 if (usb_pipeint(qtd->urb->pipe) &&
661 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100662 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200663
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200664 rl = 0;
665 nak = 0;
666 } else {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200667 ptd->dw0 |= TO_DW0_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100668 if (usb_pipecontrol(qtd->urb->pipe) ||
669 usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200670 ptd->dw3 |= TO_DW3_PING(qh->ping);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200671 }
672 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100673 ptd->dw2 = 0;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200674 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
675 ptd->dw2 |= TO_DW2_RL(rl);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200676
677 /* DW3 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200678 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
679 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100680 if (usb_pipecontrol(qtd->urb->pipe)) {
681 if (qtd->data_buffer == qtd->urb->setup_packet)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200682 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100683 else if (last_qtd_of_urb(qtd, qh))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200684 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100685 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200686
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200687 ptd->dw3 |= DW3_ACTIVE_BIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200688 /* Cerr */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200689 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200690}
691
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100692static void transform_add_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100693 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200694{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100695 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200696 u32 period;
697
Arvid Brodin65f1b522011-02-26 22:07:35 +0100698 /*
699 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
700 * the algorithm from the original Philips driver code, which was
701 * pretty much used in this driver before as well, is quite horrendous
702 * and, i believe, incorrect. The code below follows the datasheet and
703 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
704 * more reliable this way (fingers crossed...).
705 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200706
Arvid Brodin65f1b522011-02-26 22:07:35 +0100707 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
708 /* urb->interval is in units of microframes (1/8 ms) */
709 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200710
Arvid Brodin65f1b522011-02-26 22:07:35 +0100711 if (qtd->urb->interval > 4)
712 usof = 0x01; /* One bit set =>
713 interval 1 ms * uFrame-match */
714 else if (qtd->urb->interval > 2)
715 usof = 0x22; /* Two bits set => interval 1/2 ms */
716 else if (qtd->urb->interval > 1)
717 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200718 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100719 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200720 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100721 /* urb->interval is in units of frames (1 ms) */
722 period = qtd->urb->interval;
723 usof = 0x0f; /* Execute Start Split on any of the
724 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200725
Arvid Brodin65f1b522011-02-26 22:07:35 +0100726 /*
727 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
728 * complete split needs to be sent. Valid only for IN." Also,
729 * "All bits can be set to one for every transfer." (p 82,
730 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
731 * that number come from? 0xff seems to work fine...
732 */
733 /* ptd->dw5 = 0x1c; */
734 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200735 }
736
Arvid Brodin65f1b522011-02-26 22:07:35 +0100737 period = period >> 1;/* Ensure equal or shorter period than requested */
738 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
739
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100740 ptd->dw2 |= period;
741 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200742}
743
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200744static void create_ptd_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100745 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200746{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200747 create_ptd_atl(qh, qtd, ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100748 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200749}
750
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100751static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200752__releases(priv->lock)
753__acquires(priv->lock)
754{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100755 struct isp1760_hcd *priv = hcd_to_priv(hcd);
756
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200757 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100758 if (urb->status == -EINPROGRESS)
759 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200760 }
761
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000762 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
763 void *ptr;
764 for (ptr = urb->transfer_buffer;
765 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
766 ptr += PAGE_SIZE)
767 flush_dcache_page(virt_to_page(ptr));
768 }
769
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200770 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100771 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200772 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100773 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200774 spin_lock(&priv->lock);
775}
776
Arvid Brodin34537732011-04-26 21:47:37 +0200777static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
778 u8 packet_type)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200779{
Arvid Brodin34537732011-04-26 21:47:37 +0200780 struct isp1760_qtd *qtd;
781
782 qtd = kmem_cache_zalloc(qtd_cachep, flags);
783 if (!qtd)
784 return NULL;
785
786 INIT_LIST_HEAD(&qtd->qtd_list);
787 qtd->urb = urb;
788 qtd->packet_type = packet_type;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200789 qtd->status = QTD_ENQUEUED;
790 qtd->actual_length = 0;
Arvid Brodin34537732011-04-26 21:47:37 +0200791
792 return qtd;
793}
794
795static void qtd_free(struct isp1760_qtd *qtd)
796{
797 WARN_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200798 kmem_cache_free(qtd_cachep, qtd);
799}
800
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200801static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
802 struct slotinfo *slots, struct isp1760_qtd *qtd,
803 struct isp1760_qh *qh, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200804{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100805 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200806 WARN_ON((slot < 0) || (slot > 31));
807 WARN_ON(qtd->length && !qtd->payload_addr);
808 WARN_ON(slots[slot].qtd);
809 WARN_ON(slots[slot].qh);
810 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200811
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200812 slots[slot].qtd = qtd;
813 slots[slot].qh = qh;
814 qh->slot = slot;
815 qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
816 interrupt routine may preempt and expects this value. */
817 ptd_write(hcd->regs, ptd_offset, slot, ptd);
818 priv->active_ptds++;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200819}
820
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200821static int is_short_bulk(struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200822{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200823 return (usb_pipebulk(qtd->urb->pipe) &&
824 (qtd->actual_length < qtd->length));
825}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200826
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200827static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
828 struct list_head *urb_list)
829{
830 int last_qtd;
831 struct isp1760_qtd *qtd, *qtd_next;
832 struct urb_listitem *urb_listitem;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200833
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200834 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
835 if (qtd->status < QTD_XFER_COMPLETE)
836 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200837
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200838 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
839 last_qtd = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200840 else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200841 last_qtd = qtd->urb != qtd_next->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200842
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200843 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
844 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200845
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200846 if (qtd->status == QTD_XFER_COMPLETE) {
847 if (qtd->actual_length) {
848 switch (qtd->packet_type) {
849 case IN_PID:
850 mem_reads8(hcd->regs, qtd->payload_addr,
851 qtd->data_buffer,
852 qtd->actual_length);
853 /* Fall through (?) */
854 case OUT_PID:
855 qtd->urb->actual_length +=
856 qtd->actual_length;
857 /* Fall through ... */
858 case SETUP_PID:
859 break;
860 }
861 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200862
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200863 if (is_short_bulk(qtd)) {
864 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
865 qtd->urb->status = -EREMOTEIO;
866 if (!last_qtd)
867 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200868 }
869 }
870
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200871 if (qtd->payload_addr)
872 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200873
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200874 if (last_qtd) {
875 if ((qtd->status == QTD_RETIRE) &&
876 (qtd->urb->status == -EINPROGRESS))
877 qtd->urb->status = -EPIPE;
878 /* Defer calling of urb_done() since it releases lock */
879 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
880 GFP_ATOMIC);
881 if (unlikely(!urb_listitem))
882 break;
883 urb_listitem->urb = qtd->urb;
884 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200885 }
886
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200887 list_del(&qtd->qtd_list);
888 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200889 }
890}
891
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200892#define ENQUEUE_DEPTH 2
893static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
894{
895 struct isp1760_hcd *priv = hcd_to_priv(hcd);
896 int ptd_offset;
897 struct slotinfo *slots;
898 int curr_slot, free_slot;
899 int n;
900 struct ptd ptd;
901 struct isp1760_qtd *qtd;
902
903 if (unlikely(list_empty(&qh->qtd_list))) {
904 WARN_ON(1);
905 return;
906 }
907
908 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
909 qtd_list)->urb->pipe)) {
910 ptd_offset = INT_PTD_OFFSET;
911 slots = priv->int_slots;
912 } else {
913 ptd_offset = ATL_PTD_OFFSET;
914 slots = priv->atl_slots;
915 }
916
917 free_slot = -1;
918 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
919 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
920 free_slot = curr_slot;
921 if (slots[curr_slot].qh == qh)
922 break;
923 }
924
925 n = 0;
926 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
927 if (qtd->status == QTD_ENQUEUED) {
928 WARN_ON(qtd->payload_addr);
929 alloc_mem(hcd, qtd);
930 if ((qtd->length) && (!qtd->payload_addr))
931 break;
932
933 if ((qtd->length) &&
934 ((qtd->packet_type == SETUP_PID) ||
935 (qtd->packet_type == OUT_PID))) {
936 mem_writes8(hcd->regs, qtd->payload_addr,
937 qtd->data_buffer, qtd->length);
938 }
939
940 qtd->status = QTD_PAYLOAD_ALLOC;
941 }
942
943 if (qtd->status == QTD_PAYLOAD_ALLOC) {
944/*
945 if ((curr_slot > 31) && (free_slot == -1))
946 dev_dbg(hcd->self.controller, "%s: No slot "
947 "available for transfer\n", __func__);
948*/
949 /* Start xfer for this endpoint if not already done */
950 if ((curr_slot > 31) && (free_slot > -1)) {
951 if (usb_pipeint(qtd->urb->pipe))
952 create_ptd_int(qh, qtd, &ptd);
953 else
954 create_ptd_atl(qh, qtd, &ptd);
955
956 start_bus_transfer(hcd, ptd_offset, free_slot,
957 slots, qtd, qh, &ptd);
958 curr_slot = free_slot;
959 }
960
961 n++;
962 if (n >= ENQUEUE_DEPTH)
963 break;
964 }
965 }
966}
967
968void schedule_ptds(struct usb_hcd *hcd)
969{
970 struct isp1760_hcd *priv;
971 struct isp1760_qh *qh, *qh_next;
972 struct list_head *ep_queue;
973 struct usb_host_endpoint *ep;
974 LIST_HEAD(urb_list);
975 struct urb_listitem *urb_listitem, *urb_listitem_next;
976
977 if (!hcd) {
978 WARN_ON(1);
979 return;
980 }
981
982 priv = hcd_to_priv(hcd);
983
984 /*
985 * check finished/retired xfers, transfer payloads, call urb_done()
986 */
987 ep_queue = &priv->interruptqhs;
988 while (ep_queue) {
989 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
990 ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
991 qtd_list)->urb->ep;
992 collect_qtds(hcd, qh, &urb_list);
993 if (list_empty(&qh->qtd_list)) {
994 list_del(&qh->qh_list);
995 if (ep->hcpriv == NULL) {
996 /* Endpoint has been disabled, so we
997 can free the associated queue head. */
998 qh_free(qh);
999 }
1000 }
1001 }
1002
1003 if (ep_queue == &priv->interruptqhs)
1004 ep_queue = &priv->controlqhs;
1005 else if (ep_queue == &priv->controlqhs)
1006 ep_queue = &priv->bulkqhs;
1007 else
1008 ep_queue = NULL;
1009 }
1010
1011 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
1012 urb_list) {
1013 isp1760_urb_done(hcd, urb_listitem->urb);
1014 kmem_cache_free(urb_listitem_cachep, urb_listitem);
1015 }
1016
1017 /*
1018 * Schedule packets for transfer.
1019 *
1020 * According to USB2.0 specification:
1021 *
1022 * 1st prio: interrupt xfers, up to 80 % of bandwidth
1023 * 2nd prio: control xfers
1024 * 3rd prio: bulk xfers
1025 *
1026 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
1027 * is very unclear on how to prioritize traffic):
1028 *
1029 * 1) Enqueue any queued control transfers, as long as payload chip mem
1030 * and PTD ATL slots are available.
1031 * 2) Enqueue any queued INT transfers, as long as payload chip mem
1032 * and PTD INT slots are available.
1033 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
1034 * and PTD ATL slots are available.
1035 *
1036 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
1037 * conservation of chip mem and performance.
1038 *
1039 * I'm sure this scheme could be improved upon!
1040 */
1041 ep_queue = &priv->controlqhs;
1042 while (ep_queue) {
1043 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1044 enqueue_qtds(hcd, qh);
1045
1046 if (ep_queue == &priv->controlqhs)
1047 ep_queue = &priv->interruptqhs;
1048 else if (ep_queue == &priv->interruptqhs)
1049 ep_queue = &priv->bulkqhs;
1050 else
1051 ep_queue = NULL;
1052 }
1053}
1054
1055#define PTD_STATE_QTD_DONE 1
1056#define PTD_STATE_QTD_RELOAD 2
1057#define PTD_STATE_URB_RETIRE 3
1058
1059static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1060 struct urb *urb)
1061{
1062 __dw dw4;
1063 int i;
1064
1065 dw4 = ptd->dw4;
1066 dw4 >>= 8;
1067
1068 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1069 need to handle these errors? Is it done in hardware? */
1070
1071 if (ptd->dw3 & DW3_HALT_BIT) {
1072
1073 urb->status = -EPROTO; /* Default unknown error */
1074
1075 for (i = 0; i < 8; i++) {
1076 switch (dw4 & 0x7) {
1077 case INT_UNDERRUN:
1078 dev_dbg(hcd->self.controller, "%s: underrun "
1079 "during uFrame %d\n",
1080 __func__, i);
1081 urb->status = -ECOMM; /* Could not write data */
1082 break;
1083 case INT_EXACT:
1084 dev_dbg(hcd->self.controller, "%s: transaction "
1085 "error during uFrame %d\n",
1086 __func__, i);
1087 urb->status = -EPROTO; /* timeout, bad CRC, PID
1088 error etc. */
1089 break;
1090 case INT_BABBLE:
1091 dev_dbg(hcd->self.controller, "%s: babble "
1092 "error during uFrame %d\n",
1093 __func__, i);
1094 urb->status = -EOVERFLOW;
1095 break;
1096 }
1097 dw4 >>= 3;
1098 }
1099
1100 return PTD_STATE_URB_RETIRE;
1101 }
1102
1103 return PTD_STATE_QTD_DONE;
1104}
1105
1106static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1107 struct urb *urb)
1108{
1109 WARN_ON(!ptd);
1110 if (ptd->dw3 & DW3_HALT_BIT) {
1111 if (ptd->dw3 & DW3_BABBLE_BIT)
1112 urb->status = -EOVERFLOW;
1113 else if (FROM_DW3_CERR(ptd->dw3))
1114 urb->status = -EPIPE; /* Stall */
1115 else if (ptd->dw3 & DW3_ERROR_BIT)
1116 urb->status = -EPROTO; /* XactErr */
1117 else
1118 urb->status = -EPROTO; /* Unknown */
1119/*
1120 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1121 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1122 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1123 __func__,
1124 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1125 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1126*/
1127 return PTD_STATE_URB_RETIRE;
1128 }
1129
1130 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1131 /* Transfer Error, *but* active and no HALT -> reload */
1132 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1133 return PTD_STATE_QTD_RELOAD;
1134 }
1135
1136 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1137 /*
1138 * NAKs are handled in HW by the chip. Usually if the
1139 * device is not able to send data fast enough.
1140 * This happens mostly on slower hardware.
1141 */
1142 return PTD_STATE_QTD_RELOAD;
1143 }
1144
1145 return PTD_STATE_QTD_DONE;
1146}
1147
1148static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1149{
1150 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1151 u32 imask;
1152 irqreturn_t irqret = IRQ_NONE;
1153 struct ptd ptd;
1154 struct isp1760_qh *qh;
1155 int int_done_map, atl_done_map;
1156 int slot;
1157 int state;
1158 struct slotinfo *slots;
1159 u32 ptd_offset;
1160 struct isp1760_qtd *qtd;
1161 int modified;
1162 static int last_active_ptds;
1163
1164 spin_lock(&priv->lock);
1165
1166 if (!(hcd->state & HC_STATE_RUNNING))
1167 goto leave;
1168
1169 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1170 if (unlikely(!imask))
1171 goto leave;
1172 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1173
1174 int_done_map = reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1175 atl_done_map = reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1176 modified = int_done_map | atl_done_map;
1177
1178 while (int_done_map || atl_done_map) {
1179 if (int_done_map) {
1180 /* INT ptd */
1181 slot = __ffs(int_done_map);
1182 int_done_map &= ~(1 << slot);
1183 slots = priv->int_slots;
1184 if (!slots[slot].qh)
1185 continue;
1186 ptd_offset = INT_PTD_OFFSET;
1187 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1188 state = check_int_transfer(hcd, &ptd,
1189 slots[slot].qtd->urb);
1190 } else {
1191 /* ATL ptd */
1192 slot = __ffs(atl_done_map);
1193 atl_done_map &= ~(1 << slot);
1194 slots = priv->atl_slots;
1195 if (!slots[slot].qh)
1196 continue;
1197 ptd_offset = ATL_PTD_OFFSET;
1198 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1199 state = check_atl_transfer(hcd, &ptd,
1200 slots[slot].qtd->urb);
1201 }
1202
1203 qtd = slots[slot].qtd;
1204 slots[slot].qtd = NULL;
1205 qh = slots[slot].qh;
1206 slots[slot].qh = NULL;
1207 priv->active_ptds--;
1208 qh->slot = -1;
1209
1210 WARN_ON(qtd->status != QTD_XFER_STARTED);
1211
1212 switch (state) {
1213 case PTD_STATE_QTD_DONE:
1214 if ((usb_pipeint(qtd->urb->pipe)) &&
1215 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1216 qtd->actual_length =
1217 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1218 else
1219 qtd->actual_length =
1220 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1221
1222 qtd->status = QTD_XFER_COMPLETE;
1223 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1224 is_short_bulk(qtd))
1225 qtd = NULL;
1226 else
1227 qtd = list_entry(qtd->qtd_list.next,
1228 typeof(*qtd), qtd_list);
1229
1230 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1231 qh->ping = FROM_DW3_PING(ptd.dw3);
1232 break;
1233
1234 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1235 qtd->status = QTD_PAYLOAD_ALLOC;
1236 ptd.dw0 |= DW0_VALID_BIT;
1237 /* RL counter = ERR counter */
1238 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1239 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1240 ptd.dw3 &= ~TO_DW3_CERR(3);
1241 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1242 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1243 qh->ping = FROM_DW3_PING(ptd.dw3);
1244 break;
1245
1246 case PTD_STATE_URB_RETIRE:
1247 qtd->status = QTD_RETIRE;
1248 qtd = NULL;
1249 qh->toggle = 0;
1250 qh->ping = 0;
1251 break;
1252
1253 default:
1254 WARN_ON(1);
1255 continue;
1256 }
1257
1258 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1259 if (slots == priv->int_slots) {
1260 if (state == PTD_STATE_QTD_RELOAD)
1261 dev_err(hcd->self.controller,
1262 "%s: PTD_STATE_QTD_RELOAD on "
1263 "interrupt packet\n", __func__);
1264 if (state != PTD_STATE_QTD_RELOAD)
1265 create_ptd_int(qh, qtd, &ptd);
1266 } else {
1267 if (state != PTD_STATE_QTD_RELOAD)
1268 create_ptd_atl(qh, qtd, &ptd);
1269 }
1270
1271 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1272 qh, &ptd);
1273 }
1274 }
1275
1276 if (modified)
1277 schedule_ptds(hcd);
1278
1279 /* ISP1760 Errata 2 explains that interrupts may be missed (or not
1280 happen?) if two USB devices are running simultaneously. Perhaps
1281 this happens when a PTD is finished during interrupt handling;
1282 enable SOF interrupts if PTDs are still scheduled when exiting this
1283 interrupt handler, just to be safe. */
1284
1285 if (priv->active_ptds != last_active_ptds) {
1286 if (priv->active_ptds > 0)
1287 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1288 INTERRUPT_ENABLE_SOT_MASK);
1289 else
1290 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1291 INTERRUPT_ENABLE_MASK);
1292 last_active_ptds = priv->active_ptds;
1293 }
1294
1295 irqret = IRQ_HANDLED;
1296leave:
1297 spin_unlock(&priv->lock);
1298
1299 return irqret;
1300}
1301
Arvid Brodin34537732011-04-26 21:47:37 +02001302static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001303{
Arvid Brodin34537732011-04-26 21:47:37 +02001304 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001305
Arvid Brodin34537732011-04-26 21:47:37 +02001306 if (len > MAX_PAYLOAD_SIZE)
1307 len = MAX_PAYLOAD_SIZE;
1308 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001309
Arvid Brodin34537732011-04-26 21:47:37 +02001310 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001311}
1312
Arvid Brodin34537732011-04-26 21:47:37 +02001313static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001314{
Arvid Brodin34537732011-04-26 21:47:37 +02001315 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001316
Arvid Brodin34537732011-04-26 21:47:37 +02001317 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001318 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001319 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001320 }
1321}
1322
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001323/*
Arvid Brodin34537732011-04-26 21:47:37 +02001324 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1325 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001326 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001327#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001328static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001329 struct urb *urb, struct list_head *head, gfp_t flags)
1330{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001331 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001332 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001333 int len, maxpacketsize;
1334 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001335
1336 /*
1337 * URBs map to sequences of QTDs: one logical transaction
1338 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001339
Arvid Brodin34537732011-04-26 21:47:37 +02001340 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1341 /* XXX This looks like usb storage / SCSI bug */
1342 dev_err(hcd->self.controller,
1343 "buf is null, dma is %08lx len is %d\n",
1344 (long unsigned)urb->transfer_dma,
1345 urb->transfer_buffer_length);
1346 WARN_ON(1);
1347 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001348
Arvid Brodin34537732011-04-26 21:47:37 +02001349 if (usb_pipein(urb->pipe))
1350 packet_type = IN_PID;
1351 else
1352 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001353
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001354 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001355 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001356 if (!qtd)
1357 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001358 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001359 list_add_tail(&qtd->qtd_list, head);
1360
1361 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001362 if (urb->transfer_buffer_length == 0)
1363 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001364 }
1365
Arvid Brodin34537732011-04-26 21:47:37 +02001366 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1367 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001368
1369 /*
1370 * buffer gets wrapped in one or more qtds;
1371 * last one may be "short" (including zero len)
1372 * and may serve as a control status ack
1373 */
Arvid Brodin34537732011-04-26 21:47:37 +02001374 buf = urb->transfer_buffer;
1375 len = urb->transfer_buffer_length;
1376
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001377 for (;;) {
1378 int this_qtd_len;
1379
Arvid Brodin34537732011-04-26 21:47:37 +02001380 qtd = qtd_alloc(flags, urb, packet_type);
1381 if (!qtd)
1382 goto cleanup;
1383 this_qtd_len = qtd_fill(qtd, buf, len);
1384 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001385
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001386 len -= this_qtd_len;
1387 buf += this_qtd_len;
1388
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001389 if (len <= 0)
1390 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001391 }
1392
1393 /*
1394 * control requests may need a terminating data "status" ack;
1395 * bulk ones may need a terminating short packet (zero length).
1396 */
1397 if (urb->transfer_buffer_length != 0) {
1398 int one_more = 0;
1399
1400 if (usb_pipecontrol(urb->pipe)) {
1401 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001402 if (packet_type == IN_PID)
1403 packet_type = OUT_PID;
1404 else
1405 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001406 } else if (usb_pipebulk(urb->pipe)
1407 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001408 && !(urb->transfer_buffer_length %
1409 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001410 one_more = 1;
1411 }
1412 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001413 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001414 if (!qtd)
1415 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001416
1417 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001418 qtd_fill(qtd, NULL, 0);
1419 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001420 }
1421 }
1422
Arvid Brodin34537732011-04-26 21:47:37 +02001423 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001424
1425cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001426 qtd_list_free(head);
1427}
1428
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001429static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1430 gfp_t mem_flags)
1431{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001432 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1433 struct list_head *ep_queue;
1434 struct isp1760_qh *qh, *qhit;
1435 unsigned long spinflags;
1436 LIST_HEAD(new_qtds);
1437 int retval;
1438 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001439
1440 switch (usb_pipetype(urb->pipe)) {
1441 case PIPE_CONTROL:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001442 ep_queue = &priv->controlqhs;
1443 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001444 case PIPE_BULK:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001445 ep_queue = &priv->bulkqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001446 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001447 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001448 if (urb->interval < 0)
1449 return -EINVAL;
1450 /* FIXME: Check bandwidth */
1451 ep_queue = &priv->interruptqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001452 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001453 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001454 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1455 "not yet supported\n",
1456 __func__);
1457 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001458 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001459 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1460 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001461 return -EPIPE;
1462 }
1463
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001464 if (usb_pipein(urb->pipe))
1465 urb->actual_length = 0;
1466
1467 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1468 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001469 return -ENOMEM;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001470 urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
Arvid Brodin34537732011-04-26 21:47:37 +02001471
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001472 retval = 0;
1473 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001474
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001475 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1476 retval = -ESHUTDOWN;
1477 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001478 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001479 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1480 if (retval)
1481 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001482
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001483 qh = urb->ep->hcpriv;
1484 if (qh) {
1485 qh_in_queue = 0;
1486 list_for_each_entry(qhit, ep_queue, qh_list) {
1487 if (qhit == qh) {
1488 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001489 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001490 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001491 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001492 if (!qh_in_queue)
1493 list_add_tail(&qh->qh_list, ep_queue);
1494 } else {
1495 qh = qh_alloc(GFP_ATOMIC);
1496 if (!qh) {
1497 retval = -ENOMEM;
1498 goto out;
1499 }
1500 list_add_tail(&qh->qh_list, ep_queue);
1501 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001502 }
1503
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001504 list_splice_tail(&new_qtds, &qh->qtd_list);
1505 schedule_ptds(hcd);
1506
1507out:
1508 spin_unlock_irqrestore(&priv->lock, spinflags);
1509 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001510}
1511
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001512static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1513 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001514{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001515 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001516 struct isp1760_qh *qh;
1517 struct isp1760_qtd *qtd;
1518 struct ptd ptd;
1519 unsigned long spinflags;
1520 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001521
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001522 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001523
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001524 qh = urb->ep->hcpriv;
1525 if (!qh) {
1526 retval = -EINVAL;
1527 goto out;
1528 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001529
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001530 /* We need to forcefully reclaim the slot since some transfers never
1531 return, e.g. interrupt transfers and NAKed bulk transfers. */
1532 if (qh->slot > -1) {
1533 memset(&ptd, 0, sizeof(ptd));
1534 if (usb_pipebulk(urb->pipe)) {
1535 priv->atl_slots[qh->slot].qh = NULL;
1536 priv->atl_slots[qh->slot].qtd = NULL;
1537 ptd_write(hcd->regs, ATL_PTD_OFFSET, qh->slot, &ptd);
1538 } else {
1539 priv->int_slots[qh->slot].qh = NULL;
1540 priv->int_slots[qh->slot].qtd = NULL;
1541 ptd_write(hcd->regs, INT_PTD_OFFSET, qh->slot, &ptd);
1542 }
1543 priv->active_ptds--;
1544 qh->slot = -1;
1545 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001546
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001547 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1548 if (qtd->urb == urb)
1549 qtd->status = QTD_RETIRE;
1550 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001551
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001552 urb->status = status;
1553 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001554
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001555out:
1556 spin_unlock_irqrestore(&priv->lock, spinflags);
1557
1558 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001559}
1560
1561static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1562{
1563 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1564 u32 temp, status = 0;
1565 u32 mask;
1566 int retval = 1;
1567 unsigned long flags;
1568
1569 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1570 if (!HC_IS_RUNNING(hcd->state))
1571 return 0;
1572
1573 /* init status to no-changes */
1574 buf[0] = 0;
1575 mask = PORT_CSC;
1576
1577 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001578 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001579
1580 if (temp & PORT_OWNER) {
1581 if (temp & PORT_CSC) {
1582 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001583 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001584 goto done;
1585 }
1586 }
1587
1588 /*
1589 * Return status information even for ports with OWNER set.
1590 * Otherwise khubd wouldn't see the disconnect event when a
1591 * high-speed device is switched over to the companion
1592 * controller by the user.
1593 */
1594
1595 if ((temp & mask) != 0
1596 || ((temp & PORT_RESUME) != 0
1597 && time_after_eq(jiffies,
1598 priv->reset_done))) {
1599 buf [0] |= 1 << (0 + 1);
1600 status = STS_PCD;
1601 }
1602 /* FIXME autosuspend idle root hubs */
1603done:
1604 spin_unlock_irqrestore(&priv->lock, flags);
1605 return status ? retval : 0;
1606}
1607
1608static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1609 struct usb_hub_descriptor *desc)
1610{
1611 int ports = HCS_N_PORTS(priv->hcs_params);
1612 u16 temp;
1613
1614 desc->bDescriptorType = 0x29;
1615 /* priv 1.0, 2.3.9 says 20ms max */
1616 desc->bPwrOn2PwrGood = 10;
1617 desc->bHubContrCurrent = 0;
1618
1619 desc->bNbrPorts = ports;
1620 temp = 1 + (ports / 8);
1621 desc->bDescLength = 7 + 2 * temp;
1622
Sarah Sharpda130512010-11-30 15:55:51 -08001623 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001624 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1625 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001626
1627 /* per-port overcurrent reporting */
1628 temp = 0x0008;
1629 if (HCS_PPC(priv->hcs_params))
1630 /* per-port power control */
1631 temp |= 0x0001;
1632 else
1633 /* no power switching */
1634 temp |= 0x0002;
1635 desc->wHubCharacteristics = cpu_to_le16(temp);
1636}
1637
1638#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1639
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001640static int check_reset_complete(struct usb_hcd *hcd, int index,
1641 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001642{
1643 if (!(port_status & PORT_CONNECT))
1644 return port_status;
1645
1646 /* if reset finished and it's still not enabled -- handoff */
1647 if (!(port_status & PORT_PE)) {
1648
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001649 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001650 "port %d full speed --> companion\n",
1651 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001652
1653 port_status |= PORT_OWNER;
1654 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001655 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001656
1657 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001658 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001659 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001660
1661 return port_status;
1662}
1663
1664static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1665 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1666{
1667 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1668 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001669 u32 temp, status;
1670 unsigned long flags;
1671 int retval = 0;
1672 unsigned selector;
1673
1674 /*
1675 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1676 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1677 * (track current state ourselves) ... blink for diagnostics,
1678 * power, "this is the one", etc. EHCI spec supports this.
1679 */
1680
1681 spin_lock_irqsave(&priv->lock, flags);
1682 switch (typeReq) {
1683 case ClearHubFeature:
1684 switch (wValue) {
1685 case C_HUB_LOCAL_POWER:
1686 case C_HUB_OVER_CURRENT:
1687 /* no hub-wide feature/status flags */
1688 break;
1689 default:
1690 goto error;
1691 }
1692 break;
1693 case ClearPortFeature:
1694 if (!wIndex || wIndex > ports)
1695 goto error;
1696 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001697 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001698
1699 /*
1700 * Even if OWNER is set, so the port is owned by the
1701 * companion controller, khubd needs to be able to clear
1702 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001703 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001704 */
1705
1706 switch (wValue) {
1707 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001708 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001709 break;
1710 case USB_PORT_FEAT_C_ENABLE:
1711 /* XXX error? */
1712 break;
1713 case USB_PORT_FEAT_SUSPEND:
1714 if (temp & PORT_RESET)
1715 goto error;
1716
1717 if (temp & PORT_SUSPEND) {
1718 if ((temp & PORT_PE) == 0)
1719 goto error;
1720 /* resume signaling for 20 msec */
1721 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001722 reg_write32(hcd->regs, HC_PORTSC1,
1723 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001724 priv->reset_done = jiffies +
1725 msecs_to_jiffies(20);
1726 }
1727 break;
1728 case USB_PORT_FEAT_C_SUSPEND:
1729 /* we auto-clear this feature */
1730 break;
1731 case USB_PORT_FEAT_POWER:
1732 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001733 reg_write32(hcd->regs, HC_PORTSC1,
1734 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001735 break;
1736 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001737 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001738 break;
1739 case USB_PORT_FEAT_C_OVER_CURRENT:
1740 /* XXX error ?*/
1741 break;
1742 case USB_PORT_FEAT_C_RESET:
1743 /* GetPortStatus clears reset */
1744 break;
1745 default:
1746 goto error;
1747 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001748 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001749 break;
1750 case GetHubDescriptor:
1751 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1752 buf);
1753 break;
1754 case GetHubStatus:
1755 /* no hub-wide feature/status flags */
1756 memset(buf, 0, 4);
1757 break;
1758 case GetPortStatus:
1759 if (!wIndex || wIndex > ports)
1760 goto error;
1761 wIndex--;
1762 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001763 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001764
1765 /* wPortChange bits */
1766 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001767 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001768
1769
1770 /* whoever resumes must GetPortStatus to complete it!! */
1771 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001772 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001773
1774 /* Remote Wakeup received? */
1775 if (!priv->reset_done) {
1776 /* resume signaling for 20 msec */
1777 priv->reset_done = jiffies
1778 + msecs_to_jiffies(20);
1779 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001780 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001781 }
1782
1783 /* resume completed? */
1784 else if (time_after_eq(jiffies,
1785 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001786 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001787 priv->reset_done = 0;
1788
1789 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001790 temp = reg_read32(hcd->regs, HC_PORTSC1);
1791 reg_write32(hcd->regs, HC_PORTSC1,
1792 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1793 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001794 PORT_RESUME, 0, 2000 /* 2msec */);
1795 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001796 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001797 "port %d resume error %d\n",
1798 wIndex + 1, retval);
1799 goto error;
1800 }
1801 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1802 }
1803 }
1804
1805 /* whoever resets must GetPortStatus to complete it!! */
1806 if ((temp & PORT_RESET)
1807 && time_after_eq(jiffies,
1808 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001809 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001810 priv->reset_done = 0;
1811
1812 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001813 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001814 /* REVISIT: some hardware needs 550+ usec to clear
1815 * this bit; seems too long to spin routinely...
1816 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001817 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001818 PORT_RESET, 0, 750);
1819 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001820 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001821 wIndex + 1, retval);
1822 goto error;
1823 }
1824
1825 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001826 temp = check_reset_complete(hcd, wIndex,
1827 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001828 }
1829 /*
1830 * Even if OWNER is set, there's no harm letting khubd
1831 * see the wPortStatus values (they should all be 0 except
1832 * for PORT_POWER anyway).
1833 */
1834
1835 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001836 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001837
1838 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001839 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001840 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001841 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001842 }
1843 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001844 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001845 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001846 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001847 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001848 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001849 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001850 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001851
1852 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1853 break;
1854 case SetHubFeature:
1855 switch (wValue) {
1856 case C_HUB_LOCAL_POWER:
1857 case C_HUB_OVER_CURRENT:
1858 /* no hub-wide feature/status flags */
1859 break;
1860 default:
1861 goto error;
1862 }
1863 break;
1864 case SetPortFeature:
1865 selector = wIndex >> 8;
1866 wIndex &= 0xff;
1867 if (!wIndex || wIndex > ports)
1868 goto error;
1869 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001870 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001871 if (temp & PORT_OWNER)
1872 break;
1873
1874/* temp &= ~PORT_RWC_BITS; */
1875 switch (wValue) {
1876 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001877 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001878 break;
1879
1880 case USB_PORT_FEAT_SUSPEND:
1881 if ((temp & PORT_PE) == 0
1882 || (temp & PORT_RESET) != 0)
1883 goto error;
1884
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001885 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001886 break;
1887 case USB_PORT_FEAT_POWER:
1888 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001889 reg_write32(hcd->regs, HC_PORTSC1,
1890 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001891 break;
1892 case USB_PORT_FEAT_RESET:
1893 if (temp & PORT_RESUME)
1894 goto error;
1895 /* line status bits may report this as low speed,
1896 * which can be fine if this root hub has a
1897 * transaction translator built in.
1898 */
1899 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1900 && PORT_USB11(temp)) {
1901 temp |= PORT_OWNER;
1902 } else {
1903 temp |= PORT_RESET;
1904 temp &= ~PORT_PE;
1905
1906 /*
1907 * caller must wait, then call GetPortStatus
1908 * usb 2.0 spec says 50 ms resets on root
1909 */
1910 priv->reset_done = jiffies +
1911 msecs_to_jiffies(50);
1912 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001913 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001914 break;
1915 default:
1916 goto error;
1917 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001918 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001919 break;
1920
1921 default:
1922error:
1923 /* "stall" on error */
1924 retval = -EPIPE;
1925 }
1926 spin_unlock_irqrestore(&priv->lock, flags);
1927 return retval;
1928}
1929
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001930static void isp1760_endpoint_disable(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001931 struct usb_host_endpoint *ep)
1932{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001933 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001934 struct isp1760_qh *qh;
1935 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001936 unsigned long spinflags;
1937 int do_iter;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001938
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001939 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001940 qh = ep->hcpriv;
1941 if (!qh)
1942 goto out;
1943
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001944 do_iter = !list_empty(&qh->qtd_list);
1945 while (do_iter) {
1946 do_iter = 0;
1947 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1948 if (qtd->urb->ep == ep) {
1949 spin_unlock_irqrestore(&priv->lock, spinflags);
1950 isp1760_urb_dequeue(hcd, qtd->urb, -ECONNRESET);
1951 spin_lock_irqsave(&priv->lock, spinflags);
1952 do_iter = 1;
1953 break; /* Restart iteration */
1954 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001955 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001956 }
1957 ep->hcpriv = NULL;
1958 /* Cannot free qh here since it will be parsed by schedule_ptds() */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001959
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001960out:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001961 spin_unlock_irqrestore(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001962}
1963
1964static int isp1760_get_frame(struct usb_hcd *hcd)
1965{
1966 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1967 u32 fr;
1968
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001969 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001970 return (fr >> 3) % priv->periodic_size;
1971}
1972
1973static void isp1760_stop(struct usb_hcd *hcd)
1974{
1975 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05001976 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001977
1978 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
1979 NULL, 0);
1980 mdelay(20);
1981
1982 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001983 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001984 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001985 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1986 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001987 spin_unlock_irq(&priv->lock);
1988
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001989 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001990}
1991
1992static void isp1760_shutdown(struct usb_hcd *hcd)
1993{
Nate Case3faefc82008-06-17 11:11:38 -05001994 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001995
1996 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001997 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1998 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001999
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002000 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002001 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002002 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002003}
2004
2005static const struct hc_driver isp1760_hc_driver = {
2006 .description = "isp1760-hcd",
2007 .product_desc = "NXP ISP1760 USB Host Controller",
2008 .hcd_priv_size = sizeof(struct isp1760_hcd),
2009 .irq = isp1760_irq,
2010 .flags = HCD_MEMORY | HCD_USB2,
2011 .reset = isp1760_hc_setup,
2012 .start = isp1760_run,
2013 .stop = isp1760_stop,
2014 .shutdown = isp1760_shutdown,
2015 .urb_enqueue = isp1760_urb_enqueue,
2016 .urb_dequeue = isp1760_urb_dequeue,
2017 .endpoint_disable = isp1760_endpoint_disable,
2018 .get_frame_number = isp1760_get_frame,
2019 .hub_status_data = isp1760_hub_status_data,
2020 .hub_control = isp1760_hub_control,
2021};
2022
2023int __init init_kmem_once(void)
2024{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002025 urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
2026 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2027 SLAB_MEM_SPREAD, NULL);
2028
2029 if (!urb_listitem_cachep)
2030 return -ENOMEM;
2031
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002032 qtd_cachep = kmem_cache_create("isp1760_qtd",
2033 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2034 SLAB_MEM_SPREAD, NULL);
2035
2036 if (!qtd_cachep)
2037 return -ENOMEM;
2038
2039 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2040 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2041
2042 if (!qh_cachep) {
2043 kmem_cache_destroy(qtd_cachep);
2044 return -ENOMEM;
2045 }
2046
2047 return 0;
2048}
2049
2050void deinit_kmem_cache(void)
2051{
2052 kmem_cache_destroy(qtd_cachep);
2053 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002054 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002055}
2056
Catalin Marinasf9031f22009-02-10 16:55:45 +00002057struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2058 int irq, unsigned long irqflags,
2059 struct device *dev, const char *busname,
2060 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002061{
2062 struct usb_hcd *hcd;
2063 struct isp1760_hcd *priv;
2064 int ret;
2065
2066 if (usb_disabled())
2067 return ERR_PTR(-ENODEV);
2068
2069 /* prevent usb-core allocating DMA pages */
2070 dev->dma_mask = NULL;
2071
Kay Sievers0031a062008-05-02 06:02:41 +02002072 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002073 if (!hcd)
2074 return ERR_PTR(-ENOMEM);
2075
2076 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002077 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002078 init_memory(priv);
2079 hcd->regs = ioremap(res_start, res_len);
2080 if (!hcd->regs) {
2081 ret = -EIO;
2082 goto err_put;
2083 }
2084
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002085 hcd->irq = irq;
2086 hcd->rsrc_start = res_start;
2087 hcd->rsrc_len = res_len;
2088
Nate Casee6942d62008-05-21 16:28:20 -05002089 ret = usb_add_hcd(hcd, irq, irqflags);
2090 if (ret)
2091 goto err_unmap;
2092
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002093 return hcd;
2094
2095err_unmap:
2096 iounmap(hcd->regs);
2097
2098err_put:
2099 usb_put_hcd(hcd);
2100
2101 return ERR_PTR(ret);
2102}
2103
2104MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2105MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2106MODULE_LICENSE("GPL v2");