blob: 8e59e0c02b8a421ecd6f2b88e75afc4001fde0e2 [file] [log] [blame]
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001/*
2 * Driver for the NXP ISP1760 chip
3 *
4 * However, the code might contain some bugs. What doesn't work for sure is:
5 * - ISO
6 * - OTG
7 e The interrupt line is configured as active low, level.
8 *
9 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
10 *
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020011 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
12 *
Sebastian Siewiordb11e472008-04-24 00:37:04 +020013 */
Laurent Pinchart7eb42c62015-01-21 00:55:40 +020014#include <linux/gpio/consumer.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020020#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020021#include <linux/debugfs.h>
22#include <linux/uaccess.h>
23#include <linux/io.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000024#include <linux/mm.h>
Arvid Brodin6d50c602011-08-21 08:29:26 +020025#include <linux/timer.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020026#include <asm/unaligned.h>
Catalin Marinasdb8516f2010-02-02 15:31:02 +000027#include <asm/cacheflush.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020028
Laurent Pinchart51714462015-01-21 00:55:59 +020029#include "isp1760-core.h"
Sebastian Siewiordb11e472008-04-24 00:37:04 +020030#include "isp1760-hcd.h"
Laurent Pincharte19c99e2015-01-21 00:55:56 +020031#include "isp1760-regs.h"
Sebastian Siewiordb11e472008-04-24 00:37:04 +020032
33static struct kmem_cache *qtd_cachep;
34static struct kmem_cache *qh_cachep;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +020035static struct kmem_cache *urb_listitem_cachep;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020036
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +020037typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
38 struct isp1760_qtd *qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020039
40static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
41{
Laurent Pinchartcdd36e82015-01-21 00:55:54 +020042 return *(struct isp1760_hcd **)hcd->hcd_priv;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020043}
Sebastian Siewiordb11e472008-04-24 00:37:04 +020044
Laurent Pincharte19c99e2015-01-21 00:55:56 +020045/* urb state*/
46#define DELETE_URB (0x0008)
47#define NO_TRANSFER_ACTIVE (0xffffffff)
Sebastian Siewiordb11e472008-04-24 00:37:04 +020048
Laurent Pincharte19c99e2015-01-21 00:55:56 +020049/* Philips Proprietary Transfer Descriptor (PTD) */
50typedef __u32 __bitwise __dw;
51struct ptd {
52 __dw dw0;
53 __dw dw1;
54 __dw dw2;
55 __dw dw3;
56 __dw dw4;
57 __dw dw5;
58 __dw dw6;
59 __dw dw7;
60};
61#define PTD_OFFSET 0x0400
62#define ISO_PTD_OFFSET 0x0400
63#define INT_PTD_OFFSET 0x0800
64#define ATL_PTD_OFFSET 0x0c00
65#define PAYLOAD_OFFSET 0x1000
Sebastian Siewiordb11e472008-04-24 00:37:04 +020066
Laurent Pincharte19c99e2015-01-21 00:55:56 +020067
68/* ATL */
69/* DW0 */
70#define DW0_VALID_BIT 1
71#define FROM_DW0_VALID(x) ((x) & 0x01)
72#define TO_DW0_LENGTH(x) (((u32) x) << 3)
73#define TO_DW0_MAXPACKET(x) (((u32) x) << 18)
74#define TO_DW0_MULTI(x) (((u32) x) << 29)
75#define TO_DW0_ENDPOINT(x) (((u32) x) << 31)
76/* DW1 */
77#define TO_DW1_DEVICE_ADDR(x) (((u32) x) << 3)
78#define TO_DW1_PID_TOKEN(x) (((u32) x) << 10)
79#define DW1_TRANS_BULK ((u32) 2 << 12)
80#define DW1_TRANS_INT ((u32) 3 << 12)
81#define DW1_TRANS_SPLIT ((u32) 1 << 14)
82#define DW1_SE_USB_LOSPEED ((u32) 2 << 16)
83#define TO_DW1_PORT_NUM(x) (((u32) x) << 18)
84#define TO_DW1_HUB_NUM(x) (((u32) x) << 25)
85/* DW2 */
86#define TO_DW2_DATA_START_ADDR(x) (((u32) x) << 8)
87#define TO_DW2_RL(x) ((x) << 25)
88#define FROM_DW2_RL(x) (((x) >> 25) & 0xf)
89/* DW3 */
90#define FROM_DW3_NRBYTESTRANSFERRED(x) ((x) & 0x7fff)
91#define FROM_DW3_SCS_NRBYTESTRANSFERRED(x) ((x) & 0x07ff)
92#define TO_DW3_NAKCOUNT(x) ((x) << 19)
93#define FROM_DW3_NAKCOUNT(x) (((x) >> 19) & 0xf)
94#define TO_DW3_CERR(x) ((x) << 23)
95#define FROM_DW3_CERR(x) (((x) >> 23) & 0x3)
96#define TO_DW3_DATA_TOGGLE(x) ((x) << 25)
97#define FROM_DW3_DATA_TOGGLE(x) (((x) >> 25) & 0x1)
98#define TO_DW3_PING(x) ((x) << 26)
99#define FROM_DW3_PING(x) (((x) >> 26) & 0x1)
100#define DW3_ERROR_BIT (1 << 28)
101#define DW3_BABBLE_BIT (1 << 29)
102#define DW3_HALT_BIT (1 << 30)
103#define DW3_ACTIVE_BIT (1 << 31)
104#define FROM_DW3_ACTIVE(x) (((x) >> 31) & 0x01)
105
106#define INT_UNDERRUN (1 << 2)
107#define INT_BABBLE (1 << 1)
108#define INT_EXACT (1 << 0)
109
110#define SETUP_PID (2)
111#define IN_PID (1)
112#define OUT_PID (0)
113
114/* Errata 1 */
115#define RL_COUNTER (0)
116#define NAK_COUNTER (0)
117#define ERR_COUNTER (2)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200118
119struct isp1760_qtd {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200120 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200121 void *data_buffer;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100122 u32 payload_addr;
123
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200124 /* the rest is HCD-private */
125 struct list_head qtd_list;
126 struct urb *urb;
127 size_t length;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200128 size_t actual_length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200129
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200130 /* QTD_ENQUEUED: waiting for transfer (inactive) */
131 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
132 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
133 interrupt handler may touch this qtd! */
134 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
135 /* QTD_RETIRE: transfer error/abort qtd */
136#define QTD_ENQUEUED 0
137#define QTD_PAYLOAD_ALLOC 1
138#define QTD_XFER_STARTED 2
139#define QTD_XFER_COMPLETE 3
140#define QTD_RETIRE 4
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200141 u32 status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200142};
143
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200144/* Queue head, one for each active endpoint */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200145struct isp1760_qh {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200146 struct list_head qh_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200147 struct list_head qtd_list;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200148 u32 toggle;
149 u32 ping;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200150 int slot;
Arvid Brodin74ad6022011-08-23 01:25:30 +0200151 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200152};
153
154struct urb_listitem {
155 struct list_head urb_list;
156 struct urb *urb;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200157};
158
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100159/*
160 * Access functions for isp176x registers (addresses 0..0x03FF).
161 */
162static u32 reg_read32(void __iomem *base, u32 reg)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200163{
Laurent Pinchart51714462015-01-21 00:55:59 +0200164 return isp1760_read32(base, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200165}
166
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100167static void reg_write32(void __iomem *base, u32 reg, u32 val)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200168{
Laurent Pinchart51714462015-01-21 00:55:59 +0200169 isp1760_write32(base, reg, val);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200170}
171
172/*
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100173 * Access functions for isp176x memory (offset >= 0x0400).
174 *
175 * bank_reads8() reads memory locations prefetched by an earlier write to
176 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
177 * bank optimizations, you should use the more generic mem_reads8() below.
178 *
179 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
180 * below.
181 *
182 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200183 * doesn't quite work because some people have to enforce 32-bit access
184 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100185static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
186 __u32 *dst, u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200187{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100188 __u32 __iomem *src;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200189 u32 val;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100190 __u8 *src_byteptr;
191 __u8 *dst_byteptr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200192
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100193 src = src_base + (bank_addr | src_offset);
194
195 if (src_offset < PAYLOAD_OFFSET) {
196 while (bytes >= 4) {
197 *dst = le32_to_cpu(__raw_readl(src));
198 bytes -= 4;
199 src++;
200 dst++;
201 }
202 } else {
203 while (bytes >= 4) {
204 *dst = __raw_readl(src);
205 bytes -= 4;
206 src++;
207 dst++;
208 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200209 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200210
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100211 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200212 return;
213
214 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
215 * allocated.
216 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100217 if (src_offset < PAYLOAD_OFFSET)
218 val = le32_to_cpu(__raw_readl(src));
219 else
220 val = __raw_readl(src);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200221
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100222 dst_byteptr = (void *) dst;
223 src_byteptr = (void *) &val;
224 while (bytes > 0) {
225 *dst_byteptr = *src_byteptr;
226 dst_byteptr++;
227 src_byteptr++;
228 bytes--;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200229 }
230}
231
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100232static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
233 u32 bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200234{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100235 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
236 ndelay(90);
237 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
238}
239
240static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
241 __u32 const *src, u32 bytes)
242{
243 __u32 __iomem *dst;
244
245 dst = dst_base + dst_offset;
246
247 if (dst_offset < PAYLOAD_OFFSET) {
248 while (bytes >= 4) {
249 __raw_writel(cpu_to_le32(*src), dst);
250 bytes -= 4;
251 src++;
252 dst++;
253 }
254 } else {
255 while (bytes >= 4) {
256 __raw_writel(*src, dst);
257 bytes -= 4;
258 src++;
259 dst++;
260 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200261 }
262
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100263 if (!bytes)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200264 return;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100265 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
266 * extra bytes should not be read by the HW.
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200267 */
268
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100269 if (dst_offset < PAYLOAD_OFFSET)
270 __raw_writel(cpu_to_le32(*src), dst);
271 else
272 __raw_writel(*src, dst);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200273}
274
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100275/*
276 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
277 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
278 */
279static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
280 struct ptd *ptd)
281{
282 reg_write32(base, HC_MEMORY_REG,
283 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
284 ndelay(90);
285 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
286 (void *) ptd, sizeof(*ptd));
287}
288
289static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
290 struct ptd *ptd)
291{
292 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
293 &ptd->dw1, 7*sizeof(ptd->dw1));
294 /* Make sure dw0 gets written last (after other dw's and after payload)
295 since it contains the enable bit */
296 wmb();
297 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
298 sizeof(ptd->dw0));
299}
300
301
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200302/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
303static void init_memory(struct isp1760_hcd *priv)
304{
Arvid Brodina041d8e2011-02-26 22:04:40 +0100305 int i, curr;
306 u32 payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200307
Arvid Brodina041d8e2011-02-26 22:04:40 +0100308 payload_addr = PAYLOAD_OFFSET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200309 for (i = 0; i < BLOCK_1_NUM; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100310 priv->memory_pool[i].start = payload_addr;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200311 priv->memory_pool[i].size = BLOCK_1_SIZE;
312 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100313 payload_addr += priv->memory_pool[i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200314 }
315
Arvid Brodina041d8e2011-02-26 22:04:40 +0100316 curr = i;
317 for (i = 0; i < BLOCK_2_NUM; i++) {
318 priv->memory_pool[curr + i].start = payload_addr;
319 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
320 priv->memory_pool[curr + i].free = 1;
321 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200322 }
323
Arvid Brodina041d8e2011-02-26 22:04:40 +0100324 curr = i;
325 for (i = 0; i < BLOCK_3_NUM; i++) {
326 priv->memory_pool[curr + i].start = payload_addr;
327 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
328 priv->memory_pool[curr + i].free = 1;
329 payload_addr += priv->memory_pool[curr + i].size;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200330 }
331
Arvid Brodin34537732011-04-26 21:47:37 +0200332 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200333}
334
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100335static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200336{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100337 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200338 int i;
339
Arvid Brodin34537732011-04-26 21:47:37 +0200340 WARN_ON(qtd->payload_addr);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100341
342 if (!qtd->length)
343 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200344
345 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100346 if (priv->memory_pool[i].size >= qtd->length &&
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200347 priv->memory_pool[i].free) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200348 priv->memory_pool[i].free = 0;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100349 qtd->payload_addr = priv->memory_pool[i].start;
350 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200351 }
352 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200353}
354
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100355static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200356{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100357 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200358 int i;
359
Arvid Brodina041d8e2011-02-26 22:04:40 +0100360 if (!qtd->payload_addr)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200361 return;
362
363 for (i = 0; i < BLOCKS; i++) {
Arvid Brodina041d8e2011-02-26 22:04:40 +0100364 if (priv->memory_pool[i].start == qtd->payload_addr) {
Arvid Brodin34537732011-04-26 21:47:37 +0200365 WARN_ON(priv->memory_pool[i].free);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200366 priv->memory_pool[i].free = 1;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100367 qtd->payload_addr = 0;
368 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200369 }
370 }
371
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100372 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
373 __func__, qtd->payload_addr);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200374 WARN_ON(1);
375 qtd->payload_addr = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200376}
377
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100378static int handshake(struct usb_hcd *hcd, u32 reg,
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200379 u32 mask, u32 done, int usec)
380{
381 u32 result;
382
383 do {
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100384 result = reg_read32(hcd->regs, reg);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200385 if (result == ~0)
386 return -ENODEV;
387 result &= mask;
388 if (result == done)
389 return 0;
390 udelay(1);
391 usec--;
392 } while (usec > 0);
393 return -ETIMEDOUT;
394}
395
396/* reset a non-running (STS_HALT == 1) controller */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100397static int ehci_reset(struct usb_hcd *hcd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200398{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100399 struct isp1760_hcd *priv = hcd_to_priv(hcd);
400
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100401 u32 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200402
403 command |= CMD_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100404 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200405 hcd->state = HC_STATE_HALT;
406 priv->next_statechange = jiffies;
Gustavo A. R. Silva992510f2017-07-09 21:00:07 -0500407
408 return handshake(hcd, HC_USBCMD, CMD_RESET, 0, 250 * 1000);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200409}
410
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200411static struct isp1760_qh *qh_alloc(gfp_t flags)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200412{
413 struct isp1760_qh *qh;
414
415 qh = kmem_cache_zalloc(qh_cachep, flags);
416 if (!qh)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200417 return NULL;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200418
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200419 INIT_LIST_HEAD(&qh->qh_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200420 INIT_LIST_HEAD(&qh->qtd_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200421 qh->slot = -1;
422
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200423 return qh;
424}
425
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200426static void qh_free(struct isp1760_qh *qh)
427{
428 WARN_ON(!list_empty(&qh->qtd_list));
429 WARN_ON(qh->slot > -1);
430 kmem_cache_free(qh_cachep, qh);
431}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200432
433/* one-time init, only for memory state */
434static int priv_init(struct usb_hcd *hcd)
435{
436 struct isp1760_hcd *priv = hcd_to_priv(hcd);
437 u32 hcc_params;
Arvid Brodine08f6a272011-11-23 18:10:41 +0100438 int i;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200439
440 spin_lock_init(&priv->lock);
441
Arvid Brodine08f6a272011-11-23 18:10:41 +0100442 for (i = 0; i < QH_END; i++)
443 INIT_LIST_HEAD(&priv->qh_list[i]);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200444
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200445 /*
446 * hw default: 1K periodic list heads, one per frame.
447 * periodic_size can shrink by USBCMD update if hcc_params allows.
448 */
449 priv->periodic_size = DEFAULT_I_TDPS;
450
451 /* controllers may cache some of the periodic schedule ... */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100452 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200453 /* full frame cache */
454 if (HCC_ISOC_CACHE(hcc_params))
455 priv->i_thresh = 8;
456 else /* N microframes cached */
457 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
458
459 return 0;
460}
461
462static int isp1760_hc_setup(struct usb_hcd *hcd)
463{
464 struct isp1760_hcd *priv = hcd_to_priv(hcd);
465 int result;
Nate Case3faefc82008-06-17 11:11:38 -0500466 u32 scratch, hwmode;
467
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100468 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
Nate Case3faefc82008-06-17 11:11:38 -0500469 /* Change bus pattern */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100470 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
471 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200472 if (scratch != 0xdeadbabe) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100473 dev_err(hcd->self.controller, "Scratch test failed.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200474 return -ENODEV;
475 }
476
Laurent Pinchart51714462015-01-21 00:55:59 +0200477 /*
478 * The RESET_HC bit in the SW_RESET register is supposed to reset the
479 * host controller without touching the CPU interface registers, but at
480 * least on the ISP1761 it seems to behave as the RESET_ALL bit and
481 * reset the whole device. We thus can't use it here, so let's reset
482 * the host controller through the EHCI USB Command register. The device
483 * has been reset in core code anyway, so this shouldn't matter.
484 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200485 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
486 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
487 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
488 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200489
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100490 result = ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200491 if (result)
492 return result;
493
494 /* Step 11 passed */
495
Nate Case3faefc82008-06-17 11:11:38 -0500496 /* ATL reset */
Laurent Pinchart51714462015-01-21 00:55:59 +0200497 hwmode = reg_read32(hcd->regs, HC_HW_MODE_CTRL) & ~ALL_ATX_RESET;
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100498 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
Nate Case3faefc82008-06-17 11:11:38 -0500499 mdelay(10);
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100500 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
Nate Case3faefc82008-06-17 11:11:38 -0500501
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100502 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200503
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100504 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200505
506 return priv_init(hcd);
507}
508
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200509static u32 base_to_chip(u32 base)
510{
511 return ((base - 0x400) >> 3);
512}
513
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100514static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
515{
516 struct urb *urb;
517
518 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
519 return 1;
520
521 urb = qtd->urb;
522 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
523 return (qtd->urb != urb);
524}
525
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200526/* magic numbers that can affect system performance */
527#define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
528#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
529#define EHCI_TUNE_RL_TT 0
530#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
531#define EHCI_TUNE_MULT_TT 1
532#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
533
534static void create_ptd_atl(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100535 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200536{
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200537 u32 maxpacket;
538 u32 multi;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200539 u32 rl = RL_COUNTER;
540 u32 nak = NAK_COUNTER;
541
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100542 memset(ptd, 0, sizeof(*ptd));
543
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200544 /* according to 3.6.2, max packet len can not be > 0x400 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100545 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
546 usb_pipeout(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200547 multi = 1 + ((maxpacket >> 11) & 0x3);
548 maxpacket &= 0x7ff;
549
550 /* DW0 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200551 ptd->dw0 = DW0_VALID_BIT;
552 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
553 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
554 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200555
556 /* DW1 */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100557 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200558 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
559 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200560
Arvid Brodina041d8e2011-02-26 22:04:40 +0100561 if (usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200562 ptd->dw1 |= DW1_TRANS_BULK;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100563 else if (usb_pipeint(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200564 ptd->dw1 |= DW1_TRANS_INT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200565
Arvid Brodina041d8e2011-02-26 22:04:40 +0100566 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200567 /* split transaction */
568
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200569 ptd->dw1 |= DW1_TRANS_SPLIT;
Arvid Brodina041d8e2011-02-26 22:04:40 +0100570 if (qtd->urb->dev->speed == USB_SPEED_LOW)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200571 ptd->dw1 |= DW1_SE_USB_LOSPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200572
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200573 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
574 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200575
576 /* SE bit for Split INT transfers */
Arvid Brodina041d8e2011-02-26 22:04:40 +0100577 if (usb_pipeint(qtd->urb->pipe) &&
578 (qtd->urb->dev->speed == USB_SPEED_LOW))
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100579 ptd->dw1 |= 2 << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200580
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200581 rl = 0;
582 nak = 0;
583 } else {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200584 ptd->dw0 |= TO_DW0_MULTI(multi);
Arvid Brodina041d8e2011-02-26 22:04:40 +0100585 if (usb_pipecontrol(qtd->urb->pipe) ||
586 usb_pipebulk(qtd->urb->pipe))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200587 ptd->dw3 |= TO_DW3_PING(qh->ping);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200588 }
589 /* DW2 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100590 ptd->dw2 = 0;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200591 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
592 ptd->dw2 |= TO_DW2_RL(rl);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200593
594 /* DW3 */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200595 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
596 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100597 if (usb_pipecontrol(qtd->urb->pipe)) {
598 if (qtd->data_buffer == qtd->urb->setup_packet)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200599 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100600 else if (last_qtd_of_urb(qtd, qh))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200601 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
Arvid Brodin7adc14b12011-02-26 22:08:47 +0100602 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200603
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200604 ptd->dw3 |= DW3_ACTIVE_BIT;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200605 /* Cerr */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200606 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200607}
608
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100609static void transform_add_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100610 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200611{
Arvid Brodin65f1b522011-02-26 22:07:35 +0100612 u32 usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200613 u32 period;
614
Arvid Brodin65f1b522011-02-26 22:07:35 +0100615 /*
616 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
617 * the algorithm from the original Philips driver code, which was
618 * pretty much used in this driver before as well, is quite horrendous
619 * and, i believe, incorrect. The code below follows the datasheet and
620 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
621 * more reliable this way (fingers crossed...).
622 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200623
Arvid Brodin65f1b522011-02-26 22:07:35 +0100624 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
625 /* urb->interval is in units of microframes (1/8 ms) */
626 period = qtd->urb->interval >> 3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200627
Arvid Brodin65f1b522011-02-26 22:07:35 +0100628 if (qtd->urb->interval > 4)
629 usof = 0x01; /* One bit set =>
630 interval 1 ms * uFrame-match */
631 else if (qtd->urb->interval > 2)
632 usof = 0x22; /* Two bits set => interval 1/2 ms */
633 else if (qtd->urb->interval > 1)
634 usof = 0x55; /* Four bits set => interval 1/4 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200635 else
Arvid Brodin65f1b522011-02-26 22:07:35 +0100636 usof = 0xff; /* All bits set => interval 1/8 ms */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200637 } else {
Arvid Brodin65f1b522011-02-26 22:07:35 +0100638 /* urb->interval is in units of frames (1 ms) */
639 period = qtd->urb->interval;
640 usof = 0x0f; /* Execute Start Split on any of the
641 four first uFrames */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200642
Arvid Brodin65f1b522011-02-26 22:07:35 +0100643 /*
644 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
645 * complete split needs to be sent. Valid only for IN." Also,
646 * "All bits can be set to one for every transfer." (p 82,
647 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
648 * that number come from? 0xff seems to work fine...
649 */
650 /* ptd->dw5 = 0x1c; */
651 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200652 }
653
Arvid Brodin65f1b522011-02-26 22:07:35 +0100654 period = period >> 1;/* Ensure equal or shorter period than requested */
655 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
656
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100657 ptd->dw2 |= period;
658 ptd->dw4 = usof;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200659}
660
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200661static void create_ptd_int(struct isp1760_qh *qh,
Arvid Brodina041d8e2011-02-26 22:04:40 +0100662 struct isp1760_qtd *qtd, struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200663{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200664 create_ptd_atl(qh, qtd, ptd);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100665 transform_add_int(qh, qtd, ptd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200666}
667
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100668static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200669__releases(priv->lock)
670__acquires(priv->lock)
671{
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100672 struct isp1760_hcd *priv = hcd_to_priv(hcd);
673
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200674 if (!urb->unlinked) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100675 if (urb->status == -EINPROGRESS)
676 urb->status = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200677 }
678
Catalin Marinasdb8516f2010-02-02 15:31:02 +0000679 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
680 void *ptr;
681 for (ptr = urb->transfer_buffer;
682 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
683 ptr += PAGE_SIZE)
684 flush_dcache_page(virt_to_page(ptr));
685 }
686
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200687 /* complete() can reenter this HCD */
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100688 usb_hcd_unlink_urb_from_ep(hcd, urb);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200689 spin_unlock(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +0100690 usb_hcd_giveback_urb(hcd, urb, urb->status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200691 spin_lock(&priv->lock);
692}
693
Arvid Brodin34537732011-04-26 21:47:37 +0200694static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
695 u8 packet_type)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200696{
Arvid Brodin34537732011-04-26 21:47:37 +0200697 struct isp1760_qtd *qtd;
698
699 qtd = kmem_cache_zalloc(qtd_cachep, flags);
700 if (!qtd)
701 return NULL;
702
703 INIT_LIST_HEAD(&qtd->qtd_list);
704 qtd->urb = urb;
705 qtd->packet_type = packet_type;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200706 qtd->status = QTD_ENQUEUED;
707 qtd->actual_length = 0;
Arvid Brodin34537732011-04-26 21:47:37 +0200708
709 return qtd;
710}
711
712static void qtd_free(struct isp1760_qtd *qtd)
713{
714 WARN_ON(qtd->payload_addr);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200715 kmem_cache_free(qtd_cachep, qtd);
716}
717
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200718static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +0200719 struct isp1760_slotinfo *slots,
720 struct isp1760_qtd *qtd, struct isp1760_qh *qh,
721 struct ptd *ptd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200722{
Arvid Brodinbedc0c32011-02-26 22:02:57 +0100723 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200724 int skip_map;
725
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200726 WARN_ON((slot < 0) || (slot > 31));
727 WARN_ON(qtd->length && !qtd->payload_addr);
728 WARN_ON(slots[slot].qtd);
729 WARN_ON(slots[slot].qh);
730 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200731
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200732 /* Make sure done map has not triggered from some unlinked transfer */
733 if (ptd_offset == ATL_PTD_OFFSET) {
734 priv->atl_done_map |= reg_read32(hcd->regs,
735 HC_ATL_PTD_DONEMAP_REG);
Arvid Brodin6477acc2011-08-21 08:29:28 +0200736 priv->atl_done_map &= ~(1 << slot);
737 } else {
738 priv->int_done_map |= reg_read32(hcd->regs,
739 HC_INT_PTD_DONEMAP_REG);
740 priv->int_done_map &= ~(1 << slot);
741 }
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200742
Arvid Brodin6477acc2011-08-21 08:29:28 +0200743 qh->slot = slot;
744 qtd->status = QTD_XFER_STARTED;
745 slots[slot].timestamp = jiffies;
746 slots[slot].qtd = qtd;
747 slots[slot].qh = qh;
748 ptd_write(hcd->regs, ptd_offset, slot, ptd);
749
750 if (ptd_offset == ATL_PTD_OFFSET) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200751 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
752 skip_map &= ~(1 << qh->slot);
753 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
754 } else {
Arvid Brodind05b6ec2011-05-20 00:17:45 +0200755 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
756 skip_map &= ~(1 << qh->slot);
757 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
758 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200759}
760
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200761static int is_short_bulk(struct isp1760_qtd *qtd)
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200762{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200763 return (usb_pipebulk(qtd->urb->pipe) &&
764 (qtd->actual_length < qtd->length));
765}
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200766
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200767static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
768 struct list_head *urb_list)
769{
770 int last_qtd;
771 struct isp1760_qtd *qtd, *qtd_next;
772 struct urb_listitem *urb_listitem;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200773
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200774 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
775 if (qtd->status < QTD_XFER_COMPLETE)
776 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200777
Arvid Brodin38679b72011-08-21 08:29:27 +0200778 last_qtd = last_qtd_of_urb(qtd, qh);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200779
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200780 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
781 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200782
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200783 if (qtd->status == QTD_XFER_COMPLETE) {
784 if (qtd->actual_length) {
785 switch (qtd->packet_type) {
786 case IN_PID:
787 mem_reads8(hcd->regs, qtd->payload_addr,
788 qtd->data_buffer,
789 qtd->actual_length);
790 /* Fall through (?) */
791 case OUT_PID:
792 qtd->urb->actual_length +=
793 qtd->actual_length;
794 /* Fall through ... */
795 case SETUP_PID:
796 break;
797 }
798 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200799
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200800 if (is_short_bulk(qtd)) {
801 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
802 qtd->urb->status = -EREMOTEIO;
803 if (!last_qtd)
804 qtd_next->status = QTD_RETIRE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200805 }
806 }
807
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200808 if (qtd->payload_addr)
809 free_mem(hcd, qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200810
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200811 if (last_qtd) {
812 if ((qtd->status == QTD_RETIRE) &&
813 (qtd->urb->status == -EINPROGRESS))
814 qtd->urb->status = -EPIPE;
815 /* Defer calling of urb_done() since it releases lock */
816 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
817 GFP_ATOMIC);
818 if (unlikely(!urb_listitem))
Arvid Brodin38679b72011-08-21 08:29:27 +0200819 break; /* Try again on next call */
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200820 urb_listitem->urb = qtd->urb;
821 list_add_tail(&urb_listitem->urb_list, urb_list);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200822 }
823
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200824 list_del(&qtd->qtd_list);
825 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200826 }
827}
828
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200829#define ENQUEUE_DEPTH 2
830static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
831{
832 struct isp1760_hcd *priv = hcd_to_priv(hcd);
833 int ptd_offset;
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +0200834 struct isp1760_slotinfo *slots;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200835 int curr_slot, free_slot;
836 int n;
837 struct ptd ptd;
838 struct isp1760_qtd *qtd;
839
840 if (unlikely(list_empty(&qh->qtd_list))) {
841 WARN_ON(1);
842 return;
843 }
844
Arvid Brodin74ad6022011-08-23 01:25:30 +0200845 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
846 if (qh->tt_buffer_dirty)
847 return;
848
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200849 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
850 qtd_list)->urb->pipe)) {
851 ptd_offset = INT_PTD_OFFSET;
852 slots = priv->int_slots;
853 } else {
854 ptd_offset = ATL_PTD_OFFSET;
855 slots = priv->atl_slots;
856 }
857
858 free_slot = -1;
859 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
860 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
861 free_slot = curr_slot;
862 if (slots[curr_slot].qh == qh)
863 break;
864 }
865
866 n = 0;
867 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
868 if (qtd->status == QTD_ENQUEUED) {
869 WARN_ON(qtd->payload_addr);
870 alloc_mem(hcd, qtd);
871 if ((qtd->length) && (!qtd->payload_addr))
872 break;
873
874 if ((qtd->length) &&
875 ((qtd->packet_type == SETUP_PID) ||
876 (qtd->packet_type == OUT_PID))) {
877 mem_writes8(hcd->regs, qtd->payload_addr,
878 qtd->data_buffer, qtd->length);
879 }
880
881 qtd->status = QTD_PAYLOAD_ALLOC;
882 }
883
884 if (qtd->status == QTD_PAYLOAD_ALLOC) {
885/*
886 if ((curr_slot > 31) && (free_slot == -1))
887 dev_dbg(hcd->self.controller, "%s: No slot "
888 "available for transfer\n", __func__);
889*/
890 /* Start xfer for this endpoint if not already done */
891 if ((curr_slot > 31) && (free_slot > -1)) {
892 if (usb_pipeint(qtd->urb->pipe))
893 create_ptd_int(qh, qtd, &ptd);
894 else
895 create_ptd_atl(qh, qtd, &ptd);
896
897 start_bus_transfer(hcd, ptd_offset, free_slot,
898 slots, qtd, qh, &ptd);
899 curr_slot = free_slot;
900 }
901
902 n++;
903 if (n >= ENQUEUE_DEPTH)
904 break;
905 }
906 }
907}
908
Arvid Brodinde9c6302013-01-27 16:41:26 +0100909static void schedule_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200910{
911 struct isp1760_hcd *priv;
912 struct isp1760_qh *qh, *qh_next;
913 struct list_head *ep_queue;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200914 LIST_HEAD(urb_list);
915 struct urb_listitem *urb_listitem, *urb_listitem_next;
Arvid Brodine08f6a272011-11-23 18:10:41 +0100916 int i;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200917
918 if (!hcd) {
919 WARN_ON(1);
920 return;
921 }
922
923 priv = hcd_to_priv(hcd);
924
925 /*
926 * check finished/retired xfers, transfer payloads, call urb_done()
927 */
Arvid Brodine08f6a272011-11-23 18:10:41 +0100928 for (i = 0; i < QH_END; i++) {
929 ep_queue = &priv->qh_list[i];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200930 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200931 collect_qtds(hcd, qh, &urb_list);
Arvid Brodinc64391f2011-11-23 18:13:02 +0100932 if (list_empty(&qh->qtd_list))
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200933 list_del(&qh->qh_list);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200934 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200935 }
936
937 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
938 urb_list) {
939 isp1760_urb_done(hcd, urb_listitem->urb);
940 kmem_cache_free(urb_listitem_cachep, urb_listitem);
941 }
942
943 /*
944 * Schedule packets for transfer.
945 *
946 * According to USB2.0 specification:
947 *
948 * 1st prio: interrupt xfers, up to 80 % of bandwidth
949 * 2nd prio: control xfers
950 * 3rd prio: bulk xfers
951 *
952 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
953 * is very unclear on how to prioritize traffic):
954 *
955 * 1) Enqueue any queued control transfers, as long as payload chip mem
956 * and PTD ATL slots are available.
957 * 2) Enqueue any queued INT transfers, as long as payload chip mem
958 * and PTD INT slots are available.
959 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
960 * and PTD ATL slots are available.
961 *
962 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
963 * conservation of chip mem and performance.
964 *
965 * I'm sure this scheme could be improved upon!
966 */
Arvid Brodine08f6a272011-11-23 18:10:41 +0100967 for (i = 0; i < QH_END; i++) {
968 ep_queue = &priv->qh_list[i];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200969 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
970 enqueue_qtds(hcd, qh);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +0200971 }
972}
973
974#define PTD_STATE_QTD_DONE 1
975#define PTD_STATE_QTD_RELOAD 2
976#define PTD_STATE_URB_RETIRE 3
977
978static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
979 struct urb *urb)
980{
981 __dw dw4;
982 int i;
983
984 dw4 = ptd->dw4;
985 dw4 >>= 8;
986
987 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
988 need to handle these errors? Is it done in hardware? */
989
990 if (ptd->dw3 & DW3_HALT_BIT) {
991
992 urb->status = -EPROTO; /* Default unknown error */
993
994 for (i = 0; i < 8; i++) {
995 switch (dw4 & 0x7) {
996 case INT_UNDERRUN:
997 dev_dbg(hcd->self.controller, "%s: underrun "
998 "during uFrame %d\n",
999 __func__, i);
1000 urb->status = -ECOMM; /* Could not write data */
1001 break;
1002 case INT_EXACT:
1003 dev_dbg(hcd->self.controller, "%s: transaction "
1004 "error during uFrame %d\n",
1005 __func__, i);
1006 urb->status = -EPROTO; /* timeout, bad CRC, PID
1007 error etc. */
1008 break;
1009 case INT_BABBLE:
1010 dev_dbg(hcd->self.controller, "%s: babble "
1011 "error during uFrame %d\n",
1012 __func__, i);
1013 urb->status = -EOVERFLOW;
1014 break;
1015 }
1016 dw4 >>= 3;
1017 }
1018
1019 return PTD_STATE_URB_RETIRE;
1020 }
1021
1022 return PTD_STATE_QTD_DONE;
1023}
1024
1025static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1026 struct urb *urb)
1027{
1028 WARN_ON(!ptd);
1029 if (ptd->dw3 & DW3_HALT_BIT) {
1030 if (ptd->dw3 & DW3_BABBLE_BIT)
1031 urb->status = -EOVERFLOW;
1032 else if (FROM_DW3_CERR(ptd->dw3))
1033 urb->status = -EPIPE; /* Stall */
1034 else if (ptd->dw3 & DW3_ERROR_BIT)
1035 urb->status = -EPROTO; /* XactErr */
1036 else
1037 urb->status = -EPROTO; /* Unknown */
1038/*
1039 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1040 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1041 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1042 __func__,
1043 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1044 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1045*/
1046 return PTD_STATE_URB_RETIRE;
1047 }
1048
1049 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1050 /* Transfer Error, *but* active and no HALT -> reload */
1051 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1052 return PTD_STATE_QTD_RELOAD;
1053 }
1054
1055 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1056 /*
1057 * NAKs are handled in HW by the chip. Usually if the
1058 * device is not able to send data fast enough.
1059 * This happens mostly on slower hardware.
1060 */
1061 return PTD_STATE_QTD_RELOAD;
1062 }
1063
1064 return PTD_STATE_QTD_DONE;
1065}
1066
Arvid Brodin6d50c602011-08-21 08:29:26 +02001067static void handle_done_ptds(struct usb_hcd *hcd)
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001068{
1069 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001070 struct ptd ptd;
1071 struct isp1760_qh *qh;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001072 int slot;
1073 int state;
Laurent Pinchartea0b1fa2015-01-21 00:55:55 +02001074 struct isp1760_slotinfo *slots;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001075 u32 ptd_offset;
1076 struct isp1760_qtd *qtd;
1077 int modified;
Arvid Brodin6d50c602011-08-21 08:29:26 +02001078 int skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001079
Arvid Brodin6d50c602011-08-21 08:29:26 +02001080 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1081 priv->int_done_map &= ~skip_map;
1082 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1083 priv->atl_done_map &= ~skip_map;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001084
Arvid Brodin6d50c602011-08-21 08:29:26 +02001085 modified = priv->int_done_map || priv->atl_done_map;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001086
1087 while (priv->int_done_map || priv->atl_done_map) {
1088 if (priv->int_done_map) {
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001089 /* INT ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001090 slot = __ffs(priv->int_done_map);
1091 priv->int_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001092 slots = priv->int_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001093 /* This should not trigger, and could be removed if
1094 noone have any problems with it triggering: */
1095 if (!slots[slot].qh) {
1096 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001097 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001098 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001099 ptd_offset = INT_PTD_OFFSET;
1100 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1101 state = check_int_transfer(hcd, &ptd,
1102 slots[slot].qtd->urb);
1103 } else {
1104 /* ATL ptd */
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001105 slot = __ffs(priv->atl_done_map);
1106 priv->atl_done_map &= ~(1 << slot);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001107 slots = priv->atl_slots;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001108 /* This should not trigger, and could be removed if
1109 noone have any problems with it triggering: */
1110 if (!slots[slot].qh) {
1111 WARN_ON(1);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001112 continue;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001113 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001114 ptd_offset = ATL_PTD_OFFSET;
1115 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1116 state = check_atl_transfer(hcd, &ptd,
1117 slots[slot].qtd->urb);
1118 }
1119
1120 qtd = slots[slot].qtd;
1121 slots[slot].qtd = NULL;
1122 qh = slots[slot].qh;
1123 slots[slot].qh = NULL;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001124 qh->slot = -1;
1125
1126 WARN_ON(qtd->status != QTD_XFER_STARTED);
1127
1128 switch (state) {
1129 case PTD_STATE_QTD_DONE:
1130 if ((usb_pipeint(qtd->urb->pipe)) &&
1131 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1132 qtd->actual_length =
1133 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1134 else
1135 qtd->actual_length =
1136 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1137
1138 qtd->status = QTD_XFER_COMPLETE;
1139 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1140 is_short_bulk(qtd))
1141 qtd = NULL;
1142 else
1143 qtd = list_entry(qtd->qtd_list.next,
1144 typeof(*qtd), qtd_list);
1145
1146 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1147 qh->ping = FROM_DW3_PING(ptd.dw3);
1148 break;
1149
1150 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1151 qtd->status = QTD_PAYLOAD_ALLOC;
1152 ptd.dw0 |= DW0_VALID_BIT;
1153 /* RL counter = ERR counter */
1154 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1155 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1156 ptd.dw3 &= ~TO_DW3_CERR(3);
1157 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1158 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1159 qh->ping = FROM_DW3_PING(ptd.dw3);
1160 break;
1161
1162 case PTD_STATE_URB_RETIRE:
1163 qtd->status = QTD_RETIRE;
Arvid Brodin74ad6022011-08-23 01:25:30 +02001164 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1165 (qtd->urb->status != -EPIPE) &&
1166 (qtd->urb->status != -EREMOTEIO)) {
1167 qh->tt_buffer_dirty = 1;
1168 if (usb_hub_clear_tt_buffer(qtd->urb))
1169 /* Clear failed; let's hope things work
1170 anyway */
1171 qh->tt_buffer_dirty = 0;
1172 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001173 qtd = NULL;
1174 qh->toggle = 0;
1175 qh->ping = 0;
1176 break;
1177
1178 default:
1179 WARN_ON(1);
1180 continue;
1181 }
1182
1183 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1184 if (slots == priv->int_slots) {
1185 if (state == PTD_STATE_QTD_RELOAD)
1186 dev_err(hcd->self.controller,
1187 "%s: PTD_STATE_QTD_RELOAD on "
1188 "interrupt packet\n", __func__);
1189 if (state != PTD_STATE_QTD_RELOAD)
1190 create_ptd_int(qh, qtd, &ptd);
1191 } else {
1192 if (state != PTD_STATE_QTD_RELOAD)
1193 create_ptd_atl(qh, qtd, &ptd);
1194 }
1195
1196 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1197 qh, &ptd);
1198 }
1199 }
1200
1201 if (modified)
1202 schedule_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001203}
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001204
Arvid Brodin6d50c602011-08-21 08:29:26 +02001205static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1206{
1207 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1208 u32 imask;
1209 irqreturn_t irqret = IRQ_NONE;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001210
Arvid Brodin6d50c602011-08-21 08:29:26 +02001211 spin_lock(&priv->lock);
1212
1213 if (!(hcd->state & HC_STATE_RUNNING))
1214 goto leave;
1215
1216 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1217 if (unlikely(!imask))
1218 goto leave;
1219 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1220
1221 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1222 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1223
1224 handle_done_ptds(hcd);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001225
1226 irqret = IRQ_HANDLED;
1227leave:
1228 spin_unlock(&priv->lock);
1229
1230 return irqret;
1231}
1232
Arvid Brodin6d50c602011-08-21 08:29:26 +02001233/*
1234 * Workaround for problem described in chip errata 2:
1235 *
1236 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1237 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1238 * ATL done interrupts (the "instead of" might be important since it seems
1239 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1240 * to set the PTD's done bit in addition to not generating an interrupt!).
1241 *
1242 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1243 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1244 *
1245 * If we use SOF interrupts only, we get latency between ptd completion and the
1246 * actual handling. This is very noticeable in testusb runs which takes several
1247 * minutes longer without ATL interrupts.
1248 *
1249 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1250 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1251 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1252 * completed and its done map bit is set.
1253 *
1254 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1255 * not to cause too much lag when this HW bug occurs, while still hopefully
1256 * ensuring that the check does not falsely trigger.
1257 */
Arvid Brodin6477acc2011-08-21 08:29:28 +02001258#define SLOT_TIMEOUT 300
Arvid Brodin6d50c602011-08-21 08:29:26 +02001259#define SLOT_CHECK_PERIOD 200
1260static struct timer_list errata2_timer;
1261
Arvid Brodinde9c6302013-01-27 16:41:26 +01001262static void errata2_function(unsigned long data)
Arvid Brodin6d50c602011-08-21 08:29:26 +02001263{
1264 struct usb_hcd *hcd = (struct usb_hcd *) data;
1265 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1266 int slot;
1267 struct ptd ptd;
1268 unsigned long spinflags;
1269
1270 spin_lock_irqsave(&priv->lock, spinflags);
1271
1272 for (slot = 0; slot < 32; slot++)
Arvid Brodin6477acc2011-08-21 08:29:28 +02001273 if (priv->atl_slots[slot].qh && time_after(jiffies,
1274 priv->atl_slots[slot].timestamp +
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001275 msecs_to_jiffies(SLOT_TIMEOUT))) {
Arvid Brodin6d50c602011-08-21 08:29:26 +02001276 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1277 if (!FROM_DW0_VALID(ptd.dw0) &&
1278 !FROM_DW3_ACTIVE(ptd.dw3))
1279 priv->atl_done_map |= 1 << slot;
1280 }
1281
Arvid Brodin6477acc2011-08-21 08:29:28 +02001282 if (priv->atl_done_map)
1283 handle_done_ptds(hcd);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001284
1285 spin_unlock_irqrestore(&priv->lock, spinflags);
1286
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001287 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001288 add_timer(&errata2_timer);
1289}
1290
Arvid Brodin0ba79052011-08-21 08:29:25 +02001291static int isp1760_run(struct usb_hcd *hcd)
1292{
1293 int retval;
1294 u32 temp;
1295 u32 command;
1296 u32 chipid;
1297
1298 hcd->uses_new_polling = 1;
1299
1300 hcd->state = HC_STATE_RUNNING;
1301
1302 /* Set PTD interrupt AND & OR maps */
1303 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1304 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1305 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1306 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1307 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1308 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1309 /* step 23 passed */
1310
1311 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1312 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1313
1314 command = reg_read32(hcd->regs, HC_USBCMD);
1315 command &= ~(CMD_LRESET|CMD_RESET);
1316 command |= CMD_RUN;
1317 reg_write32(hcd->regs, HC_USBCMD, command);
1318
1319 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1320 if (retval)
1321 return retval;
1322
1323 /*
1324 * XXX
1325 * Spec says to write FLAG_CF as last config action, priv code grabs
1326 * the semaphore while doing so.
1327 */
1328 down_write(&ehci_cf_port_reset_rwsem);
1329 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1330
1331 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1332 up_write(&ehci_cf_port_reset_rwsem);
1333 if (retval)
1334 return retval;
1335
Julia Lawallb7aa4cc2014-12-26 15:35:51 +01001336 setup_timer(&errata2_timer, errata2_function, (unsigned long)hcd);
Nicholas Mc Guire4d3db7d2015-02-06 05:08:53 -05001337 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
Arvid Brodin6d50c602011-08-21 08:29:26 +02001338 add_timer(&errata2_timer);
1339
Arvid Brodin0ba79052011-08-21 08:29:25 +02001340 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1341 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1342 chipid & 0xffff, chipid >> 16);
1343
1344 /* PTD Register Init Part 2, Step 28 */
1345
1346 /* Setup registers controlling PTD checking */
1347 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1348 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1349 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1350 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1351 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1352 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1353 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1354 ATL_BUF_FILL | INT_BUF_FILL);
1355
1356 /* GRR this is run-once init(), being done every time the HC starts.
1357 * So long as they're part of class devices, we can't do it init()
1358 * since the class device isn't created that early.
1359 */
1360 return 0;
1361}
1362
Arvid Brodin34537732011-04-26 21:47:37 +02001363static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001364{
Arvid Brodin34537732011-04-26 21:47:37 +02001365 qtd->data_buffer = databuffer;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001366
Arvid Brodin34537732011-04-26 21:47:37 +02001367 if (len > MAX_PAYLOAD_SIZE)
1368 len = MAX_PAYLOAD_SIZE;
1369 qtd->length = len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001370
Arvid Brodin34537732011-04-26 21:47:37 +02001371 return qtd->length;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001372}
1373
Arvid Brodin34537732011-04-26 21:47:37 +02001374static void qtd_list_free(struct list_head *qtd_list)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001375{
Arvid Brodin34537732011-04-26 21:47:37 +02001376 struct isp1760_qtd *qtd, *qtd_next;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001377
Arvid Brodin34537732011-04-26 21:47:37 +02001378 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001379 list_del(&qtd->qtd_list);
Arvid Brodin34537732011-04-26 21:47:37 +02001380 qtd_free(qtd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001381 }
1382}
1383
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001384/*
Arvid Brodin34537732011-04-26 21:47:37 +02001385 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1386 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001387 */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001388#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
Arvid Brodin34537732011-04-26 21:47:37 +02001389static void packetize_urb(struct usb_hcd *hcd,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001390 struct urb *urb, struct list_head *head, gfp_t flags)
1391{
Arvid Brodinfd436ae2011-02-26 22:03:49 +01001392 struct isp1760_qtd *qtd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001393 void *buf;
Arvid Brodin34537732011-04-26 21:47:37 +02001394 int len, maxpacketsize;
1395 u8 packet_type;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001396
1397 /*
1398 * URBs map to sequences of QTDs: one logical transaction
1399 */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001400
Arvid Brodin34537732011-04-26 21:47:37 +02001401 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1402 /* XXX This looks like usb storage / SCSI bug */
1403 dev_err(hcd->self.controller,
1404 "buf is null, dma is %08lx len is %d\n",
1405 (long unsigned)urb->transfer_dma,
1406 urb->transfer_buffer_length);
1407 WARN_ON(1);
1408 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001409
Arvid Brodin34537732011-04-26 21:47:37 +02001410 if (usb_pipein(urb->pipe))
1411 packet_type = IN_PID;
1412 else
1413 packet_type = OUT_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001414
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001415 if (usb_pipecontrol(urb->pipe)) {
Arvid Brodin34537732011-04-26 21:47:37 +02001416 qtd = qtd_alloc(flags, urb, SETUP_PID);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001417 if (!qtd)
1418 goto cleanup;
Arvid Brodin34537732011-04-26 21:47:37 +02001419 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001420 list_add_tail(&qtd->qtd_list, head);
1421
1422 /* for zero length DATA stages, STATUS is always IN */
Arvid Brodin34537732011-04-26 21:47:37 +02001423 if (urb->transfer_buffer_length == 0)
1424 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001425 }
1426
Arvid Brodin34537732011-04-26 21:47:37 +02001427 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1428 usb_pipeout(urb->pipe)));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001429
1430 /*
1431 * buffer gets wrapped in one or more qtds;
1432 * last one may be "short" (including zero len)
1433 * and may serve as a control status ack
1434 */
Arvid Brodin34537732011-04-26 21:47:37 +02001435 buf = urb->transfer_buffer;
1436 len = urb->transfer_buffer_length;
1437
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001438 for (;;) {
1439 int this_qtd_len;
1440
Arvid Brodin34537732011-04-26 21:47:37 +02001441 qtd = qtd_alloc(flags, urb, packet_type);
1442 if (!qtd)
1443 goto cleanup;
1444 this_qtd_len = qtd_fill(qtd, buf, len);
1445 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001446
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001447 len -= this_qtd_len;
1448 buf += this_qtd_len;
1449
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001450 if (len <= 0)
1451 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001452 }
1453
1454 /*
1455 * control requests may need a terminating data "status" ack;
1456 * bulk ones may need a terminating short packet (zero length).
1457 */
1458 if (urb->transfer_buffer_length != 0) {
1459 int one_more = 0;
1460
1461 if (usb_pipecontrol(urb->pipe)) {
1462 one_more = 1;
Arvid Brodin34537732011-04-26 21:47:37 +02001463 if (packet_type == IN_PID)
1464 packet_type = OUT_PID;
1465 else
1466 packet_type = IN_PID;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001467 } else if (usb_pipebulk(urb->pipe)
1468 && (urb->transfer_flags & URB_ZERO_PACKET)
Arvid Brodin34537732011-04-26 21:47:37 +02001469 && !(urb->transfer_buffer_length %
1470 maxpacketsize)) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001471 one_more = 1;
1472 }
1473 if (one_more) {
Arvid Brodin34537732011-04-26 21:47:37 +02001474 qtd = qtd_alloc(flags, urb, packet_type);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001475 if (!qtd)
1476 goto cleanup;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001477
1478 /* never any data in such packets */
Arvid Brodin34537732011-04-26 21:47:37 +02001479 qtd_fill(qtd, NULL, 0);
1480 list_add_tail(&qtd->qtd_list, head);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001481 }
1482 }
1483
Arvid Brodin34537732011-04-26 21:47:37 +02001484 return;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001485
1486cleanup:
Arvid Brodin34537732011-04-26 21:47:37 +02001487 qtd_list_free(head);
1488}
1489
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001490static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1491 gfp_t mem_flags)
1492{
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001493 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1494 struct list_head *ep_queue;
1495 struct isp1760_qh *qh, *qhit;
1496 unsigned long spinflags;
1497 LIST_HEAD(new_qtds);
1498 int retval;
1499 int qh_in_queue;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001500
1501 switch (usb_pipetype(urb->pipe)) {
1502 case PIPE_CONTROL:
Arvid Brodine08f6a272011-11-23 18:10:41 +01001503 ep_queue = &priv->qh_list[QH_CONTROL];
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001504 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001505 case PIPE_BULK:
Arvid Brodine08f6a272011-11-23 18:10:41 +01001506 ep_queue = &priv->qh_list[QH_BULK];
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001507 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001508 case PIPE_INTERRUPT:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001509 if (urb->interval < 0)
1510 return -EINVAL;
1511 /* FIXME: Check bandwidth */
Arvid Brodine08f6a272011-11-23 18:10:41 +01001512 ep_queue = &priv->qh_list[QH_INTERRUPT];
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001513 break;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001514 case PIPE_ISOCHRONOUS:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001515 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1516 "not yet supported\n",
1517 __func__);
1518 return -EPIPE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001519 default:
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001520 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1521 __func__);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001522 return -EPIPE;
1523 }
1524
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001525 if (usb_pipein(urb->pipe))
1526 urb->actual_length = 0;
1527
1528 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1529 if (list_empty(&new_qtds))
Arvid Brodin34537732011-04-26 21:47:37 +02001530 return -ENOMEM;
1531
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001532 retval = 0;
1533 spin_lock_irqsave(&priv->lock, spinflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001534
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001535 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1536 retval = -ESHUTDOWN;
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001537 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001538 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001539 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001540 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001541 if (retval) {
1542 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001543 goto out;
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001544 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001545
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001546 qh = urb->ep->hcpriv;
1547 if (qh) {
1548 qh_in_queue = 0;
1549 list_for_each_entry(qhit, ep_queue, qh_list) {
1550 if (qhit == qh) {
1551 qh_in_queue = 1;
Warren Free0afb20e2009-05-08 10:27:08 +02001552 break;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001553 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001554 }
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001555 if (!qh_in_queue)
1556 list_add_tail(&qh->qh_list, ep_queue);
1557 } else {
1558 qh = qh_alloc(GFP_ATOMIC);
1559 if (!qh) {
1560 retval = -ENOMEM;
Arvid Brodin38679b72011-08-21 08:29:27 +02001561 usb_hcd_unlink_urb_from_ep(hcd, urb);
Michael Grzeschik8788fa02012-04-05 14:56:10 +02001562 qtd_list_free(&new_qtds);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001563 goto out;
1564 }
1565 list_add_tail(&qh->qh_list, ep_queue);
1566 urb->ep->hcpriv = qh;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001567 }
1568
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001569 list_splice_tail(&new_qtds, &qh->qtd_list);
1570 schedule_ptds(hcd);
1571
1572out:
1573 spin_unlock_irqrestore(&priv->lock, spinflags);
1574 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001575}
1576
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001577static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1578 struct isp1760_qh *qh)
1579{
1580 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1581 int skip_map;
1582
1583 WARN_ON(qh->slot == -1);
1584
1585 /* We need to forcefully reclaim the slot since some transfers never
1586 return, e.g. interrupt transfers and NAKed bulk transfers. */
Arvid Brodin8b1ab602011-06-17 18:45:37 +02001587 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001588 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1589 skip_map |= (1 << qh->slot);
1590 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1591 priv->atl_slots[qh->slot].qh = NULL;
1592 priv->atl_slots[qh->slot].qtd = NULL;
1593 } else {
1594 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1595 skip_map |= (1 << qh->slot);
1596 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1597 priv->int_slots[qh->slot].qh = NULL;
1598 priv->int_slots[qh->slot].qtd = NULL;
1599 }
1600
1601 qh->slot = -1;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001602}
1603
Arvid Brodin74ad6022011-08-23 01:25:30 +02001604/*
1605 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1606 * any active transfer belonging to the urb in the process.
1607 */
1608static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1609 struct isp1760_qtd *qtd)
1610{
1611 struct urb *urb;
1612 int urb_was_running;
1613
1614 urb = qtd->urb;
1615 urb_was_running = 0;
1616 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
1617 if (qtd->urb != urb)
1618 break;
1619
1620 if (qtd->status >= QTD_XFER_STARTED)
1621 urb_was_running = 1;
1622 if (last_qtd_of_urb(qtd, qh) &&
1623 (qtd->status >= QTD_XFER_COMPLETE))
1624 urb_was_running = 0;
1625
1626 if (qtd->status == QTD_XFER_STARTED)
1627 kill_transfer(hcd, urb, qh);
1628 qtd->status = QTD_RETIRE;
1629 }
1630
1631 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
1632 qh->tt_buffer_dirty = 1;
1633 if (usb_hub_clear_tt_buffer(urb))
1634 /* Clear failed; let's hope things work anyway */
1635 qh->tt_buffer_dirty = 0;
1636 }
1637}
1638
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001639static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1640 int status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001641{
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001642 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001643 unsigned long spinflags;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001644 struct isp1760_qh *qh;
1645 struct isp1760_qtd *qtd;
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001646 int retval = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001647
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001648 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodin17d3e142011-07-20 03:13:46 +02001649 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1650 if (retval)
1651 goto out;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001652
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001653 qh = urb->ep->hcpriv;
1654 if (!qh) {
1655 retval = -EINVAL;
1656 goto out;
1657 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001658
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001659 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1660 if (qtd->urb == urb) {
Arvid Brodin74ad6022011-08-23 01:25:30 +02001661 dequeue_urb_from_qtd(hcd, qh, qtd);
Michael Grzeschik8cb22682012-04-18 09:58:23 +02001662 list_move(&qtd->qtd_list, &qh->qtd_list);
Arvid Brodin74ad6022011-08-23 01:25:30 +02001663 break;
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001664 }
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001665
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001666 urb->status = status;
1667 schedule_ptds(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001668
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001669out:
1670 spin_unlock_irqrestore(&priv->lock, spinflags);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001671 return retval;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001672}
1673
Arvid Brodin079cdb02011-05-20 00:17:34 +02001674static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1675 struct usb_host_endpoint *ep)
1676{
1677 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001678 unsigned long spinflags;
Arvid Brodinc64391f2011-11-23 18:13:02 +01001679 struct isp1760_qh *qh, *qh_iter;
1680 int i;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001681
1682 spin_lock_irqsave(&priv->lock, spinflags);
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001683
Arvid Brodin079cdb02011-05-20 00:17:34 +02001684 qh = ep->hcpriv;
1685 if (!qh)
1686 goto out;
1687
Arvid Brodinc64391f2011-11-23 18:13:02 +01001688 WARN_ON(!list_empty(&qh->qtd_list));
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001689
Arvid Brodinc64391f2011-11-23 18:13:02 +01001690 for (i = 0; i < QH_END; i++)
1691 list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
1692 if (qh_iter == qh) {
1693 list_del(&qh_iter->qh_list);
1694 i = QH_END;
1695 break;
1696 }
1697 qh_free(qh);
Arvid Brodin079cdb02011-05-20 00:17:34 +02001698 ep->hcpriv = NULL;
Arvid Brodin079cdb02011-05-20 00:17:34 +02001699
Arvid Brodind05b6ec2011-05-20 00:17:45 +02001700 schedule_ptds(hcd);
1701
Arvid Brodin079cdb02011-05-20 00:17:34 +02001702out:
1703 spin_unlock_irqrestore(&priv->lock, spinflags);
1704}
1705
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001706static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1707{
1708 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1709 u32 temp, status = 0;
1710 u32 mask;
1711 int retval = 1;
1712 unsigned long flags;
1713
Rafael J. Wysocki464ed182014-12-19 15:37:54 +01001714 /* if !PM, root hub timers won't get shut down ... */
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001715 if (!HC_IS_RUNNING(hcd->state))
1716 return 0;
1717
1718 /* init status to no-changes */
1719 buf[0] = 0;
1720 mask = PORT_CSC;
1721
1722 spin_lock_irqsave(&priv->lock, flags);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001723 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001724
1725 if (temp & PORT_OWNER) {
1726 if (temp & PORT_CSC) {
1727 temp &= ~PORT_CSC;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001728 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001729 goto done;
1730 }
1731 }
1732
1733 /*
1734 * Return status information even for ports with OWNER set.
Petr Mladek37ebb542014-09-19 17:32:23 +02001735 * Otherwise hub_wq wouldn't see the disconnect event when a
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001736 * high-speed device is switched over to the companion
1737 * controller by the user.
1738 */
1739
1740 if ((temp & mask) != 0
1741 || ((temp & PORT_RESUME) != 0
1742 && time_after_eq(jiffies,
1743 priv->reset_done))) {
1744 buf [0] |= 1 << (0 + 1);
1745 status = STS_PCD;
1746 }
1747 /* FIXME autosuspend idle root hubs */
1748done:
1749 spin_unlock_irqrestore(&priv->lock, flags);
1750 return status ? retval : 0;
1751}
1752
1753static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1754 struct usb_hub_descriptor *desc)
1755{
1756 int ports = HCS_N_PORTS(priv->hcs_params);
1757 u16 temp;
1758
Sergei Shtylyov1cf65632015-03-29 01:40:24 +03001759 desc->bDescriptorType = USB_DT_HUB;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001760 /* priv 1.0, 2.3.9 says 20ms max */
1761 desc->bPwrOn2PwrGood = 10;
1762 desc->bHubContrCurrent = 0;
1763
1764 desc->bNbrPorts = ports;
1765 temp = 1 + (ports / 8);
1766 desc->bDescLength = 7 + 2 * temp;
1767
Sarah Sharpda130512010-11-30 15:55:51 -08001768 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -07001769 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1770 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001771
1772 /* per-port overcurrent reporting */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001773 temp = HUB_CHAR_INDV_PORT_OCPM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001774 if (HCS_PPC(priv->hcs_params))
1775 /* per-port power control */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001776 temp |= HUB_CHAR_INDV_PORT_LPSM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001777 else
1778 /* no power switching */
Sergei Shtylyovcc581c12015-01-19 01:36:35 +03001779 temp |= HUB_CHAR_NO_LPSM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001780 desc->wHubCharacteristics = cpu_to_le16(temp);
1781}
1782
1783#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1784
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001785static int check_reset_complete(struct usb_hcd *hcd, int index,
1786 int port_status)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001787{
1788 if (!(port_status & PORT_CONNECT))
1789 return port_status;
1790
1791 /* if reset finished and it's still not enabled -- handoff */
1792 if (!(port_status & PORT_PE)) {
1793
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001794 dev_info(hcd->self.controller,
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001795 "port %d full speed --> companion\n",
1796 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001797
1798 port_status |= PORT_OWNER;
1799 port_status &= ~PORT_RWC_BITS;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001800 reg_write32(hcd->regs, HC_PORTSC1, port_status);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001801
1802 } else
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02001803 dev_info(hcd->self.controller, "port %d high speed\n",
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001804 index + 1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001805
1806 return port_status;
1807}
1808
1809static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1810 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1811{
1812 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1813 int ports = HCS_N_PORTS(priv->hcs_params);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001814 u32 temp, status;
1815 unsigned long flags;
1816 int retval = 0;
1817 unsigned selector;
1818
1819 /*
1820 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1821 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1822 * (track current state ourselves) ... blink for diagnostics,
1823 * power, "this is the one", etc. EHCI spec supports this.
1824 */
1825
1826 spin_lock_irqsave(&priv->lock, flags);
1827 switch (typeReq) {
1828 case ClearHubFeature:
1829 switch (wValue) {
1830 case C_HUB_LOCAL_POWER:
1831 case C_HUB_OVER_CURRENT:
1832 /* no hub-wide feature/status flags */
1833 break;
1834 default:
1835 goto error;
1836 }
1837 break;
1838 case ClearPortFeature:
1839 if (!wIndex || wIndex > ports)
1840 goto error;
1841 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001842 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001843
1844 /*
1845 * Even if OWNER is set, so the port is owned by the
Petr Mladek37ebb542014-09-19 17:32:23 +02001846 * companion controller, hub_wq needs to be able to clear
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001847 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -05001848 * USB_PORT_STAT_C_CONNECTION).
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001849 */
1850
1851 switch (wValue) {
1852 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001853 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001854 break;
1855 case USB_PORT_FEAT_C_ENABLE:
1856 /* XXX error? */
1857 break;
1858 case USB_PORT_FEAT_SUSPEND:
1859 if (temp & PORT_RESET)
1860 goto error;
1861
1862 if (temp & PORT_SUSPEND) {
1863 if ((temp & PORT_PE) == 0)
1864 goto error;
1865 /* resume signaling for 20 msec */
1866 temp &= ~(PORT_RWC_BITS);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001867 reg_write32(hcd->regs, HC_PORTSC1,
1868 temp | PORT_RESUME);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001869 priv->reset_done = jiffies +
Felipe Balbi59c99042015-02-13 15:04:06 -06001870 msecs_to_jiffies(USB_RESUME_TIMEOUT);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001871 }
1872 break;
1873 case USB_PORT_FEAT_C_SUSPEND:
1874 /* we auto-clear this feature */
1875 break;
1876 case USB_PORT_FEAT_POWER:
1877 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001878 reg_write32(hcd->regs, HC_PORTSC1,
1879 temp & ~PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001880 break;
1881 case USB_PORT_FEAT_C_CONNECTION:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001882 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001883 break;
1884 case USB_PORT_FEAT_C_OVER_CURRENT:
1885 /* XXX error ?*/
1886 break;
1887 case USB_PORT_FEAT_C_RESET:
1888 /* GetPortStatus clears reset */
1889 break;
1890 default:
1891 goto error;
1892 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001893 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001894 break;
1895 case GetHubDescriptor:
1896 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1897 buf);
1898 break;
1899 case GetHubStatus:
1900 /* no hub-wide feature/status flags */
1901 memset(buf, 0, 4);
1902 break;
1903 case GetPortStatus:
1904 if (!wIndex || wIndex > ports)
1905 goto error;
1906 wIndex--;
1907 status = 0;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001908 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001909
1910 /* wPortChange bits */
1911 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -05001912 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001913
1914
1915 /* whoever resumes must GetPortStatus to complete it!! */
1916 if (temp & PORT_RESUME) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001917 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001918
1919 /* Remote Wakeup received? */
1920 if (!priv->reset_done) {
1921 /* resume signaling for 20 msec */
1922 priv->reset_done = jiffies
1923 + msecs_to_jiffies(20);
1924 /* check the port again */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001925 mod_timer(&hcd->rh_timer, priv->reset_done);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001926 }
1927
1928 /* resume completed? */
1929 else if (time_after_eq(jiffies,
1930 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001931 status |= USB_PORT_STAT_C_SUSPEND << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001932 priv->reset_done = 0;
1933
1934 /* stop resume signaling */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001935 temp = reg_read32(hcd->regs, HC_PORTSC1);
1936 reg_write32(hcd->regs, HC_PORTSC1,
1937 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1938 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001939 PORT_RESUME, 0, 2000 /* 2msec */);
1940 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001941 dev_err(hcd->self.controller,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001942 "port %d resume error %d\n",
1943 wIndex + 1, retval);
1944 goto error;
1945 }
1946 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1947 }
1948 }
1949
1950 /* whoever resets must GetPortStatus to complete it!! */
1951 if ((temp & PORT_RESET)
1952 && time_after_eq(jiffies,
1953 priv->reset_done)) {
Alan Stern749da5f2010-03-04 17:05:08 -05001954 status |= USB_PORT_STAT_C_RESET << 16;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001955 priv->reset_done = 0;
1956
1957 /* force reset to complete */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001958 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001959 /* REVISIT: some hardware needs 550+ usec to clear
1960 * this bit; seems too long to spin routinely...
1961 */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001962 retval = handshake(hcd, HC_PORTSC1,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001963 PORT_RESET, 0, 750);
1964 if (retval != 0) {
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001965 dev_err(hcd->self.controller, "port %d reset error %d\n",
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001966 wIndex + 1, retval);
1967 goto error;
1968 }
1969
1970 /* see what we found out */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01001971 temp = check_reset_complete(hcd, wIndex,
1972 reg_read32(hcd->regs, HC_PORTSC1));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001973 }
1974 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001975 * Even if OWNER is set, there's no harm letting hub_wq
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001976 * see the wPortStatus values (they should all be 0 except
1977 * for PORT_POWER anyway).
1978 */
1979
1980 if (temp & PORT_OWNER)
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001981 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001982
1983 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -05001984 status |= USB_PORT_STAT_CONNECTION;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001985 /* status may be from integrated TT */
Arvid Brodin6bda21b2011-02-26 22:06:37 +01001986 status |= USB_PORT_STAT_HIGH_SPEED;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001987 }
1988 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -05001989 status |= USB_PORT_STAT_ENABLE;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001990 if (temp & (PORT_SUSPEND|PORT_RESUME))
Alan Stern749da5f2010-03-04 17:05:08 -05001991 status |= USB_PORT_STAT_SUSPEND;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001992 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -05001993 status |= USB_PORT_STAT_RESET;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001994 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -05001995 status |= USB_PORT_STAT_POWER;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001996
1997 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1998 break;
1999 case SetHubFeature:
2000 switch (wValue) {
2001 case C_HUB_LOCAL_POWER:
2002 case C_HUB_OVER_CURRENT:
2003 /* no hub-wide feature/status flags */
2004 break;
2005 default:
2006 goto error;
2007 }
2008 break;
2009 case SetPortFeature:
2010 selector = wIndex >> 8;
2011 wIndex &= 0xff;
2012 if (!wIndex || wIndex > ports)
2013 goto error;
2014 wIndex--;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002015 temp = reg_read32(hcd->regs, HC_PORTSC1);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002016 if (temp & PORT_OWNER)
2017 break;
2018
2019/* temp &= ~PORT_RWC_BITS; */
2020 switch (wValue) {
2021 case USB_PORT_FEAT_ENABLE:
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002022 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002023 break;
2024
2025 case USB_PORT_FEAT_SUSPEND:
2026 if ((temp & PORT_PE) == 0
2027 || (temp & PORT_RESET) != 0)
2028 goto error;
2029
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002030 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002031 break;
2032 case USB_PORT_FEAT_POWER:
2033 if (HCS_PPC(priv->hcs_params))
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002034 reg_write32(hcd->regs, HC_PORTSC1,
2035 temp | PORT_POWER);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002036 break;
2037 case USB_PORT_FEAT_RESET:
2038 if (temp & PORT_RESUME)
2039 goto error;
2040 /* line status bits may report this as low speed,
2041 * which can be fine if this root hub has a
2042 * transaction translator built in.
2043 */
2044 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2045 && PORT_USB11(temp)) {
2046 temp |= PORT_OWNER;
2047 } else {
2048 temp |= PORT_RESET;
2049 temp &= ~PORT_PE;
2050
2051 /*
2052 * caller must wait, then call GetPortStatus
2053 * usb 2.0 spec says 50 ms resets on root
2054 */
2055 priv->reset_done = jiffies +
2056 msecs_to_jiffies(50);
2057 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002058 reg_write32(hcd->regs, HC_PORTSC1, temp);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002059 break;
2060 default:
2061 goto error;
2062 }
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002063 reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002064 break;
2065
2066 default:
2067error:
2068 /* "stall" on error */
2069 retval = -EPIPE;
2070 }
2071 spin_unlock_irqrestore(&priv->lock, flags);
2072 return retval;
2073}
2074
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002075static int isp1760_get_frame(struct usb_hcd *hcd)
2076{
2077 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2078 u32 fr;
2079
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002080 fr = reg_read32(hcd->regs, HC_FRINDEX);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002081 return (fr >> 3) % priv->periodic_size;
2082}
2083
2084static void isp1760_stop(struct usb_hcd *hcd)
2085{
2086 struct isp1760_hcd *priv = hcd_to_priv(hcd);
Nate Case3faefc82008-06-17 11:11:38 -05002087 u32 temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002088
Arvid Brodin6d50c602011-08-21 08:29:26 +02002089 del_timer(&errata2_timer);
2090
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002091 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2092 NULL, 0);
2093 mdelay(20);
2094
2095 spin_lock_irq(&priv->lock);
Arvid Brodin6bda21b2011-02-26 22:06:37 +01002096 ehci_reset(hcd);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002097 /* Disable IRQ */
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002098 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2099 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002100 spin_unlock_irq(&priv->lock);
2101
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002102 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002103}
2104
2105static void isp1760_shutdown(struct usb_hcd *hcd)
2106{
Nate Case3faefc82008-06-17 11:11:38 -05002107 u32 command, temp;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002108
2109 isp1760_stop(hcd);
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002110 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2111 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002112
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002113 command = reg_read32(hcd->regs, HC_USBCMD);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002114 command &= ~CMD_RUN;
Arvid Brodinbedc0c32011-02-26 22:02:57 +01002115 reg_write32(hcd->regs, HC_USBCMD, command);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002116}
2117
Arvid Brodin74ad6022011-08-23 01:25:30 +02002118static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2119 struct usb_host_endpoint *ep)
2120{
2121 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2122 struct isp1760_qh *qh = ep->hcpriv;
2123 unsigned long spinflags;
2124
2125 if (!qh)
2126 return;
2127
2128 spin_lock_irqsave(&priv->lock, spinflags);
2129 qh->tt_buffer_dirty = 0;
2130 schedule_ptds(hcd);
2131 spin_unlock_irqrestore(&priv->lock, spinflags);
2132}
2133
2134
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002135static const struct hc_driver isp1760_hc_driver = {
2136 .description = "isp1760-hcd",
2137 .product_desc = "NXP ISP1760 USB Host Controller",
Laurent Pinchartcdd36e82015-01-21 00:55:54 +02002138 .hcd_priv_size = sizeof(struct isp1760_hcd *),
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002139 .irq = isp1760_irq,
2140 .flags = HCD_MEMORY | HCD_USB2,
2141 .reset = isp1760_hc_setup,
2142 .start = isp1760_run,
2143 .stop = isp1760_stop,
2144 .shutdown = isp1760_shutdown,
2145 .urb_enqueue = isp1760_urb_enqueue,
2146 .urb_dequeue = isp1760_urb_dequeue,
2147 .endpoint_disable = isp1760_endpoint_disable,
2148 .get_frame_number = isp1760_get_frame,
2149 .hub_status_data = isp1760_hub_status_data,
2150 .hub_control = isp1760_hub_control,
Arvid Brodin74ad6022011-08-23 01:25:30 +02002151 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002152};
2153
Laurent Pinchart5a6356a2015-01-21 00:55:49 +02002154int __init isp1760_init_kmem_once(void)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002155{
Michael Grzeschik94011ec2012-04-05 14:56:08 +02002156 urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002157 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2158 SLAB_MEM_SPREAD, NULL);
2159
2160 if (!urb_listitem_cachep)
2161 return -ENOMEM;
2162
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002163 qtd_cachep = kmem_cache_create("isp1760_qtd",
2164 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2165 SLAB_MEM_SPREAD, NULL);
2166
2167 if (!qtd_cachep)
2168 return -ENOMEM;
2169
2170 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2171 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2172
2173 if (!qh_cachep) {
2174 kmem_cache_destroy(qtd_cachep);
2175 return -ENOMEM;
2176 }
2177
2178 return 0;
2179}
2180
Laurent Pinchart5a6356a2015-01-21 00:55:49 +02002181void isp1760_deinit_kmem_cache(void)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002182{
2183 kmem_cache_destroy(qtd_cachep);
2184 kmem_cache_destroy(qh_cachep);
Arvid Brodin71a9f9d2011-04-26 21:48:30 +02002185 kmem_cache_destroy(urb_listitem_cachep);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002186}
2187
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002188int isp1760_hcd_register(struct isp1760_hcd *priv, void __iomem *regs,
2189 struct resource *mem, int irq, unsigned long irqflags,
Laurent Pinchart51714462015-01-21 00:55:59 +02002190 struct device *dev)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002191{
2192 struct usb_hcd *hcd;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002193 int ret;
2194
Kay Sievers0031a062008-05-02 06:02:41 +02002195 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002196 if (!hcd)
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +02002197 return -ENOMEM;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002198
Laurent Pinchartcdd36e82015-01-21 00:55:54 +02002199 *(struct isp1760_hcd **)hcd->hcd_priv = priv;
2200
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002201 priv->hcd = hcd;
Laurent Pinchart7eb42c62015-01-21 00:55:40 +02002202
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002203 init_memory(priv);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002204
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002205 hcd->irq = irq;
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002206 hcd->regs = regs;
Laurent Pinchart4942e002015-01-21 00:55:51 +02002207 hcd->rsrc_start = mem->start;
2208 hcd->rsrc_len = resource_size(mem);
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002209
Alan Stern074f9dd2015-01-29 15:05:04 -05002210 /* This driver doesn't support wakeup requests */
2211 hcd->cant_recv_wakeups = 1;
2212
Nate Casee6942d62008-05-21 16:28:20 -05002213 ret = usb_add_hcd(hcd, irq, irqflags);
2214 if (ret)
Laurent Pinchart10feee12015-01-21 00:55:52 +02002215 goto error;
Nate Casee6942d62008-05-21 16:28:20 -05002216
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002217 device_wakeup_enable(hcd->self.controller);
2218
Laurent Pinchartf0bdbb02015-01-21 00:55:47 +02002219 return 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002220
Laurent Pinchart10feee12015-01-21 00:55:52 +02002221error:
Laurent Pinchart30573752015-01-21 00:55:48 +02002222 usb_put_hcd(hcd);
Laurent Pinchart30573752015-01-21 00:55:48 +02002223 return ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +02002224}
2225
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002226void isp1760_hcd_unregister(struct isp1760_hcd *priv)
Laurent Pinchart10c73f02015-01-21 00:55:45 +02002227{
Laurent Pinchartd21daf12015-01-21 00:56:05 +02002228 if (!priv->hcd)
2229 return;
2230
Laurent Pinchart4b1a5772015-01-21 00:55:57 +02002231 usb_remove_hcd(priv->hcd);
2232 usb_put_hcd(priv->hcd);
Laurent Pinchart10c73f02015-01-21 00:55:45 +02002233}