isp1760-hcd.c 57.3 KB
Newer Older
S
Sebastian Siewior 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Driver for the NXP ISP1760 chip
 *
 * However, the code might contain some bugs. What doesn't work for sure is:
 * - ISO
 * - OTG
 e The interrupt line is configured as active low, level.
 *
 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
 *
11 12
 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
 *
S
Sebastian Siewior 已提交
13 14 15 16 17 18
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/usb.h>
19
#include <linux/usb/hcd.h>
S
Sebastian Siewior 已提交
20 21 22
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
23
#include <linux/mm.h>
24
#include <linux/timer.h>
S
Sebastian Siewior 已提交
25
#include <asm/unaligned.h>
26
#include <asm/cacheflush.h>
S
Sebastian Siewior 已提交
27 28 29 30 31

#include "isp1760-hcd.h"

static struct kmem_cache *qtd_cachep;
static struct kmem_cache *qh_cachep;
32
static struct kmem_cache *urb_listitem_cachep;
S
Sebastian Siewior 已提交
33 34 35 36

struct isp1760_hcd {
	u32 hcs_params;
	spinlock_t		lock;
37
	struct slotinfo		atl_slots[32];
38
	int			atl_done_map;
39
	struct slotinfo		int_slots[32];
40
	int			int_done_map;
S
Sebastian Siewior 已提交
41
	struct memory_chunk memory_pool[BLOCKS];
42
	struct list_head	controlqhs, bulkqhs, interruptqhs;
S
Sebastian Siewior 已提交
43 44 45 46 47 48 49

	/* periodic schedule support */
#define	DEFAULT_I_TDPS		1024
	unsigned		periodic_size;
	unsigned		i_thresh;
	unsigned long		reset_done;
	unsigned long		next_statechange;
50
	unsigned int		devflags;
S
Sebastian Siewior 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
};

static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
{
	return (struct isp1760_hcd *) (hcd->hcd_priv);
}

/* Section 2.2 Host Controller Capability Registers */
#define HC_LENGTH(p)		(((p)>>00)&0x00ff)	/* bits 7:0 */
#define HC_VERSION(p)		(((p)>>16)&0xffff)	/* bits 31:16 */
#define HCS_INDICATOR(p)	((p)&(1 << 16))	/* true: has port indicators */
#define HCS_PPC(p)		((p)&(1 << 4))	/* true: port power control */
#define HCS_N_PORTS(p)		(((p)>>0)&0xf)	/* bits 3:0, ports on HC */
#define HCC_ISOC_CACHE(p)       ((p)&(1 << 7))  /* true: can cache isoc frame */
#define HCC_ISOC_THRES(p)       (((p)>>4)&0x7)  /* bits 6:4, uframes cached */

/* Section 2.3 Host Controller Operational Registers */
#define CMD_LRESET	(1<<7)		/* partial reset (no ports, etc) */
#define CMD_RESET	(1<<1)		/* reset HC not bus */
#define CMD_RUN		(1<<0)		/* start/stop HC */
#define STS_PCD		(1<<2)		/* port change detect */
#define FLAG_CF		(1<<0)		/* true: we'll support "high speed" */

#define PORT_OWNER	(1<<13)		/* true: companion hc owns this port */
#define PORT_POWER	(1<<12)		/* true: has power (see PPC) */
#define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10))	/* USB 1.1 device */
#define PORT_RESET	(1<<8)		/* reset port */
#define PORT_SUSPEND	(1<<7)		/* suspend port */
#define PORT_RESUME	(1<<6)		/* resume it */
#define PORT_PE		(1<<2)		/* port enable */
#define PORT_CSC	(1<<1)		/* connect status change */
#define PORT_CONNECT	(1<<0)		/* device connected */
#define PORT_RWC_BITS   (PORT_CSC)

struct isp1760_qtd {
	u8 packet_type;
	void *data_buffer;
88 89
	u32 payload_addr;

S
Sebastian Siewior 已提交
90 91 92 93
	/* the rest is HCD-private */
	struct list_head qtd_list;
	struct urb *urb;
	size_t length;
94 95 96 97 98 99 100 101 102 103 104 105 106
	size_t actual_length;

	/* QTD_ENQUEUED:	waiting for transfer (inactive) */
	/* QTD_PAYLOAD_ALLOC:	chip mem has been allocated for payload */
	/* QTD_XFER_STARTED:	valid ptd has been written to isp176x - only
				interrupt handler may touch this qtd! */
	/* QTD_XFER_COMPLETE:	payload has been transferred successfully */
	/* QTD_RETIRE:		transfer error/abort qtd */
#define QTD_ENQUEUED		0
#define QTD_PAYLOAD_ALLOC	1
#define QTD_XFER_STARTED	2
#define QTD_XFER_COMPLETE	3
#define QTD_RETIRE		4
S
Sebastian Siewior 已提交
107 108 109
	u32 status;
};

110
/* Queue head, one for each active endpoint */
S
Sebastian Siewior 已提交
111
struct isp1760_qh {
112
	struct list_head qh_list;
S
Sebastian Siewior 已提交
113 114 115
	struct list_head qtd_list;
	u32 toggle;
	u32 ping;
116
	int slot;
117
	int tt_buffer_dirty;	/* See USB2.0 spec section 11.17.5 */
118 119 120 121 122
};

struct urb_listitem {
	struct list_head urb_list;
	struct urb *urb;
S
Sebastian Siewior 已提交
123 124
};

125 126 127 128
/*
 * Access functions for isp176x registers (addresses 0..0x03FF).
 */
static u32 reg_read32(void __iomem *base, u32 reg)
S
Sebastian Siewior 已提交
129
{
130
	return readl(base + reg);
S
Sebastian Siewior 已提交
131 132
}

133
static void reg_write32(void __iomem *base, u32 reg, u32 val)
S
Sebastian Siewior 已提交
134
{
135
	writel(val, base + reg);
S
Sebastian Siewior 已提交
136 137 138
}

/*
139 140 141 142 143 144 145 146 147 148
 * Access functions for isp176x memory (offset >= 0x0400).
 *
 * bank_reads8() reads memory locations prefetched by an earlier write to
 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
 * bank optimizations, you should use the more generic mem_reads8() below.
 *
 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
 * below.
 *
 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
S
Sebastian Siewior 已提交
149 150
 * doesn't quite work because some people have to enforce 32-bit access
 */
151 152
static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
							__u32 *dst, u32 bytes)
S
Sebastian Siewior 已提交
153
{
154
	__u32 __iomem *src;
S
Sebastian Siewior 已提交
155
	u32 val;
156 157
	__u8 *src_byteptr;
	__u8 *dst_byteptr;
S
Sebastian Siewior 已提交
158

159
	src = src_base + (bank_addr | src_offset);
S
Sebastian Siewior 已提交
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (src_offset < PAYLOAD_OFFSET) {
		while (bytes >= 4) {
			*dst = le32_to_cpu(__raw_readl(src));
			bytes -= 4;
			src++;
			dst++;
		}
	} else {
		while (bytes >= 4) {
			*dst = __raw_readl(src);
			bytes -= 4;
			src++;
			dst++;
		}
S
Sebastian Siewior 已提交
175 176
	}

177
	if (!bytes)
S
Sebastian Siewior 已提交
178 179 180 181 182
		return;

	/* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
	 * allocated.
	 */
183 184 185 186 187 188 189 190 191 192 193 194
	if (src_offset < PAYLOAD_OFFSET)
		val = le32_to_cpu(__raw_readl(src));
	else
		val = __raw_readl(src);

	dst_byteptr = (void *) dst;
	src_byteptr = (void *) &val;
	while (bytes > 0) {
		*dst_byteptr = *src_byteptr;
		dst_byteptr++;
		src_byteptr++;
		bytes--;
S
Sebastian Siewior 已提交
195 196 197
	}
}

198 199
static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
								u32 bytes)
S
Sebastian Siewior 已提交
200
{
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
	ndelay(90);
	bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
}

static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
						__u32 const *src, u32 bytes)
{
	__u32 __iomem *dst;

	dst = dst_base + dst_offset;

	if (dst_offset < PAYLOAD_OFFSET) {
		while (bytes >= 4) {
			__raw_writel(cpu_to_le32(*src), dst);
			bytes -= 4;
			src++;
			dst++;
		}
	} else {
		while (bytes >= 4) {
			__raw_writel(*src, dst);
			bytes -= 4;
			src++;
			dst++;
		}
S
Sebastian Siewior 已提交
227 228
	}

229
	if (!bytes)
S
Sebastian Siewior 已提交
230
		return;
231 232
	/* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
	 * extra bytes should not be read by the HW.
S
Sebastian Siewior 已提交
233 234
	 */

235 236 237 238
	if (dst_offset < PAYLOAD_OFFSET)
		__raw_writel(cpu_to_le32(*src), dst);
	else
		__raw_writel(*src, dst);
S
Sebastian Siewior 已提交
239 240
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
 */
static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
								struct ptd *ptd)
{
	reg_write32(base, HC_MEMORY_REG,
				ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
	ndelay(90);
	bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
						(void *) ptd, sizeof(*ptd));
}

static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
								struct ptd *ptd)
{
	mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
						&ptd->dw1, 7*sizeof(ptd->dw1));
	/* Make sure dw0 gets written last (after other dw's and after payload)
	   since it contains the enable bit */
	wmb();
	mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
							sizeof(ptd->dw0));
}


S
Sebastian Siewior 已提交
268 269 270
/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
static void init_memory(struct isp1760_hcd *priv)
{
271 272
	int i, curr;
	u32 payload_addr;
S
Sebastian Siewior 已提交
273

274
	payload_addr = PAYLOAD_OFFSET;
S
Sebastian Siewior 已提交
275
	for (i = 0; i < BLOCK_1_NUM; i++) {
276
		priv->memory_pool[i].start = payload_addr;
S
Sebastian Siewior 已提交
277 278
		priv->memory_pool[i].size = BLOCK_1_SIZE;
		priv->memory_pool[i].free = 1;
279
		payload_addr += priv->memory_pool[i].size;
S
Sebastian Siewior 已提交
280 281
	}

282 283 284 285 286 287
	curr = i;
	for (i = 0; i < BLOCK_2_NUM; i++) {
		priv->memory_pool[curr + i].start = payload_addr;
		priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
		priv->memory_pool[curr + i].free = 1;
		payload_addr += priv->memory_pool[curr + i].size;
S
Sebastian Siewior 已提交
288 289
	}

290 291 292 293 294 295
	curr = i;
	for (i = 0; i < BLOCK_3_NUM; i++) {
		priv->memory_pool[curr + i].start = payload_addr;
		priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
		priv->memory_pool[curr + i].free = 1;
		payload_addr += priv->memory_pool[curr + i].size;
S
Sebastian Siewior 已提交
296 297
	}

298
	WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
S
Sebastian Siewior 已提交
299 300
}

301
static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
302
{
303
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
304 305
	int i;

306
	WARN_ON(qtd->payload_addr);
307 308 309

	if (!qtd->length)
		return;
S
Sebastian Siewior 已提交
310 311

	for (i = 0; i < BLOCKS; i++) {
312
		if (priv->memory_pool[i].size >= qtd->length &&
S
Sebastian Siewior 已提交
313 314
				priv->memory_pool[i].free) {
			priv->memory_pool[i].free = 0;
315 316
			qtd->payload_addr = priv->memory_pool[i].start;
			return;
S
Sebastian Siewior 已提交
317 318 319 320
		}
	}
}

321
static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
322
{
323
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
324 325
	int i;

326
	if (!qtd->payload_addr)
S
Sebastian Siewior 已提交
327 328 329
		return;

	for (i = 0; i < BLOCKS; i++) {
330
		if (priv->memory_pool[i].start == qtd->payload_addr) {
331
			WARN_ON(priv->memory_pool[i].free);
S
Sebastian Siewior 已提交
332
			priv->memory_pool[i].free = 1;
333 334
			qtd->payload_addr = 0;
			return;
S
Sebastian Siewior 已提交
335 336 337
		}
	}

338 339
	dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
						__func__, qtd->payload_addr);
340 341
	WARN_ON(1);
	qtd->payload_addr = 0;
S
Sebastian Siewior 已提交
342 343
}

344
static int handshake(struct usb_hcd *hcd, u32 reg,
S
Sebastian Siewior 已提交
345 346 347 348 349
		      u32 mask, u32 done, int usec)
{
	u32 result;

	do {
350
		result = reg_read32(hcd->regs, reg);
S
Sebastian Siewior 已提交
351 352 353 354 355 356 357 358 359 360 361 362
		if (result == ~0)
			return -ENODEV;
		result &= mask;
		if (result == done)
			return 0;
		udelay(1);
		usec--;
	} while (usec > 0);
	return -ETIMEDOUT;
}

/* reset a non-running (STS_HALT == 1) controller */
363
static int ehci_reset(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
364 365
{
	int retval;
366 367
	struct isp1760_hcd *priv = hcd_to_priv(hcd);

368
	u32 command = reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
369 370

	command |= CMD_RESET;
371
	reg_write32(hcd->regs, HC_USBCMD, command);
S
Sebastian Siewior 已提交
372 373
	hcd->state = HC_STATE_HALT;
	priv->next_statechange = jiffies;
374
	retval = handshake(hcd, HC_USBCMD,
S
Sebastian Siewior 已提交
375 376 377 378
			    CMD_RESET, 0, 250 * 1000);
	return retval;
}

379
static struct isp1760_qh *qh_alloc(gfp_t flags)
S
Sebastian Siewior 已提交
380 381 382 383 384
{
	struct isp1760_qh *qh;

	qh = kmem_cache_zalloc(qh_cachep, flags);
	if (!qh)
385
		return NULL;
S
Sebastian Siewior 已提交
386

387
	INIT_LIST_HEAD(&qh->qh_list);
S
Sebastian Siewior 已提交
388
	INIT_LIST_HEAD(&qh->qtd_list);
389 390
	qh->slot = -1;

S
Sebastian Siewior 已提交
391 392 393
	return qh;
}

394 395 396 397 398 399
static void qh_free(struct isp1760_qh *qh)
{
	WARN_ON(!list_empty(&qh->qtd_list));
	WARN_ON(qh->slot > -1);
	kmem_cache_free(qh_cachep, qh);
}
S
Sebastian Siewior 已提交
400 401 402 403 404 405 406 407 408

/* one-time init, only for memory state */
static int priv_init(struct usb_hcd *hcd)
{
	struct isp1760_hcd		*priv = hcd_to_priv(hcd);
	u32			hcc_params;

	spin_lock_init(&priv->lock);

409 410 411 412
	INIT_LIST_HEAD(&priv->interruptqhs);
	INIT_LIST_HEAD(&priv->controlqhs);
	INIT_LIST_HEAD(&priv->bulkqhs);

S
Sebastian Siewior 已提交
413 414 415 416 417 418 419
	/*
	 * hw default: 1K periodic list heads, one per frame.
	 * periodic_size can shrink by USBCMD update if hcc_params allows.
	 */
	priv->periodic_size = DEFAULT_I_TDPS;

	/* controllers may cache some of the periodic schedule ... */
420
	hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
S
Sebastian Siewior 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433
	/* full frame cache */
	if (HCC_ISOC_CACHE(hcc_params))
		priv->i_thresh = 8;
	else /* N microframes cached */
		priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);

	return 0;
}

static int isp1760_hc_setup(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	int result;
434 435 436 437 438 439 440 441 442 443 444 445 446
	u32 scratch, hwmode;

	/* Setup HW Mode Control: This assumes a level active-low interrupt */
	hwmode = HW_DATA_BUS_32BIT;

	if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
		hwmode &= ~HW_DATA_BUS_32BIT;
	if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
		hwmode |= HW_ANA_DIGI_OC;
	if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
		hwmode |= HW_DACK_POL_HIGH;
	if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
		hwmode |= HW_DREQ_POL_HIGH;
447 448 449 450
	if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
		hwmode |= HW_INTR_HIGH_ACT;
	if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
		hwmode |= HW_INTR_EDGE_TRIG;
451 452 453 454 455 456

	/*
	 * We have to set this first in case we're in 16-bit mode.
	 * Write it twice to ensure correct upper bits if switching
	 * to 16-bit mode.
	 */
457 458
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
S
Sebastian Siewior 已提交
459

460
	reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
461
	/* Change bus pattern */
462 463
	scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
	scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
S
Sebastian Siewior 已提交
464
	if (scratch != 0xdeadbabe) {
465
		dev_err(hcd->self.controller, "Scratch test failed.\n");
S
Sebastian Siewior 已提交
466 467 468 469
		return -ENODEV;
	}

	/* pre reset */
470 471 472 473
	reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
	reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
	reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
	reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
S
Sebastian Siewior 已提交
474 475

	/* reset */
476
	reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
S
Sebastian Siewior 已提交
477 478
	mdelay(100);

479
	reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
S
Sebastian Siewior 已提交
480 481
	mdelay(100);

482
	result = ehci_reset(hcd);
S
Sebastian Siewior 已提交
483 484 485 486 487
	if (result)
		return result;

	/* Step 11 passed */

488
	dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
489 490 491
			   (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
			   16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
			   "analog" : "digital");
S
Sebastian Siewior 已提交
492 493

	/* ATL reset */
494
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
S
Sebastian Siewior 已提交
495
	mdelay(10);
496
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
S
Sebastian Siewior 已提交
497

498
	reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
499 500 501

	/*
	 * PORT 1 Control register of the ISP1760 is the OTG control
502 503 504
	 * register on ISP1761. Since there is no OTG or device controller
	 * support in this driver, we use port 1 as a "normal" USB host port on
	 * both chips.
505
	 */
506
	reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
507
	mdelay(10);
S
Sebastian Siewior 已提交
508

509
	priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
S
Sebastian Siewior 已提交
510 511 512 513 514 515 516 517 518

	return priv_init(hcd);
}

static u32 base_to_chip(u32 base)
{
	return ((base - 0x400) >> 3);
}

519 520 521 522 523 524 525 526 527 528 529 530
static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
{
	struct urb *urb;

	if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
		return 1;

	urb = qtd->urb;
	qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
	return (qtd->urb != urb);
}

531 532 533 534 535 536 537 538 539
/* magic numbers that can affect system performance */
#define	EHCI_TUNE_CERR		3	/* 0-3 qtd retries; 0 == don't stop */
#define	EHCI_TUNE_RL_HS		4	/* nak throttle; see 4.9 */
#define	EHCI_TUNE_RL_TT		0
#define	EHCI_TUNE_MULT_HS	1	/* 1-3 transactions/uframe; 4.10.3 */
#define	EHCI_TUNE_MULT_TT	1
#define	EHCI_TUNE_FLS		2	/* (small) 256 frame schedule */

static void create_ptd_atl(struct isp1760_qh *qh,
540
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
541 542 543 544 545 546
{
	u32 maxpacket;
	u32 multi;
	u32 rl = RL_COUNTER;
	u32 nak = NAK_COUNTER;

547 548
	memset(ptd, 0, sizeof(*ptd));

S
Sebastian Siewior 已提交
549
	/* according to 3.6.2, max packet len can not be > 0x400 */
550 551
	maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
						usb_pipeout(qtd->urb->pipe));
S
Sebastian Siewior 已提交
552 553 554 555
	multi =  1 + ((maxpacket >> 11) & 0x3);
	maxpacket &= 0x7ff;

	/* DW0 */
556 557 558 559
	ptd->dw0 = DW0_VALID_BIT;
	ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
	ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
	ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
S
Sebastian Siewior 已提交
560 561

	/* DW1 */
562
	ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
563 564
	ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
	ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
S
Sebastian Siewior 已提交
565

566
	if (usb_pipebulk(qtd->urb->pipe))
567
		ptd->dw1 |= DW1_TRANS_BULK;
568
	else if  (usb_pipeint(qtd->urb->pipe))
569
		ptd->dw1 |= DW1_TRANS_INT;
S
Sebastian Siewior 已提交
570

571
	if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
S
Sebastian Siewior 已提交
572 573
		/* split transaction */

574
		ptd->dw1 |= DW1_TRANS_SPLIT;
575
		if (qtd->urb->dev->speed == USB_SPEED_LOW)
576
			ptd->dw1 |= DW1_SE_USB_LOSPEED;
S
Sebastian Siewior 已提交
577

578 579
		ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
		ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
S
Sebastian Siewior 已提交
580 581

		/* SE bit for Split INT transfers */
582 583
		if (usb_pipeint(qtd->urb->pipe) &&
				(qtd->urb->dev->speed == USB_SPEED_LOW))
584
			ptd->dw1 |= 2 << 16;
S
Sebastian Siewior 已提交
585 586 587 588

		rl = 0;
		nak = 0;
	} else {
589
		ptd->dw0 |= TO_DW0_MULTI(multi);
590 591
		if (usb_pipecontrol(qtd->urb->pipe) ||
						usb_pipebulk(qtd->urb->pipe))
592
			ptd->dw3 |= TO_DW3_PING(qh->ping);
S
Sebastian Siewior 已提交
593 594
	}
	/* DW2 */
595
	ptd->dw2 = 0;
596 597
	ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
	ptd->dw2 |= TO_DW2_RL(rl);
S
Sebastian Siewior 已提交
598 599

	/* DW3 */
600 601
	ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
	ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
602 603
	if (usb_pipecontrol(qtd->urb->pipe)) {
		if (qtd->data_buffer == qtd->urb->setup_packet)
604
			ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
605
		else if (last_qtd_of_urb(qtd, qh))
606
			ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
607
	}
S
Sebastian Siewior 已提交
608

609
	ptd->dw3 |= DW3_ACTIVE_BIT;
S
Sebastian Siewior 已提交
610
	/* Cerr */
611
	ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
S
Sebastian Siewior 已提交
612 613
}

614
static void transform_add_int(struct isp1760_qh *qh,
615
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
616
{
617
	u32 usof;
S
Sebastian Siewior 已提交
618 619
	u32 period;

620 621 622 623 624 625 626 627
	/*
	 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
	 * the algorithm from the original Philips driver code, which was
	 * pretty much used in this driver before as well, is quite horrendous
	 * and, i believe, incorrect. The code below follows the datasheet and
	 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
	 * more reliable this way (fingers crossed...).
	 */
S
Sebastian Siewior 已提交
628

629 630 631 632 633 634 635 636 637 638 639
	if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
		/* urb->interval is in units of microframes (1/8 ms) */
		period = qtd->urb->interval >> 3;

		if (qtd->urb->interval > 4)
			usof = 0x01; /* One bit set =>
						interval 1 ms * uFrame-match */
		else if (qtd->urb->interval > 2)
			usof = 0x22; /* Two bits set => interval 1/2 ms */
		else if (qtd->urb->interval > 1)
			usof = 0x55; /* Four bits set => interval 1/4 ms */
S
Sebastian Siewior 已提交
640
		else
641
			usof = 0xff; /* All bits set => interval 1/8 ms */
S
Sebastian Siewior 已提交
642
	} else {
643 644 645 646 647 648 649 650 651 652 653 654 655 656
		/* urb->interval is in units of frames (1 ms) */
		period = qtd->urb->interval;
		usof = 0x0f;		/* Execute Start Split on any of the
					   four first uFrames */

		/*
		 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
		 * complete split needs to be sent. Valid only for IN." Also,
		 * "All bits can be set to one for every transfer." (p 82,
		 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
		 * that number come from? 0xff seems to work fine...
		 */
		/* ptd->dw5 = 0x1c; */
		ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
S
Sebastian Siewior 已提交
657 658
	}

659 660 661
	period = period >> 1;/* Ensure equal or shorter period than requested */
	period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */

662 663
	ptd->dw2 |= period;
	ptd->dw4 = usof;
S
Sebastian Siewior 已提交
664 665
}

666
static void create_ptd_int(struct isp1760_qh *qh,
667
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
668
{
669
	create_ptd_atl(qh, qtd, ptd);
670
	transform_add_int(qh, qtd, ptd);
S
Sebastian Siewior 已提交
671 672
}

673
static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
S
Sebastian Siewior 已提交
674 675 676
__releases(priv->lock)
__acquires(priv->lock)
{
677 678
	struct isp1760_hcd *priv = hcd_to_priv(hcd);

S
Sebastian Siewior 已提交
679
	if (!urb->unlinked) {
680 681
		if (urb->status == -EINPROGRESS)
			urb->status = 0;
S
Sebastian Siewior 已提交
682 683
	}

684 685 686 687 688 689 690 691
	if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
		void *ptr;
		for (ptr = urb->transfer_buffer;
		     ptr < urb->transfer_buffer + urb->transfer_buffer_length;
		     ptr += PAGE_SIZE)
			flush_dcache_page(virt_to_page(ptr));
	}

S
Sebastian Siewior 已提交
692
	/* complete() can reenter this HCD */
693
	usb_hcd_unlink_urb_from_ep(hcd, urb);
S
Sebastian Siewior 已提交
694
	spin_unlock(&priv->lock);
695
	usb_hcd_giveback_urb(hcd, urb, urb->status);
S
Sebastian Siewior 已提交
696 697 698
	spin_lock(&priv->lock);
}

699 700
static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
								u8 packet_type)
S
Sebastian Siewior 已提交
701
{
702 703 704 705 706 707 708 709 710
	struct isp1760_qtd *qtd;

	qtd = kmem_cache_zalloc(qtd_cachep, flags);
	if (!qtd)
		return NULL;

	INIT_LIST_HEAD(&qtd->qtd_list);
	qtd->urb = urb;
	qtd->packet_type = packet_type;
711 712
	qtd->status = QTD_ENQUEUED;
	qtd->actual_length = 0;
713 714 715 716 717 718 719

	return qtd;
}

static void qtd_free(struct isp1760_qtd *qtd)
{
	WARN_ON(qtd->payload_addr);
S
Sebastian Siewior 已提交
720 721 722
	kmem_cache_free(qtd_cachep, qtd);
}

723 724 725
static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
				struct slotinfo *slots, struct isp1760_qtd *qtd,
				struct isp1760_qh *qh, struct ptd *ptd)
S
Sebastian Siewior 已提交
726
{
727
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
728 729
	int skip_map;

730 731 732 733 734 735
	WARN_ON((slot < 0) || (slot > 31));
	WARN_ON(qtd->length && !qtd->payload_addr);
	WARN_ON(slots[slot].qtd);
	WARN_ON(slots[slot].qh);
	WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);

736 737 738 739
	/* Make sure done map has not triggered from some unlinked transfer */
	if (ptd_offset == ATL_PTD_OFFSET) {
		priv->atl_done_map |= reg_read32(hcd->regs,
						HC_ATL_PTD_DONEMAP_REG);
740 741 742 743 744 745
		priv->atl_done_map &= ~(1 << slot);
	} else {
		priv->int_done_map |= reg_read32(hcd->regs,
						HC_INT_PTD_DONEMAP_REG);
		priv->int_done_map &= ~(1 << slot);
	}
746

747 748 749 750 751 752 753 754
	qh->slot = slot;
	qtd->status = QTD_XFER_STARTED;
	slots[slot].timestamp = jiffies;
	slots[slot].qtd = qtd;
	slots[slot].qh = qh;
	ptd_write(hcd->regs, ptd_offset, slot, ptd);

	if (ptd_offset == ATL_PTD_OFFSET) {
755 756 757 758 759 760 761 762
		skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
		skip_map &= ~(1 << qh->slot);
		reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
	} else {
		skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
		skip_map &= ~(1 << qh->slot);
		reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
	}
S
Sebastian Siewior 已提交
763 764
}

765
static int is_short_bulk(struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
766
{
767 768
	return (usb_pipebulk(qtd->urb->pipe) &&
					(qtd->actual_length < qtd->length));
S
Sebastian Siewior 已提交
769 770
}

771 772
static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
						struct list_head *urb_list)
S
Sebastian Siewior 已提交
773
{
774 775 776
	int last_qtd;
	struct isp1760_qtd *qtd, *qtd_next;
	struct urb_listitem *urb_listitem;
S
Sebastian Siewior 已提交
777

778 779 780
	list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
		if (qtd->status < QTD_XFER_COMPLETE)
			break;
S
Sebastian Siewior 已提交
781

782
		last_qtd = last_qtd_of_urb(qtd, qh);
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

		if ((!last_qtd) && (qtd->status == QTD_RETIRE))
			qtd_next->status = QTD_RETIRE;

		if (qtd->status == QTD_XFER_COMPLETE) {
			if (qtd->actual_length) {
				switch (qtd->packet_type) {
				case IN_PID:
					mem_reads8(hcd->regs, qtd->payload_addr,
							qtd->data_buffer,
							qtd->actual_length);
					/* Fall through (?) */
				case OUT_PID:
					qtd->urb->actual_length +=
							qtd->actual_length;
					/* Fall through ... */
				case SETUP_PID:
					break;
				}
			}
S
Sebastian Siewior 已提交
803

804 805 806 807 808 809 810
			if (is_short_bulk(qtd)) {
				if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
					qtd->urb->status = -EREMOTEIO;
				if (!last_qtd)
					qtd_next->status = QTD_RETIRE;
			}
		}
S
Sebastian Siewior 已提交
811

812 813
		if (qtd->payload_addr)
			free_mem(hcd, qtd);
S
Sebastian Siewior 已提交
814

815 816 817 818 819 820 821 822
		if (last_qtd) {
			if ((qtd->status == QTD_RETIRE) &&
					(qtd->urb->status == -EINPROGRESS))
				qtd->urb->status = -EPIPE;
			/* Defer calling of urb_done() since it releases lock */
			urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
								GFP_ATOMIC);
			if (unlikely(!urb_listitem))
823
				break; /* Try again on next call */
824 825 826
			urb_listitem->urb = qtd->urb;
			list_add_tail(&urb_listitem->urb_list, urb_list);
		}
827

828 829 830 831
		list_del(&qtd->qtd_list);
		qtd_free(qtd);
	}
}
832

833 834 835 836 837 838 839 840 841 842
#define ENQUEUE_DEPTH	2
static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	int ptd_offset;
	struct slotinfo *slots;
	int curr_slot, free_slot;
	int n;
	struct ptd ptd;
	struct isp1760_qtd *qtd;
S
Sebastian Siewior 已提交
843

844 845 846 847
	if (unlikely(list_empty(&qh->qtd_list))) {
		WARN_ON(1);
		return;
	}
S
Sebastian Siewior 已提交
848

849 850 851 852
	/* Make sure this endpoint's TT buffer is clean before queueing ptds */
	if (qh->tt_buffer_dirty)
		return;

853 854 855 856 857 858 859 860
	if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
							qtd_list)->urb->pipe)) {
		ptd_offset = INT_PTD_OFFSET;
		slots = priv->int_slots;
	} else {
		ptd_offset = ATL_PTD_OFFSET;
		slots = priv->atl_slots;
	}
S
Sebastian Siewior 已提交
861

862 863 864 865 866 867 868
	free_slot = -1;
	for (curr_slot = 0; curr_slot < 32; curr_slot++) {
		if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
			free_slot = curr_slot;
		if (slots[curr_slot].qh == qh)
			break;
	}
S
Sebastian Siewior 已提交
869

870 871 872 873 874 875 876
	n = 0;
	list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
		if (qtd->status == QTD_ENQUEUED) {
			WARN_ON(qtd->payload_addr);
			alloc_mem(hcd, qtd);
			if ((qtd->length) && (!qtd->payload_addr))
				break;
S
Sebastian Siewior 已提交
877

878 879 880 881 882 883
			if ((qtd->length) &&
			    ((qtd->packet_type == SETUP_PID) ||
			     (qtd->packet_type == OUT_PID))) {
				mem_writes8(hcd->regs, qtd->payload_addr,
						qtd->data_buffer, qtd->length);
			}
S
Sebastian Siewior 已提交
884

885
			qtd->status = QTD_PAYLOAD_ALLOC;
S
Sebastian Siewior 已提交
886 887
		}

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
		if (qtd->status == QTD_PAYLOAD_ALLOC) {
/*
			if ((curr_slot > 31) && (free_slot == -1))
				dev_dbg(hcd->self.controller, "%s: No slot "
					"available for transfer\n", __func__);
*/
			/* Start xfer for this endpoint if not already done */
			if ((curr_slot > 31) && (free_slot > -1)) {
				if (usb_pipeint(qtd->urb->pipe))
					create_ptd_int(qh, qtd, &ptd);
				else
					create_ptd_atl(qh, qtd, &ptd);

				start_bus_transfer(hcd, ptd_offset, free_slot,
							slots, qtd, qh, &ptd);
				curr_slot = free_slot;
			}
S
Sebastian Siewior 已提交
905

906 907 908 909 910 911
			n++;
			if (n >= ENQUEUE_DEPTH)
				break;
		}
	}
}
S
Sebastian Siewior 已提交
912

913 914 915 916 917 918 919 920 921 922 923 924 925
void schedule_ptds(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv;
	struct isp1760_qh *qh, *qh_next;
	struct list_head *ep_queue;
	struct usb_host_endpoint *ep;
	LIST_HEAD(urb_list);
	struct urb_listitem *urb_listitem, *urb_listitem_next;

	if (!hcd) {
		WARN_ON(1);
		return;
	}
S
Sebastian Siewior 已提交
926

927
	priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
928

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	/*
	 * check finished/retired xfers, transfer payloads, call urb_done()
	 */
	ep_queue = &priv->interruptqhs;
	while (ep_queue) {
		list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
			ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
							qtd_list)->urb->ep;
			collect_qtds(hcd, qh, &urb_list);
			if (list_empty(&qh->qtd_list)) {
				list_del(&qh->qh_list);
				if (ep->hcpriv == NULL) {
					/* Endpoint has been disabled, so we
					can free the associated queue head. */
					qh_free(qh);
				}
S
Sebastian Siewior 已提交
945 946 947
			}
		}

948 949 950 951 952 953 954
		if (ep_queue == &priv->interruptqhs)
			ep_queue = &priv->controlqhs;
		else if (ep_queue == &priv->controlqhs)
			ep_queue = &priv->bulkqhs;
		else
			ep_queue = NULL;
	}
S
Sebastian Siewior 已提交
955

956 957 958 959 960
	list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
								urb_list) {
		isp1760_urb_done(hcd, urb_listitem->urb);
		kmem_cache_free(urb_listitem_cachep, urb_listitem);
	}
S
Sebastian Siewior 已提交
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	/*
	 * Schedule packets for transfer.
	 *
	 * According to USB2.0 specification:
	 *
	 * 1st prio: interrupt xfers, up to 80 % of bandwidth
	 * 2nd prio: control xfers
	 * 3rd prio: bulk xfers
	 *
	 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
	 * is very unclear on how to prioritize traffic):
	 *
	 * 1) Enqueue any queued control transfers, as long as payload chip mem
	 *    and PTD ATL slots are available.
	 * 2) Enqueue any queued INT transfers, as long as payload chip mem
	 *    and PTD INT slots are available.
	 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
	 *    and PTD ATL slots are available.
	 *
	 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
	 * conservation of chip mem and performance.
	 *
	 * I'm sure this scheme could be improved upon!
	 */
	ep_queue = &priv->controlqhs;
	while (ep_queue) {
		list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
			enqueue_qtds(hcd, qh);

		if (ep_queue == &priv->controlqhs)
			ep_queue = &priv->interruptqhs;
		else if (ep_queue == &priv->interruptqhs)
			ep_queue = &priv->bulkqhs;
		else
			ep_queue = NULL;
	}
}
S
Sebastian Siewior 已提交
999

1000 1001 1002
#define PTD_STATE_QTD_DONE	1
#define PTD_STATE_QTD_RELOAD	2
#define PTD_STATE_URB_RETIRE	3
S
Sebastian Siewior 已提交
1003

1004 1005 1006 1007 1008
static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
								struct urb *urb)
{
	__dw dw4;
	int i;
S
Sebastian Siewior 已提交
1009

1010 1011
	dw4 = ptd->dw4;
	dw4 >>= 8;
S
Sebastian Siewior 已提交
1012

1013 1014
	/* FIXME: ISP1761 datasheet does not say what to do with these. Do we
	   need to handle these errors? Is it done in hardware? */
S
Sebastian Siewior 已提交
1015

1016
	if (ptd->dw3 & DW3_HALT_BIT) {
S
Sebastian Siewior 已提交
1017

1018
		urb->status = -EPROTO; /* Default unknown error */
S
Sebastian Siewior 已提交
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		for (i = 0; i < 8; i++) {
			switch (dw4 & 0x7) {
			case INT_UNDERRUN:
				dev_dbg(hcd->self.controller, "%s: underrun "
						"during uFrame %d\n",
						__func__, i);
				urb->status = -ECOMM; /* Could not write data */
				break;
			case INT_EXACT:
				dev_dbg(hcd->self.controller, "%s: transaction "
						"error during uFrame %d\n",
						__func__, i);
				urb->status = -EPROTO; /* timeout, bad CRC, PID
							  error etc. */
				break;
			case INT_BABBLE:
				dev_dbg(hcd->self.controller, "%s: babble "
						"error during uFrame %d\n",
						__func__, i);
				urb->status = -EOVERFLOW;
				break;
			}
			dw4 >>= 3;
		}
S
Sebastian Siewior 已提交
1044

1045 1046
		return PTD_STATE_URB_RETIRE;
	}
S
Sebastian Siewior 已提交
1047

1048 1049
	return PTD_STATE_QTD_DONE;
}
S
Sebastian Siewior 已提交
1050

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
								struct urb *urb)
{
	WARN_ON(!ptd);
	if (ptd->dw3 & DW3_HALT_BIT) {
		if (ptd->dw3 & DW3_BABBLE_BIT)
			urb->status = -EOVERFLOW;
		else if (FROM_DW3_CERR(ptd->dw3))
			urb->status = -EPIPE;  /* Stall */
		else if (ptd->dw3 & DW3_ERROR_BIT)
			urb->status = -EPROTO; /* XactErr */
		else
			urb->status = -EPROTO; /* Unknown */
/*
		dev_dbg(hcd->self.controller, "%s: ptd error:\n"
			"        dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
			"        dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
			__func__,
			ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
			ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
*/
		return PTD_STATE_URB_RETIRE;
	}
S
Sebastian Siewior 已提交
1074

1075 1076 1077 1078 1079
	if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
		/* Transfer Error, *but* active and no HALT -> reload */
		dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
		return PTD_STATE_QTD_RELOAD;
	}
S
Sebastian Siewior 已提交
1080

1081 1082 1083 1084 1085 1086 1087
	if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
		/*
		 * NAKs are handled in HW by the chip. Usually if the
		 * device is not able to send data fast enough.
		 * This happens mostly on slower hardware.
		 */
		return PTD_STATE_QTD_RELOAD;
S
Sebastian Siewior 已提交
1088
	}
1089 1090

	return PTD_STATE_QTD_DONE;
S
Sebastian Siewior 已提交
1091 1092
}

1093
static void handle_done_ptds(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
1094
{
1095
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
1096 1097
	struct ptd ptd;
	struct isp1760_qh *qh;
1098 1099 1100 1101 1102 1103
	int slot;
	int state;
	struct slotinfo *slots;
	u32 ptd_offset;
	struct isp1760_qtd *qtd;
	int modified;
1104
	int skip_map;
1105

1106 1107 1108 1109
	skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
	priv->int_done_map &= ~skip_map;
	skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
	priv->atl_done_map &= ~skip_map;
1110

1111
	modified = priv->int_done_map || priv->atl_done_map;
1112 1113 1114

	while (priv->int_done_map || priv->atl_done_map) {
		if (priv->int_done_map) {
1115
			/* INT ptd */
1116 1117
			slot = __ffs(priv->int_done_map);
			priv->int_done_map &= ~(1 << slot);
1118
			slots = priv->int_slots;
1119 1120 1121 1122
			/* This should not trigger, and could be removed if
			   noone have any problems with it triggering: */
			if (!slots[slot].qh) {
				WARN_ON(1);
1123
				continue;
1124
			}
1125 1126 1127 1128
			ptd_offset = INT_PTD_OFFSET;
			ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
			state = check_int_transfer(hcd, &ptd,
							slots[slot].qtd->urb);
S
Sebastian Siewior 已提交
1129
		} else {
1130
			/* ATL ptd */
1131 1132
			slot = __ffs(priv->atl_done_map);
			priv->atl_done_map &= ~(1 << slot);
1133
			slots = priv->atl_slots;
1134 1135 1136 1137
			/* This should not trigger, and could be removed if
			   noone have any problems with it triggering: */
			if (!slots[slot].qh) {
				WARN_ON(1);
1138
				continue;
1139
			}
1140 1141 1142 1143
			ptd_offset = ATL_PTD_OFFSET;
			ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
			state = check_atl_transfer(hcd, &ptd,
							slots[slot].qtd->urb);
S
Sebastian Siewior 已提交
1144 1145
		}

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
		qtd = slots[slot].qtd;
		slots[slot].qtd = NULL;
		qh = slots[slot].qh;
		slots[slot].qh = NULL;
		qh->slot = -1;

		WARN_ON(qtd->status != QTD_XFER_STARTED);

		switch (state) {
		case PTD_STATE_QTD_DONE:
			if ((usb_pipeint(qtd->urb->pipe)) &&
				       (qtd->urb->dev->speed != USB_SPEED_HIGH))
				qtd->actual_length =
				       FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
			else
				qtd->actual_length =
					FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
S
Sebastian Siewior 已提交
1163

1164 1165 1166 1167 1168 1169 1170
			qtd->status = QTD_XFER_COMPLETE;
			if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
							is_short_bulk(qtd))
				qtd = NULL;
			else
				qtd = list_entry(qtd->qtd_list.next,
							typeof(*qtd), qtd_list);
S
Sebastian Siewior 已提交
1171

1172 1173 1174
			qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
			qh->ping = FROM_DW3_PING(ptd.dw3);
			break;
S
Sebastian Siewior 已提交
1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
		case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
			qtd->status = QTD_PAYLOAD_ALLOC;
			ptd.dw0 |= DW0_VALID_BIT;
			/* RL counter = ERR counter */
			ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
			ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
			ptd.dw3 &= ~TO_DW3_CERR(3);
			ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
			qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
			qh->ping = FROM_DW3_PING(ptd.dw3);
			break;
S
Sebastian Siewior 已提交
1187

1188 1189
		case PTD_STATE_URB_RETIRE:
			qtd->status = QTD_RETIRE;
1190 1191 1192 1193 1194 1195 1196 1197 1198
			if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
					(qtd->urb->status != -EPIPE) &&
					(qtd->urb->status != -EREMOTEIO)) {
				qh->tt_buffer_dirty = 1;
				if (usb_hub_clear_tt_buffer(qtd->urb))
					/* Clear failed; let's hope things work
					   anyway */
					qh->tt_buffer_dirty = 0;
			}
1199 1200 1201 1202
			qtd = NULL;
			qh->toggle = 0;
			qh->ping = 0;
			break;
S
Sebastian Siewior 已提交
1203

1204 1205 1206 1207
		default:
			WARN_ON(1);
			continue;
		}
S
Sebastian Siewior 已提交
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
		if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
			if (slots == priv->int_slots) {
				if (state == PTD_STATE_QTD_RELOAD)
					dev_err(hcd->self.controller,
						"%s: PTD_STATE_QTD_RELOAD on "
						"interrupt packet\n", __func__);
				if (state != PTD_STATE_QTD_RELOAD)
					create_ptd_int(qh, qtd, &ptd);
			} else {
				if (state != PTD_STATE_QTD_RELOAD)
					create_ptd_atl(qh, qtd, &ptd);
			}
S
Sebastian Siewior 已提交
1221

1222 1223 1224 1225
			start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
				qh, &ptd);
		}
	}
S
Sebastian Siewior 已提交
1226

1227 1228
	if (modified)
		schedule_ptds(hcd);
1229
}
S
Sebastian Siewior 已提交
1230

1231 1232 1233 1234 1235
static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	u32 imask;
	irqreturn_t irqret = IRQ_NONE;
S
Sebastian Siewior 已提交
1236

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	spin_lock(&priv->lock);

	if (!(hcd->state & HC_STATE_RUNNING))
		goto leave;

	imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
	if (unlikely(!imask))
		goto leave;
	reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */

	priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
	priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);

	handle_done_ptds(hcd);
S
Sebastian Siewior 已提交
1251

1252 1253 1254
	irqret = IRQ_HANDLED;
leave:
	spin_unlock(&priv->lock);
S
Sebastian Siewior 已提交
1255

1256
	return irqret;
S
Sebastian Siewior 已提交
1257 1258
}

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
/*
 * Workaround for problem described in chip errata 2:
 *
 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
 * One solution suggested in the errata is to use SOF interrupts _instead_of_
 * ATL done interrupts (the "instead of" might be important since it seems
 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
 * to set the PTD's done bit in addition to not generating an interrupt!).
 *
 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
 *
 * If we use SOF interrupts only, we get latency between ptd completion and the
 * actual handling. This is very noticeable in testusb runs which takes several
 * minutes longer without ATL interrupts.
 *
 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
 * completed and its done map bit is set.
 *
 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
 * not to cause too much lag when this HW bug occurs, while still hopefully
 * ensuring that the check does not falsely trigger.
 */
1284
#define SLOT_TIMEOUT 300
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
#define SLOT_CHECK_PERIOD 200
static struct timer_list errata2_timer;

void errata2_function(unsigned long data)
{
	struct usb_hcd *hcd = (struct usb_hcd *) data;
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	int slot;
	struct ptd ptd;
	unsigned long spinflags;

	spin_lock_irqsave(&priv->lock, spinflags);

	for (slot = 0; slot < 32; slot++)
1299 1300 1301
		if (priv->atl_slots[slot].qh && time_after(jiffies,
					priv->atl_slots[slot].timestamp +
					SLOT_TIMEOUT * HZ / 1000)) {
1302 1303 1304 1305 1306 1307
			ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
			if (!FROM_DW0_VALID(ptd.dw0) &&
					!FROM_DW3_ACTIVE(ptd.dw3))
				priv->atl_done_map |= 1 << slot;
		}

1308 1309
	if (priv->atl_done_map)
		handle_done_ptds(hcd);
1310 1311 1312 1313 1314 1315 1316

	spin_unlock_irqrestore(&priv->lock, spinflags);

	errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
	add_timer(&errata2_timer);
}

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
static int isp1760_run(struct usb_hcd *hcd)
{
	int retval;
	u32 temp;
	u32 command;
	u32 chipid;

	hcd->uses_new_polling = 1;

	hcd->state = HC_STATE_RUNNING;

	/* Set PTD interrupt AND & OR maps */
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
	reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
	/* step 23 passed */

	temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);

	command = reg_read32(hcd->regs, HC_USBCMD);
	command &= ~(CMD_LRESET|CMD_RESET);
	command |= CMD_RUN;
	reg_write32(hcd->regs, HC_USBCMD, command);

	retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
	if (retval)
		return retval;

	/*
	 * XXX
	 * Spec says to write FLAG_CF as last config action, priv code grabs
	 * the semaphore while doing so.
	 */
	down_write(&ehci_cf_port_reset_rwsem);
	reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);

	retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
	up_write(&ehci_cf_port_reset_rwsem);
	if (retval)
		return retval;

1362 1363 1364 1365 1366 1367
	init_timer(&errata2_timer);
	errata2_timer.function = errata2_function;
	errata2_timer.data = (unsigned long) hcd;
	errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
	add_timer(&errata2_timer);

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
	dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
					chipid & 0xffff, chipid >> 16);

	/* PTD Register Init Part 2, Step 28 */

	/* Setup registers controlling PTD checking */
	reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
	reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
	reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
	reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
	reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
	reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
	reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
						ATL_BUF_FILL | INT_BUF_FILL);

	/* GRR this is run-once init(), being done every time the HC starts.
	 * So long as they're part of class devices, we can't do it init()
	 * since the class device isn't created that early.
	 */
	return 0;
}

1391
static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
S
Sebastian Siewior 已提交
1392
{
1393
	qtd->data_buffer = databuffer;
S
Sebastian Siewior 已提交
1394

1395 1396 1397
	if (len > MAX_PAYLOAD_SIZE)
		len = MAX_PAYLOAD_SIZE;
	qtd->length = len;
S
Sebastian Siewior 已提交
1398

1399
	return qtd->length;
S
Sebastian Siewior 已提交
1400 1401
}

1402
static void qtd_list_free(struct list_head *qtd_list)
S
Sebastian Siewior 已提交
1403
{
1404
	struct isp1760_qtd *qtd, *qtd_next;
S
Sebastian Siewior 已提交
1405

1406
	list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
S
Sebastian Siewior 已提交
1407
		list_del(&qtd->qtd_list);
1408
		qtd_free(qtd);
S
Sebastian Siewior 已提交
1409 1410 1411 1412
	}
}

/*
1413 1414
 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
S
Sebastian Siewior 已提交
1415
 */
1416
#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1417
static void packetize_urb(struct usb_hcd *hcd,
S
Sebastian Siewior 已提交
1418 1419
		struct urb *urb, struct list_head *head, gfp_t flags)
{
1420
	struct isp1760_qtd *qtd;
S
Sebastian Siewior 已提交
1421
	void *buf;
1422 1423
	int len, maxpacketsize;
	u8 packet_type;
S
Sebastian Siewior 已提交
1424 1425 1426 1427 1428

	/*
	 * URBs map to sequences of QTDs:  one logical transaction
	 */

1429 1430 1431 1432 1433 1434 1435 1436
	if (!urb->transfer_buffer && urb->transfer_buffer_length) {
		/* XXX This looks like usb storage / SCSI bug */
		dev_err(hcd->self.controller,
				"buf is null, dma is %08lx len is %d\n",
				(long unsigned)urb->transfer_dma,
				urb->transfer_buffer_length);
		WARN_ON(1);
	}
S
Sebastian Siewior 已提交
1437

1438 1439 1440 1441
	if (usb_pipein(urb->pipe))
		packet_type = IN_PID;
	else
		packet_type = OUT_PID;
S
Sebastian Siewior 已提交
1442 1443

	if (usb_pipecontrol(urb->pipe)) {
1444
		qtd = qtd_alloc(flags, urb, SETUP_PID);
S
Sebastian Siewior 已提交
1445 1446
		if (!qtd)
			goto cleanup;
1447
		qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
S
Sebastian Siewior 已提交
1448 1449 1450
		list_add_tail(&qtd->qtd_list, head);

		/* for zero length DATA stages, STATUS is always IN */
1451 1452
		if (urb->transfer_buffer_length == 0)
			packet_type = IN_PID;
S
Sebastian Siewior 已提交
1453 1454
	}

1455 1456
	maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
						usb_pipeout(urb->pipe)));
S
Sebastian Siewior 已提交
1457 1458 1459 1460 1461 1462

	/*
	 * buffer gets wrapped in one or more qtds;
	 * last one may be "short" (including zero len)
	 * and may serve as a control status ack
	 */
1463 1464 1465
	buf = urb->transfer_buffer;
	len = urb->transfer_buffer_length;

S
Sebastian Siewior 已提交
1466 1467 1468
	for (;;) {
		int this_qtd_len;

1469 1470 1471 1472 1473
		qtd = qtd_alloc(flags, urb, packet_type);
		if (!qtd)
			goto cleanup;
		this_qtd_len = qtd_fill(qtd, buf, len);
		list_add_tail(&qtd->qtd_list, head);
S
Sebastian Siewior 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

		len -= this_qtd_len;
		buf += this_qtd_len;

		if (len <= 0)
			break;
	}

	/*
	 * control requests may need a terminating data "status" ack;
	 * bulk ones may need a terminating short packet (zero length).
	 */
	if (urb->transfer_buffer_length != 0) {
		int one_more = 0;

		if (usb_pipecontrol(urb->pipe)) {
			one_more = 1;
1491 1492 1493 1494
			if (packet_type == IN_PID)
				packet_type = OUT_PID;
			else
				packet_type = IN_PID;
S
Sebastian Siewior 已提交
1495 1496
		} else if (usb_pipebulk(urb->pipe)
				&& (urb->transfer_flags & URB_ZERO_PACKET)
1497 1498
				&& !(urb->transfer_buffer_length %
							maxpacketsize)) {
S
Sebastian Siewior 已提交
1499 1500 1501
			one_more = 1;
		}
		if (one_more) {
1502
			qtd = qtd_alloc(flags, urb, packet_type);
S
Sebastian Siewior 已提交
1503 1504 1505 1506
			if (!qtd)
				goto cleanup;

			/* never any data in such packets */
1507 1508
			qtd_fill(qtd, NULL, 0);
			list_add_tail(&qtd->qtd_list, head);
S
Sebastian Siewior 已提交
1509 1510 1511
		}
	}

1512
	return;
S
Sebastian Siewior 已提交
1513 1514

cleanup:
1515 1516 1517
	qtd_list_free(head);
}

S
Sebastian Siewior 已提交
1518 1519 1520
static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
		gfp_t mem_flags)
{
1521 1522 1523 1524 1525 1526 1527
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	struct list_head *ep_queue;
	struct isp1760_qh *qh, *qhit;
	unsigned long spinflags;
	LIST_HEAD(new_qtds);
	int retval;
	int qh_in_queue;
S
Sebastian Siewior 已提交
1528 1529 1530

	switch (usb_pipetype(urb->pipe)) {
	case PIPE_CONTROL:
1531 1532
		ep_queue = &priv->controlqhs;
		break;
S
Sebastian Siewior 已提交
1533
	case PIPE_BULK:
1534
		ep_queue = &priv->bulkqhs;
S
Sebastian Siewior 已提交
1535 1536
		break;
	case PIPE_INTERRUPT:
1537 1538 1539 1540
		if (urb->interval < 0)
			return -EINVAL;
		/* FIXME: Check bandwidth  */
		ep_queue = &priv->interruptqhs;
S
Sebastian Siewior 已提交
1541 1542
		break;
	case PIPE_ISOCHRONOUS:
1543 1544 1545 1546
		dev_err(hcd->self.controller, "%s: isochronous USB packets "
							"not yet supported\n",
							__func__);
		return -EPIPE;
S
Sebastian Siewior 已提交
1547
	default:
1548 1549
		dev_err(hcd->self.controller, "%s: unknown pipe type\n",
							__func__);
S
Sebastian Siewior 已提交
1550 1551 1552
		return -EPIPE;
	}

1553 1554
	if (usb_pipein(urb->pipe))
		urb->actual_length = 0;
S
Sebastian Siewior 已提交
1555

1556 1557 1558
	packetize_urb(hcd, urb, &new_qtds, mem_flags);
	if (list_empty(&new_qtds))
		return -ENOMEM;
S
Sebastian Siewior 已提交
1559

1560 1561
	retval = 0;
	spin_lock_irqsave(&priv->lock, spinflags);
S
Sebastian Siewior 已提交
1562

1563 1564 1565
	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
		retval = -ESHUTDOWN;
		goto out;
S
Sebastian Siewior 已提交
1566
	}
1567 1568 1569
	retval = usb_hcd_link_urb_to_ep(hcd, urb);
	if (retval)
		goto out;
S
Sebastian Siewior 已提交
1570

1571 1572 1573 1574 1575 1576
	qh = urb->ep->hcpriv;
	if (qh) {
		qh_in_queue = 0;
		list_for_each_entry(qhit, ep_queue, qh_list) {
			if (qhit == qh) {
				qh_in_queue = 1;
1577
				break;
1578 1579 1580 1581 1582 1583 1584 1585
			}
		}
		if (!qh_in_queue)
			list_add_tail(&qh->qh_list, ep_queue);
	} else {
		qh = qh_alloc(GFP_ATOMIC);
		if (!qh) {
			retval = -ENOMEM;
1586
			usb_hcd_unlink_urb_from_ep(hcd, urb);
1587
			goto out;
S
Sebastian Siewior 已提交
1588
		}
1589 1590
		list_add_tail(&qh->qh_list, ep_queue);
		urb->ep->hcpriv = qh;
S
Sebastian Siewior 已提交
1591 1592
	}

1593 1594 1595 1596 1597 1598
	list_splice_tail(&new_qtds, &qh->qtd_list);
	schedule_ptds(hcd);

out:
	spin_unlock_irqrestore(&priv->lock, spinflags);
	return retval;
S
Sebastian Siewior 已提交
1599 1600
}

1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
		struct isp1760_qh *qh)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	int skip_map;

	WARN_ON(qh->slot == -1);

	/* We need to forcefully reclaim the slot since some transfers never
	   return, e.g. interrupt transfers and NAKed bulk transfers. */
1611
	if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
		skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
		skip_map |= (1 << qh->slot);
		reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
		priv->atl_slots[qh->slot].qh = NULL;
		priv->atl_slots[qh->slot].qtd = NULL;
	} else {
		skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
		skip_map |= (1 << qh->slot);
		reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
		priv->int_slots[qh->slot].qh = NULL;
		priv->int_slots[qh->slot].qtd = NULL;
	}

	qh->slot = -1;
}

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
/*
 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
 * any active transfer belonging to the urb in the process.
 */
static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
						struct isp1760_qtd *qtd)
{
	struct urb *urb;
	int urb_was_running;

	urb = qtd->urb;
	urb_was_running = 0;
	list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
		if (qtd->urb != urb)
			break;

		if (qtd->status >= QTD_XFER_STARTED)
			urb_was_running = 1;
		if (last_qtd_of_urb(qtd, qh) &&
					(qtd->status >= QTD_XFER_COMPLETE))
			urb_was_running = 0;

		if (qtd->status == QTD_XFER_STARTED)
			kill_transfer(hcd, urb, qh);
		qtd->status = QTD_RETIRE;
	}

	if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
		qh->tt_buffer_dirty = 1;
		if (usb_hub_clear_tt_buffer(urb))
			/* Clear failed; let's hope things work anyway */
			qh->tt_buffer_dirty = 0;
	}
}

1663 1664
static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
		int status)
S
Sebastian Siewior 已提交
1665
{
1666
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
1667
	unsigned long spinflags;
1668 1669 1670
	struct isp1760_qh *qh;
	struct isp1760_qtd *qtd;
	int retval = 0;
S
Sebastian Siewior 已提交
1671

1672
	spin_lock_irqsave(&priv->lock, spinflags);
1673 1674 1675
	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
	if (retval)
		goto out;
S
Sebastian Siewior 已提交
1676

1677 1678 1679 1680 1681
	qh = urb->ep->hcpriv;
	if (!qh) {
		retval = -EINVAL;
		goto out;
	}
S
Sebastian Siewior 已提交
1682

1683 1684
	list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
		if (qtd->urb == urb) {
1685 1686
			dequeue_urb_from_qtd(hcd, qh, qtd);
			break;
1687
		}
S
Sebastian Siewior 已提交
1688

1689 1690
	urb->status = status;
	schedule_ptds(hcd);
S
Sebastian Siewior 已提交
1691

1692 1693 1694
out:
	spin_unlock_irqrestore(&priv->lock, spinflags);
	return retval;
S
Sebastian Siewior 已提交
1695 1696
}

1697 1698 1699 1700
static void isp1760_endpoint_disable(struct usb_hcd *hcd,
		struct usb_host_endpoint *ep)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
1701
	unsigned long spinflags;
1702 1703 1704 1705
	struct isp1760_qh *qh;
	struct isp1760_qtd *qtd;

	spin_lock_irqsave(&priv->lock, spinflags);
1706

1707 1708 1709 1710
	qh = ep->hcpriv;
	if (!qh)
		goto out;

1711 1712 1713 1714 1715
	list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
		if (qtd->status != QTD_RETIRE) {
			dequeue_urb_from_qtd(hcd, qh, qtd);
			qtd->urb->status = -ECONNRESET;
		}
1716

1717 1718 1719
	ep->hcpriv = NULL;
	/* Cannot free qh here since it will be parsed by schedule_ptds() */

1720 1721
	schedule_ptds(hcd);

1722 1723 1724 1725
out:
	spin_unlock_irqrestore(&priv->lock, spinflags);
}

S
Sebastian Siewior 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	u32 temp, status = 0;
	u32 mask;
	int retval = 1;
	unsigned long flags;

	/* if !USB_SUSPEND, root hub timers won't get shut down ... */
	if (!HC_IS_RUNNING(hcd->state))
		return 0;

	/* init status to no-changes */
	buf[0] = 0;
	mask = PORT_CSC;

	spin_lock_irqsave(&priv->lock, flags);
1743
	temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1744 1745 1746 1747

	if (temp & PORT_OWNER) {
		if (temp & PORT_CSC) {
			temp &= ~PORT_CSC;
1748
			reg_write32(hcd->regs, HC_PORTSC1, temp);
S
Sebastian Siewior 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
			goto done;
		}
	}

	/*
	 * Return status information even for ports with OWNER set.
	 * Otherwise khubd wouldn't see the disconnect event when a
	 * high-speed device is switched over to the companion
	 * controller by the user.
	 */

	if ((temp & mask) != 0
			|| ((temp & PORT_RESUME) != 0
				&& time_after_eq(jiffies,
					priv->reset_done))) {
		buf [0] |= 1 << (0 + 1);
		status = STS_PCD;
	}
	/* FIXME autosuspend idle root hubs */
done:
	spin_unlock_irqrestore(&priv->lock, flags);
	return status ? retval : 0;
}

static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
		struct usb_hub_descriptor *desc)
{
	int ports = HCS_N_PORTS(priv->hcs_params);
	u16 temp;

	desc->bDescriptorType = 0x29;
	/* priv 1.0, 2.3.9 says 20ms max */
	desc->bPwrOn2PwrGood = 10;
	desc->bHubContrCurrent = 0;

	desc->bNbrPorts = ports;
	temp = 1 + (ports / 8);
	desc->bDescLength = 7 + 2 * temp;

1788
	/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
J
John Youn 已提交
1789 1790
	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
S
Sebastian Siewior 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804

	/* per-port overcurrent reporting */
	temp = 0x0008;
	if (HCS_PPC(priv->hcs_params))
		/* per-port power control */
		temp |= 0x0001;
	else
		/* no power switching */
		temp |= 0x0002;
	desc->wHubCharacteristics = cpu_to_le16(temp);
}

#define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)

1805 1806
static int check_reset_complete(struct usb_hcd *hcd, int index,
		int port_status)
S
Sebastian Siewior 已提交
1807 1808 1809 1810 1811 1812 1813
{
	if (!(port_status & PORT_CONNECT))
		return port_status;

	/* if reset finished and it's still not enabled -- handoff */
	if (!(port_status & PORT_PE)) {

1814
		dev_info(hcd->self.controller,
1815 1816
					"port %d full speed --> companion\n",
					index + 1);
S
Sebastian Siewior 已提交
1817 1818 1819

		port_status |= PORT_OWNER;
		port_status &= ~PORT_RWC_BITS;
1820
		reg_write32(hcd->regs, HC_PORTSC1, port_status);
S
Sebastian Siewior 已提交
1821 1822

	} else
1823
		dev_info(hcd->self.controller, "port %d high speed\n",
1824
								index + 1);
S
Sebastian Siewior 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861

	return port_status;
}

static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
		u16 wValue, u16 wIndex, char *buf, u16 wLength)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	int ports = HCS_N_PORTS(priv->hcs_params);
	u32 temp, status;
	unsigned long flags;
	int retval = 0;
	unsigned selector;

	/*
	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
	 * (track current state ourselves) ... blink for diagnostics,
	 * power, "this is the one", etc.  EHCI spec supports this.
	 */

	spin_lock_irqsave(&priv->lock, flags);
	switch (typeReq) {
	case ClearHubFeature:
		switch (wValue) {
		case C_HUB_LOCAL_POWER:
		case C_HUB_OVER_CURRENT:
			/* no hub-wide feature/status flags */
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
1862
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1863 1864 1865 1866 1867

		/*
		 * Even if OWNER is set, so the port is owned by the
		 * companion controller, khubd needs to be able to clear
		 * the port-change status bits (especially
1868
		 * USB_PORT_STAT_C_CONNECTION).
S
Sebastian Siewior 已提交
1869 1870 1871 1872
		 */

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
1873
			reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
S
Sebastian Siewior 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
			break;
		case USB_PORT_FEAT_C_ENABLE:
			/* XXX error? */
			break;
		case USB_PORT_FEAT_SUSPEND:
			if (temp & PORT_RESET)
				goto error;

			if (temp & PORT_SUSPEND) {
				if ((temp & PORT_PE) == 0)
					goto error;
				/* resume signaling for 20 msec */
				temp &= ~(PORT_RWC_BITS);
1887 1888
				reg_write32(hcd->regs, HC_PORTSC1,
							temp | PORT_RESUME);
S
Sebastian Siewior 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897
				priv->reset_done = jiffies +
					msecs_to_jiffies(20);
			}
			break;
		case USB_PORT_FEAT_C_SUSPEND:
			/* we auto-clear this feature */
			break;
		case USB_PORT_FEAT_POWER:
			if (HCS_PPC(priv->hcs_params))
1898 1899
				reg_write32(hcd->regs, HC_PORTSC1,
							temp & ~PORT_POWER);
S
Sebastian Siewior 已提交
1900 1901
			break;
		case USB_PORT_FEAT_C_CONNECTION:
1902
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
S
Sebastian Siewior 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			/* XXX error ?*/
			break;
		case USB_PORT_FEAT_C_RESET:
			/* GetPortStatus clears reset */
			break;
		default:
			goto error;
		}
1913
		reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
		break;
	case GetHubDescriptor:
		isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
			buf);
		break;
	case GetHubStatus:
		/* no hub-wide feature/status flags */
		memset(buf, 0, 4);
		break;
	case GetPortStatus:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		status = 0;
1928
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1929 1930 1931

		/* wPortChange bits */
		if (temp & PORT_CSC)
1932
			status |= USB_PORT_STAT_C_CONNECTION << 16;
S
Sebastian Siewior 已提交
1933 1934 1935 1936


		/* whoever resumes must GetPortStatus to complete it!! */
		if (temp & PORT_RESUME) {
1937
			dev_err(hcd->self.controller, "Port resume should be skipped.\n");
S
Sebastian Siewior 已提交
1938 1939 1940 1941 1942 1943 1944

			/* Remote Wakeup received? */
			if (!priv->reset_done) {
				/* resume signaling for 20 msec */
				priv->reset_done = jiffies
						+ msecs_to_jiffies(20);
				/* check the port again */
1945
				mod_timer(&hcd->rh_timer, priv->reset_done);
S
Sebastian Siewior 已提交
1946 1947 1948 1949 1950
			}

			/* resume completed? */
			else if (time_after_eq(jiffies,
					priv->reset_done)) {
1951
				status |= USB_PORT_STAT_C_SUSPEND << 16;
S
Sebastian Siewior 已提交
1952 1953 1954
				priv->reset_done = 0;

				/* stop resume signaling */
1955 1956 1957 1958
				temp = reg_read32(hcd->regs, HC_PORTSC1);
				reg_write32(hcd->regs, HC_PORTSC1,
					temp & ~(PORT_RWC_BITS | PORT_RESUME));
				retval = handshake(hcd, HC_PORTSC1,
S
Sebastian Siewior 已提交
1959 1960
					   PORT_RESUME, 0, 2000 /* 2msec */);
				if (retval != 0) {
1961
					dev_err(hcd->self.controller,
S
Sebastian Siewior 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
						"port %d resume error %d\n",
						wIndex + 1, retval);
					goto error;
				}
				temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
			}
		}

		/* whoever resets must GetPortStatus to complete it!! */
		if ((temp & PORT_RESET)
				&& time_after_eq(jiffies,
					priv->reset_done)) {
1974
			status |= USB_PORT_STAT_C_RESET << 16;
S
Sebastian Siewior 已提交
1975 1976 1977
			priv->reset_done = 0;

			/* force reset to complete */
1978
			reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
S
Sebastian Siewior 已提交
1979 1980 1981
			/* REVISIT:  some hardware needs 550+ usec to clear
			 * this bit; seems too long to spin routinely...
			 */
1982
			retval = handshake(hcd, HC_PORTSC1,
S
Sebastian Siewior 已提交
1983 1984
					PORT_RESET, 0, 750);
			if (retval != 0) {
1985
				dev_err(hcd->self.controller, "port %d reset error %d\n",
S
Sebastian Siewior 已提交
1986 1987 1988 1989 1990
						wIndex + 1, retval);
				goto error;
			}

			/* see what we found out */
1991 1992
			temp = check_reset_complete(hcd, wIndex,
					reg_read32(hcd->regs, HC_PORTSC1));
S
Sebastian Siewior 已提交
1993 1994 1995 1996 1997 1998 1999 2000
		}
		/*
		 * Even if OWNER is set, there's no harm letting khubd
		 * see the wPortStatus values (they should all be 0 except
		 * for PORT_POWER anyway).
		 */

		if (temp & PORT_OWNER)
2001
			dev_err(hcd->self.controller, "PORT_OWNER is set\n");
S
Sebastian Siewior 已提交
2002 2003

		if (temp & PORT_CONNECT) {
2004
			status |= USB_PORT_STAT_CONNECTION;
S
Sebastian Siewior 已提交
2005
			/* status may be from integrated TT */
2006
			status |= USB_PORT_STAT_HIGH_SPEED;
S
Sebastian Siewior 已提交
2007 2008
		}
		if (temp & PORT_PE)
2009
			status |= USB_PORT_STAT_ENABLE;
S
Sebastian Siewior 已提交
2010
		if (temp & (PORT_SUSPEND|PORT_RESUME))
2011
			status |= USB_PORT_STAT_SUSPEND;
S
Sebastian Siewior 已提交
2012
		if (temp & PORT_RESET)
2013
			status |= USB_PORT_STAT_RESET;
S
Sebastian Siewior 已提交
2014
		if (temp & PORT_POWER)
2015
			status |= USB_PORT_STAT_POWER;
S
Sebastian Siewior 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034

		put_unaligned(cpu_to_le32(status), (__le32 *) buf);
		break;
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_LOCAL_POWER:
		case C_HUB_OVER_CURRENT:
			/* no hub-wide feature/status flags */
			break;
		default:
			goto error;
		}
		break;
	case SetPortFeature:
		selector = wIndex >> 8;
		wIndex &= 0xff;
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
2035
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
2036 2037 2038 2039 2040 2041
		if (temp & PORT_OWNER)
			break;

/*		temp &= ~PORT_RWC_BITS; */
		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
2042
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
S
Sebastian Siewior 已提交
2043 2044 2045 2046 2047 2048 2049
			break;

		case USB_PORT_FEAT_SUSPEND:
			if ((temp & PORT_PE) == 0
					|| (temp & PORT_RESET) != 0)
				goto error;

2050
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
S
Sebastian Siewior 已提交
2051 2052 2053
			break;
		case USB_PORT_FEAT_POWER:
			if (HCS_PPC(priv->hcs_params))
2054 2055
				reg_write32(hcd->regs, HC_PORTSC1,
							temp | PORT_POWER);
S
Sebastian Siewior 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
			break;
		case USB_PORT_FEAT_RESET:
			if (temp & PORT_RESUME)
				goto error;
			/* line status bits may report this as low speed,
			 * which can be fine if this root hub has a
			 * transaction translator built in.
			 */
			if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
					&& PORT_USB11(temp)) {
				temp |= PORT_OWNER;
			} else {
				temp |= PORT_RESET;
				temp &= ~PORT_PE;

				/*
				 * caller must wait, then call GetPortStatus
				 * usb 2.0 spec says 50 ms resets on root
				 */
				priv->reset_done = jiffies +
					msecs_to_jiffies(50);
			}
2078
			reg_write32(hcd->regs, HC_PORTSC1, temp);
S
Sebastian Siewior 已提交
2079 2080 2081 2082
			break;
		default:
			goto error;
		}
2083
		reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
		break;

	default:
error:
		/* "stall" on error */
		retval = -EPIPE;
	}
	spin_unlock_irqrestore(&priv->lock, flags);
	return retval;
}

static int isp1760_get_frame(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	u32 fr;

2100
	fr = reg_read32(hcd->regs, HC_FRINDEX);
S
Sebastian Siewior 已提交
2101 2102 2103 2104 2105 2106
	return (fr >> 3) % priv->periodic_size;
}

static void isp1760_stop(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
2107
	u32 temp;
S
Sebastian Siewior 已提交
2108

2109 2110
	del_timer(&errata2_timer);

S
Sebastian Siewior 已提交
2111 2112 2113 2114 2115
	isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER,	1,
			NULL, 0);
	mdelay(20);

	spin_lock_irq(&priv->lock);
2116
	ehci_reset(hcd);
S
Sebastian Siewior 已提交
2117
	/* Disable IRQ */
2118 2119
	temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
S
Sebastian Siewior 已提交
2120 2121
	spin_unlock_irq(&priv->lock);

2122
	reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
S
Sebastian Siewior 已提交
2123 2124 2125 2126
}

static void isp1760_shutdown(struct usb_hcd *hcd)
{
2127
	u32 command, temp;
S
Sebastian Siewior 已提交
2128 2129

	isp1760_stop(hcd);
2130 2131
	temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
S
Sebastian Siewior 已提交
2132

2133
	command = reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
2134
	command &= ~CMD_RUN;
2135
	reg_write32(hcd->regs, HC_USBCMD, command);
S
Sebastian Siewior 已提交
2136 2137
}

2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
						struct usb_host_endpoint *ep)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	struct isp1760_qh *qh = ep->hcpriv;
	unsigned long spinflags;

	if (!qh)
		return;

	spin_lock_irqsave(&priv->lock, spinflags);
	qh->tt_buffer_dirty = 0;
	schedule_ptds(hcd);
	spin_unlock_irqrestore(&priv->lock, spinflags);
}


S
Sebastian Siewior 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
static const struct hc_driver isp1760_hc_driver = {
	.description		= "isp1760-hcd",
	.product_desc		= "NXP ISP1760 USB Host Controller",
	.hcd_priv_size		= sizeof(struct isp1760_hcd),
	.irq			= isp1760_irq,
	.flags			= HCD_MEMORY | HCD_USB2,
	.reset			= isp1760_hc_setup,
	.start			= isp1760_run,
	.stop			= isp1760_stop,
	.shutdown		= isp1760_shutdown,
	.urb_enqueue		= isp1760_urb_enqueue,
	.urb_dequeue		= isp1760_urb_dequeue,
	.endpoint_disable	= isp1760_endpoint_disable,
	.get_frame_number	= isp1760_get_frame,
	.hub_status_data	= isp1760_hub_status_data,
	.hub_control		= isp1760_hub_control,
2171
	.clear_tt_buffer_complete	= isp1760_clear_tt_buffer_complete,
S
Sebastian Siewior 已提交
2172 2173 2174 2175
};

int __init init_kmem_once(void)
{
2176 2177 2178 2179 2180 2181 2182
	urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
			sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
			SLAB_MEM_SPREAD, NULL);

	if (!urb_listitem_cachep)
		return -ENOMEM;

S
Sebastian Siewior 已提交
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	qtd_cachep = kmem_cache_create("isp1760_qtd",
			sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
			SLAB_MEM_SPREAD, NULL);

	if (!qtd_cachep)
		return -ENOMEM;

	qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
			0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);

	if (!qh_cachep) {
		kmem_cache_destroy(qtd_cachep);
		return -ENOMEM;
	}

	return 0;
}

void deinit_kmem_cache(void)
{
	kmem_cache_destroy(qtd_cachep);
	kmem_cache_destroy(qh_cachep);
2205
	kmem_cache_destroy(urb_listitem_cachep);
S
Sebastian Siewior 已提交
2206 2207
}

2208 2209 2210 2211
struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
				 int irq, unsigned long irqflags,
				 struct device *dev, const char *busname,
				 unsigned int devflags)
S
Sebastian Siewior 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
{
	struct usb_hcd *hcd;
	struct isp1760_hcd *priv;
	int ret;

	if (usb_disabled())
		return ERR_PTR(-ENODEV);

	/* prevent usb-core allocating DMA pages */
	dev->dma_mask = NULL;

2223
	hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
S
Sebastian Siewior 已提交
2224 2225 2226 2227
	if (!hcd)
		return ERR_PTR(-ENOMEM);

	priv = hcd_to_priv(hcd);
2228
	priv->devflags = devflags;
S
Sebastian Siewior 已提交
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	init_memory(priv);
	hcd->regs = ioremap(res_start, res_len);
	if (!hcd->regs) {
		ret = -EIO;
		goto err_put;
	}

	hcd->irq = irq;
	hcd->rsrc_start = res_start;
	hcd->rsrc_len = res_len;

2240 2241 2242 2243
	ret = usb_add_hcd(hcd, irq, irqflags);
	if (ret)
		goto err_unmap;

S
Sebastian Siewior 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
	return hcd;

err_unmap:
	 iounmap(hcd->regs);

err_put:
	 usb_put_hcd(hcd);

	 return ERR_PTR(ret);
}

MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
MODULE_LICENSE("GPL v2");