icside.c 18.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 * linux/drivers/ide/icside.c
 *
Martin Dalecki's avatar
Martin Dalecki committed
4
 * Copyright (c) 1996-2002 Russell King.
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Changelog:
 *  08-Jun-1996	RMK	Created
 *  12-Sep-1997	RMK	Added interrupt enable/disable
 *  17-Apr-1999	RMK	Added support for V6 EASI
 *  22-May-1999	RMK	Added support for V6 DMA
 */

#include <linux/config.h>
#include <linux/string.h>
#include <linux/module.h>
#include <linux/ioport.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22 23 24 25 26 27
#include <linux/blkdev.h>
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/ide.h>
#include <linux/pci.h>
#include <linux/init.h>

#include <asm/dma.h>
#include <asm/ecard.h>
#include <asm/io.h>
Martin Dalecki's avatar
Martin Dalecki committed
28
#include <asm/page.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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

/*
 * Maximum number of interfaces per card
 */
#define MAX_IFS	2

#define ICS_IDENT_OFFSET		0x8a0

#define ICS_ARCIN_V5_INTRSTAT		0x000
#define ICS_ARCIN_V5_INTROFFSET		0x001
#define ICS_ARCIN_V5_IDEOFFSET		0xa00
#define ICS_ARCIN_V5_IDEALTOFFSET	0xae0
#define ICS_ARCIN_V5_IDESTEPPING	4

#define ICS_ARCIN_V6_IDEOFFSET_1	0x800
#define ICS_ARCIN_V6_INTROFFSET_1	0x880
#define ICS_ARCIN_V6_INTRSTAT_1		0x8a4
#define ICS_ARCIN_V6_IDEALTOFFSET_1	0x8e0
#define ICS_ARCIN_V6_IDEOFFSET_2	0xc00
#define ICS_ARCIN_V6_INTROFFSET_2	0xc80
#define ICS_ARCIN_V6_INTRSTAT_2		0xca4
#define ICS_ARCIN_V6_IDEALTOFFSET_2	0xce0
#define ICS_ARCIN_V6_IDESTEPPING	4

struct cardinfo {
	unsigned int dataoffset;
	unsigned int ctrloffset;
	unsigned int stepping;
};

static struct cardinfo icside_cardinfo_v5 = {
	ICS_ARCIN_V5_IDEOFFSET,
	ICS_ARCIN_V5_IDEALTOFFSET,
	ICS_ARCIN_V5_IDESTEPPING
};

static struct cardinfo icside_cardinfo_v6_1 = {
	ICS_ARCIN_V6_IDEOFFSET_1,
	ICS_ARCIN_V6_IDEALTOFFSET_1,
	ICS_ARCIN_V6_IDESTEPPING
};

static struct cardinfo icside_cardinfo_v6_2 = {
	ICS_ARCIN_V6_IDEOFFSET_2,
	ICS_ARCIN_V6_IDEALTOFFSET_2,
	ICS_ARCIN_V6_IDESTEPPING
};

Martin Dalecki's avatar
Martin Dalecki committed
77 78 79 80 81 82
struct icside_state {
	unsigned int channel;
	unsigned int enabled;
	unsigned int irq_port;
};

Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static const card_ids icside_cids[] = {
	{ MANU_ICS,  PROD_ICS_IDE  },
	{ MANU_ICS2, PROD_ICS2_IDE },
	{ 0xffff, 0xffff }
};

typedef enum {
	ics_if_unknown,
	ics_if_arcin_v5,
	ics_if_arcin_v6
} iftype_t;

/* ---------------- Version 5 PCB Support Functions --------------------- */
/* Prototype: icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
 * Purpose  : enable interrupts from card
 */
static void icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
{
	unsigned int memc_port = (unsigned int)ec->irq_data;
	outb (0, memc_port + ICS_ARCIN_V5_INTROFFSET);
}

/* Prototype: icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
 * Purpose  : disable interrupts from card
 */
static void icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
{
	unsigned int memc_port = (unsigned int)ec->irq_data;
	inb (memc_port + ICS_ARCIN_V5_INTROFFSET);
}

static const expansioncard_ops_t icside_ops_arcin_v5 = {
	icside_irqenable_arcin_v5,
	icside_irqdisable_arcin_v5,
	NULL,
	NULL,
	NULL,
	NULL
};


/* ---------------- Version 6 PCB Support Functions --------------------- */
/* Prototype: icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
 * Purpose  : enable interrupts from card
 */
static void icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
{
Martin Dalecki's avatar
Martin Dalecki committed
130 131 132 133
	struct icside_state *state = ec->irq_data;
	unsigned int base = state->irq_port;

	state->enabled = 1;
Linus Torvalds's avatar
Linus Torvalds committed
134

Martin Dalecki's avatar
Martin Dalecki committed
135 136 137 138 139 140 141 142 143 144
	switch (state->channel) {
	case 0:
		outb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
		inb(base + ICS_ARCIN_V6_INTROFFSET_2);
		break;
	case 1:
		outb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
		inb(base + ICS_ARCIN_V6_INTROFFSET_1);
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149 150 151
}

/* Prototype: icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
 * Purpose  : disable interrupts from card
 */
static void icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
{
Martin Dalecki's avatar
Martin Dalecki committed
152 153 154
	struct icside_state *state = ec->irq_data;

	state->enabled = 0;
Linus Torvalds's avatar
Linus Torvalds committed
155

Martin Dalecki's avatar
Martin Dalecki committed
156 157
	inb (state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
	inb (state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161 162 163 164
}

/* Prototype: icside_irqprobe(struct expansion_card *ec)
 * Purpose  : detect an active interrupt from card
 */
static int icside_irqpending_arcin_v6(struct expansion_card *ec)
{
Martin Dalecki's avatar
Martin Dalecki committed
165
	struct icside_state *state = ec->irq_data;
Linus Torvalds's avatar
Linus Torvalds committed
166

Martin Dalecki's avatar
Martin Dalecki committed
167 168
	return inb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
	       inb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
Linus Torvalds's avatar
Linus Torvalds committed
169 170 171 172 173 174 175 176 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

static const expansioncard_ops_t icside_ops_arcin_v6 = {
	icside_irqenable_arcin_v6,
	icside_irqdisable_arcin_v6,
	icside_irqpending_arcin_v6,
	NULL,
	NULL,
	NULL
};

/* Prototype: icside_identifyif (struct expansion_card *ec)
 * Purpose  : identify IDE interface type
 * Notes    : checks the description string
 */
static iftype_t __init icside_identifyif (struct expansion_card *ec)
{
	unsigned int addr;
	iftype_t iftype;
	int id = 0;

	iftype = ics_if_unknown;

	addr = ecard_address (ec, ECARD_IOC, ECARD_FAST) + ICS_IDENT_OFFSET;

	id = inb (addr) & 1;
	id |= (inb (addr + 1) & 1) << 1;
	id |= (inb (addr + 2) & 1) << 2;
	id |= (inb (addr + 3) & 1) << 3;

	switch (id) {
	case 0: /* A3IN */
		printk("icside: A3IN unsupported\n");
		break;

	case 1: /* A3USER */
		printk("icside: A3USER unsupported\n");
		break;

	case 3:	/* ARCIN V6 */
		printk(KERN_DEBUG "icside: detected ARCIN V6 in slot %d\n", ec->slot_no);
		iftype = ics_if_arcin_v6;
		break;

	case 15:/* ARCIN V5 (no id) */
		printk(KERN_DEBUG "icside: detected ARCIN V5 in slot %d\n", ec->slot_no);
		iftype = ics_if_arcin_v5;
		break;

	default:/* we don't know - complain very loudly */
		printk("icside: ***********************************\n");
		printk("icside: *** UNKNOWN ICS INTERFACE id=%d ***\n", id);
		printk("icside: ***********************************\n");
		printk("icside: please report this to linux@arm.linux.org.uk\n");
		printk("icside: defaulting to ARCIN V5\n");
		iftype = ics_if_arcin_v5;
		break;
	}

	return iftype;
}

Martin Dalecki's avatar
Martin Dalecki committed
231 232 233 234
/*
 * Handle routing of interrupts.  This is called before
 * we write the command to the drive.
 */
Martin Dalecki's avatar
Martin Dalecki committed
235
static void icside_maskproc(struct ata_device *drive)
Martin Dalecki's avatar
Martin Dalecki committed
236
{
Martin Dalecki's avatar
Martin Dalecki committed
237
	const int mask = 0;
Martin Dalecki's avatar
Martin Dalecki committed
238 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
	struct ata_channel *ch = drive->channel;
	struct icside_state *state = ch->hw.priv;
	unsigned long flags;

	local_irq_save(flags);

	state->channel = ch->unit;

	if (state->enabled && !mask) {
		switch (ch->unit) {
		case 0:
			outb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
			inb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
			break;
		case 1:
			outb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
			inb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
			break;
		}
	} else {
		inb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
		inb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
	}

	local_irq_restore(flags);
}

Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270 271 272 273 274 275 276 277
#ifdef CONFIG_BLK_DEV_IDEDMA_ICS
/*
 * SG-DMA support.
 *
 * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
 * There is only one DMA controller per card, which means that only
 * one drive can be accessed at one time.  NOTE! We do not enforce that
 * here, but we rely on the main IDE driver spotting that both
 * interfaces use the same IRQ, which should guarantee this.
 */
#define NR_ENTRIES 256
#define TABLE_SIZE (NR_ENTRIES * 8)

278
static int ide_build_sglist(struct ata_device *drive, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
279
{
280
	struct ata_channel *ch = drive->channel;
Martin Dalecki's avatar
Martin Dalecki committed
281 282 283
	struct scatterlist *sg = ch->sg_table;
	int nents;

Martin Dalecki's avatar
Martin Dalecki committed
284
	if ((rq->flags & REQ_SPECIAL) && (drive->type == ATA_DISK)) {
Martin Dalecki's avatar
Martin Dalecki committed
285 286 287 288 289 290 291 292 293 294 295 296 297
		struct ata_taskfile *args = rq->special;

		if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
			ch->sg_dma_direction = PCI_DMA_TODEVICE;
		else
			ch->sg_dma_direction = PCI_DMA_FROMDEVICE;

		memset(sg, 0, sizeof(*sg));
		sg->page   = virt_to_page(rq->buffer);
		sg->offset = ((unsigned long)rq->buffer) & ~PAGE_MASK;
		sg->length = rq->nr_sectors * SECTOR_SIZE;
		nents = 1;
	} else {
298
		nents = blk_rq_map_sg(&drive->queue, rq, sg);
Martin Dalecki's avatar
Martin Dalecki committed
299 300 301 302 303 304 305 306 307 308

		if (rq->q && nents > rq->nr_phys_segments)
			printk("icside: received %d segments, build %d\n",
				rq->nr_phys_segments, nents);

		if (rq_data_dir(rq) == READ)
			ch->sg_dma_direction = PCI_DMA_FROMDEVICE;
		else
			ch->sg_dma_direction = PCI_DMA_TODEVICE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
309

Martin Dalecki's avatar
Martin Dalecki committed
310
	return pci_map_sg(NULL, sg, nents, ch->sg_dma_direction);
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313
}

/* Teardown mappings after DMA has completed.  */
Martin Dalecki's avatar
Martin Dalecki committed
314
static void icside_destroy_dmatable(struct ata_device *drive)
Linus Torvalds's avatar
Linus Torvalds committed
315
{
Martin Dalecki's avatar
Martin Dalecki committed
316 317
	struct scatterlist *sg = drive->channel->sg_table;
	int nents = drive->channel->sg_nents;
Linus Torvalds's avatar
Linus Torvalds committed
318

Martin Dalecki's avatar
Martin Dalecki committed
319
	pci_unmap_sg(NULL, sg, nents, drive->channel->sg_dma_direction);
Linus Torvalds's avatar
Linus Torvalds committed
320 321
}

Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/*
 * Configure the IOMD to give the appropriate timings for the transfer
 * mode being requested.  We take the advice of the ATA standards, and
 * calculate the cycle time based on the transfer mode, and the EIDE
 * MW DMA specs that the drive provides in the IDENTIFY command.
 *
 * We have the following IOMD DMA modes to choose from:
 *
 *	Type	Active		Recovery	Cycle
 *	A	250 (250)	312 (550)	562 (800)
 *	B	187		250		437
 *	C	125 (125)	125 (375)	250 (500)
 *	D	62		125		187
 *
 * (figures in brackets are actual measured timings)
 *
 * However, we also need to take care of the read/write active and
 * recovery timings:
 *
 *			Read	Write
 *  	Mode	Active	-- Recovery --	Cycle	IOMD type
 *	MW0	215	50	215	480	A
 *	MW1	80	50	50	150	C
 *	MW2	70	25	25	120	C
 */
Linus Torvalds's avatar
Linus Torvalds committed
347
static int
Martin Dalecki's avatar
Martin Dalecki committed
348
icside_config_if(struct ata_device *drive, int xfer_mode)
Linus Torvalds's avatar
Linus Torvalds committed
349
{
Martin Dalecki's avatar
Martin Dalecki committed
350
	int on = 0, cycle_time = 0, use_dma_info = 0;
Linus Torvalds's avatar
Linus Torvalds committed
351 352

	switch (xfer_mode) {
Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356
	case XFER_MW_DMA_2: cycle_time = 250; use_dma_info = 1;	break;
	case XFER_MW_DMA_1: cycle_time = 250; use_dma_info = 1;	break;
	case XFER_MW_DMA_0: cycle_time = 480;			break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
357

Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362 363
	/*
	 * If we're going to be doing MW_DMA_1 or MW_DMA_2, we should
	 * take care to note the values in the ID...
	 */
	if (use_dma_info && drive->id->eide_dma_time > cycle_time)
		cycle_time = drive->id->eide_dma_time;
Linus Torvalds's avatar
Linus Torvalds committed
364

Linus Torvalds's avatar
Linus Torvalds committed
365
	drive->drive_data = cycle_time;
Linus Torvalds's avatar
Linus Torvalds committed
366

Linus Torvalds's avatar
Linus Torvalds committed
367
	if (cycle_time && ide_config_drive_speed(drive, xfer_mode) == 0)
Martin Dalecki's avatar
Martin Dalecki committed
368
		on = 1;
Linus Torvalds's avatar
Linus Torvalds committed
369 370 371
	else
		drive->drive_data = 480;

Martin Dalecki's avatar
Martin Dalecki committed
372 373
	printk("%s: %02x selected (peak %dMB/s)\n", drive->name,
		xfer_mode, 2000 / drive->drive_data);
Linus Torvalds's avatar
Linus Torvalds committed
374

Linus Torvalds's avatar
Linus Torvalds committed
375
	drive->current_speed = xfer_mode;
Linus Torvalds's avatar
Linus Torvalds committed
376

Martin Dalecki's avatar
Martin Dalecki committed
377
	return on;
Linus Torvalds's avatar
Linus Torvalds committed
378 379
}

Martin Dalecki's avatar
Martin Dalecki committed
380
static int icside_set_speed(struct ata_device *drive, u8 speed)
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384
{
	return icside_config_if(drive, speed);
}

Martin Dalecki's avatar
Martin Dalecki committed
385
static void icside_dma_enable(struct ata_device *drive, int on, int verbose)
Linus Torvalds's avatar
Linus Torvalds committed
386
{
Martin Dalecki's avatar
Martin Dalecki committed
387 388 389 390 391 392
	if (!on) {
		if (verbose)
			printk("%s: DMA disabled\n", drive->name);
#ifdef CONFIG_BLK_DEV_IDE_TCQ
		udma_tcq_enable(drive, 0);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
393
	}
Martin Dalecki's avatar
Martin Dalecki committed
394

Martin Dalecki's avatar
Martin Dalecki committed
395 396 397 398 399 400
	/*
	 * We don't need any bouncing.  Yes, this looks the
	 * wrong way around, but it is correct.
	 */
	blk_queue_bounce_limit(&drive->queue, BLK_BOUNCE_ANY);
	drive->using_dma = on;
Linus Torvalds's avatar
Linus Torvalds committed
401

Martin Dalecki's avatar
Martin Dalecki committed
402 403 404 405 406
#ifdef CONFIG_CLK_DEV_IDE_TCQ_DEFAULT
	if (on)
		udma_tcq_enable(drive, 1);
#endif
}
Linus Torvalds's avatar
Linus Torvalds committed
407

Martin Dalecki's avatar
Martin Dalecki committed
408
static int icside_dma_check(struct ata_device *drive, int map)
Linus Torvalds's avatar
Linus Torvalds committed
409 410
{
	struct hd_driveid *id = drive->id;
Martin Dalecki's avatar
Martin Dalecki committed
411
	struct ata_channel *ch = drive->channel;
Linus Torvalds's avatar
Linus Torvalds committed
412
	int xfer_mode = XFER_PIO_2;
Martin Dalecki's avatar
Martin Dalecki committed
413
	int on;
Linus Torvalds's avatar
Linus Torvalds committed
414

Martin Dalecki's avatar
Martin Dalecki committed
415
	if (!id || !(id->capability & 1) || !ch->autodma)
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
		goto out;

	/*
	 * Enable DMA on any drive that has multiword DMA
	 */
	if (id->field_valid & 2) {
		if (id->dma_mword & 4) {
			xfer_mode = XFER_MW_DMA_2;
		} else if (id->dma_mword & 2) {
			xfer_mode = XFER_MW_DMA_1;
		} else if (id->dma_mword & 1) {
			xfer_mode = XFER_MW_DMA_0;
		}
	}

out:
Martin Dalecki's avatar
Martin Dalecki committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	on = icside_config_if(drive, xfer_mode);

	icside_dma_enable(drive, on, 0);

	return 0;
}

static int icside_dma_stop(struct ata_device *drive)
{
	struct ata_channel *ch = drive->channel;

	disable_dma(ch->hw.dma);
	icside_destroy_dmatable(drive);

	return get_dma_residue(ch->hw.dma) != 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
448

Martin Dalecki's avatar
Martin Dalecki committed
449
static void icside_dma_start(struct ata_device *drive, struct request *rq)
Martin Dalecki's avatar
Martin Dalecki committed
450 451 452
{
	struct ata_channel *ch = drive->channel;

Martin Dalecki's avatar
Martin Dalecki committed
453
	/* We can not enable DMA on both channels simultaneously. */
Martin Dalecki's avatar
Martin Dalecki committed
454 455 456 457 458 459 460 461 462 463 464 465
	BUG_ON(dma_channel_active(ch->hw.dma));
	enable_dma(ch->hw.dma);
}

/*
 * dma_intr() is the handler for disk read/write DMA interrupts
 */
static ide_startstop_t icside_dmaintr(struct ata_device *drive, struct request *rq)
{
	int dma_stat;

	dma_stat = icside_dma_stop(drive);
Martin Dalecki's avatar
Martin Dalecki committed
466
	if (ata_status(drive, DRIVE_READY, drive->bad_wstat | DRQ_STAT)) {
Martin Dalecki's avatar
Martin Dalecki committed
467 468
		if (!dma_stat) {
			__ide_end_request(drive, rq, 1, rq->nr_sectors);
Martin Dalecki's avatar
Martin Dalecki committed
469
			return ATA_OP_FINISHED;
Martin Dalecki's avatar
Martin Dalecki committed
470 471 472 473
		}
		printk("%s: dma_intr: bad DMA status (dma_stat=%x)\n",
		       drive->name, dma_stat);
	}
Martin Dalecki's avatar
Martin Dalecki committed
474
	return ata_error(drive, rq, __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
475 476
}

Linus Torvalds's avatar
Linus Torvalds committed
477
static int
Martin Dalecki's avatar
Martin Dalecki committed
478 479
icside_dma_common(struct ata_device *drive, struct request *rq,
		  unsigned int dma_mode)
Linus Torvalds's avatar
Linus Torvalds committed
480
{
Martin Dalecki's avatar
Martin Dalecki committed
481 482 483 484 485 486 487 488
	struct ata_channel *ch = drive->channel;
	unsigned int count;

	/*
	 * We can not enable DMA on both channels.
	 */
	BUG_ON(dma_channel_active(ch->hw.dma));

Martin Dalecki's avatar
Martin Dalecki committed
489
	count = ch->sg_nents = ide_build_sglist(drive, rq);
Martin Dalecki's avatar
Martin Dalecki committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	if (!count)
		return 1;

	/*
	 * Route the DMA signals to to this channel.
	 */
	outb(ch->select_data, ch->config_data);

	/*
	 * Select the correct timing for this drive.
	 */
	set_dma_speed(ch->hw.dma, drive->drive_data);

	/*
	 * Tell the DMA engine about the SG table and
	 * data direction.
	 */
	set_dma_sg(ch->hw.dma, ch->sg_table, count);
	set_dma_mode(ch->hw.dma, dma_mode);
Linus Torvalds's avatar
Linus Torvalds committed
509

Martin Dalecki's avatar
Martin Dalecki committed
510 511 512
	return 0;
}

Martin Dalecki's avatar
Martin Dalecki committed
513
static int icside_dma_init(struct ata_device *drive, struct request *rq)
Martin Dalecki's avatar
Martin Dalecki committed
514 515
{
	struct ata_channel *ch = drive->channel;
Martin Dalecki's avatar
Martin Dalecki committed
516
	u8 int cmd;
Martin Dalecki's avatar
Martin Dalecki committed
517 518

	if (icside_dma_common(drive, rq, DMA_MODE_WRITE))
Martin Dalecki's avatar
Martin Dalecki committed
519
		return ATA_OP_FINISHED;
Martin Dalecki's avatar
Martin Dalecki committed
520 521

	if (drive->type != ATA_DISK)
Martin Dalecki's avatar
Martin Dalecki committed
522
		return ATA_OP_CONTINUES;
Martin Dalecki's avatar
Martin Dalecki committed
523

Martin Dalecki's avatar
Martin Dalecki committed
524
	ata_set_handler(drive, icside_dmaintr, WAIT_CMD, NULL);
Martin Dalecki's avatar
Martin Dalecki committed
525

Martin Dalecki's avatar
Martin Dalecki committed
526
	if ((rq->flags & REQ_SPECIAL) && drive->addressing == 1) {
Martin Dalecki's avatar
Martin Dalecki committed
527
		struct ata_taskfile *args = rq->special;
Martin Dalecki's avatar
Martin Dalecki committed
528
		cmd = args->cmd;
Martin Dalecki's avatar
Martin Dalecki committed
529
	} else if (drive->addressing) {
Martin Dalecki's avatar
Martin Dalecki committed
530
		cmd = rq_data_dir(rq) == WRITE ? WIN_WRITEDMA_EXT : WIN_READDMA_EXT;
Martin Dalecki's avatar
Martin Dalecki committed
531
	} else {
Martin Dalecki's avatar
Martin Dalecki committed
532
		cmd = rq_data_dir(rq) == WRITE ? WIN_WRITEDMA : WIN_READDMA;
Linus Torvalds's avatar
Linus Torvalds committed
533
	}
Martin Dalecki's avatar
Martin Dalecki committed
534 535 536
	OUT_BYTE(cmd, IDE_COMMAND_REG);

	enable_dma(ch->hw.dma);
Martin Dalecki's avatar
Martin Dalecki committed
537

Martin Dalecki's avatar
Martin Dalecki committed
538
	return ATA_OP_CONTINUES;
Linus Torvalds's avatar
Linus Torvalds committed
539 540
}

Martin Dalecki's avatar
Martin Dalecki committed
541 542 543 544 545 546 547 548 549 550 551 552
static int icside_irq_status(struct ata_device *drive)
{
	struct ata_channel *ch = drive->channel;
	struct icside_state *state = ch->hw.priv;

	return inb(state->irq_port +
		   (ch->unit ?
			ICS_ARCIN_V6_INTRSTAT_2 :
			ICS_ARCIN_V6_INTRSTAT_1)) & 1;
}

static void icside_dma_timeout(struct ata_device *drive)
Linus Torvalds's avatar
Linus Torvalds committed
553
{
Martin Dalecki's avatar
Martin Dalecki committed
554
	printk(KERN_ERR "ATA: %s: UDMA timeout occured:", drive->name);
Martin Dalecki's avatar
Martin Dalecki committed
555
	ata_status(drive, 0, 0);
556
	ata_dump(drive, NULL, "UDMA timeout");
Martin Dalecki's avatar
Martin Dalecki committed
557 558 559 560 561 562
}

static void icside_irq_lost(struct ata_device *drive)
{
	printk(KERN_ERR "ATA: %s: IRQ lost\n", drive->name);
}
Linus Torvalds's avatar
Linus Torvalds committed
563

Martin Dalecki's avatar
Martin Dalecki committed
564 565 566 567 568 569 570 571 572 573 574
static int icside_setup_dma(struct ata_channel *ch)
{
	int autodma = 0;

#ifdef CONFIG_IDEDMA_ICS_AUTO
	autodma = 1;
#endif

	printk("    %s: SG-DMA", ch->name);

	ch->sg_table = kmalloc(sizeof(struct scatterlist) * NR_ENTRIES,
Linus Torvalds's avatar
Linus Torvalds committed
575
				 GFP_KERNEL);
Martin Dalecki's avatar
Martin Dalecki committed
576
	if (!ch->sg_table)
Linus Torvalds's avatar
Linus Torvalds committed
577 578
		goto failed;

Martin Dalecki's avatar
Martin Dalecki committed
579 580 581
	ch->dmatable_cpu    = NULL;
	ch->dmatable_dma    = 0;
	ch->speedproc       = icside_set_speed;
Martin Dalecki's avatar
Martin Dalecki committed
582
	ch->udma_setup	    = icside_dma_check;
Martin Dalecki's avatar
Martin Dalecki committed
583 584 585
	ch->udma_enable     = icside_dma_enable;
	ch->udma_start      = icside_dma_start;
	ch->udma_stop       = icside_dma_stop;
Martin Dalecki's avatar
Martin Dalecki committed
586
	ch->udma_init	    = icside_dma_init;
Martin Dalecki's avatar
Martin Dalecki committed
587 588 589 590 591 592
	ch->udma_irq_status = icside_irq_status;
	ch->udma_timeout    = icside_dma_timeout;
	ch->udma_irq_lost   = icside_irq_lost;
	ch->autodma         = autodma;

	printk(" capable%s\n", autodma ? ", auto-enable" : "");
Linus Torvalds's avatar
Linus Torvalds committed
593 594 595 596

	return 1;

failed:
Martin Dalecki's avatar
Martin Dalecki committed
597
	printk(" disabled, unable to allocate DMA table\n");
Linus Torvalds's avatar
Linus Torvalds committed
598 599
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
600

Martin Dalecki's avatar
Martin Dalecki committed
601
void ide_release_dma(struct ata_channel *ch)
Linus Torvalds's avatar
Linus Torvalds committed
602
{
Martin Dalecki's avatar
Martin Dalecki committed
603 604 605
	if (ch->sg_table) {
		kfree(ch->sg_table);
		ch->sg_table = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
606 607
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
608 609
#endif

Martin Dalecki's avatar
Martin Dalecki committed
610
static struct ata_channel *icside_find_hwif(unsigned long dataport)
Linus Torvalds's avatar
Linus Torvalds committed
611
{
Martin Dalecki's avatar
Martin Dalecki committed
612
	struct ata_channel *ch;
Linus Torvalds's avatar
Linus Torvalds committed
613 614 615
	int index;

	for (index = 0; index < MAX_HWIFS; ++index) {
Martin Dalecki's avatar
Martin Dalecki committed
616 617
		ch = &ide_hwifs[index];
		if (ch->io_ports[IDE_DATA_OFFSET] == (ide_ioreg_t)dataport)
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621
			goto found;
	}

	for (index = 0; index < MAX_HWIFS; ++index) {
Martin Dalecki's avatar
Martin Dalecki committed
622 623
		ch = &ide_hwifs[index];
		if (!ch->io_ports[IDE_DATA_OFFSET])
Linus Torvalds's avatar
Linus Torvalds committed
624 625 626 627 628
			goto found;
	}

	return NULL;
found:
Martin Dalecki's avatar
Martin Dalecki committed
629
	return ch;
Linus Torvalds's avatar
Linus Torvalds committed
630 631
}

Martin Dalecki's avatar
Martin Dalecki committed
632
static struct ata_channel *
Linus Torvalds's avatar
Linus Torvalds committed
633 634 635
icside_setup(unsigned long base, struct cardinfo *info, int irq)
{
	unsigned long port = base + info->dataoffset;
Martin Dalecki's avatar
Martin Dalecki committed
636
	struct ata_channel *ch;
Linus Torvalds's avatar
Linus Torvalds committed
637

Martin Dalecki's avatar
Martin Dalecki committed
638 639
	ch = icside_find_hwif(base);
	if (ch) {
Linus Torvalds's avatar
Linus Torvalds committed
640 641
		int i;

Martin Dalecki's avatar
Martin Dalecki committed
642
		memset(&ch->hw, 0, sizeof(hw_regs_t));
Linus Torvalds's avatar
Linus Torvalds committed
643 644

		for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
Martin Dalecki's avatar
Martin Dalecki committed
645 646
			ch->hw.io_ports[i] = (ide_ioreg_t)port;
			ch->io_ports[i] = (ide_ioreg_t)port;
Linus Torvalds's avatar
Linus Torvalds committed
647 648
			port += 1 << info->stepping;
		}
Martin Dalecki's avatar
Martin Dalecki committed
649 650 651 652 653 654 655
		ch->hw.io_ports[IDE_CONTROL_OFFSET] = base + info->ctrloffset;
		ch->io_ports[IDE_CONTROL_OFFSET] = base + info->ctrloffset;
		ch->hw.irq  = irq;
		ch->irq     = irq;
		ch->hw.dma  = NO_DMA;
		ch->noprobe = 0;
		ch->chipset = ide_acorn;
Linus Torvalds's avatar
Linus Torvalds committed
656 657
	}

Martin Dalecki's avatar
Martin Dalecki committed
658
	return ch;
Linus Torvalds's avatar
Linus Torvalds committed
659 660
}

Martin Dalecki's avatar
Martin Dalecki committed
661
static int __init icside_register_v5(struct expansion_card *ec)
Linus Torvalds's avatar
Linus Torvalds committed
662 663
{
	unsigned long slot_port;
Martin Dalecki's avatar
Martin Dalecki committed
664
	struct ata_channel *ch;
Linus Torvalds's avatar
Linus Torvalds committed
665 666 667 668 669 670 671 672 673 674 675 676 677

	slot_port = ecard_address(ec, ECARD_MEMC, 0);

	ec->irqaddr  = (unsigned char *)ioaddr(slot_port + ICS_ARCIN_V5_INTRSTAT);
	ec->irqmask  = 1;
	ec->irq_data = (void *)slot_port;
	ec->ops      = (expansioncard_ops_t *)&icside_ops_arcin_v5;

	/*
	 * Be on the safe side - disable interrupts
	 */
	inb(slot_port + ICS_ARCIN_V5_INTROFFSET);

Martin Dalecki's avatar
Martin Dalecki committed
678
	ch = icside_setup(slot_port, &icside_cardinfo_v5, ec->irq);
Linus Torvalds's avatar
Linus Torvalds committed
679

Martin Dalecki's avatar
Martin Dalecki committed
680
	return ch ? 0 : -1;
Linus Torvalds's avatar
Linus Torvalds committed
681 682
}

Martin Dalecki's avatar
Martin Dalecki committed
683
static int __init icside_register_v6(struct expansion_card *ec)
Linus Torvalds's avatar
Linus Torvalds committed
684 685
{
	unsigned long slot_port, port;
Martin Dalecki's avatar
Martin Dalecki committed
686 687 688
	struct icside_state *state;
	struct ata_channel *ch0, *ch1;
	unsigned int sel = 0;
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

	slot_port = ecard_address(ec, ECARD_IOC, ECARD_FAST);
	port      = ecard_address(ec, ECARD_EASI, ECARD_FAST);

	if (port == 0)
		port = slot_port;
	else
		sel = 1 << 5;

	outb(sel, slot_port);

	/*
	 * Be on the safe side - disable interrupts
	 */
	inb(port + ICS_ARCIN_V6_INTROFFSET_1);
	inb(port + ICS_ARCIN_V6_INTROFFSET_2);

Martin Dalecki's avatar
Martin Dalecki committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	/*
	 * Find and register the interfaces.
	 */
	ch0 = icside_setup(port, &icside_cardinfo_v6_1, ec->irq);
	ch1 = icside_setup(port, &icside_cardinfo_v6_2, ec->irq);

	if (!ch0 || !ch1)
		return -ENODEV;

	state = kmalloc(sizeof(struct icside_state), GFP_KERNEL);
	if (!state)
		return -ENOMEM;

	state->channel    = 0;
	state->enabled    = 0;
	state->irq_port   = port;

	ec->irq_data      = state;
	ec->ops           = (expansioncard_ops_t *)&icside_ops_arcin_v6;

	ch0->maskproc     = icside_maskproc;
	ch0->unit         = 0;
	ch0->hw.priv      = state;
	ch0->config_data  = slot_port;
	ch0->select_data  = sel;
	ch0->hw.dma       = ec->dma;

	ch1->maskproc     = icside_maskproc;
	ch1->unit         = 1;
	ch1->hw.priv      = state;
	ch1->config_data  = slot_port;
	ch1->select_data  = sel | 1;
	ch1->hw.dma       = ec->dma;
Linus Torvalds's avatar
Linus Torvalds committed
739 740

#ifdef CONFIG_BLK_DEV_IDEDMA_ICS
Martin Dalecki's avatar
Martin Dalecki committed
741 742 743
	if (ec->dma != NO_DMA && !request_dma(ec->dma, ch0->name)) {
		icside_setup_dma(ch0);
		icside_setup_dma(ch1);
Linus Torvalds's avatar
Linus Torvalds committed
744
	}
Linus Torvalds's avatar
Linus Torvalds committed
745
#endif
Martin Dalecki's avatar
Martin Dalecki committed
746
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
}

int __init icside_init(void)
{
	ecard_startfind ();

	do {
		struct expansion_card *ec;
		int result;

		ec = ecard_find(0, icside_cids);
		if (ec == NULL)
			break;

		ecard_claim(ec);

		switch (icside_identifyif(ec)) {
		case ics_if_arcin_v5:
Martin Dalecki's avatar
Martin Dalecki committed
765
			result = icside_register_v5(ec);
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768
			break;

		case ics_if_arcin_v6:
Martin Dalecki's avatar
Martin Dalecki committed
769
			result = icside_register_v6(ec);
Linus Torvalds's avatar
Linus Torvalds committed
770 771 772 773 774 775 776 777 778 779 780 781 782
			break;

		default:
			result = -1;
			break;
		}

		if (result)
			ecard_release(ec);
	} while (1);

	return 0;
}