isp1760-hcd.c 54.0 KB
Newer Older
S
Sebastian Siewior 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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>
 *
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/usb.h>
17
#include <linux/usb/hcd.h>
S
Sebastian Siewior 已提交
18 19 20
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
21
#include <linux/mm.h>
S
Sebastian Siewior 已提交
22
#include <asm/unaligned.h>
23
#include <asm/cacheflush.h>
S
Sebastian Siewior 已提交
24 25 26 27 28 29 30 31 32 33 34 35

#include "isp1760-hcd.h"

static struct kmem_cache *qtd_cachep;
static struct kmem_cache *qh_cachep;

struct isp1760_hcd {
	u32 hcs_params;
	spinlock_t		lock;
	struct inter_packet_info atl_ints[32];
	struct inter_packet_info int_ints[32];
	struct memory_chunk memory_pool[BLOCKS];
36
	u32 atl_queued;
S
Sebastian Siewior 已提交
37 38 39 40 41 42 43

	/* periodic schedule support */
#define	DEFAULT_I_TDPS		1024
	unsigned		periodic_size;
	unsigned		i_thresh;
	unsigned long		reset_done;
	unsigned long		next_statechange;
44
	unsigned int		devflags;
S
Sebastian Siewior 已提交
45 46 47 48 49 50 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
};

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;
82 83
	u32 payload_addr;

S
Sebastian Siewior 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	/* the rest is HCD-private */
	struct list_head qtd_list;
	struct urb *urb;
	size_t length;

	/* isp special*/
	u32 status;
#define URB_ENQUEUED		(1 << 1)
};

struct isp1760_qh {
	/* first part defined by EHCI spec */
	struct list_head qtd_list;

	u32 toggle;
	u32 ping;
};

102 103 104 105
/*
 * Access functions for isp176x registers (addresses 0..0x03FF).
 */
static u32 reg_read32(void __iomem *base, u32 reg)
S
Sebastian Siewior 已提交
106
{
107
	return readl(base + reg);
S
Sebastian Siewior 已提交
108 109
}

110
static void reg_write32(void __iomem *base, u32 reg, u32 val)
S
Sebastian Siewior 已提交
111
{
112
	writel(val, base + reg);
S
Sebastian Siewior 已提交
113 114 115
}

/*
116 117 118 119 120 121 122 123 124 125
 * 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 已提交
126 127
 * doesn't quite work because some people have to enforce 32-bit access
 */
128 129
static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
							__u32 *dst, u32 bytes)
S
Sebastian Siewior 已提交
130
{
131
	__u32 __iomem *src;
S
Sebastian Siewior 已提交
132
	u32 val;
133 134
	__u8 *src_byteptr;
	__u8 *dst_byteptr;
S
Sebastian Siewior 已提交
135

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151
	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 已提交
152 153
	}

154
	if (!bytes)
S
Sebastian Siewior 已提交
155 156 157 158 159
		return;

	/* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
	 * allocated.
	 */
160 161 162 163 164 165 166 167 168 169 170 171
	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 已提交
172 173 174
	}
}

175 176
static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
								u32 bytes)
S
Sebastian Siewior 已提交
177
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	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 已提交
204 205
	}

206
	if (!bytes)
S
Sebastian Siewior 已提交
207
		return;
208 209
	/* 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 已提交
210 211
	 */

212 213 214 215
	if (dst_offset < PAYLOAD_OFFSET)
		__raw_writel(cpu_to_le32(*src), dst);
	else
		__raw_writel(*src, dst);
S
Sebastian Siewior 已提交
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/*
 * 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 已提交
245 246 247
/* memory management of the 60kb on the chip from 0x1000 to 0xffff */
static void init_memory(struct isp1760_hcd *priv)
{
248 249
	int i, curr;
	u32 payload_addr;
S
Sebastian Siewior 已提交
250

251
	payload_addr = PAYLOAD_OFFSET;
S
Sebastian Siewior 已提交
252
	for (i = 0; i < BLOCK_1_NUM; i++) {
253
		priv->memory_pool[i].start = payload_addr;
S
Sebastian Siewior 已提交
254 255
		priv->memory_pool[i].size = BLOCK_1_SIZE;
		priv->memory_pool[i].free = 1;
256
		payload_addr += priv->memory_pool[i].size;
S
Sebastian Siewior 已提交
257 258
	}

259 260 261 262 263 264
	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 已提交
265 266
	}

267 268 269 270 271 272
	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 已提交
273 274
	}

275
	BUG_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
S
Sebastian Siewior 已提交
276 277
}

278
static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
279
{
280
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
281 282
	int i;

283 284 285 286
	BUG_ON(qtd->payload_addr);

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

	for (i = 0; i < BLOCKS; i++) {
289
		if (priv->memory_pool[i].size >= qtd->length &&
S
Sebastian Siewior 已提交
290 291
				priv->memory_pool[i].free) {
			priv->memory_pool[i].free = 0;
292 293
			qtd->payload_addr = priv->memory_pool[i].start;
			return;
S
Sebastian Siewior 已提交
294 295 296
		}
	}

297 298 299 300
	dev_err(hcd->self.controller,
				"%s: Can not allocate %lu bytes of memory\n"
				"Current memory map:\n",
				__func__, qtd->length);
S
Sebastian Siewior 已提交
301
	for (i = 0; i < BLOCKS; i++) {
302
		dev_err(hcd->self.controller, "Pool %2d size %4d status: %d\n",
S
Sebastian Siewior 已提交
303 304 305 306 307
				i, priv->memory_pool[i].size,
				priv->memory_pool[i].free);
	}
	/* XXX maybe -ENOMEM could be possible */
	BUG();
308
	return;
S
Sebastian Siewior 已提交
309 310
}

311
static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
312
{
313
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
314 315
	int i;

316
	if (!qtd->payload_addr)
S
Sebastian Siewior 已提交
317 318 319
		return;

	for (i = 0; i < BLOCKS; i++) {
320
		if (priv->memory_pool[i].start == qtd->payload_addr) {
S
Sebastian Siewior 已提交
321 322
			BUG_ON(priv->memory_pool[i].free);
			priv->memory_pool[i].free = 1;
323 324
			qtd->payload_addr = 0;
			return;
S
Sebastian Siewior 已提交
325 326 327
		}
	}

328 329
	dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
						__func__, qtd->payload_addr);
S
Sebastian Siewior 已提交
330 331 332 333 334
	BUG();
}

static void isp1760_init_regs(struct usb_hcd *hcd)
{
335 336 337 338 339 340 341 342
	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);

	reg_write32(hcd->regs, HC_ATL_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
	reg_write32(hcd->regs, HC_INT_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
	reg_write32(hcd->regs, HC_ISO_PTD_DONEMAP_REG, ~NO_TRANSFER_ACTIVE);
S
Sebastian Siewior 已提交
343 344
}

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

	do {
351
		result = reg_read32(hcd->regs, reg);
S
Sebastian Siewior 已提交
352 353 354 355 356 357 358 359 360 361 362 363
		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 */
364
static int ehci_reset(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
365 366
{
	int retval;
367 368
	struct isp1760_hcd *priv = hcd_to_priv(hcd);

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

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

static void qh_destroy(struct isp1760_qh *qh)
{
	BUG_ON(!list_empty(&qh->qtd_list));
	kmem_cache_free(qh_cachep, qh);
}

386
static struct isp1760_qh *isp1760_qh_alloc(gfp_t flags)
S
Sebastian Siewior 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
{
	struct isp1760_qh *qh;

	qh = kmem_cache_zalloc(qh_cachep, flags);
	if (!qh)
		return qh;

	INIT_LIST_HEAD(&qh->qtd_list);
	return qh;
}

/* 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 */

/* 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);

	/*
	 * 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 ... */
421
	hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
S
Sebastian Siewior 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434
	/* 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;
435 436 437 438 439 440 441 442 443 444 445 446 447
	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;
448 449 450 451
	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;
452 453 454 455 456 457

	/*
	 * 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.
	 */
458 459
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
	reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
S
Sebastian Siewior 已提交
460

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

	/* pre reset */
	isp1760_init_regs(hcd);

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

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

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

	/* Step 11 passed */

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

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

496 497
	reg_write32(hcd->regs, HC_INTERRUPT_REG, INTERRUPT_ENABLE_MASK);
	reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
498 499 500

	/*
	 * PORT 1 Control register of the ISP1760 is the OTG control
501 502 503
	 * 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.
504
	 */
505
	reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
506
	mdelay(10);
S
Sebastian Siewior 已提交
507

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

	return priv_init(hcd);
}

static void isp1760_init_maps(struct usb_hcd *hcd)
{
	/*set last maps, for iso its only 1, else 32 tds bitmap*/
516 517 518
	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);
S
Sebastian Siewior 已提交
519 520 521 522
}

static void isp1760_enable_interrupts(struct usb_hcd *hcd)
{
523 524 525 526 527 528
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0);
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0);
	reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
	reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
S
Sebastian Siewior 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542
	/* step 23 passed */
}

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;
	isp1760_enable_interrupts(hcd);
543 544
	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 已提交
545

546
	command = reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
547 548
	command &= ~(CMD_LRESET|CMD_RESET);
	command |= CMD_RUN;
549
	reg_write32(hcd->regs, HC_USBCMD, command);
S
Sebastian Siewior 已提交
550

551
	retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN,
S
Sebastian Siewior 已提交
552 553 554 555 556 557 558 559 560 561
			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);
562
	reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
S
Sebastian Siewior 已提交
563

564
	retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
S
Sebastian Siewior 已提交
565 566 567 568
	up_write(&ehci_cf_port_reset_rwsem);
	if (retval)
		return retval;

569
	chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
570 571
	dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
					chipid & 0xffff, chipid >> 16);
S
Sebastian Siewior 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

	/* PTD Register Init Part 2, Step 28 */
	/* enable INTs */
	isp1760_init_maps(hcd);

	/* 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;
}

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

589 590 591 592 593 594 595 596 597 598 599 600
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);
}

601
static void transform_into_atl(struct isp1760_qh *qh,
602
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
603 604 605 606 607 608 609
{
	u32 maxpacket;
	u32 multi;
	u32 pid_code;
	u32 rl = RL_COUNTER;
	u32 nak = NAK_COUNTER;

610 611
	memset(ptd, 0, sizeof(*ptd));

S
Sebastian Siewior 已提交
612
	/* according to 3.6.2, max packet len can not be > 0x400 */
613 614
	maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
						usb_pipeout(qtd->urb->pipe));
S
Sebastian Siewior 已提交
615 616 617 618
	multi =  1 + ((maxpacket >> 11) & 0x3);
	maxpacket &= 0x7ff;

	/* DW0 */
619 620 621
	ptd->dw0 = PTD_VALID;
	ptd->dw0 |= PTD_LENGTH(qtd->length);
	ptd->dw0 |= PTD_MAXPACKET(maxpacket);
622
	ptd->dw0 |= PTD_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
S
Sebastian Siewior 已提交
623 624

	/* DW1 */
625 626
	ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
	ptd->dw1 |= PTD_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
S
Sebastian Siewior 已提交
627 628

	pid_code = qtd->packet_type;
629
	ptd->dw1 |= PTD_PID_TOKEN(pid_code);
S
Sebastian Siewior 已提交
630

631
	if (usb_pipebulk(qtd->urb->pipe))
632
		ptd->dw1 |= PTD_TRANS_BULK;
633
	else if  (usb_pipeint(qtd->urb->pipe))
634
		ptd->dw1 |= PTD_TRANS_INT;
S
Sebastian Siewior 已提交
635

636
	if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
S
Sebastian Siewior 已提交
637 638
		/* split transaction */

639
		ptd->dw1 |= PTD_TRANS_SPLIT;
640
		if (qtd->urb->dev->speed == USB_SPEED_LOW)
641
			ptd->dw1 |= PTD_SE_USB_LOSPEED;
S
Sebastian Siewior 已提交
642

643 644
		ptd->dw1 |= PTD_PORT_NUM(qtd->urb->dev->ttport);
		ptd->dw1 |= PTD_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
S
Sebastian Siewior 已提交
645 646

		/* SE bit for Split INT transfers */
647 648
		if (usb_pipeint(qtd->urb->pipe) &&
				(qtd->urb->dev->speed == USB_SPEED_LOW))
649
			ptd->dw1 |= 2 << 16;
S
Sebastian Siewior 已提交
650

651
		ptd->dw3 = 0;
S
Sebastian Siewior 已提交
652 653 654
		rl = 0;
		nak = 0;
	} else {
655
		ptd->dw0 |= PTD_MULTI(multi);
656 657
		if (usb_pipecontrol(qtd->urb->pipe) ||
						usb_pipebulk(qtd->urb->pipe))
658
			ptd->dw3 = qh->ping;
S
Sebastian Siewior 已提交
659
		else
660
			ptd->dw3 = 0;
S
Sebastian Siewior 已提交
661 662
	}
	/* DW2 */
663
	ptd->dw2 = 0;
664
	ptd->dw2 |= PTD_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
665 666
	ptd->dw2 |= PTD_RL_CNT(rl);
	ptd->dw3 |= PTD_NAC_CNT(nak);
S
Sebastian Siewior 已提交
667 668

	/* DW3 */
669 670 671 672 673 674 675
	ptd->dw3 |= qh->toggle;
	if (usb_pipecontrol(qtd->urb->pipe)) {
		if (qtd->data_buffer == qtd->urb->setup_packet)
			ptd->dw3 &= ~PTD_DATA_TOGGLE(1);
		else if (last_qtd_of_urb(qtd, qh))
			ptd->dw3 |= PTD_DATA_TOGGLE(1);
	}
S
Sebastian Siewior 已提交
676

677
	ptd->dw3 |= PTD_ACTIVE;
S
Sebastian Siewior 已提交
678
	/* Cerr */
679
	ptd->dw3 |= PTD_CERR(ERR_COUNTER);
S
Sebastian Siewior 已提交
680 681
}

682
static void transform_add_int(struct isp1760_qh *qh,
683
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
684
{
685
	u32 usof;
S
Sebastian Siewior 已提交
686 687
	u32 period;

688 689 690 691 692 693 694 695
	/*
	 * 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 已提交
696

697 698 699 700 701 702 703 704 705 706 707
	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 已提交
708
		else
709
			usof = 0xff; /* All bits set => interval 1/8 ms */
S
Sebastian Siewior 已提交
710
	} else {
711 712 713 714 715 716 717 718 719 720 721 722 723 724
		/* 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 已提交
725 726
	}

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

730 731
	ptd->dw2 |= period;
	ptd->dw4 = usof;
S
Sebastian Siewior 已提交
732 733
}

734
static void transform_into_int(struct isp1760_qh *qh,
735
			struct isp1760_qtd *qtd, struct ptd *ptd)
S
Sebastian Siewior 已提交
736
{
737 738
	transform_into_atl(qh, qtd, ptd);
	transform_add_int(qh, qtd, ptd);
S
Sebastian Siewior 已提交
739 740 741 742 743 744 745 746 747 748
}

static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len,
		u32 token)
{
	int count;

	qtd->data_buffer = databuffer;
	qtd->packet_type = GET_QTD_TOKEN_TYPE(token);

749 750
	if (len > MAX_PAYLOAD_SIZE)
		count = MAX_PAYLOAD_SIZE;
S
Sebastian Siewior 已提交
751 752 753 754 755 756 757
	else
		count = len;

	qtd->length = count;
	return count;
}

758
static int check_error(struct usb_hcd *hcd, struct ptd *ptd)
S
Sebastian Siewior 已提交
759 760 761
{
	int error = 0;

762
	if (ptd->dw3 & DW3_HALT_BIT) {
S
Sebastian Siewior 已提交
763 764
		error = -EPIPE;

765
		if (ptd->dw3 & DW3_ERROR_BIT)
766
			pr_err("error bit is set in DW3\n");
S
Sebastian Siewior 已提交
767 768
	}

769
	if (ptd->dw3 & DW3_QTD_ACTIVE) {
770 771 772
		dev_err(hcd->self.controller, "Transfer active bit is set DW3\n"
			"nak counter: %d, rl: %d\n",
			(ptd->dw3 >> 19) & 0xf, (ptd->dw2 >> 25) & 0xf);
S
Sebastian Siewior 已提交
773 774 775 776 777
	}

	return error;
}

778
static void check_int_err_status(struct usb_hcd *hcd, u32 dw4)
S
Sebastian Siewior 已提交
779 780 781 782 783 784 785 786
{
	u32 i;

	dw4 >>= 8;

	for (i = 0; i < 8; i++) {
		switch (dw4 & 0x7) {
		case INT_UNDERRUN:
787
			dev_err(hcd->self.controller, "Underrun (%d)\n", i);
S
Sebastian Siewior 已提交
788 789 790
			break;

		case INT_EXACT:
791 792
			dev_err(hcd->self.controller,
						"Transaction error (%d)\n", i);
S
Sebastian Siewior 已提交
793 794 795
			break;

		case INT_BABBLE:
796
			dev_err(hcd->self.controller, "Babble error (%d)\n", i);
S
Sebastian Siewior 已提交
797 798 799 800 801 802
			break;
		}
		dw4 >>= 3;
	}
}

803
static void enqueue_one_qtd(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
804
{
805 806
	if (qtd->length && (qtd->length <= MAX_PAYLOAD_SIZE)) {
		switch (qtd->packet_type) {
S
Sebastian Siewior 已提交
807 808 809 810
		case IN_PID:
			break;
		case OUT_PID:
		case SETUP_PID:
811 812
			mem_writes8(hcd->regs, qtd->payload_addr,
						qtd->data_buffer, qtd->length);
S
Sebastian Siewior 已提交
813 814 815 816
		}
	}
}

817 818
static void enqueue_one_atl_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
					u32 slot, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
819
{
820
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
821 822
	struct ptd ptd;

823 824
	alloc_mem(hcd, qtd);
	transform_into_atl(qh, qtd, &ptd);
825
	ptd_write(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
826
	enqueue_one_qtd(hcd, qtd);
S
Sebastian Siewior 已提交
827 828 829

	priv->atl_ints[slot].qh = qh;
	priv->atl_ints[slot].qtd = qtd;
830
	qtd->status |= URB_ENQUEUED;
S
Sebastian Siewior 已提交
831 832 833
	qtd->status |= slot << 16;
}

834 835
static void enqueue_one_int_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
					u32 slot, struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
836
{
837
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
838 839
	struct ptd ptd;

840 841
	alloc_mem(hcd, qtd);
	transform_into_int(qh, qtd, &ptd);
842
	ptd_write(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
843
	enqueue_one_qtd(hcd, qtd);
S
Sebastian Siewior 已提交
844 845 846

	priv->int_ints[slot].qh = qh;
	priv->int_ints[slot].qtd = qtd;
847
	qtd->status |= URB_ENQUEUED;
S
Sebastian Siewior 已提交
848 849 850
	qtd->status |= slot << 16;
}

851 852
static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
				  struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
853 854 855 856 857 858
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	u32 skip_map, or_map;
	u32 slot;
	u32 buffstatus;

859 860 861 862 863 864
	/*
	 * When this function is called from the interrupt handler to enqueue
	 * a follow-up packet, the SKIP register gets written and read back
	 * almost immediately. With ISP1761, this register requires a delay of
	 * 195ns between a write and subsequent read (see section 15.1.1.3).
	 */
865
	mmiowb();
866
	ndelay(195);
867
	skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
868 869 870 871

	BUG_ON(!skip_map);
	slot = __ffs(skip_map);

872
	enqueue_one_atl_qtd(hcd, qh, slot, qtd);
S
Sebastian Siewior 已提交
873

874
	or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
875
	or_map |= (1 << slot);
876
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
S
Sebastian Siewior 已提交
877

878
	skip_map &= ~(1 << slot);
879
	reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
S
Sebastian Siewior 已提交
880

881 882
	priv->atl_queued++;
	if (priv->atl_queued == 2)
883 884
		reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
				INTERRUPT_ENABLE_SOT_MASK);
885

886
	buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
S
Sebastian Siewior 已提交
887
	buffstatus |= ATL_BUFFER;
888
	reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
S
Sebastian Siewior 已提交
889 890
}

891 892
static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
				  struct isp1760_qtd *qtd)
S
Sebastian Siewior 已提交
893 894 895 896 897
{
	u32 skip_map, or_map;
	u32 slot;
	u32 buffstatus;

898 899 900 901 902 903
	/*
	 * When this function is called from the interrupt handler to enqueue
	 * a follow-up packet, the SKIP register gets written and read back
	 * almost immediately. With ISP1761, this register requires a delay of
	 * 195ns between a write and subsequent read (see section 15.1.1.3).
	 */
904
	mmiowb();
905
	ndelay(195);
906
	skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
907 908 909 910

	BUG_ON(!skip_map);
	slot = __ffs(skip_map);

911
	enqueue_one_int_qtd(hcd, qh, slot, qtd);
S
Sebastian Siewior 已提交
912

913
	or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
914
	or_map |= (1 << slot);
915
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
S
Sebastian Siewior 已提交
916

917
	skip_map &= ~(1 << slot);
918
	reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
S
Sebastian Siewior 已提交
919

920
	buffstatus = reg_read32(hcd->regs, HC_BUFFER_STATUS_REG);
S
Sebastian Siewior 已提交
921
	buffstatus |= INT_BUFFER;
922
	reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, buffstatus);
S
Sebastian Siewior 已提交
923 924
}

925
static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
S
Sebastian Siewior 已提交
926 927 928
__releases(priv->lock)
__acquires(priv->lock)
{
929 930
	struct isp1760_hcd *priv = hcd_to_priv(hcd);

S
Sebastian Siewior 已提交
931
	if (!urb->unlinked) {
932 933
		if (urb->status == -EINPROGRESS)
			urb->status = 0;
S
Sebastian Siewior 已提交
934 935
	}

936 937 938 939 940 941 942 943
	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 已提交
944
	/* complete() can reenter this HCD */
945
	usb_hcd_unlink_urb_from_ep(hcd, urb);
S
Sebastian Siewior 已提交
946
	spin_unlock(&priv->lock);
947
	usb_hcd_giveback_urb(hcd, urb, urb->status);
S
Sebastian Siewior 已提交
948 949 950 951 952
	spin_lock(&priv->lock);
}

static void isp1760_qtd_free(struct isp1760_qtd *qtd)
{
953
	BUG_ON(qtd->payload_addr);
S
Sebastian Siewior 已提交
954 955 956
	kmem_cache_free(qtd_cachep, qtd);
}

957 958
static struct isp1760_qtd *clean_this_qtd(struct isp1760_qtd *qtd,
							struct isp1760_qh *qh)
S
Sebastian Siewior 已提交
959 960 961
{
	struct isp1760_qtd *tmp_qtd;

962 963 964 965 966
	if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
		tmp_qtd = NULL;
	else
		tmp_qtd = list_entry(qtd->qtd_list.next, struct isp1760_qtd,
								qtd_list);
S
Sebastian Siewior 已提交
967 968 969 970 971 972 973 974 975 976
	list_del(&qtd->qtd_list);
	isp1760_qtd_free(qtd);
	return tmp_qtd;
}

/*
 * Remove this QTD from the QH list and free its memory. If this QTD
 * isn't the last one than remove also his successor(s).
 * Returns the QTD which is part of an new URB and should be enqueued.
 */
977 978
static struct isp1760_qtd *clean_up_qtdlist(struct isp1760_qtd *qtd,
							struct isp1760_qh *qh)
S
Sebastian Siewior 已提交
979
{
980
	struct urb *urb;
S
Sebastian Siewior 已提交
981

982
	urb = qtd->urb;
S
Sebastian Siewior 已提交
983
	do {
984 985
		qtd = clean_this_qtd(qtd, qh);
	} while (qtd && (qtd->urb == urb));
S
Sebastian Siewior 已提交
986 987 988 989

	return qtd;
}

990
static void do_atl_int(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
991
{
992
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
993 994
	u32 done_map, skip_map;
	struct ptd ptd;
995 996
	struct urb *urb;
	u32 slot;
S
Sebastian Siewior 已提交
997 998 999 1000 1001 1002 1003 1004 1005
	u32 length;
	u32 or_map;
	u32 status = -EINVAL;
	int error;
	struct isp1760_qtd *qtd;
	struct isp1760_qh *qh;
	u32 rl;
	u32 nakcount;

1006 1007
	done_map = reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
	skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
1008

1009
	or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
S
Sebastian Siewior 已提交
1010
	or_map &= ~done_map;
1011
	reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
S
Sebastian Siewior 已提交
1012 1013 1014

	while (done_map) {
		status = 0;
1015
		priv->atl_queued--;
S
Sebastian Siewior 已提交
1016

1017 1018 1019
		slot = __ffs(done_map);
		done_map &= ~(1 << slot);
		skip_map |= (1 << slot);
S
Sebastian Siewior 已提交
1020

1021 1022
		qtd = priv->atl_ints[slot].qtd;
		qh = priv->atl_ints[slot].qh;
S
Sebastian Siewior 已提交
1023 1024

		if (!qh) {
1025
			dev_err(hcd->self.controller, "qh is 0\n");
S
Sebastian Siewior 已提交
1026 1027
			continue;
		}
1028
		ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1029

1030 1031
		rl = (ptd.dw2 >> 25) & 0x0f;
		nakcount = (ptd.dw3 >> 19) & 0xf;
S
Sebastian Siewior 已提交
1032 1033

		/* Transfer Error, *but* active and no HALT -> reload */
1034 1035
		if ((ptd.dw3 & DW3_ERROR_BIT) && (ptd.dw3 & DW3_QTD_ACTIVE) &&
				!(ptd.dw3 & DW3_HALT_BIT)) {
S
Sebastian Siewior 已提交
1036 1037 1038 1039 1040 1041 1042 1043

			/* according to ppriv code, we have to
			 * reload this one if trasfered bytes != requested bytes
			 * else act like everything went smooth..
			 * XXX This just doesn't feel right and hasn't
			 * triggered so far.
			 */

1044
			length = PTD_XFERRED_LENGTH(ptd.dw3);
1045 1046
			dev_err(hcd->self.controller,
					"Should reload now... transferred %d "
S
Sebastian Siewior 已提交
1047 1048 1049 1050
					"of %zu\n", length, qtd->length);
			BUG();
		}

1051
		if (!nakcount && (ptd.dw3 & DW3_QTD_ACTIVE)) {
S
Sebastian Siewior 已提交
1052 1053
			u32 buffstatus;

1054
			/*
S
Sebastian Siewior 已提交
1055 1056
			 * NAKs are handled in HW by the chip. Usually if the
			 * device is not able to send data fast enough.
1057
			 * This happens mostly on slower hardware.
S
Sebastian Siewior 已提交
1058 1059 1060
			 */

			/* RL counter = ERR counter */
1061 1062 1063 1064
			ptd.dw3 &= ~(0xf << 19);
			ptd.dw3 |= rl << 19;
			ptd.dw3 &= ~(3 << (55 - 32));
			ptd.dw3 |= ERR_COUNTER << (55 - 32);
S
Sebastian Siewior 已提交
1065 1066 1067 1068 1069 1070

			/*
			 * It is not needed to write skip map back because it
			 * is unchanged. Just make sure that this entry is
			 * unskipped once it gets written to the HW.
			 */
1071
			skip_map &= ~(1 << slot);
1072
			or_map = reg_read32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG);
1073
			or_map |= 1 << slot;
1074
			reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, or_map);
S
Sebastian Siewior 已提交
1075

1076
			ptd.dw0 |= PTD_VALID;
1077
			ptd_write(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
S
Sebastian Siewior 已提交
1078

1079 1080
			priv->atl_queued++;
			if (priv->atl_queued == 2)
1081 1082
				reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
						INTERRUPT_ENABLE_SOT_MASK);
1083

1084 1085
			buffstatus = reg_read32(hcd->regs,
							HC_BUFFER_STATUS_REG);
S
Sebastian Siewior 已提交
1086
			buffstatus |= ATL_BUFFER;
1087 1088
			reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
								buffstatus);
S
Sebastian Siewior 已提交
1089 1090 1091
			continue;
		}

1092
		error = check_error(hcd, &ptd);
S
Sebastian Siewior 已提交
1093 1094
		if (error) {
			status = error;
1095 1096 1097
			priv->atl_ints[slot].qh->toggle = 0;
			priv->atl_ints[slot].qh->ping = 0;
			qtd->urb->status = -EPIPE;
S
Sebastian Siewior 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

#if 0
			printk(KERN_ERR "Error in %s().\n", __func__);
			printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
					"dw3: %08x dw4: %08x dw5: %08x dw6: "
					"%08x dw7: %08x\n",
					ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
					ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
#endif
		} else {
1108 1109
			priv->atl_ints[slot].qh->toggle = ptd.dw3 & (1 << 25);
			priv->atl_ints[slot].qh->ping = ptd.dw3 & (1 << 26);
S
Sebastian Siewior 已提交
1110 1111
		}

1112
		length = PTD_XFERRED_LENGTH(ptd.dw3);
S
Sebastian Siewior 已提交
1113
		if (length) {
1114
			switch (DW1_GET_PID(ptd.dw1)) {
S
Sebastian Siewior 已提交
1115
			case IN_PID:
1116
				mem_reads8(hcd->regs, qtd->payload_addr,
1117
						qtd->data_buffer, length);
S
Sebastian Siewior 已提交
1118 1119 1120

			case OUT_PID:

1121
				qtd->urb->actual_length += length;
S
Sebastian Siewior 已提交
1122 1123 1124 1125 1126 1127

			case SETUP_PID:
				break;
			}
		}

1128 1129
		priv->atl_ints[slot].qtd = NULL;
		priv->atl_ints[slot].qh = NULL;
S
Sebastian Siewior 已提交
1130

1131
		free_mem(hcd, qtd);
S
Sebastian Siewior 已提交
1132

1133
		reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
S
Sebastian Siewior 已提交
1134

1135
		if (qtd->urb->status == -EPIPE) {
S
Sebastian Siewior 已提交
1136 1137
			/* HALT was received */

1138
			urb = qtd->urb;
1139
			qtd = clean_up_qtdlist(qtd, qh);
1140
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
1141

1142 1143
		} else if (usb_pipebulk(qtd->urb->pipe) &&
						(length < qtd->length)) {
S
Sebastian Siewior 已提交
1144 1145
			/* short BULK received */

1146 1147 1148 1149 1150 1151
			if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK) {
				qtd->urb->status = -EREMOTEIO;
				dev_dbg(hcd->self.controller,
						"short bulk, %d instead %zu "
						"with URB_SHORT_NOT_OK flag.\n",
						length, qtd->length);
S
Sebastian Siewior 已提交
1152 1153
			}

1154 1155
			if (qtd->urb->status == -EINPROGRESS)
				qtd->urb->status = 0;
S
Sebastian Siewior 已提交
1156

1157
			urb = qtd->urb;
1158
			qtd = clean_up_qtdlist(qtd, qh);
1159
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
1160

1161
		} else if (last_qtd_of_urb(qtd, qh)) {
S
Sebastian Siewior 已提交
1162 1163
			/* that was the last qtd of that URB */

1164 1165
			if (qtd->urb->status == -EINPROGRESS)
				qtd->urb->status = 0;
S
Sebastian Siewior 已提交
1166

1167 1168 1169
			urb = qtd->urb;
			qtd = clean_up_qtdlist(qtd, qh);
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
1170 1171 1172 1173

		} else {
			/* next QTD of this URB */

1174
			qtd = clean_this_qtd(qtd, qh);
S
Sebastian Siewior 已提交
1175 1176 1177 1178
			BUG_ON(!qtd);
		}

		if (qtd)
1179
			enqueue_an_ATL_packet(hcd, qh, qtd);
S
Sebastian Siewior 已提交
1180

1181
		skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
1182
	}
1183
	if (priv->atl_queued <= 1)
1184 1185
		reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
							INTERRUPT_ENABLE_MASK);
S
Sebastian Siewior 已提交
1186 1187
}

1188
static void do_intl_int(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
1189
{
1190
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
1191 1192
	u32 done_map, skip_map;
	struct ptd ptd;
1193
	struct urb *urb;
S
Sebastian Siewior 已提交
1194 1195 1196
	u32 length;
	u32 or_map;
	int error;
1197
	u32 slot;
S
Sebastian Siewior 已提交
1198 1199 1200
	struct isp1760_qtd *qtd;
	struct isp1760_qh *qh;

1201 1202
	done_map = reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
	skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
1203

1204
	or_map = reg_read32(hcd->regs, HC_INT_IRQ_MASK_OR_REG);
S
Sebastian Siewior 已提交
1205
	or_map &= ~done_map;
1206
	reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, or_map);
S
Sebastian Siewior 已提交
1207 1208

	while (done_map) {
1209 1210 1211
		slot = __ffs(done_map);
		done_map &= ~(1 << slot);
		skip_map |= (1 << slot);
S
Sebastian Siewior 已提交
1212

1213 1214
		qtd = priv->int_ints[slot].qtd;
		qh = priv->int_ints[slot].qh;
S
Sebastian Siewior 已提交
1215 1216

		if (!qh) {
1217
			dev_err(hcd->self.controller, "(INT) qh is 0\n");
S
Sebastian Siewior 已提交
1218 1219 1220
			continue;
		}

1221 1222
		ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
		check_int_err_status(hcd, ptd.dw4);
S
Sebastian Siewior 已提交
1223

1224
		error = check_error(hcd, &ptd);
S
Sebastian Siewior 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233
		if (error) {
#if 0
			printk(KERN_ERR "Error in %s().\n", __func__);
			printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
					"dw3: %08x dw4: %08x dw5: %08x dw6: "
					"%08x dw7: %08x\n",
					ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
					ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
#endif
1234 1235 1236
			qtd->urb->status = -EPIPE;
			priv->int_ints[slot].qh->toggle = 0;
			priv->int_ints[slot].qh->ping = 0;
S
Sebastian Siewior 已提交
1237 1238

		} else {
1239 1240
			priv->int_ints[slot].qh->toggle = ptd.dw3 & (1 << 25);
			priv->int_ints[slot].qh->ping = ptd.dw3 & (1 << 26);
S
Sebastian Siewior 已提交
1241 1242
		}

1243
		if (qtd->urb->dev->speed != USB_SPEED_HIGH)
1244
			length = PTD_XFERRED_LENGTH_LO(ptd.dw3);
S
Sebastian Siewior 已提交
1245
		else
1246
			length = PTD_XFERRED_LENGTH(ptd.dw3);
S
Sebastian Siewior 已提交
1247 1248

		if (length) {
1249
			switch (DW1_GET_PID(ptd.dw1)) {
S
Sebastian Siewior 已提交
1250
			case IN_PID:
1251
				mem_reads8(hcd->regs, qtd->payload_addr,
1252
						qtd->data_buffer, length);
S
Sebastian Siewior 已提交
1253 1254
			case OUT_PID:

1255
				qtd->urb->actual_length += length;
S
Sebastian Siewior 已提交
1256 1257 1258 1259 1260 1261

			case SETUP_PID:
				break;
			}
		}

1262 1263
		priv->int_ints[slot].qtd = NULL;
		priv->int_ints[slot].qh = NULL;
S
Sebastian Siewior 已提交
1264

1265
		reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1266
		free_mem(hcd, qtd);
S
Sebastian Siewior 已提交
1267

1268
		if (qtd->urb->status == -EPIPE) {
S
Sebastian Siewior 已提交
1269 1270
			/* HALT received */

1271 1272 1273
			urb = qtd->urb;
			qtd = clean_up_qtdlist(qtd, qh);
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
1274

1275
		} else if (last_qtd_of_urb(qtd, qh)) {
S
Sebastian Siewior 已提交
1276

1277 1278
			if (qtd->urb->status == -EINPROGRESS)
				qtd->urb->status = 0;
S
Sebastian Siewior 已提交
1279

1280 1281 1282
			urb = qtd->urb;
			qtd = clean_up_qtdlist(qtd, qh);
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
1283 1284 1285 1286

		} else {
			/* next QTD of this URB */

1287
			qtd = clean_this_qtd(qtd, qh);
S
Sebastian Siewior 已提交
1288 1289 1290 1291
			BUG_ON(!qtd);
		}

		if (qtd)
1292
			enqueue_an_INT_packet(hcd, qh, qtd);
S
Sebastian Siewior 已提交
1293

1294
		skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
S
Sebastian Siewior 已提交
1295 1296 1297
	}
}

1298
static struct isp1760_qh *qh_make(struct usb_hcd *hcd, struct urb *urb,
S
Sebastian Siewior 已提交
1299 1300 1301 1302 1303
		gfp_t flags)
{
	struct isp1760_qh *qh;
	int is_input, type;

1304
	qh = isp1760_qh_alloc(flags);
S
Sebastian Siewior 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	if (!qh)
		return qh;

	/*
	 * init endpoint/device data for this QH
	 */
	is_input = usb_pipein(urb->pipe);
	type = usb_pipetype(urb->pipe);

	if (!usb_pipecontrol(urb->pipe))
		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input,
				1);
	return qh;
}

/*
 * For control/bulk/interrupt, return QH with these TDs appended.
 * Allocates and initializes the QH if necessary.
 * Returns null if it can't allocate a QH it needs to.
 * If the QH has TDs (urbs) already, that's great.
 */
1326
static struct isp1760_qh *qh_append_tds(struct usb_hcd *hcd,
S
Sebastian Siewior 已提交
1327 1328 1329 1330 1331 1332 1333 1334
		struct urb *urb, struct list_head *qtd_list, int epnum,
		void **ptr)
{
	struct isp1760_qh *qh;

	qh = (struct isp1760_qh *)*ptr;
	if (!qh) {
		/* can't sleep here, we have priv->lock... */
1335
		qh = qh_make(hcd, urb, GFP_ATOMIC);
S
Sebastian Siewior 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
		if (!qh)
			return qh;
		*ptr = qh;
	}

	list_splice(qtd_list, qh->qtd_list.prev);

	return qh;
}

1346
static void qtd_list_free(struct urb *urb, struct list_head *qtd_list)
S
Sebastian Siewior 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
{
	struct list_head *entry, *temp;

	list_for_each_safe(entry, temp, qtd_list) {
		struct isp1760_qtd	*qtd;

		qtd = list_entry(entry, struct isp1760_qtd, qtd_list);
		list_del(&qtd->qtd_list);
		isp1760_qtd_free(qtd);
	}
}

1359
static int isp1760_prepare_enqueue(struct usb_hcd *hcd, struct urb *urb,
S
Sebastian Siewior 已提交
1360 1361
		struct list_head *qtd_list, gfp_t mem_flags, packet_enqueue *p)
{
1362
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
	struct isp1760_qtd         *qtd;
	int                     epnum;
	unsigned long           flags;
	struct isp1760_qh          *qh = NULL;
	int                     rc;
	int qh_busy;

	qtd = list_entry(qtd_list->next, struct isp1760_qtd, qtd_list);
	epnum = urb->ep->desc.bEndpointAddress;

	spin_lock_irqsave(&priv->lock, flags);
1374
	if (!HCD_HW_ACCESSIBLE(hcd)) {
S
Sebastian Siewior 已提交
1375 1376 1377
		rc = -ESHUTDOWN;
		goto done;
	}
1378
	rc = usb_hcd_link_urb_to_ep(hcd, urb);
S
Sebastian Siewior 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387
	if (rc)
		goto done;

	qh = urb->ep->hcpriv;
	if (qh)
		qh_busy = !list_empty(&qh->qtd_list);
	else
		qh_busy = 0;

1388
	qh = qh_append_tds(hcd, urb, qtd_list, epnum, &urb->ep->hcpriv);
S
Sebastian Siewior 已提交
1389
	if (!qh) {
1390
		usb_hcd_unlink_urb_from_ep(hcd, urb);
S
Sebastian Siewior 已提交
1391 1392 1393 1394 1395
		rc = -ENOMEM;
		goto done;
	}

	if (!qh_busy)
1396
		p(hcd, qh, qtd);
S
Sebastian Siewior 已提交
1397 1398 1399 1400

done:
	spin_unlock_irqrestore(&priv->lock, flags);
	if (!qh)
1401
		qtd_list_free(urb, qtd_list);
S
Sebastian Siewior 已提交
1402 1403 1404
	return rc;
}

1405
static struct isp1760_qtd *isp1760_qtd_alloc(gfp_t flags)
S
Sebastian Siewior 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
{
	struct isp1760_qtd *qtd;

	qtd = kmem_cache_zalloc(qtd_cachep, flags);
	if (qtd)
		INIT_LIST_HEAD(&qtd->qtd_list);

	return qtd;
}

/*
 * create a list of filled qtds for this URB; won't link into qh.
 */
1419 1420
#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
static struct list_head *qh_urb_transaction(struct usb_hcd *hcd,
S
Sebastian Siewior 已提交
1421 1422
		struct urb *urb, struct list_head *head, gfp_t flags)
{
1423
	struct isp1760_qtd *qtd;
S
Sebastian Siewior 已提交
1424 1425 1426 1427 1428 1429 1430 1431
	void *buf;
	int len, maxpacket;
	int is_input;
	u32 token;

	/*
	 * URBs map to sequences of QTDs:  one logical transaction
	 */
1432
	qtd = isp1760_qtd_alloc(flags);
S
Sebastian Siewior 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	if (!qtd)
		return NULL;

	list_add_tail(&qtd->qtd_list, head);
	qtd->urb = urb;
	urb->status = -EINPROGRESS;

	token = 0;
	/* for split transactions, SplitXState initialized to zero */

	len = urb->transfer_buffer_length;
	is_input = usb_pipein(urb->pipe);
	if (usb_pipecontrol(urb->pipe)) {
		/* SETUP pid */
		qtd_fill(qtd, urb->setup_packet,
				sizeof(struct usb_ctrlrequest),
				token | SETUP_PID);

		/* ... and always at least one more pid */
1452
		qtd = isp1760_qtd_alloc(flags);
S
Sebastian Siewior 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
		if (!qtd)
			goto cleanup;
		qtd->urb = urb;
		list_add_tail(&qtd->qtd_list, head);

		/* for zero length DATA stages, STATUS is always IN */
		if (len == 0)
			token |= IN_PID;
	}

	/*
	 * data transfer stage:  buffer setup
	 */
	buf = urb->transfer_buffer;

	if (is_input)
		token |= IN_PID;
	else
		token |= OUT_PID;

	maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));

	/*
	 * buffer gets wrapped in one or more qtds;
	 * last one may be "short" (including zero len)
	 * and may serve as a control status ack
	 */
	for (;;) {
		int this_qtd_len;

		if (!buf && len) {
			/* XXX This looks like usb storage / SCSI bug */
1485
			dev_err(hcd->self.controller, "buf is null, dma is %08lx len is %d\n",
S
Sebastian Siewior 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
					(long unsigned)urb->transfer_dma, len);
			WARN_ON(1);
		}

		this_qtd_len = qtd_fill(qtd, buf, len, token);
		len -= this_qtd_len;
		buf += this_qtd_len;

		if (len <= 0)
			break;

1497
		qtd = isp1760_qtd_alloc(flags);
S
Sebastian Siewior 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
		if (!qtd)
			goto cleanup;
		qtd->urb = urb;
		list_add_tail(&qtd->qtd_list, head);
	}

	/*
	 * 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;
			/* "in" <--> "out"  */
			token ^= IN_PID;
		} else if (usb_pipebulk(urb->pipe)
				&& (urb->transfer_flags & URB_ZERO_PACKET)
				&& !(urb->transfer_buffer_length % maxpacket)) {
			one_more = 1;
		}
		if (one_more) {
1521
			qtd = isp1760_qtd_alloc(flags);
S
Sebastian Siewior 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
			if (!qtd)
				goto cleanup;
			qtd->urb = urb;
			list_add_tail(&qtd->qtd_list, head);

			/* never any data in such packets */
			qtd_fill(qtd, NULL, 0, token);
		}
	}

1532
	qtd->status = 0;
S
Sebastian Siewior 已提交
1533 1534 1535
	return head;

cleanup:
1536
	qtd_list_free(urb, head);
S
Sebastian Siewior 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
	return NULL;
}

static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
		gfp_t mem_flags)
{
	struct list_head qtd_list;
	packet_enqueue *pe;

	INIT_LIST_HEAD(&qtd_list);

	switch (usb_pipetype(urb->pipe)) {
	case PIPE_CONTROL:
	case PIPE_BULK:
1551
		if (!qh_urb_transaction(hcd, urb, &qtd_list, mem_flags))
S
Sebastian Siewior 已提交
1552 1553 1554 1555 1556
			return -ENOMEM;
		pe =  enqueue_an_ATL_packet;
		break;

	case PIPE_INTERRUPT:
1557
		if (!qh_urb_transaction(hcd, urb, &qtd_list, mem_flags))
S
Sebastian Siewior 已提交
1558 1559 1560 1561 1562
			return -ENOMEM;
		pe = enqueue_an_INT_packet;
		break;

	case PIPE_ISOCHRONOUS:
1563
		dev_err(hcd->self.controller, "PIPE_ISOCHRONOUS ain't supported\n");
S
Sebastian Siewior 已提交
1564 1565 1566 1567
	default:
		return -EPIPE;
	}

1568
	return isp1760_prepare_enqueue(hcd, urb, &qtd_list, mem_flags, pe);
S
Sebastian Siewior 已提交
1569 1570
}

1571
static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
S
Sebastian Siewior 已提交
1572 1573 1574 1575 1576
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
	struct inter_packet_info *ints;
	u32 i;
	u32 reg_base, or_reg, skip_reg;
1577
	unsigned long flags;
S
Sebastian Siewior 已提交
1578
	struct ptd ptd;
1579
	packet_enqueue *pe;
S
Sebastian Siewior 已提交
1580 1581 1582 1583 1584 1585 1586 1587

	switch (usb_pipetype(urb->pipe)) {
	case PIPE_ISOCHRONOUS:
		return -EPIPE;
		break;

	case PIPE_INTERRUPT:
		ints = priv->int_ints;
1588
		reg_base = INT_PTD_OFFSET;
S
Sebastian Siewior 已提交
1589 1590
		or_reg = HC_INT_IRQ_MASK_OR_REG;
		skip_reg = HC_INT_PTD_SKIPMAP_REG;
1591
		pe = enqueue_an_INT_packet;
S
Sebastian Siewior 已提交
1592 1593 1594 1595
		break;

	default:
		ints = priv->atl_ints;
1596
		reg_base = ATL_PTD_OFFSET;
S
Sebastian Siewior 已提交
1597 1598
		or_reg = HC_ATL_IRQ_MASK_OR_REG;
		skip_reg = HC_ATL_PTD_SKIPMAP_REG;
1599
		pe =  enqueue_an_ATL_packet;
S
Sebastian Siewior 已提交
1600 1601 1602 1603 1604 1605 1606
		break;
	}

	memset(&ptd, 0, sizeof(ptd));
	spin_lock_irqsave(&priv->lock, flags);

	for (i = 0; i < 32; i++) {
1607 1608 1609 1610 1611
		if (!ints[i].qh)
			continue;
		BUG_ON(!ints[i].qtd);

		if (ints[i].qtd->urb == urb) {
S
Sebastian Siewior 已提交
1612 1613 1614
			u32 skip_map;
			u32 or_map;
			struct isp1760_qtd *qtd;
1615
			struct isp1760_qh *qh;
S
Sebastian Siewior 已提交
1616

1617
			skip_map = reg_read32(hcd->regs, skip_reg);
S
Sebastian Siewior 已提交
1618
			skip_map |= 1 << i;
1619
			reg_write32(hcd->regs, skip_reg, skip_map);
S
Sebastian Siewior 已提交
1620

1621
			or_map = reg_read32(hcd->regs, or_reg);
S
Sebastian Siewior 已提交
1622
			or_map &= ~(1 << i);
1623 1624 1625
			reg_write32(hcd->regs, or_reg, or_map);

			ptd_write(hcd->regs, reg_base, i, &ptd);
S
Sebastian Siewior 已提交
1626 1627

			qtd = ints->qtd;
1628
			qh = ints[i].qh;
S
Sebastian Siewior 已提交
1629

1630
			free_mem(hcd, qtd);
1631
			qtd = clean_up_qtdlist(qtd, qh);
S
Sebastian Siewior 已提交
1632 1633 1634 1635

			ints->qh = NULL;
			ints->qtd = NULL;

1636
			isp1760_urb_done(hcd, urb);
1637 1638
			if (qtd)
				pe(hcd, qh, qtd);
S
Sebastian Siewior 已提交
1639
			break;
1640

1641 1642
		} else {
			struct isp1760_qtd *qtd;
1643

1644 1645
			list_for_each_entry(qtd, &ints[i].qtd->qtd_list,
								qtd_list) {
1646
				if (qtd->urb == urb) {
1647
					clean_up_qtdlist(qtd, ints[i].qh);
1648
					isp1760_urb_done(hcd, urb);
1649
					qtd = NULL;
1650 1651 1652
					break;
				}
			}
1653 1654 1655

			/* We found the urb before the last slot */
			if (!qtd)
1656
				break;
S
Sebastian Siewior 已提交
1657 1658 1659 1660 1661 1662 1663 1664
		}
		ints++;
	}

	spin_unlock_irqrestore(&priv->lock, flags);
	return 0;
}

1665
static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
S
Sebastian Siewior 已提交
1666
{
1667
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
1668 1669 1670 1671 1672
	u32 imask;
	irqreturn_t irqret = IRQ_NONE;

	spin_lock(&priv->lock);

1673
	if (!(hcd->state & HC_STATE_RUNNING))
S
Sebastian Siewior 已提交
1674 1675
		goto leave;

1676
	imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
S
Sebastian Siewior 已提交
1677 1678 1679
	if (unlikely(!imask))
		goto leave;

1680
	reg_write32(hcd->regs, HC_INTERRUPT_REG, imask);
1681
	if (imask & (HC_ATL_INT | HC_SOT_INT))
1682
		do_atl_int(hcd);
S
Sebastian Siewior 已提交
1683 1684

	if (imask & HC_INTL_INT)
1685
		do_intl_int(hcd);
S
Sebastian Siewior 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709

	irqret = IRQ_HANDLED;
leave:
	spin_unlock(&priv->lock);
	return irqret;
}

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);
1710
	temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1711 1712 1713 1714

	if (temp & PORT_OWNER) {
		if (temp & PORT_CSC) {
			temp &= ~PORT_CSC;
1715
			reg_write32(hcd->regs, HC_PORTSC1, temp);
S
Sebastian Siewior 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
			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;

	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
	memset(&desc->bitmap[0], 0, temp);
	memset(&desc->bitmap[temp], 0xff, temp);

	/* 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)

1772 1773
static int check_reset_complete(struct usb_hcd *hcd, int index,
		int port_status)
S
Sebastian Siewior 已提交
1774 1775 1776 1777 1778 1779 1780
{
	if (!(port_status & PORT_CONNECT))
		return port_status;

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

1781 1782 1783
		dev_err(hcd->self.controller,
					"port %d full speed --> companion\n",
					index + 1);
S
Sebastian Siewior 已提交
1784 1785 1786

		port_status |= PORT_OWNER;
		port_status &= ~PORT_RWC_BITS;
1787
		reg_write32(hcd->regs, HC_PORTSC1, port_status);
S
Sebastian Siewior 已提交
1788 1789

	} else
1790 1791
		dev_err(hcd->self.controller, "port %d high speed\n",
								index + 1);
S
Sebastian Siewior 已提交
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828

	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--;
1829
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1830 1831 1832 1833 1834

		/*
		 * 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
1835
		 * USB_PORT_STAT_C_CONNECTION).
S
Sebastian Siewior 已提交
1836 1837 1838 1839
		 */

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
1840
			reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
S
Sebastian Siewior 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
			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);
1854 1855
				reg_write32(hcd->regs, HC_PORTSC1,
							temp | PORT_RESUME);
S
Sebastian Siewior 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864
				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))
1865 1866
				reg_write32(hcd->regs, HC_PORTSC1,
							temp & ~PORT_POWER);
S
Sebastian Siewior 已提交
1867 1868
			break;
		case USB_PORT_FEAT_C_CONNECTION:
1869
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
S
Sebastian Siewior 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			/* XXX error ?*/
			break;
		case USB_PORT_FEAT_C_RESET:
			/* GetPortStatus clears reset */
			break;
		default:
			goto error;
		}
1880
		reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
		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;
1895
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
1896 1897 1898

		/* wPortChange bits */
		if (temp & PORT_CSC)
1899
			status |= USB_PORT_STAT_C_CONNECTION << 16;
S
Sebastian Siewior 已提交
1900 1901 1902 1903


		/* whoever resumes must GetPortStatus to complete it!! */
		if (temp & PORT_RESUME) {
1904
			dev_err(hcd->self.controller, "Port resume should be skipped.\n");
S
Sebastian Siewior 已提交
1905 1906 1907 1908 1909 1910 1911

			/* Remote Wakeup received? */
			if (!priv->reset_done) {
				/* resume signaling for 20 msec */
				priv->reset_done = jiffies
						+ msecs_to_jiffies(20);
				/* check the port again */
1912
				mod_timer(&hcd->rh_timer, priv->reset_done);
S
Sebastian Siewior 已提交
1913 1914 1915 1916 1917
			}

			/* resume completed? */
			else if (time_after_eq(jiffies,
					priv->reset_done)) {
1918
				status |= USB_PORT_STAT_C_SUSPEND << 16;
S
Sebastian Siewior 已提交
1919 1920 1921
				priv->reset_done = 0;

				/* stop resume signaling */
1922 1923 1924 1925
				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 已提交
1926 1927
					   PORT_RESUME, 0, 2000 /* 2msec */);
				if (retval != 0) {
1928
					dev_err(hcd->self.controller,
S
Sebastian Siewior 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
						"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)) {
1941
			status |= USB_PORT_STAT_C_RESET << 16;
S
Sebastian Siewior 已提交
1942 1943 1944
			priv->reset_done = 0;

			/* force reset to complete */
1945
			reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
S
Sebastian Siewior 已提交
1946 1947 1948
			/* REVISIT:  some hardware needs 550+ usec to clear
			 * this bit; seems too long to spin routinely...
			 */
1949
			retval = handshake(hcd, HC_PORTSC1,
S
Sebastian Siewior 已提交
1950 1951
					PORT_RESET, 0, 750);
			if (retval != 0) {
1952
				dev_err(hcd->self.controller, "port %d reset error %d\n",
S
Sebastian Siewior 已提交
1953 1954 1955 1956 1957
						wIndex + 1, retval);
				goto error;
			}

			/* see what we found out */
1958 1959
			temp = check_reset_complete(hcd, wIndex,
					reg_read32(hcd->regs, HC_PORTSC1));
S
Sebastian Siewior 已提交
1960 1961 1962 1963 1964 1965 1966 1967
		}
		/*
		 * 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)
1968
			dev_err(hcd->self.controller, "PORT_OWNER is set\n");
S
Sebastian Siewior 已提交
1969 1970

		if (temp & PORT_CONNECT) {
1971
			status |= USB_PORT_STAT_CONNECTION;
S
Sebastian Siewior 已提交
1972
			/* status may be from integrated TT */
1973
			status |= USB_PORT_STAT_HIGH_SPEED;
S
Sebastian Siewior 已提交
1974 1975
		}
		if (temp & PORT_PE)
1976
			status |= USB_PORT_STAT_ENABLE;
S
Sebastian Siewior 已提交
1977
		if (temp & (PORT_SUSPEND|PORT_RESUME))
1978
			status |= USB_PORT_STAT_SUSPEND;
S
Sebastian Siewior 已提交
1979
		if (temp & PORT_RESET)
1980
			status |= USB_PORT_STAT_RESET;
S
Sebastian Siewior 已提交
1981
		if (temp & PORT_POWER)
1982
			status |= USB_PORT_STAT_POWER;
S
Sebastian Siewior 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001

		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--;
2002
		temp = reg_read32(hcd->regs, HC_PORTSC1);
S
Sebastian Siewior 已提交
2003 2004 2005 2006 2007 2008
		if (temp & PORT_OWNER)
			break;

/*		temp &= ~PORT_RWC_BITS; */
		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
2009
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
S
Sebastian Siewior 已提交
2010 2011 2012 2013 2014 2015 2016
			break;

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

2017
			reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
S
Sebastian Siewior 已提交
2018 2019 2020
			break;
		case USB_PORT_FEAT_POWER:
			if (HCS_PPC(priv->hcs_params))
2021 2022
				reg_write32(hcd->regs, HC_PORTSC1,
							temp | PORT_POWER);
S
Sebastian Siewior 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
			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);
			}
2045
			reg_write32(hcd->regs, HC_PORTSC1, temp);
S
Sebastian Siewior 已提交
2046 2047 2048 2049
			break;
		default:
			goto error;
		}
2050
		reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
		break;

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

2062
static void isp1760_endpoint_disable(struct usb_hcd *hcd,
S
Sebastian Siewior 已提交
2063 2064
		struct usb_host_endpoint *ep)
{
2065
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
S
Sebastian Siewior 已提交
2066 2067
	struct isp1760_qh *qh;
	struct isp1760_qtd *qtd;
2068
	unsigned long flags;
S
Sebastian Siewior 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085

	spin_lock_irqsave(&priv->lock, flags);
	qh = ep->hcpriv;
	if (!qh)
		goto out;

	ep->hcpriv = NULL;
	do {
		/* more than entry might get removed */
		if (list_empty(&qh->qtd_list))
			break;

		qtd = list_first_entry(&qh->qtd_list, struct isp1760_qtd,
				qtd_list);

		if (qtd->status & URB_ENQUEUED) {
			spin_unlock_irqrestore(&priv->lock, flags);
2086
			isp1760_urb_dequeue(hcd, qtd->urb, -ECONNRESET);
S
Sebastian Siewior 已提交
2087 2088 2089 2090 2091
			spin_lock_irqsave(&priv->lock, flags);
		} else {
			struct urb *urb;

			urb = qtd->urb;
2092
			clean_up_qtdlist(qtd, qh);
2093 2094
			urb->status = -ECONNRESET;
			isp1760_urb_done(hcd, urb);
S
Sebastian Siewior 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
		}
	} while (1);

	qh_destroy(qh);
	/* remove requests and leak them.
	 * ATL are pretty fast done, INT could take a while...
	 * The latter shoule be removed
	 */
out:
	spin_unlock_irqrestore(&priv->lock, flags);
}

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

2112
	fr = reg_read32(hcd->regs, HC_FRINDEX);
S
Sebastian Siewior 已提交
2113 2114 2115 2116 2117 2118
	return (fr >> 3) % priv->periodic_size;
}

static void isp1760_stop(struct usb_hcd *hcd)
{
	struct isp1760_hcd *priv = hcd_to_priv(hcd);
2119
	u32 temp;
S
Sebastian Siewior 已提交
2120 2121 2122 2123 2124 2125

	isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER,	1,
			NULL, 0);
	mdelay(20);

	spin_lock_irq(&priv->lock);
2126
	ehci_reset(hcd);
S
Sebastian Siewior 已提交
2127
	/* Disable IRQ */
2128 2129
	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 已提交
2130 2131
	spin_unlock_irq(&priv->lock);

2132
	reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
S
Sebastian Siewior 已提交
2133 2134 2135 2136
}

static void isp1760_shutdown(struct usb_hcd *hcd)
{
2137
	u32 command, temp;
S
Sebastian Siewior 已提交
2138 2139

	isp1760_stop(hcd);
2140 2141
	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 已提交
2142

2143
	command = reg_read32(hcd->regs, HC_USBCMD);
S
Sebastian Siewior 已提交
2144
	command &= ~CMD_RUN;
2145
	reg_write32(hcd->regs, HC_USBCMD, command);
S
Sebastian Siewior 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
}

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,
};

int __init init_kmem_once(void)
{
	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);
}

2192 2193 2194 2195
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 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
{
	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;

2207
	hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
S
Sebastian Siewior 已提交
2208 2209 2210 2211
	if (!hcd)
		return ERR_PTR(-ENOMEM);

	priv = hcd_to_priv(hcd);
2212
	priv->devflags = devflags;
S
Sebastian Siewior 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
	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;

2224 2225 2226 2227
	ret = usb_add_hcd(hcd, irq, irqflags);
	if (ret)
		goto err_unmap;

S
Sebastian Siewior 已提交
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
	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");