blob: 42672d6ec5255b3f93fbb0cd130799ba6fbb9809 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002/*
3 * Driver for the NXP ISP1760 chip
4 *
5 * However, the code might contain some bugs. What doesn't work for sure is:
6 * - ISO
7 * - OTG
8 e The interrupt line is configured as active low, level.
9 *
10 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
11 *
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020012 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
13 *
Sebastian Siewiordb11e472008-04-24 00:37:04 +020014 */
Laurent Pinchart7eb42c62015-01-21 00:55:40 +020015#include <linux/gpio/consumer.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020016#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/list.h>
20#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020021#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020022#include <linux/debugfs.h>
23#include <linux/uaccess.h>
24#include <linux/io.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000025#include <linux/mm.h>
Arvid Brodin6d50c602011-08-21 08:29:26 +020026#include <linux/timer.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020027#include <asm/unaligned.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000028#include <asm/cacheflush.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020029
Laurent Pinchart51714462015-01-21 00:55:59 +020030#include "isp1760-core.h"
Sebastian Siewiordb11e472008-04-24 00:37:04 +020031#include "isp1760-hcd.h"
Laurent Pincharte19c99e2015-01-21 00:55:56 +020032#include "isp1760-regs.h"
Sebastian Siewiordb11e472008-04-24 00:37:04 +020033
34static struct kmem_cache *qtd_cachep;
35static struct kmem_cache *qh_cachep;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020036static struct kmem_cache *urb_listitem_cachep;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020037
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +020038typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
39 struct isp1760_qtd *qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020040
41static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
42{
Laurent Pinchartcdd36e82015-01-21 00:55:54 +020043 return *(struct isp1760_hcd **)hcd->hcd_priv;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020044}
Sebastian Siewiordb11e472008-04-24 00:37:04 +020045
Laurent Pincharte19c99e2015-01-21 00:55:56 +020046/* urb state*/
47#define DELETE_URB (0x0008)
48#define NO_TRANSFER_ACTIVE (0xffffffff)
Sebastian Siewiordb11e472008-04-24 00:37:04 +020049
Laurent Pincharte19c99e2015-01-21 00:55:56 +020050/* Philips Proprietary Transfer Descriptor (PTD) */
51typedef __u32 __bitwise __dw;
52struct ptd {
53 __dw dw0;
54 __dw dw1;
55 __dw dw2;
56 __dw dw3;
57 __dw dw4;
58 __dw dw5;
59 __dw dw6;
60 __dw dw7;
61};
62#define PTD_OFFSET 0x0400
63#define ISO_PTD_OFFSET 0x0400
64#define INT_PTD_OFFSET 0x0800
65#define ATL_PTD_OFFSET 0x0c00
66#define PAYLOAD_OFFSET 0x1000
Sebastian Siewiordb11e472008-04-24 00:37:04 +020067
Laurent Pincharte19c99e2015-01-21 00:55:56 +020068
69/* ATL */
70/* DW0 */
71#define DW0_VALID_BIT 1
72#define FROM_DW0_VALID(x) ((x) & 0x01)
73#define TO_DW0_LENGTH(x) (((u32) x) << 3)
74#define TO_DW0_MAXPACKET(x) (((u32) x) << 18)
75#define TO_DW0_MULTI(x) (((u32) x) << 29)
76#define TO_DW0_ENDPOINT(x) (((u32) x) << 31)
77/* DW1 */
78#define TO_DW1_DEVICE_ADDR(x) (((u32) x) << 3)
79#define TO_DW1_PID_TOKEN(x) (((u32) x) << 10)
80#define DW1_TRANS_BULK ((u32) 2 << 12)
81#define DW1_TRANS_INT ((u32) 3 << 12)
82#define DW1_TRANS_SPLIT ((u32) 1 << 14)
83#define DW1_SE_USB_LOSPEED ((u32) 2 << 16)
84#define TO_DW1_PORT_NUM(x) (((u32) x) << 18)
85#define TO_DW1_HUB_NUM(x) (((u32) x) << 25)
86/* DW2 */
87#define TO_DW2_DATA_START_ADDR(x) (((u32) x) << 8)
88#define TO_DW2_RL(x) ((x) << 25)
89#define FROM_DW2_RL(x) (((x) >> 25) & 0xf)
90/* DW3 */
91#define FROM_DW3_NRBYTESTRANSFERRED(x) ((x) & 0x7fff)
92#define FROM_DW3_SCS_NRBYTESTRANSFERRED(x) ((x) & 0x07ff)
93#define TO_DW3_NAKCOUNT(x) ((x) << 19)
94#define FROM_DW3_NAKCOUNT(x) (((x) >> 19) & 0xf)
95#define TO_DW3_CERR(x) ((x) << 23)
96#define FROM_DW3_CERR(x) (((x) >> 23) & 0x3)
97#define TO_DW3_DATA_TOGGLE(x) ((x) << 25)
98#define FROM_DW3_DATA_TOGGLE(x) (((x) >> 25) & 0x1)
99#define TO_DW3_PING(x) ((x) << 26)
100#define FROM_DW3_PING(x) (((x) >> 26) & 0x1)
101#define DW3_ERROR_BIT (1 << 28)
102#define DW3_BABBLE_BIT (1 << 29)
103#define DW3_HALT_BIT (1 << 30)
104#define DW3_ACTIVE_BIT (1 << 31)
105#define FROM_DW3_ACTIVE(x) (((x) >> 31) & 0x01)
106
107#define INT_UNDERRUN (1 << 2)
108#define INT_BABBLE (1 << 1)
109#define INT_EXACT (1 << 0)
110
111#define SETUP_PID (2)
112#define IN_PID (1)
113#define OUT_PID (0)
114
115/* Errata 1 */
116#define RL_COUNTER (0)
117#define NAK_COUNTER (0)
118#define ERR_COUNTER (2)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200119
120struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200121 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200122 void *data_buffer;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100123 u32 payload_addr;
124
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200125 /* the rest is HCD-private */
126 struct list_head qtd_list;
127 struct urb *urb;
128 size_t length;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200129 size_t actual_length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200130
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200131 /* QTD_ENQUEUED: waiting for transfer (inactive) */
132 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
133 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
134 interrupt handler may touch this qtd! */
135 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
136 /* QTD_RETIRE: transfer error/abort qtd */
137#define QTD_ENQUEUED 0
138#define QTD_PAYLOAD_ALLOC 1
139#define QTD_XFER_STARTED 2
140#define QTD_XFER_COMPLETE 3
141#define QTD_RETIRE 4
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200142 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200143};
144
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200145/* Queue head, one for each active endpoint */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200146struct isp1760_qh {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200147 struct list_head qh_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200148 struct list_head qtd_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200149 u32 toggle;
150 u32 ping;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200151 int slot;
Arvid Brodin74ad6022011-08-23 01:25:30 +0200152 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200153};
154
155struct urb_listitem {
156 struct list_head urb_list;
157 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200158};
159
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100160/*
161 * Access functions for isp176x registers (addresses 0..0x03FF).
162 */
163static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200164{
Laurent Pinchart51714462015-01-21 00:55:59 +0200165 return isp1760_read32(base, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200166}
167
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100168static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200169{
Laurent Pinchart51714462015-01-21 00:55:59 +0200170 isp1760_write32(base, reg, val);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200171}
172
173/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100174 * Access functions for isp176x memory (offset >= 0x0400).
175 *
176 * bank_reads8() reads memory locations prefetched by an earlier write to
177 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
178 * bank optimizations, you should use the more generic mem_reads8() below.
179 *
180 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
181 * below.
182 *
183 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200184 * doesn't quite work because some people have to enforce 32-bit access
185 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100186static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
187 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200188{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100189 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200190 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100191 __u8 *src_byteptr;
192 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200193
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100194 src = src_base + (bank_addr | src_offset);
195
196 if (src_offset < PAYLOAD_OFFSET) {
197 while (bytes >= 4) {
198 *dst = le32_to_cpu(__raw_readl(src));
199 bytes -= 4;
200 src++;
201 dst++;
202 }
203 } else {
204 while (bytes >= 4) {
205 *dst = __raw_readl(src);
206 bytes -= 4;
207 src++;
208 dst++;
209 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200210 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200211
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100212 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200213 return;
214
215 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
216 * allocated.
217 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100218 if (src_offset < PAYLOAD_OFFSET)
219 val = le32_to_cpu(__raw_readl(src));
220 else
221 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200222
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100223 dst_byteptr = (void *) dst;
224 src_byteptr = (void *) &val;
225 while (bytes > 0) {
226 *dst_byteptr = *src_byteptr;
227 dst_byteptr++;
228 src_byteptr++;
229 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200230 }
231}
232
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100233static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
234 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200235{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100236 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
237 ndelay(90);
238 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
239}
240
241static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
242 __u32 const *src, u32 bytes)
243{
244 __u32 __iomem *dst;
245
246 dst = dst_base + dst_offset;
247
248 if (dst_offset < PAYLOAD_OFFSET) {
249 while (bytes >= 4) {
250 __raw_writel(cpu_to_le32(*src), dst);
251 bytes -= 4;
252 src++;
253 dst++;
254 }
255 } else {
256 while (bytes >= 4) {
257 __raw_writel(*src, dst);
258 bytes -= 4;
259 src++;
260 dst++;
261 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200262 }
263
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100264 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200265 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100266 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
267 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200268 */
269
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100270 if (dst_offset < PAYLOAD_OFFSET)
271 __raw_writel(cpu_to_le32(*src), dst);
272 else
273 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200274}
275
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100276/*
277 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
278 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
279 */
280static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
281 struct ptd *ptd)
282{
283 reg_write32(base, HC_MEMORY_REG,
284 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
285 ndelay(90);
286 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
287 (void *) ptd, sizeof(*ptd));
288}
289
290static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
291 struct ptd *ptd)
292{
293 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
294 &ptd->dw1, 7*sizeof(ptd->dw1));
295 /* Make sure dw0 gets written last (after other dw's and after payload)
296 since it contains the enable bit */
297 wmb();
298 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
299 sizeof(ptd->dw0));
300}
301
302
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200303/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
304static void init_memory(struct isp1760_hcd *priv)
305{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100306 int i, curr;
307 u32 payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200308
Arvid Brodina041d8e2011-02-26 22:04:40 +0100309 payload_addr = PAYLOAD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200310 for (i = 0; i < BLOCK_1_NUM; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100311 priv->memory_pool[i].start = payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200312 priv->memory_pool[i].size = BLOCK_1_SIZE;
313 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100314 payload_addr += priv->memory_pool[i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200315 }
316
Arvid Brodina041d8e2011-02-26 22:04:40 +0100317 curr = i;
318 for (i = 0; i < BLOCK_2_NUM; i++) {
319 priv->memory_pool[curr + i].start = payload_addr;
320 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
321 priv->memory_pool[curr + i].free = 1;
322 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200323 }
324
Arvid Brodina041d8e2011-02-26 22:04:40 +0100325 curr = i;
326 for (i = 0; i < BLOCK_3_NUM; i++) {
327 priv->memory_pool[curr + i].start = payload_addr;
328 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
329 priv->memory_pool[curr + i].free = 1;
330 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200331 }
332
Arvid Brodin34537732011-04-26 21:47:37 +0200333 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200334}
335
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100336static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200337{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100338 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200339 int i;
340
Arvid Brodin34537732011-04-26 21:47:37 +0200341 WARN_ON(qtd->payload_addr);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100342
343 if (!qtd->length)
344 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200345
346 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100347 if (priv->memory_pool[i].size >= qtd->length &&
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200348 priv->memory_pool[i].free) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200349 priv->memory_pool[i].free = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100350 qtd->payload_addr = priv->memory_pool[i].start;
351 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200352 }
353 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200354}
355
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100356static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200357{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100358 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200359 int i;
360
Arvid Brodina041d8e2011-02-26 22:04:40 +0100361 if (!qtd->payload_addr)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200362 return;
363
364 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100365 if (priv->memory_pool[i].start == qtd->payload_addr) {
Arvid Brodin34537732011-04-26 21:47:37 +0200366 WARN_ON(priv->memory_pool[i].free);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200367 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100368 qtd->payload_addr = 0;
369 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200370 }
371 }
372
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100373 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
374 __func__, qtd->payload_addr);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200375 WARN_ON(1);
376 qtd->payload_addr = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200377}
378
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100379static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200380 u32 mask, u32 done, int usec)
381{
382 u32 result;
383
384 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100385 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200386 if (result == ~0)
387 return -ENODEV;
388 result &= mask;
389 if (result == done)
390 return 0;
391 udelay(1);
392 usec--;
393 } while (usec > 0);
394 return -ETIMEDOUT;
395}
396
397/* reset a non-running (STS_HALT == 1) controller */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100398static int ehci_reset(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200399{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100400 struct isp1760_hcd *priv = hcd_to_priv(hcd);
401
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100402 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200403
404 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100405 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200406 hcd->state = HC_STATE_HALT;
407 priv->next_statechange = jiffies;
Gustavo A. R. Silva992510f2017-07-09 21:00:07 -0500408
409 return handshake(hcd, HC_USBCMD, CMD_RESET, 0, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200410}
411
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200412static struct isp1760_qh *qh_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200413{
414 struct isp1760_qh *qh;
415
416 qh = kmem_cache_zalloc(qh_cachep, flags);
417 if (!qh)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200418 return NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200419
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200420 INIT_LIST_HEAD(&qh->qh_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200421 INIT_LIST_HEAD(&qh->qtd_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200422 qh->slot = -1;
423
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200424 return qh;
425}
426
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200427static void qh_free(struct isp1760_qh *qh)
428{
429 WARN_ON(!list_empty(&qh->qtd_list));
430 WARN_ON(qh->slot > -1);
431 kmem_cache_free(qh_cachep, qh);
432}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200433
434/* one-time init, only for memory state */
435static int priv_init(struct usb_hcd *hcd)
436{
437 struct isp1760_hcd *priv = hcd_to_priv(hcd);
438 u32 hcc_params;
Arvid Brodine08f6a272011-11-23 18:10:41 +0100439 int i;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200440
441 spin_lock_init(&priv->lock);
442
Arvid Brodine08f6a272011-11-23 18:10:41 +0100443 for (i = 0; i < QH_END; i++)
444 INIT_LIST_HEAD(&priv->qh_list[i]);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200445
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200446 /*
447 * hw default: 1K periodic list heads, one per frame.
448 * periodic_size can shrink by USBCMD update if hcc_params allows.
449 */
450 priv->periodic_size = DEFAULT_I_TDPS;
451
452 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100453 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200454 /* full frame cache */
455 if (HCC_ISOC_CACHE(hcc_params))
456 priv->i_thresh = 8;
457 else /* N microframes cached */
458 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
459
460 return 0;
461}
462
463static int isp1760_hc_setup(struct usb_hcd *hcd)
464{
465 struct isp1760_hcd *priv = hcd_to_priv(hcd);
466 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500467 u32 scratch, hwmode;
468
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100469 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500470 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100471 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
472 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200473 if (scratch != 0xdeadbabe) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100474 dev_err(hcd->self.controller, "Scratch test failed.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200475 return -ENODEV;
476 }
477
Laurent Pinchart51714462015-01-21 00:55:59 +0200478 /*
479 * The RESET_HC bit in the SW_RESET register is supposed to reset the
480 * host controller without touching the CPU interface registers, but at
481 * least on the ISP1761 it seems to behave as the RESET_ALL bit and
482 * reset the whole device. We thus can't use it here, so let's reset
483 * the host controller through the EHCI USB Command register. The device
484 * has been reset in core code anyway, so this shouldn't matter.
485 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200486 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
487 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
488 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
489 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200490
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100491 result = ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200492 if (result)
493 return result;
494
495 /* Step 11 passed */
496
Nate Case3faefc82008-06-17 11:11:38 -0500497 /* ATL reset */
Laurent Pinchart51714462015-01-21 00:55:59 +0200498 hwmode = reg_read32(hcd->regs, HC_HW_MODE_CTRL) & ~ALL_ATX_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100499 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500500 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100501 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500502
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100503 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200504
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100505 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200506
507 return priv_init(hcd);
508}
509
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200510static u32 base_to_chip(u32 base)
511{
512 return ((base - 0x400) >> 3);
513}
514
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100515static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
516{
517 struct urb *urb;
518
519 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
520 return 1;
521
522 urb = qtd->urb;
523 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
524 return (qtd->urb != urb);
525}
526
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200527/* magic numbers that can affect system performance */
528#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
529#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
530#define EHCI_TUNE_RL_TT 0
531#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
532#define EHCI_TUNE_MULT_TT 1
533#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
534
535static void create_ptd_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100536 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200537{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200538 u32 maxpacket;
539 u32 multi;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200540 u32 rl = RL_COUNTER;
541 u32 nak = NAK_COUNTER;
542
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100543 memset(ptd, 0, sizeof(*ptd));
544
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200545 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100546 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
547 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200548 multi = 1 + ((maxpacket >> 11) & 0x3);
549 maxpacket &= 0x7ff;
550
551 /* DW0 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200552 ptd->dw0 = DW0_VALID_BIT;
553 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
554 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
555 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200556
557 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100558 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200559 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
560 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200561
Arvid Brodina041d8e2011-02-26 22:04:40 +0100562 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200563 ptd->dw1 |= DW1_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100564 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200565 ptd->dw1 |= DW1_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200566
Arvid Brodina041d8e2011-02-26 22:04:40 +0100567 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200568 /* split transaction */
569
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200570 ptd->dw1 |= DW1_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100571 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200572 ptd->dw1 |= DW1_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200573
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200574 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
575 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200576
577 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100578 if (usb_pipeint(qtd->urb->pipe) &&
579 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100580 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200581
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200582 rl = 0;
583 nak = 0;
584 } else {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200585 ptd->dw0 |= TO_DW0_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100586 if (usb_pipecontrol(qtd->urb->pipe) ||
587 usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200588 ptd->dw3 |= TO_DW3_PING(qh->ping);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200589 }
590 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100591 ptd->dw2 = 0;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200592 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
593 ptd->dw2 |= TO_DW2_RL(rl);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200594
595 /* DW3 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200596 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
597 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100598 if (usb_pipecontrol(qtd->urb->pipe)) {
599 if (qtd->data_buffer == qtd->urb->setup_packet)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200600 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100601 else if (last_qtd_of_urb(qtd, qh))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200602 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100603 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200604
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200605 ptd->dw3 |= DW3_ACTIVE_BIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200606 /* Cerr */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200607 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200608}
609
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100610static void transform_add_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100611 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200612{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100613 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200614 u32 period;
615
Arvid Brodin65f1b522011-02-26 22:07:35 +0100616 /*
617 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
618 * the algorithm from the original Philips driver code, which was
619 * pretty much used in this driver before as well, is quite horrendous
620 * and, i believe, incorrect. The code below follows the datasheet and
621 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
622 * more reliable this way (fingers crossed...).
623 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200624
Arvid Brodin65f1b522011-02-26 22:07:35 +0100625 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
626 /* urb->interval is in units of microframes (1/8 ms) */
627 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200628
Arvid Brodin65f1b522011-02-26 22:07:35 +0100629 if (qtd->urb->interval > 4)
630 usof = 0x01; /* One bit set =>
631 interval 1 ms * uFrame-match */
632 else if (qtd->urb->interval > 2)
633 usof = 0x22; /* Two bits set => interval 1/2 ms */
634 else if (qtd->urb->interval > 1)
635 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200636 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100637 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200638 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100639 /* urb->interval is in units of frames (1 ms) */
640 period = qtd->urb->interval;
641 usof = 0x0f; /* Execute Start Split on any of the
642 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200643
Arvid Brodin65f1b522011-02-26 22:07:35 +0100644 /*
645 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
646 * complete split needs to be sent. Valid only for IN." Also,
647 * "All bits can be set to one for every transfer." (p 82,
648 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
649 * that number come from? 0xff seems to work fine...
650 */
651 /* ptd->dw5 = 0x1c; */
652 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200653 }
654
Arvid Brodin65f1b522011-02-26 22:07:35 +0100655 period = period >> 1;/* Ensure equal or shorter period than requested */
656 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
657
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100658 ptd->dw2 |= period;
659 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200660}
661
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200662static void create_ptd_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100663 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200664{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200665 create_ptd_atl(qh, qtd, ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100666 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200667}
668
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100669static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200670__releases(priv->lock)
671__acquires(priv->lock)
672{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100673 struct isp1760_hcd *priv = hcd_to_priv(hcd);
674
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200675 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100676 if (urb->status == -EINPROGRESS)
677 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200678 }
679
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000680 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
681 void *ptr;
682 for (ptr = urb->transfer_buffer;
683 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
684 ptr += PAGE_SIZE)
685 flush_dcache_page(virt_to_page(ptr));
686 }
687
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200688 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100689 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200690 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100691 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200692 spin_lock(&priv->lock);
693}
694
Arvid Brodin34537732011-04-26 21:47:37 +0200695static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
696 u8 packet_type)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200697{
Arvid Brodin34537732011-04-26 21:47:37 +0200698 struct isp1760_qtd *qtd;
699
700 qtd = kmem_cache_zalloc(qtd_cachep, flags);
701 if (!qtd)
702 return NULL;
703
704 INIT_LIST_HEAD(&qtd->qtd_list);
705 qtd->urb = urb;
706 qtd->packet_type = packet_type;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200707 qtd->status = QTD_ENQUEUED;
708 qtd->actual_length = 0;
Arvid Brodin34537732011-04-26 21:47:37 +0200709
710 return qtd;
711}
712
713static void qtd_free(struct isp1760_qtd *qtd)
714{
715 WARN_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200716 kmem_cache_free(qtd_cachep, qtd);
717}
718
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200719static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +0200720 struct isp1760_slotinfo *slots,
721 struct isp1760_qtd *qtd, struct isp1760_qh *qh,
722 struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200723{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100724 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200725 int skip_map;
726
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200727 WARN_ON((slot < 0) || (slot > 31));
728 WARN_ON(qtd->length && !qtd->payload_addr);
729 WARN_ON(slots[slot].qtd);
730 WARN_ON(slots[slot].qh);
731 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200732
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200733 /* Make sure done map has not triggered from some unlinked transfer */
734 if (ptd_offset == ATL_PTD_OFFSET) {
735 priv->atl_done_map |= reg_read32(hcd->regs,
736 HC_ATL_PTD_DONEMAP_REG);
Arvid Brodin6477acc2011-08-21 08:29:28 +0200737 priv->atl_done_map &= ~(1 << slot);
738 } else {
739 priv->int_done_map |= reg_read32(hcd->regs,
740 HC_INT_PTD_DONEMAP_REG);
741 priv->int_done_map &= ~(1 << slot);
742 }
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200743
Arvid Brodin6477acc2011-08-21 08:29:28 +0200744 qh->slot = slot;
745 qtd->status = QTD_XFER_STARTED;
746 slots[slot].timestamp = jiffies;
747 slots[slot].qtd = qtd;
748 slots[slot].qh = qh;
749 ptd_write(hcd->regs, ptd_offset, slot, ptd);
750
751 if (ptd_offset == ATL_PTD_OFFSET) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200752 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
753 skip_map &= ~(1 << qh->slot);
754 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
755 } else {
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200756 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
757 skip_map &= ~(1 << qh->slot);
758 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
759 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200760}
761
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200762static int is_short_bulk(struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200763{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200764 return (usb_pipebulk(qtd->urb->pipe) &&
765 (qtd->actual_length < qtd->length));
766}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200767
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200768static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
769 struct list_head *urb_list)
770{
771 int last_qtd;
772 struct isp1760_qtd *qtd, *qtd_next;
773 struct urb_listitem *urb_listitem;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200774
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200775 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
776 if (qtd->status < QTD_XFER_COMPLETE)
777 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200778
Arvid Brodin38679b72011-08-21 08:29:27 +0200779 last_qtd = last_qtd_of_urb(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200780
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200781 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
782 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200783
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200784 if (qtd->status == QTD_XFER_COMPLETE) {
785 if (qtd->actual_length) {
786 switch (qtd->packet_type) {
787 case IN_PID:
788 mem_reads8(hcd->regs, qtd->payload_addr,
789 qtd->data_buffer,
790 qtd->actual_length);
791 /* Fall through (?) */
792 case OUT_PID:
793 qtd->urb->actual_length +=
794 qtd->actual_length;
795 /* Fall through ... */
796 case SETUP_PID:
797 break;
798 }
799 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200800
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200801 if (is_short_bulk(qtd)) {
802 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
803 qtd->urb->status = -EREMOTEIO;
804 if (!last_qtd)
805 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200806 }
807 }
808
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200809 if (qtd->payload_addr)
810 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200811
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200812 if (last_qtd) {
813 if ((qtd->status == QTD_RETIRE) &&
814 (qtd->urb->status == -EINPROGRESS))
815 qtd->urb->status = -EPIPE;
816 /* Defer calling of urb_done() since it releases lock */
817 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
818 GFP_ATOMIC);
819 if (unlikely(!urb_listitem))
Arvid Brodin38679b72011-08-21 08:29:27 +0200820 break; /* Try again on next call */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200821 urb_listitem->urb = qtd->urb;
822 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200823 }
824
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200825 list_del(&qtd->qtd_list);
826 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200827 }
828}
829
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200830#define ENQUEUE_DEPTH 2
831static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
832{
833 struct isp1760_hcd *priv = hcd_to_priv(hcd);
834 int ptd_offset;
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +0200835 struct isp1760_slotinfo *slots;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200836 int curr_slot, free_slot;
837 int n;
838 struct ptd ptd;
839 struct isp1760_qtd *qtd;
840
841 if (unlikely(list_empty(&qh->qtd_list))) {
842 WARN_ON(1);
843 return;
844 }
845
Arvid Brodin74ad6022011-08-23 01:25:30 +0200846 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
847 if (qh->tt_buffer_dirty)
848 return;
849
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200850 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
851 qtd_list)->urb->pipe)) {
852 ptd_offset = INT_PTD_OFFSET;
853 slots = priv->int_slots;
854 } else {
855 ptd_offset = ATL_PTD_OFFSET;
856 slots = priv->atl_slots;
857 }
858
859 free_slot = -1;
860 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
861 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
862 free_slot = curr_slot;
863 if (slots[curr_slot].qh == qh)
864 break;
865 }
866
867 n = 0;
868 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
869 if (qtd->status == QTD_ENQUEUED) {
870 WARN_ON(qtd->payload_addr);
871 alloc_mem(hcd, qtd);
872 if ((qtd->length) && (!qtd->payload_addr))
873 break;
874
875 if ((qtd->length) &&
876 ((qtd->packet_type == SETUP_PID) ||
877 (qtd->packet_type == OUT_PID))) {
878 mem_writes8(hcd->regs, qtd->payload_addr,
879 qtd->data_buffer, qtd->length);
880 }
881
882 qtd->status = QTD_PAYLOAD_ALLOC;
883 }
884
885 if (qtd->status == QTD_PAYLOAD_ALLOC) {
886/*
887 if ((curr_slot > 31) && (free_slot == -1))
888 dev_dbg(hcd->self.controller, "%s: No slot "
889 "available for transfer\n", __func__);
890*/
891 /* Start xfer for this endpoint if not already done */
892 if ((curr_slot > 31) && (free_slot > -1)) {
893 if (usb_pipeint(qtd->urb->pipe))
894 create_ptd_int(qh, qtd, &ptd);
895 else
896 create_ptd_atl(qh, qtd, &ptd);
897
898 start_bus_transfer(hcd, ptd_offset, free_slot,
899 slots, qtd, qh, &ptd);
900 curr_slot = free_slot;
901 }
902
903 n++;
904 if (n >= ENQUEUE_DEPTH)
905 break;
906 }
907 }
908}
909
Arvid Brodinde9c6302013-01-27 16:41:26 +0100910static void schedule_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200911{
912 struct isp1760_hcd *priv;
913 struct isp1760_qh *qh, *qh_next;
914 struct list_head *ep_queue;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200915 LIST_HEAD(urb_list);
916 struct urb_listitem *urb_listitem, *urb_listitem_next;
Arvid Brodine08f6a272011-11-23 18:10:41 +0100917 int i;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200918
919 if (!hcd) {
920 WARN_ON(1);
921 return;
922 }
923
924 priv = hcd_to_priv(hcd);
925
926 /*
927 * check finished/retired xfers, transfer payloads, call urb_done()
928 */
Arvid Brodine08f6a272011-11-23 18:10:41 +0100929 for (i = 0; i < QH_END; i++) {
930 ep_queue = &priv->qh_list[i];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200931 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200932 collect_qtds(hcd, qh, &urb_list);
Arvid Brodinc64391f2011-11-23 18:13:02 +0100933 if (list_empty(&qh->qtd_list))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200934 list_del(&qh->qh_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200935 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200936 }
937
938 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
939 urb_list) {
940 isp1760_urb_done(hcd, urb_listitem->urb);
941 kmem_cache_free(urb_listitem_cachep, urb_listitem);
942 }
943
944 /*
945 * Schedule packets for transfer.
946 *
947 * According to USB2.0 specification:
948 *
949 * 1st prio: interrupt xfers, up to 80 % of bandwidth
950 * 2nd prio: control xfers
951 * 3rd prio: bulk xfers
952 *
953 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
954 * is very unclear on how to prioritize traffic):
955 *
956 * 1) Enqueue any queued control transfers, as long as payload chip mem
957 * and PTD ATL slots are available.
958 * 2) Enqueue any queued INT transfers, as long as payload chip mem
959 * and PTD INT slots are available.
960 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
961 * and PTD ATL slots are available.
962 *
963 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
964 * conservation of chip mem and performance.
965 *
966 * I'm sure this scheme could be improved upon!
967 */
Arvid Brodine08f6a272011-11-23 18:10:41 +0100968 for (i = 0; i < QH_END; i++) {
969 ep_queue = &priv->qh_list[i];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200970 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
971 enqueue_qtds(hcd, qh);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200972 }
973}
974
975#define PTD_STATE_QTD_DONE 1
976#define PTD_STATE_QTD_RELOAD 2
977#define PTD_STATE_URB_RETIRE 3
978
979static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
980 struct urb *urb)
981{
982 __dw dw4;
983 int i;
984
985 dw4 = ptd->dw4;
986 dw4 >>= 8;
987
988 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
989 need to handle these errors? Is it done in hardware? */
990
991 if (ptd->dw3 & DW3_HALT_BIT) {
992
993 urb->status = -EPROTO; /* Default unknown error */
994
995 for (i = 0; i < 8; i++) {
996 switch (dw4 & 0x7) {
997 case INT_UNDERRUN:
998 dev_dbg(hcd->self.controller, "%s: underrun "
999 "during uFrame %d\n",
1000 __func__, i);
1001 urb->status = -ECOMM; /* Could not write data */
1002 break;
1003 case INT_EXACT:
1004 dev_dbg(hcd->self.controller, "%s: transaction "
1005 "error during uFrame %d\n",
1006 __func__, i);
1007 urb->status = -EPROTO; /* timeout, bad CRC, PID
1008 error etc. */
1009 break;
1010 case INT_BABBLE:
1011 dev_dbg(hcd->self.controller, "%s: babble "
1012 "error during uFrame %d\n",
1013 __func__, i);
1014 urb->status = -EOVERFLOW;
1015 break;
1016 }
1017 dw4 >>= 3;
1018 }
1019
1020 return PTD_STATE_URB_RETIRE;
1021 }
1022
1023 return PTD_STATE_QTD_DONE;
1024}
1025
1026static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1027 struct urb *urb)
1028{
1029 WARN_ON(!ptd);
1030 if (ptd->dw3 & DW3_HALT_BIT) {
1031 if (ptd->dw3 & DW3_BABBLE_BIT)
1032 urb->status = -EOVERFLOW;
1033 else if (FROM_DW3_CERR(ptd->dw3))
1034 urb->status = -EPIPE; /* Stall */
1035 else if (ptd->dw3 & DW3_ERROR_BIT)
1036 urb->status = -EPROTO; /* XactErr */
1037 else
1038 urb->status = -EPROTO; /* Unknown */
1039/*
1040 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1041 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1042 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1043 __func__,
1044 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1045 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1046*/
1047 return PTD_STATE_URB_RETIRE;
1048 }
1049
1050 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1051 /* Transfer Error, *but* active and no HALT -> reload */
1052 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1053 return PTD_STATE_QTD_RELOAD;
1054 }
1055
1056 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1057 /*
1058 * NAKs are handled in HW by the chip. Usually if the
1059 * device is not able to send data fast enough.
1060 * This happens mostly on slower hardware.
1061 */
1062 return PTD_STATE_QTD_RELOAD;
1063 }
1064
1065 return PTD_STATE_QTD_DONE;
1066}
1067
Arvid Brodin6d50c602011-08-21 08:29:26 +02001068static void handle_done_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001069{
1070 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001071 struct ptd ptd;
1072 struct isp1760_qh *qh;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001073 int slot;
1074 int state;
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +02001075 struct isp1760_slotinfo *slots;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001076 u32 ptd_offset;
1077 struct isp1760_qtd *qtd;
1078 int modified;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001079 int skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001080
Arvid Brodin6d50c602011-08-21 08:29:26 +02001081 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1082 priv->int_done_map &= ~skip_map;
1083 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1084 priv->atl_done_map &= ~skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001085
Arvid Brodin6d50c602011-08-21 08:29:26 +02001086 modified = priv->int_done_map || priv->atl_done_map;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001087
1088 while (priv->int_done_map || priv->atl_done_map) {
1089 if (priv->int_done_map) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001090 /* INT ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001091 slot = __ffs(priv->int_done_map);
1092 priv->int_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001093 slots = priv->int_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001094 /* This should not trigger, and could be removed if
1095 noone have any problems with it triggering: */
1096 if (!slots[slot].qh) {
1097 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001098 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001099 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001100 ptd_offset = INT_PTD_OFFSET;
1101 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1102 state = check_int_transfer(hcd, &ptd,
1103 slots[slot].qtd->urb);
1104 } else {
1105 /* ATL ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001106 slot = __ffs(priv->atl_done_map);
1107 priv->atl_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001108 slots = priv->atl_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001109 /* This should not trigger, and could be removed if
1110 noone have any problems with it triggering: */
1111 if (!slots[slot].qh) {
1112 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001113 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001114 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001115 ptd_offset = ATL_PTD_OFFSET;
1116 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1117 state = check_atl_transfer(hcd, &ptd,
1118 slots[slot].qtd->urb);
1119 }
1120
1121 qtd = slots[slot].qtd;
1122 slots[slot].qtd = NULL;
1123 qh = slots[slot].qh;
1124 slots[slot].qh = NULL;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001125 qh->slot = -1;
1126
1127 WARN_ON(qtd->status != QTD_XFER_STARTED);
1128
1129 switch (state) {
1130 case PTD_STATE_QTD_DONE:
1131 if ((usb_pipeint(qtd->urb->pipe)) &&
1132 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1133 qtd->actual_length =
1134 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1135 else
1136 qtd->actual_length =
1137 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1138
1139 qtd->status = QTD_XFER_COMPLETE;
1140 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1141 is_short_bulk(qtd))
1142 qtd = NULL;
1143 else
1144 qtd = list_entry(qtd->qtd_list.next,
1145 typeof(*qtd), qtd_list);
1146
1147 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1148 qh->ping = FROM_DW3_PING(ptd.dw3);
1149 break;
1150
1151 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1152 qtd->status = QTD_PAYLOAD_ALLOC;
1153 ptd.dw0 |= DW0_VALID_BIT;
1154 /* RL counter = ERR counter */
1155 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1156 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1157 ptd.dw3 &= ~TO_DW3_CERR(3);
1158 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1159 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1160 qh->ping = FROM_DW3_PING(ptd.dw3);
1161 break;
1162
1163 case PTD_STATE_URB_RETIRE:
1164 qtd->status = QTD_RETIRE;
Arvid Brodin74ad6022011-08-23 01:25:30 +02001165 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1166 (qtd->urb->status != -EPIPE) &&
1167 (qtd->urb->status != -EREMOTEIO)) {
1168 qh->tt_buffer_dirty = 1;
1169 if (usb_hub_clear_tt_buffer(qtd->urb))
1170 /* Clear failed; let's hope things work
1171 anyway */
1172 qh->tt_buffer_dirty = 0;
1173 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001174 qtd = NULL;
1175 qh->toggle = 0;
1176 qh->ping = 0;
1177 break;
1178
1179 default:
1180 WARN_ON(1);
1181 continue;
1182 }
1183
1184 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1185 if (slots == priv->int_slots) {
1186 if (state == PTD_STATE_QTD_RELOAD)
1187 dev_err(hcd->self.controller,
1188 "%s: PTD_STATE_QTD_RELOAD on "
1189 "interrupt packet\n", __func__);
1190 if (state != PTD_STATE_QTD_RELOAD)
1191 create_ptd_int(qh, qtd, &ptd);
1192 } else {
1193 if (state != PTD_STATE_QTD_RELOAD)
1194 create_ptd_atl(qh, qtd, &ptd);
1195 }
1196
1197 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1198 qh, &ptd);
1199 }
1200 }
1201
1202 if (modified)
1203 schedule_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001204}
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001205
Arvid Brodin6d50c602011-08-21 08:29:26 +02001206static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1207{
1208 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1209 u32 imask;
1210 irqreturn_t irqret = IRQ_NONE;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001211
Arvid Brodin6d50c602011-08-21 08:29:26 +02001212 spin_lock(&priv->lock);
1213
1214 if (!(hcd->state & HC_STATE_RUNNING))
1215 goto leave;
1216
1217 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1218 if (unlikely(!imask))
1219 goto leave;
1220 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1221
1222 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1223 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1224
1225 handle_done_ptds(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001226
1227 irqret = IRQ_HANDLED;
1228leave:
1229 spin_unlock(&priv->lock);
1230
1231 return irqret;
1232}
1233
Arvid Brodin6d50c602011-08-21 08:29:26 +02001234/*
1235 * Workaround for problem described in chip errata 2:
1236 *
1237 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1238 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1239 * ATL done interrupts (the "instead of" might be important since it seems
1240 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1241 * to set the PTD's done bit in addition to not generating an interrupt!).
1242 *
1243 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1244 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1245 *
1246 * If we use SOF interrupts only, we get latency between ptd completion and the
1247 * actual handling. This is very noticeable in testusb runs which takes several
1248 * minutes longer without ATL interrupts.
1249 *
1250 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1251 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1252 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1253 * completed and its done map bit is set.
1254 *
1255 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1256 * not to cause too much lag when this HW bug occurs, while still hopefully
1257 * ensuring that the check does not falsely trigger.
1258 */
Arvid Brodin6477acc2011-08-21 08:29:28 +02001259#define SLOT_TIMEOUT 300
Arvid Brodin6d50c602011-08-21 08:29:26 +02001260#define SLOT_CHECK_PERIOD 200
1261static struct timer_list errata2_timer;
Kees Cook7e33da592017-10-24 03:08:09 -07001262static struct usb_hcd *errata2_timer_hcd;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001263
Kees Cook7e33da592017-10-24 03:08:09 -07001264static void errata2_function(struct timer_list *unused)
Arvid Brodin6d50c602011-08-21 08:29:26 +02001265{
Kees Cook7e33da592017-10-24 03:08:09 -07001266 struct usb_hcd *hcd = errata2_timer_hcd;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001267 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1268 int slot;
1269 struct ptd ptd;
1270 unsigned long spinflags;
1271
1272 spin_lock_irqsave(&priv->lock, spinflags);
1273
1274 for (slot = 0; slot < 32; slot++)
Arvid Brodin6477acc2011-08-21 08:29:28 +02001275 if (priv->atl_slots[slot].qh && time_after(jiffies,
1276 priv->atl_slots[slot].timestamp +
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001277 msecs_to_jiffies(SLOT_TIMEOUT))) {
Arvid Brodin6d50c602011-08-21 08:29:26 +02001278 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1279 if (!FROM_DW0_VALID(ptd.dw0) &&
1280 !FROM_DW3_ACTIVE(ptd.dw3))
1281 priv->atl_done_map |= 1 << slot;
1282 }
1283
Arvid Brodin6477acc2011-08-21 08:29:28 +02001284 if (priv->atl_done_map)
1285 handle_done_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001286
1287 spin_unlock_irqrestore(&priv->lock, spinflags);
1288
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001289 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001290 add_timer(&errata2_timer);
1291}
1292
Arvid Brodin0ba79052011-08-21 08:29:25 +02001293static int isp1760_run(struct usb_hcd *hcd)
1294{
1295 int retval;
1296 u32 temp;
1297 u32 command;
1298 u32 chipid;
1299
1300 hcd->uses_new_polling = 1;
1301
1302 hcd->state = HC_STATE_RUNNING;
1303
1304 /* Set PTD interrupt AND & OR maps */
1305 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1306 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1307 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1308 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1309 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1310 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1311 /* step 23 passed */
1312
1313 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1314 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1315
1316 command = reg_read32(hcd->regs, HC_USBCMD);
1317 command &= ~(CMD_LRESET|CMD_RESET);
1318 command |= CMD_RUN;
1319 reg_write32(hcd->regs, HC_USBCMD, command);
1320
1321 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1322 if (retval)
1323 return retval;
1324
1325 /*
1326 * XXX
1327 * Spec says to write FLAG_CF as last config action, priv code grabs
1328 * the semaphore while doing so.
1329 */
1330 down_write(&ehci_cf_port_reset_rwsem);
1331 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1332
1333 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1334 up_write(&ehci_cf_port_reset_rwsem);
1335 if (retval)
1336 return retval;
1337
Kees Cook7e33da592017-10-24 03:08:09 -07001338 errata2_timer_hcd = hcd;
1339 timer_setup(&errata2_timer, errata2_function, 0);
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001340 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001341 add_timer(&errata2_timer);
1342
Arvid Brodin0ba79052011-08-21 08:29:25 +02001343 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1344 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1345 chipid & 0xffff, chipid >> 16);
1346
1347 /* PTD Register Init Part 2, Step 28 */
1348
1349 /* Setup registers controlling PTD checking */
1350 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1351 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1352 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1353 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1354 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1355 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1356 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1357 ATL_BUF_FILL | INT_BUF_FILL);
1358
1359 /* GRR this is run-once init(), being done every time the HC starts.
1360 * So long as they're part of class devices, we can't do it init()
1361 * since the class device isn't created that early.
1362 */
1363 return 0;
1364}
1365
Arvid Brodin34537732011-04-26 21:47:37 +02001366static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001367{
Arvid Brodin34537732011-04-26 21:47:37 +02001368 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001369
Arvid Brodin34537732011-04-26 21:47:37 +02001370 if (len > MAX_PAYLOAD_SIZE)
1371 len = MAX_PAYLOAD_SIZE;
1372 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001373
Arvid Brodin34537732011-04-26 21:47:37 +02001374 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001375}
1376
Arvid Brodin34537732011-04-26 21:47:37 +02001377static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001378{
Arvid Brodin34537732011-04-26 21:47:37 +02001379 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001380
Arvid Brodin34537732011-04-26 21:47:37 +02001381 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001382 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001383 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001384 }
1385}
1386
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001387/*
Arvid Brodin34537732011-04-26 21:47:37 +02001388 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1389 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001390 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001391#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001392static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001393 struct urb *urb, struct list_head *head, gfp_t flags)
1394{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001395 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001396 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001397 int len, maxpacketsize;
1398 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001399
1400 /*
1401 * URBs map to sequences of QTDs: one logical transaction
1402 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001403
Arvid Brodin34537732011-04-26 21:47:37 +02001404 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1405 /* XXX This looks like usb storage / SCSI bug */
1406 dev_err(hcd->self.controller,
1407 "buf is null, dma is %08lx len is %d\n",
1408 (long unsigned)urb->transfer_dma,
1409 urb->transfer_buffer_length);
1410 WARN_ON(1);
1411 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001412
Arvid Brodin34537732011-04-26 21:47:37 +02001413 if (usb_pipein(urb->pipe))
1414 packet_type = IN_PID;
1415 else
1416 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001417
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001418 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001419 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001420 if (!qtd)
1421 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001422 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001423 list_add_tail(&qtd->qtd_list, head);
1424
1425 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001426 if (urb->transfer_buffer_length == 0)
1427 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001428 }
1429
Arvid Brodin34537732011-04-26 21:47:37 +02001430 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1431 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001432
1433 /*
1434 * buffer gets wrapped in one or more qtds;
1435 * last one may be "short" (including zero len)
1436 * and may serve as a control status ack
1437 */
Arvid Brodin34537732011-04-26 21:47:37 +02001438 buf = urb->transfer_buffer;
1439 len = urb->transfer_buffer_length;
1440
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001441 for (;;) {
1442 int this_qtd_len;
1443
Arvid Brodin34537732011-04-26 21:47:37 +02001444 qtd = qtd_alloc(flags, urb, packet_type);
1445 if (!qtd)
1446 goto cleanup;
1447 this_qtd_len = qtd_fill(qtd, buf, len);
1448 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001449
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001450 len -= this_qtd_len;
1451 buf += this_qtd_len;
1452
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001453 if (len <= 0)
1454 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001455 }
1456
1457 /*
1458 * control requests may need a terminating data "status" ack;
1459 * bulk ones may need a terminating short packet (zero length).
1460 */
1461 if (urb->transfer_buffer_length != 0) {
1462 int one_more = 0;
1463
1464 if (usb_pipecontrol(urb->pipe)) {
1465 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001466 if (packet_type == IN_PID)
1467 packet_type = OUT_PID;
1468 else
1469 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001470 } else if (usb_pipebulk(urb->pipe)
1471 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001472 && !(urb->transfer_buffer_length %
1473 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001474 one_more = 1;
1475 }
1476 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001477 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001478 if (!qtd)
1479 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001480
1481 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001482 qtd_fill(qtd, NULL, 0);
1483 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001484 }
1485 }
1486
Arvid Brodin34537732011-04-26 21:47:37 +02001487 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001488
1489cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001490 qtd_list_free(head);
1491}
1492
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001493static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1494 gfp_t mem_flags)
1495{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001496 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1497 struct list_head *ep_queue;
1498 struct isp1760_qh *qh, *qhit;
1499 unsigned long spinflags;
1500 LIST_HEAD(new_qtds);
1501 int retval;
1502 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001503
1504 switch (usb_pipetype(urb->pipe)) {
1505 case PIPE_CONTROL:
Arvid Brodine08f6a272011-11-23 18:10:41 +01001506 ep_queue = &priv->qh_list[QH_CONTROL];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001507 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001508 case PIPE_BULK:
Arvid Brodine08f6a272011-11-23 18:10:41 +01001509 ep_queue = &priv->qh_list[QH_BULK];
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001510 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001511 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001512 if (urb->interval < 0)
1513 return -EINVAL;
1514 /* FIXME: Check bandwidth */
Arvid Brodine08f6a272011-11-23 18:10:41 +01001515 ep_queue = &priv->qh_list[QH_INTERRUPT];
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001516 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001517 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001518 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1519 "not yet supported\n",
1520 __func__);
1521 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001522 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001523 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1524 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001525 return -EPIPE;
1526 }
1527
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001528 if (usb_pipein(urb->pipe))
1529 urb->actual_length = 0;
1530
1531 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1532 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001533 return -ENOMEM;
1534
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001535 retval = 0;
1536 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001537
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001538 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1539 retval = -ESHUTDOWN;
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001540 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001541 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001542 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001543 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001544 if (retval) {
1545 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001546 goto out;
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001547 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001548
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001549 qh = urb->ep->hcpriv;
1550 if (qh) {
1551 qh_in_queue = 0;
1552 list_for_each_entry(qhit, ep_queue, qh_list) {
1553 if (qhit == qh) {
1554 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001555 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001556 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001557 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001558 if (!qh_in_queue)
1559 list_add_tail(&qh->qh_list, ep_queue);
1560 } else {
1561 qh = qh_alloc(GFP_ATOMIC);
1562 if (!qh) {
1563 retval = -ENOMEM;
Arvid Brodin38679b72011-08-21 08:29:27 +02001564 usb_hcd_unlink_urb_from_ep(hcd, urb);
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001565 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001566 goto out;
1567 }
1568 list_add_tail(&qh->qh_list, ep_queue);
1569 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001570 }
1571
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001572 list_splice_tail(&new_qtds, &qh->qtd_list);
1573 schedule_ptds(hcd);
1574
1575out:
1576 spin_unlock_irqrestore(&priv->lock, spinflags);
1577 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001578}
1579
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001580static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1581 struct isp1760_qh *qh)
1582{
1583 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1584 int skip_map;
1585
1586 WARN_ON(qh->slot == -1);
1587
1588 /* We need to forcefully reclaim the slot since some transfers never
1589 return, e.g. interrupt transfers and NAKed bulk transfers. */
Arvid Brodin8b1ab602011-06-17 18:45:37 +02001590 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001591 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1592 skip_map |= (1 << qh->slot);
1593 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1594 priv->atl_slots[qh->slot].qh = NULL;
1595 priv->atl_slots[qh->slot].qtd = NULL;
1596 } else {
1597 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1598 skip_map |= (1 << qh->slot);
1599 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1600 priv->int_slots[qh->slot].qh = NULL;
1601 priv->int_slots[qh->slot].qtd = NULL;
1602 }
1603
1604 qh->slot = -1;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001605}
1606
Arvid Brodin74ad6022011-08-23 01:25:30 +02001607/*
1608 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1609 * any active transfer belonging to the urb in the process.
1610 */
1611static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1612 struct isp1760_qtd *qtd)
1613{
1614 struct urb *urb;
1615 int urb_was_running;
1616
1617 urb = qtd->urb;
1618 urb_was_running = 0;
1619 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
1620 if (qtd->urb != urb)
1621 break;
1622
1623 if (qtd->status >= QTD_XFER_STARTED)
1624 urb_was_running = 1;
1625 if (last_qtd_of_urb(qtd, qh) &&
1626 (qtd->status >= QTD_XFER_COMPLETE))
1627 urb_was_running = 0;
1628
1629 if (qtd->status == QTD_XFER_STARTED)
1630 kill_transfer(hcd, urb, qh);
1631 qtd->status = QTD_RETIRE;
1632 }
1633
1634 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
1635 qh->tt_buffer_dirty = 1;
1636 if (usb_hub_clear_tt_buffer(urb))
1637 /* Clear failed; let's hope things work anyway */
1638 qh->tt_buffer_dirty = 0;
1639 }
1640}
1641
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001642static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1643 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001644{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001645 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001646 unsigned long spinflags;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001647 struct isp1760_qh *qh;
1648 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001649 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001650
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001651 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodin17d3e142011-07-20 03:13:46 +02001652 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1653 if (retval)
1654 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001655
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001656 qh = urb->ep->hcpriv;
1657 if (!qh) {
1658 retval = -EINVAL;
1659 goto out;
1660 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001661
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001662 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1663 if (qtd->urb == urb) {
Arvid Brodin74ad6022011-08-23 01:25:30 +02001664 dequeue_urb_from_qtd(hcd, qh, qtd);
Michael Grzeschik8cb22682012-04-18 09:58:23 +02001665 list_move(&qtd->qtd_list, &qh->qtd_list);
Arvid Brodin74ad6022011-08-23 01:25:30 +02001666 break;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001667 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001668
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001669 urb->status = status;
1670 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001671
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001672out:
1673 spin_unlock_irqrestore(&priv->lock, spinflags);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001674 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001675}
1676
Arvid Brodin079cdb02011-05-20 00:17:34 +02001677static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1678 struct usb_host_endpoint *ep)
1679{
1680 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001681 unsigned long spinflags;
Arvid Brodinc64391f2011-11-23 18:13:02 +01001682 struct isp1760_qh *qh, *qh_iter;
1683 int i;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001684
1685 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001686
Arvid Brodin079cdb02011-05-20 00:17:34 +02001687 qh = ep->hcpriv;
1688 if (!qh)
1689 goto out;
1690
Arvid Brodinc64391f2011-11-23 18:13:02 +01001691 WARN_ON(!list_empty(&qh->qtd_list));
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001692
Arvid Brodinc64391f2011-11-23 18:13:02 +01001693 for (i = 0; i < QH_END; i++)
1694 list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
1695 if (qh_iter == qh) {
1696 list_del(&qh_iter->qh_list);
1697 i = QH_END;
1698 break;
1699 }
1700 qh_free(qh);
Arvid Brodin079cdb02011-05-20 00:17:34 +02001701 ep->hcpriv = NULL;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001702
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001703 schedule_ptds(hcd);
1704
Arvid Brodin079cdb02011-05-20 00:17:34 +02001705out:
1706 spin_unlock_irqrestore(&priv->lock, spinflags);
1707}
1708
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001709static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1710{
1711 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1712 u32 temp, status = 0;
1713 u32 mask;
1714 int retval = 1;
1715 unsigned long flags;
1716
Rafael J. Wysocki464ed182014-12-19 15:37:54 +01001717 /* if !PM, root hub timers won't get shut down ... */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001718 if (!HC_IS_RUNNING(hcd->state))
1719 return 0;
1720
1721 /* init status to no-changes */
1722 buf[0] = 0;
1723 mask = PORT_CSC;
1724
1725 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001726 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001727
1728 if (temp & PORT_OWNER) {
1729 if (temp & PORT_CSC) {
1730 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001731 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001732 goto done;
1733 }
1734 }
1735
1736 /*
1737 * Return status information even for ports with OWNER set.
Petr Mladek37ebb542014-09-19 17:32:23 +02001738 * Otherwise hub_wq wouldn't see the disconnect event when a
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001739 * high-speed device is switched over to the companion
1740 * controller by the user.
1741 */
1742
1743 if ((temp & mask) != 0
1744 || ((temp & PORT_RESUME) != 0
1745 && time_after_eq(jiffies,
1746 priv->reset_done))) {
1747 buf [0] |= 1 << (0 + 1);
1748 status = STS_PCD;
1749 }
1750 /* FIXME autosuspend idle root hubs */
1751done:
1752 spin_unlock_irqrestore(&priv->lock, flags);
1753 return status ? retval : 0;
1754}
1755
1756static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1757 struct usb_hub_descriptor *desc)
1758{
1759 int ports = HCS_N_PORTS(priv->hcs_params);
1760 u16 temp;
1761
Sergei Shtylyov1cf65632015-03-29 01:40:24 +03001762 desc->bDescriptorType = USB_DT_HUB;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001763 /* priv 1.0, 2.3.9 says 20ms max */
1764 desc->bPwrOn2PwrGood = 10;
1765 desc->bHubContrCurrent = 0;
1766
1767 desc->bNbrPorts = ports;
1768 temp = 1 + (ports / 8);
1769 desc->bDescLength = 7 + 2 * temp;
1770
Sarah Sharpda130512010-11-30 15:55:51 -08001771 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001772 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1773 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001774
1775 /* per-port overcurrent reporting */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001776 temp = HUB_CHAR_INDV_PORT_OCPM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001777 if (HCS_PPC(priv->hcs_params))
1778 /* per-port power control */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001779 temp |= HUB_CHAR_INDV_PORT_LPSM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001780 else
1781 /* no power switching */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001782 temp |= HUB_CHAR_NO_LPSM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001783 desc->wHubCharacteristics = cpu_to_le16(temp);
1784}
1785
1786#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1787
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001788static int check_reset_complete(struct usb_hcd *hcd, int index,
1789 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001790{
1791 if (!(port_status & PORT_CONNECT))
1792 return port_status;
1793
1794 /* if reset finished and it's still not enabled -- handoff */
1795 if (!(port_status & PORT_PE)) {
1796
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001797 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001798 "port %d full speed --> companion\n",
1799 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001800
1801 port_status |= PORT_OWNER;
1802 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001803 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001804
1805 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001806 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001807 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001808
1809 return port_status;
1810}
1811
1812static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1813 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1814{
1815 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1816 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001817 u32 temp, status;
1818 unsigned long flags;
1819 int retval = 0;
1820 unsigned selector;
1821
1822 /*
1823 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1824 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1825 * (track current state ourselves) ... blink for diagnostics,
1826 * power, "this is the one", etc. EHCI spec supports this.
1827 */
1828
1829 spin_lock_irqsave(&priv->lock, flags);
1830 switch (typeReq) {
1831 case ClearHubFeature:
1832 switch (wValue) {
1833 case C_HUB_LOCAL_POWER:
1834 case C_HUB_OVER_CURRENT:
1835 /* no hub-wide feature/status flags */
1836 break;
1837 default:
1838 goto error;
1839 }
1840 break;
1841 case ClearPortFeature:
1842 if (!wIndex || wIndex > ports)
1843 goto error;
1844 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001845 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001846
1847 /*
1848 * Even if OWNER is set, so the port is owned by the
Petr Mladek37ebb542014-09-19 17:32:23 +02001849 * companion controller, hub_wq needs to be able to clear
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001850 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001851 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001852 */
1853
1854 switch (wValue) {
1855 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001856 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001857 break;
1858 case USB_PORT_FEAT_C_ENABLE:
1859 /* XXX error? */
1860 break;
1861 case USB_PORT_FEAT_SUSPEND:
1862 if (temp & PORT_RESET)
1863 goto error;
1864
1865 if (temp & PORT_SUSPEND) {
1866 if ((temp & PORT_PE) == 0)
1867 goto error;
1868 /* resume signaling for 20 msec */
1869 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001870 reg_write32(hcd->regs, HC_PORTSC1,
1871 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001872 priv->reset_done = jiffies +
Felipe Balbi59c99042015-02-13 15:04:06 -06001873 msecs_to_jiffies(USB_RESUME_TIMEOUT);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001874 }
1875 break;
1876 case USB_PORT_FEAT_C_SUSPEND:
1877 /* we auto-clear this feature */
1878 break;
1879 case USB_PORT_FEAT_POWER:
1880 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001881 reg_write32(hcd->regs, HC_PORTSC1,
1882 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001883 break;
1884 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001885 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001886 break;
1887 case USB_PORT_FEAT_C_OVER_CURRENT:
1888 /* XXX error ?*/
1889 break;
1890 case USB_PORT_FEAT_C_RESET:
1891 /* GetPortStatus clears reset */
1892 break;
1893 default:
1894 goto error;
1895 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001896 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001897 break;
1898 case GetHubDescriptor:
1899 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1900 buf);
1901 break;
1902 case GetHubStatus:
1903 /* no hub-wide feature/status flags */
1904 memset(buf, 0, 4);
1905 break;
1906 case GetPortStatus:
1907 if (!wIndex || wIndex > ports)
1908 goto error;
1909 wIndex--;
1910 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001911 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001912
1913 /* wPortChange bits */
1914 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001915 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001916
1917
1918 /* whoever resumes must GetPortStatus to complete it!! */
1919 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001920 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001921
1922 /* Remote Wakeup received? */
1923 if (!priv->reset_done) {
1924 /* resume signaling for 20 msec */
1925 priv->reset_done = jiffies
1926 + msecs_to_jiffies(20);
1927 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001928 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001929 }
1930
1931 /* resume completed? */
1932 else if (time_after_eq(jiffies,
1933 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001934 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001935 priv->reset_done = 0;
1936
1937 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001938 temp = reg_read32(hcd->regs, HC_PORTSC1);
1939 reg_write32(hcd->regs, HC_PORTSC1,
1940 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1941 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001942 PORT_RESUME, 0, 2000 /* 2msec */);
1943 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001944 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001945 "port %d resume error %d\n",
1946 wIndex + 1, retval);
1947 goto error;
1948 }
1949 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1950 }
1951 }
1952
1953 /* whoever resets must GetPortStatus to complete it!! */
1954 if ((temp & PORT_RESET)
1955 && time_after_eq(jiffies,
1956 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001957 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001958 priv->reset_done = 0;
1959
1960 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001961 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001962 /* REVISIT: some hardware needs 550+ usec to clear
1963 * this bit; seems too long to spin routinely...
1964 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001965 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001966 PORT_RESET, 0, 750);
1967 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001968 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001969 wIndex + 1, retval);
1970 goto error;
1971 }
1972
1973 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001974 temp = check_reset_complete(hcd, wIndex,
1975 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001976 }
1977 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001978 * Even if OWNER is set, there's no harm letting hub_wq
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001979 * see the wPortStatus values (they should all be 0 except
1980 * for PORT_POWER anyway).
1981 */
1982
1983 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001984 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001985
1986 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001987 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001988 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001989 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001990 }
1991 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001992 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001993 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001994 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001995 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001996 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001997 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001998 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001999
2000 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2001 break;
2002 case SetHubFeature:
2003 switch (wValue) {
2004 case C_HUB_LOCAL_POWER:
2005 case C_HUB_OVER_CURRENT:
2006 /* no hub-wide feature/status flags */
2007 break;
2008 default:
2009 goto error;
2010 }
2011 break;
2012 case SetPortFeature:
2013 selector = wIndex >> 8;
2014 wIndex &= 0xff;
2015 if (!wIndex || wIndex > ports)
2016 goto error;
2017 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002018 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002019 if (temp & PORT_OWNER)
2020 break;
2021
2022/* temp &= ~PORT_RWC_BITS; */
2023 switch (wValue) {
2024 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002025 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002026 break;
2027
2028 case USB_PORT_FEAT_SUSPEND:
2029 if ((temp & PORT_PE) == 0
2030 || (temp & PORT_RESET) != 0)
2031 goto error;
2032
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002033 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002034 break;
2035 case USB_PORT_FEAT_POWER:
2036 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002037 reg_write32(hcd->regs, HC_PORTSC1,
2038 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002039 break;
2040 case USB_PORT_FEAT_RESET:
2041 if (temp & PORT_RESUME)
2042 goto error;
2043 /* line status bits may report this as low speed,
2044 * which can be fine if this root hub has a
2045 * transaction translator built in.
2046 */
2047 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2048 && PORT_USB11(temp)) {
2049 temp |= PORT_OWNER;
2050 } else {
2051 temp |= PORT_RESET;
2052 temp &= ~PORT_PE;
2053
2054 /*
2055 * caller must wait, then call GetPortStatus
2056 * usb 2.0 spec says 50 ms resets on root
2057 */
2058 priv->reset_done = jiffies +
2059 msecs_to_jiffies(50);
2060 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002061 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002062 break;
2063 default:
2064 goto error;
2065 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002066 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002067 break;
2068
2069 default:
2070error:
2071 /* "stall" on error */
2072 retval = -EPIPE;
2073 }
2074 spin_unlock_irqrestore(&priv->lock, flags);
2075 return retval;
2076}
2077
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002078static int isp1760_get_frame(struct usb_hcd *hcd)
2079{
2080 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2081 u32 fr;
2082
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002083 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002084 return (fr >> 3) % priv->periodic_size;
2085}
2086
2087static void isp1760_stop(struct usb_hcd *hcd)
2088{
2089 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002090 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002091
Arvid Brodin6d50c602011-08-21 08:29:26 +02002092 del_timer(&errata2_timer);
2093
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002094 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2095 NULL, 0);
2096 mdelay(20);
2097
2098 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002099 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002100 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002101 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2102 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002103 spin_unlock_irq(&priv->lock);
2104
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002105 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002106}
2107
2108static void isp1760_shutdown(struct usb_hcd *hcd)
2109{
Nate Case3faefc82008-06-17 11:11:38 -05002110 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002111
2112 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002113 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2114 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002115
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002116 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002117 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002118 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002119}
2120
Arvid Brodin74ad6022011-08-23 01:25:30 +02002121static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2122 struct usb_host_endpoint *ep)
2123{
2124 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2125 struct isp1760_qh *qh = ep->hcpriv;
2126 unsigned long spinflags;
2127
2128 if (!qh)
2129 return;
2130
2131 spin_lock_irqsave(&priv->lock, spinflags);
2132 qh->tt_buffer_dirty = 0;
2133 schedule_ptds(hcd);
2134 spin_unlock_irqrestore(&priv->lock, spinflags);
2135}
2136
2137
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002138static const struct hc_driver isp1760_hc_driver = {
2139 .description = "isp1760-hcd",
2140 .product_desc = "NXP ISP1760 USB Host Controller",
Laurent Pinchartcdd36e82015-01-21 00:55:54 +02002141 .hcd_priv_size = sizeof(struct isp1760_hcd *),
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002142 .irq = isp1760_irq,
2143 .flags = HCD_MEMORY | HCD_USB2,
2144 .reset = isp1760_hc_setup,
2145 .start = isp1760_run,
2146 .stop = isp1760_stop,
2147 .shutdown = isp1760_shutdown,
2148 .urb_enqueue = isp1760_urb_enqueue,
2149 .urb_dequeue = isp1760_urb_dequeue,
2150 .endpoint_disable = isp1760_endpoint_disable,
2151 .get_frame_number = isp1760_get_frame,
2152 .hub_status_data = isp1760_hub_status_data,
2153 .hub_control = isp1760_hub_control,
Arvid Brodin74ad6022011-08-23 01:25:30 +02002154 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002155};
2156
Laurent Pinchart5a6356a2015-01-21 00:55:49 +02002157int __init isp1760_init_kmem_once(void)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002158{
Michael Grzeschik94011ec2012-04-05 14:56:08 +02002159 urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002160 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2161 SLAB_MEM_SPREAD, NULL);
2162
2163 if (!urb_listitem_cachep)
2164 return -ENOMEM;
2165
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002166 qtd_cachep = kmem_cache_create("isp1760_qtd",
2167 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2168 SLAB_MEM_SPREAD, NULL);
2169
2170 if (!qtd_cachep)
2171 return -ENOMEM;
2172
2173 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2174 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2175
2176 if (!qh_cachep) {
2177 kmem_cache_destroy(qtd_cachep);
2178 return -ENOMEM;
2179 }
2180
2181 return 0;
2182}
2183
Laurent Pinchart5a6356a2015-01-21 00:55:49 +02002184void isp1760_deinit_kmem_cache(void)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002185{
2186 kmem_cache_destroy(qtd_cachep);
2187 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002188 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002189}
2190
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002191int isp1760_hcd_register(struct isp1760_hcd *priv, void __iomem *regs,
2192 struct resource *mem, int irq, unsigned long irqflags,
Laurent Pinchart51714462015-01-21 00:55:59 +02002193 struct device *dev)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002194{
2195 struct usb_hcd *hcd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002196 int ret;
2197
Kay Sievers0031a062008-05-02 06:02:41 +02002198 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002199 if (!hcd)
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +02002200 return -ENOMEM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002201
Laurent Pinchartcdd36e82015-01-21 00:55:54 +02002202 *(struct isp1760_hcd **)hcd->hcd_priv = priv;
2203
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002204 priv->hcd = hcd;
Laurent Pinchart7eb42c62015-01-21 00:55:40 +02002205
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002206 init_memory(priv);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002207
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002208 hcd->irq = irq;
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002209 hcd->regs = regs;
Laurent Pinchart4942e002015-01-21 00:55:51 +02002210 hcd->rsrc_start = mem->start;
2211 hcd->rsrc_len = resource_size(mem);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002212
Alan Stern074f9dd2015-01-29 15:05:04 -05002213 /* This driver doesn't support wakeup requests */
2214 hcd->cant_recv_wakeups = 1;
2215
Nate Casee6942d62008-05-21 16:28:20 -05002216 ret = usb_add_hcd(hcd, irq, irqflags);
2217 if (ret)
Laurent Pinchart10feee12015-01-21 00:55:52 +02002218 goto error;
Nate Casee6942d62008-05-21 16:28:20 -05002219
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002220 device_wakeup_enable(hcd->self.controller);
2221
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +02002222 return 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002223
Laurent Pinchart10feee12015-01-21 00:55:52 +02002224error:
Laurent Pinchart30573752015-01-21 00:55:48 +02002225 usb_put_hcd(hcd);
Laurent Pinchart30573752015-01-21 00:55:48 +02002226 return ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002227}
2228
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002229void isp1760_hcd_unregister(struct isp1760_hcd *priv)
Laurent Pinchart10c73f02015-01-21 00:55:45 +02002230{
Laurent Pinchartd21daf12015-01-21 00:56:05 +02002231 if (!priv->hcd)
2232 return;
2233
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002234 usb_remove_hcd(priv->hcd);
2235 usb_put_hcd(priv->hcd);
Laurent Pinchart10c73f02015-01-21 00:55:45 +02002236}