blob: e399e235f65627747407e22c283d4b610ffc055c [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];
Arvid Brodind05b6ec2011-05-20 00:17:45 +020037 int atl_done_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020038 struct slotinfo int_slots[32];
Arvid Brodind05b6ec2011-05-20 00:17:45 +020039 int int_done_map;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020040 struct memory_chunk memory_pool[BLOCKS];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020041 struct list_head controlqhs, bulkqhs, interruptqhs;
42 int active_ptds;
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
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200492 /* This is weird: at the first plug-in of a device there seems to be
493 one packet queued that never gets returned? */
494 priv->active_ptds = -1;
495
Nate Case3faefc82008-06-17 11:11:38 -0500496 /* ATL reset */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100497 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500498 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100499 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500500
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100501 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200502
Nate Case3faefc82008-06-17 11:11:38 -0500503 /*
504 * PORT 1 Control register of the ISP1760 is the OTG control
Thomas Hommel42c65392008-12-18 10:31:40 +0100505 * register on ISP1761. Since there is no OTG or device controller
506 * support in this driver, we use port 1 as a "normal" USB host port on
507 * both chips.
Nate Case3faefc82008-06-17 11:11:38 -0500508 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100509 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
Thomas Hommel42c65392008-12-18 10:31:40 +0100510 mdelay(10);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200511
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100512 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200513
514 return priv_init(hcd);
515}
516
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200517static u32 base_to_chip(u32 base)
518{
519 return ((base - 0x400) >> 3);
520}
521
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100522static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
523{
524 struct urb *urb;
525
526 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
527 return 1;
528
529 urb = qtd->urb;
530 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
531 return (qtd->urb != urb);
532}
533
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200534/* magic numbers that can affect system performance */
535#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
536#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
537#define EHCI_TUNE_RL_TT 0
538#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
539#define EHCI_TUNE_MULT_TT 1
540#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
541
542static void create_ptd_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100543 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200544{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200545 u32 maxpacket;
546 u32 multi;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200547 u32 rl = RL_COUNTER;
548 u32 nak = NAK_COUNTER;
549
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100550 memset(ptd, 0, sizeof(*ptd));
551
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200552 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100553 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
554 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200555 multi = 1 + ((maxpacket >> 11) & 0x3);
556 maxpacket &= 0x7ff;
557
558 /* DW0 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200559 ptd->dw0 = DW0_VALID_BIT;
560 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
561 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
562 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200563
564 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100565 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200566 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
567 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200568
Arvid Brodina041d8e2011-02-26 22:04:40 +0100569 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200570 ptd->dw1 |= DW1_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100571 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200572 ptd->dw1 |= DW1_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200573
Arvid Brodina041d8e2011-02-26 22:04:40 +0100574 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200575 /* split transaction */
576
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200577 ptd->dw1 |= DW1_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100578 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200579 ptd->dw1 |= DW1_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200580
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200581 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
582 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200583
584 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100585 if (usb_pipeint(qtd->urb->pipe) &&
586 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100587 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200588
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200589 rl = 0;
590 nak = 0;
591 } else {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200592 ptd->dw0 |= TO_DW0_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100593 if (usb_pipecontrol(qtd->urb->pipe) ||
594 usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200595 ptd->dw3 |= TO_DW3_PING(qh->ping);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200596 }
597 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100598 ptd->dw2 = 0;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200599 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
600 ptd->dw2 |= TO_DW2_RL(rl);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200601
602 /* DW3 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200603 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
604 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100605 if (usb_pipecontrol(qtd->urb->pipe)) {
606 if (qtd->data_buffer == qtd->urb->setup_packet)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200607 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100608 else if (last_qtd_of_urb(qtd, qh))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200609 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b2011-02-26 22:08:47 +0100610 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200611
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200612 ptd->dw3 |= DW3_ACTIVE_BIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200613 /* Cerr */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200614 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200615}
616
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100617static void transform_add_int(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{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100620 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200621 u32 period;
622
Arvid Brodin65f1b522011-02-26 22:07:35 +0100623 /*
624 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
625 * the algorithm from the original Philips driver code, which was
626 * pretty much used in this driver before as well, is quite horrendous
627 * and, i believe, incorrect. The code below follows the datasheet and
628 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
629 * more reliable this way (fingers crossed...).
630 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200631
Arvid Brodin65f1b522011-02-26 22:07:35 +0100632 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
633 /* urb->interval is in units of microframes (1/8 ms) */
634 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200635
Arvid Brodin65f1b522011-02-26 22:07:35 +0100636 if (qtd->urb->interval > 4)
637 usof = 0x01; /* One bit set =>
638 interval 1 ms * uFrame-match */
639 else if (qtd->urb->interval > 2)
640 usof = 0x22; /* Two bits set => interval 1/2 ms */
641 else if (qtd->urb->interval > 1)
642 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200643 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100644 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200645 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100646 /* urb->interval is in units of frames (1 ms) */
647 period = qtd->urb->interval;
648 usof = 0x0f; /* Execute Start Split on any of the
649 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200650
Arvid Brodin65f1b522011-02-26 22:07:35 +0100651 /*
652 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
653 * complete split needs to be sent. Valid only for IN." Also,
654 * "All bits can be set to one for every transfer." (p 82,
655 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
656 * that number come from? 0xff seems to work fine...
657 */
658 /* ptd->dw5 = 0x1c; */
659 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200660 }
661
Arvid Brodin65f1b522011-02-26 22:07:35 +0100662 period = period >> 1;/* Ensure equal or shorter period than requested */
663 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
664
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100665 ptd->dw2 |= period;
666 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200667}
668
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200669static void create_ptd_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100670 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200671{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200672 create_ptd_atl(qh, qtd, ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100673 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200674}
675
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100676static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200677__releases(priv->lock)
678__acquires(priv->lock)
679{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100680 struct isp1760_hcd *priv = hcd_to_priv(hcd);
681
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200682 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100683 if (urb->status == -EINPROGRESS)
684 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200685 }
686
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000687 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
688 void *ptr;
689 for (ptr = urb->transfer_buffer;
690 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
691 ptr += PAGE_SIZE)
692 flush_dcache_page(virt_to_page(ptr));
693 }
694
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200695 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100696 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200697 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100698 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200699 spin_lock(&priv->lock);
700}
701
Arvid Brodin34537732011-04-26 21:47:37 +0200702static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
703 u8 packet_type)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200704{
Arvid Brodin34537732011-04-26 21:47:37 +0200705 struct isp1760_qtd *qtd;
706
707 qtd = kmem_cache_zalloc(qtd_cachep, flags);
708 if (!qtd)
709 return NULL;
710
711 INIT_LIST_HEAD(&qtd->qtd_list);
712 qtd->urb = urb;
713 qtd->packet_type = packet_type;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200714 qtd->status = QTD_ENQUEUED;
715 qtd->actual_length = 0;
Arvid Brodin34537732011-04-26 21:47:37 +0200716
717 return qtd;
718}
719
720static void qtd_free(struct isp1760_qtd *qtd)
721{
722 WARN_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200723 kmem_cache_free(qtd_cachep, qtd);
724}
725
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200726static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
727 struct slotinfo *slots, struct isp1760_qtd *qtd,
728 struct isp1760_qh *qh, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200729{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100730 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200731 int skip_map;
732
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200733 WARN_ON((slot < 0) || (slot > 31));
734 WARN_ON(qtd->length && !qtd->payload_addr);
735 WARN_ON(slots[slot].qtd);
736 WARN_ON(slots[slot].qh);
737 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200738
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200739 slots[slot].qtd = qtd;
740 slots[slot].qh = qh;
741 qh->slot = slot;
742 qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
743 interrupt routine may preempt and expects this value. */
744 ptd_write(hcd->regs, ptd_offset, slot, ptd);
745 priv->active_ptds++;
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200746
747 /* Make sure done map has not triggered from some unlinked transfer */
748 if (ptd_offset == ATL_PTD_OFFSET) {
749 priv->atl_done_map |= reg_read32(hcd->regs,
750 HC_ATL_PTD_DONEMAP_REG);
751 priv->atl_done_map &= ~(1 << qh->slot);
752
753 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
754 skip_map &= ~(1 << qh->slot);
755 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
756 } else {
757 priv->int_done_map |= reg_read32(hcd->regs,
758 HC_INT_PTD_DONEMAP_REG);
759 priv->int_done_map &= ~(1 << qh->slot);
760
761 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
762 skip_map &= ~(1 << qh->slot);
763 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
764 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200765}
766
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200767static int is_short_bulk(struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200768{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200769 return (usb_pipebulk(qtd->urb->pipe) &&
770 (qtd->actual_length < qtd->length));
771}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200772
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200773static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
774 struct list_head *urb_list)
775{
776 int last_qtd;
777 struct isp1760_qtd *qtd, *qtd_next;
778 struct urb_listitem *urb_listitem;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200779
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200780 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
781 if (qtd->status < QTD_XFER_COMPLETE)
782 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200783
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200784 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
785 last_qtd = 1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200786 else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200787 last_qtd = qtd->urb != qtd_next->urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200788
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200789 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
790 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200791
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200792 if (qtd->status == QTD_XFER_COMPLETE) {
793 if (qtd->actual_length) {
794 switch (qtd->packet_type) {
795 case IN_PID:
796 mem_reads8(hcd->regs, qtd->payload_addr,
797 qtd->data_buffer,
798 qtd->actual_length);
799 /* Fall through (?) */
800 case OUT_PID:
801 qtd->urb->actual_length +=
802 qtd->actual_length;
803 /* Fall through ... */
804 case SETUP_PID:
805 break;
806 }
807 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200808
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200809 if (is_short_bulk(qtd)) {
810 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
811 qtd->urb->status = -EREMOTEIO;
812 if (!last_qtd)
813 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200814 }
815 }
816
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200817 if (qtd->payload_addr)
818 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200819
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200820 if (last_qtd) {
821 if ((qtd->status == QTD_RETIRE) &&
822 (qtd->urb->status == -EINPROGRESS))
823 qtd->urb->status = -EPIPE;
824 /* Defer calling of urb_done() since it releases lock */
825 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
826 GFP_ATOMIC);
827 if (unlikely(!urb_listitem))
828 break;
829 urb_listitem->urb = qtd->urb;
830 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200831 }
832
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200833 list_del(&qtd->qtd_list);
834 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200835 }
836}
837
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200838#define ENQUEUE_DEPTH 2
839static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
840{
841 struct isp1760_hcd *priv = hcd_to_priv(hcd);
842 int ptd_offset;
843 struct slotinfo *slots;
844 int curr_slot, free_slot;
845 int n;
846 struct ptd ptd;
847 struct isp1760_qtd *qtd;
848
849 if (unlikely(list_empty(&qh->qtd_list))) {
850 WARN_ON(1);
851 return;
852 }
853
854 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
855 qtd_list)->urb->pipe)) {
856 ptd_offset = INT_PTD_OFFSET;
857 slots = priv->int_slots;
858 } else {
859 ptd_offset = ATL_PTD_OFFSET;
860 slots = priv->atl_slots;
861 }
862
863 free_slot = -1;
864 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
865 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
866 free_slot = curr_slot;
867 if (slots[curr_slot].qh == qh)
868 break;
869 }
870
871 n = 0;
872 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
873 if (qtd->status == QTD_ENQUEUED) {
874 WARN_ON(qtd->payload_addr);
875 alloc_mem(hcd, qtd);
876 if ((qtd->length) && (!qtd->payload_addr))
877 break;
878
879 if ((qtd->length) &&
880 ((qtd->packet_type == SETUP_PID) ||
881 (qtd->packet_type == OUT_PID))) {
882 mem_writes8(hcd->regs, qtd->payload_addr,
883 qtd->data_buffer, qtd->length);
884 }
885
886 qtd->status = QTD_PAYLOAD_ALLOC;
887 }
888
889 if (qtd->status == QTD_PAYLOAD_ALLOC) {
890/*
891 if ((curr_slot > 31) && (free_slot == -1))
892 dev_dbg(hcd->self.controller, "%s: No slot "
893 "available for transfer\n", __func__);
894*/
895 /* Start xfer for this endpoint if not already done */
896 if ((curr_slot > 31) && (free_slot > -1)) {
897 if (usb_pipeint(qtd->urb->pipe))
898 create_ptd_int(qh, qtd, &ptd);
899 else
900 create_ptd_atl(qh, qtd, &ptd);
901
902 start_bus_transfer(hcd, ptd_offset, free_slot,
903 slots, qtd, qh, &ptd);
904 curr_slot = free_slot;
905 }
906
907 n++;
908 if (n >= ENQUEUE_DEPTH)
909 break;
910 }
911 }
912}
913
914void schedule_ptds(struct usb_hcd *hcd)
915{
916 struct isp1760_hcd *priv;
917 struct isp1760_qh *qh, *qh_next;
918 struct list_head *ep_queue;
919 struct usb_host_endpoint *ep;
920 LIST_HEAD(urb_list);
921 struct urb_listitem *urb_listitem, *urb_listitem_next;
922
923 if (!hcd) {
924 WARN_ON(1);
925 return;
926 }
927
928 priv = hcd_to_priv(hcd);
929
930 /*
931 * check finished/retired xfers, transfer payloads, call urb_done()
932 */
933 ep_queue = &priv->interruptqhs;
934 while (ep_queue) {
935 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
936 ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
937 qtd_list)->urb->ep;
938 collect_qtds(hcd, qh, &urb_list);
939 if (list_empty(&qh->qtd_list)) {
940 list_del(&qh->qh_list);
941 if (ep->hcpriv == NULL) {
942 /* Endpoint has been disabled, so we
943 can free the associated queue head. */
944 qh_free(qh);
945 }
946 }
947 }
948
949 if (ep_queue == &priv->interruptqhs)
950 ep_queue = &priv->controlqhs;
951 else if (ep_queue == &priv->controlqhs)
952 ep_queue = &priv->bulkqhs;
953 else
954 ep_queue = NULL;
955 }
956
957 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
958 urb_list) {
959 isp1760_urb_done(hcd, urb_listitem->urb);
960 kmem_cache_free(urb_listitem_cachep, urb_listitem);
961 }
962
963 /*
964 * Schedule packets for transfer.
965 *
966 * According to USB2.0 specification:
967 *
968 * 1st prio: interrupt xfers, up to 80 % of bandwidth
969 * 2nd prio: control xfers
970 * 3rd prio: bulk xfers
971 *
972 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
973 * is very unclear on how to prioritize traffic):
974 *
975 * 1) Enqueue any queued control transfers, as long as payload chip mem
976 * and PTD ATL slots are available.
977 * 2) Enqueue any queued INT transfers, as long as payload chip mem
978 * and PTD INT slots are available.
979 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
980 * and PTD ATL slots are available.
981 *
982 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
983 * conservation of chip mem and performance.
984 *
985 * I'm sure this scheme could be improved upon!
986 */
987 ep_queue = &priv->controlqhs;
988 while (ep_queue) {
989 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
990 enqueue_qtds(hcd, qh);
991
992 if (ep_queue == &priv->controlqhs)
993 ep_queue = &priv->interruptqhs;
994 else if (ep_queue == &priv->interruptqhs)
995 ep_queue = &priv->bulkqhs;
996 else
997 ep_queue = NULL;
998 }
999}
1000
1001#define PTD_STATE_QTD_DONE 1
1002#define PTD_STATE_QTD_RELOAD 2
1003#define PTD_STATE_URB_RETIRE 3
1004
1005static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1006 struct urb *urb)
1007{
1008 __dw dw4;
1009 int i;
1010
1011 dw4 = ptd->dw4;
1012 dw4 >>= 8;
1013
1014 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1015 need to handle these errors? Is it done in hardware? */
1016
1017 if (ptd->dw3 & DW3_HALT_BIT) {
1018
1019 urb->status = -EPROTO; /* Default unknown error */
1020
1021 for (i = 0; i < 8; i++) {
1022 switch (dw4 & 0x7) {
1023 case INT_UNDERRUN:
1024 dev_dbg(hcd->self.controller, "%s: underrun "
1025 "during uFrame %d\n",
1026 __func__, i);
1027 urb->status = -ECOMM; /* Could not write data */
1028 break;
1029 case INT_EXACT:
1030 dev_dbg(hcd->self.controller, "%s: transaction "
1031 "error during uFrame %d\n",
1032 __func__, i);
1033 urb->status = -EPROTO; /* timeout, bad CRC, PID
1034 error etc. */
1035 break;
1036 case INT_BABBLE:
1037 dev_dbg(hcd->self.controller, "%s: babble "
1038 "error during uFrame %d\n",
1039 __func__, i);
1040 urb->status = -EOVERFLOW;
1041 break;
1042 }
1043 dw4 >>= 3;
1044 }
1045
1046 return PTD_STATE_URB_RETIRE;
1047 }
1048
1049 return PTD_STATE_QTD_DONE;
1050}
1051
1052static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1053 struct urb *urb)
1054{
1055 WARN_ON(!ptd);
1056 if (ptd->dw3 & DW3_HALT_BIT) {
1057 if (ptd->dw3 & DW3_BABBLE_BIT)
1058 urb->status = -EOVERFLOW;
1059 else if (FROM_DW3_CERR(ptd->dw3))
1060 urb->status = -EPIPE; /* Stall */
1061 else if (ptd->dw3 & DW3_ERROR_BIT)
1062 urb->status = -EPROTO; /* XactErr */
1063 else
1064 urb->status = -EPROTO; /* Unknown */
1065/*
1066 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1067 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1068 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1069 __func__,
1070 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1071 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1072*/
1073 return PTD_STATE_URB_RETIRE;
1074 }
1075
1076 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1077 /* Transfer Error, *but* active and no HALT -> reload */
1078 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1079 return PTD_STATE_QTD_RELOAD;
1080 }
1081
1082 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1083 /*
1084 * NAKs are handled in HW by the chip. Usually if the
1085 * device is not able to send data fast enough.
1086 * This happens mostly on slower hardware.
1087 */
1088 return PTD_STATE_QTD_RELOAD;
1089 }
1090
1091 return PTD_STATE_QTD_DONE;
1092}
1093
1094static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1095{
1096 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1097 u32 imask;
1098 irqreturn_t irqret = IRQ_NONE;
1099 struct ptd ptd;
1100 struct isp1760_qh *qh;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001101 int slot;
1102 int state;
1103 struct slotinfo *slots;
1104 u32 ptd_offset;
1105 struct isp1760_qtd *qtd;
1106 int modified;
1107 static int last_active_ptds;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001108 int int_skip_map, atl_skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001109
1110 spin_lock(&priv->lock);
1111
1112 if (!(hcd->state & HC_STATE_RUNNING))
1113 goto leave;
1114
1115 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1116 if (unlikely(!imask))
1117 goto leave;
1118 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1119
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001120 int_skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1121 atl_skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1122 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1123 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1124 priv->int_done_map &= ~int_skip_map;
1125 priv->atl_done_map &= ~atl_skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001126
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001127 modified = priv->int_done_map | priv->atl_done_map;
1128
1129 while (priv->int_done_map || priv->atl_done_map) {
1130 if (priv->int_done_map) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001131 /* INT ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001132 slot = __ffs(priv->int_done_map);
1133 priv->int_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001134 slots = priv->int_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001135 /* This should not trigger, and could be removed if
1136 noone have any problems with it triggering: */
1137 if (!slots[slot].qh) {
1138 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001139 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001140 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001141 ptd_offset = INT_PTD_OFFSET;
1142 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1143 state = check_int_transfer(hcd, &ptd,
1144 slots[slot].qtd->urb);
1145 } else {
1146 /* ATL ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001147 slot = __ffs(priv->atl_done_map);
1148 priv->atl_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001149 slots = priv->atl_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001150 /* This should not trigger, and could be removed if
1151 noone have any problems with it triggering: */
1152 if (!slots[slot].qh) {
1153 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001154 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001155 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001156 ptd_offset = ATL_PTD_OFFSET;
1157 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1158 state = check_atl_transfer(hcd, &ptd,
1159 slots[slot].qtd->urb);
1160 }
1161
1162 qtd = slots[slot].qtd;
1163 slots[slot].qtd = NULL;
1164 qh = slots[slot].qh;
1165 slots[slot].qh = NULL;
1166 priv->active_ptds--;
1167 qh->slot = -1;
1168
1169 WARN_ON(qtd->status != QTD_XFER_STARTED);
1170
1171 switch (state) {
1172 case PTD_STATE_QTD_DONE:
1173 if ((usb_pipeint(qtd->urb->pipe)) &&
1174 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1175 qtd->actual_length =
1176 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1177 else
1178 qtd->actual_length =
1179 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1180
1181 qtd->status = QTD_XFER_COMPLETE;
1182 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1183 is_short_bulk(qtd))
1184 qtd = NULL;
1185 else
1186 qtd = list_entry(qtd->qtd_list.next,
1187 typeof(*qtd), qtd_list);
1188
1189 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1190 qh->ping = FROM_DW3_PING(ptd.dw3);
1191 break;
1192
1193 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1194 qtd->status = QTD_PAYLOAD_ALLOC;
1195 ptd.dw0 |= DW0_VALID_BIT;
1196 /* RL counter = ERR counter */
1197 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1198 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1199 ptd.dw3 &= ~TO_DW3_CERR(3);
1200 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1201 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1202 qh->ping = FROM_DW3_PING(ptd.dw3);
1203 break;
1204
1205 case PTD_STATE_URB_RETIRE:
1206 qtd->status = QTD_RETIRE;
1207 qtd = NULL;
1208 qh->toggle = 0;
1209 qh->ping = 0;
1210 break;
1211
1212 default:
1213 WARN_ON(1);
1214 continue;
1215 }
1216
1217 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1218 if (slots == priv->int_slots) {
1219 if (state == PTD_STATE_QTD_RELOAD)
1220 dev_err(hcd->self.controller,
1221 "%s: PTD_STATE_QTD_RELOAD on "
1222 "interrupt packet\n", __func__);
1223 if (state != PTD_STATE_QTD_RELOAD)
1224 create_ptd_int(qh, qtd, &ptd);
1225 } else {
1226 if (state != PTD_STATE_QTD_RELOAD)
1227 create_ptd_atl(qh, qtd, &ptd);
1228 }
1229
1230 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1231 qh, &ptd);
1232 }
1233 }
1234
1235 if (modified)
1236 schedule_ptds(hcd);
1237
1238 /* ISP1760 Errata 2 explains that interrupts may be missed (or not
1239 happen?) if two USB devices are running simultaneously. Perhaps
1240 this happens when a PTD is finished during interrupt handling;
1241 enable SOF interrupts if PTDs are still scheduled when exiting this
1242 interrupt handler, just to be safe. */
1243
1244 if (priv->active_ptds != last_active_ptds) {
1245 if (priv->active_ptds > 0)
1246 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1247 INTERRUPT_ENABLE_SOT_MASK);
1248 else
1249 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
1250 INTERRUPT_ENABLE_MASK);
1251 last_active_ptds = priv->active_ptds;
1252 }
1253
1254 irqret = IRQ_HANDLED;
1255leave:
1256 spin_unlock(&priv->lock);
1257
1258 return irqret;
1259}
1260
Arvid Brodin0ba79052011-08-21 08:29:25 +02001261static int isp1760_run(struct usb_hcd *hcd)
1262{
1263 int retval;
1264 u32 temp;
1265 u32 command;
1266 u32 chipid;
1267
1268 hcd->uses_new_polling = 1;
1269
1270 hcd->state = HC_STATE_RUNNING;
1271
1272 /* Set PTD interrupt AND & OR maps */
1273 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1274 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1275 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1276 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1277 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1278 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1279 /* step 23 passed */
1280
1281 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1282 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1283
1284 command = reg_read32(hcd->regs, HC_USBCMD);
1285 command &= ~(CMD_LRESET|CMD_RESET);
1286 command |= CMD_RUN;
1287 reg_write32(hcd->regs, HC_USBCMD, command);
1288
1289 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1290 if (retval)
1291 return retval;
1292
1293 /*
1294 * XXX
1295 * Spec says to write FLAG_CF as last config action, priv code grabs
1296 * the semaphore while doing so.
1297 */
1298 down_write(&ehci_cf_port_reset_rwsem);
1299 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1300
1301 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1302 up_write(&ehci_cf_port_reset_rwsem);
1303 if (retval)
1304 return retval;
1305
1306 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1307 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1308 chipid & 0xffff, chipid >> 16);
1309
1310 /* PTD Register Init Part 2, Step 28 */
1311
1312 /* Setup registers controlling PTD checking */
1313 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1314 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1315 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1316 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1317 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1318 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1319 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1320 ATL_BUF_FILL | INT_BUF_FILL);
1321
1322 /* GRR this is run-once init(), being done every time the HC starts.
1323 * So long as they're part of class devices, we can't do it init()
1324 * since the class device isn't created that early.
1325 */
1326 return 0;
1327}
1328
Arvid Brodin34537732011-04-26 21:47:37 +02001329static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001330{
Arvid Brodin34537732011-04-26 21:47:37 +02001331 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001332
Arvid Brodin34537732011-04-26 21:47:37 +02001333 if (len > MAX_PAYLOAD_SIZE)
1334 len = MAX_PAYLOAD_SIZE;
1335 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001336
Arvid Brodin34537732011-04-26 21:47:37 +02001337 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001338}
1339
Arvid Brodin34537732011-04-26 21:47:37 +02001340static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001341{
Arvid Brodin34537732011-04-26 21:47:37 +02001342 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001343
Arvid Brodin34537732011-04-26 21:47:37 +02001344 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001345 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001346 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001347 }
1348}
1349
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001350/*
Arvid Brodin34537732011-04-26 21:47:37 +02001351 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1352 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001353 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001354#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001355static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001356 struct urb *urb, struct list_head *head, gfp_t flags)
1357{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001358 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001359 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001360 int len, maxpacketsize;
1361 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001362
1363 /*
1364 * URBs map to sequences of QTDs: one logical transaction
1365 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001366
Arvid Brodin34537732011-04-26 21:47:37 +02001367 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1368 /* XXX This looks like usb storage / SCSI bug */
1369 dev_err(hcd->self.controller,
1370 "buf is null, dma is %08lx len is %d\n",
1371 (long unsigned)urb->transfer_dma,
1372 urb->transfer_buffer_length);
1373 WARN_ON(1);
1374 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001375
Arvid Brodin34537732011-04-26 21:47:37 +02001376 if (usb_pipein(urb->pipe))
1377 packet_type = IN_PID;
1378 else
1379 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001380
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001381 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001382 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001383 if (!qtd)
1384 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001385 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001386 list_add_tail(&qtd->qtd_list, head);
1387
1388 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001389 if (urb->transfer_buffer_length == 0)
1390 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001391 }
1392
Arvid Brodin34537732011-04-26 21:47:37 +02001393 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1394 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001395
1396 /*
1397 * buffer gets wrapped in one or more qtds;
1398 * last one may be "short" (including zero len)
1399 * and may serve as a control status ack
1400 */
Arvid Brodin34537732011-04-26 21:47:37 +02001401 buf = urb->transfer_buffer;
1402 len = urb->transfer_buffer_length;
1403
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001404 for (;;) {
1405 int this_qtd_len;
1406
Arvid Brodin34537732011-04-26 21:47:37 +02001407 qtd = qtd_alloc(flags, urb, packet_type);
1408 if (!qtd)
1409 goto cleanup;
1410 this_qtd_len = qtd_fill(qtd, buf, len);
1411 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001412
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001413 len -= this_qtd_len;
1414 buf += this_qtd_len;
1415
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001416 if (len <= 0)
1417 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001418 }
1419
1420 /*
1421 * control requests may need a terminating data "status" ack;
1422 * bulk ones may need a terminating short packet (zero length).
1423 */
1424 if (urb->transfer_buffer_length != 0) {
1425 int one_more = 0;
1426
1427 if (usb_pipecontrol(urb->pipe)) {
1428 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001429 if (packet_type == IN_PID)
1430 packet_type = OUT_PID;
1431 else
1432 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001433 } else if (usb_pipebulk(urb->pipe)
1434 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001435 && !(urb->transfer_buffer_length %
1436 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001437 one_more = 1;
1438 }
1439 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001440 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001441 if (!qtd)
1442 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001443
1444 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001445 qtd_fill(qtd, NULL, 0);
1446 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001447 }
1448 }
1449
Arvid Brodin34537732011-04-26 21:47:37 +02001450 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001451
1452cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001453 qtd_list_free(head);
1454}
1455
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001456static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1457 gfp_t mem_flags)
1458{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001459 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1460 struct list_head *ep_queue;
1461 struct isp1760_qh *qh, *qhit;
1462 unsigned long spinflags;
1463 LIST_HEAD(new_qtds);
1464 int retval;
1465 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001466
1467 switch (usb_pipetype(urb->pipe)) {
1468 case PIPE_CONTROL:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001469 ep_queue = &priv->controlqhs;
1470 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001471 case PIPE_BULK:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001472 ep_queue = &priv->bulkqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001473 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001474 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001475 if (urb->interval < 0)
1476 return -EINVAL;
1477 /* FIXME: Check bandwidth */
1478 ep_queue = &priv->interruptqhs;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001479 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001480 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001481 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1482 "not yet supported\n",
1483 __func__);
1484 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001485 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001486 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1487 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001488 return -EPIPE;
1489 }
1490
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001491 if (usb_pipein(urb->pipe))
1492 urb->actual_length = 0;
1493
1494 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1495 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001496 return -ENOMEM;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001497 urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
Arvid Brodin34537732011-04-26 21:47:37 +02001498
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001499 retval = 0;
1500 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001501
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001502 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1503 retval = -ESHUTDOWN;
1504 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001505 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001506 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1507 if (retval)
1508 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001509
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001510 qh = urb->ep->hcpriv;
1511 if (qh) {
1512 qh_in_queue = 0;
1513 list_for_each_entry(qhit, ep_queue, qh_list) {
1514 if (qhit == qh) {
1515 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001516 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001517 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001518 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001519 if (!qh_in_queue)
1520 list_add_tail(&qh->qh_list, ep_queue);
1521 } else {
1522 qh = qh_alloc(GFP_ATOMIC);
1523 if (!qh) {
1524 retval = -ENOMEM;
1525 goto out;
1526 }
1527 list_add_tail(&qh->qh_list, ep_queue);
1528 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001529 }
1530
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001531 list_splice_tail(&new_qtds, &qh->qtd_list);
1532 schedule_ptds(hcd);
1533
1534out:
1535 spin_unlock_irqrestore(&priv->lock, spinflags);
1536 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001537}
1538
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001539static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1540 struct isp1760_qh *qh)
1541{
1542 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1543 int skip_map;
1544
1545 WARN_ON(qh->slot == -1);
1546
1547 /* We need to forcefully reclaim the slot since some transfers never
1548 return, e.g. interrupt transfers and NAKed bulk transfers. */
Arvid Brodin8b1ab602011-06-17 18:45:37 +02001549 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001550 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1551 skip_map |= (1 << qh->slot);
1552 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1553 priv->atl_slots[qh->slot].qh = NULL;
1554 priv->atl_slots[qh->slot].qtd = NULL;
1555 } else {
1556 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1557 skip_map |= (1 << qh->slot);
1558 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1559 priv->int_slots[qh->slot].qh = NULL;
1560 priv->int_slots[qh->slot].qtd = NULL;
1561 }
1562
1563 qh->slot = -1;
1564 priv->active_ptds--;
1565}
1566
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001567static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1568 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001569{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001570 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001571 unsigned long spinflags;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001572 struct isp1760_qh *qh;
1573 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001574 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001575
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001576 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodin17d3e142011-07-20 03:13:46 +02001577 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1578 if (retval)
1579 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001580
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001581 qh = urb->ep->hcpriv;
1582 if (!qh) {
1583 retval = -EINVAL;
1584 goto out;
1585 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001586
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001587 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1588 if (qtd->urb == urb) {
1589 if (qtd->status == QTD_XFER_STARTED)
1590 kill_transfer(hcd, urb, qh);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001591 qtd->status = QTD_RETIRE;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001592 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001593
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001594 urb->status = status;
1595 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001596
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001597out:
1598 spin_unlock_irqrestore(&priv->lock, spinflags);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001599 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001600}
1601
Arvid Brodin079cdb02011-05-20 00:17:34 +02001602static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1603 struct usb_host_endpoint *ep)
1604{
1605 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001606 unsigned long spinflags;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001607 struct isp1760_qh *qh;
1608 struct isp1760_qtd *qtd;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001609
1610 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001611
Arvid Brodin079cdb02011-05-20 00:17:34 +02001612 qh = ep->hcpriv;
1613 if (!qh)
1614 goto out;
1615
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001616 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1617 if (qtd->status == QTD_XFER_STARTED)
1618 kill_transfer(hcd, qtd->urb, qh);
1619 qtd->status = QTD_RETIRE;
1620 qtd->urb->status = -ECONNRESET;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001621 }
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001622
Arvid Brodin079cdb02011-05-20 00:17:34 +02001623 ep->hcpriv = NULL;
1624 /* Cannot free qh here since it will be parsed by schedule_ptds() */
1625
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001626 schedule_ptds(hcd);
1627
Arvid Brodin079cdb02011-05-20 00:17:34 +02001628out:
1629 spin_unlock_irqrestore(&priv->lock, spinflags);
1630}
1631
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001632static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1633{
1634 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1635 u32 temp, status = 0;
1636 u32 mask;
1637 int retval = 1;
1638 unsigned long flags;
1639
1640 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1641 if (!HC_IS_RUNNING(hcd->state))
1642 return 0;
1643
1644 /* init status to no-changes */
1645 buf[0] = 0;
1646 mask = PORT_CSC;
1647
1648 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001649 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001650
1651 if (temp & PORT_OWNER) {
1652 if (temp & PORT_CSC) {
1653 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001654 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001655 goto done;
1656 }
1657 }
1658
1659 /*
1660 * Return status information even for ports with OWNER set.
1661 * Otherwise khubd wouldn't see the disconnect event when a
1662 * high-speed device is switched over to the companion
1663 * controller by the user.
1664 */
1665
1666 if ((temp & mask) != 0
1667 || ((temp & PORT_RESUME) != 0
1668 && time_after_eq(jiffies,
1669 priv->reset_done))) {
1670 buf [0] |= 1 << (0 + 1);
1671 status = STS_PCD;
1672 }
1673 /* FIXME autosuspend idle root hubs */
1674done:
1675 spin_unlock_irqrestore(&priv->lock, flags);
1676 return status ? retval : 0;
1677}
1678
1679static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1680 struct usb_hub_descriptor *desc)
1681{
1682 int ports = HCS_N_PORTS(priv->hcs_params);
1683 u16 temp;
1684
1685 desc->bDescriptorType = 0x29;
1686 /* priv 1.0, 2.3.9 says 20ms max */
1687 desc->bPwrOn2PwrGood = 10;
1688 desc->bHubContrCurrent = 0;
1689
1690 desc->bNbrPorts = ports;
1691 temp = 1 + (ports / 8);
1692 desc->bDescLength = 7 + 2 * temp;
1693
Sarah Sharpda130512010-11-30 15:55:51 -08001694 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001695 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1696 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001697
1698 /* per-port overcurrent reporting */
1699 temp = 0x0008;
1700 if (HCS_PPC(priv->hcs_params))
1701 /* per-port power control */
1702 temp |= 0x0001;
1703 else
1704 /* no power switching */
1705 temp |= 0x0002;
1706 desc->wHubCharacteristics = cpu_to_le16(temp);
1707}
1708
1709#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1710
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001711static int check_reset_complete(struct usb_hcd *hcd, int index,
1712 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001713{
1714 if (!(port_status & PORT_CONNECT))
1715 return port_status;
1716
1717 /* if reset finished and it's still not enabled -- handoff */
1718 if (!(port_status & PORT_PE)) {
1719
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001720 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001721 "port %d full speed --> companion\n",
1722 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001723
1724 port_status |= PORT_OWNER;
1725 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001726 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001727
1728 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001729 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001730 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001731
1732 return port_status;
1733}
1734
1735static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1736 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1737{
1738 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1739 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001740 u32 temp, status;
1741 unsigned long flags;
1742 int retval = 0;
1743 unsigned selector;
1744
1745 /*
1746 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1747 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1748 * (track current state ourselves) ... blink for diagnostics,
1749 * power, "this is the one", etc. EHCI spec supports this.
1750 */
1751
1752 spin_lock_irqsave(&priv->lock, flags);
1753 switch (typeReq) {
1754 case ClearHubFeature:
1755 switch (wValue) {
1756 case C_HUB_LOCAL_POWER:
1757 case C_HUB_OVER_CURRENT:
1758 /* no hub-wide feature/status flags */
1759 break;
1760 default:
1761 goto error;
1762 }
1763 break;
1764 case ClearPortFeature:
1765 if (!wIndex || wIndex > ports)
1766 goto error;
1767 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001768 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001769
1770 /*
1771 * Even if OWNER is set, so the port is owned by the
1772 * companion controller, khubd needs to be able to clear
1773 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001774 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001775 */
1776
1777 switch (wValue) {
1778 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001779 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001780 break;
1781 case USB_PORT_FEAT_C_ENABLE:
1782 /* XXX error? */
1783 break;
1784 case USB_PORT_FEAT_SUSPEND:
1785 if (temp & PORT_RESET)
1786 goto error;
1787
1788 if (temp & PORT_SUSPEND) {
1789 if ((temp & PORT_PE) == 0)
1790 goto error;
1791 /* resume signaling for 20 msec */
1792 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001793 reg_write32(hcd->regs, HC_PORTSC1,
1794 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001795 priv->reset_done = jiffies +
1796 msecs_to_jiffies(20);
1797 }
1798 break;
1799 case USB_PORT_FEAT_C_SUSPEND:
1800 /* we auto-clear this feature */
1801 break;
1802 case USB_PORT_FEAT_POWER:
1803 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001804 reg_write32(hcd->regs, HC_PORTSC1,
1805 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001806 break;
1807 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001808 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001809 break;
1810 case USB_PORT_FEAT_C_OVER_CURRENT:
1811 /* XXX error ?*/
1812 break;
1813 case USB_PORT_FEAT_C_RESET:
1814 /* GetPortStatus clears reset */
1815 break;
1816 default:
1817 goto error;
1818 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001819 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001820 break;
1821 case GetHubDescriptor:
1822 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1823 buf);
1824 break;
1825 case GetHubStatus:
1826 /* no hub-wide feature/status flags */
1827 memset(buf, 0, 4);
1828 break;
1829 case GetPortStatus:
1830 if (!wIndex || wIndex > ports)
1831 goto error;
1832 wIndex--;
1833 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001834 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001835
1836 /* wPortChange bits */
1837 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001838 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001839
1840
1841 /* whoever resumes must GetPortStatus to complete it!! */
1842 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001843 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001844
1845 /* Remote Wakeup received? */
1846 if (!priv->reset_done) {
1847 /* resume signaling for 20 msec */
1848 priv->reset_done = jiffies
1849 + msecs_to_jiffies(20);
1850 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001851 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001852 }
1853
1854 /* resume completed? */
1855 else if (time_after_eq(jiffies,
1856 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001857 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001858 priv->reset_done = 0;
1859
1860 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001861 temp = reg_read32(hcd->regs, HC_PORTSC1);
1862 reg_write32(hcd->regs, HC_PORTSC1,
1863 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1864 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001865 PORT_RESUME, 0, 2000 /* 2msec */);
1866 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001867 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001868 "port %d resume error %d\n",
1869 wIndex + 1, retval);
1870 goto error;
1871 }
1872 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1873 }
1874 }
1875
1876 /* whoever resets must GetPortStatus to complete it!! */
1877 if ((temp & PORT_RESET)
1878 && time_after_eq(jiffies,
1879 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001880 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001881 priv->reset_done = 0;
1882
1883 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001884 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001885 /* REVISIT: some hardware needs 550+ usec to clear
1886 * this bit; seems too long to spin routinely...
1887 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001888 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001889 PORT_RESET, 0, 750);
1890 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001891 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001892 wIndex + 1, retval);
1893 goto error;
1894 }
1895
1896 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001897 temp = check_reset_complete(hcd, wIndex,
1898 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001899 }
1900 /*
1901 * Even if OWNER is set, there's no harm letting khubd
1902 * see the wPortStatus values (they should all be 0 except
1903 * for PORT_POWER anyway).
1904 */
1905
1906 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001907 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001908
1909 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001910 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001911 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001912 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001913 }
1914 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001915 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001916 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001917 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001918 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001919 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001920 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001921 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001922
1923 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1924 break;
1925 case SetHubFeature:
1926 switch (wValue) {
1927 case C_HUB_LOCAL_POWER:
1928 case C_HUB_OVER_CURRENT:
1929 /* no hub-wide feature/status flags */
1930 break;
1931 default:
1932 goto error;
1933 }
1934 break;
1935 case SetPortFeature:
1936 selector = wIndex >> 8;
1937 wIndex &= 0xff;
1938 if (!wIndex || wIndex > ports)
1939 goto error;
1940 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001941 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001942 if (temp & PORT_OWNER)
1943 break;
1944
1945/* temp &= ~PORT_RWC_BITS; */
1946 switch (wValue) {
1947 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001948 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001949 break;
1950
1951 case USB_PORT_FEAT_SUSPEND:
1952 if ((temp & PORT_PE) == 0
1953 || (temp & PORT_RESET) != 0)
1954 goto error;
1955
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001956 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001957 break;
1958 case USB_PORT_FEAT_POWER:
1959 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001960 reg_write32(hcd->regs, HC_PORTSC1,
1961 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001962 break;
1963 case USB_PORT_FEAT_RESET:
1964 if (temp & PORT_RESUME)
1965 goto error;
1966 /* line status bits may report this as low speed,
1967 * which can be fine if this root hub has a
1968 * transaction translator built in.
1969 */
1970 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1971 && PORT_USB11(temp)) {
1972 temp |= PORT_OWNER;
1973 } else {
1974 temp |= PORT_RESET;
1975 temp &= ~PORT_PE;
1976
1977 /*
1978 * caller must wait, then call GetPortStatus
1979 * usb 2.0 spec says 50 ms resets on root
1980 */
1981 priv->reset_done = jiffies +
1982 msecs_to_jiffies(50);
1983 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001984 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001985 break;
1986 default:
1987 goto error;
1988 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001989 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001990 break;
1991
1992 default:
1993error:
1994 /* "stall" on error */
1995 retval = -EPIPE;
1996 }
1997 spin_unlock_irqrestore(&priv->lock, flags);
1998 return retval;
1999}
2000
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002001static int isp1760_get_frame(struct usb_hcd *hcd)
2002{
2003 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2004 u32 fr;
2005
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002006 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002007 return (fr >> 3) % priv->periodic_size;
2008}
2009
2010static void isp1760_stop(struct usb_hcd *hcd)
2011{
2012 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002013 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002014
2015 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2016 NULL, 0);
2017 mdelay(20);
2018
2019 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002020 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002021 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002022 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2023 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002024 spin_unlock_irq(&priv->lock);
2025
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002026 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002027}
2028
2029static void isp1760_shutdown(struct usb_hcd *hcd)
2030{
Nate Case3faefc82008-06-17 11:11:38 -05002031 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002032
2033 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002034 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2035 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002036
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002037 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002038 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002039 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002040}
2041
2042static const struct hc_driver isp1760_hc_driver = {
2043 .description = "isp1760-hcd",
2044 .product_desc = "NXP ISP1760 USB Host Controller",
2045 .hcd_priv_size = sizeof(struct isp1760_hcd),
2046 .irq = isp1760_irq,
2047 .flags = HCD_MEMORY | HCD_USB2,
2048 .reset = isp1760_hc_setup,
2049 .start = isp1760_run,
2050 .stop = isp1760_stop,
2051 .shutdown = isp1760_shutdown,
2052 .urb_enqueue = isp1760_urb_enqueue,
2053 .urb_dequeue = isp1760_urb_dequeue,
2054 .endpoint_disable = isp1760_endpoint_disable,
2055 .get_frame_number = isp1760_get_frame,
2056 .hub_status_data = isp1760_hub_status_data,
2057 .hub_control = isp1760_hub_control,
2058};
2059
2060int __init init_kmem_once(void)
2061{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002062 urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
2063 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2064 SLAB_MEM_SPREAD, NULL);
2065
2066 if (!urb_listitem_cachep)
2067 return -ENOMEM;
2068
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002069 qtd_cachep = kmem_cache_create("isp1760_qtd",
2070 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2071 SLAB_MEM_SPREAD, NULL);
2072
2073 if (!qtd_cachep)
2074 return -ENOMEM;
2075
2076 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2077 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2078
2079 if (!qh_cachep) {
2080 kmem_cache_destroy(qtd_cachep);
2081 return -ENOMEM;
2082 }
2083
2084 return 0;
2085}
2086
2087void deinit_kmem_cache(void)
2088{
2089 kmem_cache_destroy(qtd_cachep);
2090 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002091 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002092}
2093
Catalin Marinasf9031f22009-02-10 16:55:45 +00002094struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2095 int irq, unsigned long irqflags,
2096 struct device *dev, const char *busname,
2097 unsigned int devflags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002098{
2099 struct usb_hcd *hcd;
2100 struct isp1760_hcd *priv;
2101 int ret;
2102
2103 if (usb_disabled())
2104 return ERR_PTR(-ENODEV);
2105
2106 /* prevent usb-core allocating DMA pages */
2107 dev->dma_mask = NULL;
2108
Kay Sievers0031a062008-05-02 06:02:41 +02002109 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002110 if (!hcd)
2111 return ERR_PTR(-ENOMEM);
2112
2113 priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002114 priv->devflags = devflags;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002115 init_memory(priv);
2116 hcd->regs = ioremap(res_start, res_len);
2117 if (!hcd->regs) {
2118 ret = -EIO;
2119 goto err_put;
2120 }
2121
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002122 hcd->irq = irq;
2123 hcd->rsrc_start = res_start;
2124 hcd->rsrc_len = res_len;
2125
Nate Casee6942d62008-05-21 16:28:20 -05002126 ret = usb_add_hcd(hcd, irq, irqflags);
2127 if (ret)
2128 goto err_unmap;
2129
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002130 return hcd;
2131
2132err_unmap:
2133 iounmap(hcd->regs);
2134
2135err_put:
2136 usb_put_hcd(hcd);
2137
2138 return ERR_PTR(ret);
2139}
2140
2141MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2142MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2143MODULE_LICENSE("GPL v2");