Commit | Line | Data |
---|---|---|
db11e47d SS |
1 | /* |
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 | * | |
71a9f9d2 AB |
11 | * (c) 2011 Arvid Brodin <arvid.brodin@enea.com> |
12 | * | |
db11e47d SS |
13 | */ |
14 | #include <linux/module.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/list.h> | |
18 | #include <linux/usb.h> | |
27729aad | 19 | #include <linux/usb/hcd.h> |
db11e47d SS |
20 | #include <linux/debugfs.h> |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/io.h> | |
db8516f6 | 23 | #include <linux/mm.h> |
6d50c60e | 24 | #include <linux/timer.h> |
db11e47d | 25 | #include <asm/unaligned.h> |
db8516f6 | 26 | #include <asm/cacheflush.h> |
db11e47d | 27 | |
db11e47d SS |
28 | #include "isp1760-hcd.h" |
29 | ||
30 | static struct kmem_cache *qtd_cachep; | |
31 | static struct kmem_cache *qh_cachep; | |
71a9f9d2 | 32 | static struct kmem_cache *urb_listitem_cachep; |
db11e47d SS |
33 | |
34 | struct isp1760_hcd { | |
35 | u32 hcs_params; | |
36 | spinlock_t lock; | |
71a9f9d2 | 37 | struct slotinfo atl_slots[32]; |
d05b6ec0 | 38 | int atl_done_map; |
71a9f9d2 | 39 | struct slotinfo int_slots[32]; |
d05b6ec0 | 40 | int int_done_map; |
db11e47d | 41 | struct memory_chunk memory_pool[BLOCKS]; |
71a9f9d2 | 42 | struct list_head controlqhs, bulkqhs, interruptqhs; |
db11e47d SS |
43 | |
44 | /* periodic schedule support */ | |
45 | #define DEFAULT_I_TDPS 1024 | |
46 | unsigned periodic_size; | |
47 | unsigned i_thresh; | |
48 | unsigned long reset_done; | |
49 | unsigned long next_statechange; | |
3faefc88 | 50 | unsigned int devflags; |
db11e47d SS |
51 | }; |
52 | ||
53 | static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd) | |
54 | { | |
55 | return (struct isp1760_hcd *) (hcd->hcd_priv); | |
56 | } | |
db11e47d SS |
57 | |
58 | /* Section 2.2 Host Controller Capability Registers */ | |
59 | #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */ | |
60 | #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */ | |
61 | #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */ | |
62 | #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */ | |
63 | #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */ | |
64 | #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */ | |
65 | #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */ | |
66 | ||
67 | /* Section 2.3 Host Controller Operational Registers */ | |
68 | #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */ | |
69 | #define CMD_RESET (1<<1) /* reset HC not bus */ | |
70 | #define CMD_RUN (1<<0) /* start/stop HC */ | |
71 | #define STS_PCD (1<<2) /* port change detect */ | |
72 | #define FLAG_CF (1<<0) /* true: we'll support "high speed" */ | |
73 | ||
74 | #define PORT_OWNER (1<<13) /* true: companion hc owns this port */ | |
75 | #define PORT_POWER (1<<12) /* true: has power (see PPC) */ | |
76 | #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */ | |
77 | #define PORT_RESET (1<<8) /* reset port */ | |
78 | #define PORT_SUSPEND (1<<7) /* suspend port */ | |
79 | #define PORT_RESUME (1<<6) /* resume it */ | |
80 | #define PORT_PE (1<<2) /* port enable */ | |
81 | #define PORT_CSC (1<<1) /* connect status change */ | |
82 | #define PORT_CONNECT (1<<0) /* device connected */ | |
83 | #define PORT_RWC_BITS (PORT_CSC) | |
84 | ||
85 | struct isp1760_qtd { | |
db11e47d | 86 | u8 packet_type; |
db11e47d | 87 | void *data_buffer; |
a041d8e4 AB |
88 | u32 payload_addr; |
89 | ||
db11e47d SS |
90 | /* the rest is HCD-private */ |
91 | struct list_head qtd_list; | |
92 | struct urb *urb; | |
93 | size_t length; | |
71a9f9d2 AB |
94 | size_t actual_length; |
95 | ||
96 | /* QTD_ENQUEUED: waiting for transfer (inactive) */ | |
97 | /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */ | |
98 | /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only | |
99 | interrupt handler may touch this qtd! */ | |
100 | /* QTD_XFER_COMPLETE: payload has been transferred successfully */ | |
101 | /* QTD_RETIRE: transfer error/abort qtd */ | |
102 | #define QTD_ENQUEUED 0 | |
103 | #define QTD_PAYLOAD_ALLOC 1 | |
104 | #define QTD_XFER_STARTED 2 | |
105 | #define QTD_XFER_COMPLETE 3 | |
106 | #define QTD_RETIRE 4 | |
db11e47d | 107 | u32 status; |
db11e47d SS |
108 | }; |
109 | ||
71a9f9d2 | 110 | /* Queue head, one for each active endpoint */ |
db11e47d | 111 | struct isp1760_qh { |
71a9f9d2 | 112 | struct list_head qh_list; |
db11e47d | 113 | struct list_head qtd_list; |
db11e47d SS |
114 | u32 toggle; |
115 | u32 ping; | |
71a9f9d2 AB |
116 | int slot; |
117 | }; | |
118 | ||
119 | struct urb_listitem { | |
120 | struct list_head urb_list; | |
121 | struct urb *urb; | |
db11e47d SS |
122 | }; |
123 | ||
bedc0c31 AB |
124 | /* |
125 | * Access functions for isp176x registers (addresses 0..0x03FF). | |
126 | */ | |
127 | static u32 reg_read32(void __iomem *base, u32 reg) | |
db11e47d | 128 | { |
bedc0c31 | 129 | return readl(base + reg); |
db11e47d SS |
130 | } |
131 | ||
bedc0c31 | 132 | static void reg_write32(void __iomem *base, u32 reg, u32 val) |
db11e47d | 133 | { |
bedc0c31 | 134 | writel(val, base + reg); |
db11e47d SS |
135 | } |
136 | ||
137 | /* | |
bedc0c31 AB |
138 | * Access functions for isp176x memory (offset >= 0x0400). |
139 | * | |
140 | * bank_reads8() reads memory locations prefetched by an earlier write to | |
141 | * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi- | |
142 | * bank optimizations, you should use the more generic mem_reads8() below. | |
143 | * | |
144 | * For access to ptd memory, use the specialized ptd_read() and ptd_write() | |
145 | * below. | |
146 | * | |
147 | * These functions copy via MMIO data to/from the device. memcpy_{to|from}io() | |
db11e47d SS |
148 | * doesn't quite work because some people have to enforce 32-bit access |
149 | */ | |
bedc0c31 AB |
150 | static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr, |
151 | __u32 *dst, u32 bytes) | |
db11e47d | 152 | { |
bedc0c31 | 153 | __u32 __iomem *src; |
db11e47d | 154 | u32 val; |
bedc0c31 AB |
155 | __u8 *src_byteptr; |
156 | __u8 *dst_byteptr; | |
db11e47d | 157 | |
bedc0c31 | 158 | src = src_base + (bank_addr | src_offset); |
db11e47d | 159 | |
bedc0c31 AB |
160 | if (src_offset < PAYLOAD_OFFSET) { |
161 | while (bytes >= 4) { | |
162 | *dst = le32_to_cpu(__raw_readl(src)); | |
163 | bytes -= 4; | |
164 | src++; | |
165 | dst++; | |
166 | } | |
167 | } else { | |
168 | while (bytes >= 4) { | |
169 | *dst = __raw_readl(src); | |
170 | bytes -= 4; | |
171 | src++; | |
172 | dst++; | |
173 | } | |
db11e47d SS |
174 | } |
175 | ||
bedc0c31 | 176 | if (!bytes) |
db11e47d SS |
177 | return; |
178 | ||
179 | /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully | |
180 | * allocated. | |
181 | */ | |
bedc0c31 AB |
182 | if (src_offset < PAYLOAD_OFFSET) |
183 | val = le32_to_cpu(__raw_readl(src)); | |
184 | else | |
185 | val = __raw_readl(src); | |
186 | ||
187 | dst_byteptr = (void *) dst; | |
188 | src_byteptr = (void *) &val; | |
189 | while (bytes > 0) { | |
190 | *dst_byteptr = *src_byteptr; | |
191 | dst_byteptr++; | |
192 | src_byteptr++; | |
193 | bytes--; | |
db11e47d SS |
194 | } |
195 | } | |
196 | ||
bedc0c31 AB |
197 | static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst, |
198 | u32 bytes) | |
db11e47d | 199 | { |
bedc0c31 AB |
200 | reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0)); |
201 | ndelay(90); | |
202 | bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes); | |
203 | } | |
204 | ||
205 | static void mem_writes8(void __iomem *dst_base, u32 dst_offset, | |
206 | __u32 const *src, u32 bytes) | |
207 | { | |
208 | __u32 __iomem *dst; | |
209 | ||
210 | dst = dst_base + dst_offset; | |
211 | ||
212 | if (dst_offset < PAYLOAD_OFFSET) { | |
213 | while (bytes >= 4) { | |
214 | __raw_writel(cpu_to_le32(*src), dst); | |
215 | bytes -= 4; | |
216 | src++; | |
217 | dst++; | |
218 | } | |
219 | } else { | |
220 | while (bytes >= 4) { | |
221 | __raw_writel(*src, dst); | |
222 | bytes -= 4; | |
223 | src++; | |
224 | dst++; | |
225 | } | |
db11e47d SS |
226 | } |
227 | ||
bedc0c31 | 228 | if (!bytes) |
db11e47d | 229 | return; |
bedc0c31 AB |
230 | /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the |
231 | * extra bytes should not be read by the HW. | |
db11e47d SS |
232 | */ |
233 | ||
bedc0c31 AB |
234 | if (dst_offset < PAYLOAD_OFFSET) |
235 | __raw_writel(cpu_to_le32(*src), dst); | |
236 | else | |
237 | __raw_writel(*src, dst); | |
db11e47d SS |
238 | } |
239 | ||
bedc0c31 AB |
240 | /* |
241 | * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET, | |
242 | * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32. | |
243 | */ | |
244 | static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot, | |
245 | struct ptd *ptd) | |
246 | { | |
247 | reg_write32(base, HC_MEMORY_REG, | |
248 | ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd)); | |
249 | ndelay(90); | |
250 | bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0), | |
251 | (void *) ptd, sizeof(*ptd)); | |
252 | } | |
253 | ||
254 | static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot, | |
255 | struct ptd *ptd) | |
256 | { | |
257 | mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0), | |
258 | &ptd->dw1, 7*sizeof(ptd->dw1)); | |
259 | /* Make sure dw0 gets written last (after other dw's and after payload) | |
260 | since it contains the enable bit */ | |
261 | wmb(); | |
262 | mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0, | |
263 | sizeof(ptd->dw0)); | |
264 | } | |
265 | ||
266 | ||
db11e47d SS |
267 | /* memory management of the 60kb on the chip from 0x1000 to 0xffff */ |
268 | static void init_memory(struct isp1760_hcd *priv) | |
269 | { | |
a041d8e4 AB |
270 | int i, curr; |
271 | u32 payload_addr; | |
db11e47d | 272 | |
a041d8e4 | 273 | payload_addr = PAYLOAD_OFFSET; |
db11e47d | 274 | for (i = 0; i < BLOCK_1_NUM; i++) { |
a041d8e4 | 275 | priv->memory_pool[i].start = payload_addr; |
db11e47d SS |
276 | priv->memory_pool[i].size = BLOCK_1_SIZE; |
277 | priv->memory_pool[i].free = 1; | |
a041d8e4 | 278 | payload_addr += priv->memory_pool[i].size; |
db11e47d SS |
279 | } |
280 | ||
a041d8e4 AB |
281 | curr = i; |
282 | for (i = 0; i < BLOCK_2_NUM; i++) { | |
283 | priv->memory_pool[curr + i].start = payload_addr; | |
284 | priv->memory_pool[curr + i].size = BLOCK_2_SIZE; | |
285 | priv->memory_pool[curr + i].free = 1; | |
286 | payload_addr += priv->memory_pool[curr + i].size; | |
db11e47d SS |
287 | } |
288 | ||
a041d8e4 AB |
289 | curr = i; |
290 | for (i = 0; i < BLOCK_3_NUM; i++) { | |
291 | priv->memory_pool[curr + i].start = payload_addr; | |
292 | priv->memory_pool[curr + i].size = BLOCK_3_SIZE; | |
293 | priv->memory_pool[curr + i].free = 1; | |
294 | payload_addr += priv->memory_pool[curr + i].size; | |
db11e47d SS |
295 | } |
296 | ||
34537731 | 297 | WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE); |
db11e47d SS |
298 | } |
299 | ||
6bda21bc | 300 | static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd) |
db11e47d | 301 | { |
6bda21bc | 302 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
db11e47d SS |
303 | int i; |
304 | ||
34537731 | 305 | WARN_ON(qtd->payload_addr); |
a041d8e4 AB |
306 | |
307 | if (!qtd->length) | |
308 | return; | |
db11e47d SS |
309 | |
310 | for (i = 0; i < BLOCKS; i++) { | |
a041d8e4 | 311 | if (priv->memory_pool[i].size >= qtd->length && |
db11e47d | 312 | priv->memory_pool[i].free) { |
db11e47d | 313 | priv->memory_pool[i].free = 0; |
a041d8e4 AB |
314 | qtd->payload_addr = priv->memory_pool[i].start; |
315 | return; | |
db11e47d SS |
316 | } |
317 | } | |
db11e47d SS |
318 | } |
319 | ||
6bda21bc | 320 | static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd) |
db11e47d | 321 | { |
6bda21bc | 322 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
db11e47d SS |
323 | int i; |
324 | ||
a041d8e4 | 325 | if (!qtd->payload_addr) |
db11e47d SS |
326 | return; |
327 | ||
328 | for (i = 0; i < BLOCKS; i++) { | |
a041d8e4 | 329 | if (priv->memory_pool[i].start == qtd->payload_addr) { |
34537731 | 330 | WARN_ON(priv->memory_pool[i].free); |
db11e47d | 331 | priv->memory_pool[i].free = 1; |
a041d8e4 AB |
332 | qtd->payload_addr = 0; |
333 | return; | |
db11e47d SS |
334 | } |
335 | } | |
336 | ||
6bda21bc AB |
337 | dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n", |
338 | __func__, qtd->payload_addr); | |
71a9f9d2 AB |
339 | WARN_ON(1); |
340 | qtd->payload_addr = 0; | |
db11e47d SS |
341 | } |
342 | ||
bedc0c31 | 343 | static int handshake(struct usb_hcd *hcd, u32 reg, |
db11e47d SS |
344 | u32 mask, u32 done, int usec) |
345 | { | |
346 | u32 result; | |
347 | ||
348 | do { | |
bedc0c31 | 349 | result = reg_read32(hcd->regs, reg); |
db11e47d SS |
350 | if (result == ~0) |
351 | return -ENODEV; | |
352 | result &= mask; | |
353 | if (result == done) | |
354 | return 0; | |
355 | udelay(1); | |
356 | usec--; | |
357 | } while (usec > 0); | |
358 | return -ETIMEDOUT; | |
359 | } | |
360 | ||
361 | /* reset a non-running (STS_HALT == 1) controller */ | |
6bda21bc | 362 | static int ehci_reset(struct usb_hcd *hcd) |
db11e47d SS |
363 | { |
364 | int retval; | |
6bda21bc AB |
365 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
366 | ||
bedc0c31 | 367 | u32 command = reg_read32(hcd->regs, HC_USBCMD); |
db11e47d SS |
368 | |
369 | command |= CMD_RESET; | |
bedc0c31 | 370 | reg_write32(hcd->regs, HC_USBCMD, command); |
db11e47d SS |
371 | hcd->state = HC_STATE_HALT; |
372 | priv->next_statechange = jiffies; | |
bedc0c31 | 373 | retval = handshake(hcd, HC_USBCMD, |
db11e47d SS |
374 | CMD_RESET, 0, 250 * 1000); |
375 | return retval; | |
376 | } | |
377 | ||
71a9f9d2 | 378 | static struct isp1760_qh *qh_alloc(gfp_t flags) |
db11e47d SS |
379 | { |
380 | struct isp1760_qh *qh; | |
381 | ||
382 | qh = kmem_cache_zalloc(qh_cachep, flags); | |
383 | if (!qh) | |
71a9f9d2 | 384 | return NULL; |
db11e47d | 385 | |
71a9f9d2 | 386 | INIT_LIST_HEAD(&qh->qh_list); |
db11e47d | 387 | INIT_LIST_HEAD(&qh->qtd_list); |
71a9f9d2 AB |
388 | qh->slot = -1; |
389 | ||
db11e47d SS |
390 | return qh; |
391 | } | |
392 | ||
71a9f9d2 AB |
393 | static void qh_free(struct isp1760_qh *qh) |
394 | { | |
395 | WARN_ON(!list_empty(&qh->qtd_list)); | |
396 | WARN_ON(qh->slot > -1); | |
397 | kmem_cache_free(qh_cachep, qh); | |
398 | } | |
db11e47d SS |
399 | |
400 | /* one-time init, only for memory state */ | |
401 | static int priv_init(struct usb_hcd *hcd) | |
402 | { | |
403 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
404 | u32 hcc_params; | |
405 | ||
406 | spin_lock_init(&priv->lock); | |
407 | ||
71a9f9d2 AB |
408 | INIT_LIST_HEAD(&priv->interruptqhs); |
409 | INIT_LIST_HEAD(&priv->controlqhs); | |
410 | INIT_LIST_HEAD(&priv->bulkqhs); | |
411 | ||
db11e47d SS |
412 | /* |
413 | * hw default: 1K periodic list heads, one per frame. | |
414 | * periodic_size can shrink by USBCMD update if hcc_params allows. | |
415 | */ | |
416 | priv->periodic_size = DEFAULT_I_TDPS; | |
417 | ||
418 | /* controllers may cache some of the periodic schedule ... */ | |
bedc0c31 | 419 | hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS); |
db11e47d SS |
420 | /* full frame cache */ |
421 | if (HCC_ISOC_CACHE(hcc_params)) | |
422 | priv->i_thresh = 8; | |
423 | else /* N microframes cached */ | |
424 | priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params); | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static int isp1760_hc_setup(struct usb_hcd *hcd) | |
430 | { | |
431 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
432 | int result; | |
3faefc88 NC |
433 | u32 scratch, hwmode; |
434 | ||
435 | /* Setup HW Mode Control: This assumes a level active-low interrupt */ | |
436 | hwmode = HW_DATA_BUS_32BIT; | |
437 | ||
438 | if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) | |
439 | hwmode &= ~HW_DATA_BUS_32BIT; | |
440 | if (priv->devflags & ISP1760_FLAG_ANALOG_OC) | |
441 | hwmode |= HW_ANA_DIGI_OC; | |
442 | if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH) | |
443 | hwmode |= HW_DACK_POL_HIGH; | |
444 | if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH) | |
445 | hwmode |= HW_DREQ_POL_HIGH; | |
9da69c60 MH |
446 | if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH) |
447 | hwmode |= HW_INTR_HIGH_ACT; | |
448 | if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG) | |
449 | hwmode |= HW_INTR_EDGE_TRIG; | |
3faefc88 NC |
450 | |
451 | /* | |
452 | * We have to set this first in case we're in 16-bit mode. | |
453 | * Write it twice to ensure correct upper bits if switching | |
454 | * to 16-bit mode. | |
455 | */ | |
bedc0c31 AB |
456 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode); |
457 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode); | |
db11e47d | 458 | |
bedc0c31 | 459 | reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe); |
3faefc88 | 460 | /* Change bus pattern */ |
bedc0c31 AB |
461 | scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG); |
462 | scratch = reg_read32(hcd->regs, HC_SCRATCH_REG); | |
db11e47d | 463 | if (scratch != 0xdeadbabe) { |
6bda21bc | 464 | dev_err(hcd->self.controller, "Scratch test failed.\n"); |
db11e47d SS |
465 | return -ENODEV; |
466 | } | |
467 | ||
468 | /* pre reset */ | |
71a9f9d2 AB |
469 | reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0); |
470 | reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE); | |
471 | reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE); | |
472 | reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE); | |
db11e47d SS |
473 | |
474 | /* reset */ | |
bedc0c31 | 475 | reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL); |
db11e47d SS |
476 | mdelay(100); |
477 | ||
bedc0c31 | 478 | reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC); |
db11e47d SS |
479 | mdelay(100); |
480 | ||
6bda21bc | 481 | result = ehci_reset(hcd); |
db11e47d SS |
482 | if (result) |
483 | return result; | |
484 | ||
485 | /* Step 11 passed */ | |
486 | ||
6bda21bc | 487 | dev_info(hcd->self.controller, "bus width: %d, oc: %s\n", |
3faefc88 NC |
488 | (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ? |
489 | 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ? | |
490 | "analog" : "digital"); | |
db11e47d SS |
491 | |
492 | /* ATL reset */ | |
bedc0c31 | 493 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET); |
db11e47d | 494 | mdelay(10); |
bedc0c31 | 495 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode); |
db11e47d | 496 | |
bedc0c31 | 497 | reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK); |
3faefc88 NC |
498 | |
499 | /* | |
500 | * PORT 1 Control register of the ISP1760 is the OTG control | |
42c65396 TH |
501 | * register on ISP1761. Since there is no OTG or device controller |
502 | * support in this driver, we use port 1 as a "normal" USB host port on | |
503 | * both chips. | |
3faefc88 | 504 | */ |
bedc0c31 | 505 | reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2); |
42c65396 | 506 | mdelay(10); |
db11e47d | 507 | |
bedc0c31 | 508 | priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS); |
db11e47d SS |
509 | |
510 | return priv_init(hcd); | |
511 | } | |
512 | ||
db11e47d SS |
513 | static u32 base_to_chip(u32 base) |
514 | { | |
515 | return ((base - 0x400) >> 3); | |
516 | } | |
517 | ||
7adc14b1 AB |
518 | static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh) |
519 | { | |
520 | struct urb *urb; | |
521 | ||
522 | if (list_is_last(&qtd->qtd_list, &qh->qtd_list)) | |
523 | return 1; | |
524 | ||
525 | urb = qtd->urb; | |
526 | qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list); | |
527 | return (qtd->urb != urb); | |
528 | } | |
529 | ||
71a9f9d2 AB |
530 | /* magic numbers that can affect system performance */ |
531 | #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */ | |
532 | #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */ | |
533 | #define EHCI_TUNE_RL_TT 0 | |
534 | #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */ | |
535 | #define EHCI_TUNE_MULT_TT 1 | |
536 | #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */ | |
537 | ||
538 | static void create_ptd_atl(struct isp1760_qh *qh, | |
a041d8e4 | 539 | struct isp1760_qtd *qtd, struct ptd *ptd) |
db11e47d | 540 | { |
db11e47d SS |
541 | u32 maxpacket; |
542 | u32 multi; | |
db11e47d SS |
543 | u32 rl = RL_COUNTER; |
544 | u32 nak = NAK_COUNTER; | |
545 | ||
bedc0c31 AB |
546 | memset(ptd, 0, sizeof(*ptd)); |
547 | ||
db11e47d | 548 | /* according to 3.6.2, max packet len can not be > 0x400 */ |
a041d8e4 AB |
549 | maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe, |
550 | usb_pipeout(qtd->urb->pipe)); | |
db11e47d SS |
551 | multi = 1 + ((maxpacket >> 11) & 0x3); |
552 | maxpacket &= 0x7ff; | |
553 | ||
554 | /* DW0 */ | |
71a9f9d2 AB |
555 | ptd->dw0 = DW0_VALID_BIT; |
556 | ptd->dw0 |= TO_DW0_LENGTH(qtd->length); | |
557 | ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket); | |
558 | ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe)); | |
db11e47d SS |
559 | |
560 | /* DW1 */ | |
a041d8e4 | 561 | ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1; |
71a9f9d2 AB |
562 | ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe)); |
563 | ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type); | |
db11e47d | 564 | |
a041d8e4 | 565 | if (usb_pipebulk(qtd->urb->pipe)) |
71a9f9d2 | 566 | ptd->dw1 |= DW1_TRANS_BULK; |
a041d8e4 | 567 | else if (usb_pipeint(qtd->urb->pipe)) |
71a9f9d2 | 568 | ptd->dw1 |= DW1_TRANS_INT; |
db11e47d | 569 | |
a041d8e4 | 570 | if (qtd->urb->dev->speed != USB_SPEED_HIGH) { |
db11e47d SS |
571 | /* split transaction */ |
572 | ||
71a9f9d2 | 573 | ptd->dw1 |= DW1_TRANS_SPLIT; |
a041d8e4 | 574 | if (qtd->urb->dev->speed == USB_SPEED_LOW) |
71a9f9d2 | 575 | ptd->dw1 |= DW1_SE_USB_LOSPEED; |
db11e47d | 576 | |
71a9f9d2 AB |
577 | ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport); |
578 | ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum); | |
db11e47d SS |
579 | |
580 | /* SE bit for Split INT transfers */ | |
a041d8e4 AB |
581 | if (usb_pipeint(qtd->urb->pipe) && |
582 | (qtd->urb->dev->speed == USB_SPEED_LOW)) | |
bedc0c31 | 583 | ptd->dw1 |= 2 << 16; |
db11e47d | 584 | |
db11e47d SS |
585 | rl = 0; |
586 | nak = 0; | |
587 | } else { | |
71a9f9d2 | 588 | ptd->dw0 |= TO_DW0_MULTI(multi); |
a041d8e4 AB |
589 | if (usb_pipecontrol(qtd->urb->pipe) || |
590 | usb_pipebulk(qtd->urb->pipe)) | |
71a9f9d2 | 591 | ptd->dw3 |= TO_DW3_PING(qh->ping); |
db11e47d SS |
592 | } |
593 | /* DW2 */ | |
bedc0c31 | 594 | ptd->dw2 = 0; |
71a9f9d2 AB |
595 | ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr)); |
596 | ptd->dw2 |= TO_DW2_RL(rl); | |
db11e47d SS |
597 | |
598 | /* DW3 */ | |
71a9f9d2 AB |
599 | ptd->dw3 |= TO_DW3_NAKCOUNT(nak); |
600 | ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle); | |
7adc14b1 AB |
601 | if (usb_pipecontrol(qtd->urb->pipe)) { |
602 | if (qtd->data_buffer == qtd->urb->setup_packet) | |
71a9f9d2 | 603 | ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1); |
7adc14b1 | 604 | else if (last_qtd_of_urb(qtd, qh)) |
71a9f9d2 | 605 | ptd->dw3 |= TO_DW3_DATA_TOGGLE(1); |
7adc14b1 | 606 | } |
db11e47d | 607 | |
71a9f9d2 | 608 | ptd->dw3 |= DW3_ACTIVE_BIT; |
db11e47d | 609 | /* Cerr */ |
71a9f9d2 | 610 | ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER); |
db11e47d SS |
611 | } |
612 | ||
6bda21bc | 613 | static void transform_add_int(struct isp1760_qh *qh, |
a041d8e4 | 614 | struct isp1760_qtd *qtd, struct ptd *ptd) |
db11e47d | 615 | { |
65f1b525 | 616 | u32 usof; |
db11e47d SS |
617 | u32 period; |
618 | ||
65f1b525 AB |
619 | /* |
620 | * Most of this is guessing. ISP1761 datasheet is quite unclear, and | |
621 | * the algorithm from the original Philips driver code, which was | |
622 | * pretty much used in this driver before as well, is quite horrendous | |
623 | * and, i believe, incorrect. The code below follows the datasheet and | |
624 | * USB2.0 spec as far as I can tell, and plug/unplug seems to be much | |
625 | * more reliable this way (fingers crossed...). | |
626 | */ | |
db11e47d | 627 | |
65f1b525 AB |
628 | if (qtd->urb->dev->speed == USB_SPEED_HIGH) { |
629 | /* urb->interval is in units of microframes (1/8 ms) */ | |
630 | period = qtd->urb->interval >> 3; | |
631 | ||
632 | if (qtd->urb->interval > 4) | |
633 | usof = 0x01; /* One bit set => | |
634 | interval 1 ms * uFrame-match */ | |
635 | else if (qtd->urb->interval > 2) | |
636 | usof = 0x22; /* Two bits set => interval 1/2 ms */ | |
637 | else if (qtd->urb->interval > 1) | |
638 | usof = 0x55; /* Four bits set => interval 1/4 ms */ | |
db11e47d | 639 | else |
65f1b525 | 640 | usof = 0xff; /* All bits set => interval 1/8 ms */ |
db11e47d | 641 | } else { |
65f1b525 AB |
642 | /* urb->interval is in units of frames (1 ms) */ |
643 | period = qtd->urb->interval; | |
644 | usof = 0x0f; /* Execute Start Split on any of the | |
645 | four first uFrames */ | |
646 | ||
647 | /* | |
648 | * First 8 bits in dw5 is uSCS and "specifies which uSOF the | |
649 | * complete split needs to be sent. Valid only for IN." Also, | |
650 | * "All bits can be set to one for every transfer." (p 82, | |
651 | * ISP1761 data sheet.) 0x1c is from Philips driver. Where did | |
652 | * that number come from? 0xff seems to work fine... | |
653 | */ | |
654 | /* ptd->dw5 = 0x1c; */ | |
655 | ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */ | |
db11e47d SS |
656 | } |
657 | ||
65f1b525 AB |
658 | period = period >> 1;/* Ensure equal or shorter period than requested */ |
659 | period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */ | |
660 | ||
bedc0c31 AB |
661 | ptd->dw2 |= period; |
662 | ptd->dw4 = usof; | |
db11e47d SS |
663 | } |
664 | ||
71a9f9d2 | 665 | static void create_ptd_int(struct isp1760_qh *qh, |
a041d8e4 | 666 | struct isp1760_qtd *qtd, struct ptd *ptd) |
db11e47d | 667 | { |
71a9f9d2 | 668 | create_ptd_atl(qh, qtd, ptd); |
6bda21bc | 669 | transform_add_int(qh, qtd, ptd); |
db11e47d SS |
670 | } |
671 | ||
6bda21bc | 672 | static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb) |
db11e47d SS |
673 | __releases(priv->lock) |
674 | __acquires(priv->lock) | |
675 | { | |
6bda21bc AB |
676 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
677 | ||
db11e47d | 678 | if (!urb->unlinked) { |
6bda21bc AB |
679 | if (urb->status == -EINPROGRESS) |
680 | urb->status = 0; | |
db11e47d SS |
681 | } |
682 | ||
db8516f6 CM |
683 | if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) { |
684 | void *ptr; | |
685 | for (ptr = urb->transfer_buffer; | |
686 | ptr < urb->transfer_buffer + urb->transfer_buffer_length; | |
687 | ptr += PAGE_SIZE) | |
688 | flush_dcache_page(virt_to_page(ptr)); | |
689 | } | |
690 | ||
db11e47d | 691 | /* complete() can reenter this HCD */ |
6bda21bc | 692 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
db11e47d | 693 | spin_unlock(&priv->lock); |
6bda21bc | 694 | usb_hcd_giveback_urb(hcd, urb, urb->status); |
db11e47d SS |
695 | spin_lock(&priv->lock); |
696 | } | |
697 | ||
34537731 AB |
698 | static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb, |
699 | u8 packet_type) | |
db11e47d | 700 | { |
34537731 AB |
701 | struct isp1760_qtd *qtd; |
702 | ||
703 | qtd = kmem_cache_zalloc(qtd_cachep, flags); | |
704 | if (!qtd) | |
705 | return NULL; | |
706 | ||
707 | INIT_LIST_HEAD(&qtd->qtd_list); | |
708 | qtd->urb = urb; | |
709 | qtd->packet_type = packet_type; | |
71a9f9d2 AB |
710 | qtd->status = QTD_ENQUEUED; |
711 | qtd->actual_length = 0; | |
34537731 AB |
712 | |
713 | return qtd; | |
714 | } | |
715 | ||
716 | static void qtd_free(struct isp1760_qtd *qtd) | |
717 | { | |
718 | WARN_ON(qtd->payload_addr); | |
db11e47d SS |
719 | kmem_cache_free(qtd_cachep, qtd); |
720 | } | |
721 | ||
71a9f9d2 AB |
722 | static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot, |
723 | struct slotinfo *slots, struct isp1760_qtd *qtd, | |
724 | struct isp1760_qh *qh, struct ptd *ptd) | |
db11e47d | 725 | { |
71a9f9d2 | 726 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
d05b6ec0 AB |
727 | int skip_map; |
728 | ||
71a9f9d2 AB |
729 | WARN_ON((slot < 0) || (slot > 31)); |
730 | WARN_ON(qtd->length && !qtd->payload_addr); | |
731 | WARN_ON(slots[slot].qtd); | |
732 | WARN_ON(slots[slot].qh); | |
733 | WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC); | |
734 | ||
d05b6ec0 AB |
735 | /* Make sure done map has not triggered from some unlinked transfer */ |
736 | if (ptd_offset == ATL_PTD_OFFSET) { | |
737 | priv->atl_done_map |= reg_read32(hcd->regs, | |
738 | HC_ATL_PTD_DONEMAP_REG); | |
6477acc0 AB |
739 | priv->atl_done_map &= ~(1 << slot); |
740 | } else { | |
741 | priv->int_done_map |= reg_read32(hcd->regs, | |
742 | HC_INT_PTD_DONEMAP_REG); | |
743 | priv->int_done_map &= ~(1 << slot); | |
744 | } | |
d05b6ec0 | 745 | |
6477acc0 AB |
746 | qh->slot = slot; |
747 | qtd->status = QTD_XFER_STARTED; | |
748 | slots[slot].timestamp = jiffies; | |
749 | slots[slot].qtd = qtd; | |
750 | slots[slot].qh = qh; | |
751 | ptd_write(hcd->regs, ptd_offset, slot, ptd); | |
752 | ||
753 | if (ptd_offset == ATL_PTD_OFFSET) { | |
d05b6ec0 AB |
754 | skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG); |
755 | skip_map &= ~(1 << qh->slot); | |
756 | reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map); | |
757 | } else { | |
d05b6ec0 AB |
758 | skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG); |
759 | skip_map &= ~(1 << qh->slot); | |
760 | reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map); | |
761 | } | |
db11e47d SS |
762 | } |
763 | ||
71a9f9d2 | 764 | static int is_short_bulk(struct isp1760_qtd *qtd) |
db11e47d | 765 | { |
71a9f9d2 AB |
766 | return (usb_pipebulk(qtd->urb->pipe) && |
767 | (qtd->actual_length < qtd->length)); | |
db11e47d SS |
768 | } |
769 | ||
71a9f9d2 AB |
770 | static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh, |
771 | struct list_head *urb_list) | |
db11e47d | 772 | { |
71a9f9d2 AB |
773 | int last_qtd; |
774 | struct isp1760_qtd *qtd, *qtd_next; | |
775 | struct urb_listitem *urb_listitem; | |
db11e47d | 776 | |
71a9f9d2 AB |
777 | list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) { |
778 | if (qtd->status < QTD_XFER_COMPLETE) | |
779 | break; | |
db11e47d | 780 | |
38679b72 | 781 | last_qtd = last_qtd_of_urb(qtd, qh); |
71a9f9d2 AB |
782 | |
783 | if ((!last_qtd) && (qtd->status == QTD_RETIRE)) | |
784 | qtd_next->status = QTD_RETIRE; | |
785 | ||
786 | if (qtd->status == QTD_XFER_COMPLETE) { | |
787 | if (qtd->actual_length) { | |
788 | switch (qtd->packet_type) { | |
789 | case IN_PID: | |
790 | mem_reads8(hcd->regs, qtd->payload_addr, | |
791 | qtd->data_buffer, | |
792 | qtd->actual_length); | |
793 | /* Fall through (?) */ | |
794 | case OUT_PID: | |
795 | qtd->urb->actual_length += | |
796 | qtd->actual_length; | |
797 | /* Fall through ... */ | |
798 | case SETUP_PID: | |
799 | break; | |
800 | } | |
801 | } | |
db11e47d | 802 | |
71a9f9d2 AB |
803 | if (is_short_bulk(qtd)) { |
804 | if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK) | |
805 | qtd->urb->status = -EREMOTEIO; | |
806 | if (!last_qtd) | |
807 | qtd_next->status = QTD_RETIRE; | |
808 | } | |
809 | } | |
db11e47d | 810 | |
71a9f9d2 AB |
811 | if (qtd->payload_addr) |
812 | free_mem(hcd, qtd); | |
db11e47d | 813 | |
71a9f9d2 AB |
814 | if (last_qtd) { |
815 | if ((qtd->status == QTD_RETIRE) && | |
816 | (qtd->urb->status == -EINPROGRESS)) | |
817 | qtd->urb->status = -EPIPE; | |
818 | /* Defer calling of urb_done() since it releases lock */ | |
819 | urb_listitem = kmem_cache_zalloc(urb_listitem_cachep, | |
820 | GFP_ATOMIC); | |
821 | if (unlikely(!urb_listitem)) | |
38679b72 | 822 | break; /* Try again on next call */ |
71a9f9d2 AB |
823 | urb_listitem->urb = qtd->urb; |
824 | list_add_tail(&urb_listitem->urb_list, urb_list); | |
825 | } | |
847ed3e8 | 826 | |
71a9f9d2 AB |
827 | list_del(&qtd->qtd_list); |
828 | qtd_free(qtd); | |
829 | } | |
830 | } | |
3f02a957 | 831 | |
71a9f9d2 AB |
832 | #define ENQUEUE_DEPTH 2 |
833 | static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh) | |
834 | { | |
835 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
836 | int ptd_offset; | |
837 | struct slotinfo *slots; | |
838 | int curr_slot, free_slot; | |
839 | int n; | |
840 | struct ptd ptd; | |
841 | struct isp1760_qtd *qtd; | |
db11e47d | 842 | |
71a9f9d2 AB |
843 | if (unlikely(list_empty(&qh->qtd_list))) { |
844 | WARN_ON(1); | |
845 | return; | |
846 | } | |
db11e47d | 847 | |
71a9f9d2 AB |
848 | if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd, |
849 | qtd_list)->urb->pipe)) { | |
850 | ptd_offset = INT_PTD_OFFSET; | |
851 | slots = priv->int_slots; | |
852 | } else { | |
853 | ptd_offset = ATL_PTD_OFFSET; | |
854 | slots = priv->atl_slots; | |
855 | } | |
db11e47d | 856 | |
71a9f9d2 AB |
857 | free_slot = -1; |
858 | for (curr_slot = 0; curr_slot < 32; curr_slot++) { | |
859 | if ((free_slot == -1) && (slots[curr_slot].qtd == NULL)) | |
860 | free_slot = curr_slot; | |
861 | if (slots[curr_slot].qh == qh) | |
862 | break; | |
863 | } | |
db11e47d | 864 | |
71a9f9d2 AB |
865 | n = 0; |
866 | list_for_each_entry(qtd, &qh->qtd_list, qtd_list) { | |
867 | if (qtd->status == QTD_ENQUEUED) { | |
868 | WARN_ON(qtd->payload_addr); | |
869 | alloc_mem(hcd, qtd); | |
870 | if ((qtd->length) && (!qtd->payload_addr)) | |
871 | break; | |
db11e47d | 872 | |
71a9f9d2 AB |
873 | if ((qtd->length) && |
874 | ((qtd->packet_type == SETUP_PID) || | |
875 | (qtd->packet_type == OUT_PID))) { | |
876 | mem_writes8(hcd->regs, qtd->payload_addr, | |
877 | qtd->data_buffer, qtd->length); | |
878 | } | |
db11e47d | 879 | |
71a9f9d2 | 880 | qtd->status = QTD_PAYLOAD_ALLOC; |
db11e47d SS |
881 | } |
882 | ||
71a9f9d2 AB |
883 | if (qtd->status == QTD_PAYLOAD_ALLOC) { |
884 | /* | |
885 | if ((curr_slot > 31) && (free_slot == -1)) | |
886 | dev_dbg(hcd->self.controller, "%s: No slot " | |
887 | "available for transfer\n", __func__); | |
888 | */ | |
889 | /* Start xfer for this endpoint if not already done */ | |
890 | if ((curr_slot > 31) && (free_slot > -1)) { | |
891 | if (usb_pipeint(qtd->urb->pipe)) | |
892 | create_ptd_int(qh, qtd, &ptd); | |
893 | else | |
894 | create_ptd_atl(qh, qtd, &ptd); | |
895 | ||
896 | start_bus_transfer(hcd, ptd_offset, free_slot, | |
897 | slots, qtd, qh, &ptd); | |
898 | curr_slot = free_slot; | |
899 | } | |
db11e47d | 900 | |
71a9f9d2 AB |
901 | n++; |
902 | if (n >= ENQUEUE_DEPTH) | |
903 | break; | |
904 | } | |
905 | } | |
906 | } | |
db11e47d | 907 | |
71a9f9d2 AB |
908 | void schedule_ptds(struct usb_hcd *hcd) |
909 | { | |
910 | struct isp1760_hcd *priv; | |
911 | struct isp1760_qh *qh, *qh_next; | |
912 | struct list_head *ep_queue; | |
913 | struct usb_host_endpoint *ep; | |
914 | LIST_HEAD(urb_list); | |
915 | struct urb_listitem *urb_listitem, *urb_listitem_next; | |
916 | ||
917 | if (!hcd) { | |
918 | WARN_ON(1); | |
919 | return; | |
920 | } | |
db11e47d | 921 | |
71a9f9d2 | 922 | priv = hcd_to_priv(hcd); |
db11e47d | 923 | |
71a9f9d2 AB |
924 | /* |
925 | * check finished/retired xfers, transfer payloads, call urb_done() | |
926 | */ | |
927 | ep_queue = &priv->interruptqhs; | |
928 | while (ep_queue) { | |
929 | list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) { | |
930 | ep = list_entry(qh->qtd_list.next, struct isp1760_qtd, | |
931 | qtd_list)->urb->ep; | |
932 | collect_qtds(hcd, qh, &urb_list); | |
933 | if (list_empty(&qh->qtd_list)) { | |
934 | list_del(&qh->qh_list); | |
935 | if (ep->hcpriv == NULL) { | |
936 | /* Endpoint has been disabled, so we | |
937 | can free the associated queue head. */ | |
938 | qh_free(qh); | |
939 | } | |
db11e47d SS |
940 | } |
941 | } | |
942 | ||
71a9f9d2 AB |
943 | if (ep_queue == &priv->interruptqhs) |
944 | ep_queue = &priv->controlqhs; | |
945 | else if (ep_queue == &priv->controlqhs) | |
946 | ep_queue = &priv->bulkqhs; | |
947 | else | |
948 | ep_queue = NULL; | |
949 | } | |
db11e47d | 950 | |
71a9f9d2 AB |
951 | list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list, |
952 | urb_list) { | |
953 | isp1760_urb_done(hcd, urb_listitem->urb); | |
954 | kmem_cache_free(urb_listitem_cachep, urb_listitem); | |
955 | } | |
db11e47d | 956 | |
71a9f9d2 AB |
957 | /* |
958 | * Schedule packets for transfer. | |
959 | * | |
960 | * According to USB2.0 specification: | |
961 | * | |
962 | * 1st prio: interrupt xfers, up to 80 % of bandwidth | |
963 | * 2nd prio: control xfers | |
964 | * 3rd prio: bulk xfers | |
965 | * | |
966 | * ... but let's use a simpler scheme here (mostly because ISP1761 doc | |
967 | * is very unclear on how to prioritize traffic): | |
968 | * | |
969 | * 1) Enqueue any queued control transfers, as long as payload chip mem | |
970 | * and PTD ATL slots are available. | |
971 | * 2) Enqueue any queued INT transfers, as long as payload chip mem | |
972 | * and PTD INT slots are available. | |
973 | * 3) Enqueue any queued bulk transfers, as long as payload chip mem | |
974 | * and PTD ATL slots are available. | |
975 | * | |
976 | * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between | |
977 | * conservation of chip mem and performance. | |
978 | * | |
979 | * I'm sure this scheme could be improved upon! | |
980 | */ | |
981 | ep_queue = &priv->controlqhs; | |
982 | while (ep_queue) { | |
983 | list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) | |
984 | enqueue_qtds(hcd, qh); | |
985 | ||
986 | if (ep_queue == &priv->controlqhs) | |
987 | ep_queue = &priv->interruptqhs; | |
988 | else if (ep_queue == &priv->interruptqhs) | |
989 | ep_queue = &priv->bulkqhs; | |
990 | else | |
991 | ep_queue = NULL; | |
992 | } | |
993 | } | |
db11e47d | 994 | |
71a9f9d2 AB |
995 | #define PTD_STATE_QTD_DONE 1 |
996 | #define PTD_STATE_QTD_RELOAD 2 | |
997 | #define PTD_STATE_URB_RETIRE 3 | |
db11e47d | 998 | |
71a9f9d2 AB |
999 | static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd, |
1000 | struct urb *urb) | |
1001 | { | |
1002 | __dw dw4; | |
1003 | int i; | |
db11e47d | 1004 | |
71a9f9d2 AB |
1005 | dw4 = ptd->dw4; |
1006 | dw4 >>= 8; | |
db11e47d | 1007 | |
71a9f9d2 AB |
1008 | /* FIXME: ISP1761 datasheet does not say what to do with these. Do we |
1009 | need to handle these errors? Is it done in hardware? */ | |
db11e47d | 1010 | |
71a9f9d2 | 1011 | if (ptd->dw3 & DW3_HALT_BIT) { |
db11e47d | 1012 | |
71a9f9d2 | 1013 | urb->status = -EPROTO; /* Default unknown error */ |
db11e47d | 1014 | |
71a9f9d2 AB |
1015 | for (i = 0; i < 8; i++) { |
1016 | switch (dw4 & 0x7) { | |
1017 | case INT_UNDERRUN: | |
1018 | dev_dbg(hcd->self.controller, "%s: underrun " | |
1019 | "during uFrame %d\n", | |
1020 | __func__, i); | |
1021 | urb->status = -ECOMM; /* Could not write data */ | |
1022 | break; | |
1023 | case INT_EXACT: | |
1024 | dev_dbg(hcd->self.controller, "%s: transaction " | |
1025 | "error during uFrame %d\n", | |
1026 | __func__, i); | |
1027 | urb->status = -EPROTO; /* timeout, bad CRC, PID | |
1028 | error etc. */ | |
1029 | break; | |
1030 | case INT_BABBLE: | |
1031 | dev_dbg(hcd->self.controller, "%s: babble " | |
1032 | "error during uFrame %d\n", | |
1033 | __func__, i); | |
1034 | urb->status = -EOVERFLOW; | |
1035 | break; | |
1036 | } | |
1037 | dw4 >>= 3; | |
1038 | } | |
db11e47d | 1039 | |
71a9f9d2 AB |
1040 | return PTD_STATE_URB_RETIRE; |
1041 | } | |
db11e47d | 1042 | |
71a9f9d2 AB |
1043 | return PTD_STATE_QTD_DONE; |
1044 | } | |
db11e47d | 1045 | |
71a9f9d2 AB |
1046 | static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd, |
1047 | struct urb *urb) | |
1048 | { | |
1049 | WARN_ON(!ptd); | |
1050 | if (ptd->dw3 & DW3_HALT_BIT) { | |
1051 | if (ptd->dw3 & DW3_BABBLE_BIT) | |
1052 | urb->status = -EOVERFLOW; | |
1053 | else if (FROM_DW3_CERR(ptd->dw3)) | |
1054 | urb->status = -EPIPE; /* Stall */ | |
1055 | else if (ptd->dw3 & DW3_ERROR_BIT) | |
1056 | urb->status = -EPROTO; /* XactErr */ | |
1057 | else | |
1058 | urb->status = -EPROTO; /* Unknown */ | |
1059 | /* | |
1060 | dev_dbg(hcd->self.controller, "%s: ptd error:\n" | |
1061 | " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n" | |
1062 | " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n", | |
1063 | __func__, | |
1064 | ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3, | |
1065 | ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7); | |
1066 | */ | |
1067 | return PTD_STATE_URB_RETIRE; | |
1068 | } | |
db11e47d | 1069 | |
71a9f9d2 AB |
1070 | if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) { |
1071 | /* Transfer Error, *but* active and no HALT -> reload */ | |
1072 | dev_dbg(hcd->self.controller, "PID error; reloading ptd\n"); | |
1073 | return PTD_STATE_QTD_RELOAD; | |
1074 | } | |
db11e47d | 1075 | |
71a9f9d2 AB |
1076 | if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) { |
1077 | /* | |
1078 | * NAKs are handled in HW by the chip. Usually if the | |
1079 | * device is not able to send data fast enough. | |
1080 | * This happens mostly on slower hardware. | |
1081 | */ | |
1082 | return PTD_STATE_QTD_RELOAD; | |
db11e47d | 1083 | } |
71a9f9d2 AB |
1084 | |
1085 | return PTD_STATE_QTD_DONE; | |
db11e47d SS |
1086 | } |
1087 | ||
6d50c60e | 1088 | static void handle_done_ptds(struct usb_hcd *hcd) |
db11e47d | 1089 | { |
bedc0c31 | 1090 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
db11e47d | 1091 | struct ptd ptd; |
db11e47d | 1092 | struct isp1760_qh *qh; |
71a9f9d2 AB |
1093 | int slot; |
1094 | int state; | |
1095 | struct slotinfo *slots; | |
1096 | u32 ptd_offset; | |
1097 | struct isp1760_qtd *qtd; | |
1098 | int modified; | |
6d50c60e | 1099 | int skip_map; |
71a9f9d2 | 1100 | |
6d50c60e AB |
1101 | skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG); |
1102 | priv->int_done_map &= ~skip_map; | |
1103 | skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG); | |
1104 | priv->atl_done_map &= ~skip_map; | |
71a9f9d2 | 1105 | |
6d50c60e | 1106 | modified = priv->int_done_map || priv->atl_done_map; |
d05b6ec0 AB |
1107 | |
1108 | while (priv->int_done_map || priv->atl_done_map) { | |
1109 | if (priv->int_done_map) { | |
71a9f9d2 | 1110 | /* INT ptd */ |
d05b6ec0 AB |
1111 | slot = __ffs(priv->int_done_map); |
1112 | priv->int_done_map &= ~(1 << slot); | |
71a9f9d2 | 1113 | slots = priv->int_slots; |
d05b6ec0 AB |
1114 | /* This should not trigger, and could be removed if |
1115 | noone have any problems with it triggering: */ | |
1116 | if (!slots[slot].qh) { | |
1117 | WARN_ON(1); | |
71a9f9d2 | 1118 | continue; |
d05b6ec0 | 1119 | } |
71a9f9d2 AB |
1120 | ptd_offset = INT_PTD_OFFSET; |
1121 | ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd); | |
1122 | state = check_int_transfer(hcd, &ptd, | |
1123 | slots[slot].qtd->urb); | |
db11e47d | 1124 | } else { |
71a9f9d2 | 1125 | /* ATL ptd */ |
d05b6ec0 AB |
1126 | slot = __ffs(priv->atl_done_map); |
1127 | priv->atl_done_map &= ~(1 << slot); | |
71a9f9d2 | 1128 | slots = priv->atl_slots; |
d05b6ec0 AB |
1129 | /* This should not trigger, and could be removed if |
1130 | noone have any problems with it triggering: */ | |
1131 | if (!slots[slot].qh) { | |
1132 | WARN_ON(1); | |
71a9f9d2 | 1133 | continue; |
d05b6ec0 | 1134 | } |
71a9f9d2 AB |
1135 | ptd_offset = ATL_PTD_OFFSET; |
1136 | ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd); | |
1137 | state = check_atl_transfer(hcd, &ptd, | |
1138 | slots[slot].qtd->urb); | |
db11e47d SS |
1139 | } |
1140 | ||
71a9f9d2 AB |
1141 | qtd = slots[slot].qtd; |
1142 | slots[slot].qtd = NULL; | |
1143 | qh = slots[slot].qh; | |
1144 | slots[slot].qh = NULL; | |
71a9f9d2 AB |
1145 | qh->slot = -1; |
1146 | ||
1147 | WARN_ON(qtd->status != QTD_XFER_STARTED); | |
1148 | ||
1149 | switch (state) { | |
1150 | case PTD_STATE_QTD_DONE: | |
1151 | if ((usb_pipeint(qtd->urb->pipe)) && | |
1152 | (qtd->urb->dev->speed != USB_SPEED_HIGH)) | |
1153 | qtd->actual_length = | |
1154 | FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3); | |
1155 | else | |
1156 | qtd->actual_length = | |
1157 | FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3); | |
db11e47d | 1158 | |
71a9f9d2 AB |
1159 | qtd->status = QTD_XFER_COMPLETE; |
1160 | if (list_is_last(&qtd->qtd_list, &qh->qtd_list) || | |
1161 | is_short_bulk(qtd)) | |
1162 | qtd = NULL; | |
1163 | else | |
1164 | qtd = list_entry(qtd->qtd_list.next, | |
1165 | typeof(*qtd), qtd_list); | |
db11e47d | 1166 | |
71a9f9d2 AB |
1167 | qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3); |
1168 | qh->ping = FROM_DW3_PING(ptd.dw3); | |
1169 | break; | |
db11e47d | 1170 | |
71a9f9d2 AB |
1171 | case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */ |
1172 | qtd->status = QTD_PAYLOAD_ALLOC; | |
1173 | ptd.dw0 |= DW0_VALID_BIT; | |
1174 | /* RL counter = ERR counter */ | |
1175 | ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf); | |
1176 | ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2)); | |
1177 | ptd.dw3 &= ~TO_DW3_CERR(3); | |
1178 | ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER); | |
1179 | qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3); | |
1180 | qh->ping = FROM_DW3_PING(ptd.dw3); | |
1181 | break; | |
db11e47d | 1182 | |
71a9f9d2 AB |
1183 | case PTD_STATE_URB_RETIRE: |
1184 | qtd->status = QTD_RETIRE; | |
1185 | qtd = NULL; | |
1186 | qh->toggle = 0; | |
1187 | qh->ping = 0; | |
1188 | break; | |
db11e47d | 1189 | |
71a9f9d2 AB |
1190 | default: |
1191 | WARN_ON(1); | |
1192 | continue; | |
1193 | } | |
db11e47d | 1194 | |
71a9f9d2 AB |
1195 | if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) { |
1196 | if (slots == priv->int_slots) { | |
1197 | if (state == PTD_STATE_QTD_RELOAD) | |
1198 | dev_err(hcd->self.controller, | |
1199 | "%s: PTD_STATE_QTD_RELOAD on " | |
1200 | "interrupt packet\n", __func__); | |
1201 | if (state != PTD_STATE_QTD_RELOAD) | |
1202 | create_ptd_int(qh, qtd, &ptd); | |
1203 | } else { | |
1204 | if (state != PTD_STATE_QTD_RELOAD) | |
1205 | create_ptd_atl(qh, qtd, &ptd); | |
1206 | } | |
db11e47d | 1207 | |
71a9f9d2 AB |
1208 | start_bus_transfer(hcd, ptd_offset, slot, slots, qtd, |
1209 | qh, &ptd); | |
1210 | } | |
1211 | } | |
db11e47d | 1212 | |
71a9f9d2 AB |
1213 | if (modified) |
1214 | schedule_ptds(hcd); | |
6d50c60e | 1215 | } |
db11e47d | 1216 | |
6d50c60e AB |
1217 | static irqreturn_t isp1760_irq(struct usb_hcd *hcd) |
1218 | { | |
1219 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
1220 | u32 imask; | |
1221 | irqreturn_t irqret = IRQ_NONE; | |
db11e47d | 1222 | |
6d50c60e AB |
1223 | spin_lock(&priv->lock); |
1224 | ||
1225 | if (!(hcd->state & HC_STATE_RUNNING)) | |
1226 | goto leave; | |
1227 | ||
1228 | imask = reg_read32(hcd->regs, HC_INTERRUPT_REG); | |
1229 | if (unlikely(!imask)) | |
1230 | goto leave; | |
1231 | reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */ | |
1232 | ||
1233 | priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG); | |
1234 | priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG); | |
1235 | ||
1236 | handle_done_ptds(hcd); | |
db11e47d | 1237 | |
71a9f9d2 AB |
1238 | irqret = IRQ_HANDLED; |
1239 | leave: | |
1240 | spin_unlock(&priv->lock); | |
db11e47d | 1241 | |
71a9f9d2 | 1242 | return irqret; |
db11e47d SS |
1243 | } |
1244 | ||
6d50c60e AB |
1245 | /* |
1246 | * Workaround for problem described in chip errata 2: | |
1247 | * | |
1248 | * Sometimes interrupts are not generated when ATL (not INT?) completion occurs. | |
1249 | * One solution suggested in the errata is to use SOF interrupts _instead_of_ | |
1250 | * ATL done interrupts (the "instead of" might be important since it seems | |
1251 | * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget" | |
1252 | * to set the PTD's done bit in addition to not generating an interrupt!). | |
1253 | * | |
1254 | * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their | |
1255 | * done bit is not being set. This is bad - it blocks the endpoint until reboot. | |
1256 | * | |
1257 | * If we use SOF interrupts only, we get latency between ptd completion and the | |
1258 | * actual handling. This is very noticeable in testusb runs which takes several | |
1259 | * minutes longer without ATL interrupts. | |
1260 | * | |
1261 | * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it | |
1262 | * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the | |
1263 | * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered | |
1264 | * completed and its done map bit is set. | |
1265 | * | |
1266 | * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen | |
1267 | * not to cause too much lag when this HW bug occurs, while still hopefully | |
1268 | * ensuring that the check does not falsely trigger. | |
1269 | */ | |
6477acc0 | 1270 | #define SLOT_TIMEOUT 300 |
6d50c60e AB |
1271 | #define SLOT_CHECK_PERIOD 200 |
1272 | static struct timer_list errata2_timer; | |
1273 | ||
1274 | void errata2_function(unsigned long data) | |
1275 | { | |
1276 | struct usb_hcd *hcd = (struct usb_hcd *) data; | |
1277 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
1278 | int slot; | |
1279 | struct ptd ptd; | |
1280 | unsigned long spinflags; | |
1281 | ||
1282 | spin_lock_irqsave(&priv->lock, spinflags); | |
1283 | ||
1284 | for (slot = 0; slot < 32; slot++) | |
6477acc0 AB |
1285 | if (priv->atl_slots[slot].qh && time_after(jiffies, |
1286 | priv->atl_slots[slot].timestamp + | |
1287 | SLOT_TIMEOUT * HZ / 1000)) { | |
6d50c60e AB |
1288 | ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd); |
1289 | if (!FROM_DW0_VALID(ptd.dw0) && | |
1290 | !FROM_DW3_ACTIVE(ptd.dw3)) | |
1291 | priv->atl_done_map |= 1 << slot; | |
1292 | } | |
1293 | ||
6477acc0 AB |
1294 | if (priv->atl_done_map) |
1295 | handle_done_ptds(hcd); | |
6d50c60e AB |
1296 | |
1297 | spin_unlock_irqrestore(&priv->lock, spinflags); | |
1298 | ||
1299 | errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000; | |
1300 | add_timer(&errata2_timer); | |
1301 | } | |
1302 | ||
0ba7905e AB |
1303 | static int isp1760_run(struct usb_hcd *hcd) |
1304 | { | |
1305 | int retval; | |
1306 | u32 temp; | |
1307 | u32 command; | |
1308 | u32 chipid; | |
1309 | ||
1310 | hcd->uses_new_polling = 1; | |
1311 | ||
1312 | hcd->state = HC_STATE_RUNNING; | |
1313 | ||
1314 | /* Set PTD interrupt AND & OR maps */ | |
1315 | reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0); | |
1316 | reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff); | |
1317 | reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0); | |
1318 | reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff); | |
1319 | reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0); | |
1320 | reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff); | |
1321 | /* step 23 passed */ | |
1322 | ||
1323 | temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL); | |
1324 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN); | |
1325 | ||
1326 | command = reg_read32(hcd->regs, HC_USBCMD); | |
1327 | command &= ~(CMD_LRESET|CMD_RESET); | |
1328 | command |= CMD_RUN; | |
1329 | reg_write32(hcd->regs, HC_USBCMD, command); | |
1330 | ||
1331 | retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000); | |
1332 | if (retval) | |
1333 | return retval; | |
1334 | ||
1335 | /* | |
1336 | * XXX | |
1337 | * Spec says to write FLAG_CF as last config action, priv code grabs | |
1338 | * the semaphore while doing so. | |
1339 | */ | |
1340 | down_write(&ehci_cf_port_reset_rwsem); | |
1341 | reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF); | |
1342 | ||
1343 | retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000); | |
1344 | up_write(&ehci_cf_port_reset_rwsem); | |
1345 | if (retval) | |
1346 | return retval; | |
1347 | ||
6d50c60e AB |
1348 | init_timer(&errata2_timer); |
1349 | errata2_timer.function = errata2_function; | |
1350 | errata2_timer.data = (unsigned long) hcd; | |
1351 | errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000; | |
1352 | add_timer(&errata2_timer); | |
1353 | ||
0ba7905e AB |
1354 | chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG); |
1355 | dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n", | |
1356 | chipid & 0xffff, chipid >> 16); | |
1357 | ||
1358 | /* PTD Register Init Part 2, Step 28 */ | |
1359 | ||
1360 | /* Setup registers controlling PTD checking */ | |
1361 | reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000); | |
1362 | reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000); | |
1363 | reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001); | |
1364 | reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff); | |
1365 | reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff); | |
1366 | reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff); | |
1367 | reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, | |
1368 | ATL_BUF_FILL | INT_BUF_FILL); | |
1369 | ||
1370 | /* GRR this is run-once init(), being done every time the HC starts. | |
1371 | * So long as they're part of class devices, we can't do it init() | |
1372 | * since the class device isn't created that early. | |
1373 | */ | |
1374 | return 0; | |
1375 | } | |
1376 | ||
34537731 | 1377 | static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len) |
db11e47d | 1378 | { |
34537731 | 1379 | qtd->data_buffer = databuffer; |
db11e47d | 1380 | |
34537731 AB |
1381 | if (len > MAX_PAYLOAD_SIZE) |
1382 | len = MAX_PAYLOAD_SIZE; | |
1383 | qtd->length = len; | |
db11e47d | 1384 | |
34537731 | 1385 | return qtd->length; |
db11e47d SS |
1386 | } |
1387 | ||
34537731 | 1388 | static void qtd_list_free(struct list_head *qtd_list) |
db11e47d | 1389 | { |
34537731 | 1390 | struct isp1760_qtd *qtd, *qtd_next; |
db11e47d | 1391 | |
34537731 | 1392 | list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) { |
db11e47d | 1393 | list_del(&qtd->qtd_list); |
34537731 | 1394 | qtd_free(qtd); |
db11e47d SS |
1395 | } |
1396 | } | |
1397 | ||
db11e47d | 1398 | /* |
34537731 AB |
1399 | * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize. |
1400 | * Also calculate the PID type (SETUP/IN/OUT) for each packet. | |
db11e47d | 1401 | */ |
6bda21bc | 1402 | #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff) |
34537731 | 1403 | static void packetize_urb(struct usb_hcd *hcd, |
db11e47d SS |
1404 | struct urb *urb, struct list_head *head, gfp_t flags) |
1405 | { | |
fd436aee | 1406 | struct isp1760_qtd *qtd; |
db11e47d | 1407 | void *buf; |
34537731 AB |
1408 | int len, maxpacketsize; |
1409 | u8 packet_type; | |
db11e47d SS |
1410 | |
1411 | /* | |
1412 | * URBs map to sequences of QTDs: one logical transaction | |
1413 | */ | |
db11e47d | 1414 | |
34537731 AB |
1415 | if (!urb->transfer_buffer && urb->transfer_buffer_length) { |
1416 | /* XXX This looks like usb storage / SCSI bug */ | |
1417 | dev_err(hcd->self.controller, | |
1418 | "buf is null, dma is %08lx len is %d\n", | |
1419 | (long unsigned)urb->transfer_dma, | |
1420 | urb->transfer_buffer_length); | |
1421 | WARN_ON(1); | |
1422 | } | |
db11e47d | 1423 | |
34537731 AB |
1424 | if (usb_pipein(urb->pipe)) |
1425 | packet_type = IN_PID; | |
1426 | else | |
1427 | packet_type = OUT_PID; | |
db11e47d | 1428 | |
db11e47d | 1429 | if (usb_pipecontrol(urb->pipe)) { |
34537731 | 1430 | qtd = qtd_alloc(flags, urb, SETUP_PID); |
db11e47d SS |
1431 | if (!qtd) |
1432 | goto cleanup; | |
34537731 | 1433 | qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest)); |
db11e47d SS |
1434 | list_add_tail(&qtd->qtd_list, head); |
1435 | ||
1436 | /* for zero length DATA stages, STATUS is always IN */ | |
34537731 AB |
1437 | if (urb->transfer_buffer_length == 0) |
1438 | packet_type = IN_PID; | |
db11e47d SS |
1439 | } |
1440 | ||
34537731 AB |
1441 | maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe, |
1442 | usb_pipeout(urb->pipe))); | |
db11e47d SS |
1443 | |
1444 | /* | |
1445 | * buffer gets wrapped in one or more qtds; | |
1446 | * last one may be "short" (including zero len) | |
1447 | * and may serve as a control status ack | |
1448 | */ | |
34537731 AB |
1449 | buf = urb->transfer_buffer; |
1450 | len = urb->transfer_buffer_length; | |
1451 | ||
db11e47d SS |
1452 | for (;;) { |
1453 | int this_qtd_len; | |
1454 | ||
34537731 AB |
1455 | qtd = qtd_alloc(flags, urb, packet_type); |
1456 | if (!qtd) | |
1457 | goto cleanup; | |
1458 | this_qtd_len = qtd_fill(qtd, buf, len); | |
1459 | list_add_tail(&qtd->qtd_list, head); | |
db11e47d | 1460 | |
db11e47d SS |
1461 | len -= this_qtd_len; |
1462 | buf += this_qtd_len; | |
1463 | ||
db11e47d SS |
1464 | if (len <= 0) |
1465 | break; | |
db11e47d SS |
1466 | } |
1467 | ||
1468 | /* | |
1469 | * control requests may need a terminating data "status" ack; | |
1470 | * bulk ones may need a terminating short packet (zero length). | |
1471 | */ | |
1472 | if (urb->transfer_buffer_length != 0) { | |
1473 | int one_more = 0; | |
1474 | ||
1475 | if (usb_pipecontrol(urb->pipe)) { | |
1476 | one_more = 1; | |
34537731 AB |
1477 | if (packet_type == IN_PID) |
1478 | packet_type = OUT_PID; | |
1479 | else | |
1480 | packet_type = IN_PID; | |
db11e47d SS |
1481 | } else if (usb_pipebulk(urb->pipe) |
1482 | && (urb->transfer_flags & URB_ZERO_PACKET) | |
34537731 AB |
1483 | && !(urb->transfer_buffer_length % |
1484 | maxpacketsize)) { | |
db11e47d SS |
1485 | one_more = 1; |
1486 | } | |
1487 | if (one_more) { | |
34537731 | 1488 | qtd = qtd_alloc(flags, urb, packet_type); |
db11e47d SS |
1489 | if (!qtd) |
1490 | goto cleanup; | |
db11e47d SS |
1491 | |
1492 | /* never any data in such packets */ | |
34537731 AB |
1493 | qtd_fill(qtd, NULL, 0); |
1494 | list_add_tail(&qtd->qtd_list, head); | |
db11e47d SS |
1495 | } |
1496 | } | |
1497 | ||
34537731 | 1498 | return; |
db11e47d SS |
1499 | |
1500 | cleanup: | |
34537731 AB |
1501 | qtd_list_free(head); |
1502 | } | |
1503 | ||
db11e47d SS |
1504 | static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, |
1505 | gfp_t mem_flags) | |
1506 | { | |
71a9f9d2 AB |
1507 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
1508 | struct list_head *ep_queue; | |
1509 | struct isp1760_qh *qh, *qhit; | |
1510 | unsigned long spinflags; | |
1511 | LIST_HEAD(new_qtds); | |
1512 | int retval; | |
1513 | int qh_in_queue; | |
db11e47d SS |
1514 | |
1515 | switch (usb_pipetype(urb->pipe)) { | |
1516 | case PIPE_CONTROL: | |
71a9f9d2 AB |
1517 | ep_queue = &priv->controlqhs; |
1518 | break; | |
db11e47d | 1519 | case PIPE_BULK: |
71a9f9d2 | 1520 | ep_queue = &priv->bulkqhs; |
db11e47d | 1521 | break; |
db11e47d | 1522 | case PIPE_INTERRUPT: |
71a9f9d2 AB |
1523 | if (urb->interval < 0) |
1524 | return -EINVAL; | |
1525 | /* FIXME: Check bandwidth */ | |
1526 | ep_queue = &priv->interruptqhs; | |
db11e47d | 1527 | break; |
db11e47d | 1528 | case PIPE_ISOCHRONOUS: |
71a9f9d2 AB |
1529 | dev_err(hcd->self.controller, "%s: isochronous USB packets " |
1530 | "not yet supported\n", | |
1531 | __func__); | |
1532 | return -EPIPE; | |
db11e47d | 1533 | default: |
71a9f9d2 AB |
1534 | dev_err(hcd->self.controller, "%s: unknown pipe type\n", |
1535 | __func__); | |
db11e47d SS |
1536 | return -EPIPE; |
1537 | } | |
1538 | ||
71a9f9d2 AB |
1539 | if (usb_pipein(urb->pipe)) |
1540 | urb->actual_length = 0; | |
db11e47d | 1541 | |
71a9f9d2 AB |
1542 | packetize_urb(hcd, urb, &new_qtds, mem_flags); |
1543 | if (list_empty(&new_qtds)) | |
1544 | return -ENOMEM; | |
db11e47d | 1545 | |
71a9f9d2 AB |
1546 | retval = 0; |
1547 | spin_lock_irqsave(&priv->lock, spinflags); | |
db11e47d | 1548 | |
71a9f9d2 AB |
1549 | if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { |
1550 | retval = -ESHUTDOWN; | |
1551 | goto out; | |
db11e47d | 1552 | } |
71a9f9d2 AB |
1553 | retval = usb_hcd_link_urb_to_ep(hcd, urb); |
1554 | if (retval) | |
1555 | goto out; | |
db11e47d | 1556 | |
71a9f9d2 AB |
1557 | qh = urb->ep->hcpriv; |
1558 | if (qh) { | |
1559 | qh_in_queue = 0; | |
1560 | list_for_each_entry(qhit, ep_queue, qh_list) { | |
1561 | if (qhit == qh) { | |
1562 | qh_in_queue = 1; | |
0afb20e0 | 1563 | break; |
71a9f9d2 AB |
1564 | } |
1565 | } | |
1566 | if (!qh_in_queue) | |
1567 | list_add_tail(&qh->qh_list, ep_queue); | |
1568 | } else { | |
1569 | qh = qh_alloc(GFP_ATOMIC); | |
1570 | if (!qh) { | |
1571 | retval = -ENOMEM; | |
38679b72 | 1572 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
71a9f9d2 | 1573 | goto out; |
db11e47d | 1574 | } |
71a9f9d2 AB |
1575 | list_add_tail(&qh->qh_list, ep_queue); |
1576 | urb->ep->hcpriv = qh; | |
db11e47d SS |
1577 | } |
1578 | ||
71a9f9d2 AB |
1579 | list_splice_tail(&new_qtds, &qh->qtd_list); |
1580 | schedule_ptds(hcd); | |
1581 | ||
1582 | out: | |
1583 | spin_unlock_irqrestore(&priv->lock, spinflags); | |
1584 | return retval; | |
db11e47d SS |
1585 | } |
1586 | ||
d05b6ec0 AB |
1587 | static void kill_transfer(struct usb_hcd *hcd, struct urb *urb, |
1588 | struct isp1760_qh *qh) | |
1589 | { | |
1590 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
1591 | int skip_map; | |
1592 | ||
1593 | WARN_ON(qh->slot == -1); | |
1594 | ||
1595 | /* We need to forcefully reclaim the slot since some transfers never | |
1596 | return, e.g. interrupt transfers and NAKed bulk transfers. */ | |
8b1ab60c | 1597 | if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) { |
d05b6ec0 AB |
1598 | skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG); |
1599 | skip_map |= (1 << qh->slot); | |
1600 | reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map); | |
1601 | priv->atl_slots[qh->slot].qh = NULL; | |
1602 | priv->atl_slots[qh->slot].qtd = NULL; | |
1603 | } else { | |
1604 | skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG); | |
1605 | skip_map |= (1 << qh->slot); | |
1606 | reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map); | |
1607 | priv->int_slots[qh->slot].qh = NULL; | |
1608 | priv->int_slots[qh->slot].qtd = NULL; | |
1609 | } | |
1610 | ||
1611 | qh->slot = -1; | |
d05b6ec0 AB |
1612 | } |
1613 | ||
71a9f9d2 AB |
1614 | static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, |
1615 | int status) | |
db11e47d | 1616 | { |
6bda21bc | 1617 | struct isp1760_hcd *priv = hcd_to_priv(hcd); |
d05b6ec0 | 1618 | unsigned long spinflags; |
71a9f9d2 AB |
1619 | struct isp1760_qh *qh; |
1620 | struct isp1760_qtd *qtd; | |
71a9f9d2 | 1621 | int retval = 0; |
db11e47d | 1622 | |
71a9f9d2 | 1623 | spin_lock_irqsave(&priv->lock, spinflags); |
17d3e145 AB |
1624 | retval = usb_hcd_check_unlink_urb(hcd, urb, status); |
1625 | if (retval) | |
1626 | goto out; | |
db11e47d | 1627 | |
71a9f9d2 AB |
1628 | qh = urb->ep->hcpriv; |
1629 | if (!qh) { | |
1630 | retval = -EINVAL; | |
1631 | goto out; | |
1632 | } | |
db11e47d | 1633 | |
d05b6ec0 AB |
1634 | list_for_each_entry(qtd, &qh->qtd_list, qtd_list) |
1635 | if (qtd->urb == urb) { | |
1636 | if (qtd->status == QTD_XFER_STARTED) | |
1637 | kill_transfer(hcd, urb, qh); | |
71a9f9d2 | 1638 | qtd->status = QTD_RETIRE; |
d05b6ec0 | 1639 | } |
db11e47d | 1640 | |
71a9f9d2 AB |
1641 | urb->status = status; |
1642 | schedule_ptds(hcd); | |
db11e47d | 1643 | |
71a9f9d2 AB |
1644 | out: |
1645 | spin_unlock_irqrestore(&priv->lock, spinflags); | |
71a9f9d2 | 1646 | return retval; |
db11e47d SS |
1647 | } |
1648 | ||
079cdb09 AB |
1649 | static void isp1760_endpoint_disable(struct usb_hcd *hcd, |
1650 | struct usb_host_endpoint *ep) | |
1651 | { | |
1652 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
d05b6ec0 | 1653 | unsigned long spinflags; |
079cdb09 AB |
1654 | struct isp1760_qh *qh; |
1655 | struct isp1760_qtd *qtd; | |
079cdb09 AB |
1656 | |
1657 | spin_lock_irqsave(&priv->lock, spinflags); | |
d05b6ec0 | 1658 | |
079cdb09 AB |
1659 | qh = ep->hcpriv; |
1660 | if (!qh) | |
1661 | goto out; | |
1662 | ||
d05b6ec0 AB |
1663 | list_for_each_entry(qtd, &qh->qtd_list, qtd_list) { |
1664 | if (qtd->status == QTD_XFER_STARTED) | |
1665 | kill_transfer(hcd, qtd->urb, qh); | |
1666 | qtd->status = QTD_RETIRE; | |
1667 | qtd->urb->status = -ECONNRESET; | |
079cdb09 | 1668 | } |
d05b6ec0 | 1669 | |
079cdb09 AB |
1670 | ep->hcpriv = NULL; |
1671 | /* Cannot free qh here since it will be parsed by schedule_ptds() */ | |
1672 | ||
d05b6ec0 AB |
1673 | schedule_ptds(hcd); |
1674 | ||
079cdb09 AB |
1675 | out: |
1676 | spin_unlock_irqrestore(&priv->lock, spinflags); | |
1677 | } | |
1678 | ||
db11e47d SS |
1679 | static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf) |
1680 | { | |
1681 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
1682 | u32 temp, status = 0; | |
1683 | u32 mask; | |
1684 | int retval = 1; | |
1685 | unsigned long flags; | |
1686 | ||
1687 | /* if !USB_SUSPEND, root hub timers won't get shut down ... */ | |
1688 | if (!HC_IS_RUNNING(hcd->state)) | |
1689 | return 0; | |
1690 | ||
1691 | /* init status to no-changes */ | |
1692 | buf[0] = 0; | |
1693 | mask = PORT_CSC; | |
1694 | ||
1695 | spin_lock_irqsave(&priv->lock, flags); | |
bedc0c31 | 1696 | temp = reg_read32(hcd->regs, HC_PORTSC1); |
db11e47d SS |
1697 | |
1698 | if (temp & PORT_OWNER) { | |
1699 | if (temp & PORT_CSC) { | |
1700 | temp &= ~PORT_CSC; | |
bedc0c31 | 1701 | reg_write32(hcd->regs, HC_PORTSC1, temp); |
db11e47d SS |
1702 | goto done; |
1703 | } | |
1704 | } | |
1705 | ||
1706 | /* | |
1707 | * Return status information even for ports with OWNER set. | |
1708 | * Otherwise khubd wouldn't see the disconnect event when a | |
1709 | * high-speed device is switched over to the companion | |
1710 | * controller by the user. | |
1711 | */ | |
1712 | ||
1713 | if ((temp & mask) != 0 | |
1714 | || ((temp & PORT_RESUME) != 0 | |
1715 | && time_after_eq(jiffies, | |
1716 | priv->reset_done))) { | |
1717 | buf [0] |= 1 << (0 + 1); | |
1718 | status = STS_PCD; | |
1719 | } | |
1720 | /* FIXME autosuspend idle root hubs */ | |
1721 | done: | |
1722 | spin_unlock_irqrestore(&priv->lock, flags); | |
1723 | return status ? retval : 0; | |
1724 | } | |
1725 | ||
1726 | static void isp1760_hub_descriptor(struct isp1760_hcd *priv, | |
1727 | struct usb_hub_descriptor *desc) | |
1728 | { | |
1729 | int ports = HCS_N_PORTS(priv->hcs_params); | |
1730 | u16 temp; | |
1731 | ||
1732 | desc->bDescriptorType = 0x29; | |
1733 | /* priv 1.0, 2.3.9 says 20ms max */ | |
1734 | desc->bPwrOn2PwrGood = 10; | |
1735 | desc->bHubContrCurrent = 0; | |
1736 | ||
1737 | desc->bNbrPorts = ports; | |
1738 | temp = 1 + (ports / 8); | |
1739 | desc->bDescLength = 7 + 2 * temp; | |
1740 | ||
da13051c | 1741 | /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */ |
dbe79bbe JY |
1742 | memset(&desc->u.hs.DeviceRemovable[0], 0, temp); |
1743 | memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp); | |
db11e47d SS |
1744 | |
1745 | /* per-port overcurrent reporting */ | |
1746 | temp = 0x0008; | |
1747 | if (HCS_PPC(priv->hcs_params)) | |
1748 | /* per-port power control */ | |
1749 | temp |= 0x0001; | |
1750 | else | |
1751 | /* no power switching */ | |
1752 | temp |= 0x0002; | |
1753 | desc->wHubCharacteristics = cpu_to_le16(temp); | |
1754 | } | |
1755 | ||
1756 | #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E) | |
1757 | ||
bedc0c31 AB |
1758 | static int check_reset_complete(struct usb_hcd *hcd, int index, |
1759 | int port_status) | |
db11e47d SS |
1760 | { |
1761 | if (!(port_status & PORT_CONNECT)) | |
1762 | return port_status; | |
1763 | ||
1764 | /* if reset finished and it's still not enabled -- handoff */ | |
1765 | if (!(port_status & PORT_PE)) { | |
1766 | ||
71a9f9d2 | 1767 | dev_info(hcd->self.controller, |
6bda21bc AB |
1768 | "port %d full speed --> companion\n", |
1769 | index + 1); | |
db11e47d SS |
1770 | |
1771 | port_status |= PORT_OWNER; | |
1772 | port_status &= ~PORT_RWC_BITS; | |
bedc0c31 | 1773 | reg_write32(hcd->regs, HC_PORTSC1, port_status); |
db11e47d SS |
1774 | |
1775 | } else | |
71a9f9d2 | 1776 | dev_info(hcd->self.controller, "port %d high speed\n", |
6bda21bc | 1777 | index + 1); |
db11e47d SS |
1778 | |
1779 | return port_status; | |
1780 | } | |
1781 | ||
1782 | static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq, | |
1783 | u16 wValue, u16 wIndex, char *buf, u16 wLength) | |
1784 | { | |
1785 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
1786 | int ports = HCS_N_PORTS(priv->hcs_params); | |
db11e47d SS |
1787 | u32 temp, status; |
1788 | unsigned long flags; | |
1789 | int retval = 0; | |
1790 | unsigned selector; | |
1791 | ||
1792 | /* | |
1793 | * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. | |
1794 | * HCS_INDICATOR may say we can change LEDs to off/amber/green. | |
1795 | * (track current state ourselves) ... blink for diagnostics, | |
1796 | * power, "this is the one", etc. EHCI spec supports this. | |
1797 | */ | |
1798 | ||
1799 | spin_lock_irqsave(&priv->lock, flags); | |
1800 | switch (typeReq) { | |
1801 | case ClearHubFeature: | |
1802 | switch (wValue) { | |
1803 | case C_HUB_LOCAL_POWER: | |
1804 | case C_HUB_OVER_CURRENT: | |
1805 | /* no hub-wide feature/status flags */ | |
1806 | break; | |
1807 | default: | |
1808 | goto error; | |
1809 | } | |
1810 | break; | |
1811 | case ClearPortFeature: | |
1812 | if (!wIndex || wIndex > ports) | |
1813 | goto error; | |
1814 | wIndex--; | |
bedc0c31 | 1815 | temp = reg_read32(hcd->regs, HC_PORTSC1); |
db11e47d SS |
1816 | |
1817 | /* | |
1818 | * Even if OWNER is set, so the port is owned by the | |
1819 | * companion controller, khubd needs to be able to clear | |
1820 | * the port-change status bits (especially | |
749da5f8 | 1821 | * USB_PORT_STAT_C_CONNECTION). |
db11e47d SS |
1822 | */ |
1823 | ||
1824 | switch (wValue) { | |
1825 | case USB_PORT_FEAT_ENABLE: | |
bedc0c31 | 1826 | reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE); |
db11e47d SS |
1827 | break; |
1828 | case USB_PORT_FEAT_C_ENABLE: | |
1829 | /* XXX error? */ | |
1830 | break; | |
1831 | case USB_PORT_FEAT_SUSPEND: | |
1832 | if (temp & PORT_RESET) | |
1833 | goto error; | |
1834 | ||
1835 | if (temp & PORT_SUSPEND) { | |
1836 | if ((temp & PORT_PE) == 0) | |
1837 | goto error; | |
1838 | /* resume signaling for 20 msec */ | |
1839 | temp &= ~(PORT_RWC_BITS); | |
bedc0c31 AB |
1840 | reg_write32(hcd->regs, HC_PORTSC1, |
1841 | temp | PORT_RESUME); | |
db11e47d SS |
1842 | priv->reset_done = jiffies + |
1843 | msecs_to_jiffies(20); | |
1844 | } | |
1845 | break; | |
1846 | case USB_PORT_FEAT_C_SUSPEND: | |
1847 | /* we auto-clear this feature */ | |
1848 | break; | |
1849 | case USB_PORT_FEAT_POWER: | |
1850 | if (HCS_PPC(priv->hcs_params)) | |
bedc0c31 AB |
1851 | reg_write32(hcd->regs, HC_PORTSC1, |
1852 | temp & ~PORT_POWER); | |
db11e47d SS |
1853 | break; |
1854 | case USB_PORT_FEAT_C_CONNECTION: | |
bedc0c31 | 1855 | reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC); |
db11e47d SS |
1856 | break; |
1857 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
1858 | /* XXX error ?*/ | |
1859 | break; | |
1860 | case USB_PORT_FEAT_C_RESET: | |
1861 | /* GetPortStatus clears reset */ | |
1862 | break; | |
1863 | default: | |
1864 | goto error; | |
1865 | } | |
bedc0c31 | 1866 | reg_read32(hcd->regs, HC_USBCMD); |
db11e47d SS |
1867 | break; |
1868 | case GetHubDescriptor: | |
1869 | isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *) | |
1870 | buf); | |
1871 | break; | |
1872 | case GetHubStatus: | |
1873 | /* no hub-wide feature/status flags */ | |
1874 | memset(buf, 0, 4); | |
1875 | break; | |
1876 | case GetPortStatus: | |
1877 | if (!wIndex || wIndex > ports) | |
1878 | goto error; | |
1879 | wIndex--; | |
1880 | status = 0; | |
bedc0c31 | 1881 | temp = reg_read32(hcd->regs, HC_PORTSC1); |
db11e47d SS |
1882 | |
1883 | /* wPortChange bits */ | |
1884 | if (temp & PORT_CSC) | |
749da5f8 | 1885 | status |= USB_PORT_STAT_C_CONNECTION << 16; |
db11e47d SS |
1886 | |
1887 | ||
1888 | /* whoever resumes must GetPortStatus to complete it!! */ | |
1889 | if (temp & PORT_RESUME) { | |
6bda21bc | 1890 | dev_err(hcd->self.controller, "Port resume should be skipped.\n"); |
db11e47d SS |
1891 | |
1892 | /* Remote Wakeup received? */ | |
1893 | if (!priv->reset_done) { | |
1894 | /* resume signaling for 20 msec */ | |
1895 | priv->reset_done = jiffies | |
1896 | + msecs_to_jiffies(20); | |
1897 | /* check the port again */ | |
6bda21bc | 1898 | mod_timer(&hcd->rh_timer, priv->reset_done); |
db11e47d SS |
1899 | } |
1900 | ||
1901 | /* resume completed? */ | |
1902 | else if (time_after_eq(jiffies, | |
1903 | priv->reset_done)) { | |
749da5f8 | 1904 | status |= USB_PORT_STAT_C_SUSPEND << 16; |
db11e47d SS |
1905 | priv->reset_done = 0; |
1906 | ||
1907 | /* stop resume signaling */ | |
bedc0c31 AB |
1908 | temp = reg_read32(hcd->regs, HC_PORTSC1); |
1909 | reg_write32(hcd->regs, HC_PORTSC1, | |
1910 | temp & ~(PORT_RWC_BITS | PORT_RESUME)); | |
1911 | retval = handshake(hcd, HC_PORTSC1, | |
db11e47d SS |
1912 | PORT_RESUME, 0, 2000 /* 2msec */); |
1913 | if (retval != 0) { | |
6bda21bc | 1914 | dev_err(hcd->self.controller, |
db11e47d SS |
1915 | "port %d resume error %d\n", |
1916 | wIndex + 1, retval); | |
1917 | goto error; | |
1918 | } | |
1919 | temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10)); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | /* whoever resets must GetPortStatus to complete it!! */ | |
1924 | if ((temp & PORT_RESET) | |
1925 | && time_after_eq(jiffies, | |
1926 | priv->reset_done)) { | |
749da5f8 | 1927 | status |= USB_PORT_STAT_C_RESET << 16; |
db11e47d SS |
1928 | priv->reset_done = 0; |
1929 | ||
1930 | /* force reset to complete */ | |
bedc0c31 | 1931 | reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET); |
db11e47d SS |
1932 | /* REVISIT: some hardware needs 550+ usec to clear |
1933 | * this bit; seems too long to spin routinely... | |
1934 | */ | |
bedc0c31 | 1935 | retval = handshake(hcd, HC_PORTSC1, |
db11e47d SS |
1936 | PORT_RESET, 0, 750); |
1937 | if (retval != 0) { | |
6bda21bc | 1938 | dev_err(hcd->self.controller, "port %d reset error %d\n", |
db11e47d SS |
1939 | wIndex + 1, retval); |
1940 | goto error; | |
1941 | } | |
1942 | ||
1943 | /* see what we found out */ | |
bedc0c31 AB |
1944 | temp = check_reset_complete(hcd, wIndex, |
1945 | reg_read32(hcd->regs, HC_PORTSC1)); | |
db11e47d SS |
1946 | } |
1947 | /* | |
1948 | * Even if OWNER is set, there's no harm letting khubd | |
1949 | * see the wPortStatus values (they should all be 0 except | |
1950 | * for PORT_POWER anyway). | |
1951 | */ | |
1952 | ||
1953 | if (temp & PORT_OWNER) | |
6bda21bc | 1954 | dev_err(hcd->self.controller, "PORT_OWNER is set\n"); |
db11e47d SS |
1955 | |
1956 | if (temp & PORT_CONNECT) { | |
749da5f8 | 1957 | status |= USB_PORT_STAT_CONNECTION; |
db11e47d | 1958 | /* status may be from integrated TT */ |
6bda21bc | 1959 | status |= USB_PORT_STAT_HIGH_SPEED; |
db11e47d SS |
1960 | } |
1961 | if (temp & PORT_PE) | |
749da5f8 | 1962 | status |= USB_PORT_STAT_ENABLE; |
db11e47d | 1963 | if (temp & (PORT_SUSPEND|PORT_RESUME)) |
749da5f8 | 1964 | status |= USB_PORT_STAT_SUSPEND; |
db11e47d | 1965 | if (temp & PORT_RESET) |
749da5f8 | 1966 | status |= USB_PORT_STAT_RESET; |
db11e47d | 1967 | if (temp & PORT_POWER) |
749da5f8 | 1968 | status |= USB_PORT_STAT_POWER; |
db11e47d SS |
1969 | |
1970 | put_unaligned(cpu_to_le32(status), (__le32 *) buf); | |
1971 | break; | |
1972 | case SetHubFeature: | |
1973 | switch (wValue) { | |
1974 | case C_HUB_LOCAL_POWER: | |
1975 | case C_HUB_OVER_CURRENT: | |
1976 | /* no hub-wide feature/status flags */ | |
1977 | break; | |
1978 | default: | |
1979 | goto error; | |
1980 | } | |
1981 | break; | |
1982 | case SetPortFeature: | |
1983 | selector = wIndex >> 8; | |
1984 | wIndex &= 0xff; | |
1985 | if (!wIndex || wIndex > ports) | |
1986 | goto error; | |
1987 | wIndex--; | |
bedc0c31 | 1988 | temp = reg_read32(hcd->regs, HC_PORTSC1); |
db11e47d SS |
1989 | if (temp & PORT_OWNER) |
1990 | break; | |
1991 | ||
1992 | /* temp &= ~PORT_RWC_BITS; */ | |
1993 | switch (wValue) { | |
1994 | case USB_PORT_FEAT_ENABLE: | |
bedc0c31 | 1995 | reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE); |
db11e47d SS |
1996 | break; |
1997 | ||
1998 | case USB_PORT_FEAT_SUSPEND: | |
1999 | if ((temp & PORT_PE) == 0 | |
2000 | || (temp & PORT_RESET) != 0) | |
2001 | goto error; | |
2002 | ||
bedc0c31 | 2003 | reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND); |
db11e47d SS |
2004 | break; |
2005 | case USB_PORT_FEAT_POWER: | |
2006 | if (HCS_PPC(priv->hcs_params)) | |
bedc0c31 AB |
2007 | reg_write32(hcd->regs, HC_PORTSC1, |
2008 | temp | PORT_POWER); | |
db11e47d SS |
2009 | break; |
2010 | case USB_PORT_FEAT_RESET: | |
2011 | if (temp & PORT_RESUME) | |
2012 | goto error; | |
2013 | /* line status bits may report this as low speed, | |
2014 | * which can be fine if this root hub has a | |
2015 | * transaction translator built in. | |
2016 | */ | |
2017 | if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT | |
2018 | && PORT_USB11(temp)) { | |
2019 | temp |= PORT_OWNER; | |
2020 | } else { | |
2021 | temp |= PORT_RESET; | |
2022 | temp &= ~PORT_PE; | |
2023 | ||
2024 | /* | |
2025 | * caller must wait, then call GetPortStatus | |
2026 | * usb 2.0 spec says 50 ms resets on root | |
2027 | */ | |
2028 | priv->reset_done = jiffies + | |
2029 | msecs_to_jiffies(50); | |
2030 | } | |
bedc0c31 | 2031 | reg_write32(hcd->regs, HC_PORTSC1, temp); |
db11e47d SS |
2032 | break; |
2033 | default: | |
2034 | goto error; | |
2035 | } | |
bedc0c31 | 2036 | reg_read32(hcd->regs, HC_USBCMD); |
db11e47d SS |
2037 | break; |
2038 | ||
2039 | default: | |
2040 | error: | |
2041 | /* "stall" on error */ | |
2042 | retval = -EPIPE; | |
2043 | } | |
2044 | spin_unlock_irqrestore(&priv->lock, flags); | |
2045 | return retval; | |
2046 | } | |
2047 | ||
db11e47d SS |
2048 | static int isp1760_get_frame(struct usb_hcd *hcd) |
2049 | { | |
2050 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
2051 | u32 fr; | |
2052 | ||
bedc0c31 | 2053 | fr = reg_read32(hcd->regs, HC_FRINDEX); |
db11e47d SS |
2054 | return (fr >> 3) % priv->periodic_size; |
2055 | } | |
2056 | ||
2057 | static void isp1760_stop(struct usb_hcd *hcd) | |
2058 | { | |
2059 | struct isp1760_hcd *priv = hcd_to_priv(hcd); | |
3faefc88 | 2060 | u32 temp; |
db11e47d | 2061 | |
6d50c60e AB |
2062 | del_timer(&errata2_timer); |
2063 | ||
db11e47d SS |
2064 | isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1, |
2065 | NULL, 0); | |
2066 | mdelay(20); | |
2067 | ||
2068 | spin_lock_irq(&priv->lock); | |
6bda21bc | 2069 | ehci_reset(hcd); |
db11e47d | 2070 | /* Disable IRQ */ |
bedc0c31 AB |
2071 | temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL); |
2072 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN); | |
db11e47d SS |
2073 | spin_unlock_irq(&priv->lock); |
2074 | ||
bedc0c31 | 2075 | reg_write32(hcd->regs, HC_CONFIGFLAG, 0); |
db11e47d SS |
2076 | } |
2077 | ||
2078 | static void isp1760_shutdown(struct usb_hcd *hcd) | |
2079 | { | |
3faefc88 | 2080 | u32 command, temp; |
db11e47d SS |
2081 | |
2082 | isp1760_stop(hcd); | |
bedc0c31 AB |
2083 | temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL); |
2084 | reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN); | |
db11e47d | 2085 | |
bedc0c31 | 2086 | command = reg_read32(hcd->regs, HC_USBCMD); |
db11e47d | 2087 | command &= ~CMD_RUN; |
bedc0c31 | 2088 | reg_write32(hcd->regs, HC_USBCMD, command); |
db11e47d SS |
2089 | } |
2090 | ||
2091 | static const struct hc_driver isp1760_hc_driver = { | |
2092 | .description = "isp1760-hcd", | |
2093 | .product_desc = "NXP ISP1760 USB Host Controller", | |
2094 | .hcd_priv_size = sizeof(struct isp1760_hcd), | |
2095 | .irq = isp1760_irq, | |
2096 | .flags = HCD_MEMORY | HCD_USB2, | |
2097 | .reset = isp1760_hc_setup, | |
2098 | .start = isp1760_run, | |
2099 | .stop = isp1760_stop, | |
2100 | .shutdown = isp1760_shutdown, | |
2101 | .urb_enqueue = isp1760_urb_enqueue, | |
2102 | .urb_dequeue = isp1760_urb_dequeue, | |
2103 | .endpoint_disable = isp1760_endpoint_disable, | |
2104 | .get_frame_number = isp1760_get_frame, | |
2105 | .hub_status_data = isp1760_hub_status_data, | |
2106 | .hub_control = isp1760_hub_control, | |
2107 | }; | |
2108 | ||
2109 | int __init init_kmem_once(void) | |
2110 | { | |
71a9f9d2 AB |
2111 | urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem", |
2112 | sizeof(struct urb_listitem), 0, SLAB_TEMPORARY | | |
2113 | SLAB_MEM_SPREAD, NULL); | |
2114 | ||
2115 | if (!urb_listitem_cachep) | |
2116 | return -ENOMEM; | |
2117 | ||
db11e47d SS |
2118 | qtd_cachep = kmem_cache_create("isp1760_qtd", |
2119 | sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY | | |
2120 | SLAB_MEM_SPREAD, NULL); | |
2121 | ||
2122 | if (!qtd_cachep) | |
2123 | return -ENOMEM; | |
2124 | ||
2125 | qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh), | |
2126 | 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); | |
2127 | ||
2128 | if (!qh_cachep) { | |
2129 | kmem_cache_destroy(qtd_cachep); | |
2130 | return -ENOMEM; | |
2131 | } | |
2132 | ||
2133 | return 0; | |
2134 | } | |
2135 | ||
2136 | void deinit_kmem_cache(void) | |
2137 | { | |
2138 | kmem_cache_destroy(qtd_cachep); | |
2139 | kmem_cache_destroy(qh_cachep); | |
71a9f9d2 | 2140 | kmem_cache_destroy(urb_listitem_cachep); |
db11e47d SS |
2141 | } |
2142 | ||
f9031f2c CM |
2143 | struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len, |
2144 | int irq, unsigned long irqflags, | |
2145 | struct device *dev, const char *busname, | |
2146 | unsigned int devflags) | |
db11e47d SS |
2147 | { |
2148 | struct usb_hcd *hcd; | |
2149 | struct isp1760_hcd *priv; | |
2150 | int ret; | |
2151 | ||
2152 | if (usb_disabled()) | |
2153 | return ERR_PTR(-ENODEV); | |
2154 | ||
2155 | /* prevent usb-core allocating DMA pages */ | |
2156 | dev->dma_mask = NULL; | |
2157 | ||
0031a06e | 2158 | hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev)); |
db11e47d SS |
2159 | if (!hcd) |
2160 | return ERR_PTR(-ENOMEM); | |
2161 | ||
2162 | priv = hcd_to_priv(hcd); | |
3faefc88 | 2163 | priv->devflags = devflags; |
db11e47d SS |
2164 | init_memory(priv); |
2165 | hcd->regs = ioremap(res_start, res_len); | |
2166 | if (!hcd->regs) { | |
2167 | ret = -EIO; | |
2168 | goto err_put; | |
2169 | } | |
2170 | ||
db11e47d SS |
2171 | hcd->irq = irq; |
2172 | hcd->rsrc_start = res_start; | |
2173 | hcd->rsrc_len = res_len; | |
2174 | ||
e6942d63 NC |
2175 | ret = usb_add_hcd(hcd, irq, irqflags); |
2176 | if (ret) | |
2177 | goto err_unmap; | |
2178 | ||
db11e47d SS |
2179 | return hcd; |
2180 | ||
2181 | err_unmap: | |
2182 | iounmap(hcd->regs); | |
2183 | ||
2184 | err_put: | |
2185 | usb_put_hcd(hcd); | |
2186 | ||
2187 | return ERR_PTR(ret); | |
2188 | } | |
2189 | ||
2190 | MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP"); | |
2191 | MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>"); | |
2192 | MODULE_LICENSE("GPL v2"); |