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